diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index 7f48326e..756909eb 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -94,7 +94,10 @@ pub struct Cosmic { impl Cosmic where - T::Message: Send + 'static + Into>, + T::Message: Send + + 'static + + Into> + + From, { 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:: 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:: 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:: { let Some(settings) = std::sync::Arc::try_unwrap(settings) - .ok() - .and_then(|s| s.downcast:: 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:: 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:: { let Some(settings) = std::sync::Arc::try_unwrap(settings) - .ok() - .and_then(|s| s.downcast:: 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:: 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:: { let Some(settings) = std::sync::Arc::try_unwrap(settings) - .ok() - .and_then(|s| s.downcast:: 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:: 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:: iced::Task::none(), + SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())), } #[cfg(not(feature = "wayland"))] iced::Task::none() diff --git a/src/app/mod.rs b/src/app/mod.rs index 2f60b19c..ad62ae66 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -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(id: iced_core::window::Id) -> Message where Message: From + 'static, @@ -53,12 +54,14 @@ pub mod message { } #[cfg(feature = "wayland")] + #[must_use] pub fn destroy_subsurface>(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( 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( 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> + From + 'static, + Message: From + 'static, V:, { use std::{any::Any, sync::Arc}; @@ -126,17 +129,18 @@ pub mod message { > = Box::new(settings); let boxed: Box = Box::new(boxed); - Message::from(SurfaceMessage::Subsurface( + SurfaceMessage::Subsurface( Arc::new(boxed), view.map(|view| { let boxed: Box = 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( 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> + Send + Sync + 'static, >, - ) -> Message + ) -> SurfaceMessage where - Message: - Into> + From + 'static, + Message: From + 'static, V:, { use std::{any::Any, sync::Arc}; @@ -162,7 +165,7 @@ pub mod message { > = Box::new(settings); let boxed: Box = 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 = Box::new(boxed); Arc::new(boxed) }), - )) + ) } #[cfg(feature = "wayland")] + #[must_use] pub fn subsurface( 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( /// Returns error on application failure. pub fn run(settings: Settings, flags: App::Flags) -> iced::Result where - App::Message: Into>, + App::Message: Into> + From, { #[cfg(target_env = "gnu")] if let Some(threshold) = settings.default_mmap_threshold { @@ -507,6 +512,7 @@ where + std::fmt::Debug + Send + Into> + + From + 'static, { let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok(); diff --git a/src/applet/mod.rs b/src/applet/mod.rs index 9da2391b..27bd42fd 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -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>, tooltip: impl Into>, 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> - + 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>::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>::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::(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(flags: App::Flags) -> iced::Result where - App::Message: Into>, + App::Message: Into> + From, { let helper = Context::default(); diff --git a/src/surface_message.rs b/src/surface_message.rs index 754d4cf9..86a27ceb 100644 --- a/src/surface_message.rs +++ b/src/surface_message.rs @@ -1,6 +1,10 @@ +use std::future::Future; +use std::sync::Arc; + use iced::Size; use iced::Limits; +use iced::Task; pub enum MessageWrapper { Surface(SurfaceMessage), @@ -51,6 +55,7 @@ pub enum SurfaceMessage { size: Size, }, Ignore, + Task(Arc Task + 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(), } } } diff --git a/src/theme/style/mod.rs b/src/theme/style/mod.rs index 60768606..a187374c 100644 --- a/src/theme/style/mod.rs +++ b/src/theme/style/mod.rs @@ -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; diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 46264a0e..3d37498a 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -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 { diff --git a/src/widget/wayland/tooltip/widget.rs b/src/widget/wayland/tooltip/widget.rs index a6eae43a..00a81c45 100644 --- a/src/widget/wayland/tooltip/widget.rs +++ b/src/widget/wayland/tooltip/widget.rs @@ -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 { /// 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>, @@ -43,20 +50,43 @@ pub struct Tooltip<'a, Message> { #[cfg(feature = "a11y")] label: Option>, content: crate::Element<'a, Message>, - on_hover: Box Message + 'a>, on_leave: Message, width: Length, height: Length, padding: Padding, selected: bool, style: crate::theme::Tooltip, + delay: Option, + 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> + + 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>, - 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> + + 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 - for Tooltip<'a, Message> +impl< + 'a, + Message: 'a + Clone + From, + AppMessage: Clone + 'static + From, + > Widget for Tooltip<'a, Message, AppMessage> { fn tag(&self) -> tree::Tag { tree::Tag::of::() @@ -206,7 +250,6 @@ impl<'a, Message: 'a + Clone> Widget operation, ); }); - let state = tree.state.downcast_mut::(); } fn on_event( @@ -226,7 +269,9 @@ impl<'a, Message: 'a + Clone> Widget layout, cursor, shell, - &self.on_hover, + self.settings.as_ref(), + &self.view, + self.delay, &self.on_leave, || tree.state.downcast_mut::(), ); @@ -345,45 +390,69 @@ impl<'a, Message: 'a + Clone> Widget } } -impl<'a, Message: Clone + 'a> From> for crate::Element<'a, Message> { - fn from(button: Tooltip<'a, Message>) -> Self { +impl< + 'a, + Message: From + Clone + 'a, + AppMessage: From + Clone + 'static, + > From> 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>, } 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, + AppMessage: Clone + 'a + From + '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> + + Send + + Sync + + 'static, + >, + delay: Option, 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 = Box::new(boxed); + SurfaceMessage::Popup( + Arc::new(boxed), + Some({ + let boxed: Box< + dyn Fn() -> crate::Element< + 'static, + crate::app::Message, + > + Send + + Sync + + 'static, + > = Box::new(move || view()); + let boxed: Box = + 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 = Box::new(boxed); + + let sm = SurfaceMessage::Popup( + Arc::new(boxed), + Some({ + let boxed: Box< + dyn Fn() -> crate::Element< + 'static, + crate::app::Message, + > + Send + + Sync + + 'static, + > = Box::new(move || view()); + let boxed: Box = + Box::new(boxed); + Arc::new(boxed) + }), + ); + shell.publish(sm.into()); + } + } } } }