summaryrefslogtreecommitdiffstats
path: root/native/src/widget/slider.rs
diff options
context:
space:
mode:
authorLibravatar Casper Storm <casper.storm@lich.io>2022-12-13 10:05:52 +0100
committerLibravatar Casper Storm <casper.storm@lich.io>2022-12-13 10:05:52 +0100
commitbb4161c1aec0a2a76de39ff2e5ed65f7acbad471 (patch)
tree92e2660cb19fe4ff06d7ec32812e53f730674679 /native/src/widget/slider.rs
parent2e6d90f141217bad83eacd392562c13d7485881f (diff)
downloadiced-bb4161c1aec0a2a76de39ff2e5ed65f7acbad471.tar.gz
iced-bb4161c1aec0a2a76de39ff2e5ed65f7acbad471.tar.bz2
iced-bb4161c1aec0a2a76de39ff2e5ed65f7acbad471.zip
Split vertical orientation into VerticalSlider
Diffstat (limited to '')
-rw-r--r--native/src/widget/slider.rs199
1 files changed, 41 insertions, 158 deletions
diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs
index bf3383d9..87030a4d 100644
--- a/native/src/widget/slider.rs
+++ b/native/src/widget/slider.rs
@@ -16,9 +16,10 @@ use std::ops::RangeInclusive;
pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
-/// A bar and a handle that selects a single value from a range of values.
+/// An horizontal bar and a handle that selects a single value from a range of
+/// values.
///
-/// A [`Slider`] will try to fill the space of its container, based on its orientation.
+/// A [`Slider`] will try to fill the horizontal space of its container.
///
/// The [`Slider`] range of numeric values is generic and its step size defaults
/// to 1 unit.
@@ -52,9 +53,8 @@ where
value: T,
on_change: Box<dyn Fn(T) -> Message + 'a>,
on_release: Option<Message>,
- width: Option<Length>,
- height: Option<Length>,
- orientation: Orientation,
+ width: Length,
+ height: u16,
style: <Renderer::Theme as StyleSheet>::Style,
}
@@ -65,6 +65,9 @@ where
Renderer: crate::Renderer,
Renderer::Theme: StyleSheet,
{
+ /// The default height of a [`Slider`].
+ pub const DEFAULT_HEIGHT: u16 = 22;
+
/// Creates a new [`Slider`].
///
/// It expects:
@@ -95,9 +98,8 @@ where
step: T::from(1),
on_change: Box::new(on_change),
on_release: None,
- width: None,
- height: None,
- orientation: Default::default(),
+ width: Length::Fill,
+ height: Self::DEFAULT_HEIGHT,
style: Default::default(),
}
}
@@ -115,13 +117,13 @@ where
/// Sets the width of the [`Slider`].
pub fn width(mut self, width: Length) -> Self {
- self.width = Some(width);
+ self.width = width;
self
}
/// Sets the height of the [`Slider`].
- pub fn height(mut self, height: Length) -> Self {
- self.height = Some(height);
+ pub fn height(mut self, height: u16) -> Self {
+ self.height = height;
self
}
@@ -139,12 +141,6 @@ where
self.step = step;
self
}
-
- /// Sets the orientation of the [`Slider`].
- pub fn orientation(mut self, orientation: Orientation) -> Self {
- self.orientation = orientation;
- self
- }
}
impl<'a, T, Message, Renderer> Widget<Message, Renderer>
@@ -164,17 +160,11 @@ where
}
fn width(&self) -> Length {
- match self.orientation {
- Orientation::Horizontal => self.width.unwrap_or(Length::Fill),
- Orientation::Vertical => Length::Shrink,
- }
+ self.width
}
fn height(&self) -> Length {
- match self.orientation {
- Orientation::Horizontal => Length::Shrink,
- Orientation::Vertical => self.height.unwrap_or(Length::Fill),
- }
+ Length::Shrink
}
fn layout(
@@ -182,14 +172,9 @@ where
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- let width = self
- .width
- .unwrap_or_else(|| self.orientation.default_width());
- let height = self
- .height
- .unwrap_or_else(|| self.orientation.default_height());
-
- let limits = limits.width(width).height(height);
+ let limits =
+ limits.width(self.width).height(Length::Units(self.height));
+
let size = limits.resolve(Size::ZERO);
layout::Node::new(size)
@@ -216,7 +201,6 @@ where
self.step,
self.on_change.as_ref(),
&self.on_release,
- self.orientation,
)
}
@@ -239,7 +223,6 @@ where
&self.range,
theme,
&self.style,
- self.orientation,
)
}
@@ -287,7 +270,6 @@ pub fn update<Message, T>(
step: T,
on_change: &dyn Fn(T) -> Message,
on_release: &Option<Message>,
- orientation: Orientation,
) -> event::Status
where
T: Copy + Into<f64> + num_traits::FromPrimitive,
@@ -297,40 +279,17 @@ where
let mut change = || {
let bounds = layout.bounds();
-
- let cursor_below_bounds = match orientation {
- Orientation::Horizontal => cursor_position.x <= bounds.x,
- Orientation::Vertical => {
- cursor_position.y >= bounds.y + bounds.height
- }
- };
-
- let cursor_above_bounds = match orientation {
- Orientation::Horizontal => {
- cursor_position.x >= bounds.x + bounds.width
- }
- Orientation::Vertical => cursor_position.y <= bounds.y,
- };
-
- let new_value = if cursor_below_bounds {
+ let new_value = if cursor_position.x <= bounds.x {
*range.start()
- } else if cursor_above_bounds {
+ } else if cursor_position.x >= bounds.x + bounds.width {
*range.end()
} else {
let step = step.into();
let start = (*range.start()).into();
let end = (*range.end()).into();
- let percent = match orientation {
- Orientation::Horizontal => {
- f64::from(cursor_position.x - bounds.x)
- / f64::from(bounds.width)
- }
- Orientation::Vertical => {
- 1.00 - (f64::from(cursor_position.y - bounds.y)
- / f64::from(bounds.height))
- }
- };
+ let percent = f64::from(cursor_position.x - bounds.x)
+ / f64::from(bounds.width);
let steps = (percent * (end - start) / step).round();
let value = steps * step + start;
@@ -395,7 +354,6 @@ pub fn draw<T, R>(
range: &RangeInclusive<T>,
style_sheet: &dyn StyleSheet<Style = <R::Theme as StyleSheet>::Style>,
style: &<R::Theme as StyleSheet>::Style,
- orientation: Orientation,
) where
T: Into<f64> + Copy,
R: crate::Renderer,
@@ -412,26 +370,15 @@ pub fn draw<T, R>(
style_sheet.active(style)
};
- let rail = match orientation {
- Orientation::Horizontal => bounds.y + (bounds.height / 2.0).round(),
- Orientation::Vertical => bounds.x + (bounds.width / 2.0).round(),
- };
+ let rail_y = bounds.y + (bounds.height / 2.0).round();
renderer.fill_quad(
renderer::Quad {
- bounds: match orientation {
- Orientation::Horizontal => Rectangle {
- x: bounds.x,
- y: rail - 1.0,
- width: bounds.width,
- height: 2.0,
- },
- Orientation::Vertical => Rectangle {
- x: rail - 1.0,
- y: bounds.y,
- width: 2.0,
- height: bounds.height,
- },
+ bounds: Rectangle {
+ x: bounds.x,
+ y: rail_y - 1.0,
+ width: bounds.width,
+ height: 2.0,
},
border_radius: 0.0.into(),
border_width: 0.0,
@@ -442,19 +389,11 @@ pub fn draw<T, R>(
renderer.fill_quad(
renderer::Quad {
- bounds: match orientation {
- Orientation::Horizontal => Rectangle {
- x: bounds.x,
- y: rail + 1.0,
- width: bounds.width,
- height: 2.0,
- },
- Orientation::Vertical => Rectangle {
- x: rail + 1.0,
- y: bounds.y,
- width: 2.0,
- height: bounds.height,
- },
+ bounds: Rectangle {
+ x: bounds.x,
+ y: rail_y + 1.0,
+ width: bounds.width,
+ height: 2.0,
},
border_radius: 0.0.into(),
border_width: 0.0,
@@ -471,14 +410,7 @@ pub fn draw<T, R>(
HandleShape::Rectangle {
width,
border_radius,
- } => {
- let handle_height = match orientation {
- Orientation::Horizontal => bounds.height,
- Orientation::Vertical => bounds.width,
- };
-
- (f32::from(width), handle_height, border_radius)
- }
+ } => (f32::from(width), bounds.height, border_radius),
};
let value = value.into() as f32;
@@ -491,33 +423,17 @@ pub fn draw<T, R>(
let handle_offset = if range_start >= range_end {
0.0
} else {
- match orientation {
- Orientation::Horizontal => {
- bounds.width * (value - range_start) / (range_end - range_start)
- - handle_width / 2.0
- }
- Orientation::Vertical => {
- bounds.height * (value - range_end) / (range_start - range_end)
- - handle_width / 2.0
- }
- }
+ bounds.width * (value - range_start) / (range_end - range_start)
+ - handle_width / 2.0
};
renderer.fill_quad(
renderer::Quad {
- bounds: match orientation {
- Orientation::Horizontal => Rectangle {
- x: bounds.x + handle_offset.round(),
- y: rail - handle_height / 2.0,
- width: handle_width,
- height: handle_height,
- },
- Orientation::Vertical => Rectangle {
- x: rail - (handle_height / 2.0),
- y: bounds.y + handle_offset.round(),
- width: handle_height,
- height: handle_width,
- },
+ bounds: Rectangle {
+ x: bounds.x + handle_offset.round(),
+ y: rail_y - handle_height / 2.0,
+ width: handle_width,
+ height: handle_height,
},
border_radius: handle_border_radius.into(),
border_width: style.handle.border_width,
@@ -557,36 +473,3 @@ impl State {
State::default()
}
}
-
-/// The orientation of a [`Slider`].
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
-pub enum Orientation {
- #[default]
- /// Default orientation.
- /// Will fill the horizontal space of its container.
- Horizontal,
- /// Vertical orientation.
- /// Will fill the vertical space of its container.
- Vertical,
-}
-
-impl Orientation {
- /// The default height of a [`Slider`] in horizontal orientation.
- pub const DEFAULT_HEIGHT: Length = Length::Units(22);
- /// The default width of a [`Slider`] in vertical orientation.
- pub const DEFAULT_WIDTH: Length = Length::Units(22);
-
- fn default_height(&self) -> Length {
- match self {
- Orientation::Horizontal => Self::DEFAULT_HEIGHT,
- Orientation::Vertical => Length::Fill,
- }
- }
-
- fn default_width(&self) -> Length {
- match self {
- Orientation::Horizontal => Length::Fill,
- Orientation::Vertical => Self::DEFAULT_WIDTH,
- }
- }
-}