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/widget | |
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/widget')
-rw-r--r-- | native/src/widget/text.rs | 34 |
1 files changed, 27 insertions, 7 deletions
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; |