refactor(tooltip): add extra message parameter to tooltips

This commit is contained in:
Ashley Wulber 2025-03-05 15:45:44 -05:00
parent 9821c92f59
commit 5072cc3045
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
3 changed files with 38 additions and 34 deletions

View file

@ -117,10 +117,9 @@ impl cosmic::Application for Window {
} }
Message::Surface(a) => { Message::Surface(a) => {
dbg!("hi i got an action..."); return cosmic::task::message(cosmic::Action::Cosmic(
return cosmic::task::message(cosmic::Action::Cosmic(cosmic::app::Action::Surface( cosmic::app::Action::Surface(a),
a, ));
)));
} }
Message::Selected(i) => { Message::Selected(i) => {
self.selected = Some(i); self.selected = Some(i);
@ -195,11 +194,12 @@ impl cosmic::Application for Window {
}, },
); );
Element::from( Element::from(self.core.applet.applet_tooltip::<Message>(
self.core btn,
.applet "test",
.applet_tooltip(btn, "test", self.popup.is_some(), |a| Message::Surface(a)), self.popup.is_some(),
) |a| Message::Surface(a),
))
} }
fn view_window(&self, _id: Id) -> Element<Message> { fn view_window(&self, _id: Id) -> Element<Message> {
@ -209,13 +209,4 @@ impl cosmic::Application for Window {
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> { fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
Some(cosmic::applet::style()) Some(cosmic::applet::style())
} }
fn subscription(&self) -> cosmic::iced::Subscription<Message> {
listen_with(|e, status, id| {
if matches!(e, iced::event::Event::Keyboard(_)) {
dbg!(e, id);
}
None
})
}
} }

View file

@ -225,13 +225,13 @@ impl Context {
tooltip: impl Into<Cow<'static, str>>, tooltip: impl Into<Cow<'static, str>>,
has_popup: bool, has_popup: bool,
on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static, 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 window_id = *TOOLTIP_WINDOW_ID;
let subsurface_id = TOOLTIP_ID.clone(); let subsurface_id = TOOLTIP_ID.clone();
let anchor = self.anchor; let anchor = self.anchor;
let tooltip = tooltip.into(); let tooltip = tooltip.into();
crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message>::new( crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message, Message>::new(
content, content,
(!has_popup).then_some(move |bounds: Rectangle| { (!has_popup).then_some(move |bounds: Rectangle| {
let window_id = window_id; let window_id = window_id;

View file

@ -39,7 +39,7 @@ enum Variant<Message> {
/// A generic button which emits a message when pressed. /// A generic button which emits a message when pressed.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
#[must_use] #[must_use]
pub struct Tooltip<'a, Message> { pub struct Tooltip<'a, Message, TopLevelMessage> {
id: Id, id: Id,
#[cfg(feature = "a11y")] #[cfg(feature = "a11y")]
name: Option<std::borrow::Cow<'a, str>>, name: Option<std::borrow::Cow<'a, str>>,
@ -64,10 +64,12 @@ pub struct Tooltip<'a, Message> {
+ 'static, + 'static,
>, >,
>, >,
view: Arc<dyn Fn() -> crate::Element<'static, Message> + Send + Sync + 'static>, view: Arc<
dyn Fn() -> crate::Element<'static, crate::Action<TopLevelMessage>> + 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. /// Creates a new [`Tooltip`] with the given content.
pub fn new( pub fn new(
content: impl Into<crate::Element<'a, Message>>, content: impl Into<crate::Element<'a, Message>>,
@ -77,7 +79,10 @@ impl<'a, Message> Tooltip<'a, Message> {
+ Sync + Sync
+ 'static, + 'static,
>, >,
view: impl Fn() -> crate::Element<'static, Message> + Send + Sync + 'static, view: impl Fn() -> crate::Element<'static, crate::Action<TopLevelMessage>>
+ Send
+ Sync
+ 'static,
on_leave: Message, on_leave: Message,
on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static, on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static,
) -> Self { ) -> Self {
@ -182,8 +187,8 @@ impl<'a, Message> Tooltip<'a, Message> {
} }
} }
impl<'a, Message: 'static + Clone> Widget<Message, crate::Theme, crate::Renderer> impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone>
for Tooltip<'a, Message> Widget<Message, crate::Theme, crate::Renderer> for Tooltip<'a, Message, TopLevelMessage>
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>() tree::Tag::of::<State>()
@ -381,8 +386,10 @@ impl<'a, Message: 'static + Clone> Widget<Message, crate::Theme, crate::Renderer
} }
} }
impl<'a, Message: Clone + 'static> From<Tooltip<'a, Message>> for crate::Element<'a, Message> { impl<'a, Message: Clone + 'static, TopLevelMessage: Clone + 'static>
fn from(button: Tooltip<'a, Message>) -> Self { From<Tooltip<'a, Message, TopLevelMessage>> for crate::Element<'a, Message>
{
fn from(button: Tooltip<'a, Message, TopLevelMessage>) -> Self {
Self::new(button) Self::new(button)
} }
} }
@ -405,7 +412,7 @@ impl State {
/// Processes the given [`Event`] and updates the [`State`] of a [`Tooltip`] /// Processes the given [`Event`] and updates the [`State`] of a [`Tooltip`]
/// accordingly. /// accordingly.
#[allow(clippy::needless_pass_by_value)] #[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, _id: Id,
event: Event, event: Event,
layout: Layout<'_>, layout: Layout<'_>,
@ -419,7 +426,9 @@ pub fn update<'a, Message: Clone + 'static>(
+ 'static, + 'static,
>, >,
>, >,
view: &Arc<dyn Fn() -> crate::Element<'static, Message> + Send + Sync + 'static>, view: &Arc<
dyn Fn() -> crate::Element<'static, crate::Action<TopLevelMessage>> + Send + Sync + 'static,
>,
delay: Option<Duration>, delay: Option<Duration>,
on_leave: &Message, on_leave: &Message,
on_surface_action: &dyn Fn(crate::surface::Action) -> Message, on_surface_action: &dyn Fn(crate::surface::Action) -> Message,
@ -493,8 +502,10 @@ pub fn update<'a, Message: Clone + 'static>(
Arc::new(boxed), Arc::new(boxed),
Some({ Some({
let boxed: Box< let boxed: Box<
dyn Fn() -> crate::Element<'static, Message> dyn Fn() -> crate::Element<
+ Send 'static,
crate::Action<TopLevelMessage>,
> + Send
+ Sync + Sync
+ 'static, + 'static,
> = Box::new(move || view()); > = Box::new(move || view());
@ -524,8 +535,10 @@ pub fn update<'a, Message: Clone + 'static>(
Arc::new(boxed), Arc::new(boxed),
Some({ Some({
let boxed: Box< let boxed: Box<
dyn Fn() -> crate::Element<'static, Message> dyn Fn() -> crate::Element<
+ Send 'static,
crate::Action<TopLevelMessage>,
> + Send
+ Sync + Sync
+ 'static, + 'static,
> = Box::new(move || view()); > = Box::new(move || view());