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,165 +120,152 @@ 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 { #[cfg(feature = "wayland")]
super::Message::App(message) => match Into::<MessageWrapper<T::Message>>::into(message) match _surface_message {
{ SurfaceMessage::AppSubsurface(settings, view) => {
MessageWrapper::Surface(surface_message) => { 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() .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 {
tracing::error!("Invalid settings for subsurface"); tracing::error!("Invalid settings for subsurface");
return Task::none(); return Task::none();
}; };
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 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!("Invalid view for subsurface view: {err:?}");
tracing::error!(
"Invalid view for subsurface view: {err:?}"
);
None None
}
}
}) {
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) }) {
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() .ok()
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else { .and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
tracing::error!("Invalid settings for subsurface"); tracing::error!("Invalid settings for subsurface");
return Task::none(); return Task::none();
}; };
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),
>>( Err(err) => {
) { tracing::error!("Invalid view for subsurface view: {err:?}");
Ok(v) => Some(v),
Err(err) => {
tracing::error!(
"Invalid view for subsurface view: {err:?}"
);
None None
}
}
}) {
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) 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() .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 {
tracing::error!("Invalid settings for popup"); tracing::error!("Invalid settings for popup");
return Task::none(); return Task::none();
}; };
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 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!("Invalid view for subsurface view: {err:?}");
tracing::error!( None
"Invalid view for subsurface view: {err:?}" }
); }
None }) {
} let settings = settings(&mut self.app);
}
}) {
let settings = settings(&mut self.app);
self.get_popup(settings, *view) self.get_popup(settings, *view)
} else { } else {
iced_winit::commands::popup::get_popup(settings(&mut self.app)) iced_winit::commands::popup::get_popup(settings(&mut self.app))
} }
} }
#[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")]
} SurfaceMessage::DestroySubsurface(id) => {
#[cfg(feature = "wayland")] iced_winit::commands::subsurface::destroy_subsurface(id)
SurfaceMessage::DestroySubsurface(id) => { }
iced_winit::commands::subsurface::destroy_subsurface(id) SurfaceMessage::ResponsiveMenuBar {
} menu_bar,
SurfaceMessage::ResponsiveMenuBar { limits,
menu_bar, size,
limits, } => {
size, let core = self.app.core_mut();
} => { core.menu_bars.insert(menu_bar, (limits, size));
let core = self.app.core_mut(); iced::Task::none()
core.menu_bars.insert(menu_bar, (limits, size)); }
iced::Task::none() SurfaceMessage::Popup(settings, view) => {
} let Some(settings) = std::sync::Arc::try_unwrap(settings)
SurfaceMessage::Popup(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok() .ok()
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else { .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"); tracing::error!("Invalid settings for popup");
return Task::none(); return Task::none();
}; };
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),
>>( Err(err) => {
) { tracing::error!("Invalid view for subsurface view: {err:?}");
Ok(v) => Some(v), None
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())
} }
} }
SurfaceMessage::Ignore => iced::Task::none(), }) {
SurfaceMessage::Task(f) => f().map(|sm| super::Message::App(sm.into())), let settings = settings();
}
#[cfg(not(feature = "wayland"))] self.get_popup(settings, Box::new(move |_| view()))
iced::Task::none() } 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<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();