///| 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 and the dock's. 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() } } } // The dock's apps, running or not. for id in self.dock_app_ids() { if !seen.contains(id) { seen.set(id, true) let h = self.app_icon(id) if h != 0U { t.str(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"], []), []) }