From 8b8f7563ad33dafeadf6238e377748cdec17d67a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Sep 2019 19:41:49 +0200 Subject: Switch to workspace layout --- src/widget/text.rs | 224 ----------------------------------------------------- 1 file changed, 224 deletions(-) delete mode 100644 src/widget/text.rs (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs deleted file mode 100644 index 59b599bb..00000000 --- a/src/widget/text.rs +++ /dev/null @@ -1,224 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A fragment of text with a generic `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// Text::new("I <3 iced!") -/// .size(40) -/// .color(Color::Black); -/// ``` -#[derive(Debug, Clone)] -pub struct Text { - content: String, - size: Option, - color: Option, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color(mut self, color: Color) -> Self { - self.color = Some(color); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment with a generic `Color`. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer, Color> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn from(text: Text) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} -- cgit From a97401aed2a173260a4abfdb65a77975ce6c0f01 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Sep 2019 19:16:06 +0200 Subject: Rethink workspace structure --- src/widget/text.rs | 224 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 src/widget/text.rs (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs new file mode 100644 index 00000000..59b599bb --- /dev/null +++ b/src/widget/text.rs @@ -0,0 +1,224 @@ +//! Write some text for your users to read. +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +}; + +use std::hash::Hash; + +/// A fragment of text with a generic `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`text::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`text::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Text; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// Text::new("I <3 iced!") +/// .size(40) +/// .color(Color::Black); +/// ``` +#[derive(Debug, Clone)] +pub struct Text { + content: String, + size: Option, + color: Option, + style: Style, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, +} + +impl Text { + /// Create a new fragment of [`Text`] with the given contents. + /// + /// [`Text`]: struct.Text.html + pub fn new(label: &str) -> Self { + Text { + content: String::from(label), + size: None, + color: None, + style: Style::default().fill_width(), + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Top, + } + } + + /// Sets the size of the [`Text`] in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn size(mut self, size: u16) -> Self { + self.size = Some(size); + self + } + + /// Sets the `Color` of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + pub fn color(mut self, color: Color) -> Self { + self.color = Some(color); + self + } + + /// Sets the width of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the [`HorizontalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + pub fn horizontal_alignment( + mut self, + alignment: HorizontalAlignment, + ) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the [`VerticalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { + self.vertical_alignment = alignment; + self + } +} + +impl Widget for Text +where + Color: Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + renderer.node(self.style, &self.content, self.size) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + layout.bounds(), + &self.content, + self.size, + self.color, + self.horizontal_alignment, + self.vertical_alignment, + ); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + + self.content.hash(state); + self.size.hash(state); + } +} + +/// The renderer of a [`Text`] fragment with a generic `Color`. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use [`Text`] in your [`UserInterface`]. +/// +/// [`Text`]: struct.Text.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] + /// contents and size. + /// + /// You should probably use [`Node::with_measure`] to allow [`Text`] to + /// adapt to the dimensions of its container. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Text`]: struct.Text.html + /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure + fn node(&self, style: Style, content: &str, size: Option) -> Node; + + /// Draws a [`Text`] fragment. + /// + /// It receives: + /// * the bounds of the [`Text`] + /// * the contents of the [`Text`] + /// * the size of the [`Text`] + /// * the color of the [`Text`] + /// * the [`HorizontalAlignment`] of the [`Text`] + /// * the [`VerticalAlignment`] of the [`Text`] + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + fn draw( + &mut self, + bounds: Rectangle, + content: &str, + size: Option, + color: Option, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, + ); +} + +impl<'a, Message, Renderer, Color> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn from(text: Text) -> Element<'a, Message, Renderer> { + Element::new(text) + } +} + +/// The horizontal alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HorizontalAlignment { + /// Align left + Left, + + /// Horizontally centered + Center, + + /// Align right + Right, +} + +/// The vertical alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum VerticalAlignment { + /// Align top + Top, + + /// Vertically centered + Center, + + /// Align bottom + Bottom, +} -- cgit From 8834772fa70850559f7bd82cc8432394e3fd9db7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 15 Sep 2019 17:43:15 +0200 Subject: Draft widget nodes and wire interaction --- src/widget/text.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs index 59b599bb..b529cfd2 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -29,8 +29,10 @@ use std::hash::Hash; /// ``` #[derive(Debug, Clone)] pub struct Text { - content: String, - size: Option, + /// The text contents + pub content: String, + /// The text size + pub size: Option, color: Option, style: Style, horizontal_alignment: HorizontalAlignment, -- cgit From f9de39ddaa3020a9585b1648afb0ead45dfd7aa9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Sep 2019 15:01:12 +0200 Subject: Unify `web` and `ggez` tour examples :tada: --- src/widget/text.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs index b529cfd2..457a6814 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -113,7 +113,7 @@ where Color: Copy + std::fmt::Debug, Renderer: self::Renderer, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { renderer.node(self.style, &self.content, self.size) } -- cgit 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/text.rs | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs index 457a6814..4ef10d52 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -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 { +pub struct Text { /// The text contents pub content: String, /// The text size @@ -39,7 +34,7 @@ pub struct Text { vertical_alignment: VerticalAlignment, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. /// /// [`Text`]: struct.Text.html @@ -65,8 +60,8 @@ impl Text { /// 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>(mut self, color: C) -> Self { + self.color = Some(color.into()); self } @@ -108,10 +103,9 @@ impl Text { } } -impl Widget for Text +impl Widget for Text where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer, + 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 { +pub trait Renderer { /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] /// contents and size. /// @@ -188,13 +182,11 @@ pub trait Renderer { ); } -impl<'a, Message, Renderer, Color> From> - for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer, + Renderer: self::Renderer, { - fn from(text: Text) -> Element<'a, Message, Renderer> { + fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } -- cgit From b9e0f7494881ad7cdfbcbc16878ecc6ef717753f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Sep 2019 19:15:31 +0200 Subject: Create `iced_core` and `iced_native` --- src/widget/text.rs | 218 ----------------------------------------------------- 1 file changed, 218 deletions(-) delete mode 100644 src/widget/text.rs (limited to 'src/widget/text.rs') diff --git a/src/widget/text.rs b/src/widget/text.rs deleted file mode 100644 index 4ef10d52..00000000 --- a/src/widget/text.rs +++ /dev/null @@ -1,218 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Color, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A paragraph of text. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{Text, Color}; -/// -/// Text::new("I <3 iced!") -/// .size(40); -/// ``` -#[derive(Debug, Clone)] -pub struct Text { - /// The text contents - pub content: String, - /// The text size - pub size: Option, - color: Option, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color>(mut self, color: C) -> Self { - self.color = Some(color.into()); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(text: Text) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} -- cgit