From 2bda1255ee306695e12b443615b9fffb42d37007 Mon Sep 17 00:00:00 2001 From: bernard alisauskas Date: Tue, 10 Nov 2026 11:06:36 +0700 Subject: [PATCH 1/4] Add toggle-titlebar: hide/show a window's title bar at runtime Flips the focused window's decoration between `server` and `minimal`, so the title bar goes away while the border, corners and shadow stay. Both modes map to server-side on the wire, so the client is not told; the compositor drops or rebuilds the window's decoration and its cached chrome. Windows that draw their own chrome (`client`) or none are left alone, so a GTK app never gets a second bar. A `minimal` rule window gains a bar on the first toggle. Runtime-only, like `msg opacity`: the override lives on the stored applied rule and is gone when the window closes. Co-Authored-By: Claude Sonnet 5.5 --- config.reference.toml | 2 ++ docs/config.md | 1 + src/config/parse.rs | 3 ++ src/config/types.rs | 1 + src/input/actions.rs | 69 ++++++++++++++++++++++++++++++++++++ src/tests/mod.rs | 1 + src/tests/toggle_titlebar.rs | 67 ++++++++++++++++++++++++++++++++++ 7 files changed, 144 insertions(+) create mode 100644 src/tests/toggle_titlebar.rs diff --git a/config.reference.toml b/config.reference.toml index 3f6d0b34..ae8cea70 100644 --- a/config.reference.toml +++ b/config.reference.toml @@ -357,6 +357,8 @@ # # fit-window-snapped — fit-window, moving its snap cluster along # # fill-window — grow the focused window into the free space around it; again to restore # # toggle-pin-to-screen — pin the focused window to the screen, ignoring pan and zoom +# # toggle-titlebar — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, +# # corners and shadow stay). Windows that draw their own chrome are left alone # # reload-config — hot-reload the config file # # toggle-cursor-pan — toggle bare-cursor edge-pan (see [navigation.edge_pan]) # # set-trackpad on|off|toggle — turn every trackpad on or off for this session diff --git a/docs/config.md b/docs/config.md index c8cc46dd..eee68fa1 100644 --- a/docs/config.md +++ b/docs/config.md @@ -779,6 +779,7 @@ Actions: - `fit-window-snapped` — fit-window, moving its snap cluster along - `fill-window` — grow the focused window into the free space around it; again to restore - `toggle-pin-to-screen` — pin the focused window to the screen, ignoring pan and zoom +- `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome are left alone - `reload-config` — hot-reload the config file - `toggle-cursor-pan` — toggle bare-cursor edge-pan (see [navigation.edge_pan]) - `set-trackpad on|off|toggle` — turn every trackpad on or off for this session diff --git a/src/config/parse.rs b/src/config/parse.rs index 10498e22..bcb6d5f4 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -215,6 +215,7 @@ pub fn parse_action(s: &str) -> Result { Ok(Action::SwitchLayout(target)) } "toggle-pin-to-screen" => Ok(Action::TogglePinToScreen), + "toggle-titlebar" => Ok(Action::ToggleTitlebar), "reload-config" => Ok(Action::ReloadConfig), "toggle-cursor-pan" => Ok(Action::ToggleCursorPan), "set-trackpad" => { @@ -273,6 +274,7 @@ pub const ACTION_NAMES: &[(&str, &str)] = &[ ("toggle-cursor-pan", "toggle-cursor-pan"), ("toggle-fullscreen", "toggle-fullscreen"), ("toggle-pin-to-screen", "toggle-pin-to-screen"), + ("toggle-titlebar", "toggle-titlebar"), ("zoom-in", "zoom-in"), ("zoom-out", "zoom-out"), ("zoom-reset", "zoom-reset"), @@ -661,6 +663,7 @@ mod tests { Action::SendCursorToOutput(_) => "send-cursor-to-output", Action::FocusCenter => "focus-center", Action::TogglePinToScreen => "toggle-pin-to-screen", + Action::ToggleTitlebar => "toggle-titlebar", Action::SwitchLayout(_) => "switch-layout", Action::ReloadConfig => "reload-config", Action::ToggleCursorPan => "toggle-cursor-pan", diff --git a/src/config/types.rs b/src/config/types.rs index ecf4b0df..af340068 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -81,6 +81,7 @@ pub enum Action { SendCursorToOutput(Direction), FocusCenter, TogglePinToScreen, + ToggleTitlebar, SwitchLayout(LayoutSwitch), ReloadConfig, ToggleCursorPan, diff --git a/src/input/actions.rs b/src/input/actions.rs index 0c96e652..42770018 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -722,6 +722,9 @@ impl DriftWm { Action::TogglePinToScreen => { self.toggle_pin_to_screen(); } + Action::ToggleTitlebar => { + self.toggle_titlebar(); + } Action::ReloadConfig => { self.reload_config(); } @@ -785,6 +788,72 @@ impl DriftWm { } } + /// Hide or show the focused window's driftwm title bar. Flips the window's + /// decoration between `server` and `minimal`, so border, corners and shadow + /// stay; both map to server-side on the wire, so the client is not told. + /// Windows that draw their own chrome (`client`) or none are left alone. + fn toggle_titlebar(&mut self) { + use smithay::reexports::wayland_server::Resource; + + let Some(window) = self + .focused_window() + .filter(|w| !self.is_window_fullscreen(w)) + else { + return; + }; + let Some(surface) = window.wl_surface().map(|s| s.into_owned()) else { + return; + }; + let key = crate::decorations::DecorationKey::Surface(surface.id()); + let applied = driftwm::config::applied_rule(&surface); + let mode = driftwm::config::effective_decoration_mode( + applied.as_ref().and_then(|r| r.decoration.as_ref()), + &self.config.decorations.default_mode, + ) + .clone(); + let has_bar = self.decorations.contains_key(&key); + + let new_mode = match (has_bar, &mode) { + (true, _) => driftwm::config::DecorationMode::Minimal, + (false, driftwm::config::DecorationMode::Minimal) => { + driftwm::config::DecorationMode::Server + } + _ => return, + }; + + smithay::wayland::compositor::with_states(&surface, |states| { + states.data_map.insert_if_missing_threadsafe(|| { + std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) + }); + states + .data_map + .get::>() + .unwrap() + .lock() + .unwrap() + .decoration = Some(new_mode.clone()); + }); + + if has_bar { + self.decorations.remove(&key); + self.pending_ssd.remove(&surface.id()); + } else { + self.pending_ssd.insert(surface.id()); + self.decorations.insert( + key.clone(), + crate::decorations::WindowDecoration::new( + window.geometry().size.w, + true, + &self.config.decorations, + ), + ); + } + // Cached chrome is sized to the old frame (bar included). + self.render.shadow_cache.remove(&key); + self.render.border_cache.remove(&key); + self.mark_all_dirty(); + } + /// Override `[input.trackpad]`'s send-events mode for the rest of the /// session and push it to every connected trackpad. Tracking the intent /// rather than each device's current mode keeps two trackpads from drifting diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c0673035..05f13985 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -86,6 +86,7 @@ mod step_units; mod suspend_flows; mod suspended; mod tablet; +mod toggle_titlebar; mod trackpad_send_events; mod translucent_fullscreen; mod virtual_keyboard; diff --git a/src/tests/toggle_titlebar.rs b/src/tests/toggle_titlebar.rs new file mode 100644 index 00000000..e81ad662 --- /dev/null +++ b/src/tests/toggle_titlebar.rs @@ -0,0 +1,67 @@ +//! `toggle-titlebar`: hides/shows a server-decorated window's title bar at +//! runtime by flipping its decoration between `server` and `minimal`. + +use driftwm::config::{Action, DecorationMode, applied_rule}; +use smithay::reexports::wayland_server::Resource as _; + +use super::{Fixture, config, map_window, server_surface, window_by_app_id}; + +fn has_bar(f: &mut Fixture, app_id: &str) -> bool { + let window = window_by_app_id(f, app_id).unwrap(); + let key = crate::decorations::DecorationKey::Surface(server_surface(&window).id()); + f.state().decorations.contains_key(&key) +} + +fn decoration_of(f: &mut Fixture, app_id: &str) -> Option { + let window = window_by_app_id(f, app_id).unwrap(); + applied_rule(&server_surface(&window)).and_then(|r| r.decoration) +} + +#[test] +fn toggles_a_server_decorated_window_between_bar_and_minimal() { + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"bar\"\ndecoration = \"server\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + map_window(&mut f, id, "bar", (400, 300)); + assert!(has_bar(&mut f, "bar"), "the rule gave it a title bar"); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(!has_bar(&mut f, "bar"), "first toggle hides the bar"); + assert_eq!(decoration_of(&mut f, "bar"), Some(DecorationMode::Minimal)); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(has_bar(&mut f, "bar"), "second toggle brings it back"); + assert_eq!(decoration_of(&mut f, "bar"), Some(DecorationMode::Server)); +} + +#[test] +fn a_minimal_rule_window_gets_a_bar_on_first_toggle() { + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"min\"\ndecoration = \"minimal\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + map_window(&mut f, id, "min", (400, 300)); + assert!(!has_bar(&mut f, "min")); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(has_bar(&mut f, "min")); + assert_eq!(decoration_of(&mut f, "min"), Some(DecorationMode::Server)); +} + +/// A client that draws its own chrome must not suddenly get a second bar. +#[test] +fn leaves_a_client_decorated_window_alone() { + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"csd\"\ndecoration = \"client\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + map_window(&mut f, id, "csd", (400, 300)); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(!has_bar(&mut f, "csd")); + assert_eq!(decoration_of(&mut f, "csd"), Some(DecorationMode::Client)); +} From 64766bfb803c00690cdc0ecb59c372403f52692b Mon Sep 17 00:00:00 2001 From: bernard alisauskas Date: Tue, 10 Nov 2026 12:17:32 +0700 Subject: [PATCH 2/4] toggle-titlebar: refresh snap rect, skip fit/fill/widget, re-carve corners Review follow-up. - Refresh the cached snap rect: the frame changes by the bar height, and a stale rect read as a grow on the next commit and could relocate a snapped window. - Leave fit, fill and widget windows alone: their geometry was computed against the bar they have, or is rule-defined. - Skip windows that negotiated client-side decoration, so an app with its own header bar never gets a second one. - Carve the opaque-region corners when the bar goes. The compositor clips the corners of a bar-less window itself, but a client that never commits again kept declaring them opaque. The carve moves out of `commit` into `trim_opaque_corners` and is idempotent. - Move the mechanism into state/window_frame.rs, drop the redundant shadow/border cache evictions and the unread `pending_ssd` bookkeeping. - Document the runtime rule rewrite in window-rules.md. Co-Authored-By: Claude Sonnet 5.5 --- config.reference.toml | 3 +- docs/config.md | 2 +- docs/window-rules.md | 5 ++ src/handlers/compositor.rs | 134 ++++++++++++++++++++--------------- src/input/actions.rs | 72 +++---------------- src/state/window_frame.rs | 72 +++++++++++++++++++ src/tests/toggle_titlebar.rs | 97 +++++++++++++++++++++++++ 7 files changed, 263 insertions(+), 122 deletions(-) diff --git a/config.reference.toml b/config.reference.toml index ae8cea70..97d23b4c 100644 --- a/config.reference.toml +++ b/config.reference.toml @@ -358,7 +358,8 @@ # # fill-window — grow the focused window into the free space around it; again to restore # # toggle-pin-to-screen — pin the focused window to the screen, ignoring pan and zoom # # toggle-titlebar — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, -# # corners and shadow stay). Windows that draw their own chrome are left alone +# # corners and shadow stay). Windows that draw their own chrome, and fit, fill or +# # fullscreen windows, are left alone # # reload-config — hot-reload the config file # # toggle-cursor-pan — toggle bare-cursor edge-pan (see [navigation.edge_pan]) # # set-trackpad on|off|toggle — turn every trackpad on or off for this session diff --git a/docs/config.md b/docs/config.md index eee68fa1..beed3f29 100644 --- a/docs/config.md +++ b/docs/config.md @@ -779,7 +779,7 @@ Actions: - `fit-window-snapped` — fit-window, moving its snap cluster along - `fill-window` — grow the focused window into the free space around it; again to restore - `toggle-pin-to-screen` — pin the focused window to the screen, ignoring pan and zoom -- `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome are left alone +- `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome, and fit, fill or fullscreen windows, are left alone - `reload-config` — hot-reload the config file - `toggle-cursor-pan` — toggle bare-cursor edge-pan (see [navigation.edge_pan]) - `set-trackpad on|off|toggle` — turn every trackpad on or off for this session diff --git a/docs/window-rules.md b/docs/window-rules.md index 6d1c04d8..a4b70650 100644 --- a/docs/window-rules.md +++ b/docs/window-rules.md @@ -473,6 +473,11 @@ pinned, output, … — are resolved **once, when a window maps**: reloading you config only affects windows opened afterwards, and a window that changes its title after mapping is **not** re-checked against `title` rules. +Two things change a window's stored rule *after* it maps, and a config reload +does not undo them: `msg opacity ` pins the opacity, and `toggle-titlebar` +flips `decoration` between `server` and `minimal`. Both are runtime-only, so they +are not saved with the session and end when the window closes. + A few things re-resolve live against the current config instead: `pass_keys` is evaluated per keypress and `pass_mouse` per button press and scroll (so a config reload — and a title change — takes effect immediately), layer-surface chrome is diff --git a/src/handlers/compositor.rs b/src/handlers/compositor.rs index 9fb579df..b153b42a 100644 --- a/src/handlers/compositor.rs +++ b/src/handlers/compositor.rs @@ -215,69 +215,13 @@ impl CompositorHandler for DriftWm { self.mark_dirty_for_surface(surface); // Trim corner rects from CSD toplevels' opaque regions so the - // background can render through. Some CSD apps (LibreOffice/GTK3) - // declare the full rect opaque while rendering transparent corners, - // leaving black artifacts where damage tracking skips redraws. - // ARGB only — XRGB is handled in RoundedCornerElement::opaque_regions. - // Skipped for `decoration = "none"` (pass-through promise). + // background can render through (see `trim_opaque_corners`). Skipped for + // windows with a title bar: the bar's frame draws its own corners. if !self .decorations .contains_key(&DecorationKey::Surface(surface.id())) { - with_states(surface, |states| { - // Every commit of every surface lands here, and only toplevels - // can carve — bail before the rule read so subsurfaces, popups, - // layer surfaces and cursors pay a single data_map lookup. - if states.data_map.get::().is_none() { - return; - } - // `applied_rule` re-enters `with_states`, whose private-data - // mutex this closure already holds — read the rule off the - // states in hand instead. - let applied = states - .data_map - .get::>() - .and_then(|m| m.lock().ok()) - .map(|guard| guard.clone()); - let mode = driftwm::config::effective_decoration_mode( - applied.as_ref().and_then(|r| r.decoration.as_ref()), - &self.config.decorations.default_mode, - ); - if matches!(mode, driftwm::config::DecorationMode::None) { - return; - } - let corner_radius = driftwm::config::effective_corner_radius( - applied.as_ref(), - mode, - &self.config.decorations, - ); - let mut guard = states.cached_state.get::(); - let attrs = guard.current(); - if let Some(ref mut region) = attrs.opaque_region { - let Some(bounds) = region - .rects - .iter() - .filter(|(k, _)| matches!(k, RectangleKind::Add)) - .map(|(_, r)| *r) - .reduce(|a, b| a.merge(b)) - else { - return; - }; - let r = corner_radius + 2; - if bounds.size.w > 2 * r && bounds.size.h > 2 * r { - let (x, y, w, h) = - (bounds.loc.x, bounds.loc.y, bounds.size.w, bounds.size.h); - for corner in [ - Rectangle::new((x, y).into(), (r, r).into()), - Rectangle::new((x + w - r, y).into(), (r, r).into()), - Rectangle::new((x + w - r, y + h - r).into(), (r, r).into()), - Rectangle::new((x, y + h - r).into(), (r, r).into()), - ] { - region.rects.push((RectangleKind::Subtract, corner)); - } - } - } - }); + self.trim_opaque_corners(surface); } // The buffer handler below rebuilds every surface in this tree, sync @@ -1399,6 +1343,78 @@ impl DriftWm { self.refresh_stable_snap_rect(&client); } + /// Trim corner rects from a CSD toplevel's opaque region so the background + /// can render through. Some CSD apps (LibreOffice/GTK3) declare the full rect + /// opaque while rendering transparent corners, leaving black artifacts where + /// damage tracking skips redraws. ARGB only — XRGB is handled in + /// `RoundedCornerElement::opaque_regions`. Skipped for `decoration = "none"` + /// (pass-through promise). Idempotent, so it can also run outside a commit. + pub(crate) fn trim_opaque_corners( + &self, + surface: &smithay::reexports::wayland_server::protocol::wl_surface::WlSurface, + ) { + with_states(surface, |states| { + // Every commit of every surface lands here, and only toplevels + // can carve — bail before the rule read so subsurfaces, popups, + // layer surfaces and cursors pay a single data_map lookup. + if states.data_map.get::().is_none() { + return; + } + // `applied_rule` re-enters `with_states`, whose private-data + // mutex this closure already holds — read the rule off the + // states in hand instead. + let applied = states + .data_map + .get::>() + .and_then(|m| m.lock().ok()) + .map(|guard| guard.clone()); + let mode = driftwm::config::effective_decoration_mode( + applied.as_ref().and_then(|r| r.decoration.as_ref()), + &self.config.decorations.default_mode, + ); + if matches!(mode, driftwm::config::DecorationMode::None) { + return; + } + let corner_radius = driftwm::config::effective_corner_radius( + applied.as_ref(), + mode, + &self.config.decorations, + ); + let mut guard = states.cached_state.get::(); + let attrs = guard.current(); + if let Some(ref mut region) = attrs.opaque_region { + let Some(bounds) = region + .rects + .iter() + .filter(|(k, _)| matches!(k, RectangleKind::Add)) + .map(|(_, r)| *r) + .reduce(|a, b| a.merge(b)) + else { + return; + }; + let r = corner_radius + 2; + if bounds.size.w > 2 * r && bounds.size.h > 2 * r { + let (x, y, w, h) = (bounds.loc.x, bounds.loc.y, bounds.size.w, bounds.size.h); + let corners = [ + Rectangle::new((x, y).into(), (r, r).into()), + Rectangle::new((x + w - r, y).into(), (r, r).into()), + Rectangle::new((x + w - r, y + h - r).into(), (r, r).into()), + Rectangle::new((x, y + h - r).into(), (r, r).into()), + ]; + for corner in corners { + let carved = region + .rects + .iter() + .any(|(k, r)| matches!(k, RectangleKind::Subtract) && *r == corner); + if !carved { + region.rects.push((RectangleKind::Subtract, corner)); + } + } + } + } + }); + } + /// A snapped window that resizes *itself* larger — not via a resize grab — /// can grow over its neighbors. The classic case is a game that maps at a /// small size then jumps to its full render resolution a frame later. Move diff --git a/src/input/actions.rs b/src/input/actions.rs index 42770018..1cfd264f 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -788,70 +788,20 @@ impl DriftWm { } } - /// Hide or show the focused window's driftwm title bar. Flips the window's - /// decoration between `server` and `minimal`, so border, corners and shadow - /// stay; both map to server-side on the wire, so the client is not told. - /// Windows that draw their own chrome (`client`) or none are left alone. + /// Hide or show the focused window's driftwm title bar; see + /// [`DriftWm::toggle_window_titlebar`]. Fit, fill and widget windows are + /// left alone: their geometry was computed against the current bar (or is + /// rule-defined), and nothing here would redo it. fn toggle_titlebar(&mut self) { - use smithay::reexports::wayland_server::Resource; - - let Some(window) = self - .focused_window() - .filter(|w| !self.is_window_fullscreen(w)) - else { - return; - }; - let Some(surface) = window.wl_surface().map(|s| s.into_owned()) else { + let Some(window) = self.focused_window().filter(|w| { + !self.is_window_fullscreen(w) + && !self.stage.is_fit(w) + && !self.stage.is_fill(w) + && !w.is_widget() + }) else { return; }; - let key = crate::decorations::DecorationKey::Surface(surface.id()); - let applied = driftwm::config::applied_rule(&surface); - let mode = driftwm::config::effective_decoration_mode( - applied.as_ref().and_then(|r| r.decoration.as_ref()), - &self.config.decorations.default_mode, - ) - .clone(); - let has_bar = self.decorations.contains_key(&key); - - let new_mode = match (has_bar, &mode) { - (true, _) => driftwm::config::DecorationMode::Minimal, - (false, driftwm::config::DecorationMode::Minimal) => { - driftwm::config::DecorationMode::Server - } - _ => return, - }; - - smithay::wayland::compositor::with_states(&surface, |states| { - states.data_map.insert_if_missing_threadsafe(|| { - std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) - }); - states - .data_map - .get::>() - .unwrap() - .lock() - .unwrap() - .decoration = Some(new_mode.clone()); - }); - - if has_bar { - self.decorations.remove(&key); - self.pending_ssd.remove(&surface.id()); - } else { - self.pending_ssd.insert(surface.id()); - self.decorations.insert( - key.clone(), - crate::decorations::WindowDecoration::new( - window.geometry().size.w, - true, - &self.config.decorations, - ), - ); - } - // Cached chrome is sized to the old frame (bar included). - self.render.shadow_cache.remove(&key); - self.render.border_cache.remove(&key); - self.mark_all_dirty(); + self.toggle_window_titlebar(&window); } /// Override `[input.trackpad]`'s send-events mode for the rest of the diff --git a/src/state/window_frame.rs b/src/state/window_frame.rs index 8073eaf5..87de77ea 100644 --- a/src/state/window_frame.rs +++ b/src/state/window_frame.rs @@ -34,6 +34,78 @@ impl DriftWm { .map_or(0, |_| self.config.decorations.title_bar_height) } + /// Hide or show `window`'s driftwm title bar by flipping its decoration + /// between `server` and `minimal`: the bar goes, the border, corners and + /// shadow stay. Both are `ServerSide` on the wire, so the client is not told. + /// The content keeps its place; the bar is drawn above it. + /// + /// Left alone: a window that negotiated client-side decoration or draws no + /// chrome (`client` / `none`), so an app with its own header bar never gets a + /// second one. The override is written into the window's stored rule, so it is + /// not saved with the session and is gone when the window closes. + /// + /// The frame changes by the bar height, so the cached snap rect is refreshed; + /// neighbours in a snap cluster are not re-snapped. + pub fn toggle_window_titlebar(&mut self, window: &Window) { + use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1::Mode; + + let Some(surface) = window.wl_surface().map(|s| s.into_owned()) else { + return; + }; + if window + .toplevel() + .is_some_and(|t| t.with_pending_state(|s| s.decoration_mode) == Some(Mode::ClientSide)) + { + return; + } + let key = crate::decorations::DecorationKey::Surface(surface.id()); + let applied = driftwm::config::applied_rule(&surface); + let mode = driftwm::config::effective_decoration_mode( + applied.as_ref().and_then(|r| r.decoration.as_ref()), + &self.config.decorations.default_mode, + ); + let has_bar = self.decorations.contains_key(&key); + + let new_mode = match (has_bar, mode) { + (true, _) => driftwm::config::DecorationMode::Minimal, + (false, driftwm::config::DecorationMode::Minimal) => { + driftwm::config::DecorationMode::Server + } + _ => return, + }; + + smithay::wayland::compositor::with_states(&surface, |states| { + states.data_map.insert_if_missing_threadsafe(|| { + std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) + }); + states + .data_map + .get::>() + .unwrap() + .lock() + .unwrap() + .decoration = Some(new_mode); + }); + + if has_bar { + self.decorations.remove(&key); + // With the bar gone the corners are clipped by the compositor, so the + // surface's opaque region has to give them up too. + self.trim_opaque_corners(&surface); + } else if window.geometry().size.w > 0 { + self.decorations.insert( + key, + crate::decorations::WindowDecoration::new( + window.geometry().size.w, + true, + &self.config.decorations, + ), + ); + } + self.refresh_stable_snap_rect(&StageWindow::Client(window.clone())); + self.mark_all_dirty(); + } + /// Border width for an element with no surface to resolve a per-rule /// override against — a suspended window. Uses the global default mode's /// width, matching what a relaunched client would get before its rule diff --git a/src/tests/toggle_titlebar.rs b/src/tests/toggle_titlebar.rs index e81ad662..c7fa362a 100644 --- a/src/tests/toggle_titlebar.rs +++ b/src/tests/toggle_titlebar.rs @@ -5,6 +5,7 @@ use driftwm::config::{Action, DecorationMode, applied_rule}; use smithay::reexports::wayland_server::Resource as _; use super::{Fixture, config, map_window, server_surface, window_by_app_id}; +use crate::state::StageWindow; fn has_bar(f: &mut Fixture, app_id: &str) -> bool { let window = window_by_app_id(f, app_id).unwrap(); @@ -65,3 +66,99 @@ fn leaves_a_client_decorated_window_alone() { assert!(!has_bar(&mut f, "csd")); assert_eq!(decoration_of(&mut f, "csd"), Some(DecorationMode::Client)); } + +/// The snap rect is cached at settle events. A frame that changes by the bar +/// height without refreshing it would read as a grow on the next commit and make +/// `reflow_grown_snapped_window` relocate the window. +#[test] +fn the_cached_snap_rect_follows_the_frame() { + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"bar\"\ndecoration = \"server\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + map_window(&mut f, id, "bar", (400, 300)); + let window = window_by_app_id(&mut f, "bar").unwrap(); + let surface_id = server_surface(&window).id(); + let height = |f: &mut Fixture| { + let cached = *f.state().stable_snap_rects.get(&surface_id).unwrap(); + let live = f + .state() + .snap_rect_for(&StageWindow::Client(window.clone())) + .unwrap(); + assert_eq!(cached.y_high - cached.y_low, live.y_high - live.y_low); + cached.y_high - cached.y_low + }; + + let with_bar = height(&mut f); + f.state().execute_action(&Action::ToggleTitlebar); + let without_bar = height(&mut f); + assert_eq!( + with_bar - without_bar, + f64::from(f.state().config.decorations.title_bar_height) + ); + + f.state().execute_action(&Action::ToggleTitlebar); + assert_eq!(height(&mut f), with_bar); +} + +/// Fit and fill size the window against the bar it has now; the toggle would +/// leave it a bar-height off the usable area, so it leaves them alone. +#[test] +fn leaves_a_fitted_window_alone() { + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"fit\"\ndecoration = \"server\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + map_window(&mut f, id, "fit", (400, 300)); + + f.state().execute_action(&Action::FitWindow); + f.double_roundtrip(id); + let window = window_by_app_id(&mut f, "fit").unwrap(); + assert!(f.state().stage.is_fit(&window), "the window is fitted"); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(has_bar(&mut f, "fit")); + assert_eq!(decoration_of(&mut f, "fit"), Some(DecorationMode::Server)); +} + +/// The compositor clips a bar-less window's corners itself, so its opaque region +/// has to give them up as the bar goes, without waiting for the client to commit. +#[test] +fn carves_the_opaque_corners_when_the_bar_goes() { + use smithay::wayland::compositor::{RectangleKind, SurfaceAttributes, with_states}; + + let mut f = Fixture::with_config(config( + "[[window_rules]]\napp_id = \"bar\"\ndecoration = \"server\"\n", + )); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + let surface = map_window(&mut f, id, "bar", (400, 300)); + f.client(id) + .set_opaque_region(&surface, &[(0, 0, 400, 300)]); + f.client(id).window(&surface).commit(); + f.roundtrip(id); + + let carved = |f: &mut Fixture| { + let window = window_by_app_id(f, "bar").unwrap(); + with_states(&server_surface(&window), |states| { + let mut guard = states.cached_state.get::(); + guard.current().opaque_region.as_ref().map_or(0, |region| { + region + .rects + .iter() + .filter(|(kind, _)| matches!(kind, RectangleKind::Subtract)) + .count() + }) + }) + }; + + assert_eq!(carved(&mut f), 0, "a bar draws its own corners"); + f.state().execute_action(&Action::ToggleTitlebar); + assert_eq!(carved(&mut f), 4, "one carved rect per corner"); + + f.state().execute_action(&Action::ToggleTitlebar); + f.state().execute_action(&Action::ToggleTitlebar); + assert_eq!(carved(&mut f), 4, "toggling again does not stack carves"); +} From ba32223147c0d1efbabb533240ef9a9925d0ef8c Mon Sep 17 00:00:00 2001 From: bernard alisauskas Date: Tue, 10 Nov 2026 13:20:14 +0700 Subject: [PATCH 3/4] Add toggle-borders: hide/show a window's border, shadow and corner clip A runtime flag on the window's stored rule, checked by effective_border_width, effective_shadow_enabled and effective_corner_radius the way `decoration = "none"` already is, so render, layout, snap, fit and IPC all see it through the helpers they already use. The rule's own values stay untouched, so toggling back needs nothing saved. Applies to client-decorated windows too (the compositor draws those three around them as well); left alone for `decoration = "none"`, fullscreen, fit, fill and widget windows, the same guards as toggle-titlebar, which now share a target helper and a post-change step (snap rect refresh, corner carve, redraw). Runtime-only, like `msg opacity`: not saved in session.json, gone when the window closes. Co-Authored-By: Claude Sonnet 5.5 --- config.reference.toml | 3 + docs/config.md | 1 + docs/window-rules.md | 7 +- src/config/parse.rs | 3 + src/config/types.rs | 58 +++++++++++++++- src/input/actions.rs | 26 ++++--- src/state/window_frame.rs | 54 +++++++++++++-- src/tests/mod.rs | 1 + src/tests/toggle_borders.rs | 132 ++++++++++++++++++++++++++++++++++++ 9 files changed, 263 insertions(+), 22 deletions(-) create mode 100644 src/tests/toggle_borders.rs diff --git a/config.reference.toml b/config.reference.toml index 97d23b4c..87c72873 100644 --- a/config.reference.toml +++ b/config.reference.toml @@ -360,6 +360,9 @@ # # toggle-titlebar — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, # # corners and shadow stay). Windows that draw their own chrome, and fit, fill or # # fullscreen windows, are left alone +# # toggle-borders — hide or show the focused window's border, drop shadow and corner clip together, so it +# # draws as the bare client surface; again to restore. Fit, fill, fullscreen and widget +# # windows, and `decoration = "none"` ones, are left alone # # reload-config — hot-reload the config file # # toggle-cursor-pan — toggle bare-cursor edge-pan (see [navigation.edge_pan]) # # set-trackpad on|off|toggle — turn every trackpad on or off for this session diff --git a/docs/config.md b/docs/config.md index beed3f29..6adabf6a 100644 --- a/docs/config.md +++ b/docs/config.md @@ -780,6 +780,7 @@ Actions: - `fill-window` — grow the focused window into the free space around it; again to restore - `toggle-pin-to-screen` — pin the focused window to the screen, ignoring pan and zoom - `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome, and fit, fill or fullscreen windows, are left alone +- `toggle-borders` — hide or show the focused window's border, drop shadow and corner clip together, so it draws as the bare client surface; again to restore. Fit, fill, fullscreen and widget windows, and `decoration = "none"` ones, are left alone - `reload-config` — hot-reload the config file - `toggle-cursor-pan` — toggle bare-cursor edge-pan (see [navigation.edge_pan]) - `set-trackpad on|off|toggle` — turn every trackpad on or off for this session diff --git a/docs/window-rules.md b/docs/window-rules.md index a4b70650..7986eb6c 100644 --- a/docs/window-rules.md +++ b/docs/window-rules.md @@ -473,9 +473,10 @@ pinned, output, … — are resolved **once, when a window maps**: reloading you config only affects windows opened afterwards, and a window that changes its title after mapping is **not** re-checked against `title` rules. -Two things change a window's stored rule *after* it maps, and a config reload -does not undo them: `msg opacity ` pins the opacity, and `toggle-titlebar` -flips `decoration` between `server` and `minimal`. Both are runtime-only, so they +Three things change a window's stored rule *after* it maps, and a config reload +does not undo them: `msg opacity ` pins the opacity, `toggle-titlebar` +flips `decoration` between `server` and `minimal`, and `toggle-borders` hides or +restores the border, shadow and corner clip as one. All are runtime-only, so they are not saved with the session and end when the window closes. A few things re-resolve live against the current config instead: `pass_keys` is diff --git a/src/config/parse.rs b/src/config/parse.rs index bcb6d5f4..f30103e2 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -216,6 +216,7 @@ pub fn parse_action(s: &str) -> Result { } "toggle-pin-to-screen" => Ok(Action::TogglePinToScreen), "toggle-titlebar" => Ok(Action::ToggleTitlebar), + "toggle-borders" => Ok(Action::ToggleBorders), "reload-config" => Ok(Action::ReloadConfig), "toggle-cursor-pan" => Ok(Action::ToggleCursorPan), "set-trackpad" => { @@ -271,6 +272,7 @@ pub const ACTION_NAMES: &[(&str, &str)] = &[ ("spawn", "spawn foo"), ("suspend-window", "suspend-window"), ("switch-layout", "switch-layout next"), + ("toggle-borders", "toggle-borders"), ("toggle-cursor-pan", "toggle-cursor-pan"), ("toggle-fullscreen", "toggle-fullscreen"), ("toggle-pin-to-screen", "toggle-pin-to-screen"), @@ -664,6 +666,7 @@ mod tests { Action::FocusCenter => "focus-center", Action::TogglePinToScreen => "toggle-pin-to-screen", Action::ToggleTitlebar => "toggle-titlebar", + Action::ToggleBorders => "toggle-borders", Action::SwitchLayout(_) => "switch-layout", Action::ReloadConfig => "reload-config", Action::ToggleCursorPan => "toggle-cursor-pan", diff --git a/src/config/types.rs b/src/config/types.rs index af340068..b116003e 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -82,6 +82,7 @@ pub enum Action { FocusCenter, TogglePinToScreen, ToggleTitlebar, + ToggleBorders, SwitchLayout(LayoutSwitch), ReloadConfig, ToggleCursorPan, @@ -923,6 +924,10 @@ pub struct AppliedWindowRule { pub shadow: Option, pub output: Option, pub layer_order: Option, + /// Runtime only (`toggle-borders`): draw the window without its border, + /// shadow and corner clip, whatever the rule and `[decorations]` say. A flag + /// rather than overwritten values, so toggling back needs nothing saved. + pub borders_hidden: bool, } impl AppliedWindowRule { @@ -1018,6 +1023,7 @@ impl From<&WindowRule> for AppliedWindowRule { shadow: rule.shadow, output: rule.output.clone(), layer_order: rule.layer_order, + borders_hidden: false, } } } @@ -1149,7 +1155,7 @@ pub fn effective_border_width( mode: &DecorationMode, decorations: &DecorationConfig, ) -> i32 { - if matches!(mode, DecorationMode::None) { + if matches!(mode, DecorationMode::None) || applied.is_some_and(|r| r.borders_hidden) { return 0; } if let Some(bw) = applied.and_then(|r| r.border_width) { @@ -1221,7 +1227,7 @@ pub fn effective_corner_radius( mode: &DecorationMode, decorations: &DecorationConfig, ) -> i32 { - if matches!(mode, DecorationMode::None) { + if matches!(mode, DecorationMode::None) || applied.is_some_and(|r| r.borders_hidden) { return 0; } if let Some(cr) = applied.and_then(|r| r.corner_radius) { @@ -1237,7 +1243,7 @@ pub fn effective_shadow_enabled( mode: &DecorationMode, decorations: &DecorationConfig, ) -> bool { - if matches!(mode, DecorationMode::None) { + if matches!(mode, DecorationMode::None) || applied.is_some_and(|r| r.borders_hidden) { return false; } if let Some(s) = applied.and_then(|r| r.shadow) { @@ -1415,6 +1421,52 @@ pub struct BackgroundConfig { mod tests { use super::*; + #[test] + fn hidden_borders_zero_the_border_shadow_and_corner_clip() { + let decorations = DecorationConfig { + border_width: 4, + corner_radius: 12, + shadow: true, + ..DecorationConfig::default() + }; + let rule = AppliedWindowRule { + border_width: Some(6), + corner_radius: Some(20), + shadow: Some(true), + ..AppliedWindowRule::default() + }; + let hidden = AppliedWindowRule { + borders_hidden: true, + ..rule.clone() + }; + for mode in [ + DecorationMode::Client, + DecorationMode::Server, + DecorationMode::Minimal, + ] { + assert_eq!(effective_border_width(Some(&rule), &mode, &decorations), 6); + assert_eq!( + effective_corner_radius(Some(&rule), &mode, &decorations), + 20 + ); + assert!(effective_shadow_enabled(Some(&rule), &mode, &decorations)); + + assert_eq!( + effective_border_width(Some(&hidden), &mode, &decorations), + 0 + ); + assert_eq!( + effective_corner_radius(Some(&hidden), &mode, &decorations), + 0 + ); + assert!(!effective_shadow_enabled( + Some(&hidden), + &mode, + &decorations + )); + } + } + #[test] fn runs_during_fullscreen_allowlist() { assert!(Action::ToggleFullscreen.runs_during_fullscreen()); diff --git a/src/input/actions.rs b/src/input/actions.rs index 1cfd264f..1bc6cf67 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -723,7 +723,14 @@ impl DriftWm { self.toggle_pin_to_screen(); } Action::ToggleTitlebar => { - self.toggle_titlebar(); + if let Some(window) = self.chrome_toggle_target() { + self.toggle_window_titlebar(&window); + } + } + Action::ToggleBorders => { + if let Some(window) = self.chrome_toggle_target() { + self.toggle_window_borders(&window); + } } Action::ReloadConfig => { self.reload_config(); @@ -788,20 +795,17 @@ impl DriftWm { } } - /// Hide or show the focused window's driftwm title bar; see - /// [`DriftWm::toggle_window_titlebar`]. Fit, fill and widget windows are - /// left alone: their geometry was computed against the current bar (or is - /// rule-defined), and nothing here would redo it. - fn toggle_titlebar(&mut self) { - let Some(window) = self.focused_window().filter(|w| { + /// The window `toggle-titlebar` / `toggle-borders` act on: the focused one. + /// Fit, fill and widget windows are left alone: their geometry was computed + /// against the chrome they have now (or is rule-defined), and nothing here + /// would redo it. + fn chrome_toggle_target(&self) -> Option { + self.focused_window().filter(|w| { !self.is_window_fullscreen(w) && !self.stage.is_fit(w) && !self.stage.is_fill(w) && !w.is_widget() - }) else { - return; - }; - self.toggle_window_titlebar(&window); + }) } /// Override `[input.trackpad]`'s send-events mode for the rest of the diff --git a/src/state/window_frame.rs b/src/state/window_frame.rs index 87de77ea..c951e23e 100644 --- a/src/state/window_frame.rs +++ b/src/state/window_frame.rs @@ -44,8 +44,6 @@ impl DriftWm { /// second one. The override is written into the window's stored rule, so it is /// not saved with the session and is gone when the window closes. /// - /// The frame changes by the bar height, so the cached snap rect is refreshed; - /// neighbours in a snap cluster are not re-snapped. pub fn toggle_window_titlebar(&mut self, window: &Window) { use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1::Mode; @@ -89,9 +87,6 @@ impl DriftWm { if has_bar { self.decorations.remove(&key); - // With the bar gone the corners are clipped by the compositor, so the - // surface's opaque region has to give them up too. - self.trim_opaque_corners(&surface); } else if window.geometry().size.w > 0 { self.decorations.insert( key, @@ -102,6 +97,55 @@ impl DriftWm { ), ); } + self.chrome_changed(window, &surface); + } + + /// Hide or show `window`'s border, drop shadow and corner clip together, so + /// it draws as the bare client surface; again to restore whatever its rule and + /// `[decorations]` say. The title bar is separate (`toggle-titlebar`). + /// + /// Applies to a `client`-decorated window too, since the compositor draws + /// those three around it as well. Left alone for `decoration = "none"`, which + /// already has none of them. The flag lives on the window's stored rule, so + /// it is not saved with the session and is gone when the window closes. + pub fn toggle_window_borders(&mut self, window: &Window) { + let Some(surface) = window.wl_surface().map(|s| s.into_owned()) else { + return; + }; + let applied = driftwm::config::applied_rule(&surface); + let mode = driftwm::config::effective_decoration_mode( + applied.as_ref().and_then(|r| r.decoration.as_ref()), + &self.config.decorations.default_mode, + ); + if matches!(mode, driftwm::config::DecorationMode::None) { + return; + } + let hidden = applied.is_some_and(|r| r.borders_hidden); + + smithay::wayland::compositor::with_states(&surface, |states| { + states.data_map.insert_if_missing_threadsafe(|| { + std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) + }); + states + .data_map + .get::>() + .unwrap() + .lock() + .unwrap() + .borders_hidden = !hidden; + }); + self.chrome_changed(window, &surface); + } + + /// Bookkeeping after a toggle changed what the frame is made of. The frame + /// size changed, so the cached snap rect is refreshed; neighbours in a snap + /// cluster are not re-snapped. A window without a bar has its corners clipped + /// by the compositor, so the opaque-region carve follows the new radius. + fn chrome_changed(&mut self, window: &Window, surface: &WlSurface) { + let key = crate::decorations::DecorationKey::Surface(surface.id()); + if !self.decorations.contains_key(&key) { + self.trim_opaque_corners(surface); + } self.refresh_stable_snap_rect(&StageWindow::Client(window.clone())); self.mark_all_dirty(); } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 05f13985..eb0ce9a5 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -86,6 +86,7 @@ mod step_units; mod suspend_flows; mod suspended; mod tablet; +mod toggle_borders; mod toggle_titlebar; mod trackpad_send_events; mod translucent_fullscreen; diff --git a/src/tests/toggle_borders.rs b/src/tests/toggle_borders.rs new file mode 100644 index 00000000..9171cf26 --- /dev/null +++ b/src/tests/toggle_borders.rs @@ -0,0 +1,132 @@ +//! `toggle-borders`: hides/shows a window's border, shadow and corner clip as +//! one by flipping a runtime flag on its stored rule. + +use driftwm::config::{Action, applied_rule}; +use smithay::reexports::wayland_server::Resource as _; + +use super::{Fixture, config, map_window, server_surface, window_by_app_id}; +use crate::state::StageWindow; + +const BORDERED: &str = "[decorations]\nborder_width = 4\ncorner_radius = 12\n"; + +fn fixture(rules: &str) -> (Fixture, super::client::ClientId) { + let mut f = Fixture::with_config(config(&format!("{BORDERED}{rules}"))); + f.add_output(1, (1920, 1080)); + let id = f.add_client(); + (f, id) +} + +fn border_width(f: &mut Fixture, app_id: &str) -> i32 { + let window = window_by_app_id(f, app_id).unwrap(); + f.state().window_border_width(&server_surface(&window)) +} + +fn hidden(f: &mut Fixture, app_id: &str) -> bool { + let window = window_by_app_id(f, app_id).unwrap(); + applied_rule(&server_surface(&window)).is_some_and(|r| r.borders_hidden) +} + +#[test] +fn toggles_the_border_off_and_back_to_what_the_config_says() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"w\"\ndecoration = \"server\"\n"); + map_window(&mut f, id, "w", (400, 300)); + assert_eq!(border_width(&mut f, "w"), 4); + + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(border_width(&mut f, "w"), 0); + assert!(hidden(&mut f, "w")); + + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(border_width(&mut f, "w"), 4, "restored, nothing was saved"); + assert!(!hidden(&mut f, "w")); +} + +#[test] +fn restores_a_rules_own_border_width_not_the_global_one() { + let (mut f, id) = + fixture("[[window_rules]]\napp_id = \"w\"\ndecoration = \"server\"\nborder_width = 9\n"); + map_window(&mut f, id, "w", (400, 300)); + assert_eq!(border_width(&mut f, "w"), 9); + + f.state().execute_action(&Action::ToggleBorders); + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(border_width(&mut f, "w"), 9); +} + +/// The compositor draws a border, shadow and corner clip around a client-decorated +/// window too, so unlike the title bar this applies to it. +#[test] +fn applies_to_a_client_decorated_window() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"csd\"\ndecoration = \"client\"\n"); + map_window(&mut f, id, "csd", (400, 300)); + assert_eq!(border_width(&mut f, "csd"), 4); + + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(border_width(&mut f, "csd"), 0); +} + +#[test] +fn leaves_a_decoration_none_window_alone() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"bare\"\ndecoration = \"none\"\n"); + map_window(&mut f, id, "bare", (400, 300)); + + f.state().execute_action(&Action::ToggleBorders); + assert!(!hidden(&mut f, "bare"), "it already has no chrome to hide"); +} + +#[test] +fn leaves_a_fitted_window_alone() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"fit\"\ndecoration = \"server\"\n"); + map_window(&mut f, id, "fit", (400, 300)); + f.state().execute_action(&Action::FitWindow); + f.double_roundtrip(id); + let window = window_by_app_id(&mut f, "fit").unwrap(); + assert!(f.state().stage.is_fit(&window)); + + f.state().execute_action(&Action::ToggleBorders); + assert!(!hidden(&mut f, "fit")); + assert_eq!(border_width(&mut f, "fit"), 4); +} + +/// The frame is the content plus the border on every side, so hiding it shrinks +/// the frame by twice its width; the cached snap rect has to follow. +#[test] +fn the_cached_snap_rect_follows_the_frame() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"w\"\ndecoration = \"server\"\n"); + map_window(&mut f, id, "w", (400, 300)); + let window = window_by_app_id(&mut f, "w").unwrap(); + let surface_id = server_surface(&window).id(); + let width = |f: &mut Fixture| { + let cached = *f.state().stable_snap_rects.get(&surface_id).unwrap(); + let live = f + .state() + .snap_rect_for(&StageWindow::Client(window.clone())) + .unwrap(); + assert_eq!(cached.x_high - cached.x_low, live.x_high - live.x_low); + cached.x_high - cached.x_low + }; + + let with_border = width(&mut f); + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(with_border - width(&mut f), 8.0); + + f.state().execute_action(&Action::ToggleBorders); + assert_eq!(width(&mut f), with_border); +} + +/// Border and title bar are independent toggles, so a user can have either. +#[test] +fn is_independent_of_the_title_bar() { + let (mut f, id) = fixture("[[window_rules]]\napp_id = \"w\"\ndecoration = \"server\"\n"); + map_window(&mut f, id, "w", (400, 300)); + let window = window_by_app_id(&mut f, "w").unwrap(); + let key = crate::decorations::DecorationKey::Surface(server_surface(&window).id()); + + f.state().execute_action(&Action::ToggleBorders); + assert!(f.state().decorations.contains_key(&key), "bar stays"); + + f.state().execute_action(&Action::ToggleTitlebar); + assert!(!f.state().decorations.contains_key(&key)); + assert!(hidden(&mut f, "w"), "border stays hidden"); + assert_eq!(border_width(&mut f, "w"), 0); +} From 879a88d14d619dc0628f70789805299fa71ef419 Mon Sep 17 00:00:00 2001 From: bernard alisauskas Date: Tue, 10 Nov 2026 19:56:10 +0700 Subject: [PATCH 4/4] chrome toggles: share the rule update, skip pending fit/fullscreen, drop stale render caches Review fixes: don't write the decoration override when no bar can be created, drop the cached border/shadow elements when chrome changes, skip windows with a deferred fit or fullscreen, document that widget windows are skipped, and tidy the doc comments. --- config.reference.toml | 4 +-- docs/config.md | 2 +- src/input/actions.rs | 6 +++- src/state/window_frame.rs | 65 +++++++++++++++++++-------------------- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/config.reference.toml b/config.reference.toml index 87c72873..63958412 100644 --- a/config.reference.toml +++ b/config.reference.toml @@ -358,8 +358,8 @@ # # fill-window — grow the focused window into the free space around it; again to restore # # toggle-pin-to-screen — pin the focused window to the screen, ignoring pan and zoom # # toggle-titlebar — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, -# # corners and shadow stay). Windows that draw their own chrome, and fit, fill or -# # fullscreen windows, are left alone +# # corners and shadow stay). Windows that draw their own chrome, and fit, fill, +# # fullscreen or widget windows, are left alone # # toggle-borders — hide or show the focused window's border, drop shadow and corner clip together, so it # # draws as the bare client surface; again to restore. Fit, fill, fullscreen and widget # # windows, and `decoration = "none"` ones, are left alone diff --git a/docs/config.md b/docs/config.md index 6adabf6a..8d8d5ccb 100644 --- a/docs/config.md +++ b/docs/config.md @@ -779,7 +779,7 @@ Actions: - `fit-window-snapped` — fit-window, moving its snap cluster along - `fill-window` — grow the focused window into the free space around it; again to restore - `toggle-pin-to-screen` — pin the focused window to the screen, ignoring pan and zoom -- `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome, and fit, fill or fullscreen windows, are left alone +- `toggle-titlebar` — hide or show the focused window's driftwm title bar (server ↔ minimal: the border, corners and shadow stay). Windows that draw their own chrome, and fit, fill, fullscreen or widget windows, are left alone - `toggle-borders` — hide or show the focused window's border, drop shadow and corner clip together, so it draws as the bare client surface; again to restore. Fit, fill, fullscreen and widget windows, and `decoration = "none"` ones, are left alone - `reload-config` — hot-reload the config file - `toggle-cursor-pan` — toggle bare-cursor edge-pan (see [navigation.edge_pan]) diff --git a/src/input/actions.rs b/src/input/actions.rs index 1bc6cf67..d667e845 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -801,7 +801,11 @@ impl DriftWm { /// would redo it. fn chrome_toggle_target(&self) -> Option { self.focused_window().filter(|w| { - !self.is_window_fullscreen(w) + let pending = w.wl_surface().is_some_and(|s| { + self.pending_fit.contains(&*s) || self.pending_fullscreen.contains_key(&*s) + }); + !pending + && !self.is_window_fullscreen(w) && !self.stage.is_fit(w) && !self.stage.is_fill(w) && !w.is_widget() diff --git a/src/state/window_frame.rs b/src/state/window_frame.rs index c951e23e..5a4ef549 100644 --- a/src/state/window_frame.rs +++ b/src/state/window_frame.rs @@ -17,6 +17,23 @@ use driftwm::window_ext::WindowExt; use super::{DriftWm, StageWindow}; +/// Mutate the window's stored rule in place, creating it if the window has none. +fn update_applied_rule( + surface: &WlSurface, + f: impl FnOnce(&mut driftwm::config::AppliedWindowRule), +) { + smithay::wayland::compositor::with_states(surface, |states| { + states.data_map.insert_if_missing_threadsafe(|| { + std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) + }); + let rule = states + .data_map + .get::>() + .unwrap(); + f(&mut rule.lock().unwrap()); + }); +} + impl DriftWm { pub fn window_ssd_bar(&self, window: &W) -> i32 { // Every stand-in draws the same textless bar (a CSD-origin one shrinks @@ -40,10 +57,9 @@ impl DriftWm { /// The content keeps its place; the bar is drawn above it. /// /// Left alone: a window that negotiated client-side decoration or draws no - /// chrome (`client` / `none`), so an app with its own header bar never gets a - /// second one. The override is written into the window's stored rule, so it is + /// chrome (`client` / `none`), so an app that negotiated its own header bar + /// never gets a second one. The override is written into the window's stored rule, so it is /// not saved with the session and is gone when the window closes. - /// pub fn toggle_window_titlebar(&mut self, window: &Window) { use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1::Mode; @@ -72,29 +88,20 @@ impl DriftWm { _ => return, }; - smithay::wayland::compositor::with_states(&surface, |states| { - states.data_map.insert_if_missing_threadsafe(|| { - std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) - }); - states - .data_map - .get::>() - .unwrap() - .lock() - .unwrap() - .decoration = Some(new_mode); - }); + // A bar needs a sized window; bail before the rule write so the rule + // and the decorations map cannot disagree. + let width = window.geometry().size.w; + if !has_bar && width <= 0 { + return; + } + update_applied_rule(&surface, |r| r.decoration = Some(new_mode)); if has_bar { self.decorations.remove(&key); - } else if window.geometry().size.w > 0 { + } else { self.decorations.insert( key, - crate::decorations::WindowDecoration::new( - window.geometry().size.w, - true, - &self.config.decorations, - ), + crate::decorations::WindowDecoration::new(width, true, &self.config.decorations), ); } self.chrome_changed(window, &surface); @@ -122,18 +129,7 @@ impl DriftWm { } let hidden = applied.is_some_and(|r| r.borders_hidden); - smithay::wayland::compositor::with_states(&surface, |states| { - states.data_map.insert_if_missing_threadsafe(|| { - std::sync::Mutex::new(driftwm::config::AppliedWindowRule::default()) - }); - states - .data_map - .get::>() - .unwrap() - .lock() - .unwrap() - .borders_hidden = !hidden; - }); + update_applied_rule(&surface, |r| r.borders_hidden = !hidden); self.chrome_changed(window, &surface); } @@ -143,6 +139,9 @@ impl DriftWm { /// by the compositor, so the opaque-region carve follows the new radius. fn chrome_changed(&mut self, window: &Window, surface: &WlSurface) { let key = crate::decorations::DecorationKey::Surface(surface.id()); + // Drop the cached border/shadow elements; they are rebuilt on demand. + self.render.border_cache.remove(&key); + self.render.shadow_cache.remove(&key); if !self.decorations.contains_key(&key) { self.trim_opaque_corners(surface); }