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/tooltip.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'widget/src/tooltip.rs') diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index 32c962fc..39f2e07d 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -20,6 +20,7 @@ pub struct Tooltip< Theme = crate::Theme, Renderer = crate::Renderer, > where + Theme: container::Catalog, Renderer: text::Renderer, { content: Element<'a, Message, Theme, Renderer>, @@ -28,11 +29,12 @@ pub struct Tooltip< gap: f32, padding: f32, snap_within_viewport: bool, - style: container::Style<'a, Theme>, + class: Theme::Class<'a>, } impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer> where + Theme: container::Catalog, Renderer: text::Renderer, { /// The default padding of a [`Tooltip`] drawn by this renderer. @@ -45,10 +47,7 @@ where content: impl Into>, tooltip: impl Into>, position: Position, - ) -> Self - where - Theme: container::DefaultStyle + 'a, - { + ) -> Self { Tooltip { content: content.into(), tooltip: tooltip.into(), @@ -56,7 +55,7 @@ where gap: 0.0, padding: Self::DEFAULT_PADDING, snap_within_viewport: true, - style: Box::new(Theme::default_style), + class: Theme::default(), } } @@ -79,11 +78,23 @@ where } /// Sets the style of the [`Tooltip`]. + #[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 [`Tooltip`]. + #[cfg(feature = "advanced")] + #[must_use] + pub fn class(mut self, class: impl Into>) -> Self { + self.class = class.into(); self } } @@ -91,6 +102,7 @@ where impl<'a, Message, Theme, Renderer> Widget for Tooltip<'a, Message, Theme, Renderer> where + Theme: container::Catalog, Renderer: text::Renderer, { fn children(&self) -> Vec { @@ -239,7 +251,7 @@ where positioning: self.position, gap: self.gap, padding: self.padding, - style: &self.style, + class: &self.class, }))) } else { None @@ -262,7 +274,7 @@ impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: 'a, - Theme: 'a, + Theme: container::Catalog + 'a, Renderer: text::Renderer + 'a, { fn from( @@ -299,6 +311,7 @@ enum State { struct Overlay<'a, 'b, Message, Theme, Renderer> where + Theme: container::Catalog, Renderer: text::Renderer, { position: Point, @@ -310,14 +323,14 @@ where positioning: Position, gap: f32, padding: f32, - style: - &'b (dyn Fn(&Theme, container::Status) -> container::Appearance + 'a), + class: &'b Theme::Class<'a>, } impl<'a, 'b, Message, Theme, Renderer> overlay::Overlay for Overlay<'a, 'b, Message, Theme, Renderer> where + Theme: container::Catalog, Renderer: text::Renderer, { fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node { @@ -426,7 +439,7 @@ where layout: Layout<'_>, cursor_position: mouse::Cursor, ) { - let style = (self.style)(theme, container::Status::Idle); + let style = theme.style(self.class); container::draw_background(renderer, &style, layout.bounds()); -- cgit