Files
chandra/compositor/shell/shell.mbt
T
nandiandClaude Sonnet 5.5 a63f65806d
CI / test (push) Successful in 25s
Make the desktop interactive: pointer/keyboard routing, launcher popup, app launching
- server: wl_pointer/wl_keyboard events (enter/leave/motion/button/key/modifiers),
  host keymap shared with client keyboards
- nested: receives host pointer + keyboard + keymap, queues InputEvents
- host: routes input to the launcher popup or the window under the pointer;
  click-to-focus moves keyboard focus
- shell: launcher button opens lib/launcher popup; entries start real apps
  (foot, konsole, dolphin, firefox, kate, systemsettings if installed) with
  WAYLAND_DISPLAY pointed at the compositor
- sys: has_exe + spawn
- e2e test for input delivery and the launcher popup

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

322 lines
11 KiB
MoonBit

///| Loads the desktop shell (lib/shell background + panel chrome, lib/panel
/// layout, lib/launcher button, lib/clock widget) into the native compositor.
/// The shell logic is the same code the wasm components wrap; this package only
/// adapts its WIT-shaped types to the compositor's scene types.
///|
fn conv_color(c : @types.Color) -> @scene.Rgba {
{ r: c.r, g: c.g, b: c.b, a: c.a }
}
///|
fn conv_theme(t : @scene.ThemeTokens) -> @theme.Tokens {
let p = t.palette
let col = fn(c : @scene.Rgba) -> @types.Color { { r: c.r, g: c.g, b: c.b, a: c.a } }
{
name: t.name,
palette: {
desktop_top: col(p.desktop_top),
desktop_bottom: col(p.desktop_bottom),
surface: col(p.surface),
surface_hover: col(p.surface_hover),
surface_popup: col(p.surface_popup),
foreground: col(p.foreground),
foreground_muted: col(p.foreground_muted),
accent: col(p.accent),
accent_foreground: col(p.accent_foreground),
border: col(p.border),
},
font_size: t.font_size.reinterpret_as_uint(),
radius: t.radius.reinterpret_as_uint(),
spacing: t.spacing.reinterpret_as_uint(),
panel_height: t.panel_height.reinterpret_as_uint(),
}
}
///| Translate a widget render-list into scene ops, offset to screen coordinates.
fn translate(
ops : Array[@types.DrawOp],
dx : Int,
dy : Int,
out : Array[@scene.DrawOp],
) -> Unit {
let rect = fn(r : @types.Rect) -> @scene.Rect {
{
x: r.x + dx,
y: r.y + dy,
w: r.w.reinterpret_as_int(),
h: r.h.reinterpret_as_int(),
}
}
for op in ops {
match op {
FillRect(f) =>
out.push(
@scene.DrawOp::FillRect(
rect(f.rect),
conv_color(f.color),
f.radius.reinterpret_as_int(),
),
)
StrokeRect(s) =>
out.push(
@scene.DrawOp::StrokeRect(
rect(s.rect),
conv_color(s.color),
s.width.reinterpret_as_int(),
s.radius.reinterpret_as_int(),
),
)
Line(l) =>
out.push(
@scene.DrawOp::Line(
l.start.x + dx,
l.start.y + dy,
l.end.x + dx,
l.end.y + dy,
conv_color(l.color),
l.width.reinterpret_as_int(),
),
)
Circle(c) =>
out.push(
@scene.DrawOp::Circle(
c.center.x + dx,
c.center.y + dy,
c.radius.reinterpret_as_int(),
conv_color(c.color),
c.filled,
),
)
Text(t) => {
let align = match t.style.align {
LEFT => @scene.TextAlign::Left
CENTER => @scene.TextAlign::Center
RIGHT => @scene.TextAlign::Right
}
out.push(
@scene.DrawOp::Text(
t.origin.x + dx,
t.origin.y + dy,
t.text,
t.style.size.reinterpret_as_int(),
conv_color(t.style.color),
align,
t.style.bold,
),
)
}
PushClip(r) => out.push(@scene.DrawOp::PushClip(rect(r)))
PopClip => out.push(@scene.DrawOp::PopClip)
}
}
}
///| Current local time in the WIT `datetime` shape.
fn now() -> @types.Datetime {
{
year: @sys.wall_time_field(0).reinterpret_as_uint(),
month: @sys.wall_time_field(1).to_byte(),
day: @sys.wall_time_field(2).to_byte(),
hour: @sys.wall_time_field(3).to_byte(),
minute: @sys.wall_time_field(4).to_byte(),
second: @sys.wall_time_field(5).to_byte(),
weekday: @sys.wall_time_field(6).to_byte(),
}
}
///| The desktop shell: wallpaper + bottom panel with launcher, task list and clock.
pub(all) struct DesktopShell {
launcher_state : @launcher.State
apps : Array[@types.AppEntry]
exec : Map[String, String]
/// Wayland socket handed to launched apps as WAYLAND_DISPLAY.
display : String
mut screen_w : Int
mut launcher_rect : @scene.Rect
mut popup_rect : @scene.Rect
}
///| Apps the launcher offers, kept only if their executable exists.
/// (id, name, comment, category, glyph, executable)
let candidates : Array[(String, String, String, String, String, String)] = [
("org.moonplasma.terminal", "Terminal", "Command-line shell", "System", ">_", "foot"),
("org.moonplasma.konsole", "Konsole", "KDE terminal", "System", ">_", "konsole"),
("org.moonplasma.files", "Files", "Browse folders and files", "Utilities", "F", "dolphin"),
("org.moonplasma.browser", "Web Browser", "Browse the web", "Internet", "W", "firefox"),
("org.moonplasma.editor", "Text Editor", "Edit plain text files", "Utilities", "T", "kate"),
("org.moonplasma.settings", "System Settings", "Configure the desktop", "System", "S", "systemsettings"),
]
///|
pub fn DesktopShell::new(display : String) -> DesktopShell {
let apps : Array[@types.AppEntry] = []
let exec : Map[String, String] = {}
for c in candidates {
let (id, name, comment, category, glyph, bin) = c
if @sys.has_exe(bin) {
apps.push({ id, name, comment, category, glyph })
exec.set(id, bin)
}
}
{
launcher_state: @launcher.State::new(),
apps,
exec,
display,
screen_w: 0,
launcher_rect: { x: 0, y: 0, w: 0, h: 0 },
popup_rect: { x: 0, y: 0, w: 0, h: 0 },
}
}
///|
fn inside(r : @scene.Rect, x : Int, y : Int) -> Bool {
r.w > 0 && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h
}
///| True while the launcher popup is open (it then owns keyboard input).
pub fn DesktopShell::popup_open(self : DesktopShell) -> Bool {
self.launcher_state.open
}
///| True if (x, y) is over the launcher button or its open popup.
pub fn DesktopShell::hit(self : DesktopShell, x : Int, y : Int) -> Bool {
inside(self.launcher_rect, x, y) ||
(self.launcher_state.open && inside(self.popup_rect, x, y))
}
///|
fn DesktopShell::launch(self : DesktopShell, id : String) -> Result[Unit, String] {
match self.exec.get(id) {
None => Err("no executable for " + id)
Some(bin) =>
if @sys.spawn(bin, self.display) {
Ok(())
} else {
Err("could not start " + bin)
}
}
}
///| Pointer press at (x, y). Returns true when the shell consumed it.
pub fn DesktopShell::pointer_down(self : DesktopShell, x : Int, y : Int) -> Bool {
let launch = fn(id : String) { self.launch(id) }
if inside(self.launcher_rect, x, y) {
ignore(
@launcher.handle_event(self.apps, self.launcher_state, Widget, PointerDown({ x: x - self.launcher_rect.x, y: y - self.launcher_rect.y }), launch),
)
true
} else if self.launcher_state.open {
if inside(self.popup_rect, x, y) {
ignore(
@launcher.handle_event(self.apps, self.launcher_state, Popup, PointerDown({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch),
)
true
} else {
// Click outside: dismiss, and let the click through.
ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, FocusLost, launch))
false
}
} else {
false
}
}
///| Pointer motion; returns true when over the popup (so windows don't see it).
pub fn DesktopShell::pointer_move(self : DesktopShell, x : Int, y : Int) -> Bool {
if self.launcher_state.open {
let launch = fn(id : String) { self.launch(id) }
if inside(self.popup_rect, x, y) {
ignore(
@launcher.handle_event(self.apps, self.launcher_state, Popup, PointerMove({ x: x - self.popup_rect.x, y: y - self.popup_rect.y }), launch),
)
return true
}
ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, PointerLeave, launch))
}
false
}
///| Key name as used by lib/launcher ("Escape", "Backspace", "Enter" or one character).
pub fn DesktopShell::key_down(self : DesktopShell, name : String) -> Unit {
let launch = fn(id : String) { self.launch(id) }
ignore(@launcher.handle_event(self.apps, self.launcher_state, Popup, KeyDown(name), launch))
}
///| Build the (background, panel) render-lists for the current frame.
pub fn DesktopShell::render(
self : DesktopShell,
w : Int,
h : Int,
theme : @scene.ThemeTokens,
layout_label : String,
window_titles : Array[String],
) -> (Array[@scene.DrawOp], Array[@scene.DrawOp]) {
let tokens = conv_theme(theme)
self.screen_w = w
let bg : Array[@scene.DrawOp] = []
translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg)
let (_, _, clock_w, clock_h, clock_min) = @clock.digital_info()
let slots : Array[@panel.SlotIn] = [
{ id: "launcher", pref_w: 190, pref_h: 36, min_w: 170, stretch: false, end_align: false },
{ id: "layout", pref_w: 96, pref_h: 36, min_w: 80, stretch: false, end_align: false },
{ id: "tasks", pref_w: 100, pref_h: 36, min_w: 0, stretch: true, end_align: false },
{ id: "clock", pref_w: clock_w, pref_h: clock_h, min_w: clock_min, stretch: false, end_align: true },
]
let panel_ops : Array[@scene.DrawOp] = []
match @panel.layout(w, h, theme.panel_height, 4, tokens.spacing.reinterpret_as_int(), slots) {
Err(_) => ()
Ok(pl) => {
let pr = @ui.rect(pl.x, pl.y, pl.w, pl.h)
translate(@libshell.render_panel(pr, tokens), 0, 0, panel_ops)
for p in pl.placements {
let size = @ui.size(p.w, p.h)
let local : Array[@types.DrawOp] = match p.id {
"launcher" => @launcher.render_button(size, tokens, self.launcher_state)
"clock" => @clock.render_digital(size, tokens, now())
"layout" => [
@ui.fill_rect(@ui.rect(0, 0, p.w, p.h), tokens.palette.surface_hover, radius=tokens.radius.reinterpret_as_int()),
@ui.text(p.w / 2, p.h / 2, layout_label, tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground, align=CENTER),
]
_ => []
}
translate(local, p.x, p.y, panel_ops)
if p.id == "launcher" {
self.launcher_rect = { x: p.x, y: p.y, w: p.w, h: p.h }
}
if p.id == "tasks" {
let mut tx = 0
for title in window_titles {
let pill_w = 140
if tx + pill_w <= p.w {
translate(
[
@ui.fill_rect(@ui.rect(tx, 0, pill_w, p.h), tokens.palette.surface_hover, radius=tokens.radius.reinterpret_as_int()),
@ui.text(tx + 10, p.h / 2, title, tokens.font_size.reinterpret_as_int() - 2, tokens.palette.foreground),
],
p.x,
p.y,
panel_ops,
)
tx = tx + pill_w + 6
}
}
}
}
}
}
if self.launcher_state.open {
let size = @launcher.popup_size(self.apps, self.launcher_state)
let pw = size.w.reinterpret_as_int()
let ph = size.h.reinterpret_as_int()
let (px, py) = @panel.popup_position(w, h - theme.panel_height, self.launcher_rect.x, pw, ph, 6)
self.popup_rect = { x: px, y: py, w: pw, h: ph }
translate(@launcher.render_popup(self.apps, tokens, self.launcher_state), px, py, panel_ops)
} else {
self.popup_rect = { x: 0, y: 0, w: 0, h: 0 }
}
(bg, panel_ops)
}