Skip to content

Commit 0e2545f

Browse files
committed
Address code review feedback
1 parent 8b74155 commit 0e2545f

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

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

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

1515
//! A toggle switch widget.
1616
17-
use crate::kurbo::{Circle, Point, Rect, RoundedRect, Size};
17+
use crate::kurbo::{Circle, Point, Rect, RoundedRect, Shape, Size};
1818
use crate::piet::{FontBuilder, Text, TextLayout, TextLayoutBuilder};
1919
use crate::piet::{LinearGradient, RenderContext, UnitPoint};
2020
use crate::theme;
2121
use crate::widget::Align;
2222
use crate::{
23-
BaseState, BoxConstraints, Env, Event, EventCtx, LayoutCtx, PaintCtx, UpdateCtx, Widget,
23+
BaseState, BoxConstraints, Env, Event, EventCtx, LayoutCtx, LocalizedString, PaintCtx,
24+
UpdateCtx, Widget,
2425
};
2526

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-
27+
/// A switch that toggles a boolean.
3528
#[derive(Debug, Clone, Default)]
36-
pub struct SwitchRaw {
29+
pub struct Switch {
3730
knob_pos: Point,
3831
knob_hovered: bool,
3932
}
4033

41-
impl SwitchRaw {
34+
impl Switch {
35+
pub fn new() -> impl Widget<bool> {
36+
Align::vertical(UnitPoint::CENTER, Self::default())
37+
}
38+
4239
fn knob_hit_test(&self, knob_width: f64, mouse_pos: Point) -> bool {
4340
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
41+
knob_circle.winding(mouse_pos) > 0
4842
}
4943

5044
fn paint_label(
@@ -58,8 +52,14 @@ impl SwitchRaw {
5852
) {
5953
let font_name = env.get(theme::FONT_NAME);
6054
let font_size = env.get(theme::TEXT_SIZE_NORMAL);
55+
let switch_height = env.get(theme::BORDERED_WIDGET_HEIGHT);
6156

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

6464
let font = paint_ctx
6565
.text()
@@ -70,7 +70,7 @@ impl SwitchRaw {
7070

7171
let text_layout = paint_ctx
7272
.text()
73-
.new_text_layout(&font, label)
73+
.new_text_layout(&font, &localized_label)
7474
.unwrap()
7575
.build()
7676
.unwrap();
@@ -79,12 +79,12 @@ impl SwitchRaw {
7979
Point::ORIGIN,
8080
Size::new(
8181
(base_state.size().width - text_layout.width()).max(0.0),
82-
base_state.size().height + (font_size * 1.2) / 2.,
82+
switch_height + (font_size * 1.2) / 2.,
8383
),
8484
));
8585

8686
// adjust label position
87-
origin.y = origin.y.min(base_state.size().height);
87+
origin.y = origin.y.min(switch_height);
8888

8989
if *data {
9090
origin.x = switch_padding * 2.
@@ -96,7 +96,7 @@ impl SwitchRaw {
9696
}
9797
}
9898

99-
impl Widget<bool> for SwitchRaw {
99+
impl Widget<bool> for Switch {
100100
fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &bool, env: &Env) {
101101
let switch_padding = 3.;
102102
let switch_height = env.get(theme::BORDERED_WIDGET_HEIGHT);
@@ -110,6 +110,7 @@ impl Widget<bool> for SwitchRaw {
110110
);
111111

112112
// paint different background for on and off state
113+
// todo: make color configurable
113114
let background_gradient = if *data {
114115
LinearGradient::new(
115116
UnitPoint::TOP,
@@ -210,11 +211,7 @@ impl Widget<bool> for SwitchRaw {
210211
if ctx.is_active() {
211212
ctx.set_active(false);
212213
if ctx.is_hot() {
213-
if *data {
214-
*data = false;
215-
} else {
216-
*data = true;
217-
}
214+
*data = !*data
218215
}
219216
ctx.invalidate();
220217
}
@@ -224,11 +221,7 @@ impl Widget<bool> for SwitchRaw {
224221
// todo: animate dragging of knob
225222
}
226223
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-
}
224+
self.knob_hovered = self.knob_hit_test(knob_size, mouse.pos)
232225
}
233226
ctx.invalidate();
234227
}

0 commit comments

Comments
 (0)