From 6bafdc4b3cd907697f315ac2db2c33938a178956 Mon Sep 17 00:00:00 2001 From: jhannyj Date: Thu, 1 Feb 2024 21:23:18 -0500 Subject: Add support for a generic `Element` in `Tooltip` --- widget/src/helpers.rs | 6 +++--- widget/src/tooltip.rs | 50 +++++++++++++++----------------------------------- 2 files changed, 18 insertions(+), 38 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 3f34d165..a643add3 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -123,20 +123,20 @@ where Button::new(content) } -/// Creates a new [`Tooltip`] with the provided content, tooltip text, and [`tooltip::Position`]. +/// Creates a new [`Tooltip`] with the provided content, tooltip element, and [`tooltip::Position`]. /// /// [`Tooltip`]: crate::Tooltip /// [`tooltip::Position`]: crate::tooltip::Position pub fn tooltip<'a, Message, Theme, Renderer>( content: impl Into>, - tooltip: impl ToString, + tooltip: impl Into>, position: tooltip::Position, ) -> crate::Tooltip<'a, Message, Theme, Renderer> where Theme: container::StyleSheet + text::StyleSheet, Renderer: core::text::Renderer, { - Tooltip::new(content, tooltip.to_string(), position) + Tooltip::new(content, tooltip, position) } /// Creates a new [`Text`] widget with the provided content. diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index 87bec932..a6eb783c 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -11,9 +11,6 @@ use crate::core::{ Clipboard, Element, Length, Padding, Pixels, Point, Rectangle, Shell, Size, Vector, }; -use crate::Text; - -use std::borrow::Cow; /// An element to display a widget over another. #[allow(missing_debug_implementations)] @@ -27,7 +24,7 @@ pub struct Tooltip< Renderer: text::Renderer, { content: Element<'a, Message, Theme, Renderer>, - tooltip: Text<'a, Theme, Renderer>, + tooltip: Element<'a, Message, Theme, Renderer>, position: Position, gap: f32, padding: f32, @@ -48,12 +45,12 @@ where /// [`Tooltip`]: struct.Tooltip.html pub fn new( content: impl Into>, - tooltip: impl Into>, + tooltip: impl Into>, position: Position, ) -> Self { Tooltip { content: content.into(), - tooltip: Text::new(tooltip), + tooltip: tooltip.into(), position, gap: 0.0, padding: Self::DEFAULT_PADDING, @@ -62,26 +59,6 @@ where } } - /// Sets the size of the text of the [`Tooltip`]. - pub fn size(mut self, size: impl Into) -> Self { - self.tooltip = self.tooltip.size(size); - self - } - - /// Sets the [`text::Shaping`] strategy of the [`Tooltip`]. - pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { - self.tooltip = self.tooltip.shaping(shaping); - self - } - - /// Sets the font of the [`Tooltip`]. - /// - /// [`Font`]: Renderer::Font - pub fn font(mut self, font: impl Into) -> Self { - self.tooltip = self.tooltip.font(font); - self - } - /// Sets the gap between the content and its [`Tooltip`]. pub fn gap(mut self, gap: impl Into) -> Self { self.gap = gap.into().0; @@ -119,12 +96,15 @@ where fn children(&self) -> Vec { vec![ widget::Tree::new(&self.content), - widget::Tree::new(&self.tooltip as &dyn Widget), + widget::Tree::new(&self.tooltip), ] } fn diff(&self, tree: &mut widget::Tree) { - tree.diff_children(&[self.content.as_widget(), &self.tooltip]); + tree.diff_children(&[ + self.content.as_widget(), + self.tooltip.as_widget(), + ]); } fn state(&self) -> widget::tree::State { @@ -312,13 +292,13 @@ enum State { }, } -struct Overlay<'a, 'b, Theme, Renderer> +struct Overlay<'a, 'b, Message, Theme, Renderer> where Theme: container::StyleSheet + widget::text::StyleSheet, Renderer: text::Renderer, { position: Point, - tooltip: &'b Text<'a, Theme, Renderer>, + tooltip: &'b Element<'a, Message, Theme, Renderer>, state: &'b mut widget::Tree, cursor_position: Point, content_bounds: Rectangle, @@ -331,7 +311,7 @@ where impl<'a, 'b, Message, Theme, Renderer> overlay::Overlay - for Overlay<'a, 'b, Theme, Renderer> + for Overlay<'a, 'b, Message, Theme, Renderer> where Theme: container::StyleSheet + widget::text::StyleSheet, Renderer: text::Renderer, @@ -339,8 +319,8 @@ where fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node { let viewport = Rectangle::with_size(bounds); - let text_layout = Widget::<(), Theme, Renderer>::layout( - self.tooltip, + let text_layout = Widget::::layout( + self.tooltip.as_widget(), self.state, renderer, &layout::Limits::new( @@ -450,8 +430,8 @@ where text_color: style.text_color.unwrap_or(inherited_style.text_color), }; - Widget::<(), Theme, Renderer>::draw( - self.tooltip, + Widget::::draw( + self.tooltip.as_widget(), self.state, renderer, theme, -- cgit