Task manager: draw each app's real icon from its .desktop file
CI / test (push) Failing after 16s

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
This commit is contained in:
Claude committed 2026-11-25 18:52:58 +00:00
1 parent 0e6a1257e1
commit f2e870c251
53 files changed
+2284 -473

No files matched your search

+10 -1
View File
@@ -10,6 +10,8 @@ priv struct DesktopApp {
command : String
/// StartupWMClass: the app id its windows carry when that is not the desktop file's name.
wm_class : String
/// Icon=: an icon theme name ("firefox") or an absolute path.
icon : String
}
///| Directories scanned, lowest priority first (later ones override by file name).
@@ -66,6 +68,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
let mut terminal = false
let mut kind = "Application"
let mut wm_class = ""
let mut icon = ""
for raw in text.split("\n") {
let line = raw.trim(chars=" \r\t").to_string()
if line.length() == 0 || line.has_prefix("#") {
@@ -93,6 +96,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
"NoDisplay" | "Hidden" => if val == "true" { hidden = true }
"Terminal" => terminal = val == "true"
"StartupWMClass" => wm_class = val
"Icon" => icon = val
_ => ()
}
}
@@ -113,7 +117,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
_ => "Other"
}
let comment = if comment.length() > 0 { comment } else { generic }
Some({ id, name, comment, category, command, wm_class })
Some({ id, name, comment, category, command, wm_class, icon })
}
///| Scan every applications dir; later dirs override earlier ones, result sorted by name.
@@ -169,6 +173,11 @@ test "parse_desktop keeps StartupWMClass" {
Some(a) => assert_eq(a.wm_class, "Code")
None => fail("expected entry")
}
let t2 = "[Desktop Entry]\nType=Application\nName=Files\nExec=nautilus\nIcon=org.gnome.Nautilus\n[Desktop Action w]\nIcon=other\n"
match parse_desktop("org.gnome.Nautilus", t2, true) {
Some(a) => assert_eq(a.icon, "org.gnome.Nautilus")
None => fail("expected entry")
}
}
///|
+145
View File
@@ -0,0 +1,145 @@
///| 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"], []), [])
}
+1
View File
@@ -14,6 +14,7 @@ import {
"jqlssg/moonedn" @edn,
"chandra/desktop/compositor/sys" @sys,
"chandra/desktop/compositor/renderer" @renderer,
"chandra/desktop/compositor/raster" @raster,
"chandra/desktop/compositor/wasm" @wasm,
}
+14
View File
@@ -108,6 +108,7 @@ fn translate(
}
PushClip(r) => out.push(@scene.DrawOp::PushClip(rect(r)))
PopClip => out.push(@scene.DrawOp::PopClip)
Image(i) => out.push(@scene.DrawOp::Image(rect(i.rect), i.image))
}
}
}
@@ -184,6 +185,10 @@ pub(all) struct DesktopShell {
exec : Map[String, String]
/// Lower-cased StartupWMClass -> desktop file id, for apps whose windows' app id differs.
wm_classes : Map[String, String]
/// Desktop file id -> its Icon= value, and the image handle found for each window app id
/// (0: no icon), for the `icons` capability.
icon_names : Map[String, String]
icon_handles : Map[String, UInt]
/// Wayland socket handed to launched apps as WAYLAND_DISPLAY.
display : String
/// Panel modules in panel order; a module with `stretch` splits the panel into a left and a right group.
@@ -266,7 +271,11 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
let apps : Array[@types.AppEntry] = []
let exec : Map[String, String] = {}
let wm_classes : Map[String, String] = {}
let icon_names : Map[String, String] = {}
for a in scan_desktop_apps() {
if a.icon.length() > 0 {
icon_names.set(a.id, a.icon)
}
if a.wm_class.length() > 0 {
wm_classes.set(a.wm_class.to_lower(), a.id)
}
@@ -282,6 +291,8 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
apps,
exec,
wm_classes,
icon_names,
icon_handles: {},
display,
widgets: [],
next_slot: 1,
@@ -785,6 +796,9 @@ pub fn DesktopShell::render(
self.sampler.refresh()
@wasm.set_windows(self.win_list)
}
if self.widgets.iter().any(w => w.has_cap("icons")) {
self.publish_icons()
}
let bg : Array[@scene.DrawOp] = []
translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg)
self.wallpaper_len = bg.length()