Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui_linux: Replace kde-blur protocol with ext-background-effect protocol (#53746)
A Badly Drawn Wobbler14b7b0ef3764
diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rsindex 1c87031c7aa..8aded9780a3 100644--- a/crates/gpui/src/geometry.rs+++ b/crates/gpui/src/geometry.rs@@ -2803,6 +2803,12 @@ impl Pixels {self.0}+ /// Converts to `i32`, truncating toward zero.+ /// Out-of-range values saturate, and NaN becomes zero.+ pub fn to_i32(self) -> i32 {+ self.0 as i32+ }+/// Floors the `Pixels` value to the nearest whole number.////// # Returns@@ -3984,6 +3990,21 @@ wheremod tests {use super::*;+ #[test]+ fn test_pixels_to_i32() {+ for (value, expected) in [+ (12.9, 12),+ (-12.9, -12),+ (f32::MAX, i32::MAX),+ (f32::MIN, i32::MIN),+ (f32::INFINITY, i32::MAX),+ (f32::NEG_INFINITY, i32::MIN),+ (f32::NAN, 0),+ ] {+ assert_eq!(px(value).to_i32(), expected);+ }+ }+#[test]fn test_bounds_intersects() {let bounds1 = Bounds {diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rsindex fb12125cf11..6ddbac4c432 100644--- a/crates/gpui/src/platform.rs+++ b/crates/gpui/src/platform.rs@@ -1398,6 +1398,7 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {WindowControls::default()}fn set_client_inset(&self, _inset: Pixels) {}+ fn set_client_corner_radii(&self, _radii: Corners<Pixels>) {}fn gpu_specs(&self) -> Option<GpuSpecs>;fn update_ime_position(&self, _bounds: Bounds<Pixels>);diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex cf98dcf80f6..3802130e5f6 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -3077,6 +3077,11 @@ impl Window {self.platform_window.set_client_inset(inset);}+ /// Sets the client-drawn corner radii so platform effects can match the window's shape.+ pub fn set_client_corner_radii(&mut self, radii: Corners<Pixels>) {+ self.platform_window.set_client_corner_radii(radii);+ }+/// Returns the client_inset value by [`Self::set_client_inset`].pub fn client_inset(&self) -> Option<Pixels> {self.client_insetdiff --git a/crates/gpui_linux/src/linux/wayland/client.rs b/crates/gpui_linux/src/linux/wayland/client.rsindex fa007ef00c7..625b1d51920 100644--- a/crates/gpui_linux/src/linux/wayland/client.rs+++ b/crates/gpui_linux/src/linux/wayland/client.rs@@ -41,6 +41,9 @@ use wayland_client::{wl_shm_pool, wl_surface,},};+use wayland_protocols::ext::background_effect::v1::client::{+ ext_background_effect_manager_v1, ext_background_effect_surface_v1,+};use wayland_protocols::wp::pointer_gestures::zv1::client::{zwp_pointer_gesture_pinch_v1, zwp_pointer_gestures_v1,};@@ -253,7 +256,9 @@ pub struct Globals {Option<wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1>,pub decoration_manager: Option<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>,pub layer_shell: Option<zwlr_layer_shell_v1::ZwlrLayerShellV1>,- pub blur_manager: Option<org_kde_kwin_blur_manager::OrgKdeKwinBlurManager>,+ pub kde_blur_manager: Option<org_kde_kwin_blur_manager::OrgKdeKwinBlurManager>,+ pub background_effects_manager:+ Option<ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1>,pub text_input_manager: Option<zwp_text_input_manager_v3::ZwpTextInputManagerV3>,pub gesture_manager: Option<zwp_pointer_gestures_v1::ZwpPointerGesturesV1>,pub dialog: Option<xdg_wm_dialog_v1::XdgWmDialogV1>,@@ -303,7 +308,8 @@ impl Globals {fractional_scale_manager: globals.bind(&qh, 1..=1, ()).ok(),decoration_manager: globals.bind(&qh, 1..=1, ()).ok(),layer_shell: globals.bind(&qh, 1..=5, ()).ok(),- blur_manager: globals.bind(&qh, 1..=1, ()).ok(),+ kde_blur_manager: globals.bind(&qh, 1..=1, ()).ok(),+ background_effects_manager: globals.bind(&qh, 1..=1, ()).ok(),text_input_manager: globals.bind(&qh, 1..=1, ()).ok(),gesture_manager: globals.bind(&qh, 1..=3, ()).ok(),dialog: globals.bind(&qh, dialog_v..=dialog_v, ()).ok(),@@ -1668,6 +1674,8 @@ delegate_noop!(WaylandClientStatePtr: ignore zxdg_decoration_manager_v1::ZxdgDecdelegate_noop!(WaylandClientStatePtr: ignore zwlr_layer_shell_v1::ZwlrLayerShellV1);delegate_noop!(WaylandClientStatePtr: ignore xdg_positioner::XdgPositioner);delegate_noop!(WaylandClientStatePtr: ignore org_kde_kwin_blur_manager::OrgKdeKwinBlurManager);+delegate_noop!(WaylandClientStatePtr: ignore ext_background_effect_manager_v1::ExtBackgroundEffectManagerV1);+delegate_noop!(WaylandClientStatePtr: ignore ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1);delegate_noop!(WaylandClientStatePtr: ignore zwp_text_input_manager_v3::ZwpTextInputManagerV3);delegate_noop!(WaylandClientStatePtr: ignore org_kde_kwin_blur::OrgKdeKwinBlur);delegate_noop!(WaylandClientStatePtr: ignore wp_viewporter::WpViewporter);diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rsindex f7eca602e37..6647a8ef7f7 100644--- a/crates/gpui_linux/src/linux/wayland/window.rs+++ b/crates/gpui_linux/src/linux/wayland/window.rs@@ -18,6 +18,7 @@ use wayland_client::{Proxy,protocol::{wl_callback, wl_output, wl_seat, wl_surface},};+use wayland_protocols::ext::background_effect::v1::client::ext_background_effect_surface_v1;use wayland_protocols::wp::viewporter::client::wp_viewport;use wayland_protocols::xdg::decoration::zv1::client::zxdg_toplevel_decoration_v1;use wayland_protocols::xdg::shell::client::xdg_popup;@@ -34,7 +35,7 @@ use wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1;use crate::linux::wayland::{display::WaylandDisplay, serial::SerialKind};use crate::linux::{Globals, Output, PendingActivation, WaylandClientStatePtr, get_window};use gpui::{- AnyWindowHandle, Bounds, Capslock, Decorations, DevicePixels, ExternalDragPayload,+ AnyWindowHandle, Bounds, Capslock, Corners, Decorations, DevicePixels, ExternalDragPayload,FrameRequestSource, GpuSpecs, Modifiers, Pixels, PlatformAtlas, PlatformDisplay,PlatformFrameSignal, PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptButton,PromptLevel, RequestFrameOptions, ResizeEdge, Scene, Size, Tiling, WindowAppearance,@@ -108,7 +109,8 @@ pub struct WaylandWindowState {pub surface: wl_surface::WlSurface,app_id: Option<String>,appearance: WindowAppearance,- blur: Option<org_kde_kwin_blur::OrgKdeKwinBlur>,+ kde_blur: Option<org_kde_kwin_blur::OrgKdeKwinBlur>,+ background_effects: Option<ext_background_effect_surface_v1::ExtBackgroundEffectSurfaceV1>,viewport: Option<wp_viewport::WpViewport>,outputs: HashMap<ObjectId, Output>,display: Option<(ObjectId, Output)>,@@ -139,6 +141,7 @@ pub struct WaylandWindowState {in_progress_window_controls: Option<WindowControls>,window_controls: WindowControls,client_inset: Option<Pixels>,+ client_corner_radii: Corners<Pixels>,accesskit_adapter: Option<accesskit_unix::Adapter>,}@@ -610,7 +613,8 @@ impl WaylandWindowState {children: FxHashMap::default(),surface,app_id: options.app_id,- blur: None,+ kde_blur: None,+ background_effects: None,viewport,globals,outputs: HashMap::default(),@@ -639,6 +643,7 @@ impl WaylandWindowState {in_progress_window_controls: None,window_controls: WindowControls::default(),client_inset: None,+ client_corner_radii: Corners::default(),accesskit_adapter: None,})}@@ -783,10 +788,15 @@ impl Drop for WaylandWindow {state.renderer.take();// Destroy blur first, this has no dependencies.- if let Some(blur) = &state.blur {+ if let Some(blur) = &state.kde_blur {blur.release();}+ // Destroy background effects, this has no dependencies.+ if let Some(background_effects) = &state.background_effects {+ background_effects.destroy();+ }+// Decorations must be destroyed before the xdg state.// See https://wayland.app/protocols/xdg-decoration-unstable-v1#zxdg_toplevel_decoration_v1if let Some(decoration) = &state.surface_state.decoration() {@@ -1162,7 +1172,7 @@ impl WaylandWindowStatePtr {);let initial_configure = self.frame_loop.get() == FrameLoop::Unconfigured;- drop(state);+ update_window(state);if initial_configure {self.frame(None, FrameRequestSource::NativeCallback);} else {@@ -1531,13 +1541,11 @@ impl WaylandWindowStatePtr {self.callbacks.borrow_mut().resize = Some(fun);}- {- let state = self.state.borrow();- if let Some(viewport) = &state.viewport {- viewport- .set_destination(f32::from(size.width) as i32, f32::from(size.height) as i32);- }+ let state = self.state.borrow_mut();+ if let Some(viewport) = &state.viewport {+ viewport.set_destination(f32::from(size.width) as i32, f32::from(size.height) as i32);}+ update_window(state);}pub fn resize(&self, size: Size<Pixels>) {@@ -2212,6 +2220,15 @@ impl PlatformWindow for WaylandWindow {}}+ fn set_client_corner_radii(&self, radii: Corners<Pixels>) {+ let mut state = self.borrow_mut();+ if radii != state.client_corner_radii {+ state.client_corner_radii = radii;+ update_window(state);+ self.0.request_redraw();+ }+ }+fn update_ime_position(&self, bounds: Bounds<Pixels>) {let state = self.borrow();if !state.active {@@ -2291,31 +2308,166 @@ impl accesskit::DeactivationHandler for TrivialDeactivationHandler {}}+fn add_rounded_rect(+ area: Bounds<i32>,+ radii: Corners<Pixels>,+ mut add_rectangle: impl FnMut(i32, i32, i32, i32),+) {+ if area.size.width <= 0 || area.size.height <= 0 {+ return;+ }++ let clamp_radius = |radius: Pixels| {+ radius+ .to_i32()+ .max(0)+ .min(area.size.width / 2)+ .min(area.size.height / 2)+ };+ let top_left = clamp_radius(radii.top_left);+ let top_right = clamp_radius(radii.top_right);+ let bottom_left = clamp_radius(radii.bottom_left);+ let bottom_right = clamp_radius(radii.bottom_right);++ let corner_inset = |radius: i32, row: i32| {+ if row >= radius {+ return 0;+ }+ let y = f64::from(radius) - (f64::from(row) + 0.5);+ let x = (f64::from(radius).powi(2) - y * y).sqrt();+ (f64::from(radius) - x - 0.5).ceil() as i32+ };++ for (left_radius, right_radius, bottom) in [+ (top_left, top_right, false),+ (bottom_left, bottom_right, true),+ ] {+ for row in 0..left_radius.max(right_radius) {+ let left_inset = corner_inset(left_radius, row);+ let right_inset = corner_inset(right_radius, row);+ let width = area.size.width - left_inset - right_inset;+ if width <= 0 {+ continue;+ }+ let y = if bottom {+ area.origin.y + area.size.height - row - 1+ } else {+ area.origin.y + row+ };+ add_rectangle(area.origin.x + left_inset, y, width, 1);+ }+ }++ let top_height = top_left.max(top_right);+ let bottom_height = bottom_left.max(bottom_right);+ let height = area.size.height - top_height - bottom_height;+ if height > 0 {+ add_rectangle(+ area.origin.x,+ area.origin.y + top_height,+ area.size.width,+ height,+ );+ }+}++#[cfg(test)]+mod rounded_rect_tests {+ use super::add_rounded_rect;+ use gpui::{Bounds, Corners, Pixels, point, px, size};++ fn rectangles(area: Bounds<i32>, radii: Corners<Pixels>) -> Vec<(i32, i32, i32, i32)> {+ let mut rectangles = Vec::new();+ add_rounded_rect(area, radii, |x, y, width, height| {+ rectangles.push((x, y, width, height));+ });+ rectangles+ }++ #[test]+ fn square_corners() {+ let area = Bounds::new(point(10, 20), size(8, 8));+ assert_eq!(rectangles(area, Corners::default()), vec![(10, 20, 8, 8)]);+ }++ #[test]+ fn independent_corners() {+ let area = Bounds::new(point(10, 20), size(8, 8));+ let radii = Corners {+ top_left: px(4.0),+ top_right: px(0.0),+ bottom_left: px(0.0),+ bottom_right: px(4.0),+ };+ assert_eq!(+ rectangles(area, radii),+ vec![+ (12, 20, 6, 1),+ (11, 21, 7, 1),+ (10, 22, 8, 1),+ (10, 23, 8, 1),+ (10, 27, 6, 1),+ (10, 26, 7, 1),+ (10, 25, 8, 1),+ (10, 24, 8, 1),+ ]+ );+ }++ #[test]+ fn clamps_radii() {+ let area = Bounds::new(point(10, 20), size(10, 6));+ assert_eq!(+ rectangles(area, Corners::all(px(100.0))),+ rectangles(area, Corners::all(px(3.0)))+ );+ assert_eq!(+ rectangles(area, Corners::all(px(-1.0))),+ rectangles(area, Corners::default())+ );+ }++ #[test]+ fn empty_area() {+ for dimensions in [size(0, 10), size(10, 0), size(-1, 10), size(10, -1)] {+ let area = Bounds::new(point(10, 20), dimensions);+ assert!(rectangles(area, Corners::all(px(4.0))).is_empty());+ }+ }+}+fn update_window(mut state: RefMut<WaylandWindowState>) {let opaque = !state.is_transparent();if let Some(renderer) = &mut state.renderer {renderer.update_transparency(!opaque);}- let opaque_area = state.window_bounds.map(|v| f32::from(v) as i32);- opaque_area.inset(f32::from(state.inset()) as i32);++ let area = inset_by_tiling(+ state.bounds.map_origin(|_| px(0.0)),+ state.inset(),+ state.tiling,+ )+ .map(|value| value.to_i32())+ .map_size(|value| value.max(0));let region = state.globals.compositor.create_region(&state.globals.qh, ());- region.add(- opaque_area.origin.x,- opaque_area.origin.y,- opaque_area.size.width,- opaque_area.size.height,- );- // Note that rounded corners make this rectangle API hard to work with.- // As this is common when using CSD, let's just disable this API.+ // Client decorations include transparent shadows and antialiased corners,+ // so don't promise the compositor an opaque region.if state.background_appearance == WindowBackgroundAppearance::Opaque&& state.decorations == WindowDecorations::Server{+ region.add(+ area.origin.x,+ area.origin.y,+ area.size.width,+ area.size.height,+ );+// Promise the compositor that this region of the window surface// contains no transparent pixels. This allows the compositor to skip// updating whatever is behind the surface for better performance.@@ -2324,17 +2476,45 @@ fn update_window(mut state: RefMut<WaylandWindowState>) {state.surface.set_opaque_region(None);}- if let Some(ref blur_manager) = state.globals.blur_manager {+ // Prefer the ext-background-effect protocol, as it is DE agnostic and+ // replaces the deprecated kde-blur protocol, which is no longer supported+ // since KDE Plasma 6.7.+ // https://invent.kde.org/plasma/kwin/-/commit/7f9e0aa1cd2c2ab4fea1097800893b6f780089eb+ if let Some(ref background_effects_manager) = state.globals.background_effects_manager {+ if state.background_appearance == WindowBackgroundAppearance::Blurred {+ if state.background_effects.is_none() {+ log::debug!("Using ext-background-effect protocol");+ let background_effects = background_effects_manager.get_background_effect(+ &state.surface,+ &state.globals.qh,+ (),+ );+ state.background_effects = Some(background_effects);+ }++ add_rounded_rect(area, state.client_corner_radii, |x, y, width, height| {+ region.add(x, y, width, height);+ });++ state+ .background_effects+ .as_ref()+ .unwrap()+ .set_blur_region(Some(®ion));+ } else if let Some(background_effects) = state.background_effects.take() {+ background_effects.destroy();+ }+ } else if let Some(ref blur_manager) = state.globals.kde_blur_manager {if state.background_appearance == WindowBackgroundAppearance::Blurred {- if state.blur.is_none() {+ if state.kde_blur.is_none() {+ log::debug!("Using kde-blur protocol");let blur = blur_manager.create(&state.surface, &state.globals.qh, ());- state.blur = Some(blur);+ state.kde_blur = Some(blur);}- state.blur.as_ref().unwrap().commit();+ state.kde_blur.as_ref().unwrap().commit();} else {- // It probably doesn't hurt to clear the blur for opaque windowsblur_manager.unset(&state.surface);- if let Some(b) = state.blur.take() {+ if let Some(b) = state.kde_blur.take() {b.release()}}diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex 15d03480089..b36c80ff87d 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -60,10 +60,10 @@ use futures::{};use gpui::{Action, AnyEntity, AnyView, AnyWeakView, App, AppContext, AsyncApp, AsyncWindowContext, Axis,- Bounds, ClipboardItem, Context, CursorStyle, Decorations, DragMoveEvent, Entity, EntityId,- EventEmitter, FocusHandle, Focusable, Global, HitboxBehavior, Hsla, KeyContext, Keystroke,- ManagedView, MouseButton, PathPromptOptions, Point, PromptLevel, Render, ResizeEdge, Size,- Stateful, Subscription, SystemWindowTabController, Task, TaskExt, Tiling, WeakEntity,+ Bounds, ClipboardItem, Context, Corners, CursorStyle, Decorations, DragMoveEvent, Entity,+ EntityId, EventEmitter, FocusHandle, Focusable, Global, HitboxBehavior, Hsla, KeyContext,+ Keystroke, ManagedView, MouseButton, PathPromptOptions, Point, PromptLevel, Render, ResizeEdge,+ Size, Stateful, Subscription, SystemWindowTabController, Task, TaskExt, Tiling, WeakEntity,WindowBounds, WindowHandle, WindowId, WindowOptions, actions, canvas, point, relative, size,transparent_black,};@@ -12242,8 +12242,26 @@ pub fn client_side_decorations(};match decorations {- Decorations::Client { .. } => window.set_client_inset(theme::CLIENT_SIDE_DECORATION_SHADOW),- Decorations::Server => window.set_client_inset(px(0.0)),+ Decorations::Client { .. } => {+ window.set_client_inset(theme::CLIENT_SIDE_DECORATION_SHADOW);+ let radius = |rounded| {+ if rounded {+ theme::CLIENT_SIDE_DECORATION_ROUNDING+ } else {+ px(0.0)+ }+ };+ window.set_client_corner_radii(Corners {+ top_left: radius(!tiling.top && !tiling.left),+ top_right: radius(!tiling.top && !tiling.right),+ bottom_left: radius(!tiling.bottom && !tiling.left),+ bottom_right: radius(!tiling.bottom && !tiling.right),+ });+ }+ Decorations::Server => {+ window.set_client_inset(px(0.0));+ window.set_client_corner_radii(Corners::default());+ }}struct GlobalResizeEdge(ResizeEdge);