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 stepper wraparound #721

Merged
merged 2 commits into from
Mar 20, 2020
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
1 change: 1 addition & 0 deletions druid/examples/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn build_widget(state: &Params) -> Box<dyn Widget<AppState>> {
Stepper::new()
.with_range(0.0, 1.0)
.with_step(0.1)
.with_wraparound(true)
.lens(DemoState::volume),
0.0,
);
Expand Down
53 changes: 33 additions & 20 deletions druid/src/widget/stepper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,38 @@ impl Stepper {

/// Set whether the stepper should wrap around the minimum/maximum values.
///
/// When wraparound is enabled incrementing above max behaves like this:
/// - if the previous value is < max it becomes max
/// - if the previous value is = max it becomes min
/// Same logic applies for decrementing
///
/// The default is `false`.
pub fn with_wraparound(mut self, wrap: bool) -> Self {
self.wrap = wrap;
self
}

fn change_value(&mut self, _ctx: &mut EventCtx, data: &mut f64, _env: &Env) {
// increase/decrease value depending on which button is currently active
let delta = if self.increase_active {
self.step
} else if self.decrease_active {
-1. * self.step
} else {
0.0
};

*data = (*data + delta).max(self.min).min(self.max);
fn increment(&mut self, data: &mut f64) {
let next = *data + self.step;
let was_greater = *data + EPSILON >= self.max;
let is_greater = next + EPSILON > self.max;
*data = match (self.wrap, was_greater, is_greater) {
(true, true, true) => self.min,
(true, false, true) => self.max,
(false, _, true) => self.max,
_ => next,
}
}

if self.wrap {
if (*data - self.min).abs() < EPSILON {
*data = self.max
} else {
*data = self.min
}
fn decrement(&mut self, data: &mut f64) {
let next = *data - self.step;
let was_less = *data - EPSILON <= self.min;
let is_less = next - EPSILON < self.min;
*data = match (self.wrap, was_less, is_less) {
(true, true, true) => self.max,
(true, false, true) => self.min,
(false, _, true) => self.min,
_ => next,
}
}
}
Expand Down Expand Up @@ -191,12 +199,12 @@ impl Widget<f64> for Stepper {

if mouse.pos.y > height / 2. {
self.decrease_active = true;
self.decrement(data);
} else {
self.increase_active = true;
self.increment(data);
}

self.change_value(ctx, data, env);

let delay = Instant::now() + STEPPER_REPEAT_DELAY;
self.timer_id = ctx.request_timer(delay);

Expand All @@ -212,7 +220,12 @@ impl Widget<f64> for Stepper {
ctx.request_paint();
}
Event::Timer(id) if *id == self.timer_id => {
self.change_value(ctx, data, env);
if self.increase_active {
self.increment(data);
}
if self.decrease_active {
self.decrement(data);
}
let delay = Instant::now() + STEPPER_REPEAT;
self.timer_id = ctx.request_timer(delay);
}
Expand Down