From 63f249a395e135ce7a71facb0cfdb12f8e6b11dc Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 24 Feb 2025 14:14:17 -0500 Subject: [PATCH] refactor: remove SurfaceMessageHandler --- examples/applet/src/window.rs | 12 +- examples/application/src/main.rs | 14 +-- src/app/core.rs | 4 +- src/app/cosmic.rs | 195 +++++++++++++++---------------- src/app/mod.rs | 48 ++++---- src/applet/mod.rs | 3 +- src/surface_message.rs | 11 -- 7 files changed, 131 insertions(+), 156 deletions(-) diff --git a/examples/applet/src/window.rs b/examples/applet/src/window.rs index 273ef626..234b38ca 100644 --- a/examples/applet/src/window.rs +++ b/examples/applet/src/window.rs @@ -5,7 +5,7 @@ use cosmic::iced::{self, Length, Limits, Task}; use cosmic::iced_runtime::core::window; use cosmic::iced_runtime::platform_specific::wayland::popup::SctkPopupSettings; use cosmic::iced_runtime::platform_specific::wayland::subsurface; -use cosmic::surface_message::{SurfaceMessage, SurfaceMessageHandler}; +use cosmic::surface_message::{MessageWrapper, SurfaceMessage}; use cosmic::widget::dropdown::DropdownView; use cosmic::widget::{autosize, dropdown, layer_container, list_column, settings, toggler}; use cosmic::{iced_core, Element}; @@ -63,11 +63,11 @@ impl std::fmt::Debug for Message { } } -impl SurfaceMessageHandler for Message { - fn to_surface_message(self) -> cosmic::surface_message::MessageWrapper { - match self { - Message::Surface(msg) => cosmic::surface_message::MessageWrapper::Surface(msg), - msg => cosmic::surface_message::MessageWrapper::Message(msg), +impl From for MessageWrapper { + fn from(value: Message) -> Self { + match value { + Message::Surface(s) => MessageWrapper::Surface(s), + m => MessageWrapper::Message(m), } } } diff --git a/examples/application/src/main.rs b/examples/application/src/main.rs index 4a6252f2..fdd14ce2 100644 --- a/examples/application/src/main.rs +++ b/examples/application/src/main.rs @@ -11,7 +11,7 @@ use cosmic::iced::alignment::{Horizontal, Vertical}; use cosmic::iced::widget::column; use cosmic::iced::Length; use cosmic::iced_core::Size; -use cosmic::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler}; +use cosmic::surface_message::{MessageWrapper, SurfaceMessage}; use cosmic::widget::icon::{from_name, Handle}; use cosmic::widget::menu::KeyBind; use cosmic::widget::{button, text}; @@ -89,17 +89,15 @@ pub enum Message { Hi, } -#[cfg(feature = "wayland")] -impl SurfaceMessageHandler for Message { - fn to_surface_message(self) -> MessageWrapper { - match self { - Message::Surface(msg) => MessageWrapper::Surface(msg), - msg => MessageWrapper::Message(msg), +impl From for MessageWrapper { + fn from(value: Message) -> Self { + match value { + Message::Surface(s) => MessageWrapper::Surface(s), + m => MessageWrapper::Message(m), } } } -#[cfg(feature = "wayland")] impl From for Message { fn from(value: SurfaceMessage) -> Self { Message::Surface(value) diff --git a/src/app/core.rs b/src/app/core.rs index a076373f..f684cd5c 100644 --- a/src/app/core.rs +++ b/src/app/core.rs @@ -19,7 +19,7 @@ use iced_core::{window::Id, Element}; use palette::Srgba; use slotmap::Key; -use crate::{surface_message::SurfaceMessageHandler, Theme}; +use crate::Theme; use super::{Renderer, Task}; @@ -396,7 +396,7 @@ impl Core { 'a, Message: Clone + From - + crate::surface_message::SurfaceMessageHandler + + Into> + 'static, A: menu::Action, >( diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index f2465934..c10d7b65 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -7,7 +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::surface_message::{MessageWrapper, SurfaceMessage}; use crate::theme::{Theme, ThemeType, THEME}; use crate::widget::nav_bar; use crate::{keyboard_nav, Element}; @@ -94,7 +94,7 @@ pub struct Cosmic { impl Cosmic where - T::Message: Send + 'static + crate::surface_message::SurfaceMessageHandler, + T::Message: Send + 'static + Into>, { pub fn init( (mut core, flags): (Core, T::Flags), @@ -125,135 +125,128 @@ where message: super::Message, ) -> iced::Task> { let message = match message { - super::Message::App(message) => { - match SurfaceMessageHandler::to_surface_message(message) { - MessageWrapper::Surface(surface_message) => { - #[cfg(feature = "wayland")] - match surface_message { - SurfaceMessage::Subsurface(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + super::Message::App(message) => match Into::>::into(message) + { + MessageWrapper::Surface(surface_message) => { + #[cfg(feature = "wayland")] + match surface_message { + SurfaceMessage::Subsurface(settings, view) => { + let Some(settings) = std::sync::Arc::try_unwrap(settings) .ok() .and_then(|s| s.downcast:: 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:: Fn( - &'a T, - ) - -> Element<'a, super::Message> - + Send - + Sync, - >>( - ) { - Ok(v) => Some(v), - Err(err) => { - tracing::error!( - "Invalid view for subsurface view: {err:?}" - ); - None - } + if let Some(view) = view.and_then(|view| { + match std::sync::Arc::try_unwrap(view).ok()?.downcast:: Fn(&'a T) -> Element<'a, super::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, - )) } + }) { + let settings = settings(&mut self.app); + self.get_subsurface(settings, *view) + } else { + iced_winit::commands::subsurface::get_subsurface(settings( + &mut self.app, + )) } - SurfaceMessage::AppPopup(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + } + SurfaceMessage::AppPopup(settings, view) => { + let Some(settings) = std::sync::Arc::try_unwrap(settings) .ok() .and_then(|s| s.downcast:: 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:: Fn( - &'a T, - ) - -> Element<'a, super::Message> - + Send - + Sync, - >>( - ) { - Ok(v) => Some(v), - Err(err) => { - tracing::error!( - "Invalid view for subsurface view: {err:?}" - ); - None - } + if let Some(view) = view.and_then(|view| { + match std::sync::Arc::try_unwrap(view).ok()?.downcast:: Fn(&'a T) -> Element<'a, super::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)) } + }) { + 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")] - SurfaceMessage::DestroyPopup(id) => { - iced_winit::commands::popup::destroy_popup(id) - } - #[cfg(feature = "wayland")] - SurfaceMessage::DestroySubsurface(id) => { - iced_winit::commands::subsurface::destroy_subsurface(id) - } - SurfaceMessage::ResponsiveMenuBar { - menu_bar, - limits, - size, - } => { - let core = self.app.core_mut(); - core.menu_bars.insert(menu_bar, (limits, size)); - iced::Task::none() - } - SurfaceMessage::Popup(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + } + #[cfg(feature = "wayland")] + SurfaceMessage::DestroyPopup(id) => { + iced_winit::commands::popup::destroy_popup(id) + } + #[cfg(feature = "wayland")] + SurfaceMessage::DestroySubsurface(id) => { + iced_winit::commands::subsurface::destroy_subsurface(id) + } + SurfaceMessage::ResponsiveMenuBar { + menu_bar, + limits, + size, + } => { + let core = self.app.core_mut(); + 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:: 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:: Element<'static, super::Message> - + Send - + Sync, - >>( - ) { - Ok(v) => Some(v), - Err(err) => { - tracing::error!( - "Invalid view for subsurface view: {err:?}" - ); - None - } + if let Some(view) = view.and_then(|view| { + match std::sync::Arc::try_unwrap(view).ok()?.downcast:: Element<'static, super::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()) } + }) { + 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() } - MessageWrapper::Message(message) => self.app.update(message), + #[cfg(not(feature = "wayland"))] + iced::Task::none() } - } + 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")] diff --git a/src/app/mod.rs b/src/app/mod.rs index aa86eb3e..cbdcd60a 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -6,7 +6,7 @@ //! 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}; +use crate::surface_message::{MessageWrapper, SurfaceMessage}; pub mod command; pub mod context_drawer; @@ -17,7 +17,7 @@ pub(crate) mod multi_window; pub mod settings; pub mod message { - use crate::surface_message::{MessageWrapper, SurfaceMessage, SurfaceMessageHandler}; + use crate::surface_message::{MessageWrapper, SurfaceMessage}; use iced::{Limits, Size}; @@ -50,7 +50,8 @@ pub mod message { #[cfg(feature = "wayland")] pub fn destroy_popup(id: iced_core::window::Id) -> App::Message where - App::Message: SurfaceMessageHandler + From, + App::Message: + Into> + From, { let surface_msg = SurfaceMessage::DestroyPopup(id); App::Message::from(surface_msg) @@ -60,7 +61,8 @@ pub mod message { #[cfg(feature = "wayland")] pub fn destroy_popup_simple(id: iced_core::window::Id) -> Message where - Message: SurfaceMessageHandler + From + 'static, + Message: + Into> + From + 'static, { Message::from(SurfaceMessage::DestroyPopup(id)) } @@ -68,7 +70,8 @@ pub mod message { #[cfg(feature = "wayland")] pub fn destroy_subsurface(id: iced_core::window::Id) -> App::Message where - App::Message: SurfaceMessageHandler + From, + App::Message: + Into> + From, { let surface_msg = SurfaceMessage::DestroySubsurface(id); App::Message::from(surface_msg) @@ -90,11 +93,12 @@ pub mod message { >, ) -> App::Message where - App::Message: SurfaceMessageHandler + From, + App::Message: + Into> + From, { use std::{any::Any, sync::Arc}; - use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler}; + use crate::surface_message::SurfaceMessage; let boxed: Box< dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send @@ -124,12 +128,13 @@ pub mod message { >, ) -> Message where - Message: SurfaceMessageHandler + From + 'static, + Message: + Into> + From + 'static, V:, { use std::{any::Any, sync::Arc}; - use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler}; + use crate::surface_message::SurfaceMessage; let boxed: Box< dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send @@ -153,20 +158,6 @@ pub mod message { )) } - // TODO could this somehow be used by widgets? It is the wrong message type though. - // #[cfg(feature = "wayland")] - // pub fn get_simple_popup( - // settings: iced_runtime::platform_specific::wayland::popup::SctkPopupSettings, - // view: Option< - // impl Fn() -> crate::Element<'static, super::Message> + Send + Sync + 'static, - // >, - // ) -> App::Message - // where - // App::Message: SurfaceMessageHandler + From, - // { - // todo!() - // } - #[cfg(feature = "wayland")] pub fn subsurface( settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static, @@ -181,7 +172,8 @@ pub mod message { >, ) -> App::Message where - App::Message: SurfaceMessageHandler + From, + App::Message: + Into> + From, { use crate::surface_message::SurfaceMessage; use std::{any::Any, sync::Arc}; @@ -303,7 +295,7 @@ pub(crate) fn iced_settings( /// Returns error on application failure. pub fn run(settings: Settings, flags: App::Flags) -> iced::Result where - App::Message: SurfaceMessageHandler, + App::Message: Into>, { #[cfg(target_env = "gnu")] if let Some(threshold) = settings.default_mmap_threshold { @@ -492,7 +484,11 @@ impl DbusActivation { pub fn run_single_instance(settings: Settings, flags: App::Flags) -> iced::Result where App::Flags: CosmicFlags, - App::Message: Clone + std::fmt::Debug + Send + SurfaceMessageHandler + 'static, + App::Message: Clone + + std::fmt::Debug + + Send + + Into> + + 'static, { let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok(); diff --git a/src/applet/mod.rs b/src/applet/mod.rs index 9b2aa130..ca5f697d 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -11,7 +11,6 @@ use crate::{ window, Color, Length, Limits, Rectangle, }, iced_widget, - surface_message::SurfaceMessageHandler, theme::{self, system_dark, system_light, Button, THEME}, widget::{ self, @@ -380,7 +379,7 @@ impl Context { /// Returns error on application failure. pub fn run(flags: App::Flags) -> iced::Result where - App::Message: SurfaceMessageHandler, + App::Message: Into>, { let helper = Context::default(); diff --git a/src/surface_message.rs b/src/surface_message.rs index 68e57004..da34bf20 100644 --- a/src/surface_message.rs +++ b/src/surface_message.rs @@ -7,17 +7,6 @@ pub enum MessageWrapper { Message(M), } -pub trait SurfaceMessageHandler: Sized { - fn to_surface_message(self) -> MessageWrapper; -} - -#[cfg(not(feature = "wayland"))] -impl SurfaceMessageHandler for M { - fn to_surface_message(self) -> MessageWrapper { - MessageWrapper::Message(self) - } -} - /// Ignore this message in your application. It will be intercepted. #[derive(Clone)] pub enum SurfaceMessage {