refactor: add support for tooltip delays

This commit is contained in:
Ashley Wulber 2025-02-28 18:44:26 -05:00
parent b10a81cbcd
commit b53f6566bc
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
7 changed files with 278 additions and 127 deletions

View file

@ -339,7 +339,7 @@ pub use toggler::toggler;
#[doc(inline)]
pub use tooltip::{tooltip, Tooltip};
#[cfg(feature = "wayland")]
#[cfg(all(feature = "wayland", feature = "winit"))]
pub mod wayland;
pub mod tooltip {

View file

@ -6,6 +6,11 @@
//!
//! A [`Tooltip`] has some local [`State`].
use std::any::Any;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use iced::Task;
use iced_runtime::core::widget::Id;
use iced_core::event::{self, Event};
@ -20,6 +25,8 @@ use iced_core::{
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
};
use crate::surface_message::SurfaceMessage;
pub use super::{Catalog, Style};
/// Internally defines different button widget variants.
@ -34,7 +41,7 @@ enum Variant<Message> {
/// 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, AppMessage> {
id: Id,
#[cfg(feature = "a11y")]
name: Option<std::borrow::Cow<'a, str>>,
@ -43,20 +50,43 @@ pub struct Tooltip<'a, Message> {
#[cfg(feature = "a11y")]
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
content: crate::Element<'a, Message>,
on_hover: Box<dyn Fn(iced_core::Layout) -> Message + 'a>,
on_leave: Message,
width: Length,
height: Length,
padding: Padding,
selected: bool,
style: crate::theme::Tooltip,
delay: Option<Duration>,
settings: Option<
Arc<
dyn Fn(Rectangle) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
>,
>,
view: Arc<
dyn Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
>,
}
impl<'a, Message> Tooltip<'a, Message> {
impl<'a, Message, AppMessage> Tooltip<'a, Message, AppMessage> {
/// Creates a new [`Tooltip`] with the given content.
pub fn new(
content: impl Into<crate::Element<'a, Message>>,
on_hover: impl Fn(iced_core::Layout) -> Message + 'a,
settings: Option<
impl Fn(Rectangle) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
>,
view: impl Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
on_leave: Message,
) -> Self {
Self {
@ -68,16 +98,27 @@ impl<'a, Message> Tooltip<'a, Message> {
#[cfg(feature = "a11y")]
label: None,
content: content.into(),
on_hover: Box::new(on_hover),
width: Length::Shrink,
height: Length::Shrink,
padding: Padding::new(0.0),
selected: false,
style: crate::theme::Tooltip::default(),
on_leave,
delay: None,
settings: if let Some(s) = settings {
Some(Arc::new(s))
} else {
None
},
view: Arc::new(view),
}
}
pub fn delay(mut self, dur: Duration) -> Self {
self.delay = Some(dur);
self
}
/// Sets the [`Id`] of the [`Tooltip`].
pub fn id(mut self, id: Id) -> Self {
self.id = id;
@ -148,8 +189,11 @@ impl<'a, Message> Tooltip<'a, Message> {
}
}
impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
for Tooltip<'a, Message>
impl<
'a,
Message: 'a + Clone + From<SurfaceMessage>,
AppMessage: Clone + 'static + From<SurfaceMessage>,
> Widget<Message, crate::Theme, crate::Renderer> for Tooltip<'a, Message, AppMessage>
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@ -206,7 +250,6 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
operation,
);
});
let state = tree.state.downcast_mut::<State>();
}
fn on_event(
@ -226,7 +269,9 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
layout,
cursor,
shell,
&self.on_hover,
self.settings.as_ref(),
&self.view,
self.delay,
&self.on_leave,
|| tree.state.downcast_mut::<State>(),
);
@ -345,45 +390,69 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
}
}
impl<'a, Message: Clone + 'a> From<Tooltip<'a, Message>> for crate::Element<'a, Message> {
fn from(button: Tooltip<'a, Message>) -> Self {
impl<
'a,
Message: From<SurfaceMessage> + Clone + 'a,
AppMessage: From<SurfaceMessage> + Clone + 'static,
> From<Tooltip<'a, Message, AppMessage>> for crate::Element<'a, Message>
{
fn from(button: Tooltip<'a, Message, AppMessage>) -> Self {
Self::new(button)
}
}
/// The local state of a [`Tooltip`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Default)]
#[allow(clippy::struct_field_names)]
pub struct State {
is_hovered: bool,
is_hovered: Arc<Mutex<bool>>,
}
impl State {
/// Returns whether the [`Tooltip`] is currently hovered or not.
pub fn is_hovered(self) -> bool {
self.is_hovered
let guard = self.is_hovered.lock().unwrap();
*guard
}
}
/// Processes the given [`Event`] and updates the [`State`] of a [`Tooltip`]
/// accordingly.
#[allow(clippy::needless_pass_by_value)]
pub fn update<'a, Message: Clone>(
pub fn update<
'a,
Message: Clone + From<SurfaceMessage>,
AppMessage: Clone + 'a + From<SurfaceMessage> + 'static,
>(
_id: Id,
event: Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>,
on_hover: &dyn Fn(Layout<'_>) -> Message,
settings: Option<
&Arc<
dyn Fn(Rectangle) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
>,
>,
view: &Arc<
dyn Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
>,
delay: Option<Duration>,
on_leave: &Message,
state: impl FnOnce() -> &'a mut State,
) -> event::Status {
match event {
Event::Touch(touch::Event::FingerLifted { .. }) => {
let state = state();
if state.is_hovered {
state.is_hovered = false;
let mut guard = state.is_hovered.lock().unwrap();
if *guard {
*guard = false;
shell.publish(on_leave.clone());
@ -393,8 +462,10 @@ pub fn update<'a, Message: Clone>(
Event::Touch(touch::Event::FingerLost { .. }) | Event::Mouse(mouse::Event::CursorLeft) => {
let state = state();
if state.is_hovered {
state.is_hovered = false;
let mut guard = state.is_hovered.lock().unwrap();
if *guard {
*guard = false;
shell.publish(on_leave.clone());
}
@ -403,16 +474,95 @@ pub fn update<'a, Message: Clone>(
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
let state = state();
let bounds = layout.bounds();
let is_hovered = state.is_hovered.clone();
let mut guard = state.is_hovered.lock().unwrap();
if state.is_hovered {
state.is_hovered = cursor.is_over(bounds);
if !state.is_hovered {
if *guard {
*guard = cursor.is_over(bounds);
if !*guard {
shell.publish(on_leave.clone());
}
} else {
state.is_hovered = cursor.is_over(bounds);
if state.is_hovered {
shell.publish(on_hover(layout).clone());
*guard = cursor.is_over(bounds);
if *guard {
if let Some(settings) = settings {
if let Some(delay) = delay {
let s = settings.clone();
let view = view.clone();
let bounds = layout.bounds();
shell.publish(
SurfaceMessage::Task(Arc::new(move || {
let s = s.clone();
let view = view.clone();
let is_hovered = is_hovered.clone();
Task::future(async move {
_ = tokio::time::sleep(delay).await;
let is_hovered = is_hovered.clone();
let g = is_hovered.lock().unwrap();
if !*g {
return SurfaceMessage::Ignore;
}
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(move || s(bounds));
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
SurfaceMessage::Popup(
Arc::new(boxed),
Some({
let boxed: Box<
dyn Fn() -> crate::Element<
'static,
crate::app::Message<AppMessage>,
> + Send
+ Sync
+ 'static,
> = Box::new(move || view());
let boxed: Box<dyn Any + Send + Sync + 'static> =
Box::new(boxed);
Arc::new(boxed)
}),
)
})
}))
.into(),
);
} else {
let s = settings.clone();
let view = view.clone();
let bounds = layout.bounds();
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(move || s(bounds));
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
let sm = SurfaceMessage::Popup(
Arc::new(boxed),
Some({
let boxed: Box<
dyn Fn() -> crate::Element<
'static,
crate::app::Message<AppMessage>,
> + Send
+ Sync
+ 'static,
> = Box::new(move || view());
let boxed: Box<dyn Any + Send + Sync + 'static> =
Box::new(boxed);
Arc::new(boxed)
}),
);
shell.publish(sm.into());
}
}
}
}
}