Add height method to Slider

This commit is contained in:
Héctor Ramón Jiménez 2020-08-01 08:18:52 +02:00
parent 72cfe85f7f
commit 9ba4cfd23f
3 changed files with 29 additions and 24 deletions

View File

@ -19,17 +19,13 @@ pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet};
pub type Slider<'a, T, Message, Backend> = pub type Slider<'a, T, Message, Backend> =
iced_native::Slider<'a, T, Message, Renderer<Backend>>; iced_native::Slider<'a, T, Message, Renderer<Backend>>;
const HANDLE_HEIGHT: f32 = 22.0;
impl<B> slider::Renderer for Renderer<B> impl<B> slider::Renderer for Renderer<B>
where where
B: Backend, B: Backend,
{ {
type Style = Box<dyn StyleSheet>; type Style = Box<dyn StyleSheet>;
fn height(&self) -> u32 { const DEFAULT_HEIGHT: u16 = 22;
30
}
fn draw( fn draw(
&mut self, &mut self,
@ -81,16 +77,18 @@ where
let (range_start, range_end) = range.into_inner(); let (range_start, range_end) = range.into_inner();
let (handle_width, handle_height, handle_border_radius) = let (handle_width, handle_height, handle_border_radius) = match style
match style.handle.shape { .handle
HandleShape::Circle { radius } => { .shape
(f32::from(radius * 2), f32::from(radius * 2), radius) {
} HandleShape::Circle { radius } => {
HandleShape::Rectangle { (f32::from(radius * 2), f32::from(radius * 2), radius)
width, }
border_radius, HandleShape::Rectangle {
} => (f32::from(width), HANDLE_HEIGHT, border_radius), width,
}; border_radius,
} => (f32::from(width), f32::from(bounds.height), border_radius),
};
let handle_offset = (bounds.width - handle_width) let handle_offset = (bounds.width - handle_width)
* ((value - range_start) / (range_end - range_start).max(1.0)); * ((value - range_start) / (range_end - range_start).max(1.0));

View File

@ -197,9 +197,7 @@ impl checkbox::Renderer for Null {
impl slider::Renderer for Null { impl slider::Renderer for Null {
type Style = (); type Style = ();
fn height(&self) -> u32 { const DEFAULT_HEIGHT: u16 = 30;
30
}
fn draw( fn draw(
&mut self, &mut self,

View File

@ -46,6 +46,7 @@ pub struct Slider<'a, T, Message, Renderer: self::Renderer> {
on_change: Box<dyn Fn(T) -> Message>, on_change: Box<dyn Fn(T) -> Message>,
on_release: Option<Message>, on_release: Option<Message>,
width: Length, width: Length,
height: u16,
style: Renderer::Style, style: Renderer::Style,
} }
@ -95,6 +96,7 @@ where
on_change: Box::new(on_change), on_change: Box::new(on_change),
on_release: None, on_release: None,
width: Length::Fill, width: Length::Fill,
height: Renderer::DEFAULT_HEIGHT,
style: Renderer::Style::default(), style: Renderer::Style::default(),
} }
} }
@ -120,6 +122,14 @@ where
self 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`]. /// Sets the style of the [`Slider`].
/// ///
/// [`Slider`]: struct.Slider.html /// [`Slider`]: struct.Slider.html
@ -171,12 +181,11 @@ where
fn layout( fn layout(
&self, &self,
renderer: &Renderer, _renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let limits = limits let limits =
.width(self.width) limits.width(self.width).height(Length::Units(self.height));
.height(Length::Units(renderer.height() as u16));
let size = limits.resolve(Size::ZERO); let size = limits.resolve(Size::ZERO);
@ -281,10 +290,10 @@ pub trait Renderer: crate::Renderer {
/// The style supported by this renderer. /// The style supported by this renderer.
type Style: Default; type Style: Default;
/// Returns the height of the [`Slider`]. /// The default height of a [`Slider`].
/// ///
/// [`Slider`]: struct.Slider.html /// [`Slider`]: struct.Slider.html
fn height(&self) -> u32; const DEFAULT_HEIGHT: u16;
/// Draws a [`Slider`]. /// Draws a [`Slider`].
/// ///