Remove generic Color in widgets

This commit is contained in:
Héctor Ramón Jiménez 2019-09-19 18:47:01 +02:00
parent f9de39ddaa
commit b83a4b42dd
12 changed files with 83 additions and 120 deletions

View file

@ -2,8 +2,6 @@ use super::{into_color, Renderer};
use ggez::graphics::{DrawMode, MeshBuilder, Rect}; use ggez::graphics::{DrawMode, MeshBuilder, Rect};
impl iced::renderer::Debugger for Renderer<'_> { impl iced::renderer::Debugger for Renderer<'_> {
type Color = iced::Color;
fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) { fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) {
let bounds = layout.bounds(); let bounds = layout.bounds();

View file

@ -5,7 +5,7 @@ use iced::text;
use std::cell::RefCell; use std::cell::RefCell;
use std::f32; use std::f32;
impl text::Renderer<iced::Color> for Renderer<'_> { impl text::Renderer for Renderer<'_> {
fn node( fn node(
&self, &self,
style: iced::Style, style: iced::Style,

View file

@ -1,10 +1,9 @@
use super::Renderer; use super::Renderer;
pub use iced::{button, slider, text, Align, Button, Color, Slider}; pub use iced::{
button, slider, text, Align, Button, Checkbox, Color, Radio, Slider, Text,
};
pub type Text = iced::Text<Color>;
pub type Checkbox<Message> = iced::Checkbox<Color, Message>;
pub type Radio<Message> = iced::Radio<Color, Message>;
pub type Image<'a> = iced::Image<&'a str>; pub type Image<'a> = iced::Image<&'a str>;
pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>;

View file

@ -1,7 +1,7 @@
use stretch::{geometry, result}; use stretch::{geometry, result};
use crate::{ use crate::{
renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget,
}; };
/// A generic [`Widget`]. /// A generic [`Widget`].
@ -210,16 +210,16 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> {
/// ///
/// [`Element`]: struct.Element.html /// [`Element`]: struct.Element.html
/// [`Renderer`]: trait.Renderer.html /// [`Renderer`]: trait.Renderer.html
pub fn explain( pub fn explain<C: Into<Color>>(
self, self,
color: Renderer::Color, color: C,
) -> Element<'a, Message, Renderer> ) -> Element<'a, Message, Renderer>
where where
Message: 'static, Message: 'static,
Renderer: 'a + renderer::Debugger, Renderer: 'a + renderer::Debugger,
{ {
Element { Element {
widget: Box::new(Explain::new(self, color)), widget: Box::new(Explain::new(self, color.into())),
} }
} }
@ -309,7 +309,7 @@ where
struct Explain<'a, Message, Renderer: renderer::Debugger> { struct Explain<'a, Message, Renderer: renderer::Debugger> {
element: Element<'a, Message, Renderer>, element: Element<'a, Message, Renderer>,
color: Renderer::Color, color: Color,
} }
impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer>
@ -327,10 +327,7 @@ impl<'a, Message, Renderer> Explain<'a, Message, Renderer>
where where
Renderer: renderer::Debugger, Renderer: renderer::Debugger,
{ {
fn new( fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self {
element: Element<'a, Message, Renderer>,
color: Renderer::Color,
) -> Self {
Explain { element, color } Explain { element, color }
} }
} }

View file

@ -78,7 +78,7 @@
//! # mod iced_wgpu { //! # mod iced_wgpu {
//! # use iced::{ //! # use iced::{
//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, //! # button, text, text::HorizontalAlignment, text::VerticalAlignment,
//! # MouseCursor, Node, Point, Rectangle, Style, //! # MouseCursor, Node, Point, Rectangle, Style, Color
//! # }; //! # };
//! # //! #
//! # pub struct Renderer {} //! # pub struct Renderer {}
@ -96,7 +96,7 @@
//! # } //! # }
//! # } //! # }
//! # //! #
//! # impl text::Renderer<[f32; 4]> for Renderer { //! # impl text::Renderer for Renderer {
//! # fn node(&self, style: Style, _content: &str, _size: Option<u16>) -> Node { //! # fn node(&self, style: Style, _content: &str, _size: Option<u16>) -> Node {
//! # Node::new(style) //! # Node::new(style)
//! # } //! # }
@ -106,7 +106,7 @@
//! # _bounds: Rectangle, //! # _bounds: Rectangle,
//! # _content: &str, //! # _content: &str,
//! # _size: Option<u16>, //! # _size: Option<u16>,
//! # _color: Option<[f32; 4]>, //! # _color: Option<Color>,
//! # _horizontal_alignment: HorizontalAlignment, //! # _horizontal_alignment: HorizontalAlignment,
//! # _vertical_alignment: VerticalAlignment, //! # _vertical_alignment: VerticalAlignment,
//! # ) { //! # ) {

View file

@ -17,19 +17,12 @@
//! [`text::Renderer`]: ../widget/text/trait.Renderer.html //! [`text::Renderer`]: ../widget/text/trait.Renderer.html
//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html //! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html
//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html //! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html
use crate::Layout; use crate::{Color, Layout};
/// A renderer able to graphically explain a [`Layout`]. /// A renderer able to graphically explain a [`Layout`].
/// ///
/// [`Layout`]: ../struct.Layout.html /// [`Layout`]: ../struct.Layout.html
pub trait Debugger { pub trait Debugger {
/// The color type that will be used to configure the _explanation_.
///
/// This is the type that will be asked in [`Element::explain`].
///
/// [`Element::explain`]: ../struct.Element.html#method.explain
type Color: Copy;
/// Explains the [`Layout`] of an [`Element`] for debugging purposes. /// Explains the [`Layout`] of an [`Element`] for debugging purposes.
/// ///
/// This will be called when [`Element::explain`] has been used. It should /// This will be called when [`Element::explain`] has been used. It should
@ -41,5 +34,5 @@ pub trait Debugger {
/// [`Layout`]: struct.Layout.html /// [`Layout`]: struct.Layout.html
/// [`Element`]: struct.Element.html /// [`Element`]: struct.Element.html
/// [`Element::explain`]: struct.Element.html#method.explain /// [`Element::explain`]: struct.Element.html#method.explain
fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); fn explain(&mut self, layout: &Layout<'_>, color: Color);
} }

View file

@ -4,11 +4,11 @@ use std::hash::Hash;
use crate::input::{mouse, ButtonState}; use crate::input::{mouse, ButtonState};
use crate::widget::{text, Column, Row, Text}; use crate::widget::{text, Column, Row, Text};
use crate::{ use crate::{
Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point,
Widget, 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 /// It implements [`Widget`] when the associated `Renderer` implements the
/// [`checkbox::Renderer`] trait. /// [`checkbox::Renderer`] trait.
@ -19,12 +19,7 @@ use crate::{
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use iced::Checkbox; /// use iced::{Checkbox, Color};
///
/// #[derive(Debug, Clone, Copy)]
/// pub enum Color {
/// Black,
/// }
/// ///
/// pub enum Message { /// pub enum Message {
/// CheckboxToggled(bool), /// CheckboxToggled(bool),
@ -32,25 +27,28 @@ use crate::{
/// ///
/// let is_checked = true; /// let is_checked = true;
/// ///
/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) /// 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) /// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true)
pub struct Checkbox<Color, Message> { pub struct Checkbox<Message> {
/// Whether the checkbox is checked or not /// Whether the checkbox is checked or not
pub is_checked: bool, 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>, pub on_toggle: Box<dyn Fn(bool) -> Message>,
/// The label of the checkbox /// The label of the checkbox
pub label: String, 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> impl<Message> std::fmt::Debug for Checkbox<Message> {
where
Color: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Checkbox") f.debug_struct("Checkbox")
.field("is_checked", &self.is_checked) .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`]. /// Creates a new [`Checkbox`].
/// ///
/// It expects: /// 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 /// [`Checkbox`]: struct.Checkbox.html
pub fn label_color(mut self, color: Color) -> Self { pub fn label_color<C: Into<Color>>(mut self, color: C) -> Self {
self.label_color = Some(color); self.label_color = Some(color.into());
self self
} }
} }
impl<Color, Message, Renderer> Widget<Message, Renderer> impl<Message, Renderer> Widget<Message, Renderer> for Checkbox<Message>
for Checkbox<Color, Message>
where where
Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer,
Renderer: self::Renderer + text::Renderer<Color>,
{ {
fn node(&self, renderer: &mut Renderer) -> Node { fn node(&self, renderer: &mut Renderer) -> Node {
Row::<(), Renderer>::new() Row::<(), Renderer>::new()
@ -191,16 +187,13 @@ pub trait Renderer {
) -> MouseCursor; ) -> MouseCursor;
} }
impl<'a, Color, Message, Renderer> From<Checkbox<Color, Message>> impl<'a, Message, Renderer> From<Checkbox<Message>>
for Element<'a, Message, Renderer> for Element<'a, Message, Renderer>
where where
Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer,
Renderer: self::Renderer + text::Renderer<Color>,
Message: 'static, Message: 'static,
{ {
fn from( fn from(checkbox: Checkbox<Message>) -> Element<'a, Message, Renderer> {
checkbox: Checkbox<Color, Message>,
) -> Element<'a, Message, Renderer> {
Element::new(checkbox) Element::new(checkbox)
} }
} }

View file

@ -2,13 +2,13 @@
use crate::input::{mouse, ButtonState}; use crate::input::{mouse, ButtonState};
use crate::widget::{text, Column, Row, Text}; use crate::widget::{text, Column, Row, Text};
use crate::{ use crate::{
Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point,
Widget, Rectangle, Widget,
}; };
use std::hash::Hash; 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 /// It implements [`Widget`] when the associated `Renderer` implements the
/// [`radio::Renderer`] trait. /// [`radio::Renderer`] trait.
@ -18,12 +18,7 @@ use std::hash::Hash;
/// ///
/// # Example /// # Example
/// ``` /// ```
/// use iced::{Column, Radio}; /// use iced::Radio;
///
/// #[derive(Debug, Clone, Copy)]
/// pub enum Color {
/// Black,
/// }
/// ///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// pub enum Choice { /// pub enum Choice {
@ -38,27 +33,28 @@ use std::hash::Hash;
/// ///
/// let selected_choice = Some(Choice::A); /// let selected_choice = Some(Choice::A);
/// ///
/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) /// 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) /// 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) /// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true)
pub struct Radio<Color, Message> { pub struct Radio<Message> {
/// Whether the radio button is selected or not /// Whether the radio button is selected or not
pub is_selected: bool, pub is_selected: bool,
/// The message to produce when the radio button is clicked /// The message to produce when the radio button is clicked
pub on_click: Message, pub on_click: Message,
/// The label of the radio button /// The label of the radio button
pub label: String, 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 where
Color: std::fmt::Debug,
Message: std::fmt::Debug, Message: std::fmt::Debug,
{ {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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. /// Creates a new [`Radio`] button.
/// ///
/// It expects: /// It expects:
@ -98,17 +94,15 @@ impl<Color, Message> Radio<Color, Message> {
/// Sets the `Color` of the label of the [`Radio`]. /// Sets the `Color` of the label of the [`Radio`].
/// ///
/// [`Radio`]: struct.Radio.html /// [`Radio`]: struct.Radio.html
pub fn label_color(mut self, color: Color) -> Self { pub fn label_color<C: Into<Color>>(mut self, color: C) -> Self {
self.label_color = Some(color); self.label_color = Some(color.into());
self self
} }
} }
impl<Color, Message, Renderer> Widget<Message, Renderer> impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message>
for Radio<Color, Message>
where where
Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer,
Renderer: self::Renderer + text::Renderer<Color>,
Message: Copy + std::fmt::Debug, Message: Copy + std::fmt::Debug,
{ {
fn node(&self, renderer: &mut Renderer) -> Node { fn node(&self, renderer: &mut Renderer) -> Node {
@ -201,14 +195,13 @@ pub trait Renderer {
) -> MouseCursor; ) -> MouseCursor;
} }
impl<'a, Color, Message, Renderer> From<Radio<Color, Message>> impl<'a, Message, Renderer> From<Radio<Message>>
for Element<'a, Message, Renderer> for Element<'a, Message, Renderer>
where where
Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer,
Renderer: self::Renderer + text::Renderer<Color>,
Message: 'static + Copy + std::fmt::Debug, 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) Element::new(checkbox)
} }
} }

View file

@ -1,11 +1,12 @@
//! Write some text for your users to read. //! Write some text for your users to read.
use crate::{ 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; 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 /// It implements [`Widget`] when the associated `Renderer` implements the
/// [`text::Renderer`] trait. /// [`text::Renderer`] trait.
@ -16,19 +17,13 @@ use std::hash::Hash;
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use iced::Text; /// use iced::{Text, Color};
///
/// #[derive(Debug, Clone, Copy)]
/// pub enum Color {
/// Black,
/// }
/// ///
/// Text::new("I <3 iced!") /// Text::new("I <3 iced!")
/// .size(40) /// .size(40);
/// .color(Color::Black);
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Text<Color> { pub struct Text {
/// The text contents /// The text contents
pub content: String, pub content: String,
/// The text size /// The text size
@ -39,7 +34,7 @@ pub struct Text<Color> {
vertical_alignment: VerticalAlignment, vertical_alignment: VerticalAlignment,
} }
impl<Color> Text<Color> { impl Text {
/// Create a new fragment of [`Text`] with the given contents. /// Create a new fragment of [`Text`] with the given contents.
/// ///
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
@ -65,8 +60,8 @@ impl<Color> Text<Color> {
/// Sets the `Color` of the [`Text`]. /// Sets the `Color` of the [`Text`].
/// ///
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
pub fn color(mut self, color: Color) -> Self { pub fn color<C: Into<Color>>(mut self, color: C) -> Self {
self.color = Some(color); self.color = Some(color.into());
self 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 where
Color: Copy + std::fmt::Debug, Renderer: self::Renderer,
Renderer: self::Renderer<Color>,
{ {
fn node(&self, renderer: &mut Renderer) -> Node { fn node(&self, renderer: &mut Renderer) -> Node {
renderer.node(self.style, &self.content, self.size) 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 /// Your [renderer] will need to implement this trait before being
/// able to use [`Text`] in your [`UserInterface`]. /// able to use [`Text`] in your [`UserInterface`].
@ -151,7 +145,7 @@ where
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
/// [renderer]: ../../renderer/index.html /// [renderer]: ../../renderer/index.html
/// [`UserInterface`]: ../../struct.UserInterface.html /// [`UserInterface`]: ../../struct.UserInterface.html
pub trait Renderer<Color> { pub trait Renderer {
/// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`]
/// contents and size. /// contents and size.
/// ///
@ -188,13 +182,11 @@ pub trait Renderer<Color> {
); );
} }
impl<'a, Message, Renderer, Color> From<Text<Color>> impl<'a, Message, Renderer> From<Text> for Element<'a, Message, Renderer>
for Element<'a, Message, Renderer>
where where
Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer,
Renderer: self::Renderer<Color>,
{ {
fn from(text: Text<Color>) -> Element<'a, Message, Renderer> { fn from(text: Text) -> Element<'a, Message, Renderer> {
Element::new(text) Element::new(text)
} }
} }

View file

@ -1,8 +1,8 @@
use crate::{Bus, Color, Element, Widget}; use crate::{Bus, Element, Widget};
use dodrio::bumpalo; use dodrio::bumpalo;
pub type Checkbox<Message> = iced::Checkbox<Color, Message>; pub use iced::Checkbox;
impl<Message> Widget<Message> for Checkbox<Message> impl<Message> Widget<Message> for Checkbox<Message>
where where

View file

@ -1,8 +1,8 @@
use crate::{Bus, Color, Element, Widget}; use crate::{Bus, Element, Widget};
use dodrio::bumpalo; use dodrio::bumpalo;
pub type Radio<Message> = iced::Radio<Color, Message>; pub use iced::Radio;
impl<Message> Widget<Message> for Radio<Message> impl<Message> Widget<Message> for Radio<Message>
where where

View file

@ -1,9 +1,7 @@
use crate::{Bus, Color, Element, Widget}; use crate::{Bus, Element, Widget};
use dodrio::bumpalo; use dodrio::bumpalo;
pub use iced::text::HorizontalAlignment; pub use iced::text::*;
pub type Text = iced::Text<Color>;
impl<'a, Message> Widget<Message> for Text { impl<'a, Message> Widget<Message> for Text {
fn node<'b>( fn node<'b>(