diff options
author | 2024-03-04 19:31:26 +0100 | |
---|---|---|
committer | 2024-03-04 19:32:13 +0100 | |
commit | 4130ae4be95ce850263fbc55f490b68a95361d58 (patch) | |
tree | 25b1acc723e217e5890e20aa8e796db0f35be231 /core | |
parent | ce309db37b8eb9860ae1f1be1710fb39e7a9edea (diff) | |
download | iced-4130ae4be95ce850263fbc55f490b68a95361d58.tar.gz iced-4130ae4be95ce850263fbc55f490b68a95361d58.tar.bz2 iced-4130ae4be95ce850263fbc55f490b68a95361d58.zip |
Simplify theming for `Text` widget
Diffstat (limited to 'core')
-rw-r--r-- | core/src/widget/text.rs | 65 |
1 files changed, 48 insertions, 17 deletions
diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 0796c4e4..217ad8b3 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -29,7 +29,7 @@ where vertical_alignment: alignment::Vertical, font: Option<Renderer::Font>, shaping: Shaping, - style: Theme::Style, + style: Style<Theme>, } impl<'a, Theme, Renderer> Text<'a, Theme, Renderer> @@ -49,7 +49,7 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: Shaping::Basic, - style: Default::default(), + style: Style::Themed(Theme::default()), } } @@ -74,8 +74,20 @@ where } /// Sets the style of the [`Text`]. - pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { - self.style = style.into(); + pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { + self.style = Style::Themed(style); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color(mut self, color: impl Into<Color>) -> Self { + self.style = Style::Colored(Some(color.into())); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self { + self.style = Style::Colored(color.map(Into::into)); self } @@ -175,14 +187,12 @@ where ) { let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>(); - draw( - renderer, - style, - layout, - state, - theme.appearance(self.style.clone()), - viewport, - ); + let appearance = match self.style { + Style::Themed(f) => f(theme), + Style::Colored(color) => Appearance { color }, + }; + + draw(renderer, style, layout, state, appearance, viewport); } } @@ -298,7 +308,7 @@ where horizontal_alignment: self.horizontal_alignment, vertical_alignment: self.vertical_alignment, font: self.font, - style: self.style.clone(), + style: self.style, shaping: self.shaping, } } @@ -327,11 +337,18 @@ where /// The style sheet of some text. pub trait StyleSheet { - /// The supported style of the [`StyleSheet`]. - type Style: Default + Clone; + /// Returns the default styling strategy for [`Text`]. + fn default() -> fn(&Self) -> Appearance { + |_| Appearance::default() + } +} - /// Produces the [`Appearance`] of some text. - fn appearance(&self, style: Self::Style) -> Appearance; +impl StyleSheet for Color { + fn default() -> fn(&Self) -> Appearance { + |color| Appearance { + color: Some(*color), + } + } } /// The apperance of some text. @@ -342,3 +359,17 @@ pub struct Appearance { /// The default, `None`, means using the inherited color. pub color: Option<Color>, } + +#[derive(Debug)] +enum Style<Theme> { + Themed(fn(&Theme) -> Appearance), + Colored(Option<Color>), +} + +impl<Theme> Clone for Style<Theme> { + fn clone(&self) -> Self { + *self + } +} + +impl<Theme> Copy for Style<Theme> {} |