diff --git a/config.reference.toml b/config.reference.toml index 3f6d0b34..63958412 100644 --- a/config.reference.toml +++ b/config.reference.toml @@ -357,6 +357,12 @@ # # 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, +# # 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]) # # 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..8d8d5ccb 100644 --- a/docs/config.md +++ b/docs/config.md @@ -779,6 +779,8 @@ 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, 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]) - `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..7986eb6c 100644 --- a/docs/window-rules.md +++ b/docs/window-rules.md @@ -473,6 +473,12 @@ 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. +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 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/config/parse.rs b/src/config/parse.rs index 10498e22..f30103e2 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -215,6 +215,8 @@ pub fn parse_action(s: &str) -> Result { Ok(Action::SwitchLayout(target)) } "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" => { @@ -270,9 +272,11 @@ 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"), + ("toggle-titlebar", "toggle-titlebar"), ("zoom-in", "zoom-in"), ("zoom-out", "zoom-out"), ("zoom-reset", "zoom-reset"), @@ -661,6 +665,8 @@ mod tests { Action::SendCursorToOutput(_) => "send-cursor-to-output", 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 ecf4b0df..b116003e 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -81,6 +81,8 @@ pub enum Action { SendCursorToOutput(Direction), FocusCenter, TogglePinToScreen, + ToggleTitlebar, + ToggleBorders, SwitchLayout(LayoutSwitch), ReloadConfig, ToggleCursorPan, @@ -922,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 { @@ -1017,6 +1023,7 @@ impl From<&WindowRule> for AppliedWindowRule { shadow: rule.shadow, output: rule.output.clone(), layer_order: rule.layer_order, + borders_hidden: false, } } } @@ -1148,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) { @@ -1220,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) { @@ -1236,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) { @@ -1414,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/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 0c96e652..d667e845 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -722,6 +722,16 @@ impl DriftWm { Action::TogglePinToScreen => { self.toggle_pin_to_screen(); } + Action::ToggleTitlebar => { + 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(); } @@ -785,6 +795,23 @@ impl DriftWm { } } + /// 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| { + 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() + }) + } + /// 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/state/window_frame.rs b/src/state/window_frame.rs index 8073eaf5..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 @@ -34,6 +51,104 @@ 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 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; + + 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, + }; + + // 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 { + self.decorations.insert( + key, + crate::decorations::WindowDecoration::new(width, true, &self.config.decorations), + ); + } + 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); + + update_applied_rule(&surface, |r| r.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()); + // 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); + } + 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/mod.rs b/src/tests/mod.rs index c0673035..eb0ce9a5 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -86,6 +86,8 @@ mod step_units; mod suspend_flows; mod suspended; mod tablet; +mod toggle_borders; +mod toggle_titlebar; mod trackpad_send_events; mod translucent_fullscreen; mod virtual_keyboard; 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); +} diff --git a/src/tests/toggle_titlebar.rs b/src/tests/toggle_titlebar.rs new file mode 100644 index 00000000..c7fa362a --- /dev/null +++ b/src/tests/toggle_titlebar.rs @@ -0,0 +1,164 @@ +//! `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}; +use crate::state::StageWindow; + +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)); +} + +/// 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"); +}