Skip to content

Commit

Permalink
Animate position relative to widget
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 8, 2024
1 parent ff1c567 commit c8ff9eb
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions crates/egui/src/widgets/theme_switch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,17 @@ mod painting {
.find(|button| &button.option.value == value)
.map(|button| button.center)
{
let pos = animate_active_indicator_position(ui, space.response.id, pos);
let pos = animate_active_indicator_position(ui, space.response.id, space.rect.min, pos);
ui.painter().circle(pos, space.radius, fill, Stroke::NONE);
}
}

fn animate_active_indicator_position(ui: &mut Ui, id: Id, pos: Pos2) -> Pos2 {
fn animate_active_indicator_position(ui: &mut Ui, id: Id, anchor: Pos2, pos: Pos2) -> Pos2 {
let animation_time = ui.style().animation_time;
let x = ui.ctx().animate_value_with_time(id, pos.x, animation_time);
// Animate the relative position to prevent
// animating the active indicator when the switch itself is moved around.
let x = pos.x - anchor.x;
let x = anchor.x + ui.ctx().animate_value_with_time(id, x, animation_time);
pos2(x, pos.y)
}

Expand Down

0 comments on commit c8ff9eb

Please sign in to comment.