refactor: SurfaceMessage

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

View file

@ -1,10 +1,10 @@
use cosmic::app::message::{SurfaceMessage, SurfaceMessageHandler};
use cosmic::app::Core; use cosmic::app::Core;
use cosmic::iced::window::Id; use cosmic::iced::window::Id;
use cosmic::iced::{Length, Limits, Task}; use cosmic::iced::{Length, Limits, Task};
use cosmic::iced_runtime::core::window; use cosmic::iced_runtime::core::window;
use cosmic::iced_runtime::platform_specific::wayland::popup::SctkPopupSettings; use cosmic::iced_runtime::platform_specific::wayland::popup::SctkPopupSettings;
use cosmic::iced_runtime::platform_specific::wayland::subsurface; use cosmic::iced_runtime::platform_specific::wayland::subsurface;
use cosmic::surface_message::{SurfaceMessage, SurfaceMessageHandler};
use cosmic::widget::dropdown::DropdownView; use cosmic::widget::dropdown::DropdownView;
use cosmic::widget::{autosize, dropdown, layer_container, list_column, settings, toggler}; use cosmic::widget::{autosize, dropdown, layer_container, list_column, settings, toggler};
use cosmic::{iced_core, Element}; use cosmic::{iced_core, Element};
@ -63,10 +63,10 @@ impl std::fmt::Debug for Message {
} }
impl SurfaceMessageHandler for Message { impl SurfaceMessageHandler for Message {
fn to_surface_message(self) -> cosmic::app::message::MessageWrapper<Self> { fn to_surface_message(self) -> cosmic::surface_message::MessageWrapper<Self> {
match self { match self {
Message::Surface(msg) => cosmic::app::message::MessageWrapper::Surface(msg), Message::Surface(msg) => cosmic::surface_message::MessageWrapper::Surface(msg),
msg => cosmic::app::message::MessageWrapper::Message(msg), msg => cosmic::surface_message::MessageWrapper::Message(msg),
} }
} }
} }
@ -123,7 +123,7 @@ impl cosmic::Application for Window {
} }
Message::OpenDropdown(sctk_popup_settings, view) => { Message::OpenDropdown(sctk_popup_settings, view) => {
self.dropdown_id = sctk_popup_settings.id; self.dropdown_id = sctk_popup_settings.id;
return cosmic::task::message(cosmic::app::message::get_popup::< return cosmic::task::message(cosmic::app::message::app_popup::<
Window, Window,
>( >(
move |_: &mut Window| sctk_popup_settings.clone(), move |_: &mut Window| sctk_popup_settings.clone(),
@ -148,7 +148,7 @@ impl cosmic::Application for Window {
if let Some(id) = self.popup { if let Some(id) = self.popup {
cosmic::app::message::destroy_popup::<Window>(id) cosmic::app::message::destroy_popup::<Window>(id)
} else { } else {
cosmic::app::message::get_popup::<Window>( cosmic::app::message::app_popup::<Window>(
|state: &mut Window| { |state: &mut Window| {
let new_id = Id::unique(); let new_id = Id::unique();
state.popup = Some(new_id); state.popup = Some(new_id);
@ -198,7 +198,7 @@ impl cosmic::Application for Window {
}, },
), ),
|_| { |_| {
cosmic::app::message::get_subsurface( cosmic::app::message::subsurface(
|app: &mut Window| { |app: &mut Window| {
subsurface::SctkSubsurfaceSettings { subsurface::SctkSubsurfaceSettings {
parent: window::Id::RESERVED, parent: window::Id::RESERVED,

View file

@ -6,12 +6,12 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::LazyLock; use std::sync::LazyLock;
use cosmic::app::message::{SurfaceMessage, SurfaceMessageHandler};
use cosmic::app::{Core, Settings, Task}; use cosmic::app::{Core, Settings, Task};
use cosmic::iced::alignment::{Horizontal, Vertical}; use cosmic::iced::alignment::{Horizontal, Vertical};
use cosmic::iced::widget::column; use cosmic::iced::widget::column;
use cosmic::iced::Length; use cosmic::iced::Length;
use cosmic::iced_core::Size; use cosmic::iced_core::Size;
use cosmic::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use cosmic::widget::icon::{from_name, Handle}; use cosmic::widget::icon::{from_name, Handle};
use cosmic::widget::menu::KeyBind; use cosmic::widget::menu::KeyBind;
use cosmic::widget::{ use cosmic::widget::{
@ -88,10 +88,10 @@ pub enum Message {
} }
impl SurfaceMessageHandler for Message { impl SurfaceMessageHandler for Message {
fn to_surface_message(self) -> cosmic::app::message::MessageWrapper<Self> { fn to_surface_message(self) -> MessageWrapper<Self> {
match self { match self {
Message::Surface(msg) => cosmic::app::message::MessageWrapper::Surface(msg), Message::Surface(msg) => MessageWrapper::Surface(msg),
msg => cosmic::app::message::MessageWrapper::Message(msg), msg => MessageWrapper::Message(msg),
} }
} }
} }

View file

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

View file

@ -7,6 +7,7 @@ use std::sync::Arc;
use super::{Application, ApplicationExt, Core, Subscription}; use super::{Application, ApplicationExt, Core, Subscription};
use crate::config::CosmicTk; use crate::config::CosmicTk;
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use crate::theme::{Theme, ThemeType, THEME}; use crate::theme::{Theme, ThemeType, THEME};
use crate::widget::nav_bar; use crate::widget::nav_bar;
use crate::{keyboard_nav, Element}; use crate::{keyboard_nav, Element};
@ -91,7 +92,7 @@ pub struct Cosmic<App: Application> {
impl<T: Application> Cosmic<T> impl<T: Application> Cosmic<T>
where where
T::Message: Send + 'static + super::message::SurfaceMessageHandler, T::Message: Send + 'static + crate::surface_message::SurfaceMessageHandler,
{ {
pub fn init( pub fn init(
(mut core, flags): (Core, T::Flags), (mut core, flags): (Core, T::Flags),
@ -123,11 +124,11 @@ where
) -> iced::Task<super::Message<T::Message>> { ) -> iced::Task<super::Message<T::Message>> {
let message = match message { let message = match message {
super::Message::App(message) => { super::Message::App(message) => {
match super::message::SurfaceMessageHandler::to_surface_message(message) { match SurfaceMessageHandler::to_surface_message(message) {
super::message::MessageWrapper::Surface(surface_message) => { MessageWrapper::Surface(surface_message) => {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
match surface_message { match surface_message {
super::message::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(&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 {
@ -159,7 +160,7 @@ where
)) ))
} }
} }
super::message::SurfaceMessage::Popup(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 {
@ -191,14 +192,14 @@ where
} }
} }
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
super::message::SurfaceMessage::DestroyPopup(id) => { SurfaceMessage::DestroyPopup(id) => {
iced_winit::commands::popup::destroy_popup(id) iced_winit::commands::popup::destroy_popup(id)
} }
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
super::message::SurfaceMessage::DestroySubsurface(id) => { SurfaceMessage::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id) iced_winit::commands::subsurface::destroy_subsurface(id)
} }
super::message::SurfaceMessage::ResponsiveMenuBar { SurfaceMessage::ResponsiveMenuBar {
menu_bar, menu_bar,
limits, limits,
size, size,
@ -207,11 +208,42 @@ where
core.menu_bars.insert(menu_bar, (limits, size)); core.menu_bars.insert(menu_bar, (limits, size));
iced::Task::none() 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"))] #[cfg(not(feature = "wayland"))]
iced::Task::none() 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), 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) //! Check out our [application](https://github.com/pop-os/libcosmic/tree/master/examples/application)
//! example in our repository. //! example in our repository.
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
pub mod command; pub mod command;
pub mod context_drawer; pub mod context_drawer;
mod core; mod core;
@ -15,78 +17,10 @@ pub(crate) mod multi_window;
pub mod settings; pub mod settings;
pub mod message { pub mod message {
use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler};
use iced::{Limits, Size}; 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)] #[derive(Clone, Debug)]
#[must_use] #[must_use]
pub enum Message<M> { pub enum Message<M> {
@ -122,6 +56,15 @@ pub mod message {
App::Message::from(surface_msg) 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")] #[cfg(feature = "wayland")]
pub fn destroy_subsurface<App: super::Application>(id: iced_core::window::Id) -> App::Message pub fn destroy_subsurface<App: super::Application>(id: iced_core::window::Id) -> App::Message
where where
@ -132,7 +75,7 @@ pub mod message {
} }
#[cfg(feature = "wayland")] #[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 settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send + Send
+ Sync + Sync
@ -148,6 +91,8 @@ pub mod message {
App::Message: SurfaceMessageHandler + From<SurfaceMessage>, App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{ {
use std::{any::Any, sync::Arc}; use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box< let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send + Send
@ -156,7 +101,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);
App::Message::from(SurfaceMessage::Popup( App::Message::from(SurfaceMessage::AppPopup(
Arc::new(boxed), Arc::new(boxed),
view.map(|view| { view.map(|view| {
let boxed: Box< 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. // TODO could this somehow be used by widgets? It is the wrong message type though.
// #[cfg(feature = "wayland")] // #[cfg(feature = "wayland")]
// pub fn get_simple_popup<App: super::Application>( // pub fn get_simple_popup<App: super::Application>(
@ -186,7 +171,7 @@ pub mod message {
// } // }
#[cfg(feature = "wayland")] #[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, settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
view: Option< view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>> impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
@ -199,6 +184,8 @@ pub mod message {
App::Message: SurfaceMessageHandler + From<SurfaceMessage>, App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{ {
use std::{any::Any, sync::Arc}; use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box< let boxed: Box<
dyn Fn( dyn Fn(
&mut App, &mut App,
@ -245,7 +232,6 @@ use context_drawer::ContextDrawer;
use iced::window; use iced::window;
use iced::{Length, Subscription}; use iced::{Length, Subscription};
pub use message::Message; pub use message::Message;
use message::SurfaceMessageHandler;
use url::Url; use url::Url;
#[cfg(feature = "single-instance")] #[cfg(feature = "single-instance")]
use { use {

View file

@ -2,7 +2,7 @@
pub mod token; pub mod token;
use crate::{ use crate::{
app::{self, iced_settings, message::SurfaceMessageHandler, Core}, app::{self, iced_settings, Core},
cctk::sctk, cctk::sctk,
iced::{ iced::{
self, self,
@ -11,6 +11,7 @@ use crate::{
window, Color, Length, Limits, Rectangle, window, Color, Length, Limits, Rectangle,
}, },
iced_widget, iced_widget,
surface_message::SurfaceMessageHandler,
theme::{self, system_dark, system_light, Button, THEME}, theme::{self, system_dark, system_light, Button, THEME},
widget::{ widget::{
self, self,

View file

@ -85,6 +85,8 @@ pub mod process;
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
pub use cctk; pub use cctk;
pub mod surface_message;
pub mod theme; pub mod theme;
#[doc(inline)] #[doc(inline)]

81
src/surface_message.rs Normal file
View file

@ -0,0 +1,81 @@
use iced::Size;
use iced::Limits;
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 accepting the App as a parameter
AppPopup(
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
/// Create a popup
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::AppPopup(arg0, arg1) => {
f.debug_tuple("AppPopup").field(arg0).field(arg1).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(),
}
}
}

View file

@ -9,7 +9,7 @@ use iced_core::renderer;
use iced_core::widget::{tree, Id, Tree}; use iced_core::widget::{tree, Id, Tree};
use iced_core::{Clipboard, Element, Layout, Length, Rectangle, Shell, Vector, Widget}; use iced_core::{Clipboard, Element, Layout, Length, Rectangle, Shell, Vector, Widget};
use crate::app::message::SurfaceMessage; use crate::surface_message::SurfaceMessage;
pub(crate) fn responsive_container<'a, Message: 'static + From<SurfaceMessage>, Theme, E>( pub(crate) fn responsive_container<'a, Message: 'static + From<SurfaceMessage>, Theme, E>(
content: E, content: E,