summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorLibravatar Sebastian Zivota <sebastian.zivota@mailbox.org>2020-06-02 20:34:13 +0200
committerLibravatar Sebastian Zivota <sebastian.zivota@mailbox.org>2020-06-11 00:18:24 +0200
commitc3643eaf6d90dc8b60c0b762200bf1f8097cdfe9 (patch)
tree6651e8db73202abf3482dedbfd9b65e28319f488 /native
parentbaa1389f71e419383429eb10ac051b1aaf0a3e26 (diff)
downloadiced-c3643eaf6d90dc8b60c0b762200bf1f8097cdfe9.tar.gz
iced-c3643eaf6d90dc8b60c0b762200bf1f8097cdfe9.tar.bz2
iced-c3643eaf6d90dc8b60c0b762200bf1f8097cdfe9.zip
Add `step` member to slider widgets
Both the native and the web slider now have a member `step` to control the least possible change of the slider's value. It defaults to 1.0 for all sliders and can be adjusted with the step method.
Diffstat (limited to 'native')
-rw-r--r--native/src/widget/slider.rs20
1 files changed, 16 insertions, 4 deletions
diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs
index 753a49fe..8670628c 100644
--- a/native/src/widget/slider.rs
+++ b/native/src/widget/slider.rs
@@ -16,6 +16,8 @@ use std::{hash::Hash, ops::RangeInclusive};
///
/// A [`Slider`] will try to fill the horizontal space of its container.
///
+/// The step size defaults to 1.0.
+///
/// [`Slider`]: struct.Slider.html
///
/// # Example
@@ -38,6 +40,7 @@ use std::{hash::Hash, ops::RangeInclusive};
pub struct Slider<'a, Message, Renderer: self::Renderer> {
state: &'a mut State,
range: RangeInclusive<f32>,
+ step: f32,
value: f32,
on_change: Box<dyn Fn(f32) -> Message>,
on_release: Option<Message>,
@@ -71,6 +74,7 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> {
state,
value: value.max(*range.start()).min(*range.end()),
range,
+ step: 1.0,
on_change: Box::new(on_change),
on_release: None,
width: Length::Fill,
@@ -106,6 +110,14 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> {
self.style = style.into();
self
}
+
+ /// Sets the step size of the [`Slider`].
+ ///
+ /// [`Slider`]: struct.Slider.html
+ pub fn step(mut self, step: f32) -> Self {
+ self.step = step;
+ self
+ }
}
/// The local state of a [`Slider`].
@@ -164,16 +176,16 @@ where
) {
let mut change = || {
let bounds = layout.bounds();
-
if cursor_position.x <= bounds.x {
messages.push((self.on_change)(*self.range.start()));
} else if cursor_position.x >= bounds.x + bounds.width {
messages.push((self.on_change)(*self.range.end()));
} else {
let percent = (cursor_position.x - bounds.x) / bounds.width;
- let value = (self.range.end() - self.range.start()) * percent
- + self.range.start();
-
+ let steps = (percent * (self.range.end() - self.range.start())
+ / self.step)
+ .round();
+ let value = steps * self.step + self.range.start();
messages.push((self.on_change)(value));
}
};