From 4130ae4be95ce850263fbc55f490b68a95361d58 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Mar 2024 19:31:26 +0100 Subject: Simplify theming for `Text` widget --- core/src/widget/text.rs | 65 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 17 deletions(-) (limited to 'core/src/widget') 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, shaping: Shaping, - style: Theme::Style, + style: Style, } 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) -> 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) -> Self { + self.style = Style::Colored(Some(color.into())); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color_maybe(mut self, color: Option>) -> Self { + self.style = Style::Colored(color.map(Into::into)); self } @@ -175,14 +187,12 @@ where ) { let state = tree.state.downcast_ref::>(); - 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, } + +#[derive(Debug)] +enum Style { + Themed(fn(&Theme) -> Appearance), + Colored(Option), +} + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} -- cgit