refactor: avoid adding new trait bounds

This commit is contained in:
Ashley Wulber 2025-03-03 00:14:13 -05:00
parent b53f6566bc
commit f26880180e
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
3 changed files with 116 additions and 131 deletions

View file

@ -94,10 +94,7 @@ pub struct Cosmic<App: Application> {
impl<T: Application> Cosmic<T> impl<T: Application> Cosmic<T>
where where
T::Message: Send T::Message: Send + 'static + Into<crate::surface_message::MessageWrapper<T::Message>>,
+ 'static
+ Into<crate::surface_message::MessageWrapper<T::Message>>
+ From<SurfaceMessage>,
{ {
pub fn init( pub fn init(
(mut core, flags): (Core, T::Flags), (mut core, flags): (Core, T::Flags),
@ -123,16 +120,12 @@ where
self.app.title(id).to_string() self.app.title(id).to_string()
} }
pub fn update( pub fn surface_update(
&mut self, &mut self,
message: super::Message<T::Message>, _surface_message: SurfaceMessage,
) -> iced::Task<super::Message<T::Message>> { ) -> iced::Task<super::Message<T::Message>> {
let message = match message {
super::Message::App(message) => match Into::<MessageWrapper<T::Message>>::into(message)
{
MessageWrapper::Surface(surface_message) => {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
match surface_message { match _surface_message {
SurfaceMessage::AppSubsurface(settings, view) => { SurfaceMessage::AppSubsurface(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings) let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok() .ok()
@ -146,13 +139,10 @@ where
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>> dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>>
+ Send + Send
+ Sync, + Sync,
>>( >>() {
) {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(err) => { Err(err) => {
tracing::error!( tracing::error!("Invalid view for subsurface view: {err:?}");
"Invalid view for subsurface view: {err:?}"
);
None None
} }
@ -161,9 +151,7 @@ where
let settings = settings(&mut self.app); let settings = settings(&mut self.app);
self.get_subsurface(settings, *view) self.get_subsurface(settings, *view)
} else { } else {
iced_winit::commands::subsurface::get_subsurface(settings( iced_winit::commands::subsurface::get_subsurface(settings(&mut self.app))
&mut self.app,
))
} }
} }
SurfaceMessage::Subsurface(settings, view) => { SurfaceMessage::Subsurface(settings, view) => {
@ -176,16 +164,11 @@ where
if let Some(view) = view.and_then(|view| { if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box< match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn() -> Element<'static, super::Message<T::Message>> dyn Fn() -> Element<'static, super::Message<T::Message>> + Send + Sync,
+ Send >>() {
+ Sync,
>>(
) {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(err) => { Err(err) => {
tracing::error!( tracing::error!("Invalid view for subsurface view: {err:?}");
"Invalid view for subsurface view: {err:?}"
);
None None
} }
@ -210,13 +193,10 @@ where
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>> dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>>
+ Send + Send
+ Sync, + Sync,
>>( >>() {
) {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(err) => { Err(err) => {
tracing::error!( tracing::error!("Invalid view for subsurface view: {err:?}");
"Invalid view for subsurface view: {err:?}"
);
None None
} }
} }
@ -229,9 +209,7 @@ where
} }
} }
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
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")]
SurfaceMessage::DestroySubsurface(id) => { SurfaceMessage::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id) iced_winit::commands::subsurface::destroy_subsurface(id)
@ -255,16 +233,11 @@ where
if let Some(view) = view.and_then(|view| { if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box< match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn() -> Element<'static, super::Message<T::Message>> dyn Fn() -> Element<'static, super::Message<T::Message>> + Send + Sync,
+ Send >>() {
+ Sync,
>>(
) {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(err) => { Err(err) => {
tracing::error!( tracing::error!("Invalid view for subsurface view: {err:?}");
"Invalid view for subsurface view: {err:?}"
);
None None
} }
} }
@ -277,11 +250,22 @@ where
} }
} }
SurfaceMessage::Ignore => iced::Task::none(), SurfaceMessage::Ignore => iced::Task::none(),
SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())), SurfaceMessage::Task(f) => f().map(|sm| super::Message::Surface(sm)),
} }
#[cfg(not(feature = "wayland"))] #[cfg(not(feature = "wayland"))]
iced::Task::none() iced::Task::none()
} }
pub fn update(
&mut self,
message: super::Message<T::Message>,
) -> iced::Task<super::Message<T::Message>> {
let message = match message {
super::Message::Surface(surface_message) => self.surface_update(surface_message),
super::Message::App(message) => match Into::<MessageWrapper<T::Message>>::into(message)
{
MessageWrapper::Surface(surface_message) => self.surface_update(surface_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

@ -29,6 +29,8 @@ pub mod message {
DbusActivation(super::DbusActivationMessage), DbusActivation(super::DbusActivationMessage),
/// Do nothing /// Do nothing
None, None,
/// Internal surface message
Surface(SurfaceMessage),
} }
pub const fn app<M>(message: M) -> Message<M> { pub const fn app<M>(message: M) -> Message<M> {
@ -319,7 +321,7 @@ pub(crate) fn iced_settings<App: Application>(
/// Returns error on application failure. /// 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 where
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>, App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
{ {
#[cfg(target_env = "gnu")] #[cfg(target_env = "gnu")]
if let Some(threshold) = settings.default_mmap_threshold { if let Some(threshold) = settings.default_mmap_threshold {
@ -512,7 +514,6 @@ where
+ std::fmt::Debug + std::fmt::Debug
+ Send + Send
+ Into<crate::surface_message::MessageWrapper<App::Message>> + Into<crate::surface_message::MessageWrapper<App::Message>>
+ From<SurfaceMessage>
+ 'static, + 'static,
{ {
let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok(); let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok();

View file

@ -441,7 +441,7 @@ impl Context {
/// Returns error on application failure. /// Returns error on application failure.
pub fn run<App: Application>(flags: App::Flags) -> iced::Result pub fn run<App: Application>(flags: App::Flags) -> iced::Result
where where
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>, App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
{ {
let helper = Context::default(); let helper = Context::default();