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

widget: slider: Add stepping functionality #1875

Merged
merged 9 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Robert Wittams
Jaap Aarts
Maximilian Köstler
Bruno Dupuis
Christopher Noel Hesse
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ You can find its changes [documented below](#070---2021-01-01).
- x11: Implement primary_clipboard ([#1867] by [@psychon])
- x11: Set WM_CLASS property ([#1868] by [@psychon])
- Expose `RawWindowHandle` for `WindowHandle` under the `raw-win-handle` feature ([#1828] by [@djeedai])
- Widget/Slider: Add stepping functionality ([#1875] by [@raymanfx])

### Changed

Expand Down
9 changes: 7 additions & 2 deletions druid/examples/widget_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ fn ui_builder() -> impl Widget<AppData> {
))
.with_child(label_widget(
Flex::column()
.with_child(Slider::new().lens(AppData::progressbar))
.with_child(
Slider::new()
.with_range(0.05, 0.95)
.with_step(0.10)
.lens(AppData::progressbar),
)
.with_spacer(4.0)
.with_child(Label::new(|data: &AppData, _: &_| {
format!("{:3.0}%", data.progressbar * 100.0)
format!("{:3.2}%", data.progressbar * 100.)
})),
"Slider",
))
Expand Down
41 changes: 39 additions & 2 deletions druid/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::kurbo::{Circle, Shape};
use crate::widget::prelude::*;
use crate::{theme, LinearGradient, Point, Rect, UnitPoint};
use tracing::{instrument, trace};
use tracing::{instrument, trace, warn};

const TRACK_THICKNESS: f64 = 4.0;
const BORDER_WIDTH: f64 = 2.0;
Expand All @@ -31,6 +31,7 @@ const KNOB_STROKE_WIDTH: f64 = 2.0;
pub struct Slider {
min: f64,
max: f64,
step: Option<f64>,
knob_pos: Point,
knob_hovered: bool,
x_offset: f64,
Expand All @@ -42,6 +43,7 @@ impl Slider {
Slider {
min: 0.,
max: 1.,
step: None,
knob_pos: Default::default(),
knob_hovered: Default::default(),
x_offset: Default::default(),
Expand All @@ -56,6 +58,24 @@ impl Slider {
self.max = max;
self
}

/// Builder-style method to set the stepping.
///
/// The default step size is `0.0` (smooth).
pub fn with_step(mut self, step: f64) -> Self {
maan2003 marked this conversation as resolved.
Show resolved Hide resolved
if step < 0.0 {
warn!("bad stepping (must be positive): {}", step);
return self;
}
self.step = if step > 0.0 {
Some(step)
} else {
// A stepping value of 0.0 would yield an infinite amount of steps.
// Enforce no stepping instead.
None
};
Comment on lines +66 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change it, but just for posterity I'd have just written:

Suggested change
if step < 0.0 {
warn!("bad stepping (must be positive): {}", step);
return self;
}
self.step = if step > 0.0 {
Some(step)
} else {
// A stepping value of 0.0 would yield an infinite amount of steps.
// Enforce no stepping instead.
None
};
if step < 0.0 {
warn!("bad stepping (must be positive): {}", step);
} else {
self.step = Some(step);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That has a drawback IMO: you'd actually need to check for step <= 0 since otherwise we would divide by zero elsewhere iirc.

We decided to allow 0 as a valid step value though, indicating smooth stepping (no intervals) - but we have to explicitly model this as None.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets change that step > 0.0 to step != 0.0 to make it more explicit

self
}
}

impl Slider {
Expand All @@ -68,7 +88,24 @@ impl Slider {
let scalar = ((mouse_x + self.x_offset - knob_width / 2.) / (slider_width - knob_width))
.max(0.0)
.min(1.0);
self.min + scalar * (self.max - self.min)
let mut value = self.min + scalar * (self.max - self.min);
if let Some(step) = self.step {
let max_step_value = ((self.max - self.min) / step).floor() * step + self.min;
if value > max_step_value {
// edge case: make sure max is reachable
let left_dist = value - max_step_value;
let right_dist = self.max - value;
value = if left_dist < right_dist {
max_step_value
} else {
self.max
};
} else {
// snap to discrete intervals
value = (((value - self.min) / step).round() * step + self.min).min(self.max);
raymanfx marked this conversation as resolved.
Show resolved Hide resolved
}
}
value
}

fn normalize(&self, data: f64) -> f64 {
Expand Down