Files
chandra/compositor/shell/icons.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

146 lines
5.1 KiB
MoonBit

///| App icons for panel modules (the `icons` capability): a window's app id is matched to its
/// .desktop file (`resolve_app_id`), whose Icon= key is looked up in the freedesktop icon themes
/// (a simplified icon theme spec lookup), and the file is loaded for `Image` draw ops.
///| Base directories icon themes live in, highest priority first.
fn icon_base_dirs() -> Array[String] {
let home = @sys.getenv("HOME")
let data_home = {
let x = @sys.getenv("XDG_DATA_HOME")
if x.length() > 0 { x } else { home + "/.local/share" }
}
let dirs = [data_home + "/icons", home + "/.icons"]
let xdg = @sys.getenv("XDG_DATA_DIRS")
let list = if xdg.length() > 0 { xdg } else { "/usr/local/share:/usr/share" }
for p in list.split(":") {
if p.length() > 0 {
dirs.push(p.to_string() + "/icons")
}
}
dirs.push(home + "/.local/share/flatpak/exports/share/icons")
dirs.push("/var/lib/flatpak/exports/share/icons")
dirs
}
///| Themes searched: hicolor is where apps install their icons; the others cover apps whose icon
/// only ships with a desktop theme.
let icon_themes : Array[String] = ["hicolor", "Adwaita", "breeze"]
///| Fixed-size directories, best first for a ~22-48 px icon (scaled down well); the scalable SVG
/// goes after the first three when the host can draw SVG.
let icon_sizes : Array[String] = [
"48x48", "64x64", "32x32", "96x96", "128x128", "256x256", "24x24", "22x22", "16x16",
"512x512",
]
///| Candidate files for icon `name` (an Icon= value), in lookup order. `svg_ok`: whether the
/// image loader can read SVG (the host does; the native software renderer only reads PNG).
fn icon_candidates(
name : String,
svg_ok : Bool,
bases : Array[String],
pixmaps : Array[String],
) -> Array[String] {
if name.length() == 0 {
return []
}
if name.has_prefix("/") {
return if name.has_suffix(".png") || (svg_ok && name.has_suffix(".svg")) {
[name]
} else {
[]
}
}
// Icon=foo.png is not allowed by the spec but common: look it up as "foo".
let stem = if name.has_suffix(".png") || name.has_suffix(".svg") || name.has_suffix(".xpm") {
name[:name.length() - 4].to_string()
} else {
name
}
let out : Array[String] = []
for theme in icon_themes {
for base in bases {
let dir = base + "/" + theme
for i = 0; i < icon_sizes.length(); i = i + 1 {
if i == 3 && svg_ok {
out.push(dir + "/scalable/apps/" + stem + ".svg")
}
out.push(dir + "/" + icon_sizes[i] + "/apps/" + stem + ".png")
}
}
}
for p in pixmaps {
out.push(p + "/" + stem + ".png")
if svg_ok {
out.push(p + "/" + stem + ".svg")
}
}
out
}
///| The icon file for Icon= value `name`, or None when no theme has it.
fn find_icon_file(name : String, svg_ok : Bool) -> String? {
let pixmaps = ["/usr/share/pixmaps"]
for path in icon_candidates(name, svg_ok, icon_base_dirs(), pixmaps) {
if @sys.read_file_bytes(path).length() > 0 {
return Some(path)
}
}
None
}
///| The image handle of the icon for windows carrying `app_id` (0: none). Cached per app id,
/// so each app's themes are searched once.
fn DesktopShell::app_icon(self : DesktopShell, app_id : String) -> UInt {
match self.icon_handles.get(app_id) {
Some(h) => return h
None => ()
}
let icon = match resolve_app_id(app_id, self.exec, self.wm_classes) {
Some(id) => self.icon_names.get(id).unwrap_or("")
None => ""
}
// No desktop file: many apps' icons are named after their app id anyway.
let name = if icon.length() > 0 { icon } else { app_id }
let h = match find_icon_file(name, @sys.host_raster()) {
Some(path) => @raster.load_image(path)
None => 0U
}
self.icon_handles.set(app_id, h)
h
}
///| Publish the icons table (app id -> image handle) for the windows' apps.
fn DesktopShell::publish_icons(self : DesktopShell) -> Unit {
let t = @wasm.Table::new()
let seen : Map[String, Bool] = {}
for w in self.win_list {
if w.app_id.length() > 0 && !seen.contains(w.app_id) {
seen.set(w.app_id, true)
let h = self.app_icon(w.app_id)
if h != 0U {
t.str(w.app_id)
t.int(h.reinterpret_as_int())
t.end_row()
}
}
}
@wasm.publish(Icons, t)
}
///|
test "icon lookup tries the themes' sized PNGs, then scalable SVG when it can, then pixmaps" {
let c = icon_candidates("firefox", true, ["/u/icons"], ["/p"])
assert_eq(c[0], "/u/icons/hicolor/48x48/apps/firefox.png")
assert_eq(c[3], "/u/icons/hicolor/scalable/apps/firefox.svg")
assert_eq(c[c.length() - 2], "/p/firefox.png")
assert_true(c.contains("/u/icons/breeze/16x16/apps/firefox.png"))
// The software renderer reads PNG only.
assert_false(icon_candidates("firefox", false, ["/u/icons"], ["/p"]).iter().any(p => p.has_suffix(".svg")))
// Icon=foo.png means foo; absolute paths are taken as they are.
assert_eq(icon_candidates("foo.png", false, ["/b"], [])[0], "/b/hicolor/48x48/apps/foo.png")
assert_eq(icon_candidates("/opt/app/icon.png", false, ["/b"], []), ["/opt/app/icon.png"])
assert_eq(icon_candidates("/opt/app/icon.svg", false, ["/b"], []), [])
assert_eq(icon_candidates("", true, ["/b"], []), [])
}