From 03b34931383e701c39c653a7662a616fe21a0947 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:07:22 +0700 Subject: Remove trait-specific draw logic in `iced_native` --- native/src/widget/text.rs | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 168d49c2..9915a6e9 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -133,22 +133,13 @@ where fn draw( &self, - renderer: &mut Renderer, - defaults: &Renderer::Defaults, - layout: Layout<'_>, + _renderer: &mut Renderer, + _defaults: &Renderer::Defaults, + _layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, - ) -> Renderer::Output { - renderer.draw( - defaults, - layout.bounds(), - &self.content, - self.size.unwrap_or(renderer.default_size()), - self.font, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ) + ) { + // TODO } fn hash_layout(&self, state: &mut Hasher) { @@ -201,27 +192,6 @@ pub trait Renderer: crate::Renderer { point: Point, nearest_only: bool, ) -> Option; - - /// 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`] - fn draw( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - content: &str, - size: u16, - font: Self::Font, - color: Option, - horizontal_alignment: alignment::Horizontal, - vertical_alignment: alignment::Vertical, - ) -> Self::Output; } impl<'a, Message, Renderer> From> -- cgit From 3a0c503db99eb3d45ac971132904df419ee566b6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:59:19 +0700 Subject: Implement `Widget::draw` for `Text` --- native/src/widget/text.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 9915a6e9..ea1ba7ac 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -1,6 +1,7 @@ //! Write some text for your users to read. use crate::alignment; use crate::layout; +use crate::renderer; use crate::{ Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -133,13 +134,35 @@ where fn draw( &self, - _renderer: &mut Renderer, + renderer: &mut Renderer, _defaults: &Renderer::Defaults, - _layout: Layout<'_>, + layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, ) { - // TODO + let bounds = layout.bounds(); + + let x = match self.horizontal_alignment { + alignment::Horizontal::Left => bounds.x, + alignment::Horizontal::Center => bounds.center_x(), + alignment::Horizontal::Right => bounds.x + bounds.width, + }; + + let y = match self.vertical_alignment { + alignment::Vertical::Top => bounds.y, + alignment::Vertical::Center => bounds.center_y(), + alignment::Vertical::Bottom => bounds.y + bounds.height, + }; + + renderer.fill_text(renderer::text::Section { + content: &self.content, + size: self.size.map(f32::from), + bounds: Rectangle { x, y, ..bounds }, + color: self.color, + font: self.font, + horizontal_alignment: self.horizontal_alignment, + vertical_alignment: self.vertical_alignment, + }); } fn hash_layout(&self, state: &mut Hasher) { @@ -159,10 +182,7 @@ where /// able to use [`Text`] in your user interface. /// /// [renderer]: crate::Renderer -pub trait Renderer: crate::Renderer { - /// The font type used for [`Text`]. - type Font: Default + Copy; - +pub trait Renderer: renderer::Text { /// Returns the default size of [`Text`]. fn default_size(&self) -> u16; -- cgit From edea093350e1b576e2b7db50c525e7fa5c3bea9f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:19:04 +0700 Subject: Move `Defaults` from `iced_graphics` to `iced_native` --- native/src/widget/text.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index ea1ba7ac..a2438d9c 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -135,7 +135,7 @@ where fn draw( &self, renderer: &mut Renderer, - _defaults: &Renderer::Defaults, + style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, @@ -156,9 +156,9 @@ where renderer.fill_text(renderer::text::Section { content: &self.content, - size: self.size.map(f32::from), + size: f32::from(self.size.unwrap_or(renderer.default_size())), bounds: Rectangle { x, y, ..bounds }, - color: self.color, + color: self.color.unwrap_or(style.text_color), font: self.font, horizontal_alignment: self.horizontal_alignment, vertical_alignment: self.vertical_alignment, -- cgit From e914888f57394e4b67b40e42f1ad9df4ae8147e6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Oct 2021 18:40:39 +0700 Subject: Implement `Widget::draw` for `TextInput` --- native/src/widget/text.rs | 48 +++++------------------------------------------ 1 file changed, 5 insertions(+), 43 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index a2438d9c..78bfa4e2 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -24,7 +24,7 @@ use std::hash::Hash; /// /// ![Text drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true) #[derive(Debug)] -pub struct Text { +pub struct Text { content: String, size: Option, color: Option, @@ -35,7 +35,7 @@ pub struct Text { vertical_alignment: alignment::Vertical, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. pub fn new>(label: T) -> Self { Text { @@ -103,7 +103,7 @@ impl Text { impl Widget for Text where - Renderer: self::Renderer, + Renderer: renderer::Text, { fn width(&self) -> Length { self.width @@ -176,55 +176,17 @@ where } } -/// The renderer of a [`Text`] fragment. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your user interface. -/// -/// [renderer]: crate::Renderer -pub trait Renderer: renderer::Text { - /// Returns the default size of [`Text`]. - fn default_size(&self) -> u16; - - /// Measures the [`Text`] in the given bounds and returns the minimum - /// boundaries that can fit the contents. - fn measure( - &self, - content: &str, - size: u16, - font: Self::Font, - bounds: Size, - ) -> (f32, f32); - - /// Tests whether the provided point is within the boundaries of [`Text`] - /// laid out with the given parameters, returning information about - /// the nearest character. - /// - /// If `nearest_only` is true, the hit test does not consider whether the - /// the point is interior to any glyph bounds, returning only the character - /// with the nearest centeroid. - fn hit_test( - &self, - contents: &str, - size: f32, - font: Self::Font, - bounds: Size, - point: Point, - nearest_only: bool, - ) -> Option; -} - impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: self::Renderer + 'a, + Renderer: renderer::Text + 'a, { fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } -impl Clone for Text { +impl Clone for Text { fn clone(&self) -> Self { Self { content: self.content.clone(), -- cgit From d39ad717ed0ab85acbe935d7ab883166b36e7bc7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Oct 2021 19:06:53 +0700 Subject: Wire up styling to `Radio` in `iced_native` --- native/src/widget/text.rs | 72 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 23 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 78bfa4e2..2432dbc8 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -140,29 +140,17 @@ where _cursor_position: Point, _viewport: &Rectangle, ) { - let bounds = layout.bounds(); - - let x = match self.horizontal_alignment { - alignment::Horizontal::Left => bounds.x, - alignment::Horizontal::Center => bounds.center_x(), - alignment::Horizontal::Right => bounds.x + bounds.width, - }; - - let y = match self.vertical_alignment { - alignment::Vertical::Top => bounds.y, - alignment::Vertical::Center => bounds.center_y(), - alignment::Vertical::Bottom => bounds.y + bounds.height, - }; - - renderer.fill_text(renderer::text::Section { - content: &self.content, - size: f32::from(self.size.unwrap_or(renderer.default_size())), - bounds: Rectangle { x, y, ..bounds }, - color: self.color.unwrap_or(style.text_color), - font: self.font, - horizontal_alignment: self.horizontal_alignment, - vertical_alignment: self.vertical_alignment, - }); + draw( + renderer, + style, + layout, + &self.content, + self.font, + self.size, + self.color, + self.horizontal_alignment, + self.vertical_alignment, + ); } fn hash_layout(&self, state: &mut Hasher) { @@ -176,6 +164,44 @@ where } } +pub fn draw( + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + content: &str, + font: Renderer::Font, + size: Option, + color: Option, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, +) where + Renderer: renderer::Text, +{ + let bounds = layout.bounds(); + + let x = match horizontal_alignment { + alignment::Horizontal::Left => bounds.x, + alignment::Horizontal::Center => bounds.center_x(), + alignment::Horizontal::Right => bounds.x + bounds.width, + }; + + let y = match vertical_alignment { + alignment::Vertical::Top => bounds.y, + alignment::Vertical::Center => bounds.center_y(), + alignment::Vertical::Bottom => bounds.y + bounds.height, + }; + + renderer.fill_text(renderer::text::Section { + content, + size: f32::from(size.unwrap_or(renderer.default_size())), + bounds: Rectangle { x, y, ..bounds }, + color: color.unwrap_or(style.text_color), + font, + horizontal_alignment, + vertical_alignment, + }); +} + impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where -- cgit From 0aafcde0ef1533c9eeba0379de8c0082e30c7504 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 15:35:12 +0700 Subject: Remove `widget` module re-exports in `iced_native` --- native/src/widget/text.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 2432dbc8..563ab8c4 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -15,7 +15,7 @@ use std::hash::Hash; /// # Example /// /// ``` -/// # type Text = iced_native::Text; +/// # type Text = iced_native::widget::Text; /// # /// Text::new("I <3 iced!") /// .color([0.0, 0.0, 1.0]) -- cgit From b3a01973c6c726e6539be959659f4306ef3234c6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:13:03 +0700 Subject: Introduce first-class `text` module in `iced_native` --- native/src/widget/text.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 563ab8c4..79688b28 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -2,12 +2,11 @@ use crate::alignment; use crate::layout; use crate::renderer; +use crate::text; use crate::{ Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; -pub use iced_core::text::Hit; - use std::hash::Hash; /// A paragraph of text. @@ -24,7 +23,7 @@ use std::hash::Hash; /// /// ![Text drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true) #[derive(Debug)] -pub struct Text { +pub struct Text { content: String, size: Option, color: Option, @@ -35,7 +34,7 @@ pub struct Text { vertical_alignment: alignment::Vertical, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. pub fn new>(label: T) -> Self { Text { @@ -103,7 +102,7 @@ impl Text { impl Widget for Text where - Renderer: renderer::Text, + Renderer: text::Renderer, { fn width(&self) -> Length { self.width @@ -175,7 +174,7 @@ pub fn draw( horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, ) where - Renderer: renderer::Text, + Renderer: text::Renderer, { let bounds = layout.bounds(); @@ -191,7 +190,7 @@ pub fn draw( alignment::Vertical::Bottom => bounds.y + bounds.height, }; - renderer.fill_text(renderer::text::Section { + renderer.fill_text(crate::text::Text { content, size: f32::from(size.unwrap_or(renderer.default_size())), bounds: Rectangle { x, y, ..bounds }, @@ -205,14 +204,14 @@ pub fn draw( impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: renderer::Text + 'a, + Renderer: text::Renderer + 'a, { fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } -impl Clone for Text { +impl Clone for Text { fn clone(&self) -> Self { Self { content: self.content.clone(), -- cgit From 023aded2772f0cd6abd716fe5c8624d5d22e21fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 19:22:29 +0700 Subject: Rename `fill_rectangle` to `fill_quad` in `Renderer` --- native/src/widget/text.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'native/src/widget/text.rs') diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 79688b28..4dbc4a65 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -163,6 +163,16 @@ where } } +/// Draws text using the same logic as the [`Text`] widget. +/// +/// Specifically: +/// +/// * If no `size` is provided, the default text size of the `Renderer` will be +/// used. +/// * If no `color` is provided, the [`renderer::Style::text_color`] will be +/// used. +/// * The alignment attributes do not affect the position of the bounds of the +/// [`Layout`]. pub fn draw( renderer: &mut Renderer, style: &renderer::Style, -- cgit