Files
chandra/compositor/host/compositor.mbt
T
Claude b33373d801
CI / test (push) Failing after 15s
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hikqzveCoytgF4Uyvh
2026-11-25 19:03:04 +00:00

1455 lines
44 KiB
MoonBit

///| Pure-MoonBit Wayland compositor host wiring WaylandServer, lib/wm, Breeze SSD, SceneGraph, and SoftwareRenderer.
///|
pub(all) struct HostWindow {
id : UInt64
mut title : String
mut app_id : String
mut geometry : @scene.Rect
mut focused : Bool
mut maximized : Bool
mut fullscreen : Bool
mut surface : @scene.ShmBufferSnapshot?
/// A size change still to be sent to the client (coalesced while dragging a divider).
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.
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
}
///|
pub(all) struct ChandraCompositor {
/// Open popups, oldest first (the last is topmost).
popups : Array[HostPopup]
server : @wayland.WaylandServer
wm : @wm.WmState
/// The theme in use: `dark_theme` (the configured one) or Moon Day, per the Dark mode setting.
mut theme : @scene.ThemeTokens
dark_theme : @scene.ThemeTokens
/// Also set the system-wide dark/light preference for apps (system_theme.mbt). The login
/// session turns it on; nested windows and tests leave it off.
mut sync_system_theme : Bool
mut output_w : Int
mut output_h : Int
mut work_area : @wm.Rect
windows : Map[UInt64, HostWindow]
damage : @scene.DamageTracker
shell : @shell.DesktopShell
mut ptr_x : Double
mut ptr_y : Double
mut ptr_win : UInt64?
/// Divider being dragged between tiled windows.
mut split_drag : @wm.SplitHandle?
/// The last left press on a title bar (titleclick.mbt): drag to move, double-click to maximize.
mut title_press : TitlePress?
/// True while a button press was forwarded to a client (so its release is too).
mut press_fwd : Bool
mut kbd_win : UInt64?
mut mods : UInt
/// A Super key is down and nothing else was pressed since: releasing it opens the launcher.
mut super_solo : Bool
/// Super+D (showdesktop.mbt): whether a Super key is held, whether the D press was swallowed,
/// and the windows it minimized (bottom to top) so a second Super+D can restore them.
mut super_down : Bool
mut d_swallowed : Bool
mut desktop_hidden : Array[UInt64]
mut layout_mode : @wm.LayoutMode
/// Set when the user asked to end the session (the logout button); the main loop then exits.
mut quit_requested : Bool
/// Alt+Tab switcher (switcher.mbt): whether it is up, its window list (most recent first) and selection.
mut switcher_on : Bool
mut switcher : Array[UInt64]
mut switcher_sel : Int
mut alt_down : Bool
/// Milliseconds spent so far in the shell (panel modules) and in the rasteriser, for --profile.
/// The last composed frame and what it was made from: an unchanged desktop is not redrawn.
mut last_frame : @renderer.Pixmap?
mut last_sig : FrameSig?
/// True when the frame returned by the latest compose_frame differs from the one before it.
mut frame_changed : Bool
mut prof_shell_ms : UInt64
mut prof_raster_ms : UInt64
/// Session restore (session.mbt): apps launched at start whose window hasn't appeared yet,
/// until when they are waited for, and the windows that took their place.
restore_pending : Array[@shell.SessionEntry]
mut restore_until : UInt64
restore_claimed : Map[UInt64, Bool]
/// The session as last written, as last seen (and since when), and when to look again.
mut session_saved : String
mut session_seen : String
mut session_seen_at : UInt64
mut session_check_at : UInt64
}
///|
pub fn ChandraCompositor::new(
socket_path : String,
theme : @scene.ThemeTokens,
output_w : Int,
output_h : Int,
) -> ChandraCompositor? {
ignore(@font.install()) // Adwaita Mono; keeps the bitmap font if it's missing
let server = match @wayland.WaylandServer::listen(socket_path, output_w, output_h) {
Some(s) => s
None => return None
}
let wm = @wm.WmState::new(
inner_gap=8,
outer_gap=8,
titlebar_h=32,
border_w=2,
)
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)
let comp : ChandraCompositor = {
server,
wm,
theme,
dark_theme: theme,
sync_system_theme: false,
output_w,
output_h,
work_area,
windows: {},
damage: @scene.DamageTracker::new(),
shell,
ptr_x: 0.0,
ptr_y: 0.0,
popups: [],
ptr_win: None,
split_drag: None,
title_press: None,
press_fwd: false,
kbd_win: None,
mods: 0U,
super_solo: false,
super_down: false,
d_swallowed: false,
desktop_hidden: [],
layout_mode: @wm.LayoutMode::Tiling,
quit_requested: false,
switcher_on: false,
switcher: [],
switcher_sel: 0,
alt_down: false,
last_frame: None,
last_sig: None,
frame_changed: true,
prof_shell_ms: 0UL,
prof_raster_ms: 0UL,
restore_pending: [],
restore_until: 0UL,
restore_claimed: {},
session_saved: "",
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.
fn work_area_for(w : Int, h : Int, theme : @scene.ThemeTokens) -> @wm.Rect {
@wm.compute_work_area(
{ x: 0, y: 0, w, h },
excl_top=32, // room for the server-side title bar drawn above each client
excl_bottom=theme.panel_height,
)
}
///| The output changed size (the nested window was resized, maximized or made fullscreen):
/// lay the desktop out for the new size, so nothing is drawn past the edge and cropped away.
pub fn ChandraCompositor::resize(self : ChandraCompositor, w : Int, h : Int) -> Unit {
if w <= 0 || h <= 0 || (w == self.output_w && h == self.output_h) {
return
}
self.output_w = w
self.output_h = h
self.work_area = work_area_for(w, h, self.theme)
self.server.set_output_size(w, h)
self.shell.set_output(self.theme, w, h)
self.apply_actions(self.wm.fit_work_area(self.work_area))
self.last_frame = None
self.last_sig = None
self.frame_changed = true
self.damage.mark_full()
}
///|
pub fn ChandraCompositor::close(self : ChandraCompositor) -> Unit {
for _, win in self.windows {
release_snapshot(win.surface)
}
self.shell.shutdown()
self.server.close()
}
///| Let the host forget a client surface it captured (a no-op without the host rasteriser).
fn release_snapshot(s : @scene.ShmBufferSnapshot?) -> Unit {
match s {
Some(snap) => if snap.host_id != 0U { @sys.surface_release(snap.host_id) }
None => ()
}
}
///| What a frame was drawn from, compared with the next frame's to see whether it changed.
#derive(Eq)
pub(all) struct FrameSig {
bg : Array[@scene.DrawOp]
panel : Array[@scene.DrawOp]
wins : Array[(UInt64, @scene.Rect, Bool, Bool, Array[@scene.DrawOp])]
popups : Array[@scene.Rect]
}
///|
fn ChandraCompositor::apply_actions(
self : ChandraCompositor,
actions : Array[@wm.WmAction],
) -> Unit {
for act in actions {
match act {
@wm.WmAction::SetGeometry(wid, r) =>
match self.windows.get(wid) {
Some(win) => {
win.geometry = @scene.Rect::new(r.x, r.y, r.w, r.h)
if self.split_drag is Some(_) || self.wm.drag is Some(_) {
// Dragging: send one configure per frame, not one per mouse event.
win.cfg_dirty = true
} else {
win.cfg_dirty = false
self.server.send_toplevel_configure(
wid,
r.w,
r.h,
win.focused,
win.maximized,
win.fullscreen,
)
}
self.damage.mark_full()
}
None => ()
}
@wm.WmAction::Focus(wid) => {
for id, w in self.windows {
w.focused = id == wid
}
self.set_kbd_focus(Some(wid))
self.damage.mark_full()
}
@wm.WmAction::Raise(_) => self.damage.mark_full()
@wm.WmAction::Close(wid) => self.server.send_toplevel_close(wid)
@wm.WmAction::SetMaximized(wid, m) =>
match self.windows.get(wid) {
Some(win) => win.maximized = m
None => ()
}
@wm.WmAction::SetFullscreen(wid, f) =>
match self.windows.get(wid) {
Some(win) => win.fullscreen = f
None => ()
}
}
}
}
///| Send the size changes coalesced during a divider drag.
fn ChandraCompositor::flush_configures(self : ChandraCompositor) -> Unit {
for wid, win in self.windows {
if win.cfg_dirty {
win.cfg_dirty = false
self.server.send_toplevel_configure(
wid,
win.geometry.w,
win.geometry.h,
win.focused,
win.maximized,
win.fullscreen,
)
}
}
}
///|
pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
self.flush_configures()
let events = self.server.poll_events()
for ev in events {
match ev {
@wayland.CompositorEvent::ToplevelOpened(wid, app_id, title, min_w, min_h) => {
let win : HostWindow = {
id: wid,
title,
app_id,
geometry: @scene.Rect::new(100, 100, 640, 480),
focused: true,
maximized: false,
fullscreen: false,
surface: None,
cfg_dirty: false,
}
self.windows.set(wid, win)
// An app whose window was last left somewhere reopens there, floating, kept on screen.
let saved = self.shell.saved_window(app_id)
let g : @wm.Rect = match saved {
Some((sx, sy, sw, sh)) => {
let w = if sw > self.output_w { self.output_w } else { sw }
let h = if sh > self.output_h { self.output_h } else { sh }
let x = if sx + w > self.output_w { self.output_w - w } else if sx < 0 { 0 } else { sx }
let y = if sy + h > self.output_h { self.output_h - h } else if sy < 32 { 32 } else { sy }
{ x, y, w, h }
}
None => { x: 100, y: 100, w: 640, h: 480 }
}
win.geometry = @scene.Rect::new(g.x, g.y, g.w, g.h)
let rec : @wm.WindowRecord = {
id: wid,
app_id,
title,
geometry: g,
floating_geometry: g,
min_size: { w: min_w, h: min_h },
max_size: None,
focused: true,
maximized: false,
fullscreen: false,
minimized: false,
is_floating: saved is Some(_),
workspace: 0U,
can_move: true,
can_resize: true,
can_maximize: true,
can_minimize: true,
can_fullscreen: true,
}
let actions = self.wm.open_window(rec, self.work_area)
self.apply_actions(actions)
self.claim_restored(wid, app_id)
}
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
match self.windows.get(wid) {
Some(win) => {
if app_id.length() > 0 {
win.app_id = app_id
self.claim_restored(wid, app_id)
}
if title.length() > 0 {
win.title = title
}
self.damage.mark_full()
}
None => ()
}
@wayland.CompositorEvent::ToplevelCommitted(wid, snap) =>
match self.windows.get(wid) {
Some(win) => {
match snap {
Some(s) => {
release_snapshot(win.surface)
win.surface = Some(s)
self.damage.mark_rect(win.geometry)
}
None => ()
}
}
None => ()
}
@wayland.CompositorEvent::PopupOpened(wid, parent, rx, ry, w, h) => {
// 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 => ()
}
self.damage.mark_full()
}
}
@wayland.CompositorEvent::PopupClosed(wid) => self.close_popup(wid)
@wayland.CompositorEvent::ToplevelClosed(wid) => {
self.close_popup(wid)
match self.windows.get(wid) {
Some(win) => release_snapshot(win.surface)
None => ()
}
ignore(self.windows.remove(wid))
ignore(self.restore_claimed.remove(wid))
if self.ptr_win == Some(wid) {
self.ptr_win = None
}
if self.kbd_win == Some(wid) {
self.kbd_win = None
}
let actions = self.wm.close_window(wid, self.work_area)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMaximizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Maximize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelFullscreenRequest(wid, f) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Fullscreen(f),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMinimizeRequest(wid, m) => {
let actions = self.wm.handle_request(
wid,
@wm.WindowRequest::Minimize(m),
self.work_area,
)
self.apply_actions(actions)
}
@wayland.CompositorEvent::ToplevelMoveRequest(_) => ()
}
}
self.session_tick()
}
///|
pub fn ChandraCompositor::set_layout_mode(
self : ChandraCompositor,
mode : @wm.LayoutMode,
) -> Unit {
self.layout_mode = mode
let actions = self.wm.set_layout_mode(mode, self.work_area)
self.apply_actions(actions)
// Remember it (a no-op unless persistence is on).
self.shell.layout_sel = match mode {
@wm.LayoutMode::Tiling => 0
@wm.LayoutMode::Monocle => 1
@wm.LayoutMode::Floating => 2
}
self.shell.save_state()
}
///| Restore what the last session left (layout mode, recent apps) from state.edn. Call once,
/// after turning `shell.persist` on; a no-op without persistence.
pub fn ChandraCompositor::restore_state(self : ChandraCompositor) -> Unit {
let st = self.shell.load_state()
self.shell.apply_state(st)
self.apply_theme()
match st.layout {
Some(0) => self.set_layout_mode(@wm.LayoutMode::Tiling)
Some(1) => self.set_layout_mode(@wm.LayoutMode::Monocle)
Some(2) => self.set_layout_mode(@wm.LayoutMode::Floating)
_ => ()
}
}
///|
fn ChandraCompositor::build_scene(
self : ChandraCompositor,
) -> (@scene.SceneGraph, Bool) {
self.flush_configures()
let scene = @scene.SceneGraph::new(self.output_w, self.output_h)
let order = self.wm.stacking_order()
for wid in order {
match self.windows.get(wid) {
Some(win) => {
let (insets, deco_ops) = @scene.build_breeze_decoration(
win.title,
win.app_id,
win.focused,
win.fullscreen,
win.geometry.w + 4,
win.geometry.h + 34,
self.theme,
)
scene.windows.push({
id: win.id,
title: win.title,
app_id: win.app_id,
focused: win.focused,
fullscreen: win.fullscreen,
geometry: win.geometry,
deco_insets: insets,
deco_ops,
surface: win.surface,
})
}
None => ()
}
}
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
@wm.LayoutMode::Floating => 2
}
// The task list shows every window, minimized ones too (so they can be restored).
let wins : Array[@shell.WinInfo] = []
for wid in order {
match self.windows.get(wid) {
Some(win) =>
wins.push({
id: win.id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: win.focused,
minimized: false,
})
None => ()
}
}
for id, r in self.wm.windows {
if r.minimized {
match self.windows.get(id) {
Some(win) =>
wins.push({
id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: false,
minimized: true,
})
None => ()
}
}
}
self.shell.windows = wins
// The window list the `windows` interface serves to wasm widgets (task-bar order).
let entries : Array[@wasm.WindowEntry] = []
for w in wins {
match self.windows.get(w.id) {
Some(win) => entries.push({ id: w.id, title: w.title, app_id: win.app_id, focused: w.focused })
None => ()
}
}
self.shell.win_list = entries
let t0 = @sys.monotonic_ms()
let (bg_ops, panel_ops) = self.shell.render(self.output_w, self.output_h, self.theme)
if self.switcher_on {
panel_ops.append(self.switcher_ops())
}
self.run_shell_actions()
// The wallpaper (the first ops) is cached by the renderer; the desktop card after it changes.
let n_wall = if self.shell.wallpaper_len <= bg_ops.length() { self.shell.wallpaper_len } else { 0 }
scene.wallpaper_ops = Array::makei(n_wall, i => bg_ops[i])
scene.background_ops = Array::makei(bg_ops.length() - n_wall, i => bg_ops[n_wall + i])
scene.panel_ops = panel_ops
let damaged = self.damage.take_damage(self.output_w, self.output_h).length() > 0
let sig : FrameSig = {
bg: bg_ops,
panel: panel_ops,
wins: scene.windows.map(w => (w.id, w.geometry, w.focused, w.fullscreen, w.deco_ops)),
popups: scene.popups.map(p => p.0),
}
// Unchanged since the last scene (no client commit, same shell and decoration ops)?
let unchanged = match self.last_sig {
Some(ps) => !damaged && ps == sig
None => false
}
if !unchanged {
self.last_sig = Some(sig)
}
self.prof_shell_ms = self.prof_shell_ms + (@sys.monotonic_ms() - t0)
(scene, !unchanged)
}
///| Compose the desktop for the host rasteriser (the wasm compositor): the scene to draw, or None
/// when nothing changed since the last one, so an idle desktop costs nothing to draw or present.
pub fn ChandraCompositor::compose_scene(
self : ChandraCompositor,
) -> @scene.SceneGraph? {
let (scene, changed) = self.build_scene()
self.frame_changed = changed
if changed { Some(scene) } else { None }
}
///| Compose the desktop and draw it with the software renderer (the native build and the tests).
/// An unchanged desktop hands back the previous pixels instead of rasterising them again.
pub fn ChandraCompositor::compose_frame(
self : ChandraCompositor,
) -> @renderer.Pixmap {
let (scene, changed) = self.build_scene()
match self.last_frame {
Some(prev) =>
if !changed {
self.frame_changed = false
return prev
}
None => ()
}
self.frame_changed = true
let t1 = @sys.monotonic_ms()
let px = @renderer.render_scene(scene)
self.last_frame = Some(px)
self.prof_raster_ms = self.prof_raster_ms + (@sys.monotonic_ms() - t1)
px
}
///|
/// Compose the current desktop GUI frame and present it onto a wasi-gfx
/// `SurfaceFrameBufferContext`.
pub fn ChandraCompositor::present_to_gfx(
self : ChandraCompositor,
ctx : @gfx.SurfaceFrameBufferContext,
) -> @renderer.Pixmap {
let pixmap = self.compose_frame()
if self.frame_changed {
@gfx.present_pixmap(ctx, pixmap)
}
pixmap
}
///|
/// Process pending wasi-gfx surface input events (pointer clicks for focus).
pub fn ChandraCompositor::handle_gfx_events(
self : ChandraCompositor,
surface : @gfx.Surface,
) -> Unit {
let downs = surface.drain_pointer_down()
for ev in downs {
let px = ev.x.to_int()
let py = ev.y.to_int()
let order = self.wm.stacking_order()
// Top-most window hit-test (iterate reverse Z-order).
let mut i = order.length() - 1
while i >= 0 {
let wid = order[i]
match self.windows.get(wid) {
Some(win) =>
if px >= win.geometry.x &&
px < win.geometry.x + win.geometry.w &&
py >= win.geometry.y - 32 &&
py < win.geometry.y + win.geometry.h {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
break
}
None => ()
}
i = i - 1
}
}
}
///| 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)
None => {
for p in self.popups {
if p.id == wid {
let r = self.popup_rect(p)
return (r.x + p.geo_x, r.y + p.geo_y)
}
}
(0, 0)
}
}
}
///| 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 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)
}
///|
fn ChandraCompositor::origin_of_parent(self : ChandraCompositor, p : HostPopup) -> (Int, Int) {
if p.parent == p.id {
(0, 0)
} else {
self.origin_of(p.parent)
}
}
///| Top-most popup under (x, y).
fn ChandraCompositor::popup_at(self : ChandraCompositor, 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 ChandraCompositor::popup_root(self : ChandraCompositor, 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 ChandraCompositor::close_popup(self : ChandraCompositor, 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)
for p in self.popups {
if p.id == wid {
release_snapshot(p.surface)
}
}
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 ChandraCompositor::pointer_into_popup(self : ChandraCompositor, 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 ChandraCompositor::set_kbd_focus(self : ChandraCompositor, wid : UInt64?) -> Unit {
if self.kbd_win == wid {
return
}
match self.kbd_win {
Some(old) => self.server.keyboard_leave(old)
None => ()
}
self.kbd_win = wid
match wid {
Some(w) => {
self.server.keyboard_enter(w)
self.server.keyboard_modifiers(w, self.mods, 0U, 0U, 0U)
}
None => ()
}
}
///| Top-most window under (x, y): (id, in client area). The titlebar counts as the window but not the client.
fn ChandraCompositor::window_at(self : ChandraCompositor, x : Int, y : Int) -> (UInt64, Bool)? {
let order = self.wm.stacking_order()
let mut i = order.length() - 1
while i >= 0 {
match self.windows.get(order[i]) {
Some(win) => {
let g = win.geometry
if x >= g.x && x < g.x + g.w && y >= g.y && y < g.y + g.h {
return Some((win.id, true))
}
if x >= g.x - 2 && x < g.x + g.w + 2 && y >= g.y - 32 && y < g.y {
return Some((win.id, false))
}
}
None => ()
}
i = i - 1
}
None
}
///| Resize zone under (x, y) on a floating window: its corners (12px) and left/right/bottom
/// edges. The first window whose outer box contains the point decides; tiled windows resize
/// through the dividers instead.
fn ChandraCompositor::resize_at(
self : ChandraCompositor,
x : Int,
y : Int,
) -> (UInt64, @wm.DragMode)? {
let order = self.wm.stacking_order()
let mut i = order.length() - 1
while i >= 0 {
let id = order[i]
match (self.windows.get(id), self.wm.windows.get(id)) {
(Some(win), Some(rec)) => {
let g = win.geometry
let l = g.x
let r = g.x + g.w
let t = g.y - 32
let b = g.y + g.h
if x >= l - 6 && x < r + 6 && y >= t - 6 && y < b + 6 {
if !rec.is_floating || rec.maximized || rec.fullscreen {
return None
}
let nl = x < l + 12
let nr = x >= r - 12
let in_l = x < l + 2
let in_r = x >= r - 2
let in_b = y >= b - 2
let in_t = y < t + 4
if in_t && nl {
return Some((id, @wm.DragMode::ResizeNW))
}
if in_t && nr {
return Some((id, @wm.DragMode::ResizeNE))
}
if in_b && nl {
return Some((id, @wm.DragMode::ResizeSW))
}
if in_b && nr {
return Some((id, @wm.DragMode::ResizeSE))
}
if in_b {
return Some((id, @wm.DragMode::ResizeS))
}
if in_l {
return Some((id, if y >= b - 12 { @wm.DragMode::ResizeSW } else { @wm.DragMode::ResizeW }))
}
if in_r {
return Some((id, if y >= b - 12 { @wm.DragMode::ResizeSE } else { @wm.DragMode::ResizeE }))
}
return None
}
}
_ => ()
}
i = i - 1
}
None
}
///| Title-bar button under (x, y) for window `wid`: 1 minimize, 2 maximize, 3 close, 0 none.
/// Mirrors the layout in `build_breeze_decoration` (18px buttons, 8px apart, 10px from the right edge).
fn ChandraCompositor::title_button_at(
self : ChandraCompositor,
wid : UInt64,
x : Int,
y : Int,
) -> Int {
match self.windows.get(wid) {
None => 0
Some(win) => {
let g = win.geometry
let outer_w = g.w + 4
let lx = x - (g.x - 2)
let ly = y - (g.y - 32)
let size = 18
let by = (32 - size) / 2
if ly < by - 3 || ly >= by + size + 3 {
return 0
}
let close_x = outer_w - 10 - size
let max_x = close_x - 8 - size
let min_x = max_x - 8 - size
if lx >= close_x - 3 && lx < close_x + size + 3 {
3
} else if lx >= max_x - 3 && lx < max_x + size + 3 {
2
} else if lx >= min_x - 3 && lx < min_x + size + 3 {
1
} else {
0
}
}
}
}
///| Activate the title-bar button `btn` of window `wid`.
fn ChandraCompositor::press_title_button(
self : ChandraCompositor,
wid : UInt64,
btn : Int,
) -> Unit {
self.leave_pointer()
let req = match btn {
1 => @wm.WindowRequest::Minimize(true)
2 => {
let maxed = match self.wm.windows.get(wid) {
Some(r) => r.maximized
None => false
}
@wm.WindowRequest::Maximize(!maxed)
}
_ => @wm.WindowRequest::Close
}
self.apply_actions(self.wm.handle_request(wid, req, self.work_area))
if btn == 1 {
// Hand focus to the top-most remaining window.
let order = self.wm.stacking_order()
if order.length() > 0 {
let top = order[order.length() - 1]
self.activate_window(top)
} else {
self.set_kbd_focus(None)
}
}
self.damage.mark_full()
}
///| Focus a window and bring it to the front through the window manager, so its stacking
/// order (not just the drawing) changes.
fn ChandraCompositor::activate_window(self : ChandraCompositor, wid : UInt64) -> Unit {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
}
///| Restore a minimized window (or focus a visible one) from its task-bar pill.
fn ChandraCompositor::activate_task(self : ChandraCompositor, wid : UInt64) -> Unit {
let minimized = match self.wm.windows.get(wid) {
Some(r) => r.minimized
None => return
}
if minimized {
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Minimize(false), self.work_area),
)
}
self.apply_actions(
self.wm.handle_request(wid, @wm.WindowRequest::Activate, self.work_area),
)
}
///| Use the dark (configured) or the light theme, as the shell's Dark mode setting says, for the
/// shell, the panel modules, the window decorations and the switcher; redraws everything, and
/// (in the login session) tells apps through the system preference.
pub fn ChandraCompositor::apply_theme(self : ChandraCompositor) -> Unit {
let t = if self.shell.dark_mode { self.dark_theme } else { @scene.ThemeTokens::moon_day() }
self.theme = t
self.shell.set_output(t, self.output_w, self.output_h)
self.damage.mark_full()
self.sync_system_theme_pref(self.shell.dark_mode)
}
///| Carry out what panel modules asked for: focus a window, switch layout.
fn ChandraCompositor::run_shell_actions(self : ChandraCompositor) -> Unit {
for a in self.shell.take_actions() {
match a {
@shell.ShellAction::FocusWindow(wid) => self.activate_task(wid)
@shell.ShellAction::Logout => self.quit_requested = true
@shell.ShellAction::ThemeChanged => self.apply_theme()
@shell.ShellAction::SetLayout(m) =>
match m {
0 => self.set_layout_mode(@wm.LayoutMode::Tiling)
1 => self.set_layout_mode(@wm.LayoutMode::Monocle)
2 => self.set_layout_mode(@wm.LayoutMode::Floating)
_ => ()
}
}
}
}
///| Tiling divider under (x, y), with a few pixels of slack either side.
fn ChandraCompositor::handle_at(
self : ChandraCompositor,
x : Int,
y : Int,
) -> @wm.SplitHandle? {
let slop = 3
for h in self.wm.split_handles(self.work_area) {
let r = h.rect
// Title bars sit 32px above the windows, so the hit area reaches up over them.
let hit = if h.horizontal {
x >= r.x - slop && x < r.x + r.w + slop && y >= r.y - 32 && y < r.y + r.h
} else {
y >= r.y - slop && y < r.y + r.h + slop && x >= r.x && x < r.x + r.w
}
if hit {
return Some(h)
}
}
None
}
///|
fn ChandraCompositor::leave_pointer(self : ChandraCompositor) -> Unit {
match self.ptr_win {
Some(w) => {
self.server.pointer_leave(w)
self.ptr_win = None
}
None => ()
}
}
///| evdev keycode -> the key name lib/launcher understands (US layout).
fn key_name(code : UInt, shift : Bool) -> String {
let rows = [
(16U, "qwertyuiop"),
(30U, "asdfghjkl"),
(44U, "zxcvbnm"),
(2U, "1234567890"),
]
for r in rows {
let (first, chars) = r
if code >= first && code < first + chars.length().reinterpret_as_uint() {
let c = Int::unsafe_to_char(chars[(code - first).reinterpret_as_int()].to_int())
return if shift && c >= 'a' && c <= 'z' {
Int::unsafe_to_char(c.to_int() - 32).to_string()
} else {
c.to_string()
}
}
}
match code {
1U => "Escape"
14U => "Backspace"
28U => "Enter"
57U => " "
12U => "-"
52U => "."
53U => "/"
_ => ""
}
}
///| Route host input: shell (launcher) first, then the window under the pointer
/// / the keyboard-focused window.
pub fn ChandraCompositor::handle_input(
self : ChandraCompositor,
ev : @nested.InputEvent,
time_ms : UInt,
) -> Unit {
match ev {
PointerMove(x, y) => {
self.ptr_x = x
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) {
"" =>
match self.window_at(xi, yi) {
Some((_, true)) => "window-surface"
Some((_, false)) => "window-decoration"
None => if self.shell.desk_hit(xi, yi) { "desktop-widget" } else { "desktop" }
}
sid => sid
}
self.shell.set_hover(id, xi, yi)
return
}
if self.title_bar_motion(xi, yi) {
return
}
if self.wm.drag is Some(_) {
match self.wm.update_drag(xi, yi) {
Some(a) => self.apply_actions([a])
None => ()
}
return
}
if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) {
self.leave_pointer()
return
}
match self.split_drag {
Some(h) => {
self.apply_actions(self.wm.drag_split(h, xi, yi, self.work_area))
return
}
None => ()
}
if self.handle_at(xi, yi) is Some(_) || self.resize_at(xi, yi) is Some(_) {
self.leave_pointer()
return
}
match self.window_at(xi, yi) {
Some((wid, true)) =>
match self.windows.get(wid) {
Some(win) => {
let lx = x - win.geometry.x.to_double()
let ly = y - win.geometry.y.to_double()
if self.ptr_win != Some(wid) {
self.leave_pointer()
self.ptr_win = Some(wid)
self.server.pointer_enter(wid, lx, ly)
}
self.server.pointer_motion(wid, time_ms, lx, ly)
}
None => ()
}
_ => self.leave_pointer()
}
}
Scroll(axis, v, discrete) => {
if v == 0.0 && discrete == 0 {
return
}
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)
self.server.pointer_axis(p.id, time_ms, axis, v, discrete)
return
}
None => ()
}
if axis == 0U && self.shell.inspecting() {
// ~10 units per wheel notch -> 3 lines.
let lines = if v > 0.0 { (v / 10.0).to_int() + 1 } else { (v / 10.0).to_int() - 1 }
ignore(self.shell.scroll_inspector(lines))
return
}
if self.wm.drag is Some(_) || self.split_drag is Some(_) {
return
}
// The wheel scrolls the window under the pointer (focused or not), like Plasma.
match self.window_at(xi, yi) {
Some((wid, true)) =>
match self.windows.get(wid) {
Some(win) => {
if self.ptr_win != Some(wid) {
self.leave_pointer()
self.ptr_win = Some(wid)
self.server.pointer_enter(
wid,
self.ptr_x - win.geometry.x.to_double(),
self.ptr_y - win.geometry.y.to_double(),
)
}
self.server.pointer_axis(wid, time_ms, axis, v, discrete)
}
None => ()
}
_ => ()
}
}
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 && button == 272U {
self.title_bar_release()
}
if !pressed && self.shell.pointer_up() {
self.run_shell_actions()
return
}
if !pressed && self.split_drag is Some(_) {
self.split_drag = None
return
}
if !pressed && self.wm.drag is Some(_) {
let dragged = self.wm.drag.unwrap().win_id
self.wm.end_drag()
// Remember where a floating window was left.
match (self.wm.windows.get(dragged), self.windows.get(dragged)) {
(Some(r), Some(win)) =>
if r.is_floating && !r.maximized && !r.fullscreen {
self.shell.remember_window(win.app_id, win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h)
}
_ => ()
}
self.flush_configures()
return
}
// Right button over a swappable panel widget opens its alternatives menu.
if button == 273U && pressed && self.shell.right_click(xi, yi) {
return
}
if pressed {
self.super_solo = false
if button != 273U && self.shell.pointer_down(xi, yi) {
self.run_shell_actions()
return
}
match self.window_at(xi, yi) {
Some((wid, false)) => {
let btn = self.title_button_at(wid, xi, yi)
if btn != 0 {
self.press_title_button(wid, btn)
return
}
if button == 272U && self.resize_at(xi, yi) is None {
// Drag the window by its title bar, or double-click it to maximize / restore.
self.title_bar_press(wid, xi, yi, time_ms)
return
}
}
_ => ()
}
if button == 272U {
match self.resize_at(xi, yi) {
Some((wid, mode)) => {
self.leave_pointer()
self.activate_window(wid)
ignore(self.wm.start_drag(wid, mode, xi, yi))
return
}
None => ()
}
}
match self.handle_at(xi, yi) {
Some(h) => {
self.split_drag = Some(h)
self.leave_pointer()
return
}
None => ()
}
if self.window_at(xi, yi) is None && self.shell.desk_press(xi, yi) {
self.leave_pointer()
return
}
match self.window_at(xi, yi) {
Some((wid, _)) => {
self.activate_window(wid)
}
None => ()
}
}
match self.ptr_win {
Some(w) =>
if pressed {
self.press_fwd = true
self.server.pointer_button(w, time_ms, button, pressed)
} else if self.press_fwd {
self.press_fwd = false
self.server.pointer_button(w, time_ms, button, pressed)
}
None => ()
}
}
Key(code, pressed) =>
if self.switcher_key(code, pressed, time_ms) {
self.super_solo = false
} else if self.show_desktop_key(code, pressed) {
self.super_solo = false
} else if code == 125U || code == 126U {
// Super (left/right): a bare tap toggles the launcher search; never reaches clients.
if pressed {
self.super_solo = true
} else if self.super_solo {
self.super_solo = false
if !self.shell.inspecting() {
self.shell.toggle_launcher()
}
}
} else if self.shell.inspecting() {
self.super_solo = false
// Escape leaves inspect mode; other keys are not forwarded while inspecting.
if code == 1U && pressed {
self.shell.set_inspecting(false)
}
} else if self.shell.popup_open() {
self.super_solo = false
if pressed {
let name = key_name(code, (self.mods & 1U) != 0U)
if name.length() > 0 {
self.shell.key_down(name)
}
}
} else {
self.super_solo = false
match self.kbd_win {
Some(w) => self.server.keyboard_key(w, time_ms, code, pressed)
None => ()
}
}
Modifiers(dep, lat, lock, group) => {
self.mods = dep
match self.kbd_win {
Some(w) => self.server.keyboard_modifiers(w, dep, lat, lock, group)
None => ()
}
}
}
}
///| Cursor shape (wp_cursor_shape_v1 value) for the current pointer position: shell UI
/// decides over the launcher; a client window decides inside its client area; otherwise default.
pub fn ChandraCompositor::desired_cursor(self : ChandraCompositor) -> UInt {
let shell_shape = self.shell.cursor_at(self.ptr_x.to_int(), self.ptr_y.to_int())
if shell_shape != 0U {
return shell_shape
}
fn mode_cursor(m : @wm.DragMode) -> UInt {
match m {
Move => 4U
ResizeE | ResizeW => 26U
ResizeN | ResizeS => 27U
ResizeNW | ResizeSE => 29U
ResizeNE | ResizeSW => 28U
}
}
match self.wm.drag {
Some(d) => return mode_cursor(d.mode)
None => ()
}
match self.resize_at(self.ptr_x.to_int(), self.ptr_y.to_int()) {
Some((_, m)) => return mode_cursor(m)
None => ()
}
let handle = match self.split_drag {
Some(h) => Some(h)
None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int())
}
match handle {
Some(h) => return if h.horizontal { 26U } else { 27U } // ew-resize / ns-resize
None => ()
}
let handle = match self.split_drag {
Some(h) => Some(h)
None => self.handle_at(self.ptr_x.to_int(), self.ptr_y.to_int())
}
match handle {
Some(h) => return if h.horizontal { 26U } else { 27U } // ew-resize / ns-resize
None => ()
}
if self.ptr_win is None && self.shell.desk_hit(self.ptr_x.to_int(), self.ptr_y.to_int()) &&
self.window_at(self.ptr_x.to_int(), self.ptr_y.to_int()) is None {
return 4U // the desktop card can be dragged
}
match self.ptr_win {
Some(w) => self.server.client_cursor_shape(w)
None => 1U
}
}