Files
chandra/compositor/host/input_test.mbt
T
2026-11-25 00:44:34 -08:00

754 lines
30 KiB
MoonBit

///| Input routing: host pointer/keyboard events reach the focused client window, and the
/// panel (made entirely of wasm modules) responds to the mouse and keyboard.
/// Needs the modules: run scripts/build-modules.sh (just test does).
///| Rectangle of the panel module `id` (file stem), as last drawn.
fn rect_of(comp : @host.MoonPlasmaCompositor, id : String) -> @scene.Rect {
comp.shell.slot_rect(comp.shell.slot_of(id))
}
///| Centre of the panel module `id`.
fn center_of(comp : @host.MoonPlasmaCompositor, id : String) -> (Double, Double) raise {
let r = rect_of(comp, id)
assert_true(r.w > 0)
((r.x + r.w / 2).to_double(), (r.y + r.h / 2).to_double())
}
///| Move to (x, y), press and release `btn`, then draw a frame.
fn click_at(comp : @host.MoonPlasmaCompositor, x : Int, y : Int, t : UInt, btn? : UInt = 272U) -> Unit {
comp.handle_input(@nested.InputEvent::PointerMove(x.to_double(), y.to_double()), t)
comp.handle_input(@nested.InputEvent::PointerButton(btn, true), t + 1U)
comp.handle_input(@nested.InputEvent::PointerButton(btn, false), t + 2U)
ignore(comp.compose_frame())
}
///| Click the centre of panel module `id`.
fn click_module(comp : @host.MoonPlasmaCompositor, id : String, t : UInt) -> Unit raise {
let (x, y) = center_of(comp, id)
click_at(comp, x.to_int(), y.to_int(), t)
}
///| A fresh compositor with the default panel and a first frame drawn.
fn new_comp(tag : String) -> @host.MoonPlasmaCompositor {
let socket_path = "/tmp/wayland-moon-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
ignore(comp.compose_frame())
comp
}
///| Drain everything the compositor sent this client as (object, opcode) pairs.
fn drain_msgs(c : TestWaylandClient) -> Array[(UInt, UInt)] {
let scratch : FixedArray[Byte] = FixedArray::make(65536, b'\x00')
let raw : Array[Byte] = []
while @sys.poll_read(c.fd, 5) > 0 {
let (n, rfd) = @sys.recvmsg_fd(c.fd, scratch, 65536)
if rfd >= 0 {
@sys.close_fd(rfd)
}
if n <= 0 {
break
}
for i = 0; i < n; i = i + 1 {
raw.push(scratch[i])
}
}
let out : Array[(UInt, UInt)] = []
let mut off = 0
while off + 8 <= raw.length() {
let obj = @wayland.read_u32_le(raw, off)
let w2 = @wayland.read_u32_le(raw, off + 4)
let size = ((w2 >> 16) & 0xFFFFU).reinterpret_as_int()
if size < 8 {
break
}
out.push((obj, w2 & 0xFFFFU))
off = off + size
}
out
}
///|
fn has_msg(msgs : Array[(UInt, UInt)], obj : UInt, op : UInt) -> Bool {
msgs.iter().any(m => m.0 == obj && m.1 == op)
}
///|
test "host input reaches the focused client and the launcher popup opens" {
let socket_path = "/tmp/wayland-moon-input-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(
comp, socket_path, "T", "t",
)
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
// Bind the seat and fetch its pointer + keyboard.
let reg_id = 2U
let seat_id = bind_global(c, reg_id, "wl_seat", 7U)
let ptr_id = c.alloc_id()
let w1 = @wayland.WireWriter::new()
w1.u32(ptr_id)
c.send(w1.finish_msg(seat_id, 0U))
let kbd_id = c.alloc_id()
let w2 = @wayland.WireWriter::new()
w2.u32(kbd_id)
c.send(w2.finish_msg(seat_id, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
ignore(drain_msgs(c))
// Pointer over the window: enter + motion; press goes through as a button.
comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 11U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 12U)
// Keyboard goes to the focused window.
comp.handle_input(@nested.InputEvent::Key(30U, true), 13U)
let msgs = drain_msgs(c)
assert_true(has_msg(msgs, ptr_id, 0U)) // wl_pointer.enter
assert_true(has_msg(msgs, ptr_id, 2U)) // motion
assert_true(has_msg(msgs, ptr_id, 3U)) // button
assert_true(has_msg(msgs, kbd_id, 3U)) // wl_keyboard.key
// Cursor: a client that asks for a text cursor gets it inside its window...
let shape_mgr = bind_global(c, reg_id, "wp_cursor_shape_manager_v1", 1U)
let shape_dev = c.alloc_id()
let w3 = @wayland.WireWriter::new()
w3.u32(shape_dev)
w3.u32(ptr_id)
c.send(w3.finish_msg(shape_mgr, 1U))
let w4 = @wayland.WireWriter::new()
w4.u32(1U) // serial
w4.u32(9U) // text
c.send(w4.finish_msg(shape_dev, 1U))
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_eq(comp.desired_cursor(), 9U)
// Launcher module: click the button -> its popup opens and swallows keys.
ignore(comp.compose_frame()) // lays out the panel
let (lx, ly) = center_of(comp, "launcher")
comp.handle_input(@nested.InputEvent::PointerMove(lx, ly), 20U)
assert_eq(comp.desired_cursor(), 4U) // ...but panel modules always show a hand
click_at(comp, lx.to_int(), ly.to_int(), 21U)
assert_true(comp.shell.popup_open())
assert_true(comp.shell.slot_popup_rect(comp.shell.slot_of("launcher")).w > 0)
comp.handle_input(@nested.InputEvent::Key(30U, true), 23U)
assert_false(has_msg(drain_msgs(c), kbd_id, 3U)) // swallowed by the launcher
// Off the shell and off any window: default arrow.
comp.handle_input(@nested.InputEvent::PointerMove(1270.0, 5.0), 25U)
assert_eq(comp.desired_cursor(), 1U)
// Escape closes it.
comp.handle_input(@nested.InputEvent::Key(1U, true), 24U)
assert_false(comp.shell.popup_open())
comp.close()
}
///|
test "dragging the divider between two tiled windows resizes them" {
let socket_path = "/tmp/wayland-moon-split-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
let left = comp.windows.get(1UL).unwrap().geometry
let right = comp.windows.get(2UL).unwrap().geometry
// The divider is the gap between the two windows.
let gx = left.x + left.w + 2
let gy = left.y + left.h / 2
assert_true(right.x > left.x + left.w)
comp.handle_input(@nested.InputEvent::PointerMove(gx.to_double(), gy.to_double()), 1U)
assert_eq(comp.desired_cursor(), 26U) // ew-resize over the divider
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
comp.handle_input(@nested.InputEvent::PointerMove(400.0, gy.to_double()), 3U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
let left2 = comp.windows.get(1UL).unwrap().geometry
let right2 = comp.windows.get(2UL).unwrap().geometry
assert_true(left2.w < left.w - 200)
assert_true(right2.w > right.w + 200)
assert_true(right2.x > left2.x + left2.w)
}
///|
test "the layout module opens a popup to pick Tiling, Monocle or Floating" {
let socket_path = "/tmp/wayland-moon-layout-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
ignore(comp.compose_frame())
let slot = comp.shell.slot_of("layout")
let (cx, cy) = center_of(comp, "layout")
comp.handle_input(@nested.InputEvent::PointerMove(cx, cy), 1U)
assert_eq(comp.desired_cursor(), 4U) // it is a button: hand cursor
// Pick row `i` from the popup the button opens.
let pick = fn(i : Int, t : UInt) raise {
click_at(comp, cx.to_int(), cy.to_int(), t) // open
let pr = comp.shell.slot_popup_rect(slot)
assert_true(pr.w > 0)
click_at(comp, pr.x + pr.w / 2, pr.y + 4 + i * 28 + 14, t + 3U)
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
}
assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling)
let tiled_w = comp.windows.get(1UL).unwrap().geometry.w
// Opening alone changes nothing.
click_at(comp, cx.to_int(), cy.to_int(), 2U)
assert_true(comp.shell.slot_popup_rect(slot).w > 0)
assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling)
// Escape closes without picking.
comp.handle_input(@nested.InputEvent::Key(1U, true), 3U)
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
pick(1, 10U)
assert_eq(comp.layout_mode, @wm.LayoutMode::Monocle)
assert_true(comp.windows.get(1UL).unwrap().geometry.w > tiled_w + 100)
pick(2, 20U)
assert_eq(comp.layout_mode, @wm.LayoutMode::Floating)
pick(0, 30U)
assert_eq(comp.layout_mode, @wm.LayoutMode::Tiling)
assert_eq(comp.windows.get(1UL).unwrap().geometry.w, tiled_w)
}
///|
test "title-bar buttons minimize, maximize and close windows" {
let socket_path = "/tmp/wayland-moon-btn-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c, _shm, _surf, _xdg, tl, _deco) = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
ignore(comp.compose_frame())
// Button centres, from the decoration layout (18px buttons, 10px from the right edge).
let btn = fn(wid : UInt64, n : Int) {
let g = comp.windows.get(wid).unwrap().geometry
let outer_w = g.w + 4
let x = g.x - 2 + outer_w - 10 - 9 - (3 - n) * 26
let y = g.y - 32 + 16
(x, y)
}
let click = fn(p : (Int, Int), t : UInt) {
click_at(comp, p.0, p.1, t)
}
// Maximize window 1: it grows.
let w0 = comp.windows.get(1UL).unwrap().geometry.w
click(btn(1UL, 2), 1U)
assert_true(comp.windows.get(1UL).unwrap().geometry.w > w0)
click(btn(1UL, 2), 10U)
assert_eq(comp.windows.get(1UL).unwrap().geometry.w, w0)
// Minimize window 1: it leaves the stacking order, then its pill in the task list restores it.
click(btn(1UL, 1), 20U)
assert_true(comp.wm.windows.get(1UL).unwrap().minimized)
assert_true(!comp.wm.stacking_order().contains(1UL))
let tasks = rect_of(comp, "tasks")
// pills: the visible window first, the minimized one second (140 wide, 6 apart)
click((tasks.x + 146 + 20, tasks.y + tasks.h / 2), 30U)
assert_true(!comp.wm.windows.get(1UL).unwrap().minimized)
// Close window 2: the client receives xdg_toplevel.close (opcode 1 on its toplevel).
ignore(drain_msgs(c))
click(btn(1UL, 3), 40U)
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(has_msg(drain_msgs(c), tl, 1U))
}
///|
test "the task list module focuses the window whose pill is clicked" {
let socket_path = "/tmp/wayland-moon-tasks-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
ignore(comp.compose_frame())
let focused = fn() {
let mut f = 0UL
for id, w in comp.windows {
if w.focused {
f = id
}
}
f
}
let tasks = rect_of(comp, "tasks")
assert_true(tasks.w > 300) // the stretching module takes the free space
let first = comp.wm.stacking_order()[0]
click_at(comp, tasks.x + 20, tasks.y + tasks.h / 2, 1U) // pill 0
assert_eq(focused(), first)
let other = comp.wm.stacking_order()
click_at(comp, tasks.x + 146 + 20, tasks.y + tasks.h / 2, 10U) // pill 1
assert_true(focused() != first || other.length() == 1)
}
///|
test "right-click a panel module to replace it in place, or remove it" {
let comp = new_comp("swap")
let before = comp.shell.slots()
let clock = rect_of(comp, "clock-digital12")
assert_true(clock.w > 0)
let find_row = fn(label : String) {
let mut i = -1
for k = 0; k < comp.shell.menu_rows.length(); k = k + 1 {
if comp.shell.menu_rows[k].label == label {
i = k
}
}
i
}
click_at(comp, clock.x + clock.w / 2, clock.y + clock.h / 2, 1U, btn=273U)
assert_true(comp.shell.menu_open)
let m = comp.shell.menu_rect
assert_true(m.w > 0 && m.y + m.h <= clock.y) // above the panel, not off-screen
let bin = find_row("Replace with Binary Clock")
assert_true(bin >= 0)
assert_true(find_row("Remove widget") >= 0)
assert_true(find_row("Module manager...") >= 0)
// Hover the row (hand cursor), click it: the binary clock takes the digital clock's place.
let ix = m.x + m.w / 2
let iy = m.y + 26 + 28 * bin + 14
comp.handle_input(@nested.InputEvent::PointerMove(ix.to_double(), iy.to_double()), 10U)
assert_eq(comp.desired_cursor(), 4U)
click_at(comp, ix, iy, 11U)
assert_true(!comp.shell.menu_open)
assert_eq(comp.shell.slot_of("clock-digital12"), "")
let after = comp.shell.slots()
assert_eq(after.length(), before.length())
assert_eq(after[after.length() - 1], comp.shell.slot_of("clock-binary")) // same place in the panel
assert_true(rect_of(comp, "clock-binary").w > 0)
// Escape dismisses the menu; "Remove widget" unloads the module.
let moon = rect_of(comp, "moon-disc")
click_at(comp, moon.x + moon.w / 2, moon.y + moon.h / 2, 20U, btn=273U)
assert_true(comp.shell.menu_open)
comp.handle_input(@nested.InputEvent::Key(1U, true), 30U)
assert_true(!comp.shell.menu_open)
click_at(comp, moon.x + moon.w / 2, moon.y + moon.h / 2, 40U, btn=273U)
let m2 = comp.shell.menu_rect
click_at(comp, m2.x + m2.w / 2, m2.y + 26 + 28 * find_row("Remove widget") + 14, 50U)
assert_eq(comp.shell.slot_of("moon-disc"), "")
assert_eq(comp.shell.slots().length(), before.length() - 1)
}
///|
test "tapping Super toggles the launcher; Super+key does not" {
let comp = new_comp("super")
let tap = fn(t : UInt) {
comp.handle_input(@nested.InputEvent::Key(125U, true), t)
comp.handle_input(@nested.InputEvent::Key(125U, false), t + 1U)
}
assert_false(comp.shell.popup_open())
tap(1U)
assert_true(comp.shell.popup_open())
tap(3U)
assert_false(comp.shell.popup_open())
// Super held while another key is pressed: not a tap.
comp.handle_input(@nested.InputEvent::Key(125U, true), 5U)
comp.handle_input(@nested.InputEvent::Key(30U, true), 6U)
comp.handle_input(@nested.InputEvent::Key(30U, false), 7U)
comp.handle_input(@nested.InputEvent::Key(125U, false), 8U)
assert_false(comp.shell.popup_open())
}
///|
test "the panel is saved to panel.edn and restored" {
// Never touch the user's real config: only run when a scratch dir is provided.
if @sys.getenv("MOON_PLASMA_TEST_CONFIG") == "1" {
let theme = @scene.ThemeTokens::moon_night()
let a = @shell.DesktopShell::new("x")
a.persist = true
a.set_output(theme, 1280, 720)
assert_true(a.unload_slot(a.slot_of("moon-disc")))
assert_true(a.load_module("clock-binary"))
let b = @shell.DesktopShell::new("x")
b.persist = true
b.set_output(theme, 1280, 720)
assert_eq(b.slot_of("moon-disc"), "")
assert_true(b.slot_of("clock-binary") != "")
assert_eq(b.slots().length(), a.slots().length())
}
}
///|
test "the wasm picker opens the module manager, which loads and unloads modules" {
let comp = new_comp("manager")
// The manager is not in the default panel: the picker loads it on demand.
assert_eq(comp.shell.slot_of("module-manager"), "")
let n0 = comp.shell.slots().length()
// Right-click empty panel space (the margin): the menu offers the module manager.
let panel = comp.shell.panel_rect
click_at(comp, 1, panel.y + 5, 1U, btn=273U)
assert_true(comp.shell.menu_open)
assert_eq(comp.shell.menu_rows[0].label, "Module manager...")
let m = comp.shell.menu_rect
click_at(comp, m.x + m.w / 2, m.y + 26 + 14, 10U)
assert_eq(comp.shell.slots().length(), n0 + 1)
let mgr = comp.shell.slot_of("module-manager")
assert_true(mgr != "")
ignore(comp.compose_frame())
let pr = comp.shell.slot_popup_rect(mgr)
assert_true(pr.w > 0) // the picker opened the app's popup
// Rows: header(24), one per loaded module (28), header(24), one per available module (28), browse.
let loaded = comp.shell.slots().length()
let avail_y = fn(k : Int) { pr.y + 4 + 24 + loaded * 28 + 24 + k * 28 + 14 }
let mut k_bin = -1
for k = 0; k < comp.shell.catalog.length(); k = k + 1 {
if comp.shell.catalog[k].id == "clock-binary" {
k_bin = k
}
}
assert_true(k_bin >= 0)
assert_eq(comp.shell.slot_of("clock-binary"), "")
click_at(comp, pr.x + 40, avail_y(k_bin), 20U) // select it: it is loaded into the panel
assert_true(comp.shell.slot_of("clock-binary") != "")
assert_eq(comp.shell.slots().length(), loaded + 1)
// The popup stays open; click the loaded module's row to unload it again.
let pr2 = comp.shell.slot_popup_rect(mgr)
assert_true(pr2.w > 0)
let slots = comp.shell.slots()
let mut idx = -1
for i = 0; i < slots.length(); i = i + 1 {
if slots[i] == comp.shell.slot_of("clock-binary") {
idx = i
}
}
click_at(comp, pr2.x + 40, pr2.y + 4 + 24 + idx * 28 + 14, 30U)
assert_eq(comp.shell.slot_of("clock-binary"), "")
assert_eq(comp.shell.slots().length(), loaded)
// The manager is just another module: it can be removed too, and the picker brings it back.
assert_true(comp.shell.unload_slot(mgr))
assert_eq(comp.shell.slot_of("module-manager"), "")
assert_true(comp.shell.open_module_manager())
assert_true(comp.shell.slot_of("module-manager") != "")
}
///|
test "dragging a panel module reorders it" {
let comp = new_comp("wdrag")
let order0 = comp.shell.slots()
let moon = rect_of(comp, "moon-disc")
let clock = rect_of(comp, "clock-digital12")
assert_true(moon.x < clock.x)
let cy = (clock.y + clock.h / 2).to_double()
comp.handle_input(@nested.InputEvent::PointerMove((clock.x + clock.w / 2).to_double(), cy), 1U)
assert_eq(comp.desired_cursor(), 4U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
// Drag the clock left past the moon.
comp.handle_input(@nested.InputEvent::PointerMove((moon.x + 2).to_double(), cy), 3U)
assert_eq(comp.desired_cursor(), 17U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
ignore(comp.compose_frame())
let order1 = comp.shell.slots()
assert_eq(order1.length(), order0.length())
assert_true(order1 != order0)
assert_eq(order1[order1.length() - 2], comp.shell.slot_of("clock-digital12"))
assert_true(rect_of(comp, "clock-digital12").x < rect_of(comp, "moon-disc").x)
assert_true(comp.desired_cursor() != 17U)
}
///|
test "any .wasm plasmoid can be loaded from a path and unloaded again" {
let comp = new_comp("path")
let n0 = comp.shell.slots().length()
let theme = @scene.ThemeTokens::moon_night()
ignore(theme)
assert_true(comp.shell.load_path("compositor/wasm/testdata/clock-digital.component.wasm") is Some(_))
assert_eq(comp.shell.slots().length(), n0 + 1)
ignore(comp.compose_frame())
let slot = comp.shell.slots()[n0]
assert_true(comp.shell.slot_rect(slot).w > 0) // it got a panel placement and was drawn
assert_true(comp.shell.load_path("compositor/wasm/testdata/missing.wasm") is None)
assert_eq(comp.shell.slots().length(), n0 + 1)
// It unloads like any module.
assert_true(comp.shell.unload_slot(slot))
ignore(comp.compose_frame())
assert_eq(comp.shell.slot_rect(slot).w, 0)
}
///|
test "modules are added until the panel is full" {
let comp = new_comp("full")
let n0 = comp.shell.slots().length()
let mut added = 0
for _i = 0; _i < 40; _i = _i + 1 {
if comp.shell.load_module("moon-disc") {
added = added + 1
}
}
assert_true(added > 2 && added < 40)
assert_eq(comp.shell.slots().length(), n0 + added)
ignore(comp.compose_frame())
assert_true(rect_of(comp, "moon-disc").w > 0)
}
///|
test "Desktop Pulse reads live machine and window state through its granted interfaces and can raise a window" {
let socket_path = "/tmp/wayland-moon-pulse-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
ignore(comp.compose_frame())
let n0 = comp.shell.slots().length()
assert_true(comp.shell.load_path("compositor/wasm/testdata/pulse.component.wasm") is Some(_))
// Its capability grant is exactly what the component imports; nothing else is reachable.
let slot = comp.shell.slots()[n0]
let w = comp.shell.widgets[n0]
assert_eq(w.slot, slot)
assert_eq(w.caps.length(), 3)
assert_true(w.caps.contains("system") && w.caps.contains("theme") && w.caps.contains("windows"))
ignore(comp.compose_frame())
// The host provider fed it real numbers and the window list.
assert_true(comp.shell.sampler.cur.mem_total_kib > 0L)
assert_true(comp.shell.sampler.cur.uptime_secs > 0L)
assert_eq(comp.shell.win_list.length(), 2)
let r = comp.shell.slot_rect(slot)
assert_true(r.w >= 300)
// Clicking each window's chip raises that window (first chip starts at x = 280).
for i = 0; i < 2; i = i + 1 {
let id = comp.shell.win_list[i].id
let cx = r.x + 280 + i * 23 + 10
let cy = r.y + r.h / 2
comp.handle_input(@nested.InputEvent::PointerMove(cx.to_double(), cy.to_double()), (10 + i * 4).reinterpret_as_uint())
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), (11 + i * 4).reinterpret_as_uint())
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), (12 + i * 4).reinterpret_as_uint())
ignore(comp.compose_frame())
assert_true(comp.windows.get(id).unwrap().focused)
let others_focused = comp.windows.iter().filter(kv => kv.0 != id && kv.1.focused).count()
assert_eq(others_focused, 0)
}
}
///|
test "the desktop card can be dragged across the wallpaper" {
let socket_path = "/tmp/wayland-moon-desk-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
ignore(comp.compose_frame())
// Default: top-right, 24px in. Grab it by its body.
let gx = 1280 - 260 - 24 + 40
let gy = 24 + 30
assert_true(comp.shell.desk_hit(gx, gy))
comp.handle_input(@nested.InputEvent::PointerMove(gx.to_double(), gy.to_double()), 1U)
assert_eq(comp.desired_cursor(), 4U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
comp.handle_input(@nested.InputEvent::PointerMove(300.0, 300.0), 3U)
assert_eq(comp.desired_cursor(), 17U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
// It moved: new top-left is pointer minus the grab offset.
assert_true(comp.shell.desk_hit(300, 300))
assert_true(!comp.shell.desk_hit(gx, gy))
assert_true(!comp.shell.desk_dragging())
}
///|
test "an xdg_popup (right-click menu) is placed, drawn, clickable and dismissed from outside" {
let socket_path = "/tmp/wayland-moon-popup-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(comp, socket_path, "T", "t")
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
let win = comp.windows.get(1UL).unwrap().geometry
let reg_id = 2U
let seat_id = bind_global(c, reg_id, "wl_seat", 7U)
let ptr_id = c.alloc_id()
let w1 = @wayland.WireWriter::new()
w1.u32(ptr_id)
c.send(w1.finish_msg(seat_id, 0U))
let comp_id = bind_global(c, reg_id, "wl_compositor", 5U)
let wm_id = bind_global(c, reg_id, "xdg_wm_base", 5U)
// Positioner: 100x60 popup whose top-left sits at (50, 60) in the window.
let pos_id = c.alloc_id()
let a = @wayland.WireWriter::new()
a.u32(pos_id)
c.send(a.finish_msg(wm_id, 1U))
let b = @wayland.WireWriter::new()
b.i32(100)
b.i32(60)
c.send(b.finish_msg(pos_id, 1U))
let d = @wayland.WireWriter::new()
d.i32(50)
d.i32(60)
d.i32(1)
d.i32(1)
c.send(d.finish_msg(pos_id, 2U))
let e = @wayland.WireWriter::new()
e.u32(2U) // anchor: top-left of the rect (bottom edge); use top_left below
c.send(e.finish_msg(pos_id, 3U))
let f = @wayland.WireWriter::new()
f.u32(8U) // gravity bottom_right: popup extends right and down from the anchor point
c.send(f.finish_msg(pos_id, 4U))
let psurf = c.alloc_id()
let g = @wayland.WireWriter::new()
g.u32(psurf)
c.send(g.finish_msg(comp_id, 0U))
let pxdg = c.alloc_id()
let h = @wayland.WireWriter::new()
h.u32(pxdg)
h.u32(psurf)
c.send(h.finish_msg(wm_id, 2U))
let popup_id = c.alloc_id()
let i = @wayland.WireWriter::new()
i.u32(popup_id)
i.u32(xdg)
i.u32(pos_id)
c.send(i.finish_msg(pxdg, 2U))
c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U))
for _n = 0; _n < 5; _n = _n + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_eq(comp.popups.length(), 1)
let msgs = drain_msgs(c)
assert_true(has_msg(msgs, popup_id, 0U)) // xdg_popup.configure
// Attach a 100x60 buffer.
let bytes = 100 * 60 * 4
let pixels : Array[Byte] = Array::make(bytes, b'\xff')
let memfd = @sys.memfd_create("popup-shm", bytes)
ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(pixels), 0))
let pool = c.alloc_id()
let j = @wayland.WireWriter::new()
j.u32(pool)
j.i32(bytes)
c.send(j.finish_msg(shm, 0U), send_fd=memfd)
@sys.close_fd(memfd)
let buf = c.alloc_id()
let k = @wayland.WireWriter::new()
k.u32(buf)
k.i32(0)
k.i32(100)
k.i32(60)
k.i32(400)
k.u32(0U)
c.send(k.finish_msg(pool, 0U))
let l = @wayland.WireWriter::new()
l.u32(buf)
l.i32(0)
l.i32(0)
c.send(l.finish_msg(psurf, 1U))
c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U))
for _n = 0; _n < 10; _n = _n + 1 {
comp.step()
@sys.sleep_ms(2)
}
let p = comp.popups[0]
assert_true(p.surface is Some(_))
let px = win.x + 50 + 5
let py = win.y + 60 + 5
// Clicking inside the popup reaches the client (not the window beneath).
drain_msgs(c) |> ignore
click_at(comp, px, py, 10U)
let msgs2 = drain_msgs(c)
assert_true(has_msg(msgs2, ptr_id, 0U)) // pointer enter on the popup surface
assert_true(has_msg(msgs2, ptr_id, 3U)) // button
// The popup is drawn above the window.
let pm = comp.compose_frame()
let (_, _, _, _) = (pm.width, pm.height, 0, 0)
// Clicking outside asks it to close.
click_at(comp, win.x + win.w - 20, win.y + win.h - 20, 20U)
let msgs3 = drain_msgs(c)
assert_true(has_msg(msgs3, popup_id, 1U)) // xdg_popup.popup_done
// The client destroys it: gone from the compositor.
c.send(@wayland.WireWriter::new().finish_msg(popup_id, 0U))
for _n = 0; _n < 5; _n = _n + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_eq(comp.popups.length(), 0)
}
///|
test "the dock module magnifies the tile under the pointer and shows nothing when the pointer leaves" {
let comp = new_comp("dock")
assert_true(comp.shell.load_path("build/components/dock.wasm") is Some(_))
ignore(comp.compose_frame())
let slot = comp.shell.slots()[comp.shell.slots().length() - 1]
let r = comp.shell.slot_rect(slot)
assert_true(r.w > 0)
let frame = fn() {
ignore(comp.compose_frame())
}
// Pointer over the dock: it becomes the hovered widget.
comp.handle_input(@nested.InputEvent::PointerMove((r.x + 14).to_double(), (r.y + r.h / 2).to_double()), 1U)
frame()
assert_eq(comp.shell.hover_slot, slot)
// Off the panel: the dock gets a leave.
comp.handle_input(@nested.InputEvent::PointerMove(640.0, 300.0), 2U)
assert_eq(comp.shell.hover_slot, "")
}
///|
test "dragging a title bar moves the window and floats it" {
let socket_path = "/tmp/wayland-moon-wdrag-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
let g0 = comp.windows.get(2UL).unwrap().geometry
let sx = g0.x + g0.w / 3
let sy = g0.y - 16 // inside the title bar, clear of the buttons
comp.handle_input(@nested.InputEvent::PointerMove(sx.to_double(), sy.to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
comp.handle_input(@nested.InputEvent::PointerMove((sx - 120).to_double(), (sy + 60).to_double()), 3U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
let g1 = comp.windows.get(2UL).unwrap().geometry
assert_eq(g1.x, g0.x - 120)
assert_eq(g1.y, g0.y + 60)
assert_eq(g1.w, g0.w)
// The other window now has the whole tiling area.
assert_true(comp.windows.get(1UL).unwrap().geometry.w > g0.w)
}
///|
test "dragging a floating window's corner resizes it" {
let socket_path = "/tmp/wayland-moon-rsz-" +
@sys.getpid().to_string() +
".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
let _a = open_and_configure_client(comp, socket_path, "A", "a")
let _b = open_and_configure_client(comp, socket_path, "B", "b")
// Float window 2 by dragging its title bar a little.
let g = comp.windows.get(2UL).unwrap().geometry
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y - 16).to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
comp.handle_input(@nested.InputEvent::PointerMove((g.x + 40).to_double(), (g.y - 6).to_double()), 3U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 4U)
let g0 = comp.windows.get(2UL).unwrap().geometry
let cx = g0.x + g0.w - 1
let cy = g0.y + g0.h - 1
comp.handle_input(@nested.InputEvent::PointerMove(cx.to_double(), cy.to_double()), 5U)
assert_eq(comp.desired_cursor(), 29U) // nwse-resize on the bottom-right corner
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 6U)
comp.handle_input(@nested.InputEvent::PointerMove((cx - 50).to_double(), (cy - 40).to_double()), 7U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 8U)
let g1 = comp.windows.get(2UL).unwrap().geometry
assert_eq(g1.x, g0.x)
assert_eq(g1.y, g0.y)
assert_eq(g1.w, g0.w - 50)
assert_eq(g1.h, g0.h - 40)
}