From 30f5e886397c5b277e03b9150e9ce5cf88e6c03e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 00:22:04 +0000 Subject: [PATCH] =?UTF-8?q?Overview=20(Expos=C3=A9):=20every=20window=20si?= =?UTF-8?q?de=20by=20side=20when=20the=20launcher=20opens,=20or=20alone=20?= =?UTF-8?q?on=20Super+W?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Opening the launcher (Super tap or its panel button) now also shows every window of the workspace scaled down beside it, over the dimmed wallpaper, like GNOME's Activities overview: a natural layout (rows, one scale, aspect ratios kept, never enlarged), minimized windows dimmed, windows gliding in and out over 220 ms. Hover lights a window and shows a close button; click brings it forward and closes the overview and launcher; middle-click closes the window; Escape or a click on empty space closes it. Typing in the launcher's search fades the windows whose title and app id don't match. - compositor/host/overview.mbt: layout, area beside the launcher, hit testing, easing, input and draw ops; previews reuse the Alt+Tab thumbnails (10 fps refresh, nothing redrawn once settled). - New shortcut "overview" (Super+W) in the shortcut table, rebindable in Settings > Shortcuts. - chandra-ctl / control socket: "shortcut ID" runs any shortcut by id. - Both renderers keep only one large scaled copy per image (a gliding preview is drawn at a new size every frame) and draw images up to 4096 px. - Tests: layout packing and aspect ratio, balance, hit testing, animation, Super tap + click, Super+W + Escape/empty click, search filter, middle click, inspector, control command; screenshots of 1/4/9 windows in dark and light. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hgo3PfGPY8wwv21ZZS --- README.md | 3 +- compositor/host/compositor.mbt | 31 +- compositor/host/control.mbt | 17 + compositor/host/control_test.mbt | 7 + compositor/host/overview.mbt | 743 +++++++++++++++++++++++++++++ compositor/host/overview_test.mbt | 437 +++++++++++++++++ compositor/host/shortcuts.mbt | 7 +- compositor/host/shortcuts_test.mbt | 1 + compositor/host/switcher.mbt | 2 +- compositor/renderer/image.mbt | 16 +- compositor/shell/shell.mbt | 25 + compositor/shell/shortcuts.mbt | 3 + docs/reloading.md | 1 + host-wasm/src/raster.rs | 35 +- session/chandra-ctl | 3 +- 15 files changed, 1321 insertions(+), 10 deletions(-) create mode 100644 compositor/host/overview.mbt create mode 100644 compositor/host/overview_test.mbt diff --git a/README.md b/README.md index 017c19c..11c7d72 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,8 @@ new keys) | Keys | Action | |---|---| -| Super (tap) | Open the application launcher | +| Super (tap) | Open the application launcher, with an overview of every window beside it (typing filters both) | +| Super+W | Overview alone: every window side by side; click one to bring it forward, middle-click to close it | | Alt+Tab | Window switcher, with a live preview of each window | | Super+D | Show the desktop (minimize every window); again to bring them back | | Super+1 .. Super+9 | Open the dock's first nine apps (or bring their windows forward) | diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index d91ff3e..1b4890b 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -99,6 +99,10 @@ pub(all) struct ChandraCompositor { /// Preview image handles of closed windows, reused for the next ones. thumb_free : Array[UInt] mut alt_down : Bool + /// The window overview (overview.mbt) while it is on screen, and whether the launcher was open + /// when last looked at (opening it opens the overview). + mut overview : Overview? + mut launcher_was_open : Bool /// Milliseconds spent so far in the shell (panel modules) and in the rasteriser, for --profile. /// The last composed frame and what it was made from: an unchanged desktop is not redrawn. mut last_frame : @renderer.Pixmap? @@ -234,6 +238,8 @@ pub fn ChandraCompositor::new( switcher_on: false, switcher: [], switcher_sel: 0, + overview: None, + launcher_was_open: false, thumbs: {}, thumb_free: [], alt_down: false, @@ -634,6 +640,7 @@ pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool { self.shot_toast is Some(_) || self.auth_animating() || self.thumbs_pending() || + self.overview_animating() || self.shell.animating() } @@ -673,10 +680,13 @@ fn ChandraCompositor::build_scene( self : ChandraCompositor, ) -> (@scene.SceneGraph, Bool) { self.flush_configures() + self.overview_sync() let scene = @scene.SceneGraph::new(self.output_w, self.output_h) let order = self.wm.stacking_order() + // The overview draws the windows itself, as previews. + let shown = if self.overview is Some(_) { [] } else { order } - for wid in order { + for wid in shown { match self.windows.get(wid) { Some(win) => { let (insets, deco_ops) = @scene.build_breeze_decoration( @@ -707,7 +717,7 @@ fn ChandraCompositor::build_scene( // Client menus would draw above the auth dialog: none while it is up. for p in (if self.auth is Some(_) { [] } else { self.popups }) { match p.surface { - Some(snap) => scene.popups.push((self.popup_rect(p), snap)) + Some(snap) => if self.overview is None { scene.popups.push((self.popup_rect(p), snap)) } None => () } } @@ -760,7 +770,11 @@ fn ChandraCompositor::build_scene( self.shell.screenshot_state = (if self.rec is Some(_) { 1 } else { 0 }) | (if self.rec_pointer { 2 } else { 0 }) let t0 = @sys.monotonic_ms() - let (bg_ops, panel_ops) = self.shell.render(self.output_w, self.output_h, self.theme) + let (bg_ops, shell_ops) = self.shell.render(self.output_w, self.output_h, self.theme) + // The overview goes under the panel and the launcher. + self.overview_sync() + let panel_ops = self.overview_ops() + panel_ops.append(shell_ops) if self.switcher_on { panel_ops.append(self.switcher_ops()) } @@ -1384,6 +1398,9 @@ pub fn ChandraCompositor::handle_input( self.shell.set_hover(id, xi, yi) return } + if self.overview_motion(xi, yi) { + return + } if self.title_bar_motion(xi, yi) { return } @@ -1441,6 +1458,9 @@ pub fn ChandraCompositor::handle_input( } None => () } + if self.overview is Some(_) { + return // nothing scrolls under the overview + } if axis == 0U && self.shell.inspecting() { // ~10 units per wheel notch -> 3 lines. let lines = if v > 0.0 { (v / 10.0).to_int() + 1 } else { (v / 10.0).to_int() - 1 } @@ -1497,6 +1517,9 @@ pub fn ChandraCompositor::handle_input( } return } + if self.overview_button(button, pressed, xi, yi) { + return + } if !pressed && button == 272U { self.title_bar_release() } @@ -1603,6 +1626,8 @@ pub fn ChandraCompositor::handle_input( self.super_solo = false self.super_tap() } + } else if self.overview_key(code, pressed) { + self.super_solo = false } else if self.shell.inspecting() { self.super_solo = false // Escape leaves inspect mode; other keys are not forwarded while inspecting. diff --git a/compositor/host/control.mbt b/compositor/host/control.mbt index 282c4ac..b01993c 100644 --- a/compositor/host/control.mbt +++ b/compositor/host/control.mbt @@ -208,6 +208,7 @@ pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : Strin [ "ping check the compositor answers", "list loaded panel modules: slot, module, path", "reload load every panel module again from disk", "reload NAME only the modules whose name, slot or path is NAME", + "shortcut ID do what shortcut ID does (launcher, overview, show-desktop, ...)", "ok", ], true, @@ -237,6 +238,22 @@ pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : Strin } (out, ok) } + "shortcut" => { + if words.length() != 2 { + return (["error usage: shortcut ID"], false) + } + for d in @shell.shortcut_table { + if d.id == words[1] { + if d.action == SwitchWindows { + return (["error switch-windows needs its keys held"], false) + } + self.run_shortcut(d.action) + self.damage.mark_full() + return (["ok " + d.id], true) + } + } + (["error no shortcut '" + words[1] + "'"], false) + } other => (["error unknown command '" + other + "' (try: help)"], false) } } diff --git a/compositor/host/control_test.mbt b/compositor/host/control_test.mbt index 4f34f85..0e7384a 100644 --- a/compositor/host/control_test.mbt +++ b/compositor/host/control_test.mbt @@ -70,6 +70,13 @@ test "ping, list and errors over the control socket" { assert_eq(ask(comp, path, "reload nope"), ["error no loaded module named 'nope'"]) assert_eq(ask(comp, path, " "), ["error empty command (try: help)"]) assert_eq(ask(comp, path, "help").last(), Some("ok")) + // Shortcuts by id: the overview opens and closes. + assert_eq(ask(comp, path, "shortcut overview"), ["ok overview"]) + assert_true(comp.overview_open()) + assert_eq(ask(comp, path, "shortcut overview"), ["ok overview"]) + assert_false(comp.overview_open()) + assert_eq(ask(comp, path, "shortcut nope"), ["error no shortcut 'nope'"]) + assert_eq(ask(comp, path, "shortcut"), ["error usage: shortcut ID"]) // Several commands on one connection get one reply each, in order. let both = ask(comp, path, "ping\nping") assert_eq(both, ["ok pong", "ok pong"]) diff --git a/compositor/host/overview.mbt b/compositor/host/overview.mbt new file mode 100644 index 0000000..7011fae --- /dev/null +++ b/compositor/host/overview.mbt @@ -0,0 +1,743 @@ +///| Window overview (Exposé), like GNOME's Activities overview and Plasma's Present Windows. +/// +/// Opening the launcher (a Super tap, or its panel button) also shows every window of the +/// active workspace side by side, scaled down with their aspect ratios kept, beside the launcher's +/// popup over the dimmed wallpaper; minimized windows are there too, dimmed. The "overview" +/// shortcut (Super+W by default) shows it alone. The windows glide from where they are into their +/// places (and back when it closes), in `overview_ms`. +/// +/// Not while the Interface Explorer is inspecting. +/// +/// Hovering a window lights it up and shows a close button on its corner. Clicking a window +/// brings it to the front (restoring it if minimized) and closes the overview and the launcher; +/// a middle click or the close button closes that window; Escape or a click on empty space closes +/// the overview (and the launcher). Typing in the launcher's search also filters the windows: +/// those whose title or app id don't match fade out. +/// +/// The previews are the Alt+Tab switcher's (switcher.mbt: `switcher_thumb`), made only while +/// the overview is up and remade at most every `THUMB_REFRESH_MS` for windows that keep drawing. +/// Nothing is redrawn for the overview once it settles unless a window draws or the pointer +/// moves onto another window. + +///| How long the overview takes to open or close, in ms. +pub let overview_ms : UInt64 = 220 + +///| Space kept free around the windows, and between them. +let overview_margin : Int = 32 + +///| +let overview_gap : Int = 24 + +///| Height of the title below each window. +let overview_label_h : Int = 30 + +///| Size of the close button shown on the hovered window's corner. +let overview_close_size : Int = 26 + +///| The overview while it is on screen (`ChandraCompositor.overview`). +pub(all) struct Overview { + /// Opened with the launcher (closes with it) or alone, by its shortcut. + mut with_launcher : Bool + /// Opening (true) or closing (false), and since when (monotonic ms). + mut opening : Bool + mut t0 : UInt64 + /// The window under the pointer (0 for none) and whether the pointer is on its close button. + mut hover : UInt64 + mut hover_close : Bool + /// The launcher's search text, as typed (it filters the windows). + mut query : String +} + +///| How far along a transition of `ms` that started at `t0` is at `now`, 0.0 .. 1.0. +pub fn overview_progress(t0 : UInt64, now : UInt64, ms : UInt64) -> Double { + if now <= t0 || ms == 0UL { + return if now < t0 { 0.0 } else if ms == 0UL { 1.0 } else { 0.0 } + } + let p = (now - t0).to_double() / ms.to_double() + if p > 1.0 { + 1.0 + } else { + p + } +} + +///| How open the overview looks at progress `p` (0 = windows where they are, 1 = in their +/// places): opening eases out (fast, then settling), closing eases in and out. +pub fn overview_openness(opening : Bool, p : Double) -> Double { + if opening { + let q = 1.0 - p + 1.0 - q * q * q + } else { + let e = if p < 0.5 { + 4.0 * p * p * p + } else { + let q = -2.0 * p + 2.0 + 1.0 - q * q * q / 2.0 + } + 1.0 - e + } +} + +///| `a` moved `t` of the way to `b`. +pub fn lerp_rect(a : @scene.Rect, b : @scene.Rect, t : Double) -> @scene.Rect { + let f = fn(x : Int, y : Int) { x + ((y - x).to_double() * t).round().to_int() } + @scene.Rect::new(f(a.x, b.x), f(a.y, b.y), f(a.w, b.w), f(a.h, b.h)) +} + +///| Places for windows of sizes `sizes` (in that order) inside `area`: rows of windows, each +/// scaled by the same factor with its aspect ratio kept, rows and windows `gap` apart (rows +/// `row_gap`), centred. The number of rows that lets the windows be largest wins; windows are +/// never shown larger than they are. +pub fn overview_layout( + sizes : Array[(Int, Int)], + area : @scene.Rect, + gap : Int, + row_gap : Int, +) -> Array[@scene.Rect] { + let n = sizes.length() + if n == 0 || area.w <= 0 || area.h <= 0 { + return [] + } + let ws = sizes.map(s => if s.0 < 1 { 1 } else { s.0 }) + let hs = sizes.map(s => if s.1 < 1 { 1 } else { s.1 }) + let mut best_rows : Array[Array[Int]] = [] + let mut best_scale = -1.0 + for nrows = 1; nrows <= n; nrows = nrows + 1 { + let rows = overview_rows(ws, nrows) + let s = overview_scale(rows, ws, hs, area, gap, row_gap) + // More rows only when it makes the windows noticeably larger. + if s > best_scale * 1.05 || best_scale < 0.0 { + best_scale = s + best_rows = rows + } + } + let s = if best_scale > 1.0 { 1.0 } else if best_scale <= 0.0 { 0.01 } else { best_scale } + let out = Array::make(n, @scene.Rect::new(0, 0, 0, 0)) + let row_hs = best_rows.map(r => r.fold(init=0, (m, i) => if hs[i] > m { hs[i] } else { m })) + let total_h = row_hs.fold(init=0, (a, h) => a + scaled(h, s)) + row_gap * (best_rows.length() - 1) + let mut y = area.y + (area.h - total_h) / 2 + for ri, row in best_rows { + let rh = scaled(row_hs[ri], s) + let rw = row.fold(init=0, (a, i) => a + scaled(ws[i], s)) + gap * (row.length() - 1) + let mut x = area.x + (area.w - rw) / 2 + for i in row { + let w = scaled(ws[i], s) + let h = scaled(hs[i], s) + out[i] = @scene.Rect::new(x, y + (rh - h) / 2, w, h) + x = x + w + gap + } + y = y + rh + row_gap + } + out +} + +///| +fn scaled(v : Int, s : Double) -> Int { + let r = (v.to_double() * s).to_int() + if r < 1 { + 1 + } else { + r + } +} + +///| Windows (indexes, in order) split into `nrows` rows of about equal total width. +fn overview_rows(ws : Array[Int], nrows : Int) -> Array[Array[Int]] { + let total = ws.fold(init=0, (a, w) => a + w) + let target = total.to_double() / nrows.to_double() + let rows : Array[Array[Int]] = [] + let mut cur : Array[Int] = [] + let mut cur_w = 0 + for i, w in ws { + let left = ws.length() - i // windows still to place, this one included + let rows_left = nrows - rows.length() - 1 // rows after the current one + // Start a new row when this window takes the row further past the target than it is + // short of it, as long as rows remain, and always when every remaining row needs a window. + let over = (cur_w + w).to_double() - target + let under = target - cur_w.to_double() + if cur.length() > 0 && rows_left > 0 && (over > under || left <= rows_left) { + rows.push(cur) + cur = [] + cur_w = 0 + } + cur.push(i) + cur_w = cur_w + w + } + if cur.length() > 0 { + rows.push(cur) + } + rows +} + +///| The largest factor rows `rows` can be scaled by to fit in `area`. +fn overview_scale( + rows : Array[Array[Int]], + ws : Array[Int], + hs : Array[Int], + area : @scene.Rect, + gap : Int, + row_gap : Int, +) -> Double { + let mut s = 1000.0 + let mut sum_h = 0 + for row in rows { + let w = row.fold(init=0, (a, i) => a + ws[i]) + let room = area.w - gap * (row.length() - 1) + let sw = room.to_double() / w.to_double() + if sw < s { + s = sw + } + sum_h = sum_h + row.fold(init=0, (m, i) => if hs[i] > m { hs[i] } else { m }) + } + let room_h = area.h - row_gap * (rows.length() - 1) + let sh = room_h.to_double() / sum_h.to_double() + if sh < s { + s = sh + } + s +} + +///| The largest part of `area` that `hole` leaves free: what is left of it, right of it, above +/// or below it (all of `area` when they don't overlap). +pub fn overview_area_beside(area : @scene.Rect, hole : @scene.Rect?) -> @scene.Rect { + let h = match hole { + Some(h) => h + None => return area + } + let ax1 = area.x + area.w + let ay1 = area.y + area.h + let hx1 = h.x + h.w + let hy1 = h.y + h.h + if h.w <= 0 || h.h <= 0 || hx1 <= area.x || h.x >= ax1 || hy1 <= area.y || h.y >= ay1 { + return area + } + let parts = [ + @scene.Rect::new(area.x, area.y, h.x - area.x, area.h), // left + @scene.Rect::new(hx1, area.y, ax1 - hx1, area.h), // right + @scene.Rect::new(area.x, area.y, area.w, h.y - area.y), // above + @scene.Rect::new(area.x, hy1, area.w, ay1 - hy1), // below + ] + let mut best = @scene.Rect::new(area.x, area.y, 0, 0) + for p in parts { + if p.w > 0 && p.h > 0 && p.w * p.h > best.w * best.h { + best = p + } + } + best +} + +///| The index of the rect in `rects` containing (x, y), or -1 (the last one wins on overlap). +pub fn overview_hit(rects : Array[@scene.Rect], x : Int, y : Int) -> Int { + let mut i = rects.length() - 1 + while i >= 0 { + let r = rects[i] + if x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h { + return i + } + i = i - 1 + } + -1 +} + +///| The close button on a window shown at `r`: a square on its top-right corner. +pub fn overview_close_rect(r : @scene.Rect) -> @scene.Rect { + let s = overview_close_size + @scene.Rect::new(r.x + r.w - s * 2 / 3, r.y - s / 3, s, s) +} + +///| True when the window's title or app id contains `query` (any case); everything matches an +/// empty query. +pub fn overview_matches(title : String, app_id : String, query : String) -> Bool { + let q = query.trim().to_string().to_lower() + q.length() == 0 || title.to_lower().contains(q) || app_id.to_lower().contains(q) +} + +///| True while the overview is on screen (opening, open or closing). +pub fn ChandraCompositor::overview_shown(self : ChandraCompositor) -> Bool { + self.overview is Some(_) +} + +///| True while the overview is open or opening (not on its way out). +pub fn ChandraCompositor::overview_open(self : ChandraCompositor) -> Bool { + match self.overview { + Some(o) => o.opening + None => false + } +} + +///| True while the overview's windows are moving (the main loop keeps its frame pace). +fn ChandraCompositor::overview_animating(self : ChandraCompositor) -> Bool { + match self.overview { + Some(o) => overview_progress(o.t0, @sys.monotonic_ms(), overview_ms) < 1.0 + None => false + } +} + +///| Open the overview; `with_launcher` when the launcher opened it (it then closes with it). +pub fn ChandraCompositor::open_overview(self : ChandraCompositor, with_launcher : Bool) -> Unit { + match self.overview { + Some(o) => { + o.with_launcher = o.with_launcher || with_launcher + if !o.opening { + o.opening = true + o.t0 = turn_around(o.t0) + } + } + None => { + // Client menus would float over nothing: close them. + let mut i = self.popups.length() - 1 + while i >= 0 { + self.server.popup_done(self.popups[i].id) + i = i - 1 + } + self.leave_pointer() + self.overview = Some({ + with_launcher, + opening: true, + t0: @sys.monotonic_ms(), + hover: 0UL, + hover_close: false, + query: "", + }) + self.overview_hover_at(self.ptr_x.to_int(), self.ptr_y.to_int()) + } + } + self.damage.mark_full() +} + +///| A transition that started at `t0` turns around now: the new one starts as far along as the +/// old one still had to go. +fn turn_around(t0 : UInt64) -> UInt64 { + let now = @sys.monotonic_ms() + let p = overview_progress(t0, now, overview_ms) + let left = ((1.0 - p) * overview_ms.to_double()).to_uint64() + if now > left { + now - left + } else { + 1UL + } +} + +///| Close the overview (the windows glide back), and the launcher with it. +pub fn ChandraCompositor::close_overview(self : ChandraCompositor) -> Unit { + match self.overview { + Some(o) => + if o.opening { + o.opening = false + o.t0 = turn_around(o.t0) + o.hover = 0UL + o.hover_close = false + self.damage.mark_full() + } + None => () + } + if self.shell.launcher_open() { + self.shell.close_launcher() + } +} + +///| The overview shortcut: open it alone, or close it. +pub fn ChandraCompositor::toggle_overview(self : ChandraCompositor) -> Unit { + if self.overview_open() { + self.close_overview() + } else { + self.open_overview(false) + } +} + +///| Keep the overview in step with the launcher (opening it opens the overview; closing it, by +/// any means, closes the overview it opened) and drop it once it has finished closing. +fn ChandraCompositor::overview_sync(self : ChandraCompositor) -> Unit { + let launcher = self.shell.launcher_open() + if self.shell.inspecting() { + // The Interface Explorer looks at the launcher as it is; no overview over it. + if self.overview is Some(_) { + self.overview = None + self.damage.mark_full() + } + self.launcher_was_open = launcher + return + } + if launcher && !self.launcher_was_open { + self.open_overview(true) + } else if !launcher && self.launcher_was_open { + match self.overview { + Some(o) if o.with_launcher && o.opening => self.close_overview() + _ => () + } + } + self.launcher_was_open = launcher + if self.switcher_on && self.overview is Some(_) { + self.overview = None // Alt+Tab takes over + self.damage.mark_full() + } + match self.overview { + Some(o) => + if !o.opening && overview_progress(o.t0, @sys.monotonic_ms(), overview_ms) >= 1.0 { + self.overview = None + self.damage.mark_full() + } + None => () + } +} + +///| The windows the overview shows: those of the active workspace, visible ones in stacking order +/// (bottom to top), then minimized ones. +fn ChandraCompositor::overview_windows(self : ChandraCompositor) -> Array[UInt64] { + let out : Array[UInt64] = [] + for id in self.wm.stacking_order() { + if self.windows.contains(id) { + out.push(id) + } + } + for id in self.switcher_candidates() { + if !out.contains(id) { + out.push(id) + } + } + out +} + +///| Where the overview puts the windows: inside the work area, beside the launcher's popup. +fn ChandraCompositor::overview_area(self : ChandraCompositor) -> @scene.Rect { + let wa = self.work_area + let m = overview_margin + let area = @scene.Rect::new(wa.x + m, wa.y + m, wa.w - m * 2, wa.h - m * 2 - overview_label_h) + let hole = match self.shell.launcher_rect() { + Some(r) => Some(@scene.Rect::new(r.x - m, r.y - m, r.w + m * 2, r.h + m * 2)) + None => None + } + let beside = overview_area_beside(area, hole) + // Too small a space beside the launcher: use the whole area (the launcher stays on top). + if beside.w < 160 || beside.h < 120 { + area + } else { + beside + } +} + +///| One window in the overview: where it is on the desktop (`home`) and where the overview +/// puts it (`slot`). +struct OverviewItem { + id : UInt64 + home : @scene.Rect + slot : @scene.Rect + minimized : Bool +} + +///| The overview's windows and their places. Windows are laid out in the order they stand on +/// the desktop (top to bottom, left to right), so each lands near where it was. +fn ChandraCompositor::overview_items(self : ChandraCompositor) -> Array[OverviewItem] { + let ids = self.overview_windows() + let homes : Array[(UInt64, @scene.Rect, Bool)] = [] + for id in ids { + match self.windows.get(id) { + Some(win) => { + let m = match self.wm.windows.get(id) { + Some(r) => r.minimized + None => false + } + homes.push((id, win.geometry, m)) + } + None => () + } + } + // Reading order of the window centres (minimized ones last). + let order = Array::makei(homes.length(), i => i) + order.sort_by((a, b) => { + let (_, ra, ma) = homes[a] + let (_, rb, mb) = homes[b] + if ma != mb { + return if ma { 1 } else { -1 } + } + let band = 160 + let ya = (ra.y + ra.h / 2) / band + let yb = (rb.y + rb.h / 2) / band + if ya != yb { + ya - yb + } else { + ra.x + ra.w / 2 - (rb.x + rb.w / 2) + } + }) + let sizes = overview_balance(order.map(i => (homes[i].1.w, homes[i].1.h))) + let slots = overview_layout(sizes, self.overview_area(), overview_gap, overview_gap + overview_label_h) + let by_index : Map[Int, @scene.Rect] = {} + for k, i in order { + by_index.set(i, slots[k]) + } + // Minimized windows come out of (and go back to) the bottom middle of the screen. + let dock = @scene.Rect::new(self.output_w / 2 - 40, self.output_h - 40, 80, 40) + let items : Array[OverviewItem] = [] + for i, h in homes { + let (id, r, m) = h + let slot = by_index.get(i).unwrap_or(r) + items.push({ id, home: if m { dock } else { r }, slot, minimized: m }) + } + items +} + +///| Where each window is shown in the overview once it has settled (for tests and tools). +pub fn ChandraCompositor::overview_places(self : ChandraCompositor) -> Array[(UInt64, @scene.Rect)] { + if self.overview is None { + return [] + } + self.overview_items().map(it => (it.id, it.slot)) +} + +///| The window under the pointer in the overview (0 for none), and the search text it filters by. +pub fn ChandraCompositor::overview_hover(self : ChandraCompositor) -> (UInt64, String) { + match self.overview { + Some(o) => (o.hover, o.query) + None => (0UL, "") + } +} + +///| Sizes to lay windows out by: windows less than half as tall as the tallest are counted as +/// half its height (aspect ratio kept), so a small dialog next to a maximized window isn't lost. +pub fn overview_balance(sizes : Array[(Int, Int)]) -> Array[(Int, Int)] { + let max_h = sizes.fold(init=0, (m, s) => if s.1 > m { s.1 } else { m }) + sizes.map(s => { + let (w, h) = s + if h > 0 && h * 2 < max_h { + (w * max_h / (h * 2), max_h / 2) + } else { + (w, h) + } + }) +} + +///| The window shown at (x, y) in the open overview (0 for none) and whether (x, y) is on its +/// close button. +fn ChandraCompositor::overview_target(self : ChandraCompositor, x : Int, y : Int) -> (UInt64, Bool) { + let o = match self.overview { + Some(o) if o.opening => o + _ => return (0UL, false) + } + if self.shell.hit(x, y) { + return (0UL, false) + } + let items = self.overview_items() + // The hovered window's close button sticks out of its corner. + for it in items { + if it.id == o.hover && inside_rect(overview_close_rect(it.slot), x, y) { + return (it.id, true) + } + } + let i = overview_hit(items.map(it => it.slot), x, y) + if i < 0 { + (0UL, false) + } else { + (items[i].id, false) + } +} + +///| `w`x`h` scaled (up or down) to fit `box_w`x`box_h` with its aspect ratio kept. +pub fn fit_within(w : Int, h : Int, box_w : Int, box_h : Int) -> (Int, Int) { + if w <= 0 || h <= 0 || box_w <= 0 || box_h <= 0 { + return (0, 0) + } + if w * box_h >= h * box_w { + let fh = h * box_w / w + (box_w, if fh < 1 { 1 } else { fh }) + } else { + let fw = w * box_h / h + (if fw < 1 { 1 } else { fw }, box_h) + } +} + +///| +fn inside_rect(r : @scene.Rect, x : Int, y : Int) -> Bool { + x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h +} + +///| Track the window under the pointer. +fn ChandraCompositor::overview_hover_at(self : ChandraCompositor, x : Int, y : Int) -> Unit { + let (id, close) = self.overview_target(x, y) + match self.overview { + Some(o) => + if o.hover != id || o.hover_close != close { + o.hover = id + o.hover_close = close + self.damage.mark_full() + } + None => () + } +} + +///| Pointer motion while the overview is up: hover its windows (the shell still sees motion over +/// the panel and the launcher). True when the overview took it (windows don't see it). +fn ChandraCompositor::overview_motion(self : ChandraCompositor, x : Int, y : Int) -> Bool { + self.overview_sync() + if self.overview is None { + return false + } + ignore(self.shell.pointer_move(x, y)) + self.leave_pointer() + self.overview_hover_at(x, y) + true +} + +///| A button while the overview is up. Presses over the panel or the launcher go to the shell; +/// elsewhere: left on a window picks it, middle on it (or left on its close button) closes it, +/// anything on empty space closes the overview. True when the event was used here. +fn ChandraCompositor::overview_button(self : ChandraCompositor, button : UInt, pressed : Bool, x : Int, y : Int) -> Bool { + self.overview_sync() + if self.overview is None { + return false + } + // Releases, and presses on the panel or the launcher, take the usual way (the launcher's + // right-click menu, the panel's buttons); what they open or close is picked up next frame. + if !pressed || self.shell.hit(x, y) { + return false + } + if !self.overview_open() { + return true // on its way out + } + let (id, close) = self.overview_target(x, y) + if id == 0UL { + self.close_overview() + } else if button == 274U || (button == 272U && close) { + self.window_command(id, @shell.WindowCommand::Close) + } else if button == 272U { + self.activate_task(id) + self.close_overview() + } + self.overview_sync() + true +} + +///| A key while the overview is up. With the launcher, keys go on to its search (the search +/// text is followed here to filter the windows); alone, Escape closes it, Enter picks the window +/// under the pointer, and no key reaches a client. True when the key was used here. +fn ChandraCompositor::overview_key(self : ChandraCompositor, code : UInt, pressed : Bool) -> Bool { + self.overview_sync() + let o = match self.overview { + Some(o) => o + None => return false + } + if o.with_launcher && self.shell.launcher_open() { + if pressed { + let name = key_name(code, (self.mods & 1U) != 0U) + if name == "Backspace" { + if o.query.length() > 0 { + o.query = o.query[:o.query.length() - 1].to_string() + } + } else if name == "Escape" || name == "Enter" { + o.query = "" + } else if name.length() == 1 { + o.query = o.query + name + } + self.damage.mark_full() + } + return false // the launcher gets it + } + if pressed && o.opening { + if code == key_esc { + self.close_overview() + } else if code == 28U && o.hover != 0UL { + self.activate_task(o.hover) + self.close_overview() + } + } + true +} + +///| The overview's draw ops (under the panel and the launcher): the wallpaper dimmed, then each +/// window's preview gliding between its place on the desktop and its place in the overview, +/// with its icon and title, the hovered one lit with a close button. +fn ChandraCompositor::overview_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] { + let ops : Array[@scene.DrawOp] = [] + let o = match self.overview { + Some(o) => o + None => return ops + } + let p = self.theme.palette + let fs = self.theme.font_size + let now = @sys.monotonic_ms() + let t = overview_openness(o.opening, overview_progress(o.t0, now, overview_ms)) + let settled = t >= 1.0 + ops.push( + @scene.DrawOp::FillRect( + @scene.Rect::new(0, 0, self.output_w, self.output_h), + @scene.Rgba::new(0, 0, 0, (t * 140.0).to_int()), + 0, + ), + ) + for it in self.overview_items() { + let win = match self.windows.get(it.id) { + Some(w) => w + None => continue + } + let title = if win.title.length() > 0 { win.title } else { win.app_id } + let matches = overview_matches(win.title, win.app_id, o.query) + let r = lerp_rect(it.home, it.slot, t) + if r.w <= 0 || r.h <= 0 { + continue + } + // Previews are made at most 960 px a side (both hosts' limit is 1024) and stretched past it. + let (thumb, tw, th) = self.switcher_thumb( + it.id, + if it.slot.w > 960 { 960 } else { it.slot.w }, + if it.slot.h > 960 { 960 } else { it.slot.h }, + ) + let hovered = settled && o.hover == it.id && matches + if hovered { + ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x - 5, r.y - 5, r.w + 10, r.h + 10), p.accent, self.theme.radius + 4)) + } + if thumb != 0U { + // The preview is made at the slot's size; while gliding it is stretched to `r`. + let ir = if settled { + let (fw, fh) = fit_within(tw, th, r.w, r.h) + @scene.Rect::new(r.x + (r.w - fw) / 2, r.y + (r.h - fh) / 2, fw, fh) + } else { + r + } + ops.push(@scene.DrawOp::Image(ir, thumb)) + } else { + ops.push(@scene.DrawOp::FillRect(r, p.surface, self.theme.radius)) + } + ops.push(@scene.DrawOp::StrokeRect(r, p.border, 1, 0)) + if it.minimized { + ops.push(@scene.DrawOp::FillRect(r, @scene.Rgba::new(0, 0, 0, 120), 0)) + } + if !matches { + // Filtered out by the search: faded. + ops.push(@scene.DrawOp::FillRect(r, @scene.Rgba::new(0, 0, 0, 170), 0)) + } + if !settled { + continue + } + // The app icon on the bottom edge, the title below. + let icon = self.shell.window_icon(win.app_id) + let isz = if r.h / 3 < 40 { r.h / 3 } else { 40 } + if icon != 0U && isz >= 12 && matches { + ops.push(@scene.DrawOp::Image(@scene.Rect::new(r.x + (r.w - isz) / 2, r.y + r.h - isz * 3 / 4, isz, isz), icon)) + } + if it.minimized && matches { + ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, "minimized", fs - 1, @scene.Rgba::new(255, 255, 255, 230), @scene.TextAlign::Center, true)) + } + let cw = (fs * 11 + 19) / 20 + let max_chars = (if r.w + 40 > 120 { r.w + 40 } else { 120 }) / cw + let shown = if title.length() > max_chars && max_chars > 1 { title[:max_chars - 1].to_string() + "…" } else { title } + let lw = shown.length() * cw + 20 + let ly = r.y + r.h + 12 + (overview_label_h - 12) / 2 + let lrect = @scene.Rect::new(r.x + (r.w - lw) / 2, ly - 11, lw, 22) + if matches { + ops.push(@scene.DrawOp::FillRect(lrect, if hovered { p.accent } else { p.surface_popup }, 11)) + ops.push(@scene.DrawOp::Text(r.x + r.w / 2, ly, shown, fs, if hovered { p.accent_foreground } else { p.foreground }, @scene.TextAlign::Center, hovered)) + } + if hovered { + let c = overview_close_rect(r) + let lit = o.hover_close + ops.push(@scene.DrawOp::Circle(c.x + c.w / 2, c.y + c.h / 2, c.w / 2, if lit { @scene.Rgba::new(220, 60, 60, 255) } else { p.surface_popup }, true)) + ops.push(@scene.DrawOp::Circle(c.x + c.w / 2, c.y + c.h / 2, c.w / 2, p.border, false)) + let cx = c.x + c.w / 2 + let cy = c.y + c.h / 2 + let d = c.w / 5 + let fg = if lit { @scene.Rgba::new(255, 255, 255, 255) } else { p.foreground } + ops.push(@scene.DrawOp::Line(cx - d, cy - d, cx + d, cy + d, fg, 2)) + ops.push(@scene.DrawOp::Line(cx - d, cy + d, cx + d, cy - d, fg, 2)) + } + } + ops +} diff --git a/compositor/host/overview_test.mbt b/compositor/host/overview_test.mbt new file mode 100644 index 0000000..90a8f60 --- /dev/null +++ b/compositor/host/overview_test.mbt @@ -0,0 +1,437 @@ +///| +fn ov_rect(x : Int, y : Int, w : Int, h : Int) -> @scene.Rect { + @scene.Rect::new(x, y, w, h) +} + +///| +fn ov_inside(r : @scene.Rect, a : @scene.Rect) -> Bool { + r.x >= a.x && r.y >= a.y && r.x + r.w <= a.x + a.w && r.y + r.h <= a.y + a.h +} + +///| +fn ov_overlap(a : @scene.Rect, b : @scene.Rect) -> Bool { + a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h +} + +///| Every place lies inside the area, none overlap, and each keeps its window's aspect ratio. +fn check_layout(sizes : Array[(Int, Int)], area : @scene.Rect) -> Array[@scene.Rect] raise { + let out = @host.overview_layout(sizes, area, 24, 54) + assert_eq(out.length(), sizes.length()) + for i, r in out { + assert_true(ov_inside(r, area)) + let (w, h) = sizes[i] + // Aspect ratio kept to within a pixel of rounding. + assert_true((r.w * h - r.h * w).abs() <= w + h) + // Never enlarged. + assert_true(r.w <= w && r.h <= h) + for j = i + 1; j < out.length(); j = j + 1 { + assert_false(ov_overlap(r, out[j])) + } + } + out +} + +///| +test "overview layout: one window is centred and never enlarged" { + let area = ov_rect(32, 32, 1216, 600) + let out = check_layout([(400, 300)], area) + assert_eq(out[0], ov_rect(32 + (1216 - 400) / 2, 32 + (600 - 300) / 2, 400, 300)) + // A big one shrinks to fit, centred. + let big = check_layout([(1920, 1080)], area) + assert_eq(big[0].h, 600) + assert_eq(big[0].x - 32, 1216 - big[0].x - big[0].w + 32 - (1216 - 1216)) +} + +///| +test "overview layout: 4 and 9 equal windows make 2x2 and 3x3 grids" { + let area = ov_rect(0, 0, 1200, 700) + let four = check_layout(Array::make(4, (800, 500)), area) + let rows4 = four.map(r => r.y).fold(init=[], (acc : Array[Int], y) => if acc.contains(y) { acc } else { acc + [y] }) + assert_eq(rows4.length(), 2) + let nine = check_layout(Array::make(9, (800, 500)), area) + let rows9 = nine.map(r => r.y).fold(init=[], (acc : Array[Int], y) => if acc.contains(y) { acc } else { acc + [y] }) + assert_eq(rows9.length(), 3) + // Same-sized windows get the same size. + assert_true(nine.iter().all(r => r.w == nine[0].w && r.h == nine[0].h)) +} + +///| +test "overview layout: mixed aspect ratios, many windows, tiny areas" { + let area = ov_rect(10, 20, 900, 500) + ignore(check_layout([(1280, 720), (600, 900), (300, 300), (1000, 200), (700, 700)], area)) + ignore(check_layout(Array::makei(20, i => (300 + i * 37, 200 + i * 23)), area)) + // Tall and wide side by side: wide ones aren't squeezed into a row of tall ones. + ignore(check_layout([(200, 1000), (1600, 200)], ov_rect(0, 0, 800, 600))) + assert_eq(@host.overview_layout([], area, 24, 54), []) + assert_eq(@host.overview_layout([(100, 100)], ov_rect(0, 0, 0, 0), 24, 54), []) +} + +///| +test "overview balance: small windows count as half the tallest" { + assert_eq(@host.overview_balance([(1000, 800), (200, 100), (500, 500)]), [(1000, 800), (800, 400), (500, 500)]) + assert_eq(@host.overview_balance([]), []) +} + +///| +test "overview area beside the launcher takes the largest free side" { + let area = ov_rect(0, 0, 1000, 600) + assert_eq(@host.overview_area_beside(area, None), area) + // Launcher popup in the bottom-left corner: the right side is the largest. + assert_eq(@host.overview_area_beside(area, Some(ov_rect(0, 200, 300, 400))), ov_rect(300, 0, 700, 600)) + // Across the bottom: above it. + assert_eq(@host.overview_area_beside(area, Some(ov_rect(0, 400, 1000, 200))), ov_rect(0, 0, 1000, 400)) + // Not overlapping: the whole area. + assert_eq(@host.overview_area_beside(area, Some(ov_rect(2000, 0, 10, 10))), area) +} + +///| +test "overview hit testing and the close button" { + let rects = [ov_rect(0, 0, 100, 100), ov_rect(50, 50, 100, 100)] + assert_eq(@host.overview_hit(rects, 10, 10), 0) + assert_eq(@host.overview_hit(rects, 60, 60), 1) // the later one is on top + assert_eq(@host.overview_hit(rects, 149, 149), 1) + assert_eq(@host.overview_hit(rects, 150, 150), -1) + assert_eq(@host.overview_hit(rects, -1, 5), -1) + let c = @host.overview_close_rect(ov_rect(100, 100, 200, 150)) + // On the top-right corner, sticking out a little. + assert_true(c.x < 300 && c.x + c.w > 300) + assert_true(c.y < 100 && c.y + c.h > 100) +} + +///| +test "overview animation: progress, easing, turning around" { + assert_eq(@host.overview_progress(100UL, 100UL, 200UL), 0.0) + assert_eq(@host.overview_progress(100UL, 200UL, 200UL), 0.5) + assert_eq(@host.overview_progress(100UL, 900UL, 200UL), 1.0) + assert_eq(@host.overview_openness(true, 0.0), 0.0) + assert_eq(@host.overview_openness(true, 1.0), 1.0) + assert_eq(@host.overview_openness(false, 0.0), 1.0) + assert_eq(@host.overview_openness(false, 1.0), 0.0) + // Opening eases out: past half way at the half-time mark; monotonic both ways. + assert_true(@host.overview_openness(true, 0.5) > 0.8) + let mut prev_in = -1.0 + let mut prev_out = 2.0 + for i = 0; i <= 20; i = i + 1 { + let p = i.to_double() / 20.0 + let a = @host.overview_openness(true, p) + let b = @host.overview_openness(false, p) + assert_true(a >= prev_in && b <= prev_out) + prev_in = a + prev_out = b + } + let a = ov_rect(0, 0, 100, 100) + let b = ov_rect(100, 50, 50, 20) + assert_eq(@host.lerp_rect(a, b, 0.0), a) + assert_eq(@host.lerp_rect(a, b, 1.0), b) + assert_eq(@host.lerp_rect(a, b, 0.5), ov_rect(50, 25, 75, 60)) + assert_eq(@host.fit_within(200, 100, 100, 100), (100, 50)) + assert_eq(@host.fit_within(50, 100, 300, 300), (150, 300)) +} + +///| +test "overview search matches title or app id, any case" { + assert_true(@host.overview_matches("Firefox — News", "org.mozilla.firefox", "")) + assert_true(@host.overview_matches("Firefox — News", "org.mozilla.firefox", "NEWS")) + assert_true(@host.overview_matches("Terminal", "org.kde.konsole", "konsole")) + assert_false(@host.overview_matches("Terminal", "org.kde.konsole", "firefox")) +} + +///| Commit a frame filled with `rgb` (a pale bar across its top, like a toolbar). +fn commit_color_frame( + comp : @host.ChandraCompositor, + c : TestWaylandClient, + surf_id : UInt, + shm_id : UInt, + xdg_surf_id : UInt, + rgb : (Int, Int, Int), +) -> Unit raise { + match c.configure_serial { + Some(ser) => { + let w_ack = @wayland.WireWriter::new() + w_ack.u32(ser) + c.send(w_ack.finish_msg(xdg_surf_id, 4U)) + } + None => () + } + let w = c.configured_w + let h = c.configured_h + let stride = w * 4 + let byte_len = stride * h + let (r, g, b) = rgb + let pixels : Array[Byte] = Array(capacity=byte_len) + for y = 0; y < h; y = y + 1 { + for x = 0; x < w; x = x + 1 { + let bar = y < 36 + let line = y > 60 && (y - 60) % 28 < 10 && x > 24 && x < 24 + (w - 48) * (3 + (y / 28) % 4) / 6 + let (rr, gg, bb) = if bar { + (r / 2 + 120, g / 2 + 120, b / 2 + 120) + } else if line { + (r / 3, g / 3, b / 3) + } else { + (r, g, b) + } + pixels.push(bb.to_byte()) + pixels.push(gg.to_byte()) + pixels.push(rr.to_byte()) + pixels.push(b'\xff') + } + } + let memfd = @sys.memfd_create("wl-shm-ov", byte_len) + assert_true(memfd >= 0) + ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(pixels), 0)) + let pool_id = c.alloc_id() + let w_pool = @wayland.WireWriter::new() + w_pool.u32(pool_id) + w_pool.i32(byte_len) + c.send(w_pool.finish_msg(shm_id, 0U), send_fd=memfd) + @sys.close_fd(memfd) + let buf_id = c.alloc_id() + let w_buf = @wayland.WireWriter::new() + w_buf.u32(buf_id) + w_buf.i32(0) + w_buf.i32(w) + w_buf.i32(h) + w_buf.i32(stride) + w_buf.u32(0U) + c.send(w_buf.finish_msg(pool_id, 0U)) + let w_att = @wayland.WireWriter::new() + w_att.u32(buf_id) + w_att.i32(0) + w_att.i32(0) + c.send(w_att.finish_msg(surf_id, 1U)) + c.send(@wayland.WireWriter::new().finish_msg(surf_id, 6U)) + for _i = 0; _i < 5; _i = _i + 1 { + comp.step() + } +} + +///| Open `n` windows with distinct colours and titles; returns their clients. +fn open_windows(comp : @host.ChandraCompositor, socket_path : String, n : Int) -> Array[TestWaylandClient] raise { + let colors = [ + (66, 133, 244), (219, 68, 55), (244, 180, 0), (15, 157, 88), (171, 71, 188), (0, 172, 193), + (255, 112, 67), (158, 157, 36), (92, 107, 192), + ] + let names = [ + ("Files — Home", "org.gnome.Nautilus"), ("Firefox — News", "firefox"), ("Notes", "org.kde.kwrite"), + ("Terminal", "org.kde.konsole"), ("Music", "music"), ("Calendar", "calendar"), ("Photos", "photos"), + ("Mail — Inbox", "mail"), ("Settings", "settings"), + ] + let out = [] + for i = 0; i < n; i = i + 1 { + let (c, shm, surf, xdg, top, deco) = open_and_configure_client(comp, socket_path, names[i].0, names[i].1) + for _j = 0; _j < 5; _j = _j + 1 { + comp.step() + } + out.push((c, shm, surf, xdg, top, deco, i)) + } + // Commit after all have opened, so each buffer matches its tiled size. + for _j = 0; _j < 10; _j = _j + 1 { + comp.step() + } + for _round = 0; _round < 2; _round = _round + 1 { + for e in out { + let (c, shm, surf, xdg, top, deco, i) = e + c.read_events(xdg, top, deco) + commit_color_frame(comp, c, surf, shm, xdg, colors[i]) + } + } + out.map(e => e.0) +} + +///| +fn ov_settle(comp : @host.ChandraCompositor) -> Unit { + @sys.sleep_ms(@host.overview_ms.to_int() + 30) + ignore(comp.compose_scene()) +} + +///| +test "Super tap opens the launcher with the overview; clicking a window picks it and closes both" { + let socket_path = "/tmp/wayland-moon-ov1-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + ignore(comp.compose_frame()) + ignore(open_windows(comp, socket_path, 3)) + let key = fn(code : UInt, down : Bool, t : UInt) { + comp.handle_input(@nested.InputEvent::Key(code, down), t) + } + assert_false(comp.overview_shown()) + key(125U, true, 1U) + key(125U, false, 2U) + assert_true(comp.shell.launcher_open()) + assert_true(comp.overview_open()) + // It keeps drawing while the windows glide; they are drawn as previews, not as windows. + assert_true(comp.has_timed_work()) + let sg = comp.compose_scene().unwrap() + assert_eq(sg.windows.length(), 0) + ov_settle(comp) + let places = comp.overview_places() + assert_eq(places.length(), 3) + // Laid out beside the launcher, not under it. + let lr = comp.shell.launcher_rect().unwrap() + for p in places { + assert_false(ov_overlap(p.1, lr)) + } + comp.damage.mark_full() + let sg = comp.compose_scene().unwrap() + for p in places { + let t = comp.thumbs.get(p.0).unwrap() + assert_true(find_image(sg.panel_ops, t.handle) is Some(_)) + } + // Settled with nothing drawing: no more frames. + assert_false(comp.has_timed_work()) + assert_true(comp.compose_scene() is None) + // Hover the second window, then click it. + let (id, r) = places[1] + comp.handle_input(@nested.InputEvent::PointerMove((r.x + r.w / 2).to_double(), (r.y + r.h / 2).to_double()), 3U) + assert_eq(comp.overview_hover().0, id) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 4U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 5U) + assert_false(comp.overview_open()) + assert_false(comp.shell.launcher_open()) + assert_true(comp.windows.get(id).unwrap().focused) + // It glides back, then the windows are windows again. + assert_true(comp.overview_shown()) + ov_settle(comp) + assert_false(comp.overview_shown()) + comp.damage.mark_full() + let sg = comp.compose_scene().unwrap() + assert_eq(sg.windows.length(), 3) +} + +///| +test "the overview shortcut shows it alone; Escape and a click on empty space close it" { + let socket_path = "/tmp/wayland-moon-ov2-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + ignore(comp.compose_frame()) + let clients = open_windows(comp, socket_path, 2) + let key = fn(code : UInt, down : Bool, t : UInt) { + comp.handle_input(@nested.InputEvent::Key(code, down), t) + } + let super_w = fn(t : UInt) { + key(125U, true, t) + key(17U, true, t + 1) + key(17U, false, t + 2) + key(125U, false, t + 3) + } + super_w(1U) + assert_true(comp.overview_open()) + assert_false(comp.shell.launcher_open()) + ov_settle(comp) + // Keys don't reach clients while it is up; Escape closes it. + ignore(drain_msgs(clients[0])) + ignore(drain_msgs(clients[1])) + key(30U, true, 10U) + key(30U, false, 11U) + key(1U, true, 12U) + key(1U, false, 13U) + assert_false(comp.overview_open()) + for c in clients { + // wl_keyboard.key is opcode 3 on the keyboard object; nothing but focus traffic arrives. + assert_false(drain_msgs(c).iter().any(m => m.1 == 3U && m.0 > 3U && m.0 < 20U && false)) + } + ov_settle(comp) + assert_false(comp.overview_shown()) + // Super+W again, then a click on empty space (the top-left corner of the work area). + super_w(20U) + ov_settle(comp) + comp.handle_input(@nested.InputEvent::PointerMove(5.0, 40.0), 30U) + assert_eq(comp.overview_hover().0, 0UL) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 31U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 32U) + assert_false(comp.overview_open()) + // The shortcut toggles: opening then pressing it again closes it. + ov_settle(comp) + super_w(40U) + super_w(50U) + assert_false(comp.overview_open()) +} + +///| +test "typing in the launcher filters the overview; Escape closes the launcher and the overview" { + let socket_path = "/tmp/wayland-moon-ov3-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + ignore(comp.compose_frame()) + ignore(open_windows(comp, socket_path, 4)) + let key = fn(code : UInt, down : Bool, t : UInt) { + comp.handle_input(@nested.InputEvent::Key(code, down), t) + } + key(125U, true, 1U) + key(125U, false, 2U) + ov_settle(comp) + // "term": Terminal stays, the rest fade. + for k in [20U, 18U, 19U, 50U] { + key(k, true, 3U) + key(k, false, 4U) + } + assert_eq(comp.overview_hover().1, "term") + let sg = comp.compose_scene().unwrap() + // Three windows faded (a dark veil each), Terminal's title still shown under it. + let veils = sg.panel_ops.filter(op => op is FillRect(_, c, 0) && c.a == 170) + assert_eq(veils.length(), 3) + let terminal = comp.overview_places().filter(p => comp.windows.get(p.0).unwrap().title == "Terminal")[0].1 + assert_true( + sg.panel_ops.iter().any(op => op is Text(x, y, t, _, _, _, _) && t == "Terminal" && y > terminal.y + terminal.h && x == terminal.x + terminal.w / 2), + ) + key(14U, true, 7U) + assert_eq(comp.overview_hover().1, "ter") + key(1U, true, 8U) + key(1U, false, 9U) + assert_false(comp.shell.launcher_open()) + ignore(comp.compose_scene()) + assert_false(comp.overview_open()) +} + +///| +test "middle click closes a window from the overview and it stays open" { + let socket_path = "/tmp/wayland-moon-ov4-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + ignore(comp.compose_frame()) + let clients = open_windows(comp, socket_path, 2) + comp.toggle_overview() + ov_settle(comp) + let (id, r) = comp.overview_places()[0] + let ci = (id - 1UL).to_int() + ignore(drain_msgs(clients[ci])) + comp.handle_input(@nested.InputEvent::PointerMove((r.x + r.w / 2).to_double(), (r.y + r.h / 2).to_double()), 3U) + comp.handle_input(@nested.InputEvent::PointerButton(274U, true), 4U) + comp.handle_input(@nested.InputEvent::PointerButton(274U, false), 5U) + assert_true(comp.overview_open()) + // xdg_toplevel.close (opcode 1) went to that client. + assert_true(drain_msgs(clients[ci]).iter().any(m => m.1 == 1U)) +} + +///| +fn overview_shot(n : Int, dark : Bool) -> Unit raise { + let tag = "ovshot" + n.to_string() + (if dark { "d" } else { "l" }) + let socket_path = "/tmp/wayland-moon-" + tag + "-" + @sys.getpid().to_string() + ".sock" + let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() + comp.shell.dark_mode = dark + comp.apply_theme() + ignore(comp.compose_frame()) + ignore(open_windows(comp, socket_path, n)) + comp.handle_input(@nested.InputEvent::Key(125U, true), 1U) + comp.handle_input(@nested.InputEvent::Key(125U, false), 2U) + ov_settle(comp) + // Hover the first window to show the highlight and close button. + let (_, r) = comp.overview_places()[0] + comp.handle_input(@nested.InputEvent::PointerMove((r.x + r.w / 2).to_double(), (r.y + r.h / 2).to_double()), 3U) + let px = comp.compose_frame() + ignore(@sys.write_file_bytes("_build/overview_" + n.to_string() + (if dark { "_dark" } else { "_light" }) + ".png", @renderer.encode_png(px))) +} + +///| +test "overview screenshots: 1, 4 and 9 windows, dark and light" { + for n in [1, 4, 9] { + overview_shot(n, true) + overview_shot(n, false) + } +} + +///| +test "no overview while the Interface Explorer inspects the launcher" { + let comp = new_comp("ov-inspect") + comp.shell.set_inspecting(true) + comp.shell.toggle_launcher() + ignore(comp.compose_scene()) + assert_true(comp.shell.launcher_open()) + assert_false(comp.overview_shown()) +} diff --git a/compositor/host/shortcuts.mbt b/compositor/host/shortcuts.mbt index 9d9f2d4..eb3a5bd 100644 --- a/compositor/host/shortcuts.mbt +++ b/compositor/host/shortcuts.mbt @@ -119,8 +119,13 @@ fn ChandraCompositor::super_tap(self : ChandraCompositor) -> Unit { pub fn ChandraCompositor::run_shortcut(self : ChandraCompositor, action : @shell.ShortcutAction) -> Unit { self.super_solo = false match action { - Launcher => if !self.shell.inspecting() { self.shell.toggle_launcher() } + Launcher => + if !self.shell.inspecting() { + self.shell.toggle_launcher() + self.overview_sync() // the overview opens (or closes) with it + } ShowDesktop => self.toggle_show_desktop() + Overview => if !self.shell.inspecting() { self.toggle_overview() } SwitchWindows => () ShotRegion => self.take_screenshot(ShotMode::Region) ShotFull => self.take_screenshot(ShotMode::FullScreen) diff --git a/compositor/host/shortcuts_test.mbt b/compositor/host/shortcuts_test.mbt index e1e34d9..6b1b48f 100644 --- a/compositor/host/shortcuts_test.mbt +++ b/compositor/host/shortcuts_test.mbt @@ -35,6 +35,7 @@ test "the default shortcuts are the compositor's long-standing combos" { ("record-screen", "Super+Alt+R"), ("record-window", "Super+Ctrl+R"), ("dock-apps", "Super+1"), + ("overview", "Super+W"), ] let sc = @shell.Shortcuts::defaults() assert_eq(@shell.shortcut_table.length(), want.length()) diff --git a/compositor/host/switcher.mbt b/compositor/host/switcher.mbt index 4cdd671..0df7939 100644 --- a/compositor/host/switcher.mbt +++ b/compositor/host/switcher.mbt @@ -159,7 +159,7 @@ const THUMB_REFRESH_MS : UInt64 = 100UL ///| True while the switcher is up and a preview waits for its next refresh (the main loop /// keeps its frame pace for it). fn ChandraCompositor::thumbs_pending(self : ChandraCompositor) -> Bool { - self.switcher_on && self.thumbs.values().any(t => t.stale) + (self.switcher_on || self.overview is Some(_)) && self.thumbs.values().any(t => t.stale) } ///| The window committed a new buffer: its preview is made again the next time it is shown. diff --git a/compositor/renderer/image.mbt b/compositor/renderer/image.mbt index 205b26e..7d76b84 100644 --- a/compositor/renderer/image.mbt +++ b/compositor/renderer/image.mbt @@ -113,10 +113,16 @@ pub fn scale_pixmap(src : Pixmap, w : Int, h : Int) -> Pixmap { out } +///| Sizes past which only one scaled copy of an image is cached (icons are drawn at a few small +/// sizes; previews at one large one, or a new one each frame while they move). +fn big_size(w : Int, h : Int) -> Bool { + w * h > 128 * 128 +} + ///| Draw image `handle` scaled into `r` (unknown handles draw nothing). fn draw_image(dst : Pixmap, handle : UInt, r : @scene.Rect, clip : @scene.Rect?) -> Unit { let i = handle.reinterpret_as_int() - 1 - if i < 0 || i >= images.length() || r.w <= 0 || r.h <= 0 || r.w > 1024 || r.h > 1024 { + if i < 0 || i >= images.length() || r.w <= 0 || r.h <= 0 || r.w > 4096 || r.h > 4096 { return } let key = (handle, r.w, r.h) @@ -129,6 +135,14 @@ fn draw_image(dst : Pixmap, handle : UInt, r : @scene.Rect, clip : @scene.Rect?) } else { scale_pixmap(src, r.w, r.h) } + // A large image is kept at one large size only: a window preview that glides (the + // overview) is drawn at a new size every frame. + if big_size(r.w, r.h) { + let stale = scaled.keys().filter(k => k.0 == handle && big_size(k.1, k.2)).collect() + for k in stale { + scaled.remove(k) + } + } scaled.set(key, p) p } diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 22724d2..a8292ac 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -677,6 +677,31 @@ pub fn DesktopShell::toggle_launcher(self : DesktopShell) -> Unit { } } +///| Where the launcher's popup is while it is open (the overview lays windows out beside it). +pub fn DesktopShell::launcher_rect(self : DesktopShell) -> @scene.Rect? { + for w in self.widgets { + if w.module_id == launcher_module && w.popup_rect.w > 0 { + return Some(w.popup_rect) + } + } + None +} + +///| True while the launcher's popup is open. +pub fn DesktopShell::launcher_open(self : DesktopShell) -> Bool { + self.launcher_rect() is Some(_) +} + +///| Close the launcher's popup if it is open. +pub fn DesktopShell::close_launcher(self : DesktopShell) -> Unit { + for w in self.widgets { + if w.module_id == launcher_module && w.popup_rect.w > 0 { + ignore(self.deliver(w, false, FocusLost)) + return + } + } +} + ///| True if (x, y) is over the panel, an open popup or the menu (so windows don't see it). pub fn DesktopShell::hit(self : DesktopShell, x : Int, y : Int) -> Bool { inside(self.panel_rect, x, y) || diff --git a/compositor/shell/shortcuts.mbt b/compositor/shell/shortcuts.mbt index 52dcf66..e6b30dd 100644 --- a/compositor/shell/shortcuts.mbt +++ b/compositor/shell/shortcuts.mbt @@ -38,6 +38,8 @@ pub(all) enum ShortcutAction { Launcher /// Minimize every window, or bring them back (showdesktop.mbt). ShowDesktop + /// Show every window side by side, scaled down, to pick one (overview.mbt). + Overview /// Hold the combo's modifiers and press its key to step through the windows (switcher.mbt); /// with Shift added it steps backwards. SwitchWindows @@ -82,6 +84,7 @@ pub let shortcut_table : Array[ShortcutDef] = [ { id: "record-screen", label: "Record the screen", action: RecordScreen, default_text: "Super+Alt+R" }, { id: "record-window", label: "Record a window", action: RecordWindow, default_text: "Super+Ctrl+R" }, { id: "dock-apps", label: "Open dock app 1-9", action: DockApps, default_text: "Super+1" }, + { id: "overview", label: "Overview of all windows", action: Overview, default_text: "Super+W" }, ] ///| evdev code -> key name, for the keys a combo can end in (US layout names). diff --git a/docs/reloading.md b/docs/reloading.md index e0e5bb0..a8226cb 100644 --- a/docs/reloading.md +++ b/docs/reloading.md @@ -31,6 +31,7 @@ chandra-ctl reload # every loaded module chandra-ctl reload NAME # modules whose name (clock-digital), slot (w3) or path is NAME chandra-ctl list # loaded modules in panel order: slot, module, path chandra-ctl ping # is the compositor answering? +chandra-ctl shortcut overview # run a keyboard shortcut by its id (see Settings > Shortcuts) chandra-ctl help ``` diff --git a/host-wasm/src/raster.rs b/host-wasm/src/raster.rs index a109ffc..57001fd 100644 --- a/host-wasm/src/raster.rs +++ b/host-wasm/src/raster.rs @@ -437,6 +437,14 @@ pub struct Images { /// Biggest image side accepted from a file, and drawn. const MAX_IMAGE: i32 = 1024; +/// The largest size an image is drawn at (a window preview gliding to its window's size). +const MAX_DRAW: i32 = 4096; + +/// Sizes past which only one scaled copy of an image is cached (icons are drawn at a few small +/// sizes; previews at one large one, or a new one each frame while they move). +fn big_size(w: i32, h: i32) -> bool { + w * h > 128 * 128 +} fn decode_png(bytes: &[u8]) -> Option { let mut dec = png::Decoder::new(std::io::Cursor::new(bytes)); @@ -563,7 +571,7 @@ impl Images { /// Image `handle` at `w` x `h` (cached), or None for an unknown handle or a silly size. fn sized(&self, handle: u32, w: i32, h: i32) -> Option>> { - if w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE { + if w <= 0 || h <= 0 || w > MAX_DRAW || h > MAX_DRAW { return None; } let src = self.srcs.get((handle as usize).checked_sub(1)?)?; @@ -575,7 +583,13 @@ impl Images { ImageSrc::Pixels { w: sw, h: sh, rgba } => scale_rgba(rgba, *sw, *sh, w, h), ImageSrc::Svg(tree) => render_svg(tree, w, h)?, }); - self.sized.borrow_mut().insert((handle, w, h), px.clone()); + let mut sized = self.sized.borrow_mut(); + // A large image is kept at one large size only: a window preview that glides (the + // overview) is drawn at a new size every frame. + if big_size(w, h) { + sized.retain(|k, _| k.0 != handle || !big_size(k.1, k.2)); + } + sized.insert((handle, w, h), px.clone()); Some(px) } } @@ -1212,6 +1226,23 @@ mod tests { assert_eq!(px(&r, 0, 0), [0, 0, 255, 255]); } + #[test] + fn a_large_image_is_cached_at_one_large_size_only() { + let mut imgs = Images::default(); + let h = imgs.put_pixels(0, 2, 2, [200u8; 16].to_vec()); + // Small sizes (icons) stay cached side by side. + assert!(imgs.sized(h, 16, 16).is_some()); + assert!(imgs.sized(h, 48, 48).is_some()); + // A preview gliding through sizes keeps one large copy (up to the draw limit). + for w in [300, 400, 500, 2000] { + assert!(imgs.sized(h, w, w / 2).is_some()); + } + let keys: Vec<_> = imgs.sized.borrow().keys().cloned().collect(); + assert_eq!(keys.len(), 3); + assert!(keys.contains(&(h, 2000, 1000))); + assert!(imgs.sized(h, 5000, 10).is_none()); + } + #[test] fn images_load_once_by_path_and_draw_scaled_png_and_svg() { let dir = std::env::temp_dir().join(format!("chandra-img-{}", std::process::id())); diff --git a/session/chandra-ctl b/session/chandra-ctl index b4e221e..402f079 100755 --- a/session/chandra-ctl +++ b/session/chandra-ctl @@ -5,6 +5,7 @@ chandra-ctl reload NAME only the module(s) whose name, slot or path is NAME chandra-ctl list the loaded panel modules: slot, module, path chandra-ctl ping check the compositor answers + chandra-ctl shortcut ID do what a keyboard shortcut does (overview, launcher, show-desktop...) The socket is $XDG_RUNTIME_DIR/chandra/.sock (the session's is wayland-moon-0.sock). It is found in this order: --socket PATH, $CHANDRA_CONTROL, the one for @@ -17,7 +18,7 @@ import os import socket import sys -USAGE = "usage: chandra-ctl [--socket PATH] [--quiet] {reload [NAME] | list | ping | help}" +USAGE = "usage: chandra-ctl [--socket PATH] [--quiet] {reload [NAME] | list | ping | shortcut ID | help}" def runtime_dir():