Files
chandra/compositor/host/csd_test.mbt
T
Claude fa932f9ac0
CI / test (push) Successful in 3m44s
GTK windows (Gajim, GNOME apps) resize from their shadow and all four edges
GTK draws no border, only a shadow, and resizes only from a press in that
shadow, which Chandra never hands to the client. Chandra's own zones were
~4px wide around the visible edge and there was none on the top edge, so
Gajim (GTK 4) looked unresizable. Client-decorated windows now get zones
that reach up to 12px into the shadow (GTK's own handle size, at least 6px)
plus 3px over their edge, with 16px corners, top edge included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiu4r7mEXBdKY6cgv6
2026-11-26 02:48:22 +00:00

323 lines
14 KiB
MoonBit

///| Client-side decorated windows: GTK apps (GNOME Files, most GNOME apps) never ask for
/// server-side decoration, draw their own title bar, and put a shadow around it in the buffer.
///| The shadow a GTK window draws around itself, in buffer pixels.
let gtk_shadow : Int = 24
///| Answer the last configure the way GTK does: a buffer `w` x `h` plus the shadow on every
/// side, the visible window (solid red, its own title bar included) announced with
/// set_window_geometry, the shadow transparent.
fn gtk_frame(
comp : @host.ChandraCompositor,
c : TestWaylandClient,
shm : UInt,
surf : UInt,
xdg : UInt,
tl : UInt,
) -> (Int, Int) {
for _i = 0; _i < 6; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, 0U)
@sys.sleep_ms(2)
}
for cfg in c.configures {
let ack = @wayland.WireWriter::new()
ack.u32(cfg.3)
c.send(ack.finish_msg(xdg, 4U))
}
c.configures.clear()
let w = c.configured_w
let h = c.configured_h
let s = gtk_shadow
let bw = w + 2 * s
let bh = h + 2 * s
let px : Array[Byte] = Array(capacity=bw * bh * 4)
for y = 0; y < bh; y = y + 1 {
for x = 0; x < bw; x = x + 1 {
if x >= s && x < s + w && y >= s && y < s + h {
px.push(b'\x00')
px.push(b'\x00')
px.push(b'\xff')
px.push(b'\xff')
} else {
for _k = 0; _k < 4; _k = _k + 1 {
px.push(b'\x00')
}
}
}
}
let fd = @sys.memfd_create("gtk-frame", bw * bh * 4)
ignore(@sys.pwrite_bytes(fd, Bytes::from_array(px), 0))
let pool = c.alloc_id()
let p = @wayland.WireWriter::new()
p.u32(pool)
p.i32(bw * bh * 4)
c.send(p.finish_msg(shm, 0U), send_fd=fd)
@sys.close_fd(fd)
let buf = c.alloc_id()
let cb = @wayland.WireWriter::new()
cb.u32(buf)
cb.i32(0)
cb.i32(bw)
cb.i32(bh)
cb.i32(bw * 4)
cb.u32(0U)
c.send(cb.finish_msg(pool, 0U))
// xdg_surface.set_window_geometry(x, y, w, h)
let g = @wayland.WireWriter::new()
g.i32(s)
g.i32(s)
g.i32(w)
g.i32(h)
c.send(g.finish_msg(xdg, 3U))
attach_commit(comp, c, surf, Some(buf))
(w, h)
}
///|
fn csd_setup(name : String) -> (@host.ChandraCompositor, TestWaylandClient, UInt, UInt, UInt, UInt) {
let socket_path = "/tmp/wayland-moon-csd-" + name + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(
comp,
socket_path,
"Home",
"org.gnome.Nautilus",
server_deco=false,
)
(comp, c, shm, surf, xdg, tl)
}
///|
test "a GTK window draws its own frame: no second title bar, and its shadow is not the window" {
// GNOME Files showed its own header bar inside a Chandra-titled frame, with its transparent
// shadow margin drawn as part of the window (content shifted down/right and cut off).
let (comp, c, shm, surf, xdg, tl) = csd_setup("frame")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.csd)
assert_eq((win.geo_x, win.geo_y), (gtk_shadow, gtk_shadow))
// The client is asked to fill the frame's whole box: where Chandra's title bar would be is its own.
let r = win.client_rect()
assert_eq((r.w, r.h), (win.geometry.w + 4, win.geometry.h + 34))
let (w, h) = gtk_frame(comp, c, shm, surf, xdg, tl)
assert_eq((w, h), (r.w, r.h))
let red = Some(@scene.Rgba::new(0xff, 0, 0, 0xff))
let frame = comp.compose_frame()
// The client's pixels start right at the box's top-left (no Chandra title bar over them)
// and fill it to the bottom-right corner.
assert_eq(frame.pixel(r.x + 3, r.y + 3), red)
assert_eq(frame.pixel(r.x + r.w - 3, r.y + r.h - 3), red)
// The transparent shadow is not drawn as part of the window.
assert_true(frame.pixel(r.x + r.w + 10, r.y + 10) != red)
assert_true(frame.pixel(r.x + 10, r.y - 10) != red)
}
///|
test "a server-decorated client keeps Chandra's frame" {
let socket_path = "/tmp/wayland-moon-csd-ssd-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(comp, socket_path, "Dolphin", "org.kde.dolphin")
let size_for = fn(w, h, _max) { (w, h) }
ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for))
let win = comp.windows.get(1UL).unwrap()
assert_false(win.csd)
let r = win.client_rect()
assert_eq((r.x, r.y, r.w, r.h), (win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h))
}
///|
test "dragging a GTK header bar moves the window, and its border resizes it" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("move")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let before = comp.windows.get(1UL).unwrap().geometry
let r = comp.windows.get(1UL).unwrap().client_rect()
// Press on the header bar (the client's own), which answers with xdg_toplevel.move.
let (px, py) = (r.x + r.w / 2, r.y + 15)
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 11U)
assert_true(comp.wm.drag is None) // the press went to the client, not to a Chandra title bar
let mv = @wayland.WireWriter::new()
mv.u32(0U) // seat
mv.u32(1U) // serial
c.send(mv.finish_msg(tl, 5U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
assert_true(comp.wm.drag is Some(_))
comp.handle_input(@nested.InputEvent::PointerMove((px + 60).to_double(), (py + 40).to_double()), 12U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 13U)
let after = comp.windows.get(1UL).unwrap().geometry
assert_eq((after.x - before.x, after.y - before.y), (60, 40))
assert_eq((after.w, after.h), (before.w, before.h))
// xdg_toplevel.resize from the bottom-right corner (edges 10).
let r = comp.windows.get(1UL).unwrap().client_rect()
let (px, py) = (r.x + r.w - 2, r.y + r.h - 2)
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 20U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 21U)
let rs = @wayland.WireWriter::new()
rs.u32(0U)
rs.u32(2U)
rs.u32(10U)
c.send(rs.finish_msg(tl, 6U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
if comp.wm.drag is Some(_) {
comp.handle_input(@nested.InputEvent::PointerMove((px + 30).to_double(), (py + 20).to_double()), 22U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 23U)
let resized = comp.windows.get(1UL).unwrap().geometry
assert_eq((resized.w - after.w, resized.h - after.h), (30, 20))
} else {
// The press landed in one of Chandra's own resize zones instead: also a resize.
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 23U)
}
}
///|
test "Chrome asking to draw its own frame gets it, instead of a second one around it" {
// Chrome with "Use system title bar and borders" off (and its installed web apps) asks for
// client-side decoration and keeps drawing its own title bar and shadow even when told
// server-side: Chandra's title bar showed above Chrome's, with Chrome's shadow as a gap.
let socket_path = "/tmp/wayland-moon-csd-chrome-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, deco) = open_and_configure_client(comp, socket_path, "YouTube", "chrome-youtube")
assert_eq(c.deco_mode, Some(2U))
// zxdg_toplevel_decoration_v1.set_mode(client_side)
let m = @wayland.WireWriter::new()
m.u32(1U)
c.send(m.finish_msg(deco, 1U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, deco)
@sys.sleep_ms(2)
}
assert_eq(c.deco_mode, Some(1U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.csd)
let r = win.client_rect()
assert_eq((r.w, r.h), (win.geometry.w + 4, win.geometry.h + 34))
// unset_mode: back to Chandra's frame.
c.send(@wayland.WireWriter::new().finish_msg(deco, 2U))
for _i = 0; _i < 4; _i = _i + 1 {
comp.step()
c.read_events(xdg, tl, deco)
@sys.sleep_ms(2)
}
assert_eq(c.deco_mode, Some(2U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
assert_false(comp.windows.get(1UL).unwrap().csd)
}
///|
test "a GTK 3 window asking through org_kde_kwin_server_decoration gets Chandra's frame" {
// GTK 3 has no xdg-decoration support: without this protocol every GTK 3 window (Webby) drew
// its own fallback title bar, with only a close button, inside a black border.
let socket_path = "/tmp/wayland-moon-csd-kde-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.set_layout_mode(@wm.LayoutMode::Floating)
let (c, shm, surf, xdg, tl, _) = open_and_configure_client(
comp,
socket_path,
"Webby",
"webby",
server_deco=false,
)
assert_true(c.globals.contains("org_kde_kwin_server_decoration_manager"))
let mgr = bind_global(c, 2U, "org_kde_kwin_server_decoration_manager", 1U)
// create(id, surface), then request_mode(Server) as gdk_wayland_window_announce_ssd does.
let deco = c.alloc_id()
let cr = @wayland.WireWriter::new()
cr.u32(deco)
cr.u32(surf)
c.send(cr.finish_msg(mgr, 0U))
let rm = @wayland.WireWriter::new()
rm.u32(2U)
c.send(rm.finish_msg(deco, 1U))
let size_for = fn(w, h, _max) { (w, h) }
ignore(answer_configures(comp, c, shm, surf, xdg, tl, size_for))
let win = comp.windows.get(1UL).unwrap()
assert_false(win.csd)
let r = win.client_rect()
assert_eq((r.x, r.y, r.w, r.h), (win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h))
// request_mode(Client): a GTK 3 app with its own header bar keeps drawing its frame.
let rc = @wayland.WireWriter::new()
rc.u32(1U)
c.send(rc.finish_msg(deco, 1U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
assert_true(comp.windows.get(1UL).unwrap().csd)
}
///|
test "a GTK window resizes from its shadow and from every edge, top included" {
// Gajim (GTK 4) couldn't be resized: GTK only resizes from a press in its shadow, which
// Chandra keeps, and Chandra's own zones were a few pixels wide with none on the top edge.
let (comp, c, shm, surf, xdg, tl) = csd_setup("grips")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let drag = fn(px : Int, py : Int, dx : Int, dy : Int) -> @scene.Rect raise {
comp.handle_input(@nested.InputEvent::PointerMove(px.to_double(), py.to_double()), 30U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 31U)
assert_true(comp.wm.drag is Some(_))
comp.handle_input(@nested.InputEvent::PointerMove((px + dx).to_double(), (py + dy).to_double()), 32U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 33U)
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
comp.windows.get(1UL).unwrap().client_rect()
}
// 10px out into the shadow on the right: wider.
let r0 = comp.windows.get(1UL).unwrap().client_rect()
let r1 = drag(r0.x + r0.w + 10, r0.y + r0.h / 2, 30, 0)
assert_eq((r1.x, r1.y, r1.w, r1.h), (r0.x, r0.y, r0.w + 30, r0.h))
// The top edge (the header bar's top 2px): taller, growing upwards.
let r2 = drag(r1.x + r1.w / 2, r1.y + 1, 0, -20)
assert_eq((r2.x, r2.y, r2.w, r2.h), (r1.x, r1.y - 20, r1.w, r1.h + 20))
// 8px out past the bottom-left corner: both at once.
let r3 = drag(r2.x - 8, r2.y + r2.h + 8, -10, 10)
assert_eq((r3.x, r3.y, r3.w, r3.h), (r2.x - 10, r2.y, r2.w + 10, r2.h + 10))
// Further out than GTK's 12px handles is the desktop again, and the header bar itself
// (below its top edge) still goes to the client.
comp.handle_input(@nested.InputEvent::PointerMove((r3.x + r3.w + 20).to_double(), (r3.y + 100).to_double()), 40U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 41U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 42U)
comp.handle_input(@nested.InputEvent::PointerMove((r3.x + r3.w / 2).to_double(), (r3.y + 15).to_double()), 43U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 44U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 45U)
}
///|
test "a maximized GTK window has no resize zones, and restores to its size" {
let (comp, c, shm, surf, xdg, tl) = csd_setup("max")
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let before = comp.windows.get(1UL).unwrap().client_rect()
// The header bar's maximize button: xdg_toplevel.set_maximized.
c.send(@wayland.WireWriter::new().finish_msg(tl, 9U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let win = comp.windows.get(1UL).unwrap()
assert_true(win.maximized)
let m = win.client_rect()
assert_true(m.w > before.w && m.h > before.h)
comp.handle_input(@nested.InputEvent::PointerMove((m.x + m.w - 1).to_double(), (m.y + m.h / 2).to_double()), 50U)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, true), 51U)
assert_true(comp.wm.drag is None)
comp.handle_input(@nested.InputEvent::PointerButton(0x110U, false), 52U)
// ... and unset_maximized puts it back.
c.send(@wayland.WireWriter::new().finish_msg(tl, 10U))
ignore(gtk_frame(comp, c, shm, surf, xdg, tl))
let after = comp.windows.get(1UL).unwrap().client_rect()
assert_false(comp.windows.get(1UL).unwrap().maximized)
assert_eq((after.w, after.h), (before.w, before.h))
}