Skip to content

Commit 79446be

Browse files
committed
Address code review feedback
1 parent 8b74155 commit 79446be

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

examples/switch.rs renamed to druid/examples/switch.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ fn build_widget() -> impl Widget<DemoState> {
2626
let switch = LensWrap::new(Switch::new(), lenses::demo_state::value);
2727
let switch_label = Label::new("Setting label");
2828

29-
row.add_child(Padding::uniform(5.0, switch_label), 0.0);
30-
row.add_child(Padding::uniform(5.0, switch), 0.0);
29+
row.add_child(Padding::new(5.0, switch_label), 0.0);
30+
row.add_child(Padding::new(5.0, switch), 0.0);
3131

32-
col.add_child(Padding::uniform(5.0, row), 1.0);
32+
col.add_child(Padding::new(5.0, row), 1.0);
3333
col
3434
}
3535

druid/src/menu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub enum MenuEntry<T> {
135135
/// A `MenuItem` always has a title (a [`LocalizedString`]) as well a [`Command`],
136136
/// that is sent to the application when the item is selected.
137137
///
138-
/// In additon, other properties can be set during construction, such as whether
138+
/// In addition, other properties can be set during construction, such as whether
139139
/// the item is selected (checked), or enabled, or if it has a hotkey.
140140
///
141141
/// [`LocalizedString`]: ../struct.LocalizedString.html

druid/src/widget/label.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<T: Data> LabelText<T> {
133133
}
134134
}
135135

136-
/// Update the localization, if necesasry.
136+
/// Update the localization, if necessary.
137137
///
138138
/// Returns `true` if the string has changed.
139139
pub fn resolve(&mut self, data: &T, env: &Env) -> bool {

src/widget/switch.rs renamed to druid/src/widget/switch.rs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,32 @@
1414

1515
//! A toggle switch widget.
1616
17-
use crate::kurbo::{Circle, Point, Rect, RoundedRect, Size};
18-
use crate::piet::{FontBuilder, Text, TextLayout, TextLayoutBuilder};
19-
use crate::piet::{LinearGradient, RenderContext, UnitPoint};
17+
use crate::kurbo::{Circle, Point, Rect, RoundedRect, Shape, Size};
18+
use crate::piet::{
19+
FontBuilder, LinearGradient, RenderContext, Text, TextLayout, TextLayoutBuilder, UnitPoint,
20+
};
2021
use crate::theme;
2122
use crate::widget::Align;
2223
use crate::{
23-
BaseState, BoxConstraints, Env, Event, EventCtx, LayoutCtx, PaintCtx, UpdateCtx, Widget,
24+
BaseState, BoxConstraints, Env, Event, EventCtx, LayoutCtx, LocalizedString, PaintCtx,
25+
UpdateCtx, Widget,
2426
};
2527

26-
#[derive(Debug, Clone)]
27-
pub struct Switch;
28-
29-
impl Switch {
30-
pub fn new() -> impl Widget<bool> {
31-
Align::vertical(UnitPoint::CENTER, SwitchRaw::default())
32-
}
33-
}
34-
28+
/// A switch that toggles a boolean.
3529
#[derive(Debug, Clone, Default)]
36-
pub struct SwitchRaw {
30+
pub struct Switch {
3731
knob_pos: Point,
3832
knob_hovered: bool,
3933
}
4034

41-
impl SwitchRaw {
35+
impl Switch {
36+
pub fn new() -> impl Widget<bool> {
37+
Align::vertical(UnitPoint::CENTER, Self::default())
38+
}
39+
4240
fn knob_hit_test(&self, knob_width: f64, mouse_pos: Point) -> bool {
4341
let knob_circle = Circle::new(self.knob_pos, knob_width / 2.);
44-
if mouse_pos.distance(knob_circle.center) < knob_circle.radius {
45-
return true;
46-
}
47-
false
42+
knob_circle.winding(mouse_pos) > 0
4843
}
4944

5045
fn paint_label(
@@ -58,33 +53,37 @@ impl SwitchRaw {
5853
) {
5954
let font_name = env.get(theme::FONT_NAME);
6055
let font_size = env.get(theme::TEXT_SIZE_NORMAL);
56+
let switch_height = env.get(theme::BORDERED_WIDGET_HEIGHT);
6157

62-
let label = if *data { "ON" } else { "OFF" };
58+
let label: LocalizedString<&str> = if *data {
59+
LocalizedString::new("On")
60+
} else {
61+
LocalizedString::new("Off")
62+
};
63+
let localized_label = label.localized_str().to_uppercase();
6364

6465
let font = paint_ctx
6566
.text()
6667
.new_font_by_name(font_name, font_size)
67-
.unwrap()
6868
.build()
6969
.unwrap();
7070

7171
let text_layout = paint_ctx
7272
.text()
73-
.new_text_layout(&font, label)
74-
.unwrap()
73+
.new_text_layout(&font, &localized_label)
7574
.build()
7675
.unwrap();
7776

7877
let mut origin = UnitPoint::LEFT.resolve(Rect::from_origin_size(
7978
Point::ORIGIN,
8079
Size::new(
8180
(base_state.size().width - text_layout.width()).max(0.0),
82-
base_state.size().height + (font_size * 1.2) / 2.,
81+
switch_height + (font_size * 1.2) / 2.,
8382
),
8483
));
8584

8685
// adjust label position
87-
origin.y = origin.y.min(base_state.size().height);
86+
origin.y = origin.y.min(switch_height);
8887

8988
if *data {
9089
origin.x = switch_padding * 2.
@@ -96,7 +95,7 @@ impl SwitchRaw {
9695
}
9796
}
9897

99-
impl Widget<bool> for SwitchRaw {
98+
impl Widget<bool> for Switch {
10099
fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &bool, env: &Env) {
101100
let switch_padding = 3.;
102101
let switch_height = env.get(theme::BORDERED_WIDGET_HEIGHT);
@@ -110,6 +109,7 @@ impl Widget<bool> for SwitchRaw {
110109
);
111110

112111
// paint different background for on and off state
112+
// todo: make color configurable
113113
let background_gradient = if *data {
114114
LinearGradient::new(
115115
UnitPoint::TOP,
@@ -210,11 +210,7 @@ impl Widget<bool> for SwitchRaw {
210210
if ctx.is_active() {
211211
ctx.set_active(false);
212212
if ctx.is_hot() {
213-
if *data {
214-
*data = false;
215-
} else {
216-
*data = true;
217-
}
213+
*data = !*data
218214
}
219215
ctx.invalidate();
220216
}
@@ -224,11 +220,7 @@ impl Widget<bool> for SwitchRaw {
224220
// todo: animate dragging of knob
225221
}
226222
if ctx.is_hot() {
227-
if self.knob_hit_test(knob_size, mouse.pos) {
228-
self.knob_hovered = true
229-
} else {
230-
self.knob_hovered = false
231-
}
223+
self.knob_hovered = self.knob_hit_test(knob_size, mouse.pos)
232224
}
233225
ctx.invalidate();
234226
}

0 commit comments

Comments
 (0)