xdg_popup support: right-click menus in client apps (Konsole etc.)
CI / test (push) Failing after 37s

- server: xdg_positioner state, get_popup placement + configure, popup commits,
  popup_done; popups share the toplevel table so pointer/keyboard routing is reused
- compositor: popups drawn above windows and panel, input goes to the popup under the
  pointer, keyboard focus follows the popup, a click outside dismisses them
- test: a client opens a popup, clicks inside it, and is told to dismiss it

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 00:03:49 -08:00
1 parent 3b2e19ace5
commit bc1e3dfdbd
5 files changed
+491 -5

No files matched your search

+189
View File
@@ -14,8 +14,22 @@ pub(all) struct HostWindow {
mut cfg_dirty : Bool
}
///| A client popup (xdg_popup): menus, combo lists, tooltips.
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 surface : @scene.ShmBufferSnapshot?
}
///|
pub(all) struct MoonPlasmaCompositor {
/// Open popups, oldest first (the last is topmost).
popups : Array[HostPopup]
server : @wayland.WaylandServer
wm : @wm.WmState
theme : @scene.ThemeTokens
@@ -77,6 +91,7 @@ pub fn MoonPlasmaCompositor::new(
shell,
ptr_x: 0.0,
ptr_y: 0.0,
popups: [],
ptr_win: None,
split_drag: None,
press_fwd: false,
@@ -230,7 +245,23 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> 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))
}
@wayland.CompositorEvent::PopupCommitted(wid, snap) =>
for p in self.popups {
if p.id == wid {
match snap {
Some(_) => p.surface = snap
None => ()
}
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupClosed(wid) => self.close_popup(wid)
@wayland.CompositorEvent::ToplevelClosed(wid) => {
self.close_popup(wid)
ignore(self.windows.remove(wid))
if self.ptr_win == Some(wid) {
self.ptr_win = None
@@ -316,6 +347,13 @@ pub fn MoonPlasmaCompositor::compose_frame(
}
}
for p in self.popups {
match p.surface {
Some(snap) => scene.popups.push((self.popup_rect(p), snap))
None => ()
}
}
self.shell.layout_sel = match self.layout_mode {
@wm.LayoutMode::Tiling => 0
@wm.LayoutMode::Monocle => 1
@@ -414,6 +452,127 @@ pub fn MoonPlasmaCompositor::handle_gfx_events(
}
///| Content origin of a window or popup.
fn MoonPlasmaCompositor::origin_of(self : MoonPlasmaCompositor, wid : UInt64) -> (Int, Int) {
match self.windows.get(wid) {
Some(win) => (win.geometry.x, win.geometry.y)
None => {
for p in self.popups {
if p.id == wid {
let r = self.popup_rect(p)
return (r.x, r.y)
}
}
(0, 0)
}
}
}
///| Absolute rectangle of a popup, nudged to stay on screen.
fn MoonPlasmaCompositor::popup_rect(self : MoonPlasmaCompositor, 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)
}
///|
fn MoonPlasmaCompositor::origin_of_parent(self : MoonPlasmaCompositor, p : HostPopup) -> (Int, Int) {
if p.parent == p.id {
(0, 0)
} else {
self.origin_of(p.parent)
}
}
///| Top-most popup under (x, y).
fn MoonPlasmaCompositor::popup_at(self : MoonPlasmaCompositor, x : Int, y : Int) -> HostPopup? {
let mut i = self.popups.length() - 1
while i >= 0 {
let p = self.popups[i]
let r = self.popup_rect(p)
if p.surface is Some(_) && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h {
return Some(p)
}
i = i - 1
}
None
}
///| The toplevel a popup chain hangs off (for returning keyboard focus).
fn MoonPlasmaCompositor::popup_root(self : MoonPlasmaCompositor, wid : UInt64) -> UInt64? {
let mut cur = wid
let mut n = 0
while n < 16 {
if self.windows.contains(cur) {
return Some(cur)
}
let mut next : UInt64? = None
for p in self.popups {
if p.id == cur {
next = Some(p.parent)
}
}
match next {
Some(x) => cur = x
None => return None
}
n = n + 1
}
None
}
///| Forget a popup (and any popups hanging off it); give keyboard focus back to its window.
fn MoonPlasmaCompositor::close_popup(self : MoonPlasmaCompositor, wid : UInt64) -> Unit {
let mut found = false
for p in self.popups {
if p.id == wid {
found = true
}
}
if !found {
return
}
let root = self.popup_root(wid)
let keep = self.popups.filter(p => p.id != wid)
self.popups.clear()
for p in keep {
self.popups.push(p)
}
self.damage.mark_full()
if self.ptr_win == Some(wid) {
self.ptr_win = None
}
if self.kbd_win == Some(wid) {
self.kbd_win = None
match self.popups.last() {
Some(top) => self.set_kbd_focus(Some(top.id))
None => self.set_kbd_focus(root)
}
}
}
///| Send pointer enter/motion for a popup under the pointer.
fn MoonPlasmaCompositor::pointer_into_popup(self : MoonPlasmaCompositor, p : HostPopup, time_ms : UInt, motion : Bool) -> Unit {
let r = self.popup_rect(p)
let lx = self.ptr_x - r.x.to_double()
let ly = self.ptr_y - r.y.to_double()
if self.ptr_win != Some(p.id) {
self.leave_pointer()
self.ptr_win = Some(p.id)
self.server.pointer_enter(p.id, lx, ly)
}
if motion {
self.server.pointer_motion(p.id, time_ms, lx, ly)
}
}
///| Move keyboard focus (wl_keyboard leave/enter + current modifiers).
fn MoonPlasmaCompositor::set_kbd_focus(self : MoonPlasmaCompositor, wid : UInt64?) -> Unit {
if self.kbd_win == wid {
@@ -630,6 +789,13 @@ pub fn MoonPlasmaCompositor::handle_input(
self.ptr_y = y
let xi = x.to_int()
let yi = y.to_int()
match self.popup_at(xi, yi) {
Some(p) => {
self.pointer_into_popup(p, time_ms, true)
return
}
None => ()
}
if self.shell.inspecting() {
self.leave_pointer()
let id = match self.shell.component_at(xi, yi) {
@@ -696,6 +862,29 @@ pub fn MoonPlasmaCompositor::handle_input(
PointerButton(button, pressed) => {
let xi = self.ptr_x.to_int()
let yi = self.ptr_y.to_int()
match self.popup_at(xi, yi) {
Some(p) => {
self.pointer_into_popup(p, time_ms, false)
if pressed {
self.press_fwd = true
self.server.pointer_button(p.id, time_ms, button, true)
} else if self.press_fwd {
self.press_fwd = false
self.server.pointer_button(p.id, time_ms, button, false)
}
return
}
None => ()
}
if pressed && self.popups.length() > 0 {
// A click outside the open popups dismisses them (the click is swallowed).
let mut i = self.popups.length() - 1
while i >= 0 {
self.server.popup_done(self.popups[i].id)
i = i - 1
}
return
}
if !pressed && self.shell.pointer_up() {
self.run_shell_actions()
return
+124
View File
@@ -553,3 +553,127 @@ test "the desktop card can be dragged across the wallpaper" {
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)
}