From 8398944886a8e2db3a8ce535694b20230be98930 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 3 Mar 2025 00:14:13 -0500 Subject: [PATCH] refactor: avoid adding new trait bounds --- src/app/cosmic.rs | 240 ++++++++++++++++++++++------------------------ src/app/mod.rs | 5 +- src/applet/mod.rs | 2 +- 3 files changed, 116 insertions(+), 131 deletions(-) diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index 756909eb..10ab6fc1 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -94,10 +94,7 @@ pub struct Cosmic { impl Cosmic where - T::Message: Send - + 'static - + Into> - + From, + T::Message: Send + 'static + Into>, { pub fn init( (mut core, flags): (Core, T::Flags), @@ -123,165 +120,152 @@ where self.app.title(id).to_string() } - pub fn update( + pub fn surface_update( &mut self, - message: super::Message, + _surface_message: SurfaceMessage, ) -> iced::Task> { - let message = match message { - super::Message::App(message) => match Into::>::into(message) - { - MessageWrapper::Surface(surface_message) => { - #[cfg(feature = "wayland")] - match surface_message { - SurfaceMessage::AppSubsurface(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + #[cfg(feature = "wayland")] + match _surface_message { + SurfaceMessage::AppSubsurface(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:?}" - ); + 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, - )) - } + None } - SurfaceMessage::Subsurface(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + } + }) { + let settings = settings(&mut self.app); + self.get_subsurface(settings, *view) + } else { + iced_winit::commands::subsurface::get_subsurface(settings(&mut self.app)) + } + } + 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:: Element<'static, super::Message> - + Send - + Sync, - >>( - ) { - Ok(v) => Some(v), - Err(err) => { - tracing::error!( - "Invalid view for subsurface view: {err:?}" - ); + 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_subsurface(settings, Box::new(move |_| view())) - } else { - iced_winit::commands::subsurface::get_subsurface(settings()) + None } } - SurfaceMessage::AppPopup(settings, view) => { - let Some(settings) = std::sync::Arc::try_unwrap(settings) + }) { + let settings = settings(); + self.get_subsurface(settings, Box::new(move |_| view())) + } else { + iced_winit::commands::subsurface::get_subsurface(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 - } - } - }) { - let settings = settings(&mut self.app); + 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)) - } - } - #[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) + 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) .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 - } - } - }) { - let settings = settings(); - - self.get_popup(settings, Box::new(move |_| view())) - } else { - iced_winit::commands::popup::get_popup(settings()) + 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 } } - SurfaceMessage::Ignore => iced::Task::none(), - SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())), - } - #[cfg(not(feature = "wayland"))] - iced::Task::none() + }) { + let settings = settings(); + + self.get_popup(settings, Box::new(move |_| view())) + } else { + iced_winit::commands::popup::get_popup(settings()) } + } + SurfaceMessage::Ignore => iced::Task::none(), + SurfaceMessage::Task(f) => f().map(|sm| super::Message::Surface(sm)), + } + + #[cfg(not(feature = "wayland"))] + iced::Task::none() + } + + pub fn update( + &mut self, + message: super::Message, + ) -> iced::Task> { + let message = match message { + super::Message::Surface(surface_message) => self.surface_update(surface_message), + super::Message::App(message) => match Into::>::into(message) + { + MessageWrapper::Surface(surface_message) => self.surface_update(surface_message), MessageWrapper::Message(message) => self.app.update(message), }, super::Message::Cosmic(message) => self.cosmic_update(message), diff --git a/src/app/mod.rs b/src/app/mod.rs index ad62ae66..126a21ad 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -29,6 +29,8 @@ pub mod message { DbusActivation(super::DbusActivationMessage), /// Do nothing None, + /// Internal surface message + Surface(SurfaceMessage), } pub const fn app(message: M) -> Message { @@ -319,7 +321,7 @@ pub(crate) fn iced_settings( /// Returns error on application failure. pub fn run(settings: Settings, flags: App::Flags) -> iced::Result where - App::Message: Into> + From, + App::Message: Into>, { #[cfg(target_env = "gnu")] if let Some(threshold) = settings.default_mmap_threshold { @@ -512,7 +514,6 @@ where + std::fmt::Debug + Send + Into> - + From + 'static, { let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok(); diff --git a/src/applet/mod.rs b/src/applet/mod.rs index 27bd42fd..f1d22819 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -441,7 +441,7 @@ impl Context { /// Returns error on application failure. pub fn run(flags: App::Flags) -> iced::Result where - App::Message: Into> + From, + App::Message: Into>, { let helper = Context::default();