diff --git a/graphics/src/widget/slider.rs b/graphics/src/widget/slider.rs index da8b5a86..99f0a098 100644 --- a/graphics/src/widget/slider.rs +++ b/graphics/src/widget/slider.rs @@ -19,17 +19,13 @@ pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; pub type Slider<'a, T, Message, Backend> = iced_native::Slider<'a, T, Message, Renderer>; -const HANDLE_HEIGHT: f32 = 22.0; - impl slider::Renderer for Renderer where B: Backend, { type Style = Box; - fn height(&self) -> u32 { - 30 - } + const DEFAULT_HEIGHT: u16 = 22; fn draw( &mut self, @@ -81,16 +77,18 @@ where let (range_start, range_end) = range.into_inner(); - let (handle_width, handle_height, handle_border_radius) = - match style.handle.shape { - HandleShape::Circle { radius } => { - (f32::from(radius * 2), f32::from(radius * 2), radius) - } - HandleShape::Rectangle { - width, - border_radius, - } => (f32::from(width), HANDLE_HEIGHT, border_radius), - }; + let (handle_width, handle_height, handle_border_radius) = match style + .handle + .shape + { + HandleShape::Circle { radius } => { + (f32::from(radius * 2), f32::from(radius * 2), radius) + } + HandleShape::Rectangle { + width, + border_radius, + } => (f32::from(width), f32::from(bounds.height), border_radius), + }; let handle_offset = (bounds.width - handle_width) * ((value - range_start) / (range_end - range_start).max(1.0)); diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 07f79319..2aee0da1 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -197,9 +197,7 @@ impl checkbox::Renderer for Null { impl slider::Renderer for Null { type Style = (); - fn height(&self) -> u32 { - 30 - } + const DEFAULT_HEIGHT: u16 = 30; fn draw( &mut self, diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 70f2b6ac..c49053f1 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -46,6 +46,7 @@ pub struct Slider<'a, T, Message, Renderer: self::Renderer> { on_change: Box Message>, on_release: Option, width: Length, + height: u16, style: Renderer::Style, } @@ -95,6 +96,7 @@ where on_change: Box::new(on_change), on_release: None, width: Length::Fill, + height: Renderer::DEFAULT_HEIGHT, style: Renderer::Style::default(), } } @@ -120,6 +122,14 @@ where self } + /// Sets the height of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html + pub fn height(mut self, height: u16) -> Self { + self.height = height; + self + } + /// Sets the style of the [`Slider`]. /// /// [`Slider`]: struct.Slider.html @@ -171,12 +181,11 @@ where fn layout( &self, - renderer: &Renderer, + _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits - .width(self.width) - .height(Length::Units(renderer.height() as u16)); + let limits = + limits.width(self.width).height(Length::Units(self.height)); let size = limits.resolve(Size::ZERO); @@ -281,10 +290,10 @@ pub trait Renderer: crate::Renderer { /// The style supported by this renderer. type Style: Default; - /// Returns the height of the [`Slider`]. + /// The default height of a [`Slider`]. /// /// [`Slider`]: struct.Slider.html - fn height(&self) -> u32; + const DEFAULT_HEIGHT: u16; /// Draws a [`Slider`]. ///