diff options
Diffstat (limited to 'src/widget/checkbox.rs')
-rw-r--r-- | src/widget/checkbox.rs | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index c30b8308..b9a40a37 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -8,13 +8,12 @@ use crate::{ Widget, }; -/// A box that can be checked. +/// A box that can be checked, with a generic text `Color`. /// -/// It implements [`Widget`] when the [`core::Renderer`] implements the +/// It implements [`Widget`] when the associated `Renderer` implements the /// [`checkbox::Renderer`] trait. /// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`core::Renderer`]: ../../core/trait.Renderer.html +/// [`Widget`]: ../trait.Widget.html /// [`checkbox::Renderer`]: trait.Renderer.html /// /// # Example @@ -25,29 +24,24 @@ use crate::{ /// #[derive(Debug, Clone, Copy)] /// pub enum Color { /// Black, -/// White, -/// } -/// -/// impl Default for Color { -/// fn default() -> Color { -/// Color::Black -/// } /// } /// /// pub enum Message { /// CheckboxToggled(bool), /// } /// -/// fn some_checkbox(is_checked: bool) -> Checkbox<Color, Message> { -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::White) -/// } +/// 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: Color, + label_color: Option<Color>, } impl<Color, Message> std::fmt::Debug for Checkbox<Color, Message> @@ -63,10 +57,7 @@ where } } -impl<Color, Message> Checkbox<Color, Message> -where - Color: Default, -{ +impl<Color, Message> Checkbox<Color, Message> { /// Creates a new [`Checkbox`]. /// /// It expects: @@ -85,16 +76,15 @@ where is_checked, on_toggle: Box::new(f), label: String::from(label), - label_color: Color::default(), + label_color: None, } } - /// Sets the [`Color`] of the label of the [`Checkbox`]. + /// Sets the `Color` of the label of the [`Checkbox`]. /// - /// [`Color`]: ../../../../graphics/struct.Color.html /// [`Checkbox`]: struct.Checkbox.html pub fn label_color(mut self, color: Color) -> Self { - self.label_color = color; + self.label_color = Some(color); self } } @@ -102,7 +92,7 @@ where impl<Color, Message, Renderer> Widget<Message, Renderer> for Checkbox<Color, Message> where - Color: 'static + Copy + Default + std::fmt::Debug, + Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer<Color>, { fn node(&self, renderer: &Renderer) -> Node { @@ -167,18 +157,18 @@ where ) } - fn hash(&self, state: &mut Hasher) { + fn hash_layout(&self, state: &mut Hasher) { self.label.hash(state); } } /// The renderer of a [`Checkbox`]. /// -/// Your [`core::Renderer`] will need to implement this trait before being +/// Your [renderer] will need to implement this trait before being /// able to use a [`Checkbox`] in your user interface. /// /// [`Checkbox`]: struct.Checkbox.html -/// [`core::Renderer`]: ../../core/trait.Renderer.html +/// [renderer]: ../../renderer/index.html pub trait Renderer { /// Draws a [`Checkbox`]. /// @@ -192,8 +182,8 @@ pub trait Renderer { fn draw( &mut self, cursor_position: Point, - bounds: Rectangle<f32>, - label_bounds: Rectangle<f32>, + bounds: Rectangle, + label_bounds: Rectangle, is_checked: bool, ) -> MouseCursor; } @@ -201,7 +191,7 @@ pub trait Renderer { impl<'a, Color, Message, Renderer> From<Checkbox<Color, Message>> for Element<'a, Message, Renderer> where - Color: 'static + Copy + Default + std::fmt::Debug, + Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer<Color>, Message: 'static, { |