Files
chandra/compositor/scene/scene.mbt
T
Claude f2e870c251
CI / test (push) Failing after 16s
Task manager: draw each app's real icon from its .desktop file
Each pill now shows the icon the app's .desktop file names (Icon=), looked up in the
freedesktop icon themes, instead of the letter tile. The tile stays as the fallback for
windows with no app id, no desktop file or no icon the host can read.

- WIT: a new `icons` capability (`app-icon(app-id) -> u32`, a handle, never a path or pixels)
  and an `image(image-op)` draw op (appended, so existing modules keep their ABI).
  taskbar-widget and plasmoid-host import `icons`. Host raster gains `load-image(path)` and a
  `draw-image` op; the plasmoids host gets an icons table (6: app id, handle).
- Compositor (shell/icons.mbt): window app id -> desktop file (resolve_app_id, so StartupWMClass
  and last-dotted-part matches work) -> Icon= -> theme lookup (XDG data dirs, ~/.icons,
  flatpak exports; hicolor, Adwaita, breeze; 48/64/32 px PNG first, then scalable SVG when the
  host can draw it, then other sizes, then /usr/share/pixmaps). Absolute Icon= paths are used as
  they are. Results are cached per app id; the table is published only when a module imports
  `icons`.
- wasm host (host-wasm): PNG via the png crate, SVG via resvg (rendered sharp at each size),
  PNGs area-averaged to the drawn size; loads cached by path.
- Native build: a small PNG decoder (compositor/renderer/png.mbt: inflate + filters, 1-16 bit,
  palettes with tRNS) and the Image op in the software renderer. SVG-only icons fall back to the
  tile there. The wasmer stub serves `icons.app-icon` from the same table.
- Tests: PNG decoder fixtures (all filters, fixed/dynamic/stored blocks, palette), image scaling
  and drawing, icon lookup order, Icon= parsing, task module icon-or-tile (native and wasmtime),
  host raster PNG+SVG loading and drawing. Checked end to end with the wasm host: foot's
  hicolor icon shows on its pill.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhmtDt52EjaW8jevGU
2026-11-25 18:52:58 +00:00

562 lines
13 KiB
MoonBit

///| Scene graph, theme tokens, damage tracking, and Breeze-style SSD + shell draw-ops in pure MoonBit.
///|
#derive(Eq, Debug)
pub(all) struct Rgba {
r : Byte
g : Byte
b : Byte
a : Byte
}
///|
pub fn Rgba::new(r : Int, g : Int, b : Int, a : Int) -> Rgba {
{
r: r.clamp(min=0, max=255).to_byte(),
g: g.clamp(min=0, max=255).to_byte(),
b: b.clamp(min=0, max=255).to_byte(),
a: a.clamp(min=0, max=255).to_byte(),
}
}
///|
pub fn Rgba::with_alpha(self : Rgba, a : Int) -> Rgba {
{ r: self.r, g: self.g, b: self.b, a: a.clamp(min=0, max=255).to_byte() }
}
///|
fn hex_nibble(c : Int) -> Int {
if c >= 48 && c <= 57 {
c - 48
} else if c >= 97 && c <= 102 {
c - 97 + 10
} else if c >= 65 && c <= 70 {
c - 65 + 10
} else {
0
}
}
///|
fn parse_hex_byte(s : String, idx : Int) -> Int {
let hi = hex_nibble(s[idx].to_int())
let lo = hex_nibble(s[idx + 1].to_int())
(hi << 4) | lo
}
///|
pub fn Rgba::from_hex(s : String) -> Rgba {
let hex = if s.has_prefix("#") {
s.clamped_view(start=1, end=s.length()).to_owned()
} else {
s
}
if hex.length() == 6 {
Rgba::new(
parse_hex_byte(hex, 0),
parse_hex_byte(hex, 2),
parse_hex_byte(hex, 4),
255,
)
} else if hex.length() == 8 {
Rgba::new(
parse_hex_byte(hex, 0),
parse_hex_byte(hex, 2),
parse_hex_byte(hex, 4),
parse_hex_byte(hex, 6),
)
} else {
Rgba::new(0, 0, 0, 255)
}
}
///|
pub(all) struct ThemePalette {
desktop_top : Rgba
desktop_bottom : Rgba
surface : Rgba
surface_hover : Rgba
surface_popup : Rgba
foreground : Rgba
foreground_muted : Rgba
accent : Rgba
accent_foreground : Rgba
border : Rgba
}
///|
pub(all) struct ThemeTokens {
name : String
palette : ThemePalette
font_size : Int
radius : Int
spacing : Int
panel_height : Int
}
///|
pub fn ThemeTokens::moon_night() -> ThemeTokens {
{
name: "Moon Night",
palette: {
desktop_top: Rgba::from_hex("#16213e"),
desktop_bottom: Rgba::from_hex("#0b1020"),
surface: Rgba::from_hex("#1f2937f2"),
surface_hover: Rgba::from_hex("#374151"),
surface_popup: Rgba::from_hex("#111827f5"),
foreground: Rgba::from_hex("#f3f4f6"),
foreground_muted: Rgba::from_hex("#9ca3af"),
accent: Rgba::from_hex("#7c9cff"),
accent_foreground: Rgba::from_hex("#0b1020"),
border: Rgba::from_hex("#3b4658"),
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///| The light counterpart of `moon_night` (config/themes/moon-day.json): what the settings
/// module's "Dark mode" switch turns to when off.
pub fn ThemeTokens::moon_day() -> ThemeTokens {
{
name: "Moon Day",
palette: {
desktop_top: Rgba::from_hex("#cfe3ff"),
desktop_bottom: Rgba::from_hex("#8fb4e8"),
surface: Rgba::from_hex("#f4f6fbf2"),
surface_hover: Rgba::from_hex("#dde3f0"),
surface_popup: Rgba::from_hex("#fbfcfff5"),
foreground: Rgba::from_hex("#1b2233"),
foreground_muted: Rgba::from_hex("#5b6577"),
accent: Rgba::from_hex("#3b6bff"),
accent_foreground: Rgba::from_hex("#ffffff"),
border: Rgba::from_hex("#b9c3d6"),
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
fn json_str(obj : Map[String, Json], key : String, fallback : String) -> String {
match obj.get(key) {
Some(String(s)) => s
_ => fallback
}
}
///|
fn json_int(obj : Map[String, Json], key : String, fallback : Int) -> Int {
match obj.get(key) {
Some(Number(n, ..)) => n.to_int()
_ => fallback
}
}
///|
pub fn ThemeTokens::from_json_string(text : String) -> ThemeTokens {
let def = ThemeTokens::moon_night()
let parsed = try {
@json.parse(text)
} catch {
_ => return def
}
guard parsed is Object(root) else { return def }
let pal_obj = match root.get("palette") {
Some(Object(m)) => m
_ => return def
}
{
name: json_str(root, "name", def.name),
palette: {
desktop_top: Rgba::from_hex(json_str(pal_obj, "desktop-top", "#16213e")),
desktop_bottom: Rgba::from_hex(
json_str(pal_obj, "desktop-bottom", "#0b1020"),
),
surface: Rgba::from_hex(json_str(pal_obj, "surface", "#1f2937f2")),
surface_hover: Rgba::from_hex(
json_str(pal_obj, "surface-hover", "#374151"),
),
surface_popup: Rgba::from_hex(
json_str(pal_obj, "surface-popup", "#111827f5"),
),
foreground: Rgba::from_hex(json_str(pal_obj, "foreground", "#f3f4f6")),
foreground_muted: Rgba::from_hex(
json_str(pal_obj, "foreground-muted", "#9ca3af"),
),
accent: Rgba::from_hex(json_str(pal_obj, "accent", "#7c9cff")),
accent_foreground: Rgba::from_hex(
json_str(pal_obj, "accent-foreground", "#0b1020"),
),
border: Rgba::from_hex(json_str(pal_obj, "border", "#3b4658")),
},
font_size: json_int(root, "font-size", def.font_size),
radius: json_int(root, "radius", def.radius),
spacing: json_int(root, "spacing", def.spacing),
panel_height: json_int(root, "panel-height", def.panel_height),
}
}
///|
#derive(Eq, Debug)
pub(all) struct Rect {
x : Int
y : Int
w : Int
h : Int
}
///|
pub fn Rect::new(x : Int, y : Int, w : Int, h : Int) -> Rect {
{ x, y, w: if w < 0 { 0 } else { w }, h: if h < 0 { 0 } else { h } }
}
///|
pub fn Rect::union(self : Rect, other : Rect) -> Rect {
let x0 = if self.x < other.x { self.x } else { other.x }
let y0 = if self.y < other.y { self.y } else { other.y }
let self_x1 = self.x + self.w
let other_x1 = other.x + other.w
let x1 = if self_x1 > other_x1 { self_x1 } else { other_x1 }
let self_y1 = self.y + self.h
let other_y1 = other.y + other.h
let y1 = if self_y1 > other_y1 { self_y1 } else { other_y1 }
Rect::new(x0, y0, x1 - x0, y1 - y0)
}
///|
pub fn Rect::contains(self : Rect, px : Int, py : Int) -> Bool {
px >= self.x && py >= self.y && px < self.x + self.w && py < self.y + self.h
}
///|
#derive(Eq, Debug)
pub(all) enum TextAlign {
Left
Center
Right
}
///|
#derive(Eq)
pub(all) enum DrawOp {
FillRect(Rect, Rgba, Int)
StrokeRect(Rect, Rgba, Int, Int)
Line(Int, Int, Int, Int, Rgba, Int)
Circle(Int, Int, Int, Rgba, Bool)
Text(Int, Int, String, Int, Rgba, TextAlign, Bool)
/// Same as Text but in the monospace face (code listings).
MonoText(Int, Int, String, Int, Rgba, TextAlign, Bool)
PushClip(Rect)
PopClip
/// A loaded image (`@renderer.add_image` / `raster.load-image` handle) scaled into the rect.
Image(Rect, UInt)
}
///|
pub(all) struct ShmBufferSnapshot {
width : Int
height : Int
stride : Int
/// Wayland wl_shm format: 0 = ARGB8888, 1 = XRGB8888
format : Int
bytes : Bytes
/// With the host rasteriser (the wasm compositor) the pixels live in the host, not in `bytes`
/// (empty): this is the host's surface id, 0 when there is none.
host_id : UInt
}
///|
pub(all) struct WindowSceneNode {
id : UInt64
title : String
app_id : String
focused : Bool
fullscreen : Bool
geometry : Rect
/// (top, right, bottom, left)
deco_insets : (Int, Int, Int, Int)
deco_ops : Array[DrawOp]
surface : ShmBufferSnapshot?
}
///|
pub(all) struct PanelWidgetNode {
id : String
rect : Rect
ops : Array[DrawOp]
popup : (Rect, Array[DrawOp])?
}
///|
pub(all) struct DamageTracker {
regions : Array[Rect]
mut full_damage : Bool
}
///|
pub fn DamageTracker::new() -> DamageTracker {
{ regions: [], full_damage: true }
}
///|
pub fn DamageTracker::mark_full(self : DamageTracker) -> Unit {
self.full_damage = true
self.regions.clear()
}
///|
pub fn DamageTracker::mark_rect(self : DamageTracker, rect : Rect) -> Unit {
if self.full_damage || rect.w <= 0 || rect.h <= 0 {
return
}
self.regions.push(rect)
}
///|
pub fn DamageTracker::take_damage(
self : DamageTracker,
output_w : Int,
output_h : Int,
) -> Array[Rect] {
if self.full_damage {
self.full_damage = false
self.regions.clear()
return [Rect::new(0, 0, output_w, output_h)]
}
let out : Array[Rect] = []
for r in self.regions {
out.push(r)
}
self.regions.clear()
out
}
///|
pub(all) struct SceneGraph {
output_w : Int
output_h : Int
/// The static wallpaper, drawn first; the renderer keeps its pixels between frames.
mut wallpaper_ops : Array[DrawOp]
mut background_ops : Array[DrawOp]
mut windows : Array[WindowSceneNode]
mut panel_ops : Array[DrawOp]
mut widgets : Array[PanelWidgetNode]
/// Client popup surfaces (menus, tooltips), drawn above everything.
mut popups : Array[(Rect, ShmBufferSnapshot)]
}
///|
pub fn SceneGraph::new(output_w : Int, output_h : Int) -> SceneGraph {
{
output_w,
output_h,
wallpaper_ops: [],
background_ops: [],
windows: [],
panel_ops: [],
widgets: [],
popups: [],
}
}
///| Generate Breeze-style Server-Side Decoration draw-ops for a window.
pub fn build_breeze_decoration(
title : String,
app_id : String,
focused : Bool,
fullscreen : Bool,
outer_w : Int,
outer_h : Int,
tokens : ThemeTokens,
) -> ((Int, Int, Int, Int), Array[DrawOp]) {
if fullscreen {
return ((0, 0, 0, 0), [])
}
let p = tokens.palette
let title_h = 32
let border = 2
let radius = tokens.radius
let title_bg = if focused { p.surface } else { p.surface.with_alpha(210) }
let border_col = if focused { p.accent } else { p.border }
let title_fg = if focused { p.foreground } else { p.foreground_muted }
let ops : Array[DrawOp] = [
FillRect(Rect::new(0, 0, outer_w, title_h), title_bg, radius),
StrokeRect(Rect::new(0, 0, outer_w, outer_h), border_col, border, radius),
Line(border, title_h - 1, outer_w - border, title_h - 1, p.border, 1),
]
// Window control buttons on the right: [ _ ] [ [] ] [ X ]
let btn_size = 18
let btn_y = (title_h - btn_size) / 2
let close_x = outer_w - 10 - btn_size
let max_x = close_x - 8 - btn_size
let min_x = max_x - 8 - btn_size
// Title: truncated so it never runs under the buttons or past the window edge.
let label = if title.length() > 0 { title } else { app_id }
let char_w = if tokens.font_size >= 14 { 12 } else { 6 }
let max_chars = (min_x - 8 - 14) / char_w
let shown = if max_chars < 3 {
""
} else if label.length() <= max_chars {
label
} else {
label[:max_chars - 2].to_string() + ".."
}
ops.push(
Text(14, title_h / 2, shown, tokens.font_size, title_fg, Left, focused),
)
let close_bg = if focused {
Rgba::new(218, 68, 83, 255)
} else {
p.surface_hover
}
let btn_bg = p.surface_hover
ops.push(FillRect(Rect::new(min_x, btn_y, btn_size, btn_size), btn_bg, 9))
ops.push(
Line(
min_x + 5,
btn_y + btn_size - 6,
min_x + btn_size - 5,
btn_y + btn_size - 6,
title_fg,
2,
),
)
ops.push(FillRect(Rect::new(max_x, btn_y, btn_size, btn_size), btn_bg, 9))
ops.push(
StrokeRect(
Rect::new(max_x + 5, btn_y + 5, btn_size - 10, btn_size - 10),
title_fg,
1,
0,
),
)
ops.push(FillRect(Rect::new(close_x, btn_y, btn_size, btn_size), close_bg, 9))
ops.push(
Line(
close_x + 5,
btn_y + 5,
close_x + btn_size - 5,
btn_y + btn_size - 5,
p.accent_foreground,
2,
),
)
ops.push(
Line(
close_x + btn_size - 5,
btn_y + 5,
close_x + 5,
btn_y + btn_size - 5,
p.accent_foreground,
2,
),
)
((title_h, border, border, border), ops)
}
///| Generate desktop background gradient + bottom panel draw-ops.
pub fn build_shell_ops(
w : Int,
h : Int,
tokens : ThemeTokens,
layout_label : String,
window_titles : Array[String],
) -> (Array[DrawOp], Array[DrawOp]) {
let p = tokens.palette
let bg_ops : Array[DrawOp] = []
let bands = 16
let band_h = (h + bands - 1) / bands
for i = 0; i < bands; i = i + 1 {
let t_r = p.desktop_top.r.to_int()
let t_g = p.desktop_top.g.to_int()
let t_b = p.desktop_top.b.to_int()
let b_r = p.desktop_bottom.r.to_int()
let b_g = p.desktop_bottom.g.to_int()
let b_b = p.desktop_bottom.b.to_int()
let r = t_r + (b_r - t_r) * i / (bands - 1)
let g = t_g + (b_g - t_g) * i / (bands - 1)
let b = t_b + (b_b - t_b) * i / (bands - 1)
bg_ops.push(
FillRect(
Rect::new(0, i * band_h, w, band_h + 1),
Rgba::new(r, g, b, 255),
0,
),
)
}
let ph = tokens.panel_height
let py = h - ph
let panel_ops : Array[DrawOp] = [
FillRect(Rect::new(0, py, w, ph), p.surface, 0),
Line(0, py, w, py, p.border, 1),
// Launcher pill
FillRect(Rect::new(8, py + 6, 92, ph - 12), p.accent, tokens.radius),
Text(
54,
py + ph / 2,
"Moon",
tokens.font_size,
p.accent_foreground,
Center,
true,
),
// Layout badge
FillRect(
Rect::new(108, py + 6, 96, ph - 12),
p.surface_hover,
tokens.radius,
),
Text(
156,
py + ph / 2,
layout_label,
tokens.font_size - 2,
p.foreground,
Center,
false,
),
]
let mut tx = 216
for title in window_titles {
let pill_w = 140
if tx + pill_w < w - 100 {
panel_ops.push(
FillRect(
Rect::new(tx, py + 6, pill_w, ph - 12),
p.surface_hover,
tokens.radius,
),
)
panel_ops.push(
Text(
tx + 10,
py + ph / 2,
title,
tokens.font_size - 2,
p.foreground,
Left,
false,
),
)
tx = tx + pill_w + 6
}
}
(bg_ops, panel_ops)
}