better surface messages

This commit is contained in:
Ashley Wulber 2024-12-31 17:18:49 -05:00
parent 681aed735b
commit b316d8901b
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
4 changed files with 256 additions and 196 deletions

View file

@ -91,7 +91,7 @@ pub struct Cosmic<App: Application> {
impl<T: Application> Cosmic<T>
where
T::Message: Send + 'static,
T::Message: Send + 'static + super::message::SurfaceMessageHandler,
{
pub fn init(
(mut core, flags): (Core, T::Flags),
@ -122,70 +122,93 @@ where
message: super::Message<T::Message>,
) -> iced::Task<super::Message<T::Message>> {
let message = match message {
super::Message::App(message) => self.app.update(message),
super::Message::App(message) => {
match super::message::SurfaceMessageHandler::to_surface_message(message) {
super::message::MessageWrapper::Surface(surface_message) =>
{
#[cfg(feature = "wayland")]
match surface_message {
super::message::SurfaceMessage::Subsurface(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();
};
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_subsurface(settings, *view)
} else {
iced_winit::commands::subsurface::get_subsurface(settings(
&mut self.app,
))
}
}
#[cfg(feature = "wayland")]
super::message::SurfaceMessage::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::SurfaceMessage::DestroyPopup(id) => {
iced_winit::commands::popup::destroy_popup(id)
}
#[cfg(feature = "wayland")]
super::message::SurfaceMessage::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id)
}
}
}
super::message::MessageWrapper::Message(message) => self.app.update(message),
}
}
super::Message::Cosmic(message) => self.cosmic_update(message),
super::Message::None => iced::Task::none(),
#[cfg(feature = "single-instance")]
super::Message::DbusActivation(message) => self.app.dbus_activation(message),
#[cfg(feature = "wayland")]
super::Message::Subsurface(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();
};
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_subsurface(settings, *view)
} else {
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")]

View file

@ -15,7 +15,57 @@ pub(crate) mod multi_window;
pub mod settings;
pub mod message {
pub enum MessageWrapper<M> {
Surface(SurfaceMessage),
Message(M),
}
pub trait SurfaceMessageHandler: Sized {
fn to_surface_message(self) -> MessageWrapper<Self>;
}
#[cfg(not(feature = "wayland"))]
impl<M> SurfaceMessageHandler for M {
fn to_surface_message(self) -> MessageWrapper<Self> {
MessageWrapper::Message(self)
}
}
#[derive(Clone)]
pub enum SurfaceMessage {
/// Create a subsurface with a view function
Subsurface(
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
/// Destroy a subsurface with a view function
DestroySubsurface(iced::window::Id),
/// 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>>>,
),
/// Destroy a subsurface with a view function
DestroyPopup(iced::window::Id),
}
#[cfg(feature = "wayland")]
impl std::fmt::Debug for SurfaceMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SurfaceMessage::Subsurface(any, any1) => f.debug_tuple("Subsurface").finish(),
SurfaceMessage::DestroySubsurface(id) => {
f.debug_tuple("DestroySubsurface").field(id).finish()
}
SurfaceMessage::Popup(any, any1) => f.debug_tuple("Popup").finish(),
SurfaceMessage::DestroyPopup(id) => {
f.debug_tuple("DestroyPopup").field(id).finish()
}
}
}
}
#[derive(Clone, Debug)]
#[must_use]
pub enum Message<M> {
/// Messages from the application, for the application.
@ -27,24 +77,6 @@ pub mod message {
DbusActivation(super::DbusActivationMessage),
/// Do nothing
None,
#[cfg(feature = "wayland")]
/// Create a subsurface with a view function
Subsurface(
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> {
@ -60,17 +92,25 @@ pub mod message {
}
#[cfg(feature = "wayland")]
pub const fn destroy_popup<M>(id: iced_core::window::Id) -> Message<M> {
Message::DestroyPopup(id)
pub fn destroy_popup<App: super::Application>(id: iced_core::window::Id) -> App::Message
where
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
let surface_msg = SurfaceMessage::DestroyPopup(id);
App::Message::from(surface_msg)
}
#[cfg(feature = "wayland")]
pub const fn destroy_subsurface<M>(id: iced_core::window::Id) -> Message<M> {
Message::DestroySubsurface(id)
pub fn destroy_subsurface<App: super::Application>(id: iced_core::window::Id) -> App::Message
where
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
let surface_msg = SurfaceMessage::DestroySubsurface(id);
App::Message::from(surface_msg)
}
#[cfg(feature = "wayland")]
pub fn get_popup<App: crate::Application>(
pub fn get_popup<App: super::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
@ -81,7 +121,10 @@ pub mod message {
+ Sync
+ 'static,
>,
) -> Message<App::Message> {
) -> App::Message
where
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
use std::{any::Any, sync::Arc};
let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
@ -91,7 +134,7 @@ pub mod message {
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Message::Popup(
App::Message::from(SurfaceMessage::Popup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
@ -103,11 +146,11 @@ pub mod message {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
))
}
#[cfg(feature = "wayland")]
pub fn get_subsurface<App: crate::Application>(
pub fn get_subsurface<App: super::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>>
@ -115,7 +158,10 @@ pub mod message {
+ Sync
+ 'static,
>,
) -> Message<App::Message> {
) -> App::Message
where
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
use std::{any::Any, sync::Arc};
let boxed: Box<
dyn Fn(
@ -128,7 +174,7 @@ pub mod message {
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Message::Subsurface(
App::Message::from(SurfaceMessage::Subsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
@ -140,7 +186,7 @@ pub mod message {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
))
}
impl<M> From<M> for Message<M> {
@ -148,29 +194,6 @@ pub mod message {
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;
@ -186,6 +209,7 @@ use context_drawer::ContextDrawer;
use iced::window;
use iced::{Length, Subscription};
pub use message::Message;
use message::SurfaceMessageHandler;
use url::Url;
#[cfg(feature = "single-instance")]
use {
@ -262,12 +286,16 @@ pub(crate) fn iced_settings<App: Application>(
/// # Errors
///
/// 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
App::Message: SurfaceMessageHandler,
{
#[cfg(target_env = "gnu")]
if let Some(threshold) = settings.default_mmap_threshold {
crate::malloc::limit_mmap_threshold(threshold);
}
let default_font = settings.default_font;
let (settings, mut flags, window_settings) = iced_settings::<App>(settings, flags);
#[cfg(not(feature = "multi-window"))]
{