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