From b33373d8010da60b20ab06f72c9a7497988b7138 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 19:03:02 +0000 Subject: [PATCH] Right-click menus inside apps: keep them open and on screen 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 Claude-Session: https://claude.ai/code/session_01DPL5hikqzveCoytgF4Uyvh --- compositor/host/compositor.mbt | 83 +++++++-- compositor/host/popup_test.mbt | 327 +++++++++++++++++++++++++++++++++ compositor/wayland/server.mbt | 173 ++++++++++++++++- 3 files changed, 563 insertions(+), 20 deletions(-) create mode 100644 compositor/host/popup_test.mbt diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 2543116..7ee72c3 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -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) } ///| diff --git a/compositor/host/popup_test.mbt b/compositor/host/popup_test.mbt new file mode 100644 index 0000000..8fda33b --- /dev/null +++ b/compositor/host/popup_test.mbt @@ -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) +} diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index c77db9a..aeb2490 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -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) {