From 65eb218d3d7ba52b2869a586a1480eeb3c8f84e4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Nov 2019 13:47:20 +0100 Subject: Move widgets from `core` to `native` and `web` Also made fields private and improved `Renderer` traits. --- native/src/widget/slider.rs | 109 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 91 insertions(+), 18 deletions(-) (limited to 'native/src/widget/slider.rs') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 3a998c40..f91d3ac5 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -4,12 +4,79 @@ //! //! [`Slider`]: struct.Slider.html //! [`State`]: struct.State.html -use std::hash::Hash; -use crate::input::{mouse, ButtonState}; -use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget}; +use crate::{ + input::{mouse, ButtonState}, + layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, + Widget, +}; -pub use iced_core::slider::*; +use std::{hash::Hash, ops::RangeInclusive}; + +pub struct Slider<'a, Message> { + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: Box Message>, + width: Length, +} + +impl<'a, Message> Slider<'a, Message> { + /// Creates a new [`Slider`]. + /// + /// It expects: + /// * the local [`State`] of the [`Slider`] + /// * an inclusive range of possible values + /// * the current value of the [`Slider`] + /// * a function that will be called when the [`Slider`] is dragged. + /// It receives the new value of the [`Slider`] and must produce a + /// `Message`. + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + pub fn new( + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: F, + ) -> Self + where + F: 'static + Fn(f32) -> Message, + { + Slider { + state, + value: value.max(*range.start()).min(*range.end()), + range, + on_change: Box::new(on_change), + width: Length::Fill, + } + } + + /// Sets the width of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } +} + +/// The local state of a [`Slider`]. +/// +/// [`Slider`]: struct.Slider.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + is_dragging: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } +} impl<'a, Message, Renderer> Widget for Slider<'a, Message> where @@ -24,7 +91,13 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - renderer.layout(&self, limits) + let limits = limits + .width(self.width) + .height(Length::Units(renderer.height() as u16)); + + let size = limits.resolve(Size::ZERO); + + layout::Node::new(size) } fn on_event( @@ -81,7 +154,13 @@ where layout: Layout<'_>, cursor_position: Point, ) -> Renderer::Output { - renderer.draw(&self, layout, cursor_position) + renderer.draw( + layout.bounds(), + cursor_position, + self.range.clone(), + self.value, + self.state.is_dragging, + ) } fn hash_layout(&self, state: &mut Hasher) { @@ -97,15 +176,7 @@ where /// [`Slider`]: struct.Slider.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer { - /// Creates a [`Node`] for the provided [`Radio`]. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Radio`]: struct.Radio.html - fn layout( - &self, - slider: &Slider<'_, Message>, - limits: &layout::Limits, - ) -> layout::Node; + fn height(&self) -> u32; /// Draws a [`Slider`]. /// @@ -119,11 +190,13 @@ pub trait Renderer: crate::Renderer { /// [`Slider`]: struct.Slider.html /// [`State`]: struct.State.html /// [`Class`]: enum.Class.html - fn draw( + fn draw( &mut self, - slider: &Slider<'_, Message>, - layout: Layout<'_>, + bounds: Rectangle, cursor_position: Point, + range: RangeInclusive, + value: f32, + is_dragging: bool, ) -> Self::Output; } -- cgit From a7dba612f03e58d7bd9527499d893987986b347c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 19:36:57 +0100 Subject: Write docs for `iced` and `iced_native` --- native/src/widget/slider.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'native/src/widget/slider.rs') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 113cc2a4..fe503c34 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -13,6 +13,28 @@ use crate::{ use std::{hash::Hash, ops::RangeInclusive}; +/// An horizontal bar and a handle that selects a single value from a range of +/// values. +/// +/// A [`Slider`] will try to fill the horizontal space of its container. +/// +/// [`Slider`]: struct.Slider.html +/// +/// # Example +/// ``` +/// # use iced_native::{slider, Slider}; +/// # +/// pub enum Message { +/// SliderChanged(f32), +/// } +/// +/// let state = &mut slider::State::new(); +/// let value = 50.0; +/// +/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); +/// ``` +/// +/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) pub struct Slider<'a, Message> { state: &'a mut State, range: RangeInclusive, @@ -180,6 +202,9 @@ where /// [`Slider`]: struct.Slider.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer { + /// Returns the height of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html fn height(&self) -> u32; /// Draws a [`Slider`]. -- cgit From d136b7ce648cde0dcdcc5388d8cb82b3e7e0fc58 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 21:16:40 +0100 Subject: Uncomment missing debug implementations rule --- native/src/widget/slider.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'native/src/widget/slider.rs') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index fe503c34..49cf0368 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -35,6 +35,7 @@ use std::{hash::Hash, ops::RangeInclusive}; /// ``` /// /// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) +#[allow(missing_debug_implementations)] pub struct Slider<'a, Message> { state: &'a mut State, range: RangeInclusive, -- cgit From 048909b45dfecef73bfacf3b5aa67462470ccca2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 22:13:54 +0100 Subject: Fix `Button` example in `iced_native` --- native/src/widget/slider.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'native/src/widget/slider.rs') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 49cf0368..f07ea7cd 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -4,7 +4,6 @@ //! //! [`Slider`]: struct.Slider.html //! [`State`]: struct.State.html - use crate::{ input::{mouse, ButtonState}, layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, -- cgit