wip: tooltips

This commit is contained in:
Ashley Wulber 2024-12-31 10:57:08 -05:00
parent 91459591ec
commit 681aed735b
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
10 changed files with 939 additions and 69 deletions

View file

@ -85,7 +85,7 @@ pub enum Message {
pub struct Cosmic<App: Application> {
pub app: App,
#[cfg(feature = "wayland")]
pub subsurfaces:
pub surface_views:
HashMap<window::Id, Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>>>>,
}
@ -129,18 +129,63 @@ where
super::Message::DbusActivation(message) => self.app.dbus_activation(message),
#[cfg(feature = "wayland")]
super::Message::Subsurface(settings, view) => {
if let Some(view) = view.and_then(|view| match view.downcast() {
Ok(v) => Some(v),
Err(err) => {
tracing::error!("Invalid view for subsurface view: {err:?}");
None
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();
};
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn(&T) -> Element<'static, super::Message<T::Message>> + Send + Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
tracing::error!("Invalid view for subsurface view: {err:?}");
None
}
}
}) {
self.get_subsurface(settings, view)
let settings = settings(&mut self.app);
self.get_subsurface(settings, *view)
} else {
iced_winit::commands::subsurface::get_subsurface(settings)
iced_winit::commands::subsurface::get_subsurface(settings(&mut self.app))
}
}
#[cfg(feature = "wayland")]
super::Message::Popup(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();
};
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn(&T) -> Element<'static, super::Message<T::Message>> + Send + Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
tracing::error!("Invalid view for subsurface view: {err:?}");
None
}
}
}) {
let settings = settings(&mut self.app);
self.get_popup(settings, *view)
} else {
iced_winit::commands::popup::get_popup(settings(&mut self.app))
}
}
#[cfg(feature = "wayland")]
super::Message::DestroyPopup(id) => iced_winit::commands::popup::destroy_popup(id),
#[cfg(feature = "wayland")]
super::Message::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id)
}
};
#[cfg(target_env = "gnu")]
@ -306,7 +351,7 @@ where
#[cfg(feature = "multi-window")]
pub fn view(&self, id: window::Id) -> Element<super::Message<T::Message>> {
#[cfg(feature = "wayland")]
if let Some(v) = self.subsurfaces.get(&id) {
if let Some(v) = self.surface_views.get(&id) {
return v(&self.app);
}
if !self
@ -722,7 +767,7 @@ impl<App: Application> Cosmic<App> {
Self {
app,
#[cfg(feature = "wayland")]
subsurfaces: HashMap::new(),
surface_views: HashMap::new(),
}
}
@ -731,11 +776,24 @@ impl<App: Application> Cosmic<App> {
pub fn get_subsurface(
&mut self,
settings: iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings,
view: Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>>>,
view: Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>> + Send + Sync>,
) -> Task<super::Message<App::Message>> {
use iced_winit::commands::subsurface::get_subsurface;
self.subsurfaces.insert(settings.id, view);
self.surface_views.insert(settings.id, view);
get_subsurface(settings)
}
#[cfg(feature = "wayland")]
/// Create a subsurface
pub fn get_popup(
&mut self,
settings: iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
view: Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>> + Send + Sync>,
) -> Task<super::Message<App::Message>> {
use iced_winit::commands::popup::get_popup;
self.surface_views.insert(settings.id, view);
get_popup(settings)
}
}

View file

@ -15,7 +15,7 @@ pub(crate) mod multi_window;
pub mod settings;
pub mod message {
#[derive(Clone, Debug)]
#[derive(Clone)]
#[must_use]
pub enum Message<M> {
/// Messages from the application, for the application.
@ -30,9 +30,21 @@ pub mod message {
#[cfg(feature = "wayland")]
/// Create a subsurface with a view function
Subsurface(
iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings,
Option<Box<dyn std::any::Any>>,
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
#[cfg(feature = "wayland")]
/// Destroy a subsurface with a view function
DestroySubsurface(iced::window::Id),
#[cfg(feature = "wayland")]
/// Create a popup with a view function
Popup(
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
#[cfg(feature = "wayland")]
/// Destroy a subsurface with a view function
DestroyPopup(iced::window::Id),
}
pub const fn app<M>(message: M) -> Message<M> {
@ -47,11 +59,118 @@ pub mod message {
Message::None
}
#[cfg(feature = "wayland")]
pub const fn destroy_popup<M>(id: iced_core::window::Id) -> Message<M> {
Message::DestroyPopup(id)
}
#[cfg(feature = "wayland")]
pub const fn destroy_subsurface<M>(id: iced_core::window::Id) -> Message<M> {
Message::DestroySubsurface(id)
}
#[cfg(feature = "wayland")]
pub fn get_popup<App: crate::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
) -> Message<App::Message> {
use std::{any::Any, sync::Arc};
let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Message::Popup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
}
#[cfg(feature = "wayland")]
pub fn get_subsurface<App: crate::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
) -> Message<App::Message> {
use std::{any::Any, sync::Arc};
let boxed: Box<
dyn Fn(
&mut App,
)
-> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Message::Subsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
}
impl<M> From<M> for Message<M> {
fn from(value: M) -> Self {
Self::App(value)
}
}
impl<M: std::fmt::Debug> std::fmt::Debug for Message<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::App(arg0) => f.debug_tuple("App").field(arg0).finish(),
Self::Cosmic(arg0) => f.debug_tuple("Cosmic").field(arg0).finish(),
#[cfg(feature = "single-instance")]
Self::DbusActivation(arg0) => f.debug_tuple("DbusActivation").field(arg0).finish(),
Self::None => write!(f, "None"),
#[cfg(feature = "wayland")]
Self::Subsurface(arg0, arg1) => f.debug_tuple("Subsurface").field(arg1).finish(),
#[cfg(feature = "wayland")]
Self::DestroySubsurface(arg0) => {
f.debug_tuple("DestroySubsurface").field(arg0).finish()
}
#[cfg(feature = "wayland")]
Self::Popup(arg0, arg1) => f.debug_tuple("Popup").field(arg1).finish(),
#[cfg(feature = "wayland")]
Self::DestroyPopup(arg0) => f.debug_tuple("DestroyPopup").field(arg0).finish(),
}
}
}
}
use std::borrow::Cow;