Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch widget visual fixes #150

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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