Files
chandra/compositor/shell/icons.mbt
T
Claude 51b914f6cf
CI / test (push) Failing after 19s
Alt+Tab switcher: show a preview of each window
The switcher is now a grid of cards: a scaled-down picture of each window's
last frame over its app icon and title, the selected card lit. Cards wrap
into rows and shrink to fit small screens.

- Previews keep the buffer's aspect ratio and are never enlarged.
- Minimized windows show the last frame they drew, dimmed, marked
  "minimized". A window with no buffer yet shows its app icon (or its
  initial) on a plain tile.
- Cheap: nothing is scaled while the switcher is closed. A preview is made
  when the switcher opens and again only when that window commits a new
  buffer (or its shown size changes), into the same image handle; handles
  of closed windows are reused. The native scaler samples at most 3x3
  points per preview pixel, so big windows cost about the same as small.
- Both hosts: native scales the wl_shm snapshot in the software renderer
  (renderer.shm_thumbnail + set_image); the wasm compositor asks the host
  with the new chandra:host/raster thumbnail(surface, w, h, reuse), which
  scales the captured surface natively, so no pixels cross the boundary.
- Frames the screenshot overlay held back mark previews stale too.

Tests: switcher_test (preview pixels, aspect, no-buffer, stale and
remade into the same handle, minimized), renderer shm_thumbnail/set_image,
host-wasm thumbnail scaling and handle reuse. Pre-existing failures on
main are unchanged (6 panel-module host tests, the screenshot panel
module test, host-wasm translucent_surfaces_blend).

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

152 lines
5.4 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 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"], []), [])
}