feat(wayland): corner-radius protocol support

This commit is contained in:
Ashley Wulber 2025-09-24 15:55:34 -04:00 committed by Ashley Wulber
parent 43314e3e6a
commit 9815d4d981
7 changed files with 355 additions and 28 deletions

View file

@ -103,7 +103,7 @@ ashpd = { version = "0.12.0", default-features = false, optional = true }
async-fs = { version = "2.1", optional = true } async-fs = { version = "2.1", optional = true }
async-std = { version = "1.13", optional = true } async-std = { version = "1.13", optional = true }
auto_enums = "0.8.7" auto_enums = "0.8.7"
cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-client-toolkit", rev = "6254f50", optional = true } cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-client-toolkit", rev = "633beb0", optional = true }
chrono = "0.4.42" chrono = "0.4.42"
cosmic-config = { path = "cosmic-config" } cosmic-config = { path = "cosmic-config" }
cosmic-settings-config = { git = "https://github.com/pop-os/cosmic-settings-daemon", optional = true } cosmic-settings-config = { git = "https://github.com/pop-os/cosmic-settings-daemon", optional = true }
@ -229,6 +229,6 @@ libcosmic = { path = "./" }
# FIXME update winit deps where necessary to use this # FIXME update winit deps where necessary to use this
# [patch.crates-io] # [patch.crates-io]
# [patch."https://github.com/pop-os/winit.git"] [patch."https://github.com/pop-os/winit.git"]
# winit = { git = "https://github.com/rust-windowing/winit.git", rev = "241b7a80bba96c91fa3901729cd5dec66abb9be4" } winit = { git = "https://github.com/pop-os/winit.git//", branch = "xdg-toplevel" }
# winit = { path = "../../winit" } # winit = { path = "../winit" }

View file

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
libcosmic = { path = "../..", features = ["debug", "winit", "tokio", "single-instance", "multi-window", "dbus-config", "wgpu"] } libcosmic = { path = "../..", features = ["debug", "winit", "tokio", "single-instance", "multi-window", "dbus-config", "wgpu", "wayland"] }

2
iced

@ -1 +1 @@
Subproject commit d05087507a7a0e37e26f174cfc97629c960b4383 Subproject commit 788be2f7825b648ec3ce33697c6e675a7b7265ec

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::sync::Arc; use std::sync::Arc;
use super::{Action, Application, ApplicationExt, Subscription}; use super::{Action, Application, ApplicationExt, Subscription};
@ -92,6 +92,7 @@ pub struct Cosmic<App: Application> {
Box<dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>>>, Box<dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>>>,
), ),
>, >,
pub tracked_windows: HashSet<window::Id>,
} }
impl<T: Application> Cosmic<T> impl<T: Application> Cosmic<T>
@ -139,11 +140,11 @@ where
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
crate::surface::Action::AppSubsurface(settings, view) => { crate::surface::Action::AppSubsurface(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings) 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<
@ -168,11 +169,11 @@ where
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
crate::surface::Action::Subsurface(settings, view) => { crate::surface::Action::Subsurface(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings) 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<
@ -195,11 +196,11 @@ where
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
crate::surface::Action::AppPopup(settings, view) => { crate::surface::Action::AppPopup(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings) 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<
@ -229,6 +230,8 @@ where
crate::surface::Action::DestroySubsurface(id) => { crate::surface::Action::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id) iced_winit::commands::subsurface::destroy_subsurface(id)
} }
#[cfg(feature = "wayland")]
crate::surface::Action::DestroyWindow(id) => iced::window::close(id),
crate::surface::Action::ResponsiveMenuBar { crate::surface::Action::ResponsiveMenuBar {
menu_bar, menu_bar,
limits, limits,
@ -241,11 +244,11 @@ where
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
crate::surface::Action::Popup(settings, view) => { crate::surface::Action::Popup(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings) 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<
@ -265,6 +268,80 @@ where
iced_winit::commands::popup::get_popup(settings()) iced_winit::commands::popup::get_popup(settings())
} }
} }
#[cfg(feature = "wayland")]
crate::surface::Action::AppWindow(id, settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings).ok().and_then(|s| {
s.downcast::<Box<dyn Fn(&mut T) -> iced::window::Settings + Send + Sync>>()
.ok()
}) else {
tracing::error!("Invalid settings for AppWindow");
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, crate::Action<T::Message>>
+ Send
+ Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
tracing::error!("Invalid view for AppWindow: {err:?}");
None
}
}
}) {
let settings = settings(&mut self.app);
self.get_window(id, settings, *view)
} else {
let settings = settings(&mut self.app);
self.tracked_windows.insert(id);
iced_runtime::task::oneshot(|channel| {
iced_runtime::Action::Window(iced_runtime::window::Action::Open(
id, settings, channel,
))
})
.discard()
}
}
#[cfg(feature = "wayland")]
crate::surface::Action::Window(id, settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings).ok().and_then(|s| {
s.downcast::<Box<dyn Fn() -> iced::window::Settings + Send + Sync>>()
.ok()
}) else {
tracing::error!("Invalid settings for Window");
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, crate::Action<T::Message>> + Send + Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
tracing::error!("Invalid view for Window: {err:?}");
None
}
}
}) {
let settings = settings();
self.get_window(id, settings, Box::new(move |_| view()))
} else {
let settings = settings();
self.tracked_windows.insert(id);
iced_runtime::task::oneshot(|channel| {
iced_runtime::Action::Window(iced_runtime::window::Action::Open(
id, settings, channel,
))
})
.discard()
}
}
crate::surface::Action::Ignore => iced::Task::none(), crate::surface::Action::Ignore => iced::Task::none(),
crate::surface::Action::Task(f) => { crate::surface::Action::Task(f) => {
f().map(|sm| crate::Action::Cosmic(Action::Surface(sm))) f().map(|sm| crate::Action::Cosmic(Action::Surface(sm)))
@ -667,6 +744,42 @@ impl<T: Application> Cosmic<T> {
new_theme.theme_type.prefer_dark(prefer_dark); new_theme.theme_type.prefer_dark(prefer_dark);
cosmic_theme.set_theme(new_theme.theme_type); cosmic_theme.set_theme(new_theme.theme_type);
#[cfg(feature = "wayland")]
if self.app.core().sync_window_border_radii_to_theme() {
use iced_runtime::platform_specific::wayland::CornerRadius;
use iced_winit::platform_specific::commands::corner_radius::corner_radius;
let t = cosmic_theme.cosmic();
let radii = t.radius_s().map(|x| if x < 4.0 { x } else { x + 4.0 });
let cur_rad = CornerRadius {
top_left: radii[0].round() as u32,
top_right: radii[1].round() as u32,
bottom_left: radii[2].round() as u32,
bottom_right: radii[3].round() as u32,
};
// Update radius for the main window
let main_window_id = self
.app
.core()
.main_window_id()
.unwrap_or(window::Id::RESERVED);
let mut cmds =
vec![corner_radius(main_window_id, Some(cur_rad)).discard()];
// Update radius for each tracked view with the window surface type
for (id, (_, surface_type, _)) in self.surface_views.iter() {
if let SurfaceIdWrapper::Window(_) = surface_type {
cmds.push(corner_radius(*id, Some(cur_rad)).discard());
}
}
// Update radius for all tracked windows
for id in self.tracked_windows.iter() {
cmds.push(corner_radius(*id, Some(cur_rad)).discard());
}
return Task::batch(cmds);
}
} }
} }
@ -725,9 +838,46 @@ impl<T: Application> Cosmic<T> {
core.system_theme = new_theme.clone(); core.system_theme = new_theme.clone();
{ {
let mut cosmic_theme = THEME.lock().unwrap(); let mut cosmic_theme = THEME.lock().unwrap();
// Only apply update if the theme is set to load a system theme // Only apply update if the theme is set to load a system theme
if let ThemeType::System { theme: _, .. } = cosmic_theme.theme_type { if let ThemeType::System { .. } = cosmic_theme.theme_type {
cosmic_theme.set_theme(new_theme.theme_type); cosmic_theme.set_theme(new_theme.theme_type);
#[cfg(feature = "wayland")]
if self.app.core().sync_window_border_radii_to_theme() {
use iced_runtime::platform_specific::wayland::CornerRadius;
use iced_winit::platform_specific::commands::corner_radius::corner_radius;
let t = cosmic_theme.cosmic();
let radii = t.radius_s().map(|x| if x < 4.0 { x } else { x + 4.0 });
let cur_rad = CornerRadius {
top_left: radii[0].round() as u32,
top_right: radii[1].round() as u32,
bottom_left: radii[2].round() as u32,
bottom_right: radii[3].round() as u32,
};
// Update radius for the main window
let main_window_id = self
.app
.core()
.main_window_id()
.unwrap_or(window::Id::RESERVED);
let mut cmds =
vec![corner_radius(main_window_id, Some(cur_rad)).discard()];
// Update radius for each tracked view with the window surface type
for (id, (_, surface_type, _)) in self.surface_views.iter() {
if let SurfaceIdWrapper::Window(_) = surface_type {
cmds.push(corner_radius(*id, Some(cur_rad)).discard());
}
}
// Update radius for all tracked windows
for id in self.tracked_windows.iter() {
cmds.push(corner_radius(*id, Some(cur_rad)).discard());
}
return Task::batch(cmds);
}
} }
} }
} }
@ -748,6 +898,10 @@ impl<T: Application> Cosmic<T> {
Action::Surface(action) => return self.surface_update(action), Action::Surface(action) => return self.surface_update(action),
Action::SurfaceClosed(id) => { Action::SurfaceClosed(id) => {
#[cfg(feature = "wayland")]
self.surface_views.remove(&id);
self.tracked_windows.remove(&id);
let mut ret = if let Some(msg) = self.app.on_close_requested(id) { let mut ret = if let Some(msg) = self.app.on_close_requested(id) {
self.app.update(msg) self.app.update(msg)
} else { } else {
@ -910,6 +1064,26 @@ impl<T: Application> Cosmic<T> {
core.applet.suggested_bounds = b; core.applet.suggested_bounds = b;
} }
Action::Opened(id) => { Action::Opened(id) => {
self.tracked_windows.insert(id);
#[cfg(feature = "wayland")]
if self.app.core().sync_window_border_radii_to_theme() {
use iced_runtime::platform_specific::wayland::CornerRadius;
use iced_winit::platform_specific::commands::corner_radius::corner_radius;
let theme = THEME.lock().unwrap();
let t = theme.cosmic();
let radii = t.radius_s().map(|x| if x < 4.0 { x } else { x + 4.0 });
let cur_rad = CornerRadius {
top_left: radii[0].round() as u32,
top_right: radii[1].round() as u32,
bottom_left: radii[2].round() as u32,
bottom_right: radii[3].round() as u32,
};
return Task::batch(vec![
corner_radius(id, Some(cur_rad)).discard(),
iced_runtime::window::run_with_handle(id, init_windowing_system),
]);
}
return iced_runtime::window::run_with_handle(id, init_windowing_system); return iced_runtime::window::run_with_handle(id, init_windowing_system);
} }
_ => {} _ => {}
@ -925,6 +1099,7 @@ impl<App: Application> Cosmic<App> {
app, app,
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
surface_views: HashMap::new(), surface_views: HashMap::new(),
tracked_windows: HashSet::new(),
} }
} }
@ -971,4 +1146,30 @@ impl<App: Application> Cosmic<App> {
); );
get_popup(settings) get_popup(settings)
} }
#[cfg(feature = "wayland")]
/// Create a window surface
pub fn get_window(
&mut self,
id: iced::window::Id,
settings: iced::window::Settings,
view: Box<
dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>> + Send + Sync,
>,
) -> Task<crate::Action<App::Message>> {
use iced_winit::SurfaceIdWrapper;
self.surface_views.insert(
id.clone(),
(
None, // TODO parent for window, platform specific option maybe?
SurfaceIdWrapper::Window(id),
view,
),
);
iced_runtime::task::oneshot(|channel| {
iced_runtime::Action::Window(iced_runtime::window::Action::Open(id, settings, channel))
})
.discard()
}
} }

View file

@ -97,6 +97,9 @@ pub struct Core {
pub(crate) exit_on_main_window_closed: bool, pub(crate) exit_on_main_window_closed: bool,
pub(crate) menu_bars: HashMap<crate::widget::Id, (Limits, Size)>, pub(crate) menu_bars: HashMap<crate::widget::Id, (Limits, Size)>,
#[cfg(feature = "wayland")]
pub(crate) sync_window_border_radii_to_theme: bool,
} }
impl Default for Core { impl Default for Core {
@ -154,6 +157,8 @@ impl Default for Core {
main_window: None, main_window: None,
exit_on_main_window_closed: true, exit_on_main_window_closed: true,
menu_bars: HashMap::new(), menu_bars: HashMap::new(),
#[cfg(feature = "wayland")]
sync_window_border_radii_to_theme: true
} }
} }
} }
@ -476,4 +481,15 @@ impl Core {
crate::command::toggle_maximize(id) crate::command::toggle_maximize(id)
} }
// TODO should we emit tasks setting the corner radius or unsetting it if this is changed?
#[cfg(feature = "wayland")]
pub fn set_sync_window_border_radii_to_theme(&mut self, sync: bool) {
self.sync_window_border_radii_to_theme = sync;
}
#[cfg(feature = "wayland")]
pub fn sync_window_border_radii_to_theme(&self) -> bool {
self.sync_window_border_radii_to_theme
}
} }

View file

@ -5,6 +5,7 @@ use super::Action;
#[cfg(feature = "winit")] #[cfg(feature = "winit")]
use crate::Application; use crate::Application;
use iced::window;
use std::{any::Any, sync::Arc}; use std::{any::Any, sync::Arc};
/// Used to produce a destroy popup message from within a widget. /// Used to produce a destroy popup message from within a widget.
@ -20,6 +21,90 @@ pub fn destroy_subsurface(id: iced_core::window::Id) -> Action {
Action::DestroySubsurface(id) Action::DestroySubsurface(id)
} }
#[cfg(feature = "wayland")]
#[must_use]
pub fn destroy_window(id: iced_core::window::Id) -> Action {
Action::DestroyWindow(id)
}
#[cfg(all(feature = "wayland", feature = "winit"))]
#[must_use]
pub fn app_window<App: Application>(
settings: impl Fn(&mut App) -> window::Settings
+ Send
+ Sync
+ 'static,
view: Option<
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, crate::Action<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> (window::Id, Action) {
let id = window::Id::unique();
let boxed: Box<
dyn Fn(&mut App) -> window::Settings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
(
id,
Action::AppWindow(
id,
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
)
)
}
/// Used to create a window message from within a widget.
#[cfg(all(feature = "wayland", feature = "winit"))]
#[must_use]
pub fn simple_window<Message: 'static>(
settings: impl Fn() -> window::Settings
+ Send
+ Sync
+ 'static,
view: Option<
impl Fn() -> crate::Element<'static, crate::Action<Message>> + Send + Sync + 'static,
>,
) -> (window::Id, Action) {
let id = window::Id::unique();
let boxed: Box<
dyn Fn() -> window::Settings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
(
id,
Action::Window(
id,
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn() -> crate::Element<'static, crate::Action<Message>> + Send + Sync + 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
)
}
#[cfg(all(feature = "wayland", feature = "winit"))] #[cfg(all(feature = "wayland", feature = "winit"))]
#[must_use] #[must_use]
pub fn app_popup<App: Application>( pub fn app_popup<App: Application>(

View file

@ -36,6 +36,22 @@ pub enum Action {
), ),
/// Destroy a subsurface with a view function /// Destroy a subsurface with a view function
DestroyPopup(iced::window::Id), DestroyPopup(iced::window::Id),
/// Create a window with a view function accepting the App as a parameter
AppWindow(
iced::window::Id,
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
/// Create a window with a view function
Window(
iced::window::Id,
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
Option<std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>>,
),
/// Destroy a window
DestroyWindow(iced::window::Id),
/// Responsive menu bar update /// Responsive menu bar update
ResponsiveMenuBar { ResponsiveMenuBar {
/// Id of the menu bar /// Id of the menu bar
@ -80,6 +96,15 @@ impl std::fmt::Debug for Action {
.field("size", size) .field("size", size)
.finish(), .finish(),
Self::Ignore => write!(f, "Ignore"), Self::Ignore => write!(f, "Ignore"),
Self::AppWindow(id, arg0, arg1) => {
f.debug_tuple("AppWindow").field(id).field(arg0).field(arg1).finish()
}
Self::Window(id, arg0, arg1) => {
f.debug_tuple("Window").field(id).field(arg0).field(arg1).finish()
}
Self::DestroyWindow(arg0) => {
f.debug_tuple("DestroyWindow").field(arg0).finish()
}
Self::Task(_) => f.debug_tuple("Future").finish(), Self::Task(_) => f.debug_tuple("Future").finish(),
} }
} }