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

+169 -4
View File
@@ -14,6 +14,13 @@ pub(all) enum CompositorEvent {
PopupOpened(UInt64, UInt64, Int, Int, Int, Int)
PopupCommitted(UInt64, @scene.ShmBufferSnapshot?)
PopupClosed(UInt64)
/// xdg_popup.grab: the popup wants keyboard focus (menus); popups without a grab never get it.
PopupGrabbed(UInt64)
/// xdg_popup.reposition placed it again: (id, x, y, w, h) relative to the parent's content.
PopupMoved(UInt64, Int, Int, Int, Int)
/// xdg_surface.set_window_geometry on a popup: (id, x, y) of the visible menu inside its
/// buffer. The positioner places that point, so the buffer is drawn this much up/left.
PopupGeometry(UInt64, Int, Int)
}
///|
@@ -80,6 +87,9 @@ struct SurfaceState {
struct XdgSurfaceState {
wl_surface_id : UInt
mut window_id : UInt64?
/// set_window_geometry origin: where the visible window starts inside the buffer (shadows).
mut geo_x : Int
mut geo_y : Int
}
///|
@@ -112,6 +122,9 @@ struct PosState {
mut gravity : UInt
mut ox : Int
mut oy : Int
/// xdg_positioner.constraint_adjustment bits: 1 slide_x, 2 slide_y, 4 flip_x, 8 flip_y,
/// 16 resize_x, 32 resize_y.
mut adjust : UInt
}
///|
@@ -235,6 +248,9 @@ pub(all) struct WaylandServer {
mut selection : (UInt, UInt)?
/// Client that currently has keyboard focus (receives selection events).
mut kbd_client : UInt?
/// Area a popup must stay inside, as (x, y, w, h) relative to its parent's content origin
/// (the parent window id is passed). The host sets this; the default never constrains.
mut popup_bounds : (UInt64) -> (Int, Int, Int, Int)
}
///|
@@ -260,6 +276,7 @@ pub fn WaylandServer::listen(
keymap: Bytes::from_array([]),
selection: None,
kbd_client: None,
popup_bounds: fn(_) { (-1000000, -1000000, 2000000, 2000000) },
})
}
@@ -796,13 +813,13 @@ fn WaylandServer::dispatch_msg(
} else if opcode == 1U {
let pos_id = reader.u32()
c.objects.set(pos_id, ObjXdgPositioner(pos_id))
c.positioners.set(pos_id, { w: 0, h: 0, ax: 0, ay: 0, aw: 0, ah: 0, anchor: 0U, gravity: 0U, ox: 0, oy: 0 })
c.positioners.set(pos_id, { w: 0, h: 0, ax: 0, ay: 0, aw: 0, ah: 0, anchor: 0U, gravity: 0U, ox: 0, oy: 0, adjust: 0U })
} else if opcode == 2U {
// get_xdg_surface(id, surface)
let xsid = reader.u32()
let wl_sid = reader.u32()
c.objects.set(xsid, ObjXdgSurface(xsid))
c.xdg_surfaces.set(xsid, { wl_surface_id: wl_sid, window_id: None })
c.xdg_surfaces.set(xsid, { wl_surface_id: wl_sid, window_id: None, geo_x: 0, geo_y: 0 })
match c.surfaces.get(wl_sid) {
Some(s) => s.xdg_surface_id = Some(xsid)
None => ()
@@ -813,6 +830,25 @@ fn WaylandServer::dispatch_msg(
c.xdg_surfaces.remove(xsid)
c.objects.remove(xsid)
send_delete_id(c, xsid)
} else if opcode == 3U {
// set_window_geometry(x, y, w, h)
let gx = reader.i32()
let gy = reader.i32()
match c.xdg_surfaces.get(xsid) {
Some(xs) => {
xs.geo_x = gx
xs.geo_y = gy
match xs.window_id {
Some(wid) =>
match c.toplevels.get(wid) {
Some(tl) if tl.popup_parent is Some(_) => events.push(PopupGeometry(wid, gx, gy))
_ => ()
}
None => ()
}
}
None => ()
}
} else if opcode == 1U {
// get_toplevel(id)
let tl_id = reader.u32()
@@ -872,7 +908,7 @@ fn WaylandServer::dispatch_msg(
None => ()
}
let (px, py, pw, ph) = match c.positioners.get(pos_id) {
Some(ps) => popup_placement(ps)
Some(ps) => constrain_popup(ps, (self.popup_bounds)(parent_wid))
None => (0, 0, 1, 1)
}
c.objects.set(pid, ObjXdgPopup(wid))
@@ -902,6 +938,10 @@ fn WaylandServer::dispatch_msg(
w2.u32(self.next_serial())
c.send_msg(w2.finish_msg(xsid, 0U))
events.push(PopupOpened(wid, parent_wid, px, py, pw, ph))
match c.xdg_surfaces.get(xsid) {
Some(xs) if xs.geo_x != 0 || xs.geo_y != 0 => events.push(PopupGeometry(wid, xs.geo_x, xs.geo_y))
_ => ()
}
}
ObjXdgPositioner(pos_id) =>
match c.positioners.get(pos_id) {
@@ -923,6 +963,8 @@ fn WaylandServer::dispatch_msg(
ps.anchor = reader.u32()
} else if opcode == 4U {
ps.gravity = reader.u32()
} else if opcode == 5U {
ps.adjust = reader.u32()
} else if opcode == 6U {
ps.ox = reader.i32()
ps.oy = reader.i32()
@@ -936,8 +978,34 @@ fn WaylandServer::dispatch_msg(
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
events.push(PopupClosed(wid))
} else if opcode == 1U {
// grab(seat, serial)
events.push(PopupGrabbed(wid))
} else if opcode == 2U {
// reposition(positioner, token): place again, then repositioned + configure.
let pos_id = reader.u32()
let token = reader.u32()
match (c.toplevels.get(wid), c.positioners.get(pos_id)) {
(Some(tl), Some(ps)) => {
let parent = tl.popup_parent.unwrap_or(0UL)
let (px, py, pw, ph) = constrain_popup(ps, (self.popup_bounds)(parent))
let wt = WireWriter::new()
wt.u32(token)
c.send_msg(wt.finish_msg(tl.wl_toplevel_id, 2U))
let w1 = WireWriter::new()
w1.i32(px)
w1.i32(py)
w1.i32(pw)
w1.i32(ph)
c.send_msg(w1.finish_msg(tl.wl_toplevel_id, 0U))
let w2 = WireWriter::new()
w2.u32(self.next_serial())
c.send_msg(w2.finish_msg(tl.xdg_surface_id, 0U))
events.push(PopupMoved(wid, px, py, pw, ph))
}
_ => ()
}
}
// grab (1) / reposition (2): the compositor keeps the placement it configured
ObjXdgToplevel(wid) =>
match c.toplevels.get(wid) {
None => ()
@@ -1507,6 +1575,103 @@ fn popup_placement(ps : PosState) -> (Int, Int, Int, Int) {
(x + ps.ox, y + ps.oy, ps.w, ps.h)
}
///| Popup placement with the positioner's constraint adjustments applied, so the popup
/// stays inside `bounds` (x, y, w, h relative to the parent's content): per axis, flip if
/// that makes it fit, else slide it in, else shrink it. Unadjustable axes are left alone.
fn constrain_popup(ps : PosState, bounds : (Int, Int, Int, Int)) -> (Int, Int, Int, Int) {
let (bx, by, bw, bh) = bounds
let (x0, y0, w0, h0) = popup_placement(ps)
let mut x = x0
let mut y = y0
let mut w = w0
let mut h = h0
let out_x = fn(x : Int, w : Int) { x < bx || x + w > bx + bw }
let out_y = fn(y : Int, h : Int) { y < by || y + h > by + bh }
// flip_x: mirror anchor and gravity left <-> right.
if out_x(x, w) && (ps.adjust & 4U) != 0U {
let (fx, _, _, _) = popup_placement({
..ps,
anchor: flip_edge(ps.anchor, true),
gravity: flip_edge(ps.gravity, true),
ox: -ps.ox,
})
if !out_x(fx, w) {
x = fx
}
}
if out_x(x, w) && (ps.adjust & 1U) != 0U {
if x + w > bx + bw {
x = bx + bw - w
}
if x < bx {
x = bx
}
}
if out_x(x, w) && (ps.adjust & 16U) != 0U {
let l = if x < bx { bx } else { x }
let r = if x + w > bx + bw { bx + bw } else { x + w }
if r > l {
x = l
w = r - l
}
}
// flip_y: mirror anchor and gravity top <-> bottom.
if out_y(y, h) && (ps.adjust & 8U) != 0U {
let (_, fy, _, _) = popup_placement({
..ps,
anchor: flip_edge(ps.anchor, false),
gravity: flip_edge(ps.gravity, false),
oy: -ps.oy,
})
if !out_y(fy, h) {
y = fy
}
}
if out_y(y, h) && (ps.adjust & 2U) != 0U {
if y + h > by + bh {
y = by + bh - h
}
if y < by {
y = by
}
}
if out_y(y, h) && (ps.adjust & 32U) != 0U {
let t = if y < by { by } else { y }
let b = if y + h > by + bh { by + bh } else { y + h }
if b > t {
y = t
h = b - t
}
}
(x, y, w, h)
}
///| Mirror an anchor/gravity value horizontally (`horiz`) or vertically.
fn flip_edge(v : UInt, horiz : Bool) -> UInt {
// 0 none, 1 top, 2 bottom, 3 left, 4 right, 5 top_left, 6 bottom_left, 7 top_right, 8 bottom_right
if horiz {
match v {
3U => 4U
4U => 3U
5U => 7U
7U => 5U
6U => 8U
8U => 6U
_ => v
}
} else {
match v {
1U => 2U
2U => 1U
5U => 6U
6U => 5U
7U => 8U
8U => 7U
_ => v
}
}
}
///| xdg_popup.popup_done: ask the client to dismiss this popup.
pub fn WaylandServer::popup_done(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) {