diff options
| author | 2019-09-19 18:47:01 +0200 | |
|---|---|---|
| committer | 2019-09-19 19:00:42 +0200 | |
| commit | b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019 (patch) | |
| tree | 58cb04c5e6be38422c8767cabba938edc4988b75 /src/widget | |
| parent | f9de39ddaa3020a9585b1648afb0ead45dfd7aa9 (diff) | |
| download | iced-b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019.tar.gz iced-b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019.tar.bz2 iced-b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019.zip | |
Remove generic `Color` in widgets
Diffstat (limited to 'src/widget')
| -rw-r--r-- | src/widget/checkbox.rs | 57 | ||||
| -rw-r--r-- | src/widget/radio.rs | 49 | ||||
| -rw-r--r-- | src/widget/text.rs | 40 | 
3 files changed, 62 insertions, 84 deletions
| 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);  /// ```  ///  ///  -pub struct Checkbox<Color, Message> { +pub struct Checkbox<Message> {      /// 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<dyn Fn(bool) -> Message>, +      /// The label of the checkbox      pub label: String, -    label_color: Option<Color>, + +    /// The color of the label +    pub label_color: Option<Color>,  } -impl<Color, Message> std::fmt::Debug for Checkbox<Color, Message> -where -    Color: std::fmt::Debug, -{ +impl<Message> std::fmt::Debug for Checkbox<Message> {      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<Color, Message> Checkbox<Color, Message> { +impl<Message> Checkbox<Message> {      /// Creates a new [`Checkbox`].      ///      /// It expects: @@ -83,20 +81,18 @@ impl<Color, Message> Checkbox<Color, Message> {          }      } -    /// 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<C: Into<Color>>(mut self, color: C) -> Self { +        self.label_color = Some(color.into());          self      }  } -impl<Color, Message, Renderer> Widget<Message, Renderer> -    for Checkbox<Color, Message> +impl<Message, Renderer> Widget<Message, Renderer> for Checkbox<Message>  where -    Color: 'static + Copy + std::fmt::Debug, -    Renderer: self::Renderer + text::Renderer<Color>, +    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<Checkbox<Color, Message>> +impl<'a, Message, Renderer> From<Checkbox<Message>>      for Element<'a, Message, Renderer>  where -    Color: 'static + Copy + std::fmt::Debug, -    Renderer: self::Renderer + text::Renderer<Color>, +    Renderer: self::Renderer + text::Renderer,      Message: 'static,  { -    fn from( -        checkbox: Checkbox<Color, Message>, -    ) -> Element<'a, Message, Renderer> { +    fn from(checkbox: Checkbox<Message>) -> Element<'a, Message, Renderer> {          Element::new(checkbox)      }  } 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);  /// ```  ///  ///  -pub struct Radio<Color, Message> { +pub struct Radio<Message> {      /// 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<Color>, + +    /// The color of the label +    pub label_color: Option<Color>,  } -impl<Color, Message> std::fmt::Debug for Radio<Color, Message> +impl<Message> std::fmt::Debug for Radio<Message>  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<Color, Message> Radio<Color, Message> { +impl<Message> Radio<Message> {      /// Creates a new [`Radio`] button.      ///      /// It expects: @@ -98,17 +94,15 @@ impl<Color, Message> Radio<Color, Message> {      /// 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<C: Into<Color>>(mut self, color: C) -> Self { +        self.label_color = Some(color.into());          self      }  } -impl<Color, Message, Renderer> Widget<Message, Renderer> -    for Radio<Color, Message> +impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message>  where -    Color: 'static + Copy + std::fmt::Debug, -    Renderer: self::Renderer + text::Renderer<Color>, +    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<Radio<Color, Message>> +impl<'a, Message, Renderer> From<Radio<Message>>      for Element<'a, Message, Renderer>  where -    Color: 'static + Copy + std::fmt::Debug, -    Renderer: self::Renderer + text::Renderer<Color>, +    Renderer: self::Renderer + text::Renderer,      Message: 'static + Copy + std::fmt::Debug,  { -    fn from(checkbox: Radio<Color, Message>) -> Element<'a, Message, Renderer> { +    fn from(checkbox: Radio<Message>) -> Element<'a, Message, Renderer> {          Element::new(checkbox)      }  } diff --git a/src/widget/text.rs b/src/widget/text.rs index 457a6814..4ef10d52 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -1,11 +1,12 @@  //! Write some text for your users to read.  use crate::{ -    Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +    Color, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, +    Widget,  };  use std::hash::Hash; -/// A fragment of text with a generic `Color`. +/// A paragraph of text.  ///  /// It implements [`Widget`] when the associated `Renderer` implements the  /// [`text::Renderer`] trait. @@ -16,19 +17,13 @@ use std::hash::Hash;  /// # Example  ///  /// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -///     Black, -/// } +/// use iced::{Text, Color};  ///  /// Text::new("I <3 iced!") -///     .size(40) -///     .color(Color::Black); +///     .size(40);  /// ```  #[derive(Debug, Clone)] -pub struct Text<Color> { +pub struct Text {      /// The text contents      pub content: String,      /// The text size @@ -39,7 +34,7 @@ pub struct Text<Color> {      vertical_alignment: VerticalAlignment,  } -impl<Color> Text<Color> { +impl Text {      /// Create a new fragment of [`Text`] with the given contents.      ///      /// [`Text`]: struct.Text.html @@ -65,8 +60,8 @@ impl<Color> Text<Color> {      /// Sets the `Color` of the [`Text`].      ///      /// [`Text`]: struct.Text.html -    pub fn color(mut self, color: Color) -> Self { -        self.color = Some(color); +    pub fn color<C: Into<Color>>(mut self, color: C) -> Self { +        self.color = Some(color.into());          self      } @@ -108,10 +103,9 @@ impl<Color> Text<Color> {      }  } -impl<Message, Renderer, Color> Widget<Message, Renderer> for Text<Color> +impl<Message, Renderer> Widget<Message, Renderer> for Text  where -    Color: Copy + std::fmt::Debug, -    Renderer: self::Renderer<Color>, +    Renderer: self::Renderer,  {      fn node(&self, renderer: &mut Renderer) -> Node {          renderer.node(self.style, &self.content, self.size) @@ -143,7 +137,7 @@ where      }  } -/// The renderer of a [`Text`] fragment with a generic `Color`. +/// The renderer of a [`Text`] fragment.  ///  /// Your [renderer] will need to implement this trait before being  /// able to use [`Text`] in your [`UserInterface`]. @@ -151,7 +145,7 @@ where  /// [`Text`]: struct.Text.html  /// [renderer]: ../../renderer/index.html  /// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer<Color> { +pub trait Renderer {      /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`]      /// contents and size.      /// @@ -188,13 +182,11 @@ pub trait Renderer<Color> {      );  } -impl<'a, Message, Renderer, Color> From<Text<Color>> -    for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From<Text> for Element<'a, Message, Renderer>  where -    Color: 'static + Copy + std::fmt::Debug, -    Renderer: self::Renderer<Color>, +    Renderer: self::Renderer,  { -    fn from(text: Text<Color>) -> Element<'a, Message, Renderer> { +    fn from(text: Text) -> Element<'a, Message, Renderer> {          Element::new(text)      }  } | 
