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/radio.rs | 211 ---------------------------------------------------- 1 file changed, 211 deletions(-) delete mode 100644 src/widget/radio.rs (limited to 'src/widget/radio.rs') 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); -/// ``` -/// -/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { - is_selected: bool, - on_click: Message, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Radio -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 Radio { - /// 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(value: V, label: &str, selected: Option, 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 Widget - for Radio -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - 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, - ) { - 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> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio) -> 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/radio.rs | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 src/widget/radio.rs (limited to 'src/widget/radio.rs') diff --git a/src/widget/radio.rs b/src/widget/radio.rs new file mode 100644 index 00000000..28353ef4 --- /dev/null +++ b/src/widget/radio.rs @@ -0,0 +1,211 @@ +//! 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); +/// ``` +/// +/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) +pub struct Radio { + is_selected: bool, + on_click: Message, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Radio +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 Radio { + /// 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(value: V, label: &str, selected: Option, 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 Widget + for Radio +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + 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, + ) { + 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> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(checkbox: Radio) -> 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/radio.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/widget/radio.rs') diff --git a/src/widget/radio.rs b/src/widget/radio.rs index 28353ef4..27c0ff17 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -47,9 +47,12 @@ use std::hash::Hash; /// /// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) pub struct Radio { - is_selected: bool, - on_click: Message, - label: String, + /// Whether the radio button is selected or not + pub is_selected: bool, + /// The message to produce when the radio button is clicked + pub on_click: Message, + /// The label of the radio button + 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/radio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widget/radio.rs') diff --git a/src/widget/radio.rs b/src/widget/radio.rs index 27c0ff17..ba082ef5 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -111,7 +111,7 @@ where Renderer: self::Renderer + text::Renderer, Message: Copy + std::fmt::Debug, { - 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/radio.rs | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'src/widget/radio.rs') diff --git a/src/widget/radio.rs b/src/widget/radio.rs index ba082ef5..048aea94 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -2,13 +2,13 @@ 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, }; use std::hash::Hash; -/// A circular button representing a choice, with a generic text `Color`. +/// A circular button representing a choice. /// /// It implements [`Widget`] when the associated `Renderer` implements the /// [`radio::Renderer`] trait. @@ -18,12 +18,7 @@ use std::hash::Hash; /// /// # Example /// ``` -/// use iced::{Column, Radio}; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } +/// use iced::Radio; /// /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// pub enum Choice { @@ -38,27 +33,28 @@ use std::hash::Hash; /// /// let selected_choice = Some(Choice::A); /// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); +/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); /// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); +/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); /// ``` /// /// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { +pub struct Radio { /// Whether the radio button is selected or not pub is_selected: bool, + /// The message to produce when the radio button is clicked pub on_click: Message, + /// The label of the radio button pub label: String, - label_color: Option, + + /// The color of the label + pub label_color: Option, } -impl std::fmt::Debug for Radio +impl std::fmt::Debug for Radio where - Color: std::fmt::Debug, Message: std::fmt::Debug, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -71,7 +67,7 @@ where } } -impl Radio { +impl Radio { /// Creates a new [`Radio`] button. /// /// It expects: @@ -98,17 +94,15 @@ impl Radio { /// 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); + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); self } } -impl Widget - for Radio +impl Widget for Radio where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, Message: Copy + std::fmt::Debug, { fn node(&self, renderer: &mut Renderer) -> Node { @@ -201,14 +195,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 + Copy + std::fmt::Debug, { - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { + fn from(checkbox: Radio) -> 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/radio.rs | 207 ---------------------------------------------------- 1 file changed, 207 deletions(-) delete mode 100644 src/widget/radio.rs (limited to 'src/widget/radio.rs') diff --git a/src/widget/radio.rs b/src/widget/radio.rs deleted file mode 100644 index 048aea94..00000000 --- a/src/widget/radio.rs +++ /dev/null @@ -1,207 +0,0 @@ -//! Create choices using radio buttons. -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, -}; - -use std::hash::Hash; - -/// A circular button representing a choice. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`radio::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`radio::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::Radio; -/// -/// #[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); -/// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); -/// ``` -/// -/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { - /// Whether the radio button is selected or not - pub is_selected: bool, - - /// The message to produce when the radio button is clicked - pub on_click: Message, - - /// The label of the radio button - pub label: String, - - /// The color of the label - pub label_color: Option, -} - -impl std::fmt::Debug for Radio -where - 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 Radio { - /// 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(value: V, label: &str, selected: Option, 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: C) -> Self { - self.label_color = Some(color.into()); - self - } -} - -impl Widget for Radio -where - Renderer: self::Renderer + text::Renderer, - Message: Copy + std::fmt::Debug, -{ - 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, - }) => { - 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, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer + text::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} -- cgit