diff options
Diffstat (limited to 'src/widget/text.rs')
-rw-r--r-- | src/widget/text.rs | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/src/widget/text.rs b/src/widget/text.rs index e1ce8b16..7b62e5cb 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -5,13 +5,12 @@ use crate::{ use std::hash::Hash; -/// A fragment of text. +/// A fragment of text with a generic `Color`. /// -/// It implements [`Widget`] when the associated [`core::Renderer`] implements -/// the [`text::Renderer`] trait. +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`text::Renderer`] trait. /// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`core::Renderer`]: ../../core/trait.Renderer.html +/// [`Widget`]: ../trait.Widget.html /// [`text::Renderer`]: trait.Renderer.html /// /// # Example @@ -19,24 +18,26 @@ use std::hash::Hash; /// ``` /// use iced::Text; /// -/// Text::<(f32, f32, f32)>::new("I <3 iced!") +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// Text::new("I <3 iced!") /// .size(40) -/// .color((0.0, 0.0, 1.0)); +/// .color(Color::Black); /// ``` #[derive(Debug, Clone)] pub struct Text<Color> { content: String, size: u16, - color: Color, + color: Option<Color>, style: Style, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, } -impl<Color> Text<Color> -where - Color: Default, -{ +impl<Color> Text<Color> { /// Create a new fragment of [`Text`] with the given contents. /// /// [`Text`]: struct.Text.html @@ -44,7 +45,7 @@ where Text { content: String::from(label), size: 20, - color: Color::default(), + color: None, style: Style::default().fill_width(), horizontal_alignment: HorizontalAlignment::Left, vertical_alignment: VerticalAlignment::Top, @@ -59,12 +60,11 @@ where self } - /// Sets the [`Color`] of the [`Text`]. + /// Sets the `Color` of the [`Text`]. /// /// [`Text`]: struct.Text.html - /// [`Color`]: ../../../graphics/struct.Color.html pub fn color(mut self, color: Color) -> Self { - self.color = color; + self.color = Some(color); self } @@ -87,7 +87,7 @@ where /// Sets the [`HorizontalAlignment`] of the [`Text`]. /// /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: ../../../graphics/enum.HorizontalAlignment.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html pub fn horizontal_alignment( mut self, alignment: HorizontalAlignment, @@ -99,7 +99,7 @@ where /// Sets the [`VerticalAlignment`] of the [`Text`]. /// /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: ../../../graphics/enum.VerticalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { self.vertical_alignment = alignment; self @@ -112,7 +112,7 @@ where Renderer: self::Renderer<Color>, { fn node(&self, renderer: &Renderer) -> Node { - renderer.node(self.style, &self.content, self.size as f32) + renderer.node(self.style, &self.content, f32::from(self.size)) } fn draw( @@ -124,7 +124,7 @@ where renderer.draw( layout.bounds(), &self.content, - self.size as f32, + f32::from(self.size), self.color, self.horizontal_alignment, self.vertical_alignment, @@ -133,7 +133,7 @@ where MouseCursor::OutOfBounds } - fn hash(&self, state: &mut Hasher) { + fn hash_layout(&self, state: &mut Hasher) { self.style.hash(state); self.content.hash(state); @@ -141,13 +141,14 @@ where } } -/// The renderer of a [`Text`] fragment. +/// The renderer of a [`Text`] fragment with a generic `Color`. /// -/// Your [`core::Renderer`] will need to implement this trait before being -/// able to use a [`Text`] in your user interface. +/// Your [renderer] will need to implement this trait before being +/// able to use [`Text`] in your [`UserInterface`]. /// /// [`Text`]: struct.Text.html -/// [`core::Renderer`]: ../../core/trait.Renderer.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html pub trait Renderer<Color> { /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] /// contents and size. @@ -155,10 +156,10 @@ pub trait Renderer<Color> { /// You should probably use [`Node::with_measure`] to allow [`Text`] to /// adapt to the dimensions of its container. /// - /// [`Node`]: ../../core/struct.Node.html - /// [`Style`]: ../../core/struct.Style.html + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../core/struct.Node.html#method.with_measure + /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure fn node(&self, style: Style, content: &str, size: f32) -> Node; /// Draws a [`Text`] fragment. @@ -172,14 +173,14 @@ pub trait Renderer<Color> { /// * the [`VerticalAlignment`] of the [`Text`] /// /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: ../../../graphics/enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: ../../../graphics/enum.VerticalAlignment.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html fn draw( &mut self, - bounds: Rectangle<f32>, + bounds: Rectangle, content: &str, size: f32, - color: Color, + color: Option<Color>, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, ); |