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 = {
"message-circle":
'',
eye:
'',
"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:
'',
bird:
'',
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:
'',
// iOS notification categories (with bell, message-circle and calendar).
"phone-incoming":
'',
"phone-missed":
'',
voicemail:
'',
mail:
'',
newspaper:
'',
"heart-pulse":
'',
briefcase:
'',
"map-pin":
'',
tv:
'',
"cloud-sun":
'',
"flask-conical":
'',
wrench:
'',
sparkles:
'',
settings:
'',
mic:
'',
languages:
'',
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:
'',
// The T3 Code mark: its "T3" letters (verbatim from T3 Code's
// assets/prod/logo.svg) inside a rounded frame, filled; the frame's inner
// contour winds opposite its outer one so the ring stays hollow.
t3code:
'',
nightscout:
'',
} as const;
export type IconName = keyof typeof ICON_SVGS;
/** Idle prompt, or the visible/hidden phases of an activity cursor. */
export type IconActivity = "idle" | "on" | "off";
// Prompt and session-marker elements replaced by renderIconWithGlyph.
const TERMINAL_PROMPT = '';
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 marker for unsupported characters. Active terminal icons
* replace the prompt with a blinking cursor, leaving the marker in place.
*/
export function renderIconWithGlyph(name: IconName, glyph: string, size: number, activity: IconActivity = "idle"): GrayImage | null {
if (name !== "terminal") return renderIcon(name, size);
const shape = TERMINAL_GLYPH_SHAPES[glyph];
if (!shape && activity === "idle") return renderIcon(name, size);
let svg: string = ICON_SVGS.terminal;
if (shape) svg = svg.replace(TERMINAL_UNDERSCORE, shape);
if (activity !== "idle") {
// IconRenderer strokes all elements at width 2; the narrow rectangle
// therefore becomes a solid cursor without needing per-element fills.
svg = svg.replace(TERMINAL_PROMPT, activity === "on"
? ''
: "");
}
return renderSvgCached(`${name}[${shape ? glyph : ""}]:${activity}`, svg, 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;
}