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

Fix switch widget toggle animation being window refresh rate dependent #1145

Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions druid/src/widget/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -156,7 +159,8 @@ impl Widget<bool> 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;
Expand All @@ -165,8 +169,13 @@ impl Widget<bool> 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();
Expand Down