From 8b8f7563ad33dafeadf6238e377748cdec17d67a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Sep 2019 19:41:49 +0200 Subject: Switch to workspace layout --- src/widget/checkbox.rs | 203 ------------------------------------------------- 1 file changed, 203 deletions(-) delete mode 100644 src/widget/checkbox.rs (limited to 'src/widget/checkbox.rs') 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); -/// ``` -/// -/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { - is_checked: bool, - on_toggle: Box Message>, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Checkbox -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 Checkbox { - /// 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(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 Widget - for Checkbox -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, -{ - 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, - ) { - 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> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static, -{ - fn from( - checkbox: Checkbox, - ) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} -- cgit From a97401aed2a173260a4abfdb65a77975ce6c0f01 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Sep 2019 19:16:06 +0200 Subject: Rethink workspace structure --- src/widget/checkbox.rs | 203 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 src/widget/checkbox.rs (limited to 'src/widget/checkbox.rs') diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs new file mode 100644 index 00000000..c60807fd --- /dev/null +++ b/src/widget/checkbox.rs @@ -0,0 +1,203 @@ +//! 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); +/// ``` +/// +/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) +pub struct Checkbox { + is_checked: bool, + on_toggle: Box Message>, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Checkbox +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 Checkbox { + /// 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(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 Widget + for Checkbox +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, +{ + 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, + ) { + 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> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static, +{ + fn from( + checkbox: Checkbox, + ) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} -- cgit From 655978f480c32bc696f0d5fe2fff834bfbf238ea Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 15 Sep 2019 18:53:13 +0200 Subject: Draft nodes for missing widgets --- src/widget/checkbox.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/widget/checkbox.rs') diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index c60807fd..4ae167ad 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -38,9 +38,12 @@ use crate::{ /// /// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) pub struct Checkbox { - is_checked: bool, - on_toggle: Box Message>, - label: String, + /// Whether the checkbox is checked or not + pub is_checked: bool, + /// Toggle message to fire + pub on_toggle: Box Message>, + /// The label of the checkbox + pub label: String, label_color: Option, } -- cgit From f9de39ddaa3020a9585b1648afb0ead45dfd7aa9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Sep 2019 15:01:12 +0200 Subject: Unify `web` and `ggez` tour examples :tada: --- src/widget/checkbox.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widget/checkbox.rs') diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index 4ae167ad..6c13252d 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -98,7 +98,7 @@ where Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { Row::<(), Renderer>::new() .spacing(15) .align_items(Align::Center) -- cgit From b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Sep 2019 18:47:01 +0200 Subject: Remove generic `Color` in widgets --- src/widget/checkbox.rs | 57 ++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) (limited to 'src/widget/checkbox.rs') diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index 6c13252d..48400fae 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -4,11 +4,11 @@ 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, + Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, + Rectangle, Widget, }; -/// A box that can be checked, with a generic text `Color`. +/// A box that can be checked. /// /// It implements [`Widget`] when the associated `Renderer` implements the /// [`checkbox::Renderer`] trait. @@ -19,12 +19,7 @@ use crate::{ /// # Example /// /// ``` -/// use iced::Checkbox; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } +/// use iced::{Checkbox, Color}; /// /// pub enum Message { /// CheckboxToggled(bool), @@ -32,25 +27,28 @@ use crate::{ /// /// let is_checked = true; /// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::Black); +/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled); /// ``` /// /// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { +pub struct Checkbox { /// Whether the checkbox is checked or not pub is_checked: bool, - /// Toggle message to fire + + /// Function to call when checkbox is toggled to produce a __message__. + /// + /// The function should be provided `true` when the checkbox is checked + /// and `false` otherwise. pub on_toggle: Box Message>, + /// The label of the checkbox pub label: String, - label_color: Option, + + /// The color of the label + pub label_color: Option, } -impl std::fmt::Debug for Checkbox -where - Color: std::fmt::Debug, -{ +impl std::fmt::Debug for Checkbox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Checkbox") .field("is_checked", &self.is_checked) @@ -60,7 +58,7 @@ where } } -impl Checkbox { +impl Checkbox { /// Creates a new [`Checkbox`]. /// /// It expects: @@ -83,20 +81,18 @@ impl Checkbox { } } - /// Sets the `Color` of the label of the [`Checkbox`]. + /// 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); + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); self } } -impl Widget - for Checkbox +impl Widget for Checkbox where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, { fn node(&self, renderer: &mut Renderer) -> Node { Row::<(), Renderer>::new() @@ -191,16 +187,13 @@ pub trait Renderer { ) -> MouseCursor; } -impl<'a, Color, Message, Renderer> From> +impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, Message: 'static, { - fn from( - checkbox: Checkbox, - ) -> Element<'a, Message, Renderer> { + fn from(checkbox: Checkbox) -> Element<'a, Message, Renderer> { Element::new(checkbox) } } -- cgit From b9e0f7494881ad7cdfbcbc16878ecc6ef717753f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Sep 2019 19:15:31 +0200 Subject: Create `iced_core` and `iced_native` --- src/widget/checkbox.rs | 199 ------------------------------------------------- 1 file changed, 199 deletions(-) delete mode 100644 src/widget/checkbox.rs (limited to 'src/widget/checkbox.rs') diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs deleted file mode 100644 index 48400fae..00000000 --- a/src/widget/checkbox.rs +++ /dev/null @@ -1,199 +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, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, - Rectangle, Widget, -}; - -/// A box that can be checked. -/// -/// 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, Color}; -/// -/// pub enum Message { -/// CheckboxToggled(bool), -/// } -/// -/// let is_checked = true; -/// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled); -/// ``` -/// -/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { - /// Whether the checkbox is checked or not - pub is_checked: bool, - - /// Function to call when checkbox is toggled to produce a __message__. - /// - /// The function should be provided `true` when the checkbox is checked - /// and `false` otherwise. - pub on_toggle: Box Message>, - - /// The label of the checkbox - pub label: String, - - /// The color of the label - pub label_color: Option, -} - -impl std::fmt::Debug for Checkbox { - 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 Checkbox { - /// 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(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: C) -> Self { - self.label_color = Some(color.into()); - self - } -} - -impl Widget for Checkbox -where - Renderer: self::Renderer + text::Renderer, -{ - fn node(&self, renderer: &mut 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, - ) { - 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, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer + text::Renderer, - Message: 'static, -{ - fn from(checkbox: Checkbox) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} -- cgit