From 29326215ccf13e1d1e25bf3bf5ada007856bff69 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Mar 2024 03:48:08 +0100 Subject: Simplify theming for `Container` widget --- widget/src/pane_grid/content.rs | 32 +++++++++++++++++++++----------- widget/src/pane_grid/title_bar.rs | 27 +++++++++++++++++++-------- 2 files changed, 40 insertions(+), 19 deletions(-) (limited to 'widget/src/pane_grid') diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index dfe0fdcf..78a4f347 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -20,25 +20,26 @@ pub struct Content< Theme = crate::Theme, Renderer = crate::Renderer, > where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { title_bar: Option>, body: Element<'a, Message, Theme, Renderer>, - style: Theme::Style, + style: fn(&Theme, container::Status) -> container::Appearance, } impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { /// Creates a new [`Content`] with the provided body. - pub fn new(body: impl Into>) -> Self { + pub fn new(body: impl Into>) -> Self + where + Theme: container::Style, + { Self { title_bar: None, body: body.into(), - style: Default::default(), + style: Theme::default(), } } @@ -52,15 +53,17 @@ where } /// Sets the style of the [`Content`]. - pub fn style(mut self, style: impl Into) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: fn(&Theme, container::Status) -> container::Appearance, + ) -> Self { + self.style = style; self } } impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { pub(super) fn state(&self) -> Tree { @@ -104,7 +107,15 @@ where let bounds = layout.bounds(); { - let style = theme.appearance(&self.style); + let style = { + let status = if cursor.is_over(bounds) { + container::Status::Hovered + } else { + container::Status::Idle + }; + + (self.style)(theme, status) + }; container::draw_background(renderer, &style, bounds); } @@ -370,7 +381,6 @@ where impl<'a, Message, Theme, Renderer> Draggable for &Content<'a, Message, Theme, Renderer> where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { fn can_be_dragged_at( @@ -393,7 +403,7 @@ impl<'a, T, Message, Theme, Renderer> From for Content<'a, Message, Theme, Renderer> where T: Into>, - Theme: container::StyleSheet, + Theme: container::Style, Renderer: crate::core::Renderer, { fn from(element: T) -> Self { diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index 5b57509b..6d786f96 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -19,24 +19,23 @@ pub struct TitleBar< Theme = crate::Theme, Renderer = crate::Renderer, > where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { content: Element<'a, Message, Theme, Renderer>, controls: Option>, padding: Padding, always_show_controls: bool, - style: Theme::Style, + style: fn(&Theme, container::Status) -> container::Appearance, } impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { /// Creates a new [`TitleBar`] with the given content. pub fn new(content: E) -> Self where + Theme: container::Style, E: Into>, { Self { @@ -44,7 +43,7 @@ where controls: None, padding: Padding::ZERO, always_show_controls: false, - style: Default::default(), + style: Theme::default(), } } @@ -64,8 +63,11 @@ where } /// Sets the style of the [`TitleBar`]. - pub fn style(mut self, style: impl Into) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: fn(&Theme, container::Status) -> container::Appearance, + ) -> Self { + self.style = style; self } @@ -85,7 +87,6 @@ where impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> where - Theme: container::StyleSheet, Renderer: crate::core::Renderer, { pub(super) fn state(&self) -> Tree { @@ -128,7 +129,17 @@ where show_controls: bool, ) { let bounds = layout.bounds(); - let style = theme.appearance(&self.style); + + let style = { + let status = if cursor.is_over(bounds) { + container::Status::Hovered + } else { + container::Status::Idle + }; + + (self.style)(theme, status) + }; + let inherited_style = renderer::Style { text_color: style.text_color.unwrap_or(inherited_style.text_color), }; -- cgit