From 5072cc304563c3de67b1fbd493a4bc3880512d8d Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 5 Mar 2025 15:45:44 -0500 Subject: [PATCH] refactor(tooltip): add extra message parameter to tooltips --- examples/applet/src/window.rs | 27 ++++++------------ src/applet/mod.rs | 4 +-- src/widget/wayland/tooltip/widget.rs | 41 ++++++++++++++++++---------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/examples/applet/src/window.rs b/examples/applet/src/window.rs index 58ee7fe5..90676751 100644 --- a/examples/applet/src/window.rs +++ b/examples/applet/src/window.rs @@ -117,10 +117,9 @@ impl cosmic::Application for Window { } Message::Surface(a) => { - dbg!("hi i got an action..."); - return cosmic::task::message(cosmic::Action::Cosmic(cosmic::app::Action::Surface( - a, - ))); + return cosmic::task::message(cosmic::Action::Cosmic( + cosmic::app::Action::Surface(a), + )); } Message::Selected(i) => { self.selected = Some(i); @@ -195,11 +194,12 @@ impl cosmic::Application for Window { }, ); - Element::from( - self.core - .applet - .applet_tooltip(btn, "test", self.popup.is_some(), |a| Message::Surface(a)), - ) + Element::from(self.core.applet.applet_tooltip::( + btn, + "test", + self.popup.is_some(), + |a| Message::Surface(a), + )) } fn view_window(&self, _id: Id) -> Element { @@ -209,13 +209,4 @@ impl cosmic::Application for Window { fn style(&self) -> Option { Some(cosmic::applet::style()) } - - fn subscription(&self) -> cosmic::iced::Subscription { - listen_with(|e, status, id| { - if matches!(e, iced::event::Event::Keyboard(_)) { - dbg!(e, id); - } - None - }) - } } diff --git a/src/applet/mod.rs b/src/applet/mod.rs index 74c72570..25af302a 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -225,13 +225,13 @@ impl Context { tooltip: impl Into>, has_popup: bool, on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static, - ) -> crate::widget::wayland::tooltip::widget::Tooltip<'a, Message> { + ) -> crate::widget::wayland::tooltip::widget::Tooltip<'a, Message, Message> { let window_id = *TOOLTIP_WINDOW_ID; let subsurface_id = TOOLTIP_ID.clone(); let anchor = self.anchor; let tooltip = tooltip.into(); - crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message>::new( + crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message, Message>::new( content, (!has_popup).then_some(move |bounds: Rectangle| { let window_id = window_id; diff --git a/src/widget/wayland/tooltip/widget.rs b/src/widget/wayland/tooltip/widget.rs index bcaa66d9..9c50cbac 100644 --- a/src/widget/wayland/tooltip/widget.rs +++ b/src/widget/wayland/tooltip/widget.rs @@ -39,7 +39,7 @@ enum Variant { /// A generic button which emits a message when pressed. #[allow(missing_debug_implementations)] #[must_use] -pub struct Tooltip<'a, Message> { +pub struct Tooltip<'a, Message, TopLevelMessage> { id: Id, #[cfg(feature = "a11y")] name: Option>, @@ -64,10 +64,12 @@ pub struct Tooltip<'a, Message> { + 'static, >, >, - view: Arc crate::Element<'static, Message> + Send + Sync + 'static>, + view: Arc< + dyn Fn() -> crate::Element<'static, crate::Action> + Send + Sync + 'static, + >, } -impl<'a, Message> Tooltip<'a, Message> { +impl<'a, Message, TopLevelMessage> Tooltip<'a, Message, TopLevelMessage> { /// Creates a new [`Tooltip`] with the given content. pub fn new( content: impl Into>, @@ -77,7 +79,10 @@ impl<'a, Message> Tooltip<'a, Message> { + Sync + 'static, >, - view: impl Fn() -> crate::Element<'static, Message> + Send + Sync + 'static, + view: impl Fn() -> crate::Element<'static, crate::Action> + + Send + + Sync + + 'static, on_leave: Message, on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static, ) -> Self { @@ -182,8 +187,8 @@ impl<'a, Message> Tooltip<'a, Message> { } } -impl<'a, Message: 'static + Clone> Widget - for Tooltip<'a, Message> +impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> + Widget for Tooltip<'a, Message, TopLevelMessage> { fn tag(&self) -> tree::Tag { tree::Tag::of::() @@ -381,8 +386,10 @@ impl<'a, Message: 'static + Clone> Widget From> for crate::Element<'a, Message> { - fn from(button: Tooltip<'a, Message>) -> Self { +impl<'a, Message: Clone + 'static, TopLevelMessage: Clone + 'static> + From> for crate::Element<'a, Message> +{ + fn from(button: Tooltip<'a, Message, TopLevelMessage>) -> Self { Self::new(button) } } @@ -405,7 +412,7 @@ impl State { /// Processes the given [`Event`] and updates the [`State`] of a [`Tooltip`] /// accordingly. #[allow(clippy::needless_pass_by_value)] -pub fn update<'a, Message: Clone + 'static>( +pub fn update<'a, Message: Clone + 'static, TopLevelMessage: Clone + 'static>( _id: Id, event: Event, layout: Layout<'_>, @@ -419,7 +426,9 @@ pub fn update<'a, Message: Clone + 'static>( + 'static, >, >, - view: &Arc crate::Element<'static, Message> + Send + Sync + 'static>, + view: &Arc< + dyn Fn() -> crate::Element<'static, crate::Action> + Send + Sync + 'static, + >, delay: Option, on_leave: &Message, on_surface_action: &dyn Fn(crate::surface::Action) -> Message, @@ -493,8 +502,10 @@ pub fn update<'a, Message: Clone + 'static>( Arc::new(boxed), Some({ let boxed: Box< - dyn Fn() -> crate::Element<'static, Message> - + Send + dyn Fn() -> crate::Element< + 'static, + crate::Action, + > + Send + Sync + 'static, > = Box::new(move || view()); @@ -524,8 +535,10 @@ pub fn update<'a, Message: Clone + 'static>( Arc::new(boxed), Some({ let boxed: Box< - dyn Fn() -> crate::Element<'static, Message> - + Send + dyn Fn() -> crate::Element< + 'static, + crate::Action, + > + Send + Sync + 'static, > = Box::new(move || view());