diff options
Diffstat (limited to '')
-rw-r--r-- | core/src/point.rs (renamed from src/point.rs) | 0 | ||||
-rw-r--r-- | core/src/rectangle.rs (renamed from src/rectangle.rs) | 0 | ||||
-rw-r--r-- | core/src/vector.rs (renamed from src/vector.rs) | 0 | ||||
-rw-r--r-- | native/src/element.rs (renamed from src/element.rs) | 44 | ||||
-rw-r--r-- | native/src/event.rs (renamed from src/event.rs) | 0 | ||||
-rw-r--r-- | native/src/hasher.rs (renamed from src/hasher.rs) | 0 | ||||
-rw-r--r-- | native/src/input.rs (renamed from src/input.rs) | 0 | ||||
-rw-r--r-- | native/src/input/button_state.rs (renamed from src/input/button_state.rs) | 0 | ||||
-rw-r--r-- | native/src/input/keyboard.rs (renamed from src/input/keyboard.rs) | 0 | ||||
-rw-r--r-- | native/src/input/keyboard/event.rs (renamed from src/input/keyboard/event.rs) | 0 | ||||
-rw-r--r-- | native/src/input/keyboard/key_code.rs (renamed from src/input/keyboard/key_code.rs) | 0 | ||||
-rw-r--r-- | native/src/input/mouse.rs (renamed from src/input/mouse.rs) | 0 | ||||
-rw-r--r-- | native/src/input/mouse/button.rs (renamed from src/input/mouse/button.rs) | 0 | ||||
-rw-r--r-- | native/src/input/mouse/event.rs (renamed from src/input/mouse/event.rs) | 0 | ||||
-rw-r--r-- | native/src/layout.rs (renamed from src/layout.rs) | 2 | ||||
-rw-r--r-- | native/src/mouse_cursor.rs (renamed from src/mouse_cursor.rs) | 0 | ||||
-rw-r--r-- | native/src/node.rs (renamed from src/node.rs) | 0 | ||||
-rw-r--r-- | native/src/renderer.rs (renamed from src/renderer.rs) | 11 | ||||
-rw-r--r-- | native/src/user_interface.rs (renamed from src/user_interface.rs) | 22 | ||||
-rw-r--r-- | native/src/widget.rs (renamed from src/widget.rs) | 14 | ||||
-rw-r--r-- | src/lib.rs | 229 | ||||
-rw-r--r-- | src/style.rs | 262 | ||||
-rw-r--r-- | src/widget/button.rs | 282 | ||||
-rw-r--r-- | src/widget/checkbox.rs | 203 | ||||
-rw-r--r-- | src/widget/column.rs | 224 | ||||
-rw-r--r-- | src/widget/image.rs | 178 | ||||
-rw-r--r-- | src/widget/panel.rs | 94 | ||||
-rw-r--r-- | src/widget/progress_bar.rs | 106 | ||||
-rw-r--r-- | src/widget/radio.rs | 211 | ||||
-rw-r--r-- | src/widget/row.rs | 219 | ||||
-rw-r--r-- | src/widget/slider.rs | 241 | ||||
-rw-r--r-- | src/widget/text.rs | 224 |
32 files changed, 47 insertions, 2519 deletions
diff --git a/src/point.rs b/core/src/point.rs index 183998dd..183998dd 100644 --- a/src/point.rs +++ b/core/src/point.rs diff --git a/src/rectangle.rs b/core/src/rectangle.rs index 95c2570c..95c2570c 100644 --- a/src/rectangle.rs +++ b/core/src/rectangle.rs diff --git a/src/vector.rs b/core/src/vector.rs index f45daab9..f45daab9 100644 --- a/src/vector.rs +++ b/core/src/vector.rs diff --git a/src/element.rs b/native/src/element.rs index 70d06f42..dd5ce621 100644 --- a/src/element.rs +++ b/native/src/element.rs @@ -1,7 +1,7 @@ use stretch::{geometry, result}; use crate::{ - renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, + renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, }; /// A generic [`Widget`]. @@ -87,7 +87,7 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { /// /// ``` /// # mod counter { - /// # use iced::{button, Button}; + /// # use iced_native::{button, Button}; /// # /// # #[derive(Debug, Clone, Copy)] /// # pub enum Message {} @@ -101,19 +101,21 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { /// # } /// # /// # mod iced_wgpu { - /// # use iced::{ - /// # button, MouseCursor, Node, Point, Rectangle, Style, + /// # use iced_native::{ + /// # button, Button, MouseCursor, Node, Point, Rectangle, Style, Layout /// # }; /// # pub struct Renderer; /// # /// # impl button::Renderer for Renderer { - /// # fn draw( + /// # fn node<Message>(&self, _button: &Button<'_, Message>) -> Node { + /// # Node::new(Style::default()) + /// # } + /// # + /// # fn draw<Message>( /// # &mut self, + /// # _button: &Button<'_, Message>, + /// # _layout: Layout<'_>, /// # _cursor_position: Point, - /// # _bounds: Rectangle, - /// # _state: &button::State, - /// # _label: &str, - /// # _class: button::Class, /// # ) -> MouseCursor { /// # MouseCursor::OutOfBounds /// # } @@ -130,7 +132,7 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { /// # pub enum Message { /// # Counter(usize, counter::Message) /// # } - /// use iced::{Element, Row}; + /// use iced_native::{Element, Row}; /// use iced_wgpu::Renderer; /// /// impl ManyCounters { @@ -210,20 +212,23 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { /// /// [`Element`]: struct.Element.html /// [`Renderer`]: trait.Renderer.html - pub fn explain( + pub fn explain<C: Into<Color>>( self, - color: Renderer::Color, + color: C, ) -> Element<'a, Message, Renderer> where Message: 'static, Renderer: 'a + renderer::Debugger, { Element { - widget: Box::new(Explain::new(self, color)), + widget: Box::new(Explain::new(self, color.into())), } } - pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { + pub(crate) fn compute_layout( + &self, + renderer: &mut Renderer, + ) -> result::Layout { let node = self.widget.node(renderer); node.0.compute_layout(geometry::Size::undefined()).unwrap() @@ -264,7 +269,7 @@ impl<'a, A, B, Renderer> Widget<B, Renderer> for Map<'a, A, B, Renderer> where A: Copy, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { self.widget.node(renderer) } @@ -306,7 +311,7 @@ where struct Explain<'a, Message, Renderer: renderer::Debugger> { element: Element<'a, Message, Renderer>, - color: Renderer::Color, + color: Color, } impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> @@ -324,10 +329,7 @@ impl<'a, Message, Renderer> Explain<'a, Message, Renderer> where Renderer: renderer::Debugger, { - fn new( - element: Element<'a, Message, Renderer>, - color: Renderer::Color, - ) -> Self { + fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self { Explain { element, color } } } @@ -337,7 +339,7 @@ impl<'a, Message, Renderer> Widget<Message, Renderer> where Renderer: renderer::Debugger, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { self.element.widget.node(renderer) } diff --git a/src/event.rs b/native/src/event.rs index 71f06006..71f06006 100644 --- a/src/event.rs +++ b/native/src/event.rs diff --git a/src/hasher.rs b/native/src/hasher.rs index 9f6aacce..9f6aacce 100644 --- a/src/hasher.rs +++ b/native/src/hasher.rs diff --git a/src/input.rs b/native/src/input.rs index 097fa730..097fa730 100644 --- a/src/input.rs +++ b/native/src/input.rs diff --git a/src/input/button_state.rs b/native/src/input/button_state.rs index e9dc05d7..e9dc05d7 100644 --- a/src/input/button_state.rs +++ b/native/src/input/button_state.rs diff --git a/src/input/keyboard.rs b/native/src/input/keyboard.rs index 57c24484..57c24484 100644 --- a/src/input/keyboard.rs +++ b/native/src/input/keyboard.rs diff --git a/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs index 8118f112..8118f112 100644 --- a/src/input/keyboard/event.rs +++ b/native/src/input/keyboard/event.rs diff --git a/src/input/keyboard/key_code.rs b/native/src/input/keyboard/key_code.rs index 207ddeac..207ddeac 100644 --- a/src/input/keyboard/key_code.rs +++ b/native/src/input/keyboard/key_code.rs diff --git a/src/input/mouse.rs b/native/src/input/mouse.rs index d37f5b96..d37f5b96 100644 --- a/src/input/mouse.rs +++ b/native/src/input/mouse.rs diff --git a/src/input/mouse/button.rs b/native/src/input/mouse/button.rs index 6320d701..6320d701 100644 --- a/src/input/mouse/button.rs +++ b/native/src/input/mouse/button.rs diff --git a/src/input/mouse/event.rs b/native/src/input/mouse/event.rs index 7b68208f..7b68208f 100644 --- a/src/input/mouse/event.rs +++ b/native/src/input/mouse/event.rs diff --git a/src/layout.rs b/native/src/layout.rs index de284a43..32630f35 100644 --- a/src/layout.rs +++ b/native/src/layout.rs @@ -12,7 +12,7 @@ use crate::{Point, Rectangle, Vector}; /// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event /// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw /// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct Layout<'a> { layout: &'a result::Layout, position: Point, diff --git a/src/mouse_cursor.rs b/native/src/mouse_cursor.rs index 4ef6361a..4ef6361a 100644 --- a/src/mouse_cursor.rs +++ b/native/src/mouse_cursor.rs diff --git a/src/node.rs b/native/src/node.rs index 1db10d7f..1db10d7f 100644 --- a/src/node.rs +++ b/native/src/node.rs diff --git a/src/renderer.rs b/native/src/renderer.rs index b445190b..2244f00b 100644 --- a/src/renderer.rs +++ b/native/src/renderer.rs @@ -17,19 +17,12 @@ //! [`text::Renderer`]: ../widget/text/trait.Renderer.html //! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html //! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html -use crate::Layout; +use crate::{Color, Layout}; /// A renderer able to graphically explain a [`Layout`]. /// /// [`Layout`]: ../struct.Layout.html pub trait Debugger { - /// The color type that will be used to configure the _explanation_. - /// - /// This is the type that will be asked in [`Element::explain`]. - /// - /// [`Element::explain`]: ../struct.Element.html#method.explain - type Color: Copy; - /// Explains the [`Layout`] of an [`Element`] for debugging purposes. /// /// This will be called when [`Element::explain`] has been used. It should @@ -41,5 +34,5 @@ pub trait Debugger { /// [`Layout`]: struct.Layout.html /// [`Element`]: struct.Element.html /// [`Element::explain`]: struct.Element.html#method.explain - fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); + fn explain(&mut self, layout: &Layout<'_>, color: Color); } diff --git a/src/user_interface.rs b/native/src/user_interface.rs index 2c7cbf82..4bfacb2e 100644 --- a/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -35,7 +35,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// is naive way to set up our application loop: /// /// ```no_run - /// use iced::{UserInterface, Cache}; + /// use iced_native::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -46,7 +46,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// # } /// # } /// # - /// # use iced::Column; + /// # use iced_native::Column; /// # /// # pub struct Counter; /// # @@ -69,7 +69,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// // Update and draw the user interface here... @@ -82,7 +82,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { pub fn build<E: Into<Element<'a, Message, Renderer>>>( root: E, cache: Cache, - renderer: &Renderer, + renderer: &mut Renderer, ) -> Self { let root = root.into(); @@ -118,7 +118,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// [the previous example](#example): /// /// ```no_run - /// use iced::{UserInterface, Cache}; + /// use iced_native::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -129,7 +129,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// # } /// # } /// # - /// # use iced::Column; + /// # use iced_native::Column; /// # /// # pub struct Counter; /// # @@ -153,7 +153,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// // Update the user interface @@ -203,7 +203,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// [completing the last example](#example-1): /// /// ```no_run - /// use iced::{UserInterface, Cache}; + /// use iced_native::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -214,7 +214,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// # } /// # } /// # - /// # use iced::Column; + /// # use iced_native::Column; /// # /// # pub struct Counter; /// # @@ -236,7 +236,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// let messages = user_interface.update(events.drain(..)); @@ -302,7 +302,7 @@ impl Cache { Cache { hash: hasher.finish(), - layout: root.compute_layout(&()), + layout: root.compute_layout(&mut ()), cursor_position: Point::new(0.0, 0.0), } } diff --git a/src/widget.rs b/native/src/widget.rs index 30606934..9b770454 100644 --- a/src/widget.rs +++ b/native/src/widget.rs @@ -15,7 +15,7 @@ //! module. Therefore, you can directly type: //! //! ``` -//! use iced::{button, Button, Widget}; +//! use iced_native::{button, Button, Widget}; //! ``` //! //! [`Widget`]: trait.Widget.html @@ -26,19 +26,25 @@ mod row; pub mod button; pub mod checkbox; pub mod image; -//pub mod progress_bar; pub mod radio; pub mod slider; pub mod text; +#[doc(no_inline)] pub use button::Button; +#[doc(no_inline)] pub use checkbox::Checkbox; +#[doc(no_inline)] pub use column::Column; +#[doc(no_inline)] pub use image::Image; -//pub use progress_bar::ProgressBar; +#[doc(no_inline)] pub use radio::Radio; +#[doc(no_inline)] pub use row::Row; +#[doc(no_inline)] pub use slider::Slider; +#[doc(no_inline)] pub use text::Text; use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; @@ -59,7 +65,7 @@ pub trait Widget<Message, Renderer>: std::fmt::Debug { /// [`Node`]: ../struct.Node.html /// [`Widget`]: trait.Widget.html /// [`Layout`]: ../struct.Layout.html - fn node(&self, renderer: &Renderer) -> Node; + fn node(&self, renderer: &mut Renderer) -> Node; /// Draws the [`Widget`] using the associated `Renderer`. /// @@ -1,229 +0,0 @@ -//! Iced is a renderer-agnostic GUI library focused on simplicity and -//! type-safety. Inspired by [Elm]. -//! -//! # Features -//! * Simple, easy-to-use, renderer-agnostic API -//! * Responsive, flexbox-based layouting -//! * Type-safe, reactive programming model -//! * Built-in widgets -//! * Custom widget support -//! -//! Check out the [repository] and the [examples] for more details! -//! -//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples -//! [repository]: https://github.com/hecrj/iced -//! -//! # Usage -//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces -//! into four different concepts: -//! -//! * __State__ — the state of your application -//! * __Messages__ — user interactions or meaningful events that you care -//! about -//! * __View logic__ — a way to display your __state__ as widgets that -//! may produce __messages__ on user interaction -//! * __Update logic__ — a way to react to __messages__ and update your -//! __state__ -//! -//! We can build something to see how this works! Let's say we want a simple counter -//! that can be incremented and decremented using two buttons. -//! -//! We start by modelling the __state__ of our application: -//! -//! ``` -//! use iced::button; -//! -//! struct Counter { -//! // The counter value -//! value: i32, -//! -//! // The local state of the two buttons -//! increment_button: button::State, -//! decrement_button: button::State, -//! } -//! ``` -//! -//! Next, we need to define the possible user interactions of our counter: -//! the button presses. These interactions are our __messages__: -//! -//! ``` -//! #[derive(Debug, Clone, Copy)] -//! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, -//! } -//! ``` -//! -//! Now, let's show the actual counter by putting it all together in our -//! __view logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! # -//! # mod iced_wgpu { -//! # use iced::{ -//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, -//! # MouseCursor, Node, Point, Rectangle, Style, -//! # }; -//! # -//! # pub struct Renderer {} -//! # -//! # impl button::Renderer for Renderer { -//! # fn draw( -//! # &mut self, -//! # _cursor_position: Point, -//! # _bounds: Rectangle, -//! # _state: &button::State, -//! # _label: &str, -//! # _class: button::Class, -//! # ) -> MouseCursor { -//! # MouseCursor::OutOfBounds -//! # } -//! # } -//! # -//! # impl text::Renderer<[f32; 4]> for Renderer { -//! # fn node(&self, style: Style, _content: &str, _size: Option<u16>) -> Node { -//! # Node::new(style) -//! # } -//! # -//! # fn draw( -//! # &mut self, -//! # _bounds: Rectangle, -//! # _content: &str, -//! # _size: Option<u16>, -//! # _color: Option<[f32; 4]>, -//! # _horizontal_alignment: HorizontalAlignment, -//! # _vertical_alignment: VerticalAlignment, -//! # ) { -//! # } -//! # } -//! # } -//! use iced::{Button, Column, Text}; -//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! -//! -//! impl Counter { -//! pub fn view(&mut self) -> Column<Message, Renderer> { -//! // We use a column: a simple vertical layout -//! Column::new() -//! .push( -//! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! Button::new(&mut self.increment_button, "+") -//! .on_press(Message::IncrementPressed), -//! ) -//! .push( -//! // We show the value of the counter here -//! Text::new(&self.value.to_string()).size(50), -//! ) -//! .push( -//! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! Button::new(&mut self.decrement_button, "-") -//! .on_press(Message::DecrementPressed), -//! ) -//! } -//! } -//! ``` -//! -//! Finally, we need to be able to react to any produced __messages__ and change -//! our __state__ accordingly in our __update logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! impl Counter { -//! // ... -//! -//! pub fn update(&mut self, message: Message) { -//! match message { -//! Message::IncrementPressed => { -//! self.value += 1; -//! } -//! Message::DecrementPressed => { -//! self.value -= 1; -//! } -//! } -//! } -//! } -//! ``` -//! -//! And that's everything! We just wrote a whole user interface. Iced is now able -//! to: -//! -//! 1. Take the result of our __view logic__ and layout its widgets. -//! 1. Process events from our system and produce __messages__ for our -//! __update logic__. -//! 1. Draw the resulting user interface using our chosen __renderer__. -//! -//! Check out the [`UserInterface`] type to learn how to wire everything up! -//! -//! [Elm]: https://elm-lang.org/ -//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ -//! [documentation]: https://docs.rs/iced -//! [examples]: https://github.com/hecrj/iced/tree/master/examples -//! [`UserInterface`]: struct.UserInterface.html -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] -#![deny(unused_results)] -#![deny(unsafe_code)] -#![deny(rust_2018_idioms)] -pub mod input; -pub mod renderer; -pub mod widget; - -mod element; -mod event; -mod hasher; -mod layout; -mod mouse_cursor; -mod node; -mod point; -mod rectangle; -mod style; -mod user_interface; -mod vector; - -#[doc(no_inline)] -pub use stretch::{geometry::Size, number::Number}; - -pub use element::Element; -pub use event::Event; -pub use hasher::Hasher; -pub use layout::Layout; -pub use mouse_cursor::MouseCursor; -pub use node::Node; -pub use point::Point; -pub use rectangle::Rectangle; -pub use style::{Align, Justify, Style}; -pub use user_interface::{Cache, UserInterface}; -pub(crate) use vector::Vector; -pub use widget::*; diff --git a/src/style.rs b/src/style.rs deleted file mode 100644 index 575ea366..00000000 --- a/src/style.rs +++ /dev/null @@ -1,262 +0,0 @@ -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; - -/// The appearance of a [`Node`]. -/// -/// [`Node`]: struct.Node.html -#[derive(Debug, Clone, Copy)] -pub struct Style(pub(crate) style::Style); - -impl Style { - /// Defines the width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn width(mut self, width: u16) -> Self { - self.0.size.width = style::Dimension::Points(width as f32); - self - } - - /// Defines the height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn height(mut self, height: u16) -> Self { - self.0.size.height = style::Dimension::Points(height as f32); - self - } - - /// Defines the minimum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_width(mut self, min_width: u16) -> Self { - self.0.min_size.width = style::Dimension::Points(min_width as f32); - self - } - - /// Defines the maximum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.0.max_size.width = style::Dimension::Points(max_width as f32); - self.fill_width() - } - - /// Defines the minimum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_height(mut self, min_height: u16) -> Self { - self.0.min_size.height = - style::Dimension::Points(f32::from(min_height)); - self - } - - /// Defines the maximum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.0.max_size.height = - style::Dimension::Points(f32::from(max_height)); - self.fill_height() - } - - /// Makes a [`Node`] fill all the horizontal available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_width(mut self) -> Self { - self.0.size.width = stretch::style::Dimension::Percent(1.0); - self - } - - /// Makes a [`Node`] fill all the vertical available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_height(mut self) -> Self { - self.0.size.height = stretch::style::Dimension::Percent(1.0); - self - } - - pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); - self - } - - pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); - self - } - - /// Sets the alignment of a [`Node`]. - /// - /// If the [`Node`] is inside a... - /// - /// * [`Column`], this setting will affect its __horizontal__ alignment. - /// * [`Row`], this setting will affect its __vertical__ alignment. - /// - /// [`Node`]: struct.Node.html - /// [`Column`]: widget/struct.Column.html - /// [`Row`]: widget/struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.0.align_self = align.into(); - self - } - - /// Sets the padding of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn padding(mut self, px: u16) -> Self { - self.0.padding = stretch::geometry::Rect { - start: style::Dimension::Points(px as f32), - end: style::Dimension::Points(px as f32), - top: style::Dimension::Points(px as f32), - bottom: style::Dimension::Points(px as f32), - }; - - self - } -} - -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) - } -} - -impl Hash for Style { - fn hash<H: Hasher>(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); - } -} - -fn hash_size<H: Hasher>( - size: &geometry::Size<style::Dimension>, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect<H: Hasher>( - rect: &geometry::Rect<style::Dimension>, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension<H: Hasher>(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } - } -} - -/// Alignment on the cross axis of a container. -/// -/// * On a [`Column`], it describes __horizontal__ alignment. -/// * On a [`Row`], it describes __vertical__ alignment. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Align { - /// Align at the start of the cross axis. - Start, - - /// Align at the center of the cross axis. - Center, - - /// Align at the end of the cross axis. - End, - - /// Stretch over the cross axis. - Stretch, -} - -#[doc(hidden)] -impl From<Align> for style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignItems::FlexStart, - Align::Center => style::AlignItems::Center, - Align::End => style::AlignItems::FlexEnd, - Align::Stretch => style::AlignItems::Stretch, - } - } -} - -#[doc(hidden)] -impl From<Align> for style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignSelf::FlexStart, - Align::Center => style::AlignSelf::Center, - Align::End => style::AlignSelf::FlexEnd, - Align::Stretch => style::AlignSelf::Stretch, - } - } -} - -/// Distribution on the main axis of a container. -/// -/// * On a [`Column`], it describes __vertical__ distribution. -/// * On a [`Row`], it describes __horizontal__ distribution. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Justify { - /// Place items at the start of the main axis. - Start, - - /// Place items at the center of the main axis. - Center, - - /// Place items at the end of the main axis. - End, - - /// Place items with space between. - SpaceBetween, - - /// Place items with space around. - SpaceAround, - - /// Place items with evenly distributed space. - SpaceEvenly, -} - -#[doc(hidden)] -impl From<Justify> for style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => style::JustifyContent::FlexStart, - Justify::Center => style::JustifyContent::Center, - Justify::End => style::JustifyContent::FlexEnd, - Justify::SpaceBetween => style::JustifyContent::SpaceBetween, - Justify::SpaceAround => style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/src/widget/button.rs b/src/widget/button.rs deleted file mode 100644 index abcdbfeb..00000000 --- a/src/widget/button.rs +++ /dev/null @@ -1,282 +0,0 @@ -//! Allow your users to perform actions by pressing a button. -//! -//! A [`Button`] has some local [`State`] and a [`Class`]. -//! -//! [`Button`]: struct.Button.html -//! [`State`]: struct.State.html -//! [`Class`]: enum.Class.html - -use crate::input::{mouse, ButtonState}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Style, Widget, -}; - -use std::hash::Hash; - -/// A generic widget that produces a message when clicked. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`button::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`button::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{button, Button}; -/// -/// pub enum Message { -/// ButtonClicked, -/// } -/// -/// let state = &mut button::State::new(); -/// -/// Button::new(state, "Click me!") -/// .on_press(Message::ButtonClicked); -/// ``` -/// -///  -pub struct Button<'a, Message> { - state: &'a mut State, - label: String, - class: Class, - on_press: Option<Message>, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Button<'a, Message> -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Button") - .field("state", &self.state) - .field("label", &self.label) - .field("class", &self.class) - .field("on_press", &self.on_press) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Button<'a, Message> { - /// Creates a new [`Button`] with some local [`State`] and the given label. - /// - /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - /// [`Class::Primary`]: enum.Class.html#variant.Primary - pub fn new(state: &'a mut State, label: &str) -> Self { - Button { - state, - label: String::from(label), - class: Class::Primary, - on_press: None, - style: Style::default().min_width(100), - } - } - - /// Sets the width of the [`Button`] in pixels. - /// - /// [`Button`]: struct.Button.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Makes the [`Button`] fill the horizontal space of its container. - /// - /// [`Button`]: struct.Button.html - pub fn fill_width(mut self) -> Self { - self.style = self.style.fill_width(); - self - } - - /// Sets the alignment of the [`Button`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Button`]: struct.Button.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the [`Class`] of the [`Button`]. - /// - /// - /// [`Button`]: struct.Button.html - /// [`Class`]: enum.Class.html - pub fn class(mut self, class: Class) -> Self { - self.class = class; - self - } - - /// Sets the message that will be produced when the [`Button`] is pressed. - /// - /// [`Button`]: struct.Button.html - pub fn on_press(mut self, msg: Message) -> Self { - self.on_press = Some(msg); - self - } -} - -impl<'a, Message, Renderer> Widget<Message, Renderer> for Button<'a, Message> -where - Renderer: self::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => { - if let Some(on_press) = self.on_press { - let bounds = layout.bounds(); - - match state { - ButtonState::Pressed => { - self.state.is_pressed = - bounds.contains(cursor_position); - } - ButtonState::Released => { - let is_clicked = self.state.is_pressed - && bounds.contains(cursor_position); - - self.state.is_pressed = false; - - if is_clicked { - messages.push(on_press); - } - } - } - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - &self.label, - self.class, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Button`]. -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_pressed: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Button`] is currently being pressed or - /// not. - /// - /// [`Button`]: struct.Button.html - pub fn is_pressed(&self) -> bool { - self.is_pressed - } -} - -/// The type of a [`Button`]. -/// -///  -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Class { - /// The [`Button`] performs the main action. - /// - /// [`Button`]: struct.Button.html - Primary, - - /// The [`Button`] performs an alternative action. - /// - /// [`Button`]: struct.Button.html - Secondary, - - /// The [`Button`] performs a productive action. - /// - /// [`Button`]: struct.Button.html - Positive, -} - -/// The renderer of a [`Button`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Button`] in your user interface. -/// -/// [`Button`]: struct.Button.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Button`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Button`] - /// * the local state of the [`Button`] - /// * the label of the [`Button`] - /// * the [`Class`] of the [`Button`] - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - label: &str, - class: Class, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From<Button<'a, Message>> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(button) - } -} diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs deleted file mode 100644 index c60807fd..00000000 --- a/src/widget/checkbox.rs +++ /dev/null @@ -1,203 +0,0 @@ -//! Show toggle controls using checkboxes. -use std::hash::Hash; - -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -/// A box that can be checked, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`checkbox::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`checkbox::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Checkbox; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// pub enum Message { -/// CheckboxToggled(bool), -/// } -/// -/// let is_checked = true; -/// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::Black); -/// ``` -/// -///  -pub struct Checkbox<Color, Message> { - is_checked: bool, - on_toggle: Box<dyn Fn(bool) -> Message>, - label: String, - label_color: Option<Color>, -} - -impl<Color, Message> std::fmt::Debug for Checkbox<Color, Message> -where - Color: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Checkbox") - .field("is_checked", &self.is_checked) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl<Color, Message> Checkbox<Color, Message> { - /// Creates a new [`Checkbox`]. - /// - /// It expects: - /// * a boolean describing whether the [`Checkbox`] is checked or not - /// * the label of the [`Checkbox`] - /// * a function that will be called when the [`Checkbox`] is toggled. - /// It will receive the new state of the [`Checkbox`] and must produce - /// a `Message`. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn new<F>(is_checked: bool, label: &str, f: F) -> Self - where - F: 'static + Fn(bool) -> Message, - { - Checkbox { - is_checked, - on_toggle: Box::new(f), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Checkbox`]. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl<Color, Message, Renderer> Widget<Message, Renderer> - for Checkbox<Color, Message> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer<Color>, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - let mouse_over = layout - .children() - .any(|child| child.bounds().contains(cursor_position)); - - if mouse_over { - messages.push((self.on_toggle)(!self.is_checked)); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let text_bounds = children[1].bounds(); - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - text_bounds, - self.is_checked, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Checkbox`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Checkbox`] in your user interface. -/// -/// [`Checkbox`]: struct.Checkbox.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Checkbox`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Checkbox`] - /// * the bounds of the label of the [`Checkbox`] - /// * whether the [`Checkbox`] is checked or not - /// - /// [`Checkbox`]: struct.Checkbox.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_checked: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From<Checkbox<Color, Message>> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer<Color>, - Message: 'static, -{ - fn from( - checkbox: Checkbox<Color, Message>, - ) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/column.rs b/src/widget/column.rs deleted file mode 100644 index ff754e98..00000000 --- a/src/widget/column.rs +++ /dev/null @@ -1,224 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents vertically. -/// -/// A [`Column`] will try to fill the horizontal space of its container. -/// -/// [`Column`]: struct.Column.html -#[derive(Default)] -pub struct Column<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec<Element<'a, Message, Renderer>>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Column") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Column<'a, Message, Renderer> { - /// Creates an empty [`Column`]. - /// - /// [`Column`]: struct.Column.html - pub fn new() -> Self { - let mut style = Style::default().fill_width(); - style.0.flex_direction = stretch::style::FlexDirection::Column; - - Column { - style, - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the vertical spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Column`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Column`]: struct.Column.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the horizontal alignment of the contents of the [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the vertical distribution strategy for the contents of the - /// [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Column`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Column`]: struct.Column.html - pub fn push<E>(mut self, child: E) -> Column<'a, Message, Renderer> - where - E: Into<Element<'a, Message, Renderer>>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget<Message, Renderer> - for Column<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec<Node> = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.bottom = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.bottom = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From<Column<'a, Message, Renderer>> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from( - column: Column<'a, Message, Renderer>, - ) -> Element<'a, Message, Renderer> { - Element::new(column) - } -} diff --git a/src/widget/image.rs b/src/widget/image.rs deleted file mode 100644 index d94bfea5..00000000 --- a/src/widget/image.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! Display images in your user interface. - -use crate::{ - Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A frame that displays an image while keeping aspect ratio. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`image::Renderer`] trait. -/// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`image::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Image; -/// -/// # let my_handle = String::from("some_handle"); -/// let image = Image::new(my_handle); -/// ``` -pub struct Image<I> { - image: I, - source: Option<Rectangle<u16>>, - width: Option<u16>, - height: Option<u16>, - style: Style, -} - -impl<I> std::fmt::Debug for Image<I> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Image") - .field("source", &self.source) - .field("width", &self.width) - .field("height", &self.height) - .field("style", &self.style) - .finish() - } -} - -impl<I> Image<I> { - /// Creates a new [`Image`] with given image handle. - /// - /// [`Image`]: struct.Image.html - pub fn new(image: I) -> Self { - Image { - image, - source: None, - width: None, - height: None, - style: Style::default(), - } - } - - /// Sets the portion of the [`Image`] to draw. - /// - /// [`Image`]: struct.Image.html - pub fn clip(mut self, source: Rectangle<u16>) -> Self { - self.source = Some(source); - self - } - - /// Sets the width of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn width(mut self, width: u16) -> Self { - self.width = Some(width); - self - } - - /// Sets the height of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn height(mut self, height: u16) -> Self { - self.height = Some(height); - self - } - - /// Sets the alignment of the [`Image`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Image`]: struct.Image.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } -} - -impl<I, Message, Renderer> Widget<Message, Renderer> for Image<I> -where - Renderer: self::Renderer<I>, - I: Clone, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node( - self.style, - &self.image, - self.width, - self.height, - self.source, - ) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(&self.image, layout.bounds(), self.source); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -/// The renderer of an [`Image`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// an [`Image`] in your user interface. -/// -/// [`Image`]: struct.Image.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer<I> { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] - /// and its size. - /// - /// You should probably keep the original aspect ratio, if possible. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Image`]: struct.Image.html - fn node( - &self, - style: Style, - image: &I, - width: Option<u16>, - height: Option<u16>, - source: Option<Rectangle<u16>>, - ) -> Node; - - /// Draws an [`Image`]. - /// - /// It receives: - /// * the bounds of the [`Image`] - /// * the handle of the loaded [`Image`] - /// * the portion of the image to draw. If not specified, the entire image - /// should be drawn. - /// - /// [`Image`]: struct.Image.html - fn draw( - &mut self, - image: &I, - bounds: Rectangle<f32>, - source: Option<Rectangle<u16>>, - ); -} - -impl<'a, I, Message, Renderer> From<Image<I>> for Element<'a, Message, Renderer> -where - Renderer: self::Renderer<I>, - I: Clone + 'a, -{ - fn from(image: Image<I>) -> Element<'a, Message, Renderer> { - Element::new(image) - } -} diff --git a/src/widget/panel.rs b/src/widget/panel.rs deleted file mode 100644 index d43d6fb6..00000000 --- a/src/widget/panel.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::hash::Hash; - -use crate::graphics::{Point, Rectangle}; -use crate::ui::core::{ - Event, Hasher, Layout, MouseCursor, Node, Style, Widget, -}; - -pub struct Panel<'a, Message, Renderer> { - style: Style, - content: Box<Widget<Message, Renderer> + 'a>, -} - -impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { - pub fn new(content: impl Widget<Message, Renderer> + 'a) -> Self { - Panel { - style: Style::default().padding(20), - content: Box::new(content), - } - } - - pub fn width(mut self, width: u32) -> Self { - self.style = self.style.width(width); - self - } - - pub fn max_width(mut self, max_width: u32) -> Self { - self.style = self.style.max_width(max_width); - self - } -} - -impl<'a, Message, Renderer> Widget<Message, Renderer> - for Panel<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Node::with_children(self.style, vec![self.content.node(renderer)]) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - [&mut self.content] - .iter_mut() - .zip(layout.children()) - .for_each(|(child, layout)| { - child.on_event(event, layout, cursor_position, messages) - }); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout, - cursor_position: Point, - ) -> MouseCursor { - let bounds = layout.bounds(); - let mut cursor = MouseCursor::OutOfBounds; - renderer.draw(bounds); - - [&self.content].iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = child.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - if cursor == MouseCursor::OutOfBounds { - if bounds.contains(cursor_position) { - MouseCursor::Idle - } else { - MouseCursor::OutOfBounds - } - } else { - cursor - } - } - - fn hash(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -pub trait Renderer { - fn draw(&mut self, bounds: Rectangle); -} diff --git a/src/widget/progress_bar.rs b/src/widget/progress_bar.rs deleted file mode 100644 index d4499160..00000000 --- a/src/widget/progress_bar.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Provide visual feedback to your users when performing a slow task. - -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A bar that is filled based on an amount of progress. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`progress_bar::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`progress_bar::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::ProgressBar; -/// -/// let progress = 0.75; -/// -/// ProgressBar::new(progress); -/// ``` -#[derive(Debug)] -pub struct ProgressBar { - progress: f32, - style: Style, -} - -impl ProgressBar { - /// Creates a new [`ProgressBar`] filled based on the given amount of - /// progress. - /// - /// The progress should be in the `0.0..=1.0` range. `0` meaning no work - /// done, and `1` meaning work finished. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn new(progress: f32) -> Self { - ProgressBar { - progress, - style: Style::default().fill_width(), - } - } - - /// Sets the width of the [`ProgressBar`] in pixels. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl<Message, Renderer> Widget<Message, Renderer> for ProgressBar -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(layout.bounds(), self.progress); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The renderer of a [`ProgressBar`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// a [`ProgressBar`] in your user interface. -/// -/// [`ProgressBar`]: struct.ProgressBar.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`ProgressBar`]. - /// - /// It receives: - /// * the bounds of the [`ProgressBar`] - /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` - /// range. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - fn draw(&mut self, bounds: Rectangle, progress: f32); -} - -impl<'a, Message, Renderer> From<ProgressBar> for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { - Element::new(progress_bar) - } -} diff --git a/src/widget/radio.rs b/src/widget/radio.rs deleted file mode 100644 index 28353ef4..00000000 --- a/src/widget/radio.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! Create choices using radio buttons. -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -use std::hash::Hash; - -/// A circular button representing a choice, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`radio::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`radio::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{Column, Radio}; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// pub enum Choice { -/// A, -/// B, -/// } -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Message { -/// RadioSelected(Choice), -/// } -/// -/// let selected_choice = Some(Choice::A); -/// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// ``` -/// -///  -pub struct Radio<Color, Message> { - is_selected: bool, - on_click: Message, - label: String, - label_color: Option<Color>, -} - -impl<Color, Message> std::fmt::Debug for Radio<Color, Message> -where - Color: std::fmt::Debug, - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Radio") - .field("is_selected", &self.is_selected) - .field("on_click", &self.on_click) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl<Color, Message> Radio<Color, Message> { - /// Creates a new [`Radio`] button. - /// - /// It expects: - /// * the value related to the [`Radio`] button - /// * the label of the [`Radio`] button - /// * the current selected value - /// * a function that will be called when the [`Radio`] is selected. It - /// receives the value of the radio and must produce a `Message`. - /// - /// [`Radio`]: struct.Radio.html - pub fn new<F, V>(value: V, label: &str, selected: Option<V>, f: F) -> Self - where - V: Eq + Copy, - F: 'static + Fn(V) -> Message, - { - Radio { - is_selected: Some(value) == selected, - on_click: f(value), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Radio`]. - /// - /// [`Radio`]: struct.Radio.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl<Color, Message, Renderer> Widget<Message, Renderer> - for Radio<Color, Message> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer<Color>, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - if layout.bounds().contains(cursor_position) { - messages.push(self.on_click); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let mut text_bounds = children[1].bounds(); - text_bounds.y -= 2.0; - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - layout.bounds(), - self.is_selected, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Radio`] button. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Radio`] button in your user interface. -/// -/// [`Radio`]: struct.Radio.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Radio`] button. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Radio`] - /// * the bounds of the label of the [`Radio`] - /// * whether the [`Radio`] is selected or not - /// - /// [`Radio`]: struct.Radio.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_selected: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From<Radio<Color, Message>> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer<Color>, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio<Color, Message>) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/row.rs b/src/widget/row.rs deleted file mode 100644 index 959528dc..00000000 --- a/src/widget/row.rs +++ /dev/null @@ -1,219 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents horizontally. -/// -/// A [`Row`] will try to fill the horizontal space of its container. -/// -/// [`Row`]: struct.Row.html -#[derive(Default)] -pub struct Row<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec<Element<'a, Message, Renderer>>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Row") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Row<'a, Message, Renderer> { - /// Creates an empty [`Row`]. - /// - /// [`Row`]: struct.Row.html - pub fn new() -> Self { - Row { - style: Style::default().fill_width(), - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the horizontal spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Row`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Row`]: struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the vertical alignment of the contents of the [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the horizontal distribution strategy for the contents of the - /// [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Row`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Row`]: struct.Row.html - pub fn push<E>(mut self, child: E) -> Row<'a, Message, Renderer> - where - E: Into<Element<'a, Message, Renderer>>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget<Message, Renderer> - for Row<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec<Node> = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.end = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.end = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From<Row<'a, Message, Renderer>> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { - Element::new(row) - } -} diff --git a/src/widget/slider.rs b/src/widget/slider.rs deleted file mode 100644 index cdec9ec4..00000000 --- a/src/widget/slider.rs +++ /dev/null @@ -1,241 +0,0 @@ -//! Display an interactive selector of a single value from a range of values. -//! -//! A [`Slider`] has some local [`State`]. -//! -//! [`Slider`]: struct.Slider.html -//! [`State`]: struct.State.html -use std::hash::Hash; -use std::ops::RangeInclusive; - -use crate::input::{mouse, ButtonState}; -use crate::{ - Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -/// 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. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`slider::Renderer`] trait. -/// -/// [`Slider`]: struct.Slider.html -/// [`Widget`]: ../trait.Widget.html -/// [`slider::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{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); -/// ``` -/// -///  -pub struct Slider<'a, Message> { - state: &'a mut State, - range: RangeInclusive<f32>, - value: f32, - on_change: Box<dyn Fn(f32) -> Message>, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Slider") - .field("state", &self.state) - .field("range", &self.range) - .field("value", &self.value) - .field("style", &self.style) - .finish() - } -} - -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<F>( - state: &'a mut State, - range: RangeInclusive<f32>, - 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), - style: Style::default().min_width(100).fill_width(), - } - } - - /// Sets the width of the [`Slider`] in pixels. - /// - /// [`Slider`]: struct.Slider.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl<'a, Message, Renderer> Widget<Message, Renderer> for Slider<'a, Message> -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(25)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec<Message>, - ) { - 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(); - - messages.push((self.on_change)(value)); - } - }; - - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { - if layout.bounds().contains(cursor_position) { - change(); - self.state.is_dragging = true; - } - } - ButtonState::Released => { - self.state.is_dragging = false; - } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - if self.state.is_dragging { - change(); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - self.range.clone(), - self.value, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// 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() - } - - /// Returns whether the associated [`Slider`] is currently being dragged or - /// not. - /// - /// [`Slider`]: struct.Slider.html - pub fn is_dragging(&self) -> bool { - self.is_dragging - } -} - -/// The renderer of a [`Slider`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Slider`] in your user interface. -/// -/// [`Slider`]: struct.Slider.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Slider`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Slider`] - /// * the local state of the [`Slider`] - /// * the range of values of the [`Slider`] - /// * the current value of the [`Slider`] - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - range: RangeInclusive<f32>, - value: f32, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From<Slider<'a, Message>> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static, -{ - fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(slider) - } -} diff --git a/src/widget/text.rs b/src/widget/text.rs deleted file mode 100644 index 59b599bb..00000000 --- a/src/widget/text.rs +++ /dev/null @@ -1,224 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A fragment of text with a generic `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// Text::new("I <3 iced!") -/// .size(40) -/// .color(Color::Black); -/// ``` -#[derive(Debug, Clone)] -pub struct Text<Color> { - content: String, - size: Option<u16>, - color: Option<Color>, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl<Color> Text<Color> { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color(mut self, color: Color) -> Self { - self.color = Some(color); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl<Message, Renderer, Color> Widget<Message, Renderer> for Text<Color> -where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer<Color>, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment with a generic `Color`. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer<Color> { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option<u16>) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option<u16>, - color: Option<Color>, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer, Color> From<Text<Color>> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer<Color>, -{ - fn from(text: Text<Color>) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} |