refactor: add support for tooltip delays
This commit is contained in:
parent
248e89afed
commit
f3c72c4692
7 changed files with 278 additions and 127 deletions
|
|
@ -94,7 +94,10 @@ pub struct Cosmic<App: Application> {
|
|||
|
||||
impl<T: Application> Cosmic<T>
|
||||
where
|
||||
T::Message: Send + 'static + Into<crate::surface_message::MessageWrapper<T::Message>>,
|
||||
T::Message: Send
|
||||
+ 'static
|
||||
+ Into<crate::surface_message::MessageWrapper<T::Message>>
|
||||
+ From<SurfaceMessage>,
|
||||
{
|
||||
pub fn init(
|
||||
(mut core, flags): (Core, T::Flags),
|
||||
|
|
@ -132,11 +135,11 @@ where
|
|||
match surface_message {
|
||||
SurfaceMessage::AppSubsurface(settings, view) => {
|
||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for subsurface");
|
||||
return Task::none();
|
||||
};
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for subsurface");
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if let Some(view) = view.and_then(|view| {
|
||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||
|
|
@ -165,11 +168,11 @@ where
|
|||
}
|
||||
SurfaceMessage::Subsurface(settings, view) => {
|
||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for subsurface");
|
||||
return Task::none();
|
||||
};
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for subsurface");
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if let Some(view) = view.and_then(|view| {
|
||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||
|
|
@ -183,7 +186,6 @@ where
|
|||
tracing::error!(
|
||||
"Invalid view for subsurface view: {err:?}"
|
||||
);
|
||||
dbg!("bad view");
|
||||
|
||||
None
|
||||
}
|
||||
|
|
@ -197,11 +199,11 @@ where
|
|||
}
|
||||
SurfaceMessage::AppPopup(settings, view) => {
|
||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for popup");
|
||||
return Task::none();
|
||||
};
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for popup");
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if let Some(view) = view.and_then(|view| {
|
||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||
|
|
@ -245,11 +247,11 @@ where
|
|||
}
|
||||
SurfaceMessage::Popup(settings, view) => {
|
||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for popup");
|
||||
return Task::none();
|
||||
};
|
||||
.ok()
|
||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
||||
tracing::error!("Invalid settings for popup");
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
if let Some(view) = view.and_then(|view| {
|
||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||
|
|
@ -275,6 +277,7 @@ where
|
|||
}
|
||||
}
|
||||
SurfaceMessage::Ignore => iced::Task::none(),
|
||||
SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())),
|
||||
}
|
||||
#[cfg(not(feature = "wayland"))]
|
||||
iced::Task::none()
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ pub mod message {
|
|||
|
||||
/// Used to produce a destroy popup message from within a widget.
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn destroy_popup<Message>(id: iced_core::window::Id) -> Message
|
||||
where
|
||||
Message: From<SurfaceMessage> + 'static,
|
||||
|
|
@ -53,12 +54,14 @@ pub mod message {
|
|||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn destroy_subsurface<M: From<SurfaceMessage>>(id: iced_core::window::Id) -> M {
|
||||
let surface_msg = SurfaceMessage::DestroySubsurface(id);
|
||||
M::from(surface_msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn app_popup<App: super::Application>(
|
||||
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
||||
+ Send
|
||||
|
|
@ -99,6 +102,7 @@ pub mod message {
|
|||
|
||||
/// Used to create a subsurface message from within a widget.
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn simple_subsurface<Message, V>(
|
||||
settings: impl Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
|
||||
view: Option<
|
||||
|
|
@ -109,10 +113,9 @@ pub mod message {
|
|||
+ 'static,
|
||||
>,
|
||||
>,
|
||||
) -> Message
|
||||
) -> SurfaceMessage
|
||||
where
|
||||
Message:
|
||||
Into<crate::surface_message::MessageWrapper<Message>> + From<SurfaceMessage> + 'static,
|
||||
Message: From<SurfaceMessage> + 'static,
|
||||
V:,
|
||||
{
|
||||
use std::{any::Any, sync::Arc};
|
||||
|
|
@ -126,17 +129,18 @@ pub mod message {
|
|||
> = Box::new(settings);
|
||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||
|
||||
Message::from(SurfaceMessage::Subsurface(
|
||||
SurfaceMessage::Subsurface(
|
||||
Arc::new(boxed),
|
||||
view.map(|view| {
|
||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
|
||||
Arc::new(boxed)
|
||||
}),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
/// Used to create a popup message from within a widget.
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn simple_popup<Message, V>(
|
||||
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
||||
+ Send
|
||||
|
|
@ -145,10 +149,9 @@ pub mod message {
|
|||
view: Option<
|
||||
impl Fn() -> crate::Element<'static, crate::app::Message<Message>> + Send + Sync + 'static,
|
||||
>,
|
||||
) -> Message
|
||||
) -> SurfaceMessage
|
||||
where
|
||||
Message:
|
||||
Into<crate::surface_message::MessageWrapper<Message>> + From<SurfaceMessage> + 'static,
|
||||
Message: From<SurfaceMessage> + 'static,
|
||||
V:,
|
||||
{
|
||||
use std::{any::Any, sync::Arc};
|
||||
|
|
@ -162,7 +165,7 @@ pub mod message {
|
|||
> = Box::new(settings);
|
||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||
|
||||
Message::from(SurfaceMessage::Popup(
|
||||
SurfaceMessage::Popup(
|
||||
Arc::new(boxed),
|
||||
view.map(|view| {
|
||||
let boxed: Box<
|
||||
|
|
@ -174,10 +177,11 @@ pub mod message {
|
|||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||
Arc::new(boxed)
|
||||
}),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
#[must_use]
|
||||
pub fn subsurface<App: super::Application>(
|
||||
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
|
||||
// XXX Boxed trait object is required for less cumbersome type inference, but we box it anyways.
|
||||
|
|
@ -229,6 +233,7 @@ pub use self::command::Task;
|
|||
pub use self::core::Core;
|
||||
pub use self::settings::Settings;
|
||||
use crate::prelude::*;
|
||||
use crate::surface_message::SurfaceMessage;
|
||||
use crate::theme::THEME;
|
||||
use crate::widget::{container, horizontal_space, id_container, menu, nav_bar, popover};
|
||||
use apply::Apply;
|
||||
|
|
@ -314,7 +319,7 @@ pub(crate) fn iced_settings<App: Application>(
|
|||
/// Returns error on application failure.
|
||||
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
|
||||
where
|
||||
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
|
||||
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>,
|
||||
{
|
||||
#[cfg(target_env = "gnu")]
|
||||
if let Some(threshold) = settings.default_mmap_threshold {
|
||||
|
|
@ -507,6 +512,7 @@ where
|
|||
+ std::fmt::Debug
|
||||
+ Send
|
||||
+ Into<crate::surface_message::MessageWrapper<App::Message>>
|
||||
+ From<SurfaceMessage>
|
||||
+ 'static,
|
||||
{
|
||||
let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok();
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ use cosmic_panel_config::{CosmicPanelBackground, PanelAnchor, PanelSize};
|
|||
use iced_core::{Layout, Padding, Shadow};
|
||||
use iced_widget::runtime::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
|
||||
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
||||
use std::{borrow::Cow, num::NonZeroU32, rc::Rc, sync::LazyLock};
|
||||
use std::{borrow::Cow, num::NonZeroU32, rc::Rc, sync::LazyLock, time::Duration};
|
||||
use tracing::info;
|
||||
|
||||
use crate::app::cosmic;
|
||||
|
|
@ -228,75 +228,61 @@ impl Context {
|
|||
content: impl Into<Element<'a, Message>>,
|
||||
tooltip: impl Into<Cow<'static, str>>,
|
||||
has_popup: bool,
|
||||
) -> 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();
|
||||
let on_hover = move |layout: Layout| {
|
||||
let bounds = layout.bounds();
|
||||
let subsurface_id = subsurface_id.clone();
|
||||
let tooltip = tooltip.clone();
|
||||
let window_id = window_id;
|
||||
let (popup_anchor, gravity) = match anchor {
|
||||
PanelAnchor::Left => (Anchor::Right, Gravity::Right),
|
||||
PanelAnchor::Right => (Anchor::Left, Gravity::Left),
|
||||
PanelAnchor::Top => (Anchor::Bottom, Gravity::Bottom),
|
||||
PanelAnchor::Bottom => (Anchor::Top, Gravity::Top),
|
||||
};
|
||||
if has_popup {
|
||||
SurfaceMessage::Ignore.into()
|
||||
} else {
|
||||
crate::app::message::simple_popup::<
|
||||
Message,
|
||||
Option<
|
||||
Box<
|
||||
dyn Fn() -> crate::Element<'static, crate::app::Message<Message>>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
>,
|
||||
>,
|
||||
>(
|
||||
move || SctkPopupSettings {
|
||||
parent: window::Id::RESERVED,
|
||||
id: window_id,
|
||||
grab: false,
|
||||
input_zone: Some(Rectangle::default()),
|
||||
positioner: SctkPositioner {
|
||||
size: None,
|
||||
size_limits: Limits::NONE.min_width(1.).min_height(1.),
|
||||
anchor_rect: Rectangle {
|
||||
x: bounds.x.round() as i32,
|
||||
y: bounds.y.round() as i32,
|
||||
width: bounds.width.round() as i32,
|
||||
height: bounds.height.round() as i32,
|
||||
},
|
||||
anchor: popup_anchor,
|
||||
gravity,
|
||||
constraint_adjustment: 15,
|
||||
offset: (0, 0),
|
||||
reactive: true,
|
||||
},
|
||||
parent_size: None,
|
||||
close_with_children: true,
|
||||
},
|
||||
Some(Box::new(move || {
|
||||
Element::<'static, crate::app::Message<Message>>::from(autosize::autosize(
|
||||
layer_container(crate::widget::text(tooltip.clone()))
|
||||
.layer(crate::cosmic_theme::Layer::Background)
|
||||
.padding(4.),
|
||||
subsurface_id.clone(),
|
||||
))
|
||||
})),
|
||||
)
|
||||
}
|
||||
};
|
||||
crate::widget::wayland::tooltip::widget::Tooltip::new(
|
||||
|
||||
crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message, Message>::new(
|
||||
content,
|
||||
on_hover,
|
||||
(!has_popup).then_some(move |bounds: Rectangle| {
|
||||
let window_id = window_id;
|
||||
let (popup_anchor, gravity) = match anchor {
|
||||
PanelAnchor::Left => (Anchor::Right, Gravity::Right),
|
||||
PanelAnchor::Right => (Anchor::Left, Gravity::Left),
|
||||
PanelAnchor::Top => (Anchor::Bottom, Gravity::Bottom),
|
||||
PanelAnchor::Bottom => (Anchor::Top, Gravity::Top),
|
||||
};
|
||||
|
||||
SctkPopupSettings {
|
||||
parent: window::Id::RESERVED,
|
||||
id: window_id,
|
||||
grab: false,
|
||||
input_zone: Some(Rectangle::new(
|
||||
iced::Point::new(-1000., -1000.),
|
||||
iced::Size::default(),
|
||||
)),
|
||||
positioner: SctkPositioner {
|
||||
size: None,
|
||||
size_limits: Limits::NONE.min_width(1.).min_height(1.),
|
||||
anchor_rect: Rectangle {
|
||||
x: bounds.x.round() as i32,
|
||||
y: bounds.y.round() as i32,
|
||||
width: bounds.width.round() as i32,
|
||||
height: bounds.height.round() as i32,
|
||||
},
|
||||
anchor: popup_anchor,
|
||||
gravity,
|
||||
constraint_adjustment: 15,
|
||||
offset: (0, 0),
|
||||
reactive: true,
|
||||
},
|
||||
parent_size: None,
|
||||
close_with_children: true,
|
||||
}
|
||||
}),
|
||||
move || {
|
||||
Element::<'static, crate::app::Message<Message>>::from(autosize::autosize(
|
||||
layer_container(crate::widget::text(tooltip.clone()))
|
||||
.layer(crate::cosmic_theme::Layer::Background)
|
||||
.padding(4.),
|
||||
subsurface_id.clone(),
|
||||
))
|
||||
},
|
||||
crate::app::message::destroy_popup::<Message>(window_id),
|
||||
)
|
||||
.delay(Duration::from_millis(100))
|
||||
}
|
||||
|
||||
// TODO popup container which tracks the size of itself and requests the popup to resize to match
|
||||
|
|
@ -455,7 +441,7 @@ impl Context {
|
|||
/// Returns error on application failure.
|
||||
pub fn run<App: Application>(flags: App::Flags) -> iced::Result
|
||||
where
|
||||
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
|
||||
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>,
|
||||
{
|
||||
let helper = Context::default();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
|
||||
use iced::Size;
|
||||
|
||||
use iced::Limits;
|
||||
use iced::Task;
|
||||
|
||||
pub enum MessageWrapper<M> {
|
||||
Surface(SurfaceMessage),
|
||||
|
|
@ -51,6 +55,7 @@ pub enum SurfaceMessage {
|
|||
size: Size,
|
||||
},
|
||||
Ignore,
|
||||
Task(Arc<dyn Fn() -> Task<SurfaceMessage> + Send + Sync>),
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for SurfaceMessage {
|
||||
|
|
@ -83,6 +88,7 @@ impl std::fmt::Debug for SurfaceMessage {
|
|||
.field("size", size)
|
||||
.finish(),
|
||||
Self::Ignore => write!(f, "Ignore"),
|
||||
Self::Task(_) => f.debug_tuple("Future").finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ mod text_input;
|
|||
#[doc(inline)]
|
||||
pub use self::text_input::TextInput;
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
#[cfg(all(feature = "wayland", feature = "winit"))]
|
||||
pub mod tooltip;
|
||||
#[cfg(feature = "wayland")]
|
||||
#[cfg(all(feature = "wayland", feature = "winit"))]
|
||||
pub use tooltip::Tooltip;
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue