refactor: avoid adding new trait bounds

This commit is contained in:
Ashley Wulber 2025-03-03 00:14:13 -05:00
parent f3c72c4692
commit 8398944886
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>
where
T::Message: Send
+ 'static
+ Into<crate::surface_message::MessageWrapper<T::Message>>
+ From<SurfaceMessage>,
T::Message: Send + 'static + Into<crate::surface_message::MessageWrapper<T::Message>>,
{
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<T::Message>,
_surface_message: SurfaceMessage,
) -> 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")]
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::<Box<dyn Fn(&mut T) -> 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::<Box<
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::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::<Box<
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::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::<Box<dyn Fn() -> 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::<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:?}"
);
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_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::<Box<dyn Fn(&mut T) -> 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 for<'a> Fn(&'a T) -> Element<'a, super::Message<T::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::<Box<
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::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::<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())
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
}
}
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<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),
},
super::Message::Cosmic(message) => self.cosmic_update(message),

View file

@ -29,6 +29,8 @@ pub mod message {
DbusActivation(super::DbusActivationMessage),
/// Do nothing
None,
/// Internal surface message
Surface(SurfaceMessage),
}
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.
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
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")]
if let Some(threshold) = settings.default_mmap_threshold {
@ -512,7 +514,6 @@ where
+ std::fmt::Debug
+ Send
+ Into<crate::surface_message::MessageWrapper<App::Message>>
+ From<SurfaceMessage>
+ 'static,
{
let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok();

View file

@ -441,7 +441,7 @@ impl Context {
/// Returns error on application failure.
pub fn run<App: Application>(flags: App::Flags) -> iced::Result
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();