diff options
author | 2021-10-14 16:59:19 +0700 | |
---|---|---|
committer | 2021-10-14 17:00:38 +0700 | |
commit | 3a0c503db99eb3d45ac971132904df419ee566b6 (patch) | |
tree | 7995e09ef75fb89f7146f4f490582b23193fdcd9 /native/src | |
parent | 03b34931383e701c39c653a7662a616fe21a0947 (diff) | |
download | iced-3a0c503db99eb3d45ac971132904df419ee566b6.tar.gz iced-3a0c503db99eb3d45ac971132904df419ee566b6.tar.bz2 iced-3a0c503db99eb3d45ac971132904df419ee566b6.zip |
Implement `Widget::draw` for `Text`
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/renderer.rs | 8 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 11 | ||||
-rw-r--r-- | native/src/renderer/text.rs | 20 | ||||
-rw-r--r-- | native/src/user_interface.rs | 3 | ||||
-rw-r--r-- | native/src/widget/text.rs | 34 |
5 files changed, 66 insertions, 10 deletions
diff --git a/native/src/renderer.rs b/native/src/renderer.rs index 3784ff24..ca0aecec 100644 --- a/native/src/renderer.rs +++ b/native/src/renderer.rs @@ -19,13 +19,17 @@ //! [`text::Renderer`]: crate::widget::text::Renderer //! [`Checkbox`]: crate::widget::Checkbox //! [`checkbox::Renderer`]: crate::widget::checkbox::Renderer +pub mod text; + +pub use text::Text; #[cfg(debug_assertions)] mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::{layout, Element, Rectangle}; +use crate::layout; +use crate::{Element, Rectangle}; /// A component that can take the state of a user interface and produce an /// output for its users. @@ -48,4 +52,6 @@ pub trait Renderer: Sized { } fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)); + + fn clear(&mut self); } diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 1ffca5c9..da758ebb 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -4,12 +4,13 @@ use crate::container; use crate::pane_grid; use crate::progress_bar; use crate::radio; +use crate::renderer::{self, Renderer}; use crate::scrollable; use crate::slider; use crate::text; use crate::text_input; use crate::toggler; -use crate::{Font, Padding, Point, Rectangle, Renderer, Size}; +use crate::{Font, Padding, Point, Rectangle, Size}; /// A renderer that does nothing. /// @@ -28,11 +29,17 @@ impl Renderer for Null { type Defaults = (); fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {} + + fn clear(&mut self) {} } -impl text::Renderer for Null { +impl renderer::Text for Null { type Font = Font; + fn fill_text(&mut self, _text: renderer::text::Section<'_, Self::Font>) {} +} + +impl text::Renderer for Null { fn default_size(&self) -> u16 { 20 } diff --git a/native/src/renderer/text.rs b/native/src/renderer/text.rs new file mode 100644 index 00000000..5c189d89 --- /dev/null +++ b/native/src/renderer/text.rs @@ -0,0 +1,20 @@ +use crate::alignment; +use crate::{Color, Rectangle, Renderer}; + +pub trait Text: Renderer { + /// The font type used. + type Font: Default + Copy; + + fn fill_text(&mut self, section: Section<'_, Self::Font>); +} + +#[derive(Debug, Clone, Copy)] +pub struct Section<'a, Font> { + pub content: &'a str, + pub bounds: Rectangle, + pub size: Option<f32>, + pub color: Option<Color>, + pub font: Font, + pub horizontal_alignment: alignment::Horizontal, + pub vertical_alignment: alignment::Vertical, +} diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 187c469a..c80aaf44 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -330,6 +330,9 @@ where /// } /// ``` pub fn draw(&mut self, renderer: &mut Renderer, cursor_position: Point) { + // TODO: Move to shell level (?) + renderer.clear(); + let viewport = Rectangle::with_size(self.bounds); let overlay = if let Some(mut overlay) = 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; |