diff --git a/components/launcher/gen/interface/chandra/desktop/plasmoid/moon.pkg b/components/launcher/gen/interface/chandra/desktop/plasmoid/moon.pkg index 6ffea35..f58ebe2 100644 --- a/components/launcher/gen/interface/chandra/desktop/plasmoid/moon.pkg +++ b/components/launcher/gen/interface/chandra/desktop/plasmoid/moon.pkg @@ -3,6 +3,7 @@ import { "chandra/desktop/interface/chandra/desktop/theme" @theme, "chandra/desktop/interface/chandra/desktop/app-catalog" @app_catalog, "chandra/desktop/interface/chandra/desktop/app-launch" @app_launch, + "chandra/desktop/interface/chandra/desktop/icons" @icons, "chandra/desktop/interface/chandra/desktop/session" @session, "chandra/desktop/interface/chandra/desktop/power" @power, "chandra/desktop/lib/launcher" @logic, diff --git a/components/launcher/gen/interface/chandra/desktop/plasmoid/stub.mbt b/components/launcher/gen/interface/chandra/desktop/plasmoid/stub.mbt index 53fe182..4a82985 100644 --- a/components/launcher/gen/interface/chandra/desktop/plasmoid/stub.mbt +++ b/components/launcher/gen/interface/chandra/desktop/plasmoid/stub.mbt @@ -1,6 +1,7 @@ ///| Implementation of `chandra:desktop/plasmoid` for the launcher. /// The state machine lives in lib/launcher; this adapts host capabilities: the app catalog and -/// launch, and for the session row `session` (reload, log out) and `power` (suspend, restart, +/// launch, each app's .desktop icon (`icons`, published by the shell for the rows shown), and for +/// the session row `session` (reload, log out) and `power` (suspend, restart, /// power off, the same calls as the Settings Power tab). ///| @@ -30,7 +31,7 @@ pub fn popup() -> PopupSurface? { let apps = @app_catalog.list_apps() Some({ size: @logic.popup_size(apps, state), - ops: @logic.render_popup(apps, @theme.get_tokens(), state), + ops: @logic.render_popup(apps, @theme.get_tokens(), state, icon=id => @icons.app_icon(id)), }) } diff --git a/compositor/host/launcher_icons_test.mbt b/compositor/host/launcher_icons_test.mbt new file mode 100644 index 0000000..1310da3 --- /dev/null +++ b/compositor/host/launcher_icons_test.mbt @@ -0,0 +1,112 @@ +///| App icons in the Applications menu and on the Icon Desktop: the shell publishes the .desktop +/// icon of every app they show (compositor/shell/icons.mbt `icon_app_ids`), and both draw it. +/// Needs the modules built: scripts/build-modules.sh. + +///| A 48x48 PNG of one colour at `path`. +fn write_icon_png(path : String, r : Int, g : Int, b : Int) -> Unit { + let p = @renderer.Pixmap::new(48, 48) + for i = 0; i < 48 * 48; i = i + 1 { + p.data[i * 4] = r.to_byte() + p.data[i * 4 + 1] = g.to_byte() + p.data[i * 4 + 2] = b.to_byte() + p.data[i * 4 + 3] = b'\xff' + } + ignore(@sys.write_file_bytes(path, @renderer.encode_png(p))) +} + +///| The (r, g, b) of the frame at (x, y). +fn frame_rgb(f : @renderer.Pixmap, x : Int, y : Int) -> (Int, Int, Int) { + let i = (y * f.width + x) * 4 + (f.data[i].to_int(), f.data[i + 1].to_int(), f.data[i + 2].to_int()) +} + +///| A compositor with the six fake apps of `desk_comp`, where app0's .desktop file says +/// Icon=. +fn icon_comp(tag : String) -> @host.ChandraCompositor { + let comp = desk_comp(tag) + let png = "/tmp/chandra-icon-" + tag + "-" + @sys.getpid().to_string() + ".png" + write_icon_png(png, 230, 20, 20) + comp.shell.exec.set("app0", "app0") + comp.shell.icon_names.set("app0", png) + // The first frame already looked app0 up (the dock pins it): forget that. + comp.shell.icon_handles.remove("app0") + comp +} + +///| +test "the Icon Desktop gets every catalog app's icon, drawn from an absolute Icon= path" { + let comp = icon_comp("idesk") + let before = comp.shell.icon_app_ids() + assert_false(before.contains("app5")) + assert_true(comp.shell.set_desktop("icon-desktop")) + ignore(comp.compose_frame()) + let ids = comp.shell.icon_app_ids() + for i = 0; i < 6; i = i + 1 { + assert_true(ids.contains("app" + i.to_string())) + } + assert_true(comp.shell.window_icon("app0") != 0U) + // app0's icon sits in the first cell (see desktop_test): its middle is the PNG's red. + let f = comp.compose_frame() + assert_eq(frame_rgb(f, 68, 50), (230, 20, 20)) + ignore(@sys.write_file_bytes("_build/desktop_icons_real.png", @renderer.encode_png(f))) + comp.shell.shutdown() +} + +///| +test "the open Applications menu gets its rows' icons and draws them" { + let comp = icon_comp("lmenu") + assert_false(comp.shell.icon_app_ids().contains("app5")) + comp.shell.toggle_launcher() + for _ in 0..<10 { + @sys.sleep_ms(30) // the popup slides in + ignore(comp.compose_frame()) + } + let pr = comp.shell.slot_popup_rect(comp.shell.slot_of("launcher")) + assert_true(pr.w > 0) + let ids = comp.shell.icon_app_ids() + for i = 0; i < 6; i = i + 1 { + assert_true(ids.contains("app" + i.to_string())) + } + let f = comp.compose_frame() + // Row 0 is app0 ("Files"): its icon is centred 30 px in, half a row (46 px) below the header. + assert_eq(frame_rgb(f, pr.x + 30, pr.y + 52 + 22), (230, 20, 20)) + ignore(@sys.write_file_bytes("_build/launcher_icons.png", @renderer.encode_png(f))) + // Closed again: the menu's apps drop out of the table. + comp.shell.toggle_launcher() + ignore(comp.compose_frame()) + assert_false(comp.shell.icon_app_ids().contains("app5")) + comp.shell.shutdown() +} + +///| For a look: the menu and the Icon Desktop with theme icons (whatever the machine's hicolor +/// theme has for these names; letter tiles or the generic icon where it has nothing). Writes +/// _build/launcher_icons_themed.png and _build/desktop_icons_themed.png. +test "screenshots: Applications menu and Icon Desktop with theme icons" { + let comp = desk_comp("ishot") + let apps = [ + ("org.libreoffice.Writer", "Writer", "Word processor", "libreoffice-writer"), + ("org.libreoffice.Calc", "Calc", "Spreadsheets", "libreoffice-calc"), + ("org.libreoffice.Impress", "Impress", "Presentations", "libreoffice-impress"), + ("org.libreoffice.Draw", "Draw", "Drawings and diagrams", "libreoffice-draw"), + ("xterm", "XTerm", "Terminal emulator", "xterm-color"), + ("gvim", "GVim", "Text editor", "gvim"), + ] + comp.shell.apps.clear() + for a in apps { + comp.shell.apps.push({ id: a.0, name: a.1, comment: a.2, category: "", glyph: "" }) + comp.shell.exec.set(a.0, a.0) + comp.shell.icon_names.set(a.0, a.3) + comp.shell.icon_handles.remove(a.0) + } + comp.shell.apps_dirty = true + assert_true(comp.shell.set_desktop("icon-desktop")) + ignore(comp.compose_frame()) + ignore(@sys.write_file_bytes("_build/desktop_icons_themed.png", @renderer.encode_png(comp.compose_frame()))) + comp.shell.toggle_launcher() + for _ in 0..<10 { + @sys.sleep_ms(30) + ignore(comp.compose_frame()) + } + ignore(@sys.write_file_bytes("_build/launcher_icons_themed.png", @renderer.encode_png(comp.compose_frame()))) + comp.shell.shutdown() +} diff --git a/compositor/shell/icons.mbt b/compositor/shell/icons.mbt index 40fb114..1be26be 100644 --- a/compositor/shell/icons.mbt +++ b/compositor/shell/icons.mbt @@ -493,37 +493,70 @@ 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() +///| The app ids whose icons the modules need now: the windows' apps, the dock's apps (running or +/// not), the rows the Applications menu shows while it is open (only those, so opening it looks +/// up a handful of icons, not the whole catalog), and every app the desktop module can show when +/// it imports `icons` (Icon Desktop). Each id once, in that order. +pub fn DesktopShell::icon_app_ids(self : DesktopShell) -> Array[String] { + let out : Array[String] = [] 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() - } + let add = fn(id : String) { + if id.length() > 0 && !seen.contains(id) { + seen.set(id, true) + out.push(id) } } - // The dock's apps, running or not. + for w in self.win_list { + add(w.app_id) + } 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() + add(id) + } + for id in self.launcher_icon_ids() { + add(id) + } + match self.desk_mod { + Some(d) if d.has_cap("icons") => + for a in self.apps { + add(a.id) } + _ => () + } + out +} + +///| The apps the open Applications menu lists (as the shell follows its search), else none. +fn DesktopShell::launcher_icon_ids(self : DesktopShell) -> Array[String] { + let st = self.launcher_state + if !st.open { + return [] + } + @launcher.shown_rows(self.apps, st).map(a => a.id) +} + +///| Publish the icons table (app id -> image handle) for `icon_app_ids`. Lookups are cached per id +/// (`lookup_icon`), and an unchanged table is not sent again. +fn DesktopShell::publish_icons(self : DesktopShell) -> Unit { + let rows : Array[(String, UInt)] = [] + for id in self.icon_app_ids() { + let h = self.app_icon(id) + if h != 0U { + rows.push((id, h)) } } + if rows == self.icons_published { + return + } + self.icons_published = rows + let t = @wasm.Table::new() + for r in rows { + t.str(r.0) + t.int(r.1.reinterpret_as_int()) + t.end_row() + } @wasm.publish(Icons, t) } - ///| The image handle of theme icon `name` (say "drive-harddisk"; 0: none), cached. pub fn DesktopShell::theme_icon(self : DesktopShell, name : String) -> UInt { if name == "" { diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 59a9f08..cdea7cf 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -242,6 +242,9 @@ pub(all) struct DesktopShell { /// (0: no icon), for the `icons` capability. icon_names : Map[String, String] icon_handles : Map[String, UInt] + /// The icons table as last published (app id, handle), so an unchanged table isn't sent again + /// every frame. + mut icons_published : Array[(String, UInt)] /// Icon base dirs and theme chain (icons.mbt), read on first use. mut icon_search : IconSearch? /// Wayland socket handed to launched apps as WAYLAND_DISPLAY. @@ -432,6 +435,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { wm_classes, icon_names, icon_handles: {}, + icons_published: [], icon_search: None, display, widgets: [], diff --git a/config/desktop.binary-clock.json b/config/desktop.binary-clock.json index 1d4e642..a5fd813 100644 --- a/config/desktop.binary-clock.json +++ b/config/desktop.binary-clock.json @@ -3,7 +3,7 @@ "theme": "moon-night", "shell": {"component": "shell", "grants": ["theme"]}, "panel": [ - {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "session", "power"]}, + {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "icons", "session", "power"]}, {"id": "clock", "component": "clock-binary", "align": "end", "grants": ["theme", "clock"]} ] } diff --git a/config/desktop.json b/config/desktop.json index ce2f546..9afe900 100644 --- a/config/desktop.json +++ b/config/desktop.json @@ -3,7 +3,7 @@ "theme": "moon-night", "shell": {"component": "shell", "grants": ["theme"]}, "panel": [ - {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "session", "power"]}, + {"id": "launcher", "component": "launcher", "align": "start", "grants": ["theme", "app-catalog", "app-launch", "icons", "session", "power"]}, {"id": "clock", "component": "clock-digital", "align": "end", "grants": ["theme", "clock"]} ] } diff --git a/host/src/platform.mjs b/host/src/platform.mjs index 7d81fa4..fd0148a 100644 --- a/host/src/platform.mjs +++ b/host/src/platform.mjs @@ -35,6 +35,8 @@ export function createPlatform({ now = () => new Date(), apps, tokens }) { return { tag: 'ok', val: undefined }; }, }), + // No icon themes here: every app gets 0, so widgets draw their letter tiles. + icons: () => ({ appIcon: () => 0 }), session: () => ({ logout() { p.sessionRequests.push('logout'); }, reload() { p.sessionRequests.push('reload'); }, @@ -52,4 +54,4 @@ export function createPlatform({ now = () => new Date(), apps, tokens }) { return p; } -export const CAPABILITIES = ['theme', 'clock', 'app-catalog', 'app-launch', 'session', 'power']; +export const CAPABILITIES = ['theme', 'clock', 'app-catalog', 'app-launch', 'icons', 'session', 'power']; diff --git a/lib/launcher/launcher.mbt b/lib/launcher/launcher.mbt index 84f0999..ade8c87 100644 --- a/lib/launcher/launcher.mbt +++ b/lib/launcher/launcher.mbt @@ -123,6 +123,21 @@ fn visible_rows(n : Int) -> Int { if n > max_rows { max_rows } else { n } } +///| The apps the popup lists now: the filtered catalog, capped at the rows that fit. The shell +/// publishes icons for exactly these while the menu is open. +pub fn shown_rows(apps : Array[@types.AppEntry], st : State) -> Array[@types.AppEntry] { + let shown = filter(apps, st.query) + let n = visible_rows(shown.length()) + if n >= shown.length() { + shown + } else { + shown[:n].to_array() + } +} + +///| Side of an app's icon in its row. +let icon_px : Int = 32 + ///| pub fn popup_size(apps : Array[@types.AppEntry], st : State) -> @types.Size { let rows = visible_rows(filter(apps, st.query).length()) @@ -197,10 +212,13 @@ pub fn render_button( } ///| +///| Draw the open popup. `icon` gives an app id's .desktop icon as an image handle (0: none, the +/// app's letter tile is drawn instead, the same one the dock uses). pub fn render_popup( apps : Array[@types.AppEntry], t : @theme.Tokens, st : State, + icon~ : (String) -> UInt = _ => 0U, ) -> Array[@types.DrawOp] { let size = popup_size(apps, st) let w = size.w.reinterpret_as_int() @@ -235,8 +253,15 @@ pub fn render_popup( } let fg = if i == st.hover { p.accent_foreground } else { p.foreground } let sub = if i == st.hover { p.accent_foreground } else { p.foreground_muted } - ops.push(@ui.circle(30, y + row_h / 2 - 1, 15, p.surface_hover)) - ops.push(@ui.text(30, y + row_h / 2 - 1, a.glyph, fs, fg, align=CENTER, bold=true)) + let cx = 30 + let cy = y + row_h / 2 - 1 + let h = icon(a.id) + if h != 0U { + ops.push(@types.DrawOp::Image({ rect: @ui.rect(cx - icon_px / 2, cy - icon_px / 2, icon_px, icon_px), image: h })) + } else { + let glyph = if a.glyph.length() > 0 { a.glyph } else { @dock.app_glyph(a.id, a.name) } + ops.append(@dock.app_icon(cx, cy, icon_px - 2, a.id, glyph)) + } ops.push(@ui.text(56, y + 14, a.name, fs, fg, bold=true)) ops.push(@ui.text(56, y + 31, a.comment, fs - 3, sub)) } @@ -539,3 +564,65 @@ test "session row: a click on an app disarms, and app rows don't reach the row" ignore(handle_event(apps, st, Popup, PointerDown({ x: 20, y: 60 }), _ => Ok(()))) assert_eq(st.armed, -1) } + +///| The image ops of a drawing, with the handles they draw. +fn images(ops : Array[@types.DrawOp]) -> Array[UInt] { + ops.filter_map(op => match op { Image(img) => Some(img.image); _ => None }) +} + +///| +test "rows draw the app's .desktop icon, or the letter tile when it has none" { + let apps = sample_apps() + let st = State::new() + st.open = true + let asked : Array[String] = [] + let icon = fn(id : String) { + asked.push(id) + if id == "b" { 0U } else if id == "a" { 7U } else { 9U } + } + let ops = render_popup(apps, test_tokens(), st, icon~) + assert_eq(asked, ["a", "b", "c"]) + assert_eq(images(ops), [7U, 9U]) + // Files has no icon: its tile shows its glyph. + assert_true(ops.iter().any(op => match op { Text(t) => t.text == "F"; _ => false })) + // With a search only the matching rows ask for icons. + st.query = "term" + asked.clear() + assert_eq(images(render_popup(apps, test_tokens(), st, icon~)), [7U]) + assert_eq(asked, ["a"]) +} + +///| +test "shown_rows is the filtered list, capped at the rows that fit" { + let apps : Array[@types.AppEntry] = [] + for i = 0; i < 12; i = i + 1 { + apps.push({ id: "app" + i.to_string(), name: "App " + i.to_string(), comment: "", category: "", glyph: "" }) + } + let st = State::new() + assert_eq(shown_rows(apps, st).length(), max_rows) + st.query = "App 1" + assert_eq(shown_rows(apps, st).map(a => a.id), ["app1", "app10", "app11"]) +} + +///| +fn test_tokens() -> @theme.Tokens { + { + name: "t", + palette: { + desktop_top: @ui.color(0, 0, 0, 255), + desktop_bottom: @ui.color(0, 0, 0, 255), + surface: @ui.color(30, 30, 30, 255), + surface_hover: @ui.color(40, 40, 40, 255), + surface_popup: @ui.color(30, 30, 30, 255), + foreground: @ui.color(255, 255, 255, 255), + foreground_muted: @ui.color(150, 150, 150, 255), + accent: @ui.color(60, 120, 220, 255), + accent_foreground: @ui.color(255, 255, 255, 255), + border: @ui.color(60, 60, 60, 255), + }, + font_size: 14U, + radius: 6U, + spacing: 6U, + panel_height: 44U, + } +} diff --git a/lib/launcher/moon.pkg b/lib/launcher/moon.pkg index 2154116..b015502 100644 --- a/lib/launcher/moon.pkg +++ b/lib/launcher/moon.pkg @@ -2,4 +2,5 @@ import { "chandra/desktop/interface/chandra/desktop/types" @types, "chandra/desktop/interface/chandra/desktop/theme" @theme, "chandra/desktop/lib/ui" @ui, + "chandra/desktop/lib/dock" @dock, } diff --git a/lib/launcher/pkg.generated.mbti b/lib/launcher/pkg.generated.mbti index 56e35c5..190af00 100644 --- a/lib/launcher/pkg.generated.mbti +++ b/lib/launcher/pkg.generated.mbti @@ -19,10 +19,12 @@ pub let popup_w : Int32 pub fn render_button(@types.Size, @theme.Tokens, State) -> Array[@types.DrawOp] -pub fn render_popup(Array[@types.AppEntry], @theme.Tokens, State) -> Array[@types.DrawOp] +pub fn render_popup(Array[@types.AppEntry], @theme.Tokens, State, icon? : (String) -> UInt) -> Array[@types.DrawOp] pub fn row_at(Array[@types.AppEntry], State, @types.Point) -> Int32 +pub fn shown_rows(Array[@types.AppEntry], State) -> Array[@types.AppEntry] + // Errors // Types and methods diff --git a/wit/worlds.wit b/wit/worlds.wit index 66c21ef..374fad8 100644 --- a/wit/worlds.wit +++ b/wit/worlds.wit @@ -30,6 +30,7 @@ world launcher-widget { include widget; import app-catalog; import app-launch; + import icons; import session; import power; }