import { GrayImage } from "./image";
declare const com: any;
declare const global: any;
/**
* Vector icons for window indicators (and anywhere else). SVGs are rendered
* once to a correctly sized grayscale bitmap by the Java IconRenderer (a
* small SVG subset: path/circle/rect/line/polyline) 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:
'',
// 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:
'',
"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:
'',
"hard-drive":
'',
music:
'',
activity:
'',
bell:
'',
calendar:
'',
"cloud-sun":
'',
"flask-conical":
'',
settings:
'',
mic:
'',
map:
'',
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 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;
if (global.isAndroid) {
try {
const bytes = com.faceclaw.app.IconRenderer.renderSvgGray(svg, Math.round(size), ICON_STROKE_WIDTH);
if (bytes && bytes.length >= size * size) {
icon = new GrayImage(size, size, 0);
for (let i = 0; i < size * size; i++) {
icon.pixels[i] = bytes[i] & 0xff;
}
}
} catch (error) {
console.warn(`renderIcon(${cacheName}) failed: ${error}`);
}
}
cache.set(key, icon);
return icon;
}