Files
chandra/compositor/host/overview_test.mbt
Claude 30f5e88639
CI / test (push) Successful in 1m35s
Overview (Exposé): every window side by side when the launcher opens, or alone on Super+W
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgo3PfGPY8wwv21ZZS
2026-11-26 00:24:16 +00:00

438 lines
17 KiB
MoonBit

///|
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())
}