refactor: SurfaceMessage

This commit is contained in:
Ashley Wulber 2025-02-18 00:59:11 -05:00
parent c6ca33457f
commit 56bbc3f289
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
9 changed files with 201 additions and 98 deletions

View file

@ -4,6 +4,7 @@
use std::{cell::OnceCell, collections::HashMap};
use crate::{
surface_message::SurfaceMessage,
theme::menu_bar,
widget::{
button, icon,
@ -18,9 +19,9 @@ use iced_core::{window::Id, Element};
use palette::Srgba;
use slotmap::Key;
use crate::Theme;
use crate::{surface_message::SurfaceMessageHandler, Theme};
use super::{message::SurfaceMessage, Renderer, Task};
use super::{Renderer, Task};
/// Status of the nav bar and its panels.
#[derive(Clone)]

View file

@ -7,6 +7,7 @@ use std::sync::Arc;
use super::{Application, ApplicationExt, Core, Subscription};
use crate::config::CosmicTk;
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use crate::theme::{Theme, ThemeType, THEME};
use crate::widget::nav_bar;
use crate::{keyboard_nav, Element};
@ -91,7 +92,7 @@ pub struct Cosmic<App: Application> {
impl<T: Application> Cosmic<T>
where
T::Message: Send + 'static + super::message::SurfaceMessageHandler,
T::Message: Send + 'static + crate::surface_message::SurfaceMessageHandler,
{
pub fn init(
(mut core, flags): (Core, T::Flags),
@ -123,11 +124,11 @@ where
) -> iced::Task<super::Message<T::Message>> {
let message = match message {
super::Message::App(message) => {
match super::message::SurfaceMessageHandler::to_surface_message(message) {
super::message::MessageWrapper::Surface(surface_message) => {
match SurfaceMessageHandler::to_surface_message(message) {
MessageWrapper::Surface(surface_message) => {
#[cfg(feature = "wayland")]
match surface_message {
super::message::SurfaceMessage::Subsurface(settings, view) => {
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 {
@ -159,7 +160,7 @@ where
))
}
}
super::message::SurfaceMessage::Popup(settings, view) => {
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 {
@ -191,14 +192,14 @@ where
}
}
#[cfg(feature = "wayland")]
super::message::SurfaceMessage::DestroyPopup(id) => {
SurfaceMessage::DestroyPopup(id) => {
iced_winit::commands::popup::destroy_popup(id)
}
#[cfg(feature = "wayland")]
super::message::SurfaceMessage::DestroySubsurface(id) => {
SurfaceMessage::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id)
}
super::message::SurfaceMessage::ResponsiveMenuBar {
SurfaceMessage::ResponsiveMenuBar {
menu_bar,
limits,
size,
@ -207,11 +208,42 @@ where
core.menu_bars.insert(menu_bar, (limits, size));
iced::Task::none()
}
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();
};
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn() -> 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();
self.get_popup(settings, Box::new(move |_| view()))
} else {
iced_winit::commands::popup::get_popup(settings())
}
}
}
#[cfg(not(feature = "wayland"))]
iced::Task::none()
}
super::message::MessageWrapper::Message(message) => self.app.update(message),
MessageWrapper::Message(message) => self.app.update(message),
}
}
super::Message::Cosmic(message) => self.cosmic_update(message),

View file

@ -6,6 +6,8 @@
//! Check out our [application](https://github.com/pop-os/libcosmic/tree/master/examples/application)
//! example in our repository.
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
pub mod command;
pub mod context_drawer;
mod core;
@ -15,78 +17,10 @@ pub(crate) mod multi_window;
pub mod settings;
pub mod message {
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use iced::{Limits, Size};
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)
}
}
/// Ignore this message in your application. It will be intercepted.
#[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),
/// Responsive menu bar update
ResponsiveMenuBar {
/// Id of the menu bar
menu_bar: crate::widget::Id,
/// Limits of the menu bar
limits: Limits,
/// Requested Full Size for expanded menu bar
size: Size,
},
}
#[cfg(feature = "wayland")]
impl std::fmt::Debug for SurfaceMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Subsurface(arg0, arg1) => {
f.debug_tuple("Subsurface").field(arg0).field(arg1).finish()
}
Self::DestroySubsurface(arg0) => {
f.debug_tuple("DestroySubsurface").field(arg0).finish()
}
Self::Popup(arg0, arg1) => f.debug_tuple("Popup").field(arg0).field(arg1).finish(),
Self::DestroyPopup(arg0) => f.debug_tuple("DestroyPopup").field(arg0).finish(),
Self::ResponsiveMenuBar {
menu_bar,
limits,
size,
} => f
.debug_struct("ResponsiveMenuBar")
.field("menu_bar", menu_bar)
.field("limits", limits)
.field("size", size)
.finish(),
}
}
}
#[derive(Clone, Debug)]
#[must_use]
pub enum Message<M> {
@ -122,6 +56,15 @@ pub mod message {
App::Message::from(surface_msg)
}
/// Used to produce a destroy popup message from within a widget.
#[cfg(feature = "wayland")]
pub fn destroy_popup_simple<Message>(id: iced_core::window::Id) -> Message
where
Message: SurfaceMessageHandler + From<SurfaceMessage> + 'static,
{
Message::from(SurfaceMessage::DestroyPopup(id))
}
#[cfg(feature = "wayland")]
pub fn destroy_subsurface<App: super::Application>(id: iced_core::window::Id) -> App::Message
where
@ -132,7 +75,7 @@ pub mod message {
}
#[cfg(feature = "wayland")]
pub fn get_popup<App: super::Application>(
pub fn app_popup<App: super::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
@ -148,6 +91,8 @@ pub mod message {
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
@ -156,7 +101,7 @@ pub mod message {
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
App::Message::from(SurfaceMessage::Popup(
App::Message::from(SurfaceMessage::AppPopup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
@ -171,6 +116,46 @@ pub mod message {
))
}
/// Used to create a popup message from within a widget.
#[cfg(feature = "wayland")]
pub fn simple_popup<Message>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
impl Fn() -> crate::Element<'static, crate::app::Message<Message>> + Send + Sync + 'static,
>,
) -> Message
where
Message: SurfaceMessageHandler + From<SurfaceMessage> + 'static,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box<
dyn Fn() -> 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::from(SurfaceMessage::Popup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn() -> crate::Element<'static, super::Message<Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
))
}
// TODO could this somehow be used by widgets? It is the wrong message type though.
// #[cfg(feature = "wayland")]
// pub fn get_simple_popup<App: super::Application>(
@ -186,7 +171,7 @@ pub mod message {
// }
#[cfg(feature = "wayland")]
pub fn get_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,
view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
@ -199,6 +184,8 @@ pub mod message {
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box<
dyn Fn(
&mut App,
@ -245,7 +232,6 @@ 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 {