diff --git a/native/src/element.rs b/native/src/element.rs index f29580fc..73e39012 100644 --- a/native/src/element.rs +++ b/native/src/element.rs @@ -81,7 +81,7 @@ where /// /// ``` /// # mod counter { - /// # use iced_native::{text, Text}; + /// # type Text = iced_native::Text; /// # /// # #[derive(Debug, Clone, Copy)] /// # pub enum Message {} diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 9033a7da..2c7babdb 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -47,6 +47,8 @@ impl row::Renderer for Null { } impl text::Renderer for Null { + type Font = Font; + const DEFAULT_SIZE: u16 = 20; fn measure( diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index d611993f..4d167df7 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -3,7 +3,7 @@ use std::hash::Hash; use crate::{ input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, + layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -186,7 +186,7 @@ where label_layout.bounds(), &self.label, self.text_size, - Font::Default, + Default::default(), None, HorizontalAlignment::Left, VerticalAlignment::Center, diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 0ec621bf..a13d86a0 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,7 +1,7 @@ //! Create choices using radio buttons. use crate::{ input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, + layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -155,7 +155,7 @@ where label_layout.bounds(), &self.label, ::DEFAULT_SIZE, - Font::Default, + Default::default(), None, HorizontalAlignment::Left, VerticalAlignment::Center, diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index dc7c33ec..d60aa468 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -1,7 +1,7 @@ //! Write some text for your users to read. use crate::{ - layout, Color, Element, Font, Hasher, HorizontalAlignment, Layout, Length, - Point, Rectangle, Size, VerticalAlignment, Widget, + layout, Color, Element, Hasher, HorizontalAlignment, Layout, Length, Point, + Rectangle, Size, VerticalAlignment, Widget, }; use std::hash::Hash; @@ -11,7 +11,7 @@ use std::hash::Hash; /// # Example /// /// ``` -/// # use iced_native::Text; +/// # type Text = iced_native::Text; /// # /// Text::new("I <3 iced!") /// .color([0.0, 0.0, 1.0]) @@ -20,18 +20,18 @@ use std::hash::Hash; /// /// ![Text drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true) #[derive(Debug, Clone)] -pub struct Text { +pub struct Text { content: String, size: Option, color: Option, - font: Font, + font: Renderer::Font, width: Length, height: Length, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. /// /// [`Text`]: struct.Text.html @@ -40,7 +40,7 @@ impl Text { content: label.into(), size: None, color: None, - font: Font::Default, + font: Default::default(), width: Length::Shrink, height: Length::Shrink, horizontal_alignment: HorizontalAlignment::Left, @@ -69,8 +69,8 @@ impl Text { /// /// [`Text`]: struct.Text.html /// [`Font`]: ../../struct.Font.html - pub fn font(mut self, font: Font) -> Self { - self.font = font; + pub fn font(mut self, font: impl Into) -> Self { + self.font = font.into(); self } @@ -112,7 +112,7 @@ impl Text { } } -impl Widget for Text +impl Widget for Text where Renderer: self::Renderer, { @@ -163,7 +163,8 @@ where } fn hash_layout(&self, state: &mut Hasher) { - std::any::TypeId::of::().hash(state); + struct Marker; + std::any::TypeId::of::().hash(state); self.content.hash(state); self.size.hash(state); @@ -181,6 +182,11 @@ where /// [renderer]: ../../renderer/index.html /// [`UserInterface`]: ../../struct.UserInterface.html pub trait Renderer: crate::Renderer { + /// The font type used for [`Text`]. + /// + /// [`Text`]: struct.Text.html + type Font: Default + Copy; + /// The default size of [`Text`]. /// /// [`Text`]: struct.Text.html @@ -194,7 +200,7 @@ pub trait Renderer: crate::Renderer { &self, content: &str, size: u16, - font: Font, + font: Self::Font, bounds: Size, ) -> (f32, f32); @@ -217,18 +223,19 @@ pub trait Renderer: crate::Renderer { bounds: Rectangle, content: &str, size: u16, - font: Font, + font: Self::Font, color: Option, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, ) -> Self::Output; } -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: self::Renderer + 'a, { - fn from(text: Text) -> Element<'a, Message, Renderer> { + fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } diff --git a/src/widget.rs b/src/widget.rs index 03e3192b..e33a6b2c 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -20,7 +20,7 @@ mod platform { pub use iced_wgpu::widget::{ button, checkbox, container, pane_grid, progress_bar, radio, - scrollable, slider, text_input, + scrollable, slider, text_input, Text, }; #[cfg(feature = "canvas")] @@ -39,7 +39,7 @@ mod platform { pub use iced_winit::svg::{Handle, Svg}; } - pub use iced_winit::{Space, Text}; + pub use iced_winit::Space; #[doc(no_inline)] pub use { diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 80bff574..f27cc430 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -7,6 +7,8 @@ use iced_native::{ use std::f32; impl text::Renderer for Renderer { + type Font = Font; + const DEFAULT_SIZE: u16 = 20; fn measure( diff --git a/wgpu/src/widget.rs b/wgpu/src/widget.rs index c3a47dff..32ccad17 100644 --- a/wgpu/src/widget.rs +++ b/wgpu/src/widget.rs @@ -17,6 +17,8 @@ pub mod scrollable; pub mod slider; pub mod text_input; +mod text; + #[doc(no_inline)] pub use button::Button; #[doc(no_inline)] @@ -36,6 +38,8 @@ pub use slider::Slider; #[doc(no_inline)] pub use text_input::TextInput; +pub use text::Text; + #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] pub mod canvas; diff --git a/wgpu/src/widget/text.rs b/wgpu/src/widget/text.rs new file mode 100644 index 00000000..1053ea97 --- /dev/null +++ b/wgpu/src/widget/text.rs @@ -0,0 +1,7 @@ +//! Write some text for your users to read. +use crate::Renderer; + +/// A paragraph of text. +/// +/// This is an alias of an `iced_native` text with an `iced_wgpu::Renderer`. +pub type Text = iced_native::Text;