diff --git a/CHANGELOG.md b/CHANGELOG.md index d0b0ef51c6..49e44c1966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ You can find its changes [documented below](#060---2020-06-01). - Re-export `druid_shell::Scalable` under `druid` namespace. ([#1075] by [@ForLoveOfCats]) - `TextBox` now supports ctrl and shift hotkeys. ([#1076] by [@vkahl]) - Selection text color to textbox. ([#1093] by [@sysint64]) -- `BoxConstraints::UNBOUNDED` constant. ([#1126] by [@danieldulaney]) +- `BoxConstraints::UNBOUNDED` constant. ([#1126] by [@danieldulaney]) - Close requests from the shell can now be intercepted ([#1118] by [@jneem]) - The Lens derive now supports an `ignore` attribute. ([#1133] by [@jneem]) - `request_update` in `EventCtx`. ([#1128] by [@raphlinus]) @@ -50,6 +50,7 @@ You can find its changes [documented below](#060---2020-06-01). - Key and KeyOrValue derive Clone ([#1119] by [@rjwittams]) - Allow submit_command from the layout method in Widgets ([#1119] by [@rjwittams]) - Allow derivation of lenses for generic types ([#1120]) by [@rjwittams]) +- Switch widget: Toggle animation being window refresh rate dependent ([#1145] by [@ForLoveOfCats]) ### Visual @@ -392,6 +393,7 @@ Last release without a changelog :( [#1126]: https://github.com/linebender/druid/pull/1120 [#1128]: https://github.com/linebender/druid/pull/1128 [#1133]: https://github.com/linebender/druid/pull/1133 +[#1145]: https://github.com/linebender/druid/pull/1145 [Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master [0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0 diff --git a/druid/src/widget/switch.rs b/druid/src/widget/switch.rs index af1f136eb1..094533df5e 100644 --- a/druid/src/widget/switch.rs +++ b/druid/src/widget/switch.rs @@ -14,6 +14,8 @@ //! A toggle switch widget. +use std::time::Duration; + use crate::kurbo::{Circle, Point, Rect, Shape, Size}; use crate::piet::{ FontBuilder, LinearGradient, RenderContext, Text, TextLayout, TextLayoutBuilder, UnitPoint, @@ -24,6 +26,7 @@ use crate::{ Widget, }; +const SWITCH_CHANGE_TIME: f64 = 0.2; const SWITCH_PADDING: f64 = 3.; const SWITCH_WIDTH_RATIO: f64 = 2.75; @@ -156,7 +159,8 @@ impl Widget for Switch { } fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &bool, env: &Env) { - if let LifeCycle::AnimFrame(_) = event { + if let LifeCycle::AnimFrame(interval) = event { + let delta = Duration::from_nanos(*interval).as_secs_f64(); let switch_height = env.get(theme::BORDERED_WIDGET_HEIGHT); let switch_width = switch_height * SWITCH_WIDTH_RATIO; let knob_size = switch_height - 2. * SWITCH_PADDING; @@ -165,8 +169,13 @@ impl Widget for Switch { // move knob to right position depending on the value if self.animation_in_progress { - let delta = if *data { 2. } else { -2. }; - self.knob_pos.x += delta; + let change_time = if *data { + SWITCH_CHANGE_TIME + } else { + -SWITCH_CHANGE_TIME + }; + let change = (switch_width / change_time) * delta; + self.knob_pos.x = (self.knob_pos.x + change).min(on_pos).max(off_pos); if self.knob_pos.x > off_pos && self.knob_pos.x < on_pos { ctx.request_anim_frame();