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>
|
impl<T: Application> Cosmic<T>
|
||||||
where
|
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(
|
pub fn init(
|
||||||
(mut core, flags): (Core, T::Flags),
|
(mut core, flags): (Core, T::Flags),
|
||||||
|
|
@ -132,11 +135,11 @@ where
|
||||||
match surface_message {
|
match surface_message {
|
||||||
SurfaceMessage::AppSubsurface(settings, view) => {
|
SurfaceMessage::AppSubsurface(settings, view) => {
|
||||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
.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");
|
tracing::error!("Invalid settings for subsurface");
|
||||||
return Task::none();
|
return Task::none();
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(view) = view.and_then(|view| {
|
if let Some(view) = view.and_then(|view| {
|
||||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||||
|
|
@ -165,11 +168,11 @@ where
|
||||||
}
|
}
|
||||||
SurfaceMessage::Subsurface(settings, view) => {
|
SurfaceMessage::Subsurface(settings, view) => {
|
||||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
|
.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");
|
tracing::error!("Invalid settings for subsurface");
|
||||||
return Task::none();
|
return Task::none();
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(view) = view.and_then(|view| {
|
if let Some(view) = view.and_then(|view| {
|
||||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||||
|
|
@ -183,7 +186,6 @@ where
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
"Invalid view for subsurface view: {err:?}"
|
"Invalid view for subsurface view: {err:?}"
|
||||||
);
|
);
|
||||||
dbg!("bad view");
|
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -197,11 +199,11 @@ where
|
||||||
}
|
}
|
||||||
SurfaceMessage::AppPopup(settings, view) => {
|
SurfaceMessage::AppPopup(settings, view) => {
|
||||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
.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");
|
tracing::error!("Invalid settings for popup");
|
||||||
return Task::none();
|
return Task::none();
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(view) = view.and_then(|view| {
|
if let Some(view) = view.and_then(|view| {
|
||||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||||
|
|
@ -245,11 +247,11 @@ where
|
||||||
}
|
}
|
||||||
SurfaceMessage::Popup(settings, view) => {
|
SurfaceMessage::Popup(settings, view) => {
|
||||||
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
let Some(settings) = std::sync::Arc::try_unwrap(settings)
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
|
.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");
|
tracing::error!("Invalid settings for popup");
|
||||||
return Task::none();
|
return Task::none();
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(view) = view.and_then(|view| {
|
if let Some(view) = view.and_then(|view| {
|
||||||
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
|
||||||
|
|
@ -275,6 +277,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SurfaceMessage::Ignore => iced::Task::none(),
|
SurfaceMessage::Ignore => iced::Task::none(),
|
||||||
|
SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())),
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "wayland"))]
|
#[cfg(not(feature = "wayland"))]
|
||||||
iced::Task::none()
|
iced::Task::none()
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@ pub mod message {
|
||||||
|
|
||||||
/// Used to produce a destroy popup message from within a widget.
|
/// Used to produce a destroy popup message from within a widget.
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn destroy_popup<Message>(id: iced_core::window::Id) -> Message
|
pub fn destroy_popup<Message>(id: iced_core::window::Id) -> Message
|
||||||
where
|
where
|
||||||
Message: From<SurfaceMessage> + 'static,
|
Message: From<SurfaceMessage> + 'static,
|
||||||
|
|
@ -53,12 +54,14 @@ pub mod message {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn destroy_subsurface<M: From<SurfaceMessage>>(id: iced_core::window::Id) -> M {
|
pub fn destroy_subsurface<M: From<SurfaceMessage>>(id: iced_core::window::Id) -> M {
|
||||||
let surface_msg = SurfaceMessage::DestroySubsurface(id);
|
let surface_msg = SurfaceMessage::DestroySubsurface(id);
|
||||||
M::from(surface_msg)
|
M::from(surface_msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn app_popup<App: super::Application>(
|
pub fn app_popup<App: super::Application>(
|
||||||
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
||||||
+ Send
|
+ Send
|
||||||
|
|
@ -99,6 +102,7 @@ pub mod message {
|
||||||
|
|
||||||
/// Used to create a subsurface message from within a widget.
|
/// Used to create a subsurface message from within a widget.
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn simple_subsurface<Message, V>(
|
pub fn simple_subsurface<Message, V>(
|
||||||
settings: impl Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
|
settings: impl Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
|
||||||
view: Option<
|
view: Option<
|
||||||
|
|
@ -109,10 +113,9 @@ pub mod message {
|
||||||
+ 'static,
|
+ 'static,
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
) -> Message
|
) -> SurfaceMessage
|
||||||
where
|
where
|
||||||
Message:
|
Message: From<SurfaceMessage> + 'static,
|
||||||
Into<crate::surface_message::MessageWrapper<Message>> + From<SurfaceMessage> + 'static,
|
|
||||||
V:,
|
V:,
|
||||||
{
|
{
|
||||||
use std::{any::Any, sync::Arc};
|
use std::{any::Any, sync::Arc};
|
||||||
|
|
@ -126,17 +129,18 @@ pub mod message {
|
||||||
> = Box::new(settings);
|
> = Box::new(settings);
|
||||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||||
|
|
||||||
Message::from(SurfaceMessage::Subsurface(
|
SurfaceMessage::Subsurface(
|
||||||
Arc::new(boxed),
|
Arc::new(boxed),
|
||||||
view.map(|view| {
|
view.map(|view| {
|
||||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
|
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
|
||||||
Arc::new(boxed)
|
Arc::new(boxed)
|
||||||
}),
|
}),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to create a popup message from within a widget.
|
/// Used to create a popup message from within a widget.
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn simple_popup<Message, V>(
|
pub fn simple_popup<Message, V>(
|
||||||
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
|
||||||
+ Send
|
+ Send
|
||||||
|
|
@ -145,10 +149,9 @@ pub mod message {
|
||||||
view: Option<
|
view: Option<
|
||||||
impl Fn() -> crate::Element<'static, crate::app::Message<Message>> + Send + Sync + 'static,
|
impl Fn() -> crate::Element<'static, crate::app::Message<Message>> + Send + Sync + 'static,
|
||||||
>,
|
>,
|
||||||
) -> Message
|
) -> SurfaceMessage
|
||||||
where
|
where
|
||||||
Message:
|
Message: From<SurfaceMessage> + 'static,
|
||||||
Into<crate::surface_message::MessageWrapper<Message>> + From<SurfaceMessage> + 'static,
|
|
||||||
V:,
|
V:,
|
||||||
{
|
{
|
||||||
use std::{any::Any, sync::Arc};
|
use std::{any::Any, sync::Arc};
|
||||||
|
|
@ -162,7 +165,7 @@ pub mod message {
|
||||||
> = Box::new(settings);
|
> = Box::new(settings);
|
||||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||||
|
|
||||||
Message::from(SurfaceMessage::Popup(
|
SurfaceMessage::Popup(
|
||||||
Arc::new(boxed),
|
Arc::new(boxed),
|
||||||
view.map(|view| {
|
view.map(|view| {
|
||||||
let boxed: Box<
|
let boxed: Box<
|
||||||
|
|
@ -174,10 +177,11 @@ pub mod message {
|
||||||
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
|
||||||
Arc::new(boxed)
|
Arc::new(boxed)
|
||||||
}),
|
}),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(feature = "wayland")]
|
||||||
|
#[must_use]
|
||||||
pub fn subsurface<App: super::Application>(
|
pub fn subsurface<App: super::Application>(
|
||||||
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
|
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.
|
// 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::core::Core;
|
||||||
pub use self::settings::Settings;
|
pub use self::settings::Settings;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use crate::surface_message::SurfaceMessage;
|
||||||
use crate::theme::THEME;
|
use crate::theme::THEME;
|
||||||
use crate::widget::{container, horizontal_space, id_container, menu, nav_bar, popover};
|
use crate::widget::{container, horizontal_space, id_container, menu, nav_bar, popover};
|
||||||
use apply::Apply;
|
use apply::Apply;
|
||||||
|
|
@ -314,7 +319,7 @@ pub(crate) fn iced_settings<App: Application>(
|
||||||
/// Returns error on application failure.
|
/// Returns error on application failure.
|
||||||
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
|
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
|
||||||
where
|
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")]
|
#[cfg(target_env = "gnu")]
|
||||||
if let Some(threshold) = settings.default_mmap_threshold {
|
if let Some(threshold) = settings.default_mmap_threshold {
|
||||||
|
|
@ -507,6 +512,7 @@ where
|
||||||
+ std::fmt::Debug
|
+ std::fmt::Debug
|
||||||
+ Send
|
+ Send
|
||||||
+ Into<crate::surface_message::MessageWrapper<App::Message>>
|
+ Into<crate::surface_message::MessageWrapper<App::Message>>
|
||||||
|
+ From<SurfaceMessage>
|
||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok();
|
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_core::{Layout, Padding, Shadow};
|
||||||
use iced_widget::runtime::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
|
use iced_widget::runtime::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
|
||||||
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
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 tracing::info;
|
||||||
|
|
||||||
use crate::app::cosmic;
|
use crate::app::cosmic;
|
||||||
|
|
@ -228,75 +228,61 @@ impl Context {
|
||||||
content: impl Into<Element<'a, Message>>,
|
content: impl Into<Element<'a, Message>>,
|
||||||
tooltip: impl Into<Cow<'static, str>>,
|
tooltip: impl Into<Cow<'static, str>>,
|
||||||
has_popup: bool,
|
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 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();
|
||||||
let on_hover = move |layout: Layout| {
|
|
||||||
let bounds = layout.bounds();
|
crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message, Message>::new(
|
||||||
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(
|
|
||||||
content,
|
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),
|
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
|
// 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.
|
/// Returns error on application failure.
|
||||||
pub fn run<App: Application>(flags: App::Flags) -> iced::Result
|
pub fn run<App: Application>(flags: App::Flags) -> iced::Result
|
||||||
where
|
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();
|
let helper = Context::default();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
|
use std::future::Future;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use iced::Size;
|
use iced::Size;
|
||||||
|
|
||||||
use iced::Limits;
|
use iced::Limits;
|
||||||
|
use iced::Task;
|
||||||
|
|
||||||
pub enum MessageWrapper<M> {
|
pub enum MessageWrapper<M> {
|
||||||
Surface(SurfaceMessage),
|
Surface(SurfaceMessage),
|
||||||
|
|
@ -51,6 +55,7 @@ pub enum SurfaceMessage {
|
||||||
size: Size,
|
size: Size,
|
||||||
},
|
},
|
||||||
Ignore,
|
Ignore,
|
||||||
|
Task(Arc<dyn Fn() -> Task<SurfaceMessage> + Send + Sync>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for SurfaceMessage {
|
impl std::fmt::Debug for SurfaceMessage {
|
||||||
|
|
@ -83,6 +88,7 @@ impl std::fmt::Debug for SurfaceMessage {
|
||||||
.field("size", size)
|
.field("size", size)
|
||||||
.finish(),
|
.finish(),
|
||||||
Self::Ignore => write!(f, "Ignore"),
|
Self::Ignore => write!(f, "Ignore"),
|
||||||
|
Self::Task(_) => f.debug_tuple("Future").finish(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ mod text_input;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use self::text_input::TextInput;
|
pub use self::text_input::TextInput;
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(all(feature = "wayland", feature = "winit"))]
|
||||||
pub mod tooltip;
|
pub mod tooltip;
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(all(feature = "wayland", feature = "winit"))]
|
||||||
pub use tooltip::Tooltip;
|
pub use tooltip::Tooltip;
|
||||||
|
|
|
||||||
|
|
@ -339,7 +339,7 @@ pub use toggler::toggler;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use tooltip::{tooltip, Tooltip};
|
pub use tooltip::{tooltip, Tooltip};
|
||||||
|
|
||||||
#[cfg(feature = "wayland")]
|
#[cfg(all(feature = "wayland", feature = "winit"))]
|
||||||
pub mod wayland;
|
pub mod wayland;
|
||||||
|
|
||||||
pub mod tooltip {
|
pub mod tooltip {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,11 @@
|
||||||
//!
|
//!
|
||||||
//! A [`Tooltip`] has some local [`State`].
|
//! 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_runtime::core::widget::Id;
|
||||||
|
|
||||||
use iced_core::event::{self, Event};
|
use iced_core::event::{self, Event};
|
||||||
|
|
@ -20,6 +25,8 @@ use iced_core::{
|
||||||
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
|
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::surface_message::SurfaceMessage;
|
||||||
|
|
||||||
pub use super::{Catalog, Style};
|
pub use super::{Catalog, Style};
|
||||||
|
|
||||||
/// Internally defines different button widget variants.
|
/// Internally defines different button widget variants.
|
||||||
|
|
@ -34,7 +41,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, AppMessage> {
|
||||||
id: Id,
|
id: Id,
|
||||||
#[cfg(feature = "a11y")]
|
#[cfg(feature = "a11y")]
|
||||||
name: Option<std::borrow::Cow<'a, str>>,
|
name: Option<std::borrow::Cow<'a, str>>,
|
||||||
|
|
@ -43,20 +50,43 @@ pub struct Tooltip<'a, Message> {
|
||||||
#[cfg(feature = "a11y")]
|
#[cfg(feature = "a11y")]
|
||||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||||
content: crate::Element<'a, Message>,
|
content: crate::Element<'a, Message>,
|
||||||
on_hover: Box<dyn Fn(iced_core::Layout) -> Message + 'a>,
|
|
||||||
on_leave: Message,
|
on_leave: Message,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
selected: bool,
|
selected: bool,
|
||||||
style: crate::theme::Tooltip,
|
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.
|
/// 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>>,
|
||||||
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,
|
on_leave: Message,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -68,16 +98,27 @@ impl<'a, Message> Tooltip<'a, Message> {
|
||||||
#[cfg(feature = "a11y")]
|
#[cfg(feature = "a11y")]
|
||||||
label: None,
|
label: None,
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
on_hover: Box::new(on_hover),
|
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
padding: Padding::new(0.0),
|
padding: Padding::new(0.0),
|
||||||
selected: false,
|
selected: false,
|
||||||
style: crate::theme::Tooltip::default(),
|
style: crate::theme::Tooltip::default(),
|
||||||
on_leave,
|
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`].
|
/// Sets the [`Id`] of the [`Tooltip`].
|
||||||
pub fn id(mut self, id: Id) -> Self {
|
pub fn id(mut self, id: Id) -> Self {
|
||||||
self.id = id;
|
self.id = id;
|
||||||
|
|
@ -148,8 +189,11 @@ impl<'a, Message> Tooltip<'a, Message> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
|
impl<
|
||||||
for Tooltip<'a, Message>
|
'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 {
|
fn tag(&self) -> tree::Tag {
|
||||||
tree::Tag::of::<State>()
|
tree::Tag::of::<State>()
|
||||||
|
|
@ -206,7 +250,6 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
|
||||||
operation,
|
operation,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
let state = tree.state.downcast_mut::<State>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
|
|
@ -226,7 +269,9 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
|
||||||
layout,
|
layout,
|
||||||
cursor,
|
cursor,
|
||||||
shell,
|
shell,
|
||||||
&self.on_hover,
|
self.settings.as_ref(),
|
||||||
|
&self.view,
|
||||||
|
self.delay,
|
||||||
&self.on_leave,
|
&self.on_leave,
|
||||||
|| tree.state.downcast_mut::<State>(),
|
|| 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> {
|
impl<
|
||||||
fn from(button: Tooltip<'a, Message>) -> Self {
|
'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)
|
Self::new(button)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The local state of a [`Tooltip`].
|
/// The local state of a [`Tooltip`].
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
#[allow(clippy::struct_field_names)]
|
#[allow(clippy::struct_field_names)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
is_hovered: bool,
|
is_hovered: Arc<Mutex<bool>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
/// Returns whether the [`Tooltip`] is currently hovered or not.
|
/// Returns whether the [`Tooltip`] is currently hovered or not.
|
||||||
pub fn is_hovered(self) -> bool {
|
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`]
|
/// 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>(
|
pub fn update<
|
||||||
|
'a,
|
||||||
|
Message: Clone + From<SurfaceMessage>,
|
||||||
|
AppMessage: Clone + 'a + From<SurfaceMessage> + 'static,
|
||||||
|
>(
|
||||||
_id: Id,
|
_id: Id,
|
||||||
event: Event,
|
event: Event,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
shell: &mut Shell<'_, Message>,
|
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,
|
on_leave: &Message,
|
||||||
state: impl FnOnce() -> &'a mut State,
|
state: impl FnOnce() -> &'a mut State,
|
||||||
) -> event::Status {
|
) -> event::Status {
|
||||||
match event {
|
match event {
|
||||||
Event::Touch(touch::Event::FingerLifted { .. }) => {
|
Event::Touch(touch::Event::FingerLifted { .. }) => {
|
||||||
let state = state();
|
let state = state();
|
||||||
|
let mut guard = state.is_hovered.lock().unwrap();
|
||||||
if state.is_hovered {
|
if *guard {
|
||||||
state.is_hovered = false;
|
*guard = false;
|
||||||
|
|
||||||
shell.publish(on_leave.clone());
|
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) => {
|
Event::Touch(touch::Event::FingerLost { .. }) | Event::Mouse(mouse::Event::CursorLeft) => {
|
||||||
let state = state();
|
let state = state();
|
||||||
if state.is_hovered {
|
let mut guard = state.is_hovered.lock().unwrap();
|
||||||
state.is_hovered = false;
|
|
||||||
|
if *guard {
|
||||||
|
*guard = false;
|
||||||
|
|
||||||
shell.publish(on_leave.clone());
|
shell.publish(on_leave.clone());
|
||||||
}
|
}
|
||||||
|
|
@ -403,16 +474,95 @@ pub fn update<'a, Message: Clone>(
|
||||||
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
||||||
let state = state();
|
let state = state();
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
let is_hovered = state.is_hovered.clone();
|
||||||
|
let mut guard = state.is_hovered.lock().unwrap();
|
||||||
|
|
||||||
if state.is_hovered {
|
if *guard {
|
||||||
state.is_hovered = cursor.is_over(bounds);
|
*guard = cursor.is_over(bounds);
|
||||||
if !state.is_hovered {
|
if !*guard {
|
||||||
shell.publish(on_leave.clone());
|
shell.publish(on_leave.clone());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
state.is_hovered = cursor.is_over(bounds);
|
*guard = cursor.is_over(bounds);
|
||||||
if state.is_hovered {
|
if *guard {
|
||||||
shell.publish(on_hover(layout).clone());
|
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