From 0f4c84b9666e6a34c99f11c51df80628110db198 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 16:32:54 +0000 Subject: [PATCH] Super+D shows the desktop: minimize every window, again to bring them back Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hktZQ73WicBDmBgP57 --- compositor/host/compositor.mbt | 10 ++++ compositor/host/input_test.mbt | 47 ++++++++++++++++ compositor/host/showdesktop.mbt | 97 +++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 compositor/host/showdesktop.mbt diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index af0a832..55e396f 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -50,6 +50,11 @@ pub(all) struct MoonPlasmaCompositor { mut mods : UInt /// A Super key is down and nothing else was pressed since: releasing it opens the launcher. mut super_solo : Bool + /// Super+D (showdesktop.mbt): whether a Super key is held, whether the D press was swallowed, + /// and the windows it minimized (bottom to top) so a second Super+D can restore them. + mut super_down : Bool + mut d_swallowed : Bool + mut desktop_hidden : Array[UInt64] mut layout_mode : @wm.LayoutMode /// Set when the user asked to end the session (the logout button); the main loop then exits. mut quit_requested : Bool @@ -113,6 +118,9 @@ pub fn MoonPlasmaCompositor::new( kbd_win: None, mods: 0U, super_solo: false, + super_down: false, + d_swallowed: false, + desktop_hidden: [], layout_mode: @wm.LayoutMode::Tiling, quit_requested: false, switcher_on: false, @@ -1232,6 +1240,8 @@ pub fn MoonPlasmaCompositor::handle_input( Key(code, pressed) => if self.switcher_key(code, pressed, time_ms) { self.super_solo = false + } else if self.show_desktop_key(code, pressed) { + self.super_solo = false } else if code == 125U || code == 126U { // Super (left/right): a bare tap toggles the launcher search; never reaches clients. if pressed { diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index d4f3607..b911d02 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -1074,3 +1074,50 @@ test "compose_scene hands the host a scene only when something changed" { assert_true(comp.switcher_open()) assert_true(comp.compose_scene() is Some(_)) } + +///| +test "super+d minimizes every window to show the desktop, and again brings them back" { + let socket_path = "/tmp/wayland-moon-supd-" + @sys.getpid().to_string() + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap() + ignore(open_and_configure_client(comp, socket_path, "A", "a")) + ignore(open_and_configure_client(comp, socket_path, "B", "b")) + ignore(comp.compose_frame()) + let key = fn(code : UInt, down : Bool, t : UInt) { + comp.handle_input(@nested.InputEvent::Key(code, down), t) + } + let focused = fn() { + let mut f = 0UL + for id, w in comp.windows { + if w.focused { + f = id + } + } + f + } + assert_eq(focused(), 2UL) + // Super+D: both windows are minimized and the launcher does not open. + key(125U, true, 1U) + key(32U, true, 2U) + key(32U, false, 3U) + key(125U, false, 4U) + assert_true(comp.wm.windows.get(1UL).unwrap().minimized) + assert_true(comp.wm.windows.get(2UL).unwrap().minimized) + assert_eq(comp.wm.stacking_order().length(), 0) + assert_true(comp.desktop_shown()) + assert_false(comp.shell.popup_open()) + // Super+D again: they come back, the one that was on top focused. + key(125U, true, 10U) + key(32U, true, 11U) + key(32U, false, 12U) + key(125U, false, 13U) + assert_false(comp.wm.windows.get(1UL).unwrap().minimized) + assert_false(comp.wm.windows.get(2UL).unwrap().minimized) + assert_false(comp.desktop_shown()) + assert_false(comp.shell.popup_open()) + assert_eq(focused(), 2UL) + // D without Super is an ordinary key. + key(32U, true, 20U) + key(32U, false, 21U) + assert_false(comp.wm.windows.get(2UL).unwrap().minimized) +} diff --git a/compositor/host/showdesktop.mbt b/compositor/host/showdesktop.mbt new file mode 100644 index 0000000..c646544 --- /dev/null +++ b/compositor/host/showdesktop.mbt @@ -0,0 +1,97 @@ +///| Super+D: show the desktop. +/// +/// Minimizes every visible window of the active workspace and remembers them; Super+D again +/// (while nothing is on screen) brings them back in their old stacking order with the one that +/// was on top focused. Opening or restoring a window in between starts over: the next Super+D +/// clears the screen again. Neither the D press nor its release reaches a client. + +///| +/// evdev codes. +let key_d : UInt32 = 32 + +///| +let key_lsuper : UInt32 = 125 + +///| +let key_rsuper : UInt32 = 126 + +///| +/// Handle a key for Super+D. Returns true when the key is consumed (not for clients). +fn MoonPlasmaCompositor::show_desktop_key( + self : MoonPlasmaCompositor, + code : UInt32, + pressed : Bool, +) -> Bool { + if code == key_lsuper || code == key_rsuper { + self.super_down = pressed + return false // the Super tap (launcher) is handled by the caller + } + if code != key_d { + return false + } + if !pressed { + // The release of a D we swallowed must not reach a client either. + let swallowed = self.d_swallowed + self.d_swallowed = false + return swallowed + } + if !self.super_down || self.shell.inspecting() { + return false + } + self.d_swallowed = true + self.toggle_show_desktop() + true +} + +///| +/// Minimize all visible windows of the active workspace, or restore the ones Super+D hid. +pub fn MoonPlasmaCompositor::toggle_show_desktop( + self : MoonPlasmaCompositor, +) -> Unit { + let visible = self.wm.stacking_order() + if visible.length() == 0 { + // Nothing on screen: bring back what we hid (bottom to top, so the old top ends up on top). + let hidden = self.desktop_hidden + self.desktop_hidden = [] + let mut top : UInt64? = None + for wid in hidden { + match self.wm.windows.get(wid) { + Some(r) => + if r.minimized { + self.apply_actions( + self.wm.handle_request( + wid, + @wm.WindowRequest::Minimize(false), + self.work_area, + ), + ) + top = Some(wid) + } + None => () + } + } + match top { + Some(wid) => self.activate_window(wid) + None => () + } + } else { + for wid in visible { + self.apply_actions( + self.wm.handle_request( + wid, + @wm.WindowRequest::Minimize(true), + self.work_area, + ), + ) + } + self.desktop_hidden = visible + self.set_kbd_focus(None) + } + self.damage.mark_full() +} + +///| +/// True while Super+D has windows hidden that another Super+D would bring back. +pub fn MoonPlasmaCompositor::desktop_shown(self : MoonPlasmaCompositor) -> Bool { + self.desktop_hidden.length() > 0 && self.wm.stacking_order().length() == 0 +}