import { GrayImage } from "./image";
import { rasterizeSvg } from "../native/svg-rasterizer";
/**
* Vector icons for window indicators (and anywhere else). SVGs are rendered
* once to a correctly sized grayscale bitmap by the platform rasterizer
* (Android IconRenderer or iOS SVGKit) and cached here. To add
* an icon, drop its SVG source into ICON_SVGS — Lucide icons
* (https://lucide.dev, stroked, 24px viewBox) work as-is; simple single-color
* Noun Project glyphs also work.
*/
// Stroke width in viewBox units (Lucide's default is 2).
const ICON_STROKE_WIDTH = 2;
// Lucide icons (MIT/ISC licensed). Kept verbatim so they can be diffed
// against upstream if an icon needs updating.
export const ICON_SVGS = {
"layout-grid":
'',
timer:
'',
calculator:
'',
// Not a Lucide icon: an L-tetromino built from four squares, for Blocks.
"l-piece":
'',
bomb:
'',
// Not a Lucide icon: a ball above two angled flippers, for Pinball.
pinball:
'',
spade:
'',
terminal:
'',
"scroll-text":
'',
"file-text":
'',
file:
'',
folder:
'',
// The same Lucide folder shape without fill="none", so the renderer fills
// it: launcher folders use this to read differently from the Files app.
"folder-filled":
'',
image:
'',
film:
'',
type:
'',
"hard-drive":
'',
music:
'',
package:
'',
activity:
'',
bell:
'',
calendar:
'',
"cloud-sun":
'',
"flask-conical":
'',
wrench:
'',
settings:
'',
mic:
'',
map:
'',
compass:
'',
// The Roam Research logo, potrace-traced from the official PNG (no
// fill="none", so the renderer fills it; hole contours wind opposite).
roam:
'',
nightscout:
'',
} as const;
export type IconName = keyof typeof ICON_SVGS;
// The "_" element of the terminal icon, swapped out for a session glyph in
// renderIconWithGlyph.
const TERMINAL_UNDERSCORE = '';
/**
* Session-marker glyphs for per-terminal window icons: the terminal icon's
* "_" replaced by a character, drawn in the same stroked style. Hand-fitted
* to the box x 11.5–16.5, y 8–16 beside the ">" chevron. The zero is slashed
* to keep it distinct from the letter O.
*/
const TERMINAL_GLYPH_SHAPES: Record = {
"0": '',
"1": '',
"2": '',
"3": '',
"4": '',
"5": '',
"6": '',
"7": '',
"8": '',
"9": '',
A: '',
B: '',
C: '',
D: '',
E: '',
F: '',
G: '',
H: '',
I: '',
J: '',
K: '',
L: '',
M: '',
N: '',
O: '',
P: '',
Q: '',
R: '',
S: '',
T: '',
U: '',
V: '',
W: '',
X: '',
Y: '',
Z: '',
};
/** Characters usable as terminal-icon glyphs, in allocation order (1-9 first, 0 as the tenth). */
export const TERMINAL_ICON_GLYPHS = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const cache = new Map();
/** Render an icon SVG to a size×size grayscale bitmap, rendered once and cached. */
export function renderIcon(name: IconName, size: number): GrayImage | null {
return renderSvgCached(name, ICON_SVGS[name], size);
}
/** Render an app-provided SVG using the same small renderer and cache. */
export function renderSvgIcon(cacheName: string, svg: string, size: number): GrayImage | null {
return renderSvgCached(`custom:${cacheName}`, svg, size);
}
/**
* Render an icon with a glyph character substituted in — currently only the
* terminal icon, whose "_" becomes the glyph (">3" instead of ">_"). Falls
* back to the plain icon for other names or unsupported characters.
*/
export function renderIconWithGlyph(name: IconName, glyph: string, size: number): GrayImage | null {
const shape = name === "terminal" ? TERMINAL_GLYPH_SHAPES[glyph] : undefined;
if (!shape) return renderIcon(name, size);
return renderSvgCached(`${name}[${glyph}]`, ICON_SVGS.terminal.replace(TERMINAL_UNDERSCORE, shape), size);
}
function renderSvgCached(cacheName: string, svg: string, size: number): GrayImage | null {
const key = `${cacheName}:${size}`;
const cached = cache.get(key);
if (cached !== undefined) return cached;
let icon: GrayImage | null = null;
try {
icon = rasterizeSvg(svg, Math.round(size), ICON_STROKE_WIDTH);
} catch (error) {
console.warn(`renderIcon(${cacheName}) failed: ${error}`);
}
cache.set(key, icon);
return icon;
}