Remove generic Color in widgets
This commit is contained in:
parent
f9de39ddaa
commit
b83a4b42dd
12 changed files with 83 additions and 120 deletions
|
|
@ -2,8 +2,6 @@ use super::{into_color, Renderer};
|
|||
use ggez::graphics::{DrawMode, MeshBuilder, Rect};
|
||||
|
||||
impl iced::renderer::Debugger for Renderer<'_> {
|
||||
type Color = iced::Color;
|
||||
|
||||
fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use iced::text;
|
|||
use std::cell::RefCell;
|
||||
use std::f32;
|
||||
|
||||
impl text::Renderer<iced::Color> for Renderer<'_> {
|
||||
impl text::Renderer for Renderer<'_> {
|
||||
fn node(
|
||||
&self,
|
||||
style: iced::Style,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
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 Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use stretch::{geometry, result};
|
||||
|
||||
use crate::{
|
||||
renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget,
|
||||
renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget,
|
||||
};
|
||||
|
||||
/// A generic [`Widget`].
|
||||
|
|
@ -210,16 +210,16 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> {
|
|||
///
|
||||
/// [`Element`]: struct.Element.html
|
||||
/// [`Renderer`]: trait.Renderer.html
|
||||
pub fn explain(
|
||||
pub fn explain<C: Into<Color>>(
|
||||
self,
|
||||
color: Renderer::Color,
|
||||
color: C,
|
||||
) -> Element<'a, Message, Renderer>
|
||||
where
|
||||
Message: 'static,
|
||||
Renderer: 'a + renderer::Debugger,
|
||||
{
|
||||
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> {
|
||||
element: Element<'a, Message, Renderer>,
|
||||
color: Renderer::Color,
|
||||
color: Color,
|
||||
}
|
||||
|
||||
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
|
||||
Renderer: renderer::Debugger,
|
||||
{
|
||||
fn new(
|
||||
element: Element<'a, Message, Renderer>,
|
||||
color: Renderer::Color,
|
||||
) -> Self {
|
||||
fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self {
|
||||
Explain { element, color }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@
|
|||
//! # mod iced_wgpu {
|
||||
//! # use iced::{
|
||||
//! # button, text, text::HorizontalAlignment, text::VerticalAlignment,
|
||||
//! # MouseCursor, Node, Point, Rectangle, Style,
|
||||
//! # MouseCursor, Node, Point, Rectangle, Style, Color
|
||||
//! # };
|
||||
//! #
|
||||
//! # 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 {
|
||||
//! # Node::new(style)
|
||||
//! # }
|
||||
|
|
@ -106,7 +106,7 @@
|
|||
//! # _bounds: Rectangle,
|
||||
//! # _content: &str,
|
||||
//! # _size: Option<u16>,
|
||||
//! # _color: Option<[f32; 4]>,
|
||||
//! # _color: Option<Color>,
|
||||
//! # _horizontal_alignment: HorizontalAlignment,
|
||||
//! # _vertical_alignment: VerticalAlignment,
|
||||
//! # ) {
|
||||
|
|
|
|||
|
|
@ -17,19 +17,12 @@
|
|||
//! [`text::Renderer`]: ../widget/text/trait.Renderer.html
|
||||
//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html
|
||||
//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html
|
||||
use crate::Layout;
|
||||
use crate::{Color, Layout};
|
||||
|
||||
/// A renderer able to graphically explain a [`Layout`].
|
||||
///
|
||||
/// [`Layout`]: ../struct.Layout.html
|
||||
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.
|
||||
///
|
||||
/// This will be called when [`Element::explain`] has been used. It should
|
||||
|
|
@ -41,5 +34,5 @@ pub trait Debugger {
|
|||
/// [`Layout`]: struct.Layout.html
|
||||
/// [`Element`]: struct.Element.html
|
||||
/// [`Element::explain`]: struct.Element.html#method.explain
|
||||
fn explain(&mut self, layout: &Layout<'_>, color: Self::Color);
|
||||
fn explain(&mut self, layout: &Layout<'_>, color: Color);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::{Bus, Color, Element, Widget};
|
||||
use crate::{Bus, Element, Widget};
|
||||
|
||||
use dodrio::bumpalo;
|
||||
|
||||
pub type Checkbox<Message> = iced::Checkbox<Color, Message>;
|
||||
pub use iced::Checkbox;
|
||||
|
||||
impl<Message> Widget<Message> for Checkbox<Message>
|
||||
where
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::{Bus, Color, Element, Widget};
|
||||
use crate::{Bus, Element, Widget};
|
||||
|
||||
use dodrio::bumpalo;
|
||||
|
||||
pub type Radio<Message> = iced::Radio<Color, Message>;
|
||||
pub use iced::Radio;
|
||||
|
||||
impl<Message> Widget<Message> for Radio<Message>
|
||||
where
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
use crate::{Bus, Color, Element, Widget};
|
||||
use crate::{Bus, Element, Widget};
|
||||
use dodrio::bumpalo;
|
||||
|
||||
pub use iced::text::HorizontalAlignment;
|
||||
|
||||
pub type Text = iced::Text<Color>;
|
||||
pub use iced::text::*;
|
||||
|
||||
impl<'a, Message> Widget<Message> for Text {
|
||||
fn node<'b>(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue