Right-click menus inside apps: keep them open and on screen
CI / test (push) Failing after 15s

Chrome's context menu opened and closed at once: when a client created an
xdg_popup the compositor moved keyboard focus to it straight away, and the
wl_keyboard.leave on the window made Chrome dismiss the menu. Popups now
only take the keyboard when they ask for a grab (xdg_popup.grab, as Qt
menus do) and once they have a buffer; Chrome's grab-less menus leave
focus on the window.

Also:
- honour xdg_positioner constraint adjustment (flip, slide, resize) so the
  configure tells the client where its menu really is, instead of the host
  silently nudging it on screen (Chrome then took the button release as a
  click on a menu item);
- answer xdg_popup.reposition with repositioned + configure (it was ignored,
  so clients waiting for it stalled);
- place popups by their window geometry, so Chrome's client-drawn menu
  shadow no longer shifts the menu right and down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hikqzveCoytgF4Uyvh
This commit is contained in:
Claude committed 2026-11-25 19:03:04 +00:00
1 parent 3454619162
commit b33373d801
3 files changed
+563 -20

No files matched your search

+67 -16
View File
@@ -19,11 +19,17 @@ pub(all) struct HostPopup {
id : UInt64
parent : UInt64
/// Position relative to the parent's content origin.
rx : Int
ry : Int
w : Int
h : Int
mut rx : Int
mut ry : Int
mut w : Int
mut h : Int
mut surface : @scene.ShmBufferSnapshot?
/// The client asked for an explicit grab (xdg_popup.grab): it takes keyboard focus once mapped.
mut grab : Bool
/// Window-geometry origin inside the buffer (client-drawn shadow); the buffer is drawn
/// this much up/left so the visible menu lands where the positioner put it.
mut geo_x : Int
mut geo_y : Int
}
///|
@@ -111,7 +117,7 @@ pub fn ChandraCompositor::new(
let work_area = work_area_for(output_w, output_h, theme)
let shell = @shell.DesktopShell::new(socket_path)
shell.set_output(theme, output_w, output_h)
Some({
let comp : ChandraCompositor = {
server,
wm,
theme,
@@ -154,7 +160,14 @@ pub fn ChandraCompositor::new(
session_seen: "",
session_seen_at: 0UL,
session_check_at: 0UL,
})
}
// Client popups (menus) are placed to stay on screen: the server asks for the output's
// rectangle relative to the parent's content origin when it applies constraint adjustment.
server.popup_bounds = fn(parent) {
let (ox, oy) = comp.origin_of(parent)
(-ox, -oy, comp.output_w, comp.output_h)
}
Some(comp)
}
///| The part of a `w` x `h` output windows are laid out in.
@@ -364,16 +377,48 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
None => ()
}
@wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => {
self.popups.push({ id: wid, parent, rx, ry, w, h, surface: None })
self.set_kbd_focus(Some(wid))
// Keyboard focus stays on the parent until the popup grabs and maps: moving it away
// early makes clients (Chrome, Qt) take the keyboard leave as "dismiss the menu".
self.popups.push({ id: wid, parent, rx, ry, w, h, surface: None, grab: false, geo_x: 0, geo_y: 0 })
}
@wayland.CompositorEvent::PopupGeometry(wid, gx, gy) =>
for p in self.popups {
if p.id == wid {
p.geo_x = gx
p.geo_y = gy
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupMoved(wid, rx, ry, w, h) =>
for p in self.popups {
if p.id == wid {
p.rx = rx
p.ry = ry
p.w = w
p.h = h
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupGrabbed(wid) =>
for p in self.popups {
if p.id == wid {
p.grab = true
if p.surface is Some(_) {
self.set_kbd_focus(Some(wid))
}
}
}
@wayland.CompositorEvent::PopupCommitted(wid, snap) =>
for p in self.popups {
if p.id == wid {
match snap {
Some(_) => {
let first_map = p.surface is None
release_snapshot(p.surface)
p.surface = snap
if first_map && p.grab {
self.set_kbd_focus(Some(wid))
}
}
None => ()
}
@@ -655,7 +700,8 @@ pub fn ChandraCompositor::handle_gfx_events(
}
///| Content origin of a window or popup.
///| Content origin of a window or popup (a popup's window geometry, which child popups'
/// positioners are relative to).
fn ChandraCompositor::origin_of(self : ChandraCompositor, wid : UInt64) -> (Int, Int) {
match self.windows.get(wid) {
Some(win) => (win.geometry.x, win.geometry.y)
@@ -663,7 +709,7 @@ fn ChandraCompositor::origin_of(self : ChandraCompositor, wid : UInt64) -> (Int,
for p in self.popups {
if p.id == wid {
let r = self.popup_rect(p)
return (r.x, r.y)
return (r.x + p.geo_x, r.y + p.geo_y)
}
}
(0, 0)
@@ -671,18 +717,23 @@ fn ChandraCompositor::origin_of(self : ChandraCompositor, wid : UInt64) -> (Int,
}
}
///| Absolute rectangle of a popup, nudged to stay on screen.
///| Absolute rectangle of a popup's buffer. Its visible part (the window geometry the
/// positioner placed, `p.w` x `p.h`) is nudged to stay on screen as a safety net; the server
/// already keeps adjustable popups inside the output, and then no nudge is needed, so the
/// client's idea of where its menu is matches what is drawn.
fn ChandraCompositor::popup_rect(self : ChandraCompositor, p : HostPopup) -> @scene.Rect {
let (w, h) = match p.surface {
Some(s) => (s.width, s.height)
None => (p.w, p.h)
}
let (bx, by) = self.origin_of_parent(p)
let x = bx + p.rx
let y = by + p.ry
let x = if x + w > self.output_w { self.output_w - w } else { x }
let y = if y + h > self.output_h { self.output_h - h } else { y }
@scene.Rect::new(if x < 0 { 0 } else { x }, if y < 0 { 0 } else { y }, w, h)
let vx = bx + p.rx
let vy = by + p.ry
let vx = if vx + p.w > self.output_w { self.output_w - p.w } else { vx }
let vy = if vy + p.h > self.output_h { self.output_h - p.h } else { vy }
let vx = if vx < 0 { 0 } else { vx }
let vy = if vy < 0 { 0 } else { vy }
@scene.Rect::new(vx - p.geo_x, vy - p.geo_y, w, h)
}
///|
+327
View File
@@ -0,0 +1,327 @@
///| Client right-click menus (xdg_popup): keyboard focus, on-screen placement, reposition,
///| and client-drawn shadows (window geometry).
///|
struct PopupRig {
comp : @host.ChandraCompositor
c : TestWaylandClient
shm : UInt
xdg : UInt
comp_id : UInt
wm_id : UInt
ptr_id : UInt
kbd_id : UInt
win : @scene.Rect
}
///| A compositor with one mapped window whose client has a pointer and a keyboard.
fn popup_rig(tag : String) -> PopupRig raise {
let socket_path = "/tmp/wayland-moon-cpopup-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.ChandraCompositor::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 seat_id = bind_global(c, 2U, "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))
let comp_id = bind_global(c, 2U, "wl_compositor", 5U)
let wm_id = bind_global(c, 2U, "xdg_wm_base", 5U)
pump(comp, 5)
let win = comp.windows.get(1UL).unwrap().geometry
{ comp, c, shm, xdg, comp_id, wm_id, ptr_id, kbd_id, win }
}
///|
fn pump(comp : @host.ChandraCompositor, n : Int) -> Unit {
for _i = 0; _i < n; _i = _i + 1 {
comp.step()
@sys.sleep_ms(2)
}
}
///| A positioner: `w`x`h` popup, 1x1 anchor rect at (ax, ay) in the parent, anchor/gravity
/// enums and constraint_adjustment bits.
fn PopupRig::positioner(
self : PopupRig,
w : Int,
h : Int,
ax : Int,
ay : Int,
anchor : UInt,
gravity : UInt,
adjust : UInt,
) -> UInt {
let c = self.c
let pos_id = c.alloc_id()
let a = @wayland.WireWriter::new()
a.u32(pos_id)
c.send(a.finish_msg(self.wm_id, 1U))
let b = @wayland.WireWriter::new()
b.i32(w)
b.i32(h)
c.send(b.finish_msg(pos_id, 1U))
let d = @wayland.WireWriter::new()
d.i32(ax)
d.i32(ay)
d.i32(1)
d.i32(1)
c.send(d.finish_msg(pos_id, 2U))
let e = @wayland.WireWriter::new()
e.u32(anchor)
c.send(e.finish_msg(pos_id, 3U))
let f = @wayland.WireWriter::new()
f.u32(gravity)
c.send(f.finish_msg(pos_id, 4U))
let g = @wayland.WireWriter::new()
g.u32(adjust)
c.send(g.finish_msg(pos_id, 5U))
pos_id
}
///| Open a popup on the window from a positioner; returns (wl_surface, xdg_surface, xdg_popup).
fn PopupRig::open_popup(self : PopupRig, pos_id : UInt, geometry? : (Int, Int, Int, Int)? = None) -> (UInt, UInt, UInt) {
let c = self.c
let psurf = c.alloc_id()
let g = @wayland.WireWriter::new()
g.u32(psurf)
c.send(g.finish_msg(self.comp_id, 0U))
let pxdg = c.alloc_id()
let h = @wayland.WireWriter::new()
h.u32(pxdg)
h.u32(psurf)
c.send(h.finish_msg(self.wm_id, 2U))
let popup_id = c.alloc_id()
let i = @wayland.WireWriter::new()
i.u32(popup_id)
i.u32(self.xdg)
i.u32(pos_id)
c.send(i.finish_msg(pxdg, 2U))
match geometry {
Some((gx, gy, gw, gh)) => {
let w = @wayland.WireWriter::new()
w.i32(gx)
w.i32(gy)
w.i32(gw)
w.i32(gh)
c.send(w.finish_msg(pxdg, 3U))
}
None => ()
}
c.send(@wayland.WireWriter::new().finish_msg(psurf, 6U))
pump(self.comp, 5)
(psurf, pxdg, popup_id)
}
///| xdg_popup.grab(seat, serial).
fn PopupRig::grab(self : PopupRig, popup_id : UInt) -> Unit {
let w = @wayland.WireWriter::new()
w.u32(0U)
w.u32(0U)
self.c.send(w.finish_msg(popup_id, 1U))
pump(self.comp, 3)
}
///| Attach a w x h shm buffer to the popup surface and commit it.
fn PopupRig::map_popup(self : PopupRig, psurf : UInt, w : Int, h : Int) -> Unit {
let c = self.c
let bytes = w * h * 4
let memfd = @sys.memfd_create("cpopup-shm", bytes)
ignore(@sys.pwrite_bytes(memfd, Bytes::from_array(Array::make(bytes, b'\xff')), 0))
let pool = c.alloc_id()
let j = @wayland.WireWriter::new()
j.u32(pool)
j.i32(bytes)
c.send(j.finish_msg(self.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(w)
k.i32(h)
k.i32(w * 4)
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))
pump(self.comp, 10)
}
///| Everything the compositor sent, with payloads: (object, opcode, payload bytes).
fn drain_full(c : TestWaylandClient) -> Array[(UInt, UInt, Array[Byte])] {
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, Array[Byte])] = []
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 || off + size > raw.length() {
break
}
out.push((obj, w2 & 0xFFFFU, raw[off + 8:off + size].to_array()))
off = off + size
}
out
}
///| Signed 32-bit argument `i` of a payload.
fn arg_i32(p : Array[Byte], i : Int) -> Int {
@wayland.read_u32_le(p, i * 4).reinterpret_as_int()
}
///| The last xdg_popup.configure (x, y, w, h) in `msgs`.
fn last_configure(msgs : Array[(UInt, UInt, Array[Byte])], popup_id : UInt) -> (Int, Int, Int, Int)? {
let mut found = None
for m in msgs {
if m.0 == popup_id && m.1 == 0U {
found = Some((arg_i32(m.2, 0), arg_i32(m.2, 1), arg_i32(m.2, 2), arg_i32(m.2, 3)))
}
}
found
}
///|
test "a right-click menu without a grab leaves keyboard focus on its window (Chrome)" {
let r = popup_rig("nograb")
assert_eq(r.comp.kbd_win, Some(1UL))
drain_full(r.c) |> ignore
let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U)
let (psurf, _, _) = r.open_popup(pos)
r.map_popup(psurf, 200, 150)
assert_eq(r.comp.popups.length(), 1)
// No wl_keyboard.leave: Chrome reads that as "dismiss the menu" and destroys it at once.
let msgs = drain_full(r.c)
assert_false(msgs.iter().any(m => m.0 == r.kbd_id && m.1 == 2U))
assert_eq(r.comp.kbd_win, Some(1UL))
}
///|
test "a grabbing right-click menu takes keyboard focus once it is drawn (Qt)" {
let r = popup_rig("grab")
let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U)
let (psurf, _, popup_id) = r.open_popup(pos)
r.grab(popup_id)
// Not mapped yet: the window keeps the keyboard.
assert_eq(r.comp.kbd_win, Some(1UL))
r.map_popup(psurf, 200, 150)
let pwid = r.comp.popups[0].id
assert_eq(r.comp.kbd_win, Some(pwid))
// Dismissed from outside and destroyed: the keyboard goes back to the window.
click_at(r.comp, r.win.x + r.win.w - 20, r.win.y + r.win.h - 20, 50U)
assert_true(has_msg(drain_msgs(r.c), popup_id, 1U)) // popup_done
r.c.send(@wayland.WireWriter::new().finish_msg(popup_id, 0U))
pump(r.comp, 5)
assert_eq(r.comp.popups.length(), 0)
assert_eq(r.comp.kbd_win, Some(1UL))
}
///|
test "a menu opened near the bottom flips above the pointer when the client allows it" {
let r = popup_rig("flip")
drain_full(r.c) |> ignore
// Window content is r.win.h tall; ask for a 300px menu growing down from 40px above its
// bottom edge, with flip_y (8) allowed.
let ay = r.win.h - 40
let pos = r.positioner(200, 300, 100, ay, 6U, 8U, 8U)
let (_, _, popup_id) = r.open_popup(pos)
let (x, y, w, h) = last_configure(drain_full(r.c), popup_id).unwrap()
// anchor bottom_left -> top_left, gravity bottom_right -> top_right: ends at the anchor.
assert_eq((x, y, w, h), (100, ay - 300, 200, 300))
assert_eq(r.comp.popups[0].ry, ay - 300)
}
///|
test "a menu that cannot flip slides or shrinks to stay on screen, as the client allows" {
let r = popup_rig("slide")
drain_full(r.c) |> ignore
// Screen bottom relative to the window's content origin.
let bottom = 720 - r.win.y
let ay = r.win.h - 40
// Too tall to fit above either, slide_y (2) only: pushed up to end at the screen bottom.
let p1 = r.positioner(200, 700, 100, ay, 6U, 8U, 2U)
let (_, _, id1) = r.open_popup(p1)
let (_, y1, _, h1) = last_configure(drain_full(r.c), id1).unwrap()
assert_eq((y1, h1), (bottom - 700, 700))
// resize_y (32) only: keeps its place and is cut to the space left below it.
let p2 = r.positioner(200, 300, 100, ay, 6U, 8U, 32U)
let (_, _, id2) = r.open_popup(p2)
let (_, y2, _, h2) = last_configure(drain_full(r.c), id2).unwrap()
assert_eq((y2, h2), (ay + 1, bottom - ay - 1))
// Off the right edge with slide_x (1): pushed left to end at the screen's right edge.
let right = 1280 - r.win.x
let p3 = r.positioner(300, 100, r.win.w - 10, 100, 6U, 8U, 1U)
let (_, _, id3) = r.open_popup(p3)
let (x3, _, w3, _) = last_configure(drain_full(r.c), id3).unwrap()
assert_eq((x3, w3), (right - 300, 300))
// No adjustment allowed: placed exactly where asked.
let p4 = r.positioner(200, 300, 100, ay, 6U, 8U, 0U)
let (_, _, id4) = r.open_popup(p4)
let (_, y4, _, _) = last_configure(drain_full(r.c), id4).unwrap()
assert_eq(y4, ay + 1)
}
///|
test "xdg_popup.reposition answers with repositioned and a new configure" {
let r = popup_rig("repos")
let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U)
let (psurf, _, popup_id) = r.open_popup(pos)
r.map_popup(psurf, 200, 150)
drain_full(r.c) |> ignore
let pos2 = r.positioner(250, 180, 300, 200, 6U, 8U, 0U)
let w = @wayland.WireWriter::new()
w.u32(pos2)
w.u32(77U)
r.c.send(w.finish_msg(popup_id, 2U))
pump(r.comp, 5)
let msgs = drain_full(r.c)
let token = msgs.iter().find_first(m => m.0 == popup_id && m.1 == 2U).unwrap()
assert_eq(arg_i32(token.2, 0), 77)
assert_eq(last_configure(msgs, popup_id), Some((300, 201, 250, 180)))
assert_eq((r.comp.popups[0].rx, r.comp.popups[0].ry), (300, 201))
}
///|
test "a menu with a client-drawn shadow is placed by its window geometry, not its buffer" {
let r = popup_rig("shadow")
// Chrome: 200x150 menu inside a 240x182 buffer, the menu starting at (20, 12).
let pos = r.positioner(200, 150, 100, 100, 6U, 8U, 0U)
let (psurf, _, _) = r.open_popup(pos, geometry=Some((20, 12, 200, 150)))
r.map_popup(psurf, 240, 182)
let p = r.comp.popups[0]
assert_eq((p.geo_x, p.geo_y), (20, 12))
// The visible menu's top-left is at the configured spot; a point 5px inside it is 25, 17
// into the buffer.
let vx = r.win.x + 100
let vy = r.win.y + 101
drain_full(r.c) |> ignore
r.comp.handle_input(@nested.InputEvent::PointerMove((vx + 5).to_double(), (vy + 5).to_double()), 10U)
let msgs = drain_full(r.c)
let enter = msgs.iter().find_first(m => m.0 == r.ptr_id && m.1 == 0U).unwrap()
// enter(serial, surface, x: fixed, y: fixed)
assert_eq(arg_i32(enter.2, 2) / 256, 25)
assert_eq(arg_i32(enter.2, 3) / 256, 17)
}