Files
chandra/compositor/shell/icons.mbt
T
Claude bb7c9b947f
CI / test (push) Failing after 45s
Settings > Apps: choose the default browser, email, files, terminal, editor, viewers and players
- A sixth Settings tab, Apps (key 6, SettingsTab::Apps; the popup is 390 px wide for it): Web
  browser, Email, File manager, Terminal, Text editor, Image viewer, Video player, Music player,
  PDF and documents, Archive manager. Each row shows the current default with its real icon (the
  icon-theme lookup the task manager uses; a letter tile when there is none). Click a row to list
  the installed apps that can take it, click one to make it the default. A chosen app that has
  been uninstalled since is named in red under its row ("gthumb is not installed any more").
- Installed apps come from the .desktop files in the XDG data dirs (and flatpak's exports), with
  MimeType= and Categories= (desktop_entry_keys, now shared with the launcher's parse_desktop).
  A kind lists the shown apps that handle one of its MIME types; the terminal lists
  TerminalEmulator apps; browsers are only offered as browsers.
- Reading follows the mimeapps.list spec: $XDG_CONFIG_HOME/[chandra-]mimeapps.list, then
  $XDG_CONFIG_DIRS (/etc/xdg), then $XDG_DATA_HOME and $XDG_DATA_DIRS applications/; the first
  installed [Default Applications] entry wins, else [Added Associations], else mimeinfo.cache
  (minus [Removed Associations]), else the first installed app that handles the type. A hidden
  (NoDisplay) default still counts and its name is shown.
- Setting writes [Default Applications] in ~/.config/mimeapps.list for each of the kind's MIME
  types the app handles (always the main one: http/https/text/html for browsers, mailto,
  inode/directory, text/plain, image/png..., video/mp4..., audio/mpeg..., application/pdf...,
  application/zip...), as xdg-mime default / xdg-settings do, without running them. Every other
  section, entry, comment and blank line is kept; the app leaves those types' [Removed
  Associations]; a chandra-mimeapps.list that already names one of those types is updated too.
- The terminal has no MIME type: state.edn :terminal "kitty" (desktop id), also written first in
  ~/.config/xdg-terminals.list (xdg-terminal-exec). Terminal=true apps from the launcher (and
  session restore) now start in it (foot -e, kgx/gnome-terminal/ptyxis --, kitty <cmd>, wezterm
  start --) instead of always foot; with no choice: xdg-terminals.list, then foot, then any
  installed terminal. Terminal apps are listed when any terminal emulator is installed, not only
  foot.
- New `default-apps` capability (categories, apps, set-default, refresh): host tables 11 (sss:
  name current missing) and 12 (issii: category id name icon current), request 19 (category *
  1000 + app, -1 rescans) in the wasmer/wasmtime stub, host-wasm and the MoonBit wasmhost
  (chandra:host/plasmoids default-app(s32)). Nothing is scanned until the tab opens (refresh).
- Tests with a temporary XDG tree: categories and current defaults (user file, system file,
  Added Associations, mimeinfo.cache, missing apps, hidden defaults), writing that keeps other
  entries, desktop-specific files, the terminal list and commands, the Apps tab end to end, and
  the :terminal round trip through state.edn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgywj7RPmFdFhpzGZj
2026-11-25 21:27:30 +00:00

167 lines
5.8 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
}
///| The image handle of desktop app `id`'s icon (Icon= value `icon`), sharing `app_icon`'s cache.
fn DesktopShell::desktop_icon(self : DesktopShell, id : String, icon : String) -> UInt {
match self.icon_handles.get(id) {
Some(h) => return h
None => ()
}
let name = if icon.length() > 0 { icon } else { id }
let h = match find_icon_file(name, @sys.host_raster()) {
Some(path) => @raster.load_image(path)
None => 0U
}
self.icon_handles.set(id, h)
h
}
///| The image handle of the app icon for a window with `app_id` (0: none), for the compositor's
/// own UI (the Alt+Tab switcher).
pub fn DesktopShell::window_icon(self : DesktopShell, app_id : String) -> UInt {
if app_id.length() == 0 { 0U } else { self.app_icon(app_id) }
}
///| 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"], []), [])
}