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

238 lines
7.3 KiB
MoonBit

///| Installed desktop applications, discovered from freedesktop `.desktop` files
/// (XDG data dirs + flatpak exports). Feeds the launcher's search.
///|
priv struct DesktopApp {
id : String
name : String
comment : String
category : String
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).
fn app_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 : Array[String] = []
let xdg = @sys.getenv("XDG_DATA_DIRS")
let list = if xdg.length() > 0 { xdg } else { "/usr/local/share:/usr/share" }
let parts : Array[String] = list.split(":").map(x => x.to_string()).collect()
// XDG_DATA_DIRS is highest priority first; we want lowest first.
for i = parts.length() - 1; i >= 0; i = i - 1 {
if parts[i].length() > 0 {
dirs.push(parts[i] + "/applications")
}
}
dirs.push("/var/lib/flatpak/exports/share/applications")
dirs.push(home + "/.local/share/flatpak/exports/share/applications")
dirs.push(data_home + "/applications")
dirs
}
///| Drop Exec field codes (%f %U ...), keep a literal %%.
fn clean_exec(exec : String) -> String {
let sb = StringBuilder::new()
let chars = exec.to_array()
let mut i = 0
while i < chars.length() {
if chars[i] == '%' && i + 1 < chars.length() {
if chars[i + 1] == '%' {
sb.write_char('%')
}
i = i + 2
} else {
sb.write_char(chars[i])
i = i + 1
}
}
sb.to_string().trim(chars=" ").to_string()
}
///| Parse one .desktop file; None when it should not be listed.
fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp? {
let mut in_entry = false
let mut name = ""
let mut generic = ""
let mut comment = ""
let mut exec = ""
let mut cats = ""
let mut hidden = false
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("#") {
continue
}
if line.has_prefix("[") {
in_entry = line == "[Desktop Entry]"
continue
}
if !in_entry {
continue
}
match line.find("=") {
None => ()
Some(eq) => {
let key = line[:eq].trim(chars=" ").to_string()
let val = line[eq + 1:].trim(chars=" ").to_string()
match key {
"Name" => name = val
"GenericName" => generic = val
"Comment" => comment = val
"Exec" => exec = val
"Categories" => cats = val
"Type" => kind = val
"NoDisplay" | "Hidden" => if val == "true" { hidden = true }
"Terminal" => terminal = val == "true"
"StartupWMClass" => wm_class = val
"Icon" => icon = val
_ => ()
}
}
}
}
if hidden || kind != "Application" || name.length() == 0 || exec.length() == 0 {
return None
}
if terminal && !have_terminal {
return None
}
let mut command = clean_exec(exec)
if terminal {
command = "foot -e " + command
}
let category = match cats.split(";").next() {
Some(c) if c.length() > 0 => c.to_string()
_ => "Other"
}
let comment = if comment.length() > 0 { comment } else { generic }
Some({ id, name, comment, category, command, wm_class, icon })
}
///| Scan every applications dir; later dirs override earlier ones, result sorted by name.
fn scan_desktop_apps() -> Array[DesktopApp] {
let have_terminal = @sys.has_exe("foot")
let by_id : Map[String, DesktopApp] = {}
let removed : Map[String, Bool] = {}
for dir in app_dirs() {
for file in @sys.list_desktop_files(dir) {
let bytes = @sys.read_file_bytes(dir + "/" + file)
let id = file[:file.length() - 8].to_string()
match parse_desktop(id, @utf8.decode_lossy(bytes), have_terminal) {
Some(app) => {
by_id.set(id, app)
removed.remove(id)
}
None => {
by_id.remove(id)
}
}
}
}
let apps : Array[DesktopApp] = []
for _, a in by_id {
apps.push(a)
}
apps.sort_by(fn(a, b) {
let x = a.name.to_lower()
let y = b.name.to_lower()
if x < y { -1 } else if x > y { 1 } else { 0 }
})
apps
}
///|
test "parse_desktop reads a normal entry and strips field codes" {
let t = "[Desktop Entry]\nType=Application\nName=Firefox\nName[de]=Feuerfuchs\nComment=Browse\nExec=firefox %u\nCategories=Network;WebBrowser;\n[Desktop Action x]\nName=Nope\n"
match parse_desktop("firefox", t, true) {
Some(a) => {
assert_eq(a.name, "Firefox")
assert_eq(a.command, "firefox")
assert_eq(a.category, "Network")
assert_eq(a.comment, "Browse")
}
None => fail("expected entry")
}
}
///|
test "parse_desktop keeps StartupWMClass" {
let t = "[Desktop Entry]\nType=Application\nName=Code\nExec=code %F\nStartupWMClass=Code\n"
match parse_desktop("visual-studio-code", t, true) {
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")
}
}
///|
test "parse_desktop hides NoDisplay entries" {
let t = "[Desktop Entry]\nType=Application\nName=X\nExec=x\nNoDisplay=true\n"
assert_true(parse_desktop("x", t, true) is None)
}
///| The desktop file id that launches the app whose windows carry `app_id`: the file of that
/// name, else one whose StartupWMClass is it, else a case-insensitive match on either, else one
/// whose last dotted part matches (a window "nautilus" for org.gnome.Nautilus.desktop).
fn resolve_app_id(
app_id : String,
exec : Map[String, String],
wm_classes : Map[String, String],
) -> String? {
if app_id.length() == 0 {
return None
}
if exec.contains(app_id) {
return Some(app_id)
}
let low = app_id.to_lower()
match wm_classes.get(low) {
Some(id) => return Some(id)
None => ()
}
for id, _ in exec {
if id.to_lower() == low {
return Some(id)
}
}
for id, _ in exec {
let parts : Array[String] = id.split(".").map(x => x.to_string()).collect()
if parts.length() > 1 && parts[parts.length() - 1].to_lower() == low {
return Some(id)
}
}
None
}
///|
test "resolve_app_id finds the desktop file for a window's app id" {
let exec : Map[String, String] = {
"foot": "foot",
"org.gnome.Nautilus": "nautilus --new-window",
"visual-studio-code": "code",
}
let wm : Map[String, String] = { "code": "visual-studio-code" }
assert_eq(resolve_app_id("foot", exec, wm), Some("foot"))
assert_eq(resolve_app_id("Code", exec, wm), Some("visual-studio-code"))
assert_eq(resolve_app_id("org.gnome.nautilus", exec, wm), Some("org.gnome.Nautilus"))
assert_eq(resolve_app_id("nautilus", exec, wm), Some("org.gnome.Nautilus"))
assert_eq(resolve_app_id("unknown", exec, wm), None)
assert_eq(resolve_app_id("", exec, wm), None)
}