From f0ae9a0c38c2532220a7460916604914db94c078 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 24 Mar 2024 05:03:09 +0100 Subject: Use `Catalog` approach for all widgets --- widget/src/pane_grid/content.rs | 59 +++++++++++++++++++----------------- widget/src/pane_grid/title_bar.rs | 64 +++++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 57 deletions(-) (limited to 'widget/src/pane_grid') diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index 98f4f99a..30ad52ca 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -6,7 +6,7 @@ use crate::core::overlay; use crate::core::renderer; use crate::core::widget::{self, Tree}; use crate::core::{ - Clipboard, Element, Layout, Point, Rectangle, Shell, Size, Vector, + self, Clipboard, Element, Layout, Point, Rectangle, Shell, Size, Vector, }; use crate::pane_grid::{Draggable, TitleBar}; @@ -20,30 +20,29 @@ pub struct Content< Theme = crate::Theme, Renderer = crate::Renderer, > where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { title_bar: Option>, body: Element<'a, Message, Theme, Renderer>, - style: container::Style<'a, Theme>, + class: Theme::Class<'a>, } impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { /// Creates a new [`Content`] with the provided body. - pub fn new(body: impl Into>) -> Self - where - Theme: container::DefaultStyle + 'a, - { + pub fn new(body: impl Into>) -> Self { Self { title_bar: None, body: body.into(), - style: Box::new(Theme::default_style), + class: Theme::default(), } } - /// Sets the [`TitleBar`] of this [`Content`]. + /// Sets the [`TitleBar`] of the [`Content`]. pub fn title_bar( mut self, title_bar: TitleBar<'a, Message, Theme, Renderer>, @@ -53,18 +52,31 @@ where } /// Sets the style of the [`Content`]. + #[must_use] pub fn style( mut self, - style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a, - ) -> Self { - self.style = Box::new(style); + style: impl Fn(&Theme) -> container::Style + 'a, + ) -> Self + where + Theme::Class<'a>: From>, + { + self.class = (Box::new(style) as container::StyleFn<'a, Theme>).into(); + self + } + + /// Sets the style class of the [`Content`]. + #[cfg(feature = "advanced")] + #[must_use] + pub fn class(mut self, class: impl Into>) -> Self { + self.class = class.into(); self } } impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { pub(super) fn state(&self) -> Tree { let children = if let Some(title_bar) = self.title_bar.as_ref() { @@ -93,7 +105,7 @@ where /// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`]. /// - /// [`Renderer`]: crate::core::Renderer + /// [`Renderer`]: core::Renderer pub fn draw( &self, tree: &Tree, @@ -107,15 +119,7 @@ where let bounds = layout.bounds(); { - let style = { - let status = if cursor.is_over(bounds) { - container::Status::Hovered - } else { - container::Status::Idle - }; - - (self.style)(theme, status) - }; + let style = theme.style(&self.class); container::draw_background(renderer, &style, bounds); } @@ -381,7 +385,8 @@ where impl<'a, Message, Theme, Renderer> Draggable for &Content<'a, Message, Theme, Renderer> where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { fn can_be_dragged_at( &self, @@ -403,8 +408,8 @@ impl<'a, T, Message, Theme, Renderer> From for Content<'a, Message, Theme, Renderer> where T: Into>, - Theme: container::DefaultStyle + 'a, - Renderer: crate::core::Renderer, + Theme: container::Catalog + 'a, + Renderer: core::Renderer, { fn from(element: T) -> Self { Self::new(element) diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index 8dfea6e3..c2eeebb7 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -6,7 +6,8 @@ use crate::core::overlay; use crate::core::renderer; use crate::core::widget::{self, Tree}; use crate::core::{ - Clipboard, Element, Layout, Padding, Point, Rectangle, Shell, Size, Vector, + self, Clipboard, Element, Layout, Padding, Point, Rectangle, Shell, Size, + Vector, }; /// The title bar of a [`Pane`]. @@ -19,32 +20,31 @@ pub struct TitleBar< Theme = crate::Theme, Renderer = crate::Renderer, > where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { content: Element<'a, Message, Theme, Renderer>, controls: Option>, padding: Padding, always_show_controls: bool, - style: container::Style<'a, Theme>, + class: Theme::Class<'a>, } impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { /// Creates a new [`TitleBar`] with the given content. pub fn new( content: impl Into>, - ) -> Self - where - Theme: container::DefaultStyle + 'a, - { + ) -> Self { Self { content: content.into(), controls: None, padding: Padding::ZERO, always_show_controls: false, - style: Box::new(Theme::default_style), + class: Theme::default(), } } @@ -63,15 +63,6 @@ where self } - /// Sets the style of the [`TitleBar`]. - pub fn style( - mut self, - style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a, - ) -> Self { - self.style = Box::new(style); - self - } - /// Sets whether or not the [`controls`] attached to this [`TitleBar`] are /// always visible. /// @@ -84,11 +75,33 @@ where self.always_show_controls = true; self } + + /// Sets the style of the [`TitleBar`]. + #[must_use] + pub fn style( + mut self, + style: impl Fn(&Theme) -> container::Style + 'a, + ) -> Self + where + Theme::Class<'a>: From>, + { + self.class = (Box::new(style) as container::StyleFn<'a, Theme>).into(); + self + } + + /// Sets the style class of the [`TitleBar`]. + #[cfg(feature = "advanced")] + #[must_use] + pub fn class(mut self, class: impl Into>) -> Self { + self.class = class.into(); + self + } } impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> where - Renderer: crate::core::Renderer, + Theme: container::Catalog, + Renderer: core::Renderer, { pub(super) fn state(&self) -> Tree { let children = if let Some(controls) = self.controls.as_ref() { @@ -117,7 +130,7 @@ where /// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`]. /// - /// [`Renderer`]: crate::core::Renderer + /// [`Renderer`]: core::Renderer pub fn draw( &self, tree: &Tree, @@ -130,16 +143,7 @@ where show_controls: bool, ) { let bounds = layout.bounds(); - - let style = { - let status = if cursor.is_over(bounds) { - container::Status::Hovered - } else { - container::Status::Idle - }; - - (self.style)(theme, status) - }; + let style = theme.style(&self.class); let inherited_style = renderer::Style { text_color: style.text_color.unwrap_or(inherited_style.text_color), -- cgit