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 From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- core/src/widget/text.rs | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 217ad8b3..e151476d 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -17,7 +17,6 @@ pub use text::{LineHeight, Shaping}; #[allow(missing_debug_implementations)] pub struct Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { content: Cow<'a, str>, @@ -34,7 +33,6 @@ where impl<'a, Theme, Renderer> Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. @@ -49,7 +47,7 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: Shaping::Basic, - style: Style::Themed(Theme::default()), + style: Style::default(), } } @@ -135,7 +133,6 @@ pub struct State(P); impl<'a, Message, Theme, Renderer> Widget for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn tag(&self) -> tree::Tag { @@ -283,7 +280,7 @@ pub fn draw( impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from( @@ -295,7 +292,6 @@ where impl<'a, Theme, Renderer> Clone for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn clone(&self) -> Self { @@ -316,7 +312,6 @@ where impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn from(content: &'a str) -> Self { @@ -327,7 +322,7 @@ where impl<'a, Message, Theme, Renderer> From<&'a str> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from(content: &'a str) -> Self { @@ -335,22 +330,6 @@ where } } -/// The style sheet of some text. -pub trait StyleSheet { - /// Returns the default styling strategy for [`Text`]. - fn default() -> fn(&Self) -> Appearance { - |_| Appearance::default() - } -} - -impl StyleSheet for Color { - fn default() -> fn(&Self) -> Appearance { - |color| Appearance { - color: Some(*color), - } - } -} - /// The apperance of some text. #[derive(Debug, Clone, Copy, Default)] pub struct Appearance { @@ -373,3 +352,15 @@ impl Clone for Style { } impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style::Colored(None) + } +} + +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style::Themed(f) + } +} -- cgit From 8919f2593e39f76b273513e959fa6d5ffb78fde2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Mar 2024 13:51:34 +0100 Subject: Clarify docs of `Text::color_maybe` --- core/src/widget/text.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e151476d..a220127c 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -83,7 +83,7 @@ where self } - /// Sets the [`Color`] of the [`Text`]. + /// Sets the [`Color`] of the [`Text`], if `Some`. pub fn color_maybe(mut self, color: Option>) -> Self { self.style = Style::Colored(color.map(Into::into)); self -- cgit