wayland: Support cosmic-corner-radius v2
This commit is contained in:
parent
55d57ddba2
commit
5ee2fa911a
7 changed files with 679 additions and 147 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -906,7 +906,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-protocols"
|
name = "cosmic-protocols"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
source = "git+https://github.com/pop-os//cosmic-protocols?branch=main#0d0b0b573fe4836ede4c237dadd365033cd28296"
|
source = "git+https://github.com/pop-os//cosmic-protocols?branch=main#32283d76a8d0342da74c4cc022a533c52dcf378f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.11.0",
|
"bitflags 2.11.0",
|
||||||
"wayland-backend",
|
"wayland-backend",
|
||||||
|
|
|
||||||
|
|
@ -212,6 +212,7 @@ pub fn draw_surface_cursor<R>(
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
[0; 4],
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
Kind::Cursor,
|
Kind::Cursor,
|
||||||
&mut |elem| push(elem.into(), h),
|
&mut |elem| push(elem.into(), h),
|
||||||
|
|
@ -247,6 +248,7 @@ pub fn draw_dnd_icon<R>(
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
[0; 4],
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
FRAME_TIME_FILTER,
|
FRAME_TIME_FILTER,
|
||||||
push,
|
push,
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ use crate::{
|
||||||
wayland::{
|
wayland::{
|
||||||
handlers::{
|
handlers::{
|
||||||
compositor::FRAME_TIME_FILTER,
|
compositor::FRAME_TIME_FILTER,
|
||||||
|
corner_radius::{pad_rect, surface_corners, surface_padding},
|
||||||
data_device::get_dnd_icon,
|
data_device::get_dnd_icon,
|
||||||
image_copy_capture::{FrameHolder, SessionData, render_session},
|
image_copy_capture::{FrameHolder, SessionData, render_session},
|
||||||
},
|
},
|
||||||
|
|
@ -78,7 +79,7 @@ use smithay::{
|
||||||
utils::{
|
utils::{
|
||||||
IsAlive, Logical, Monotonic, Physical, Point, Rectangle, Scale, Size, Time, Transform,
|
IsAlive, Logical, Monotonic, Physical, Point, Rectangle, Scale, Size, Time, Transform,
|
||||||
},
|
},
|
||||||
wayland::{dmabuf::get_dmabuf, session_lock::LockSurface},
|
wayland::{compositor::with_states, dmabuf::get_dmabuf, session_lock::LockSurface},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
|
|
@ -838,6 +839,11 @@ where
|
||||||
let mut geometry = popup.geometry().as_global();
|
let mut geometry = popup.geometry().as_global();
|
||||||
geometry.loc += location;
|
geometry.loc += location;
|
||||||
|
|
||||||
|
let radii = with_states(popup.wl_surface(), |states| {
|
||||||
|
surface_corners(states, geometry.size.as_logical())
|
||||||
|
})
|
||||||
|
.unwrap_or([0; 4]);
|
||||||
|
|
||||||
push_render_elements_from_surface_tree(
|
push_render_elements_from_surface_tree(
|
||||||
renderer,
|
renderer,
|
||||||
popup.wl_surface(),
|
popup.wl_surface(),
|
||||||
|
|
@ -849,7 +855,8 @@ where
|
||||||
Scale::from(scale),
|
Scale::from(scale),
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
radii,
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
FRAME_TIME_FILTER,
|
FRAME_TIME_FILTER,
|
||||||
&mut |elem| {
|
&mut |elem| {
|
||||||
|
|
@ -868,6 +875,17 @@ where
|
||||||
} => {
|
} => {
|
||||||
let mut geometry = layer.geometry().as_global();
|
let mut geometry = layer.geometry().as_global();
|
||||||
geometry.loc += location;
|
geometry.loc += location;
|
||||||
|
let geometry = geometry.to_local(output).as_logical();
|
||||||
|
|
||||||
|
let padded = with_states(layer.wl_surface(), |states| {
|
||||||
|
surface_padding(states, geometry.size)
|
||||||
|
.and_then(|padding| pad_rect(geometry, &padding))
|
||||||
|
})
|
||||||
|
.unwrap_or(geometry);
|
||||||
|
let radii = with_states(layer.wl_surface(), |states| {
|
||||||
|
surface_corners(states, padded.size)
|
||||||
|
})
|
||||||
|
.unwrap_or([0; 4]);
|
||||||
|
|
||||||
push_render_elements_from_surface_tree(
|
push_render_elements_from_surface_tree(
|
||||||
renderer,
|
renderer,
|
||||||
|
|
@ -876,11 +894,12 @@ where
|
||||||
.to_local(output)
|
.to_local(output)
|
||||||
.as_logical()
|
.as_logical()
|
||||||
.to_physical_precise_round(scale),
|
.to_physical_precise_round(scale),
|
||||||
geometry.to_local(output).as_logical().to_f64(),
|
geometry.to_f64(),
|
||||||
Scale::from(scale),
|
Scale::from(scale),
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
radii,
|
||||||
|
padded.to_f64(),
|
||||||
blur_strength,
|
blur_strength,
|
||||||
FRAME_TIME_FILTER,
|
FRAME_TIME_FILTER,
|
||||||
&mut |elem| {
|
&mut |elem| {
|
||||||
|
|
@ -909,6 +928,7 @@ where
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
[0; 4],
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
FRAME_TIME_FILTER,
|
FRAME_TIME_FILTER,
|
||||||
&mut |elem| elements.extend(crop_to_output(elem.into()).map(Into::into)),
|
&mut |elem| elements.extend(crop_to_output(elem.into()).map(Into::into)),
|
||||||
|
|
@ -1056,6 +1076,7 @@ fn session_lock_elements<R>(
|
||||||
1.0,
|
1.0,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
[0; 4],
|
||||||
|
None,
|
||||||
0,
|
0,
|
||||||
FRAME_TIME_FILTER,
|
FRAME_TIME_FILTER,
|
||||||
push,
|
push,
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ pub fn push_render_elements_from_surface_tree<R>(
|
||||||
alpha: f32,
|
alpha: f32,
|
||||||
should_clip: bool,
|
should_clip: bool,
|
||||||
radii: [u8; 4],
|
radii: [u8; 4],
|
||||||
|
blur_geometry: impl Into<Option<Rectangle<f64, Logical>>>,
|
||||||
blur_strength: usize,
|
blur_strength: usize,
|
||||||
kind: impl Into<KindEvaluation>,
|
kind: impl Into<KindEvaluation>,
|
||||||
push_above: &mut dyn FnMut(SurfaceRenderElement<R>),
|
push_above: &mut dyn FnMut(SurfaceRenderElement<R>),
|
||||||
|
|
@ -44,7 +45,8 @@ pub fn push_render_elements_from_surface_tree<R>(
|
||||||
R::TextureId: Clone + 'static,
|
R::TextureId: Clone + 'static,
|
||||||
{
|
{
|
||||||
let location = location.into().to_f64();
|
let location = location.into().to_f64();
|
||||||
let geometry = geometry.into().to_f64();
|
let geometry = geometry.into();
|
||||||
|
let blur_geometry = blur_geometry.into();
|
||||||
let scale = scale.into();
|
let scale = scale.into();
|
||||||
let kind = kind.into();
|
let kind = kind.into();
|
||||||
let mut passed_main = false;
|
let mut passed_main = false;
|
||||||
|
|
@ -87,10 +89,11 @@ pub fn push_render_elements_from_surface_tree<R>(
|
||||||
renderer, surface, states, location, alpha, kind,
|
renderer, surface, states, location, alpha, kind,
|
||||||
) {
|
) {
|
||||||
Ok(Some(surface)) => {
|
Ok(Some(surface)) => {
|
||||||
|
let blur_geo = blur_geometry.unwrap_or(geometry);
|
||||||
blur = BlurElement::from_surface(
|
blur = BlurElement::from_surface(
|
||||||
renderer,
|
renderer,
|
||||||
states,
|
states,
|
||||||
geometry,
|
blur_geo,
|
||||||
scale.x,
|
scale.x,
|
||||||
radii,
|
radii,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,7 @@ use crate::{
|
||||||
wayland::{SurfaceRenderElement, push_render_elements_from_surface_tree},
|
wayland::{SurfaceRenderElement, push_render_elements_from_surface_tree},
|
||||||
},
|
},
|
||||||
shell::focus::target::PointerFocusTarget,
|
shell::focus::target::PointerFocusTarget,
|
||||||
wayland::{
|
wayland::handlers::{compositor::frame_time_filter_fn, corner_radius::surface_corners},
|
||||||
handlers::compositor::frame_time_filter_fn, protocols::corner_radius::CacheableCorners,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
|
|
@ -193,22 +191,7 @@ impl CosmicSurface {
|
||||||
|
|
||||||
pub fn corner_radius(&self, geometry_size: Size<i32, Logical>) -> Option<[u8; 4]> {
|
pub fn corner_radius(&self, geometry_size: Size<i32, Logical>) -> Option<[u8; 4]> {
|
||||||
self.wl_surface().and_then(|surface| {
|
self.wl_surface().and_then(|surface| {
|
||||||
with_states(&surface, |states| {
|
with_states(&surface, |states| surface_corners(states, geometry_size))
|
||||||
let mut guard = states.cached_state.get::<CacheableCorners>();
|
|
||||||
|
|
||||||
// guard against corner radius being too large, potentially disconnecting the outline
|
|
||||||
let half_min_dim =
|
|
||||||
u8::try_from(geometry_size.w.min(geometry_size.h) / 2).unwrap_or(u8::MAX);
|
|
||||||
|
|
||||||
let corners = guard.current().0?;
|
|
||||||
|
|
||||||
Some([
|
|
||||||
corners.top_left.min(half_min_dim),
|
|
||||||
corners.top_right.min(half_min_dim),
|
|
||||||
corners.bottom_right.min(half_min_dim),
|
|
||||||
corners.bottom_left.min(half_min_dim),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -880,6 +863,10 @@ impl CosmicSurface {
|
||||||
.to_physical_precise_round(scale);
|
.to_physical_precise_round(scale);
|
||||||
let mut geometry = popup.geometry().to_f64();
|
let mut geometry = popup.geometry().to_f64();
|
||||||
geometry.loc += location.to_f64().to_logical(scale) + popup_offset.to_f64();
|
geometry.loc += location.to_f64().to_logical(scale) + popup_offset.to_f64();
|
||||||
|
let radii = with_states(popup.wl_surface(), |states| {
|
||||||
|
surface_corners(states, geometry.size.to_i32_round())
|
||||||
|
})
|
||||||
|
.unwrap_or([0; 4]);
|
||||||
|
|
||||||
push_render_elements_from_surface_tree(
|
push_render_elements_from_surface_tree(
|
||||||
renderer,
|
renderer,
|
||||||
|
|
@ -889,7 +876,8 @@ impl CosmicSurface {
|
||||||
scale,
|
scale,
|
||||||
alpha,
|
alpha,
|
||||||
false,
|
false,
|
||||||
[0; 4],
|
radii,
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
scanout_kind_eval(None, scanout_node),
|
scanout_kind_eval(None, scanout_node),
|
||||||
push,
|
push,
|
||||||
|
|
@ -934,6 +922,7 @@ impl CosmicSurface {
|
||||||
alpha,
|
alpha,
|
||||||
should_clip,
|
should_clip,
|
||||||
radii,
|
radii,
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
scanout_kind_eval(scanout_override, scanout_node),
|
scanout_kind_eval(scanout_override, scanout_node),
|
||||||
push_above,
|
push_above,
|
||||||
|
|
@ -954,6 +943,7 @@ impl CosmicSurface {
|
||||||
alpha,
|
alpha,
|
||||||
should_clip,
|
should_clip,
|
||||||
radii,
|
radii,
|
||||||
|
None,
|
||||||
blur_strength,
|
blur_strength,
|
||||||
scanout_kind_eval(scanout_override, scanout_node),
|
scanout_kind_eval(scanout_override, scanout_node),
|
||||||
push_above,
|
push_above,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1;
|
use smithay::utils::{Logical, Rectangle, Size};
|
||||||
|
use smithay::wayland::compositor::SurfaceData;
|
||||||
|
|
||||||
use crate::wayland::protocols::corner_radius::{
|
use crate::wayland::protocols::corner_radius::{
|
||||||
CornerRadiusData, CornerRadiusHandler, CornerRadiusState, delegate_corner_radius,
|
CacheableCorners, CacheablePadding, CornerRadiusData, CornerRadiusHandler, CornerRadiusState,
|
||||||
|
CornerRadiusSurface, delegate_corner_radius,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
|
@ -11,21 +13,25 @@ impl CornerRadiusHandler for State {
|
||||||
&mut self.common.corner_radius_state
|
&mut self.common.corner_radius_state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_corner_radius(
|
fn set_corner_radius(&mut self, data: &CornerRadiusData) {
|
||||||
&mut self,
|
|
||||||
_: &cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
|
||||||
data: &CornerRadiusData,
|
|
||||||
) {
|
|
||||||
if force_redraw(self, data).is_none() {
|
if force_redraw(self, data).is_none() {
|
||||||
tracing::warn!("Failed to force redraw for corner radius change.");
|
tracing::warn!("Failed to force redraw for corner radius change.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unset_corner_radius(
|
fn unset_corner_radius(&mut self, data: &CornerRadiusData) {
|
||||||
&mut self,
|
if force_redraw(self, data).is_none() {
|
||||||
_: &cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
tracing::warn!("Failed to force redraw for corner radius reset.");
|
||||||
data: &CornerRadiusData,
|
}
|
||||||
) {
|
}
|
||||||
|
|
||||||
|
fn set_padding(&mut self, data: &CornerRadiusData) {
|
||||||
|
if force_redraw(self, data).is_none() {
|
||||||
|
tracing::warn!("Failed to force redraw for corner radius change.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unset_padding(&mut self, data: &CornerRadiusData) {
|
||||||
if force_redraw(self, data).is_none() {
|
if force_redraw(self, data).is_none() {
|
||||||
tracing::warn!("Failed to force redraw for corner radius reset.");
|
tracing::warn!("Failed to force redraw for corner radius reset.");
|
||||||
}
|
}
|
||||||
|
|
@ -34,16 +40,83 @@ impl CornerRadiusHandler for State {
|
||||||
|
|
||||||
fn force_redraw(state: &mut State, data: &CornerRadiusData) -> Option<()> {
|
fn force_redraw(state: &mut State, data: &CornerRadiusData) -> Option<()> {
|
||||||
let guard = data.lock().unwrap();
|
let guard = data.lock().unwrap();
|
||||||
|
let shell = state.common.shell.read();
|
||||||
|
|
||||||
let toplevel = guard.toplevel.upgrade().ok()?;
|
let output = match &guard.surface {
|
||||||
|
CornerRadiusSurface::Toplevel(toplevel) => {
|
||||||
let surface = state.common.xdg_shell_state.get_toplevel(&toplevel)?;
|
let toplevel = toplevel.upgrade().ok()?;
|
||||||
|
let surface = state.common.xdg_shell_state.get_toplevel(&toplevel)?;
|
||||||
let guard = state.common.shell.read();
|
shell.visible_output_for_surface(surface.wl_surface())?
|
||||||
let output = guard.visible_output_for_surface(surface.wl_surface())?;
|
}
|
||||||
|
CornerRadiusSurface::Popup(popup) => {
|
||||||
|
let popup = popup.upgrade().ok()?;
|
||||||
|
let surface = state.common.xdg_shell_state.get_popup(&popup)?;
|
||||||
|
shell.visible_output_for_surface(surface.wl_surface())?
|
||||||
|
}
|
||||||
|
CornerRadiusSurface::Layer(layer) => {
|
||||||
|
let layer = layer.upgrade().ok()?;
|
||||||
|
let surface = state
|
||||||
|
.common
|
||||||
|
.layer_shell_state
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|l| l.shell_surface() == &layer)?;
|
||||||
|
shell.visible_output_for_surface(surface.wl_surface())?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
state.backend.schedule_render(output);
|
state.backend.schedule_render(output);
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn surface_corners(states: &SurfaceData, size: Size<i32, Logical>) -> Option<[u8; 4]> {
|
||||||
|
let mut guard = states.cached_state.get::<CacheableCorners>();
|
||||||
|
|
||||||
|
// guard against corner radius being too large, potentially disconnecting the outline
|
||||||
|
let half_min_dim = u8::try_from(size.w.min(size.h) / 2).unwrap_or(u8::MAX);
|
||||||
|
let corners = guard.current().0?;
|
||||||
|
|
||||||
|
Some([
|
||||||
|
u8::try_from(corners.top_left)
|
||||||
|
.unwrap_or(u8::MAX)
|
||||||
|
.min(half_min_dim),
|
||||||
|
u8::try_from(corners.top_right)
|
||||||
|
.unwrap_or(u8::MAX)
|
||||||
|
.min(half_min_dim),
|
||||||
|
u8::try_from(corners.bottom_right)
|
||||||
|
.unwrap_or(u8::MAX)
|
||||||
|
.min(half_min_dim),
|
||||||
|
u8::try_from(corners.bottom_left)
|
||||||
|
.unwrap_or(u8::MAX)
|
||||||
|
.min(half_min_dim),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn surface_padding(states: &SurfaceData, size: Size<i32, Logical>) -> Option<[i32; 4]> {
|
||||||
|
let mut guard = states.cached_state.get::<CacheablePadding>();
|
||||||
|
|
||||||
|
// guard against padding being too large
|
||||||
|
let half_min_dim = size.w.min(size.h) / 2;
|
||||||
|
let padding = guard.current().0?;
|
||||||
|
|
||||||
|
Some([
|
||||||
|
padding.top.min(half_min_dim),
|
||||||
|
padding.right.min(half_min_dim),
|
||||||
|
padding.bottom.min(half_min_dim),
|
||||||
|
padding.left.min(half_min_dim),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pad_rect(
|
||||||
|
mut rect: Rectangle<i32, Logical>,
|
||||||
|
padding: &[i32; 4],
|
||||||
|
) -> Option<Rectangle<i32, Logical>> {
|
||||||
|
rect.size.h = rect.size.h.checked_sub(padding[0])?;
|
||||||
|
rect.loc.x += padding[0];
|
||||||
|
rect.size.w = rect.size.w.checked_sub(padding[1])?;
|
||||||
|
rect.size.h = rect.size.h.checked_sub(padding[2])?;
|
||||||
|
rect.size.w = rect.size.w.checked_sub(padding[3])?;
|
||||||
|
rect.loc.y += padding[3];
|
||||||
|
Some(rect)
|
||||||
|
}
|
||||||
|
|
||||||
delegate_corner_radius!(State);
|
delegate_corner_radius!(State);
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,21 @@
|
||||||
|
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_layer_v1::{
|
||||||
|
self, CosmicCornerRadiusLayerV1,
|
||||||
|
};
|
||||||
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1;
|
use cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1;
|
||||||
use cosmic_protocols::corner_radius::v1::server::{
|
use cosmic_protocols::corner_radius::v1::server::{
|
||||||
cosmic_corner_radius_manager_v1, cosmic_corner_radius_toplevel_v1,
|
cosmic_corner_radius_manager_v1, cosmic_corner_radius_toplevel_v1,
|
||||||
};
|
};
|
||||||
use smithay::utils::HookId;
|
use smithay::desktop::utils::bbox_from_surface_tree;
|
||||||
|
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_popup::XdgPopup;
|
||||||
|
use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::server::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1;
|
||||||
|
use smithay::reexports::wayland_server::New;
|
||||||
|
use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
|
||||||
|
use smithay::utils::{HookId, Logical, Point, Rectangle};
|
||||||
use smithay::wayland::compositor::Cacheable;
|
use smithay::wayland::compositor::Cacheable;
|
||||||
use smithay::wayland::compositor::add_pre_commit_hook;
|
use smithay::wayland::compositor::add_pre_commit_hook;
|
||||||
use smithay::wayland::compositor::with_states;
|
use smithay::wayland::compositor::with_states;
|
||||||
use smithay::wayland::shell::xdg::SurfaceCachedState;
|
use smithay::wayland::shell::wlr_layer::WlrLayerShellHandler;
|
||||||
|
use smithay::wayland::shell::xdg::{SurfaceCachedState, XdgShellSurfaceUserData};
|
||||||
use smithay::{
|
use smithay::{
|
||||||
reexports::{
|
reexports::{
|
||||||
wayland_protocols::xdg::shell::server::xdg_toplevel::XdgToplevel,
|
wayland_protocols::xdg::shell::server::xdg_toplevel::XdgToplevel,
|
||||||
|
|
@ -18,6 +27,7 @@ use std::sync::Mutex;
|
||||||
use wayland_backend::server::GlobalId;
|
use wayland_backend::server::GlobalId;
|
||||||
|
|
||||||
type ToplevelHookId = Mutex<Option<(HookId, Weak<CosmicCornerRadiusToplevelV1>)>>;
|
type ToplevelHookId = Mutex<Option<(HookId, Weak<CosmicCornerRadiusToplevelV1>)>>;
|
||||||
|
type LayerHookId = Mutex<Option<(HookId, Weak<CosmicCornerRadiusLayerV1>)>>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CornerRadiusState {
|
pub struct CornerRadiusState {
|
||||||
|
|
@ -33,12 +43,13 @@ impl CornerRadiusState {
|
||||||
+ Dispatch<
|
+ Dispatch<
|
||||||
cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
||||||
CornerRadiusData,
|
CornerRadiusData,
|
||||||
> + CornerRadiusHandler
|
> + Dispatch<cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1, CornerRadiusData>
|
||||||
|
+ CornerRadiusHandler
|
||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
let global = dh
|
let global = dh
|
||||||
.create_global::<D, cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, _>(
|
.create_global::<D, cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, _>(
|
||||||
1,
|
2,
|
||||||
(),
|
(),
|
||||||
);
|
);
|
||||||
CornerRadiusState {
|
CornerRadiusState {
|
||||||
|
|
@ -52,18 +63,12 @@ impl CornerRadiusState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CornerRadiusHandler: XdgShellHandler {
|
pub trait CornerRadiusHandler: XdgShellHandler + WlrLayerShellHandler {
|
||||||
fn corner_radius_state(&mut self) -> &mut CornerRadiusState;
|
fn corner_radius_state(&mut self) -> &mut CornerRadiusState;
|
||||||
fn set_corner_radius(
|
fn set_corner_radius(&mut self, data: &CornerRadiusData);
|
||||||
&mut self,
|
fn unset_corner_radius(&mut self, data: &CornerRadiusData);
|
||||||
toplevel: &cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
fn set_padding(&mut self, data: &CornerRadiusData);
|
||||||
data: &CornerRadiusData,
|
fn unset_padding(&mut self, data: &CornerRadiusData);
|
||||||
);
|
|
||||||
fn unset_corner_radius(
|
|
||||||
&mut self,
|
|
||||||
toplevel: &cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1,
|
|
||||||
data: &CornerRadiusData,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, (), D>
|
impl<D> GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, (), D>
|
||||||
|
|
@ -72,6 +77,7 @@ where
|
||||||
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
+ Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData>
|
+ Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData>
|
||||||
|
+ Dispatch<cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1, CornerRadiusData>
|
||||||
+ CornerRadiusHandler
|
+ CornerRadiusHandler
|
||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
|
|
@ -96,6 +102,7 @@ where
|
||||||
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
+ Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData>
|
+ Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData>
|
||||||
|
+ Dispatch<cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1, CornerRadiusData>
|
||||||
+ CornerRadiusHandler
|
+ CornerRadiusHandler
|
||||||
+ 'static,
|
+ 'static,
|
||||||
{
|
{
|
||||||
|
|
@ -115,6 +122,71 @@ where
|
||||||
}
|
}
|
||||||
cosmic_corner_radius_manager_v1::Request::GetCornerRadius { id, toplevel } => {
|
cosmic_corner_radius_manager_v1::Request::GetCornerRadius { id, toplevel } => {
|
||||||
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
||||||
|
new_xdg(
|
||||||
|
surface.wl_surface(),
|
||||||
|
CornerRadiusSurface::Toplevel(surface.xdg_toplevel().downgrade()),
|
||||||
|
resource,
|
||||||
|
data_init,
|
||||||
|
id,
|
||||||
|
)
|
||||||
|
} // TODO: can this fail?
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_manager_v1::Request::GetCornerRadiusSurface { id, surface } => {
|
||||||
|
if let Some(toplevel) =
|
||||||
|
state
|
||||||
|
.xdg_shell_state()
|
||||||
|
.toplevel_surfaces()
|
||||||
|
.iter()
|
||||||
|
.find(|toplevel| {
|
||||||
|
toplevel
|
||||||
|
.xdg_toplevel()
|
||||||
|
.data::<XdgShellSurfaceUserData>()
|
||||||
|
.unwrap()
|
||||||
|
.xdg_surface()
|
||||||
|
== &surface
|
||||||
|
})
|
||||||
|
{
|
||||||
|
new_xdg(
|
||||||
|
toplevel.wl_surface(),
|
||||||
|
CornerRadiusSurface::Toplevel(toplevel.xdg_toplevel().downgrade()),
|
||||||
|
resource,
|
||||||
|
data_init,
|
||||||
|
id,
|
||||||
|
)
|
||||||
|
} else if let Some(popup) =
|
||||||
|
state
|
||||||
|
.xdg_shell_state()
|
||||||
|
.popup_surfaces()
|
||||||
|
.iter()
|
||||||
|
.find(|popup| {
|
||||||
|
popup
|
||||||
|
.xdg_popup()
|
||||||
|
.data::<XdgShellSurfaceUserData>()
|
||||||
|
.unwrap()
|
||||||
|
.xdg_surface()
|
||||||
|
== &surface
|
||||||
|
})
|
||||||
|
{
|
||||||
|
new_xdg(
|
||||||
|
popup.wl_surface(),
|
||||||
|
CornerRadiusSurface::Popup(popup.xdg_popup().downgrade()),
|
||||||
|
resource,
|
||||||
|
data_init,
|
||||||
|
id,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_manager_v1::Error::NoRole as u32,
|
||||||
|
"xdg_surface has no known role object".to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_manager_v1::Request::GetCornerRadiusLayer { id, layer } => {
|
||||||
|
if let Some(surface) = state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|surface| surface.shell_surface() == &layer)
|
||||||
|
{
|
||||||
let radius_exists = with_states(surface.wl_surface(), |surface_data| {
|
let radius_exists = with_states(surface.wl_surface(), |surface_data| {
|
||||||
let hook_id = surface_data
|
let hook_id = surface_data
|
||||||
.data_map
|
.data_map
|
||||||
|
|
@ -124,63 +196,33 @@ where
|
||||||
});
|
});
|
||||||
if radius_exists.unwrap_or_default() {
|
if radius_exists.unwrap_or_default() {
|
||||||
resource.post_error(
|
resource.post_error(
|
||||||
cosmic_corner_radius_manager_v1::Error::CornerRadiusExists as u32,
|
cosmic_corner_radius_manager_v1::Error::CornerRadiusExists as u32,
|
||||||
format!("{resource:?} CosmicCornerRadiusToplevelV1 object already exists for the surface"),
|
format!(
|
||||||
);
|
"{resource:?} CosmicCornerRadiusToplevelV1 object already exists for the surface"
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let data = Mutex::new(CornerRadiusInternal {
|
let data = Mutex::new(CornerRadiusInternal {
|
||||||
toplevel: toplevel.downgrade(),
|
surface: CornerRadiusSurface::Layer(layer.downgrade()),
|
||||||
corners: None,
|
corners: None,
|
||||||
|
padding: None,
|
||||||
});
|
});
|
||||||
let obj = data_init.init(id, data);
|
let obj = data_init.init(id, data);
|
||||||
let obj_downgrade = obj.downgrade();
|
let obj_downgrade = obj.downgrade();
|
||||||
|
|
||||||
let needs_hook = radius_exists.is_none();
|
let needs_hook = radius_exists.is_none();
|
||||||
if needs_hook {
|
if needs_hook {
|
||||||
let hook_id = add_pre_commit_hook::<D, _>(
|
let hook_id =
|
||||||
surface.wl_surface(),
|
add_pre_commit_hook::<D, _>(surface.wl_surface(), layer_radius_hook);
|
||||||
move |_, _dh, surface| {
|
|
||||||
let corner_radii_too_big = with_states(surface, |surface_data| {
|
|
||||||
let corners = *surface_data
|
|
||||||
.cached_state
|
|
||||||
.get::<CacheableCorners>()
|
|
||||||
.pending();
|
|
||||||
surface_data
|
|
||||||
.cached_state
|
|
||||||
.get::<SurfaceCachedState>()
|
|
||||||
.pending()
|
|
||||||
.geometry
|
|
||||||
.zip(corners.0.as_ref())
|
|
||||||
.is_some_and(|(geo, corners)| {
|
|
||||||
let half_min_dim =
|
|
||||||
u8::try_from(geo.size.w.min(geo.size.h) / 2)
|
|
||||||
.unwrap_or(u8::MAX);
|
|
||||||
corners.top_right > half_min_dim
|
|
||||||
|| corners.top_left > half_min_dim
|
|
||||||
|| corners.bottom_right > half_min_dim
|
|
||||||
|| corners.bottom_left > half_min_dim
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if corner_radii_too_big {
|
|
||||||
obj.post_error(
|
|
||||||
cosmic_corner_radius_toplevel_v1::Error::RadiusTooLarge
|
|
||||||
as u32,
|
|
||||||
format!("{obj:?} corner radius too large"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
with_states(surface.wl_surface(), |surface_data| {
|
with_states(surface.wl_surface(), |surface_data| {
|
||||||
let hook_ids = surface_data
|
let hook_ids = surface_data
|
||||||
.data_map
|
.data_map
|
||||||
.get_or_insert_threadsafe(|| ToplevelHookId::new(None));
|
.get_or_insert_threadsafe(|| LayerHookId::new(None));
|
||||||
let mut guard = hook_ids.lock().unwrap();
|
let mut guard = hook_ids.lock().unwrap();
|
||||||
*guard = Some((hook_id, obj_downgrade));
|
*guard = Some((hook_id, obj_downgrade));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
} // TODO: can this fail?
|
||||||
}
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
|
|
@ -197,6 +239,55 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_xdg<D>(
|
||||||
|
wl_surface: &WlSurface,
|
||||||
|
surface: CornerRadiusSurface,
|
||||||
|
resource: &cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1,
|
||||||
|
data_init: &mut smithay::reexports::wayland_server::DataInit<'_, D>,
|
||||||
|
id: New<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1>,
|
||||||
|
) where
|
||||||
|
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
|
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
|
+ Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData>
|
||||||
|
+ CornerRadiusHandler
|
||||||
|
+ 'static,
|
||||||
|
{
|
||||||
|
let radius_exists = with_states(wl_surface, |surface_data| {
|
||||||
|
let hook_id = surface_data
|
||||||
|
.data_map
|
||||||
|
.get_or_insert_threadsafe(|| ToplevelHookId::new(None));
|
||||||
|
let guard = hook_id.lock().unwrap();
|
||||||
|
guard.as_ref().map(|(_, t)| t.upgrade().is_ok())
|
||||||
|
});
|
||||||
|
if radius_exists.unwrap_or_default() {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_manager_v1::Error::CornerRadiusExists as u32,
|
||||||
|
format!(
|
||||||
|
"{resource:?} CosmicCornerRadiusToplevelV1 object already exists for the surface"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let data = Mutex::new(CornerRadiusInternal {
|
||||||
|
surface,
|
||||||
|
corners: None,
|
||||||
|
padding: None,
|
||||||
|
});
|
||||||
|
let obj = data_init.init(id, data);
|
||||||
|
let obj_downgrade = obj.downgrade();
|
||||||
|
|
||||||
|
let needs_hook = radius_exists.is_none();
|
||||||
|
if needs_hook {
|
||||||
|
let hook_id = add_pre_commit_hook::<D, _>(wl_surface, xdg_radius_hook);
|
||||||
|
with_states(wl_surface, |surface_data| {
|
||||||
|
let hook_ids = surface_data
|
||||||
|
.data_map
|
||||||
|
.get_or_insert_threadsafe(|| ToplevelHookId::new(None));
|
||||||
|
let mut guard = hook_ids.lock().unwrap();
|
||||||
|
*guard = Some((hook_id, obj_downgrade));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<D>
|
impl<D>
|
||||||
Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData, D>
|
Dispatch<cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1, CornerRadiusData, D>
|
||||||
for CornerRadiusState
|
for CornerRadiusState
|
||||||
|
|
@ -221,30 +312,35 @@ where
|
||||||
let mut guard = data.lock().unwrap();
|
let mut guard = data.lock().unwrap();
|
||||||
guard.corners = None;
|
guard.corners = None;
|
||||||
|
|
||||||
let Ok(toplevel) = guard.toplevel.upgrade() else {
|
let Some(wl_surface) = (match &guard.surface {
|
||||||
|
CornerRadiusSurface::Toplevel(toplevel) => toplevel
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|toplevel| state.xdg_shell_state().get_toplevel(&toplevel))
|
||||||
|
.map(|toplevel| toplevel.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Popup(popup) => popup
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|popup| state.xdg_shell_state().get_popup(&popup))
|
||||||
|
.map(|popup| popup.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Layer(_) => unreachable!(),
|
||||||
|
}) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
with_states(&wl_surface, |surface_data| {
|
||||||
with_states(surface.wl_surface(), |surface_data| {
|
if let Some(hook_ids_mutex) = surface_data.data_map.get::<ToplevelHookId>() {
|
||||||
if let Some(hook_ids_mutex) = surface_data.data_map.get::<ToplevelHookId>()
|
let mut hook_id = hook_ids_mutex.lock().unwrap();
|
||||||
{
|
*hook_id = None;
|
||||||
let mut hook_id = hook_ids_mutex.lock().unwrap();
|
}
|
||||||
*hook_id = None;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
let mut cached = surface_data.cached_state.get::<CacheableCorners>();
|
||||||
with_states(surface.wl_surface(), |s| {
|
let pending = cached.pending();
|
||||||
let mut cached = s.cached_state.get::<CacheableCorners>();
|
*pending = CacheableCorners(None);
|
||||||
let pending = cached.pending();
|
});
|
||||||
*pending = CacheableCorners(None);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
state.unset_corner_radius(resource, data);
|
state.unset_corner_radius(data);
|
||||||
}
|
}
|
||||||
cosmic_corner_radius_toplevel_v1::Request::SetRadius {
|
cosmic_corner_radius_toplevel_v1::Request::SetRadius {
|
||||||
top_left,
|
top_left,
|
||||||
|
|
@ -254,66 +350,388 @@ where
|
||||||
} => {
|
} => {
|
||||||
let mut guard = data.lock().unwrap();
|
let mut guard = data.lock().unwrap();
|
||||||
guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left);
|
guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left);
|
||||||
let Ok(toplevel) = guard.toplevel.upgrade() else {
|
|
||||||
|
let Some(wl_surface) = (match &guard.surface {
|
||||||
|
CornerRadiusSurface::Toplevel(toplevel) => toplevel
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|toplevel| state.xdg_shell_state().get_toplevel(&toplevel))
|
||||||
|
.map(|toplevel| toplevel.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Popup(popup) => popup
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|popup| state.xdg_shell_state().get_popup(&popup))
|
||||||
|
.map(|popup| popup.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Layer(_) => unreachable!(),
|
||||||
|
}) else {
|
||||||
resource.post_error(
|
resource.post_error(
|
||||||
cosmic_corner_radius_toplevel_v1::Error::ToplevelDestroyed as u32,
|
cosmic_corner_radius_toplevel_v1::Error::ToplevelDestroyed as u32,
|
||||||
format!("{:?} No toplevel found", resource),
|
format!("{:?} No toplevel found", resource),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
with_states(&wl_surface, |s| {
|
||||||
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
let mut cached = s.cached_state.get::<CacheableCorners>();
|
||||||
with_states(surface.wl_surface(), |s| {
|
let pending = cached.pending();
|
||||||
let mut cached = s.cached_state.get::<CacheableCorners>();
|
*pending = CacheableCorners(guard.corners);
|
||||||
let pending = cached.pending();
|
});
|
||||||
*pending = CacheableCorners(guard.corners);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
state.set_corner_radius(resource, data);
|
state.set_corner_radius(data);
|
||||||
}
|
}
|
||||||
cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => {
|
cosmic_corner_radius_toplevel_v1::Request::UnsetRadius => {
|
||||||
let mut guard = data.lock().unwrap();
|
let mut guard = data.lock().unwrap();
|
||||||
guard.corners = None;
|
guard.corners = None;
|
||||||
let Ok(toplevel) = guard.toplevel.upgrade() else {
|
|
||||||
|
let Some(wl_surface) = (match &guard.surface {
|
||||||
|
CornerRadiusSurface::Toplevel(toplevel) => toplevel
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|toplevel| state.xdg_shell_state().get_toplevel(&toplevel))
|
||||||
|
.map(|toplevel| toplevel.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Popup(popup) => popup
|
||||||
|
.upgrade()
|
||||||
|
.ok()
|
||||||
|
.and_then(|popup| state.xdg_shell_state().get_popup(&popup))
|
||||||
|
.map(|popup| popup.wl_surface().clone()),
|
||||||
|
CornerRadiusSurface::Layer(_) => unreachable!(),
|
||||||
|
}) else {
|
||||||
resource.post_error(
|
resource.post_error(
|
||||||
cosmic_corner_radius_toplevel_v1::Error::ToplevelDestroyed as u32,
|
cosmic_corner_radius_toplevel_v1::Error::ToplevelDestroyed as u32,
|
||||||
format!("{:?} No toplevel found", resource),
|
format!("{:?} No toplevel found", resource),
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(surface) = state.xdg_shell_state().get_toplevel(&toplevel) {
|
with_states(&wl_surface, |s| {
|
||||||
with_states(surface.wl_surface(), |s| {
|
let mut cached = s.cached_state.get::<CacheableCorners>();
|
||||||
let mut cached = s.cached_state.get::<CacheableCorners>();
|
let pending = cached.pending();
|
||||||
let pending = cached.pending();
|
*pending = CacheableCorners(None);
|
||||||
*pending = CacheableCorners(None);
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
state.unset_corner_radius(resource, data);
|
state.unset_corner_radius(data);
|
||||||
}
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<D> Dispatch<cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1, CornerRadiusData, D>
|
||||||
|
for CornerRadiusState
|
||||||
|
where
|
||||||
|
D: GlobalDispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
|
+ Dispatch<cosmic_corner_radius_manager_v1::CosmicCornerRadiusManagerV1, ()>
|
||||||
|
+ Dispatch<cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1, CornerRadiusData>
|
||||||
|
+ CornerRadiusHandler
|
||||||
|
+ 'static,
|
||||||
|
{
|
||||||
|
fn request(
|
||||||
|
state: &mut D,
|
||||||
|
_client: &Client,
|
||||||
|
resource: &cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1,
|
||||||
|
request: <cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1 as Resource>::Request,
|
||||||
|
data: &CornerRadiusData,
|
||||||
|
_dhandle: &DisplayHandle,
|
||||||
|
_data_init: &mut smithay::reexports::wayland_server::DataInit<'_, D>,
|
||||||
|
) {
|
||||||
|
match request {
|
||||||
|
cosmic_corner_radius_layer_v1::Request::Destroy => {
|
||||||
|
let mut guard = data.lock().unwrap();
|
||||||
|
guard.corners = None;
|
||||||
|
|
||||||
|
let CornerRadiusSurface::Layer(layer_surface) = &guard.surface else {
|
||||||
|
unreachable!("corner_radius_layer without layer shell?");
|
||||||
|
};
|
||||||
|
let Some(layer_surface) = layer_surface.upgrade().ok().and_then(|layer| {
|
||||||
|
state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|s| s.shell_surface() == &layer)
|
||||||
|
}) else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::LayerDestroyed as u32,
|
||||||
|
format!("{:?} No layer found", resource),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
with_states(layer_surface.wl_surface(), |surface_data| {
|
||||||
|
if let Some(hook_ids_mutex) = surface_data.data_map.get::<LayerHookId>() {
|
||||||
|
let mut hook_id = hook_ids_mutex.lock().unwrap();
|
||||||
|
*hook_id = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut cached = surface_data.cached_state.get::<CacheableCorners>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheableCorners(None);
|
||||||
|
|
||||||
|
let mut cached = surface_data.cached_state.get::<CacheablePadding>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheablePadding(None);
|
||||||
|
});
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
state.unset_corner_radius(data);
|
||||||
|
state.unset_padding(data);
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_layer_v1::Request::SetRadius {
|
||||||
|
top_left,
|
||||||
|
top_right,
|
||||||
|
bottom_right,
|
||||||
|
bottom_left,
|
||||||
|
} => {
|
||||||
|
let mut guard = data.lock().unwrap();
|
||||||
|
guard.set_corner_radius(top_left, top_right, bottom_right, bottom_left);
|
||||||
|
|
||||||
|
let CornerRadiusSurface::Layer(layer_surface) = &guard.surface else {
|
||||||
|
unreachable!("corner_radius_layer without layer shell?");
|
||||||
|
};
|
||||||
|
let Some(layer_surface) = layer_surface.upgrade().ok().and_then(|layer| {
|
||||||
|
state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|s| s.shell_surface() == &layer)
|
||||||
|
}) else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::LayerDestroyed as u32,
|
||||||
|
format!("{:?} No layer found", resource),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
with_states(layer_surface.wl_surface(), |s| {
|
||||||
|
let mut cached = s.cached_state.get::<CacheableCorners>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheableCorners(guard.corners);
|
||||||
|
});
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
state.set_corner_radius(data);
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_layer_v1::Request::UnsetRadius => {
|
||||||
|
let mut guard = data.lock().unwrap();
|
||||||
|
guard.corners = None;
|
||||||
|
|
||||||
|
let CornerRadiusSurface::Layer(layer_surface) = &guard.surface else {
|
||||||
|
unreachable!("corner_radius_layer without layer shell?");
|
||||||
|
};
|
||||||
|
let Some(layer_surface) = layer_surface.upgrade().ok().and_then(|layer| {
|
||||||
|
state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|s| s.shell_surface() == &layer)
|
||||||
|
}) else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::LayerDestroyed as u32,
|
||||||
|
format!("{:?} No layer found", resource),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
with_states(layer_surface.wl_surface(), |s| {
|
||||||
|
let mut cached = s.cached_state.get::<CacheableCorners>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheableCorners(None);
|
||||||
|
});
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
state.unset_corner_radius(data);
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_layer_v1::Request::SetPadding {
|
||||||
|
top,
|
||||||
|
right,
|
||||||
|
bottom,
|
||||||
|
left,
|
||||||
|
} => {
|
||||||
|
let mut guard = data.lock().unwrap();
|
||||||
|
guard.set_padding(top, right, bottom, left);
|
||||||
|
|
||||||
|
let CornerRadiusSurface::Layer(layer_surface) = &guard.surface else {
|
||||||
|
unreachable!("corner_radius_layer without layer shell?");
|
||||||
|
};
|
||||||
|
let Some(layer_surface) = layer_surface.upgrade().ok().and_then(|layer| {
|
||||||
|
state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|s| s.shell_surface() == &layer)
|
||||||
|
}) else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::LayerDestroyed as u32,
|
||||||
|
format!("{:?} No layer found", resource),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
with_states(layer_surface.wl_surface(), |s| {
|
||||||
|
let mut cached = s.cached_state.get::<CacheablePadding>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheablePadding(guard.padding);
|
||||||
|
});
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
state.set_padding(data);
|
||||||
|
}
|
||||||
|
cosmic_corner_radius_layer_v1::Request::UnsetPadding => {
|
||||||
|
let mut guard = data.lock().unwrap();
|
||||||
|
guard.corners = None;
|
||||||
|
|
||||||
|
let CornerRadiusSurface::Layer(layer_surface) = &guard.surface else {
|
||||||
|
unreachable!("corner_radius_layer without layer shell?");
|
||||||
|
};
|
||||||
|
let Some(layer_surface) = layer_surface.upgrade().ok().and_then(|layer| {
|
||||||
|
state
|
||||||
|
.shell_state()
|
||||||
|
.layer_surfaces()
|
||||||
|
.find(|s| s.shell_surface() == &layer)
|
||||||
|
}) else {
|
||||||
|
resource.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::LayerDestroyed as u32,
|
||||||
|
format!("{:?} No layer found", resource),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
with_states(layer_surface.wl_surface(), |s| {
|
||||||
|
let mut cached = s.cached_state.get::<CacheablePadding>();
|
||||||
|
let pending = cached.pending();
|
||||||
|
*pending = CacheablePadding(None);
|
||||||
|
});
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
state.unset_padding(data);
|
||||||
|
}
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn xdg_radius_hook<D: 'static>(_state: &mut D, _dh: &DisplayHandle, surface: &WlSurface) {
|
||||||
|
with_states(surface, |surface_data| {
|
||||||
|
let corners = *surface_data
|
||||||
|
.cached_state
|
||||||
|
.get::<CacheableCorners>()
|
||||||
|
.pending();
|
||||||
|
if surface_data
|
||||||
|
.cached_state
|
||||||
|
.get::<SurfaceCachedState>()
|
||||||
|
.pending()
|
||||||
|
.geometry
|
||||||
|
.zip(corners.0.as_ref())
|
||||||
|
.is_some_and(|(geo, corners)| {
|
||||||
|
let half_min_dim = (geo.size.w.min(geo.size.h) / 2) as u32;
|
||||||
|
corners.top_right > half_min_dim
|
||||||
|
|| corners.top_left > half_min_dim
|
||||||
|
|| corners.bottom_right > half_min_dim
|
||||||
|
|| corners.bottom_left > half_min_dim
|
||||||
|
})
|
||||||
|
&& let Some(hook) = surface_data.data_map.get::<ToplevelHookId>()
|
||||||
|
{
|
||||||
|
let hook_ref = hook.lock().unwrap();
|
||||||
|
if let Some((_, obj)) = hook_ref.as_ref()
|
||||||
|
&& let Ok(obj) = obj.upgrade()
|
||||||
|
{
|
||||||
|
obj.post_error(
|
||||||
|
cosmic_corner_radius_toplevel_v1::Error::RadiusTooLarge as u32,
|
||||||
|
format!("{obj:?} corner radius too large"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pad_rect(
|
||||||
|
mut rect: Rectangle<i32, Logical>,
|
||||||
|
padding: &Padding,
|
||||||
|
) -> Option<Rectangle<i32, Logical>> {
|
||||||
|
rect.size.h = rect.size.h.checked_sub(padding.top)?;
|
||||||
|
rect.loc.x += padding.top;
|
||||||
|
rect.size.w = rect.size.w.checked_sub(padding.left)?;
|
||||||
|
rect.loc.y += padding.left;
|
||||||
|
rect.size.h = rect.size.h.checked_sub(padding.bottom)?;
|
||||||
|
rect.size.w = rect.size.w.checked_sub(padding.right)?;
|
||||||
|
Some(rect)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layer_radius_hook<D: 'static>(_state: &mut D, _dh: &DisplayHandle, surface: &WlSurface) {
|
||||||
|
let bbox = bbox_from_surface_tree(surface, Point::default());
|
||||||
|
with_states(surface, |surface_data| {
|
||||||
|
let corners = *surface_data
|
||||||
|
.cached_state
|
||||||
|
.get::<CacheableCorners>()
|
||||||
|
.pending();
|
||||||
|
let padding = *surface_data
|
||||||
|
.cached_state
|
||||||
|
.get::<CacheablePadding>()
|
||||||
|
.pending();
|
||||||
|
let empty = Padding::default();
|
||||||
|
let Some(padded_box) = pad_rect(bbox, padding.0.as_ref().unwrap_or(&empty)) else {
|
||||||
|
if let Some(hook) = surface_data.data_map.get::<LayerHookId>() {
|
||||||
|
let hook_ref = hook.lock().unwrap();
|
||||||
|
if let Some((_, obj)) = hook_ref.as_ref()
|
||||||
|
&& let Ok(obj) = obj.upgrade()
|
||||||
|
{
|
||||||
|
obj.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::PaddingTooLarge as u32,
|
||||||
|
format!("{obj:?} padding too large"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if corners.0.as_ref().is_some_and(|corners| {
|
||||||
|
let half_min_dim = (padded_box.size.w.min(padded_box.size.h) / 2) as u32;
|
||||||
|
corners.top_right > half_min_dim
|
||||||
|
|| corners.top_left > half_min_dim
|
||||||
|
|| corners.bottom_right > half_min_dim
|
||||||
|
|| corners.bottom_left > half_min_dim
|
||||||
|
}) && let Some(hook) = surface_data.data_map.get::<LayerHookId>()
|
||||||
|
{
|
||||||
|
let hook_ref = hook.lock().unwrap();
|
||||||
|
if let Some((_, obj)) = hook_ref.as_ref()
|
||||||
|
&& let Ok(obj) = obj.upgrade()
|
||||||
|
{
|
||||||
|
obj.post_error(
|
||||||
|
cosmic_corner_radius_layer_v1::Error::RadiusTooLarge as u32,
|
||||||
|
format!("{obj:?} corner radius too large"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub type CornerRadiusData = Mutex<CornerRadiusInternal>;
|
pub type CornerRadiusData = Mutex<CornerRadiusInternal>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum CornerRadiusSurface {
|
||||||
|
Toplevel(Weak<XdgToplevel>),
|
||||||
|
Popup(Weak<XdgPopup>),
|
||||||
|
Layer(Weak<ZwlrLayerSurfaceV1>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CornerRadiusInternal {
|
pub struct CornerRadiusInternal {
|
||||||
pub toplevel: Weak<XdgToplevel>,
|
pub surface: CornerRadiusSurface,
|
||||||
pub corners: Option<Corners>,
|
pub corners: Option<Corners>,
|
||||||
|
pub padding: Option<Padding>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct Corners {
|
pub struct Corners {
|
||||||
pub top_left: u8,
|
pub top_left: u32,
|
||||||
pub top_right: u8,
|
pub top_right: u32,
|
||||||
pub bottom_right: u8,
|
pub bottom_right: u32,
|
||||||
pub bottom_left: u8,
|
pub bottom_left: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Default)]
|
||||||
|
pub struct Padding {
|
||||||
|
pub top: i32,
|
||||||
|
pub right: i32,
|
||||||
|
pub bottom: i32,
|
||||||
|
pub left: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Copy, Clone)]
|
#[derive(Default, Debug, Copy, Clone)]
|
||||||
|
|
@ -328,6 +746,18 @@ impl Cacheable for CacheableCorners {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Copy, Clone)]
|
||||||
|
pub struct CacheablePadding(pub Option<Padding>);
|
||||||
|
|
||||||
|
impl Cacheable for CacheablePadding {
|
||||||
|
fn commit(&mut self, _dh: &DisplayHandle) -> Self {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
fn merge_into(self, into: &mut Self, _dh: &DisplayHandle) {
|
||||||
|
*into = self;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CornerRadiusInternal {
|
impl CornerRadiusInternal {
|
||||||
fn set_corner_radius(
|
fn set_corner_radius(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
@ -337,13 +767,23 @@ impl CornerRadiusInternal {
|
||||||
bottom_left: u32,
|
bottom_left: u32,
|
||||||
) {
|
) {
|
||||||
let corners = Corners {
|
let corners = Corners {
|
||||||
top_left: top_left.clamp(u8::MIN as u32, u8::MAX as u32) as u8,
|
top_left,
|
||||||
top_right: top_right.clamp(u8::MIN as u32, u8::MAX as u32) as u8,
|
top_right,
|
||||||
bottom_right: bottom_right.clamp(u8::MIN as u32, u8::MAX as u32) as u8,
|
bottom_right,
|
||||||
bottom_left: bottom_left.clamp(u8::MIN as u32, u8::MAX as u32) as u8,
|
bottom_left,
|
||||||
};
|
};
|
||||||
self.corners = Some(corners);
|
self.corners = Some(corners);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_padding(&mut self, top: i32, right: i32, bottom: i32, left: i32) {
|
||||||
|
let padding = Padding {
|
||||||
|
top,
|
||||||
|
right,
|
||||||
|
bottom,
|
||||||
|
left,
|
||||||
|
};
|
||||||
|
self.padding = Some(padding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! delegate_corner_radius {
|
macro_rules! delegate_corner_radius {
|
||||||
|
|
@ -357,6 +797,9 @@ macro_rules! delegate_corner_radius {
|
||||||
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
|
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
|
||||||
cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1: CornerRadiusData
|
cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_toplevel_v1::CosmicCornerRadiusToplevelV1: CornerRadiusData
|
||||||
] => $crate::wayland::protocols::corner_radius::CornerRadiusState);
|
] => $crate::wayland::protocols::corner_radius::CornerRadiusState);
|
||||||
|
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
|
||||||
|
cosmic_protocols::corner_radius::v1::server::cosmic_corner_radius_layer_v1::CosmicCornerRadiusLayerV1: CornerRadiusData
|
||||||
|
] => $crate::wayland::protocols::corner_radius::CornerRadiusState);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub(crate) use delegate_corner_radius;
|
pub(crate) use delegate_corner_radius;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue