Skip to content

Commit

Permalink
Switch widget visual fixes (#150)
Browse files Browse the repository at this point in the history
* Fix switch visuals for better UX

* Comment out print statements
  • Loading branch information
giannissc authored Nov 23, 2023
1 parent d40a94f commit 9b84691
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Pod {
had_active || hot_changed
}
Event::TargetedAccessibilityAction(action) => {
println!("TODO: {:?}", action);
// println!("TODO: {:?}", action);
self.state
.sub_tree
.may_contain(&Id::try_from_accesskit(action.target).unwrap())
Expand Down
4 changes: 2 additions & 2 deletions src/widget/linear_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Widget for LinearLayout {
if index < child_count - 1 {
major_used += self.spacing;

println!("insert spacing {}", self.spacing);
// println!("insert spacing {}", self.spacing);
}
max_minor = max_minor.max(self.axis.minor(size));
}
Expand All @@ -102,7 +102,7 @@ impl Widget for LinearLayout {

fn paint(&mut self, cx: &mut PaintCx, builder: &mut SceneBuilder) {
for child in &mut self.children {
println!("paint child!");
// println!("paint child!");
child.paint(cx, builder);
}
}
Expand Down
43 changes: 31 additions & 12 deletions src/widget/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Switch {
is_on: bool,
is_dragging: bool,
knob_position: Point,
start_position: Point,
}

impl Switch {
Expand All @@ -46,6 +47,7 @@ impl Switch {
} else {
Point::new(OFF_POS, KNOB_DIAMETER / 2. + SWITCH_PADDING)
},
start_position: Point::ZERO,
}
}

Expand All @@ -66,9 +68,10 @@ const OFF_POS: f64 = KNOB_DIAMETER / 2.0 + SWITCH_PADDING;
impl Widget for Switch {
fn event(&mut self, cx: &mut EventCx, event: &Event) {
match event {
Event::MouseDown(_) => {
Event::MouseDown(mouse) => {
cx.set_active(true);
cx.request_paint();
self.start_position.x = mouse.pos.x;
}
Event::MouseUp(_) => {
if self.is_dragging {
Expand All @@ -87,9 +90,14 @@ impl Widget for Switch {
}
Event::MouseMove(mouse) => {
if cx.is_active() {
self.knob_position.x = mouse.pos.x.clamp(OFF_POS, ON_POS);
let delta = mouse.pos.x - self.start_position.x;
self.knob_position.x = if self.is_on {
(ON_POS + delta).clamp(OFF_POS, ON_POS)
} else {
(OFF_POS + delta).clamp(OFF_POS, ON_POS)
};

self.is_dragging = true;
println!("Mouse Move{:?}", self.knob_position);
}
cx.request_paint();
}
Expand Down Expand Up @@ -145,18 +153,29 @@ impl Widget for Switch {
fill_color(builder, &background_rect, background_on_state);

// Paint the Switch knob
println!("Paint: {:?}", self.knob_position);
let knob_color = if self.is_dragging || cx.is_hot() {
Color::SLATE_GRAY
let knob_border_color = Color::DIM_GRAY;

let knob_color = if cx.is_hot() && !cx.is_active() {
let r = Color::SLATE_GRAY.r + 10;
let g = Color::SLATE_GRAY.g + 10;
let b = Color::SLATE_GRAY.b + 10;
Color { r, g, b, a: 255 }
} else if cx.is_active() {
let r = Color::SLATE_GRAY.r - 10;
let g = Color::SLATE_GRAY.g - 10;
let b = Color::SLATE_GRAY.b - 10;
Color { r, g, b, a: 255 }
} else {
Color::LIGHT_SLATE_GRAY
Color::SLATE_GRAY
};
let knob_border_color = Color::DIM_GRAY;
let mut knob_size = KNOB_DIAMETER / 2.0;

if cx.is_active() {
knob_size += 1.0;
}
let knob_size = if cx.is_hot() && !cx.is_active() {
KNOB_DIAMETER / 2. + 1.
} else if cx.is_active() {
KNOB_DIAMETER / 2. - 1.
} else {
KNOB_DIAMETER / 2.
};

let knob_circle = Circle::new(self.knob_position, knob_size);
fill_color(builder, &knob_circle, knob_color);
Expand Down

0 comments on commit 9b84691

Please sign in to comment.