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

Toggle switch now becomes blue when ON #6494

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions crates/re_ui/src/toggle_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ fn toggle_switch_ui(ui: &mut egui::Ui, height: f32, on: &mut bool) -> egui::Resp
let how_on = ui.ctx().animate_bool(response.id, *on);
let visuals = ui.style().interact(&response);
let expanded_rect = visual_rect.expand(visuals.expansion);
let fg_fill = visuals.bg_fill;
let bg_fill = visuals.text_color();
let fg_fill_off = visuals.bg_fill;
let fg_fill_on = egui::Color32::from_rgba_premultiplied(0, 128, 255, 255);
let fg_fill = egui::Color32::from_rgba_premultiplied(
lerp_u8(fg_fill_off.r(), fg_fill_on.r(), how_on),
lerp_u8(fg_fill_off.g(), fg_fill_on.g(), how_on),
lerp_u8(fg_fill_off.b(), fg_fill_on.b(), how_on),
lerp_u8(fg_fill_off.a(), fg_fill_on.a(), how_on),
);
abey79 marked this conversation as resolved.
Show resolved Hide resolved
let bg_fill_off = visuals.text_color();

let rounding = 0.5 * expanded_rect.height();
ui.painter()
.rect(expanded_rect, rounding, bg_fill, egui::Stroke::NONE);
.rect_filled(expanded_rect, rounding, bg_fill_off);
let circle_x = egui::lerp(
(expanded_rect.left() + rounding)..=(expanded_rect.right() - rounding),
how_on,
Expand All @@ -31,7 +39,7 @@ fn toggle_switch_ui(ui: &mut egui::Ui, height: f32, on: &mut bool) -> egui::Resp
let circle_center = egui::pos2(circle_x, expanded_rect.center().y);
let circle_radius = 0.3 * expanded_rect.height();
ui.painter()
.circle(circle_center, circle_radius, fg_fill, egui::Stroke::NONE);
.circle_filled(circle_center, circle_radius, fg_fill);
}

response
Expand All @@ -48,3 +56,7 @@ fn toggle_switch_ui(ui: &mut egui::Ui, height: f32, on: &mut bool) -> egui::Resp
pub fn toggle_switch(height: f32, on: &mut bool) -> impl egui::Widget + '_ {
move |ui: &mut egui::Ui| toggle_switch_ui(ui, height, on)
}

fn lerp_u8(a: u8, b: u8, t: f32) -> u8 {
(a as f32 + t * (b as f32 - a as f32)).round() as u8
}
Loading