Files
chandra/lib/dock/dock_wbtest.mbt
Claude 01bd8281d5
CI / test (push) Failing after 2m33s
Dock: plain icons always come before the task pills
With window titles on, a pinned app that was running drew as a pill in its
pin slot, so idle pinned icons ended up between pills. Now the dock shows
the pinned apps that aren't running first, then every task: running pinned
apps in pin order, then the other running apps. The divider sits between
the two groups. Squeezing the pills to icons keeps the order; with titles
off the plain pinned order stays. Move left / right in the menu follow the
pin list, not the shown position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiPCFHRxaJQvwex6ry
2026-11-26 01:53:43 +00:00

279 lines
12 KiB
MoonBit

///|
fn apps() -> Array[@types.AppEntry] {
["foot", "firefox", "org.gnome.Nautilus", "gimp", "org.kde.kate", "vlc"].map(id => {
id,
name: id + "!",
comment: "",
category: "",
glyph: id.to_upper(),
})
}
///|
fn win(id : Int, app : String, focused? : Bool = false, minimized? : Bool = false) -> DockWindow {
{ id: id.to_uint64(), app_id: app, focused, title: app + " window", minimized }
}
///|
fn tokens() -> @theme.Tokens {
{
name: "t",
palette: {
desktop_top: @ui.color(0, 0, 0, 255),
desktop_bottom: @ui.color(0, 0, 0, 255),
surface: @ui.color(30, 30, 30, 255),
surface_hover: @ui.color(40, 40, 40, 255),
surface_popup: @ui.color(30, 30, 30, 255),
foreground: @ui.color(255, 255, 255, 255),
foreground_muted: @ui.color(150, 150, 150, 255),
accent: @ui.color(60, 120, 220, 255),
accent_foreground: @ui.color(255, 255, 255, 255),
border: @ui.color(60, 60, 60, 255),
},
font_size: 14U,
radius: 6U,
spacing: 6U,
panel_height: 44U,
}
}
///|
test "without a choice the dock pins the catalog's first apps; the choice round-trips" {
let c = DockConfig::new()
assert_eq(c.pins(apps()), ["foot", "firefox", "org.gnome.Nautilus", "gimp", "org.kde.kate"])
assert_eq(c.items()[4].value, "")
assert_true(c.set("pinned", "vlc;foot"))
assert_eq(c.pins(apps()), ["vlc", "foot"])
assert_eq(c.items()[4], { key: "pinned", label: "", choices: [], value: "vlc;foot" })
// Unpinning everything is a choice too, not "back to the defaults".
assert_true(c.set("pinned", encode_pins([])))
assert_eq(c.pins(apps()), [])
assert_eq(decode_pins(""), None)
assert_eq(decode_pins("a;;b;a"), Some(["a", "b"]))
// The other settings.
assert_false(c.set("icon-size", "Huge"))
assert_true(c.set("icon-size", "Large"))
assert_true(c.set("magnify", "off"))
assert_false(c.set("magnify", "off"))
let d = DockConfig::from_pairs([("icon-size", "Small"), ("running", "off")])
assert_eq(d.size, "Small")
assert_false(d.running)
}
///|
test "windows find their app by id, case, or the last dotted part" {
let ids = ["foot", "org.gnome.Nautilus", "firefox"]
assert_eq(match_app("foot", ids), Some("foot"))
assert_eq(match_app("FOOT", ids), Some("foot"))
assert_eq(match_app("nautilus", ids), Some("org.gnome.Nautilus"))
assert_eq(match_app("org.mozilla.firefox", ids), Some("firefox"))
assert_eq(match_app("gimp", ids), None)
assert_eq(match_app("", ids), None)
// By the app's name: xterm's windows belong to debian-xterm.desktop ("XTerm").
let xterm : Array[@types.AppEntry] = [{ id: "debian-xterm", name: "XTerm", comment: "", category: "", glyph: "X" }]
assert_eq(match_name("xterm", xterm), Some("debian-xterm"))
assert_eq(match_name("vim", xterm), None)
assert_eq(items(DockConfig::new(), xterm, [win(1, "xterm")]).map(i => i.windows.length()), [1])
}
///|
test "pinned apps come first in their order, then running ones, with their windows" {
let c = DockConfig::from_pairs([("pinned", "firefox;foot")])
let its = items(c, apps(), [
win(7, "foot", focused=true),
win(3, "foot"),
win(5, "vlc"),
win(9, "unknown.app"),
win(2, ""),
])
assert_eq(its.map(i => i.id), ["firefox", "foot", "vlc", "unknown.app"])
assert_eq(its.map(i => i.pinned), [true, true, false, false])
assert_eq(its[1].windows, [3UL, 7UL]) // oldest first
assert_true(its[1].focused)
assert_false(its[0].focused)
assert_eq(its[0].windows.length(), 0)
// Not in the catalog: named after the id's last part, with a letter for its tile.
assert_eq(its[3].name, "app")
assert_eq(its[3].glyph, "A")
// Running apps can be hidden.
assert_true(c.set("running", "off"))
assert_eq(items(c, apps(), [win(5, "vlc"), win(7, "foot")]).map(i => i.id), ["firefox", "foot"])
}
///|
test "items are hit-tested by slot, with a divider before the running apps" {
let c = DockConfig::from_pairs([("pinned", "foot;firefox"), ("titles", "off")])
let its = items(c, apps(), [win(1, "vlc")])
let h = 36
let w = preferred_width(its, c, h)
let p = icon_size(c, h) + 6
assert_eq(item_at(its, c, w, h, 6, 10), 0)
assert_eq(item_at(its, c, w, h, 6 + p, 10), 1)
// The divider shifts the running app right.
assert_eq(item_center(its, c, w, h, 2) - item_center(its, c, w, h, 1), p + 11)
assert_eq(item_at(its, c, w, h, item_center(its, c, w, h, 2), 10), 2)
assert_eq(item_at(its, c, w, h, w + 5, 10), -1)
assert_eq(item_at(its, c, w, h, 6, -1), -1)
// Too narrow for all of them: the ones that don't fit can't be hit.
assert_eq(item_at(its, c, w - 10, h, item_center(its, c, w, h, 2), 10), -1)
// Bigger icons, wider dock; magnification grows the hovered one most.
let big = DockConfig::from_pairs([("pinned", "foot;firefox"), ("icon-size", "Large")])
assert_true(preferred_width(items(big, apps(), []), big, 44) > preferred_width(items(c, apps(), []), c, 44))
assert_true(item_size(c, 1, 1, h) > item_size(c, 0, 1, h))
assert_true(item_size(c, 0, 1, h) > item_size(c, 3, 1, h))
assert_true(c.set("magnify", "off"))
assert_eq(item_size(c, 1, 1, h), icon_size(c, h))
}
///|
test "a click starts an app that isn't running and walks through a running app's windows" {
let c = DockConfig::from_pairs([("pinned", "foot;firefox"), ("titles", "off")])
let its = items(c, apps(), [win(3, "foot"), win(8, "foot")])
let h = 36
let w = preferred_width(its, c, h)
let x0 = item_center(its, c, w, h, 0)
let x1 = item_center(its, c, w, h, 1)
let (hv, redraw, act) = handle(its, c, -1, w, h, None, PointerMove({ x: x1, y: 10 }))
assert_eq((hv, redraw, act), (1, true, Nothing))
assert_eq(handle(its, c, 1, w, h, None, PointerMove({ x: x1, y: 10 })).1, false)
assert_eq(handle(its, c, 1, w, h, None, PointerDown({ x: x1, y: 10 })).2, Launch("firefox"))
// Not focused: its newest window comes forward.
assert_eq(handle(its, c, 0, w, h, Some(99UL), PointerDown({ x: x0, y: 10 })).2, Focus(8UL))
// Focused: the next of its windows, round and round.
let its = items(c, apps(), [win(3, "foot"), win(8, "foot", focused=true)])
assert_eq(handle(its, c, 0, w, h, Some(8UL), PointerDown({ x: x0, y: 10 })).2, Focus(3UL))
let its = items(c, apps(), [win(3, "foot", focused=true)])
assert_eq(handle(its, c, 0, w, h, Some(3UL), PointerDown({ x: x0, y: 10 })).2, Nothing)
let (hv, redraw, _) = handle(its, c, 0, w, h, None, PointerLeave)
assert_eq((hv, redraw), (-1, true))
}
///|
test "render draws each icon (theme image or letter tile), a dot per window and the divider" {
let c = DockConfig::from_pairs([("pinned", "foot;firefox"), ("titles", "off")])
let its = items(c, apps(), [win(3, "foot", focused=true), win(4, "vlc"), win(5, "vlc")])
let h = 36
let w = preferred_width(its, c, h)
let ops = render(@ui.size(w, h), tokens(), c, its, -1, id => if id == "firefox" { 42U } else { 0U })
let images = ops.filter(op => op is Image(_))
assert_eq(images.length(), 1)
let texts = ops.filter(op => op is Text(_))
assert_eq(texts.length(), 2) // foot's and vlc's letter tiles
// background + divider + foot (tile, glyph, 1 dot) + firefox (image) + vlc (tile, glyph, 2 dots)
assert_eq(ops.length(), 1 + 1 + 3 + 1 + 4)
// Nothing to show: a "+" tile (fill, outline, two strokes) and its hint, after the background.
let none = DockConfig::from_pairs([("pinned", ";")])
let empty = render(@ui.size(preferred_width([], none, h), h), tokens(), none, [], -1, _ => 0U)
assert_eq(empty.length(), 6)
assert_true(empty.iter().any(op => op is Text(t) && t.text == "Add apps"))
}
///|
test "the empty dock is one target: hovering it lights the + tile, a click is the shell's" {
let none = DockConfig::from_pairs([("pinned", ";")])
let (hv, redraw, act) = handle([], none, -1, 150, 36, None, PointerMove({ x: 10, y: 10 }))
assert_eq((hv, redraw, act), (0, true, Nothing))
let (_, _, act) = handle([], none, 0, 150, 36, None, PointerDown({ x: 10, y: 10 }))
assert_eq(act, Nothing)
}
///|
test "a window's app: a pin, a catalog app, or its own id" {
let a = apps()
assert_eq(owner_of("org.mozilla.firefox", [], a), Some("firefox"))
assert_eq(owner_of("vlc", ["vlc"], a), Some("vlc"))
assert_eq(owner_of("org.example.Notes", [], a), Some("org.example.Notes"))
assert_eq(owner_of("", [], a), None)
}
///|
test "activate starts an app without windows and brings one forward otherwise" {
let c = DockConfig::from_pairs([("pinned", "foot;firefox")])
let its = items(c, apps(), [win(3, "foot", focused=true), win(4, "foot")])
// Firefox isn't running, so it comes first (before foot's task pill).
assert_eq(its.map(i => i.id), ["firefox", "foot"])
assert_eq(activate(its[0], Some(3UL)), Launch("firefox"))
assert_eq(activate(its[1], None), Focus(4UL))
assert_eq(activate(its[1], Some(3UL)), Focus(4UL))
}
///|
test "pins are toggled and moved within bounds" {
assert_eq(toggle_pin(["a", "b"], "b"), ["a"])
assert_eq(toggle_pin(["a"], "c"), ["a", "c"])
assert_eq(move_pin(["a", "b", "c"], "b", -1), ["b", "a", "c"])
assert_eq(move_pin(["a", "b", "c"], "c", 1), ["a", "b", "c"])
assert_eq(move_pin(["a", "b", "c"], "x", 1), ["a", "b", "c"])
}
///|
test "app glyph comes from the app id's last part, else the title" {
assert_eq(app_glyph("org.gnome.Nautilus", "Files"), "N")
assert_eq(app_glyph("foot", ""), "F")
assert_eq(app_glyph("org.chandra.", "x"), "C")
assert_eq(app_glyph("", " terminal"), "T")
assert_eq(app_glyph("", ""), "?")
}
///|
test "an app's icon is its coloured tile plus glyph" {
let ops = app_icon(20, 17, 22, "foot", "F")
assert_eq(ops.length(), 2)
match ops[0] {
FillRect(f) => {
assert_eq(f.rect.x, 9)
assert_eq(f.color.r, tile_color("foot").r)
}
_ => fail("expected a tile")
}
}
///|
test "apps with windows are titled pills that shrink, then drop their titles, as room runs out" {
let c = DockConfig::from_pairs([("pinned", "foot;firefox")])
let its = items(c, apps(), [win(3, "foot"), win(4, "vlc", minimized=true), win(5, "vlc")])
let h = 36
let base = icon_size(c, h)
// Room for all: firefox (no windows) is an icon, first; foot and vlc get full pills.
assert_eq(its.map(i => i.id), ["firefox", "foot", "vlc"])
assert_eq(widths(its, c, 1000, h), [base, label_max, label_max])
assert_eq(item_title(its[1]), "foot window")
assert_eq(item_title(its[2]), "vlc! (2)")
assert_eq(its[2].minimized, 1)
// Less room: the pills share what's left...
let ws = widths(its, c, 300, h)
assert_true(ws[1] < label_max && ws[1] >= label_min && ws[1] == ws[2])
assert_true(slot_x(its, c, 300, h, 2) + ws[2] + 6 <= 300)
// ...and below label_min they are icons again.
assert_eq(widths(its, c, 150, h), [base, base, base])
// A click anywhere on a pill is that app.
let x1 = slot_x(its, c, 1000, h, 1)
assert_eq(item_at(its, c, 1000, h, x1 + label_max - 10, 10), 1)
let ops = render(@ui.size(1000, h), tokens(), c, its, -1, _ => 0U)
assert_true(ops.iter().any(op => op is Text(t) && t.text == "foot window"))
// Off: icons only.
assert_true(c.set("titles", "off"))
assert_eq(widths(its, c, 1000, h), [base, base, base])
}
///|
test "with titles, plain icons always come before the tasks" {
let c = DockConfig::from_pairs([("pinned", "firefox;nautilus-x;foot;gimp")])
let a = apps() + [{ id: "nautilus-x", name: "Files", comment: "", category: "", glyph: "F" }]
let wins = [win(1, "firefox"), win(2, "foot"), win(3, "vlc"), win(4, "org.kde.kate")]
let its = items(c, a, wins)
// Not running: Files, GIMP; then the tasks: pinned Firefox, foot, then vlc and Kate.
assert_eq(its.map(i => i.id), ["nautilus-x", "gimp", "firefox", "foot", "vlc", "org.kde.kate"])
// The divider goes between the icons and the tasks.
assert_eq(first_running(its, c), 2)
let h = 36
assert_eq(slot_x(its, c, 2000, h, 2) - slot_x(its, c, 2000, h, 1), icon_size(c, h) + 6 + 11)
// Squeezed to icons, the order stays (so a click lands where it did).
assert_eq(widths(its, c, 200, h).map(x => x == icon_size(c, h)), [true, true, true, true, true, true])
// Titles off: the pinned order, running apps after the divider.
assert_true(c.set("titles", "off"))
let its = items(c, a, wins)
assert_eq(its.map(i => i.id), ["firefox", "nautilus-x", "foot", "gimp", "vlc", "org.kde.kate"])
assert_eq(first_running(its, c), 4)
}