Files
chandra/compositor/host/compositor.mbt
T
nandiandClaude Sonnet 5.5 70ad87f2d2
CI / test (push) Failing after 18s
Task manager panel module and Alt+Tab window switcher
- components/tasks: the 'tasks' module the panel and components.txt already named but whose source
  was missing. One pill per window in opening order (stable, not stacking order), focused lit,
  minimized dimmed, hover, titles shortened, pills shrink to fit; click focuses/restores.
  Tests that loaded it (task list, title-bar minimize/restore) pass again.
- compositor/host/switcher.mbt: hold Alt, press Tab: most-recently-used window list with the
  previous window selected; Tab/Shift+Tab/arrows move, releasing Alt switches (restoring a
  minimized window), Escape cancels. Tab is not forwarded to clients.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 03:39:19 -08:00

1235 lines
36 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.
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
output_w : Int
output_h : Int
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?
/// 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
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
}
///|
pub fn MoonPlasmaCompositor::new(
socket_path : String,
theme : @scene.ThemeTokens,
output_w : Int,
output_h : Int,
) -> MoonPlasmaCompositor? {
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 out_rect : @wm.Rect = { x: 0, y: 0, w: output_w, h: output_h }
let work_area = @wm.compute_work_area(
out_rect,
excl_top=32, // room for the server-side title bar drawn above each client
excl_bottom=theme.panel_height,
)
let shell = @shell.DesktopShell::new(socket_path)
shell.set_output(theme, output_w, output_h)
Some({
server,
wm,
theme,
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,
press_fwd: false,
kbd_win: None,
mods: 0U,
super_solo: false,
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,
})
}
///|
pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
self.shell.shutdown()
self.server.close()
}
///| 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 MoonPlasmaCompositor::apply_actions(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::flush_configures(self : MoonPlasmaCompositor) -> 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 MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> 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)
}
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
match self.windows.get(wid) {
Some(win) => {
if app_id.length() > 0 {
win.app_id = 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) => {
win.surface = Some(s)
self.damage.mark_rect(win.geometry)
}
None => ()
}
}
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
}
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(_) => ()
}
}
}
///|
pub fn MoonPlasmaCompositor::set_layout_mode(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::restore_state(self : MoonPlasmaCompositor) -> Unit {
let st = self.shell.load_state()
self.shell.apply_state(st)
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)
_ => ()
}
}
///|
pub fn MoonPlasmaCompositor::compose_frame(
self : MoonPlasmaCompositor,
) -> @renderer.Pixmap {
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()
scene.background_ops = bg_ops
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),
}
// Nothing changed since the last frame (no client commit, same shell and decoration ops):
// hand back the previous pixels instead of rasterising 2 million pixels again.
match (self.last_frame, self.last_sig) {
(Some(prev), Some(ps)) =>
if !damaged && ps == sig {
self.frame_changed = false
return prev
}
_ => ()
}
self.frame_changed = true
self.last_sig = Some(sig)
let t1 = @sys.monotonic_ms()
let px = @renderer.render_scene(scene)
self.last_frame = Some(px)
self.prof_shell_ms = self.prof_shell_ms + (t1 - t0)
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 MoonPlasmaCompositor::present_to_gfx(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::handle_gfx_events(
self : MoonPlasmaCompositor,
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.
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 {
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 MoonPlasmaCompositor::window_at(self : MoonPlasmaCompositor, 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 MoonPlasmaCompositor::resize_at(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::title_button_at(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::press_title_button(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::activate_window(self : MoonPlasmaCompositor, 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 MoonPlasmaCompositor::activate_task(self : MoonPlasmaCompositor, 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),
)
}
///| Carry out what panel modules asked for: focus a window, switch layout.
fn MoonPlasmaCompositor::run_shell_actions(self : MoonPlasmaCompositor) -> 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::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 MoonPlasmaCompositor::handle_at(
self : MoonPlasmaCompositor,
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 MoonPlasmaCompositor::leave_pointer(self : MoonPlasmaCompositor) -> 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 MoonPlasmaCompositor::handle_input(
self : MoonPlasmaCompositor,
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.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(v) => {
// ~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))
}
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
}
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; a tiled window becomes floating first.
self.leave_pointer()
self.activate_window(wid)
self.apply_actions(self.wm.float_window(wid, self.work_area))
ignore(self.wm.start_drag(wid, @wm.DragMode::Move, xi, yi))
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 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 MoonPlasmaCompositor::desired_cursor(self : MoonPlasmaCompositor) -> 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
}
}