Refactor: Organize files by app

This commit is contained in:
James Babcock committed 2026-08-03 00:08:52 -07:00
1 parent 1ec6b3ff83
commit 9b3832d7a7
40 files changed
+175 -175

No files matched your search

+666
View File
@@ -0,0 +1,666 @@
/**
* Blocks app worker: a falling-blocks game. One singleton window holds the
* whole game; a gravity interval drives piece descent while the window is
* foreground and the screen is on (losing either auto-pauses).
*
* Controls (in play): scroll moves the piece, click rotates, long-press hard
* drops, double-click pauses. Paused: click resumes, double-click yields
* focus, long-press opens the window menu.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_LONG_PRESS,
GESTURE_SCROLL,
} from "../../ui/gestures";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const smallFont = getDefaultSmallFont();
const COLS = 10;
const ROWS = 20;
const CELL = 12;
const BOARD_X = 60;
const BOARD_Y = 10;
const PANEL_X = 230;
/** Gravity starts here and speeds up with score (see dropIntervalMs). */
const BASE_DROP_MS = 1200;
const MIN_DROP_MS = 200;
/** Every this many points, gravity gets one 12% step faster. */
const SPEED_STEP_SCORE = 300;
/** Points per cleared-line count (index = simultaneous lines), times level. */
const LINE_SCORES = [0, 100, 300, 500, 800];
const HARD_DROP_POINTS_PER_ROW = 2;
const LINES_PER_LEVEL = 10;
/**
* The seven tetrominoes as cells in an N×N grid (spawn orientation), plus a
* per-piece gray so they stay distinguishable after 4bpp quantization.
*/
const PIECE_DEFS: Array<{ size: number; cells: Array<[number, number]>; shade: number }> = [
{ size: 4, cells: [[0, 1], [1, 1], [2, 1], [3, 1]], shade: 250 }, // I
{ size: 2, cells: [[0, 0], [1, 0], [0, 1], [1, 1]], shade: 225 }, // O
{ size: 3, cells: [[1, 0], [0, 1], [1, 1], [2, 1]], shade: 200 }, // T
{ size: 3, cells: [[1, 0], [2, 0], [0, 1], [1, 1]], shade: 175 }, // S
{ size: 3, cells: [[0, 0], [1, 0], [1, 1], [2, 1]], shade: 150 }, // Z
{ size: 3, cells: [[0, 0], [0, 1], [1, 1], [2, 1]], shade: 125 }, // J
{ size: 3, cells: [[2, 0], [0, 1], [1, 1], [2, 1]], shade: 100 }, // L
];
/** All four rotations of each piece, precomputed by grid rotation. */
const PIECE_ROTATIONS: Array<Array<Array<[number, number]>>> = PIECE_DEFS.map((def) => {
const rotations: Array<Array<[number, number]>> = [def.cells];
for (let r = 1; r < 4; r++) {
rotations.push(
rotations[r - 1]!.map(([x, y]) => [def.size - 1 - y, x] as [number, number]),
);
}
return rotations;
});
/** Horizontal wall-kick offsets tried in order when a rotation collides. */
const KICK_OFFSETS = [0, -1, 1, -2, 2];
/**
* Buzzer effects, each a single sequencer message (movement and rotation stay
* silent: they're too frequent for the BLE budget and would grate anyway).
* Frequencies are equal-tempered note pitches; duty tuned soft for the piezo.
*/
const SFX_LOCK: Step[] = [{ freq: 330, duty: 40, ms: 20 }];
const SFX_HARD_DROP: Step[] = [
{ freq: 1200, duty: 40, ms: 12 },
{ freq: 700, duty: 45, ms: 14 },
{ freq: 300, duty: 55, ms: 30 },
];
/** Rising arpeggio, one note per cleared line (index = line count). */
const SFX_LINE_CLEAR: Step[][] = [
[],
[{ freq: 1047, ms: 60 }, { freq: 1319, ms: 110 }],
[{ freq: 1047, ms: 55 }, { freq: 1319, ms: 55 }, { freq: 1568, ms: 130 }],
[{ freq: 1047, ms: 50 }, { freq: 1319, ms: 50 }, { freq: 1568, ms: 50 }, { freq: 2093, ms: 150 }],
[
{ freq: 1568, ms: 45 },
{ freq: 2093, ms: 45 },
{ freq: 2637, ms: 45 },
{ freq: 3136, ms: 60 },
{ freq: 1, duty: 0, ms: 30 },
{ freq: 3136, ms: 140 },
],
];
const SFX_LEVEL_UP: Step[] = [
{ freq: 784, ms: 45 },
{ freq: 988, ms: 45 },
{ freq: 1175, ms: 45 },
{ freq: 1568, ms: 60 },
{ freq: 1, duty: 0, ms: 25 },
{ freq: 2093, ms: 160 },
];
const SFX_GAME_OVER: Step[] = [
{ freq: 494, duty: 45, ms: 150 },
{ freq: 466, duty: 45, ms: 150 },
{ freq: 440, duty: 45, ms: 150 },
{ freq: 415, duty: 45, ms: 150 },
{ freq: 392, duty: 45, ms: 380 },
];
const SFX_PAUSE: Step[] = [
{ freq: 1319, duty: 40, ms: 60 },
{ freq: 880, duty: 40, ms: 110 },
];
/** Also the new-game and sound-toggled-on confirmation. */
const SFX_RESUME: Step[] = [
{ freq: 880, duty: 40, ms: 60 },
{ freq: 1319, duty: 40, ms: 110 },
];
type GamePhase = "playing" | "paused" | "game-over";
type BlocksWindow = {
windowId: string;
surfaceId: string;
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
phase: GamePhase;
/** Locked cells; 0 = empty, otherwise the piece's shade byte. */
board: Uint8Array;
pieceIndex: number;
pieceRotation: number;
pieceX: number;
pieceY: number;
nextPieceIndex: number;
/** 7-bag randomizer: refilled and shuffled when drained. */
bag: number[];
score: number;
lines: number;
tickTimer: ReturnType<typeof setInterval> | null;
tickIntervalMs: number;
soundOn: boolean;
lastSubmittedFingerprint: string;
};
const windows = new Map<string, BlocksWindow>();
let screenOn = true;
function post(message: WorkerAppReply): void {
global.postMessage(message);
}
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "open-window": {
const window: BlocksWindow = {
windowId: message.windowId,
surfaceId: message.surfaceId,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
phase: "playing",
board: new Uint8Array(COLS * ROWS),
pieceIndex: 0,
pieceRotation: 0,
pieceX: 0,
pieceY: 0,
nextPieceIndex: 0,
bag: [],
score: 0,
lines: 0,
tickTimer: null,
tickIntervalMs: BASE_DROP_MS,
soundOn: true,
lastSubmittedFingerprint: "",
};
windows.set(message.windowId, window);
resetGame(window);
break;
}
case "close-window": {
const window = windows.get(message.windowId);
if (window?.tickTimer) clearInterval(window.tickTimer);
windows.delete(message.windowId);
break;
}
case "input": {
const window = windows.get(message.windowId);
if (!window) {
frameTimings.finishFrame(message.frameId, "discarded: unknown blocks window");
break;
}
window.focused = message.focused;
inferForeground(window, message.focused);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
case "render": {
const window = windows.get(message.windowId);
if (!window) break;
window.focused = message.focused;
inferForeground(window, message.focused);
renderAndSubmit(window, 0);
break;
}
case "foreground": {
const window = windows.get(message.windowId);
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
// The player can't see the board while backgrounded, so don't let
// gravity keep running.
if (!window.foreground && window.phase === "playing") window.phase = "paused";
updateTickTimer(window);
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "screen":
screenOn = message.on;
for (const window of windows.values()) {
if (!screenOn && window.phase === "playing") window.phase = "paused";
updateTickTimer(window);
}
break;
}
};
/**
* Input and render messages only ever target the shell's foreground window,
* so a focused message proves this window is foreground. This backstops the
* "foreground" message itself, which can be lost when it is posted while a
* freshly spawned worker is still evaluating its bundle (seen on the very
* first launch: gravity never started because foreground stayed false).
*/
function inferForeground(window: BlocksWindow, focused: boolean): void {
if (!focused || window.foreground) return;
window.foreground = true;
updateTickTimer(window);
}
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread
* (same path as frame submission). Effects never exceed one message, so no
* phrase pacing is needed.
*/
function playSfx(window: BlocksWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
} catch (error) {
console.warn(`blocks sfx failed: ${error}`);
}
}
/** The window's long-press menu (sound toggle + default entries). */
function openWindowMenu(window: BlocksWindow): void {
windowMenu(window).open([
{
label: window.soundOn ? "Sound: on" : "Sound: off",
onSelect: (ctx) => {
ctx.stack.pop();
window.soundOn = !window.soundOn;
if (window.soundOn) playSfx(window, SFX_RESUME);
},
},
...defaultWindowMenuItems(window.windowId, post),
]);
}
function windowMenu(window: BlocksWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function handleInput(window: BlocksWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`blocks menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (window.phase === "playing") {
handlePlayingInput(window, event, frameId);
} else {
handleIdleInput(window, event, frameId);
}
}
function handlePlayingInput(window: BlocksWindow, event: DashboardInputEvent, frameId: number): void {
switch (event.type) {
case "scroll-up":
tryMove(window, -1, 0);
break;
case "scroll-down":
tryMove(window, 1, 0);
break;
case "click":
tryRotate(window);
break;
case "long-press":
hardDrop(window);
break;
case "double-click":
window.phase = "paused";
updateTickTimer(window);
playSfx(window, SFX_PAUSE);
break;
default:
frameTimings.finishFrame(frameId, "discarded: blocks ignored input");
return;
}
renderAndSubmit(window, frameId);
}
/** Input while paused or game over. */
function handleIdleInput(window: BlocksWindow, event: DashboardInputEvent, frameId: number): void {
switch (event.type) {
case "click":
if (window.phase === "game-over") resetGame(window);
window.phase = "playing";
updateTickTimer(window);
playSfx(window, SFX_RESUME);
break;
case "double-click":
frameTimings.finishFrame(frameId, "discarded: blocks yielded focus");
post({ type: "yield-focus", windowId: window.windowId });
return;
case "long-press":
openWindowMenu(window);
break;
default:
frameTimings.finishFrame(frameId, "discarded: blocks ignored input");
return;
}
renderAndSubmit(window, frameId);
}
function resetGame(window: BlocksWindow): void {
window.board.fill(0);
window.score = 0;
window.lines = 0;
window.phase = "playing";
window.bag = [];
window.nextPieceIndex = drawFromBag(window);
spawnPiece(window);
updateTickTimer(window);
}
function drawFromBag(window: BlocksWindow): number {
if (window.bag.length === 0) {
window.bag = PIECE_DEFS.map((_, index) => index);
for (let i = window.bag.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[window.bag[i], window.bag[j]] = [window.bag[j]!, window.bag[i]!];
}
}
return window.bag.pop()!;
}
/** Spawn the queued piece at the top; a blocked spawn ends the game. */
function spawnPiece(window: BlocksWindow): void {
window.pieceIndex = window.nextPieceIndex;
window.nextPieceIndex = drawFromBag(window);
window.pieceRotation = 0;
const size = PIECE_DEFS[window.pieceIndex]!.size;
window.pieceX = Math.floor((COLS - size) / 2);
// Start with the piece's topmost cell in row 0.
window.pieceY = -Math.min(...pieceCells(window).map(([, y]) => y));
if (collides(window, window.pieceX, window.pieceY, window.pieceRotation)) {
window.phase = "game-over";
updateTickTimer(window);
}
}
function pieceCells(window: BlocksWindow): Array<[number, number]> {
return PIECE_ROTATIONS[window.pieceIndex]![window.pieceRotation]!;
}
function collides(window: BlocksWindow, atX: number, atY: number, rotation: number): boolean {
for (const [cx, cy] of PIECE_ROTATIONS[window.pieceIndex]![rotation]!) {
const x = atX + cx;
const y = atY + cy;
if (x < 0 || x >= COLS || y >= ROWS) return true;
if (y >= 0 && window.board[y * COLS + x] !== 0) return true;
}
return false;
}
function tryMove(window: BlocksWindow, dx: number, dy: number): boolean {
if (collides(window, window.pieceX + dx, window.pieceY + dy, window.pieceRotation)) return false;
window.pieceX += dx;
window.pieceY += dy;
return true;
}
function tryRotate(window: BlocksWindow): void {
const rotation = (window.pieceRotation + 1) % 4;
for (const kick of KICK_OFFSETS) {
if (!collides(window, window.pieceX + kick, window.pieceY, rotation)) {
window.pieceX += kick;
window.pieceRotation = rotation;
return;
}
}
}
function hardDrop(window: BlocksWindow): void {
let dropped = 0;
while (tryMove(window, 0, 1)) dropped++;
window.score += dropped * HARD_DROP_POINTS_PER_ROW;
lockPiece(window, true);
}
/** One gravity step: descend, or lock and spawn the next piece. */
function tick(window: BlocksWindow): void {
if (window.phase !== "playing") return;
if (!tryMove(window, 0, 1)) lockPiece(window);
renderAndSubmit(window, 0);
}
function lockPiece(window: BlocksWindow, fromHardDrop = false): void {
const shade = PIECE_DEFS[window.pieceIndex]!.shade;
let lockedAboveTop = false;
for (const [cx, cy] of pieceCells(window)) {
const x = window.pieceX + cx;
const y = window.pieceY + cy;
if (y < 0) {
lockedAboveTop = true;
continue;
}
window.board[y * COLS + x] = shade;
}
if (lockedAboveTop) {
window.phase = "game-over";
updateTickTimer(window);
playSfx(window, SFX_GAME_OVER);
return;
}
const levelBefore = level(window);
const cleared = clearLines(window);
spawnPiece(window);
// Score changes may have crossed a speed step.
updateTickTimer(window);
// One sound per lock, most newsworthy event first.
if (window.phase === "game-over") {
playSfx(window, SFX_GAME_OVER);
} else if (level(window) > levelBefore) {
playSfx(window, SFX_LEVEL_UP);
} else if (cleared > 0) {
playSfx(window, SFX_LINE_CLEAR[cleared] ?? SFX_LINE_CLEAR[4]!);
} else {
playSfx(window, fromHardDrop ? SFX_HARD_DROP : SFX_LOCK);
}
}
function clearLines(window: BlocksWindow): number {
let cleared = 0;
for (let y = ROWS - 1; y >= 0; y--) {
let full = true;
for (let x = 0; x < COLS; x++) {
if (window.board[y * COLS + x] === 0) {
full = false;
break;
}
}
if (!full) continue;
// Shift everything above this row down one and re-check the same row.
window.board.copyWithin(COLS, 0, y * COLS);
window.board.fill(0, 0, COLS);
cleared++;
y++;
}
if (cleared > 0) {
window.score += LINE_SCORES[cleared]! * level(window);
window.lines += cleared;
}
return cleared;
}
function level(window: BlocksWindow): number {
return 1 + Math.floor(window.lines / LINES_PER_LEVEL);
}
function dropIntervalMs(window: BlocksWindow): number {
const steps = Math.floor(window.score / SPEED_STEP_SCORE);
return Math.max(MIN_DROP_MS, Math.round(BASE_DROP_MS * Math.pow(0.88, steps)));
}
/** Keep the gravity interval running exactly when the game is live and visible. */
function updateTickTimer(window: BlocksWindow): void {
const intervalMs = dropIntervalMs(window);
const shouldRun = window.phase === "playing" && window.foreground && screenOn;
if (window.tickTimer !== null && (!shouldRun || intervalMs !== window.tickIntervalMs)) {
clearInterval(window.tickTimer);
window.tickTimer = null;
}
if (shouldRun && window.tickTimer === null) {
window.tickIntervalMs = intervalMs;
window.tickTimer = setInterval(() => tick(window), intervalMs);
}
}
function paint(window: BlocksWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: BlocksWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
image.drawRect(BOARD_X - 2, BOARD_Y - 2, COLS * CELL + 4, ROWS * CELL + 4, 120);
if (window.phase === "paused") {
// Hide the board so pausing can't be used to study the stack.
drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, BOARD_Y + 90, "PAUSED", 230);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 130, `${GESTURE_CLICK} resume`, 150);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 150, `${GESTURE_DOUBLE_CLICK} leave`, 150);
} else {
paintBoard(image, window);
if (window.phase === "game-over") paintGameOver(image, window);
}
paintPanel(image, window);
return image;
}
function paintBoard(image: GrayImage, window: BlocksWindow): void {
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
const shade = window.board[y * COLS + x]!;
if (shade !== 0) drawCell(image, x, y, shade);
}
}
if (window.phase !== "playing") return;
// Ghost outline at the piece's landing position.
let ghostY = window.pieceY;
while (!collides(window, window.pieceX, ghostY + 1, window.pieceRotation)) ghostY++;
const shade = PIECE_DEFS[window.pieceIndex]!.shade;
for (const [cx, cy] of pieceCells(window)) {
const x = window.pieceX + cx;
if (ghostY + cy >= 0 && ghostY > window.pieceY) {
image.drawRect(BOARD_X + x * CELL, BOARD_Y + (ghostY + cy) * CELL, CELL - 1, CELL - 1, 70);
}
if (window.pieceY + cy >= 0) drawCell(image, x, window.pieceY + cy, shade);
}
}
function drawCell(image: GrayImage, x: number, y: number, shade: number): void {
image.fillRect(BOARD_X + x * CELL, BOARD_Y + y * CELL, CELL - 1, CELL - 1, shade);
}
function paintGameOver(image: GrayImage, window: BlocksWindow): void {
const boxY = BOARD_Y + 70;
image.fillRect(BOARD_X, boxY, COLS * CELL, 100, 0);
image.drawRect(BOARD_X + 4, boxY + 4, COLS * CELL - 8, 92, 150);
drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, boxY + 16, "GAME", 245);
drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, boxY + 40, "OVER", 245);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, boxY + 70, `${GESTURE_CLICK} new game`, 150);
}
function paintPanel(image: GrayImage, window: BlocksWindow): void {
image.drawText(smallFont, PANEL_X, BOARD_Y, "Next", 140);
const previewBoxSize = 4 * CELL + 16;
const previewY = BOARD_Y + 22;
image.drawRect(PANEL_X, previewY, previewBoxSize, previewBoxSize, 90);
const def = PIECE_DEFS[window.nextPieceIndex]!;
const cells = PIECE_ROTATIONS[window.nextPieceIndex]![0]!;
const minX = Math.min(...cells.map(([x]) => x));
const maxX = Math.max(...cells.map(([x]) => x));
const minY = Math.min(...cells.map(([, y]) => y));
const maxY = Math.max(...cells.map(([, y]) => y));
const offsetX = PANEL_X + Math.round((previewBoxSize - (maxX - minX + 1) * CELL) / 2);
const offsetY = previewY + Math.round((previewBoxSize - (maxY - minY + 1) * CELL) / 2);
for (const [cx, cy] of cells) {
image.fillRect(offsetX + (cx - minX) * CELL, offsetY + (cy - minY) * CELL, CELL - 1, CELL - 1, def.shade);
}
const statX = PANEL_X + previewBoxSize + 40;
drawStat(image, statX, BOARD_Y + 10, "Score", String(window.score));
drawStat(image, statX, BOARD_Y + 70, "Lines", String(window.lines));
drawStat(image, statX, BOARD_Y + 130, "Level", String(level(window)));
image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} move ${GESTURE_CLICK} rotate`, 115);
image.drawText(smallFont, PANEL_X, 222, `${GESTURE_LONG_PRESS} drop ${GESTURE_DOUBLE_CLICK} pause`, 115);
}
function drawStat(image: GrayImage, x: number, y: number, label: string, value: string): void {
image.drawText(smallFont, x, y, label, 140);
image.drawText(largeFont, x, y + 18, value, 235);
}
function drawCenteredIn(
image: GrayImage,
font: typeof smallFont,
x: number,
width: number,
y: number,
text: string,
value: number,
): void {
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
}
function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: blocks content unchanged");
return;
}
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
0,
0,
image.width,
image.height,
fingerprint,
paintMs,
frameId,
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
frameTimings.finishFrame(frameId, "discarded: blocks render failed");
console.error(`blocks worker render failed: ${error}`);
}
}
+54
View File
@@ -0,0 +1,54 @@
import { ensureCalendarPermission, hasCalendarPermission } from "../../g2/android-permissions";
import { invalidateCalendarCache } from "../../native/calendar";
import { CalendarLayer } from "./calendar";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
export const CALENDAR_WINDOW_ID = "calendar";
export const CALENDAR_SURFACE_ID = "window:calendar";
/**
* The Calendar app: a single screen listing upcoming events from the Android
* Calendar provider. If calendar permission is missing it shows a prompt and
* fires the system permission dialog (on launch and on any tap); once granted
* it re-renders with the event list.
*/
export function createCalendarAppWindow(options: InProcessAppOptions): InProcessWindow {
let requesting = false;
let app: InProcessWindow;
const requestPermission = () => {
if (requesting || hasCalendarPermission()) return;
requesting = true;
void ensureCalendarPermission().then((granted) => {
requesting = false;
if (granted) {
invalidateCalendarCache();
app.requestRender();
}
});
};
app = createInProcessWindow({
appId: "calendar",
windowId: CALENDAR_WINDOW_ID,
title: "Calendar",
iconLetter: "Ca",
icon: "calendar",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new CalendarLayer(requestPermission)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
// Prompt immediately on launch so the user doesn't have to discover the tap.
requestPermission();
return app;
}
+191
View File
@@ -0,0 +1,191 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { readUpcomingEvents, type CalendarEvent } from "../../native/calendar";
import { timeFormatSetting } from "../../ui/dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { hasCalendarPermission } from "../../g2/android-permissions";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
const PAGE_X = 12;
const PAGE_Y = 12;
const LIST_TOP = 38;
const ROW_X = 16;
const LINE_HEIGHT = 14;
const DAY_HEADER_HEIGHT = 16;
const ROW_GAP = 4;
const MAX_EVENTS = 50;
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
type EventRow = {
event: CalendarEvent;
/** Day header text drawn above this row, or null when it shares the prior day. */
dayHeader: string | null;
lines: string[];
height: number;
};
/**
* The Calendar app's single screen. Without calendar permission it shows a
* prompt telling the user to grant access on the phone (the launch path also
* fires the system permission dialog); with permission it lists upcoming
* events grouped by day, scrollable when they overflow the viewport.
*/
export class CalendarLayer implements Layer {
private selectedIndex = 0;
constructor(private readonly requestPermission: () => void) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Calendar", 220);
if (!hasCalendarPermission()) {
this.paintPermissionPrompt(image, font, width, height);
return image;
}
const events = readUpcomingEvents(MAX_EVENTS);
if (!events.length) {
image.drawText(font, 24, 72, "No upcoming events.", 190);
image.drawText(font, 24, height - 36, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const rows = buildEventRows(events);
this.selectedIndex = clamp(this.selectedIndex, 0, rows.length - 1);
image.drawText(font, width - 96, PAGE_Y + 9, `${this.selectedIndex + 1}/${rows.length}`, 150);
const listBottom = height;
const scrollY = scrollForSelected(rows, this.selectedIndex, listBottom - LIST_TOP);
let cursorY = LIST_TOP - scrollY;
for (let index = 0; index < rows.length; index++) {
const row = rows[index]!;
if (cursorY + row.height >= LIST_TOP && cursorY <= listBottom) {
drawEventRow(image, font, row, ROW_X, cursorY, width, index === this.selectedIndex);
}
cursorY += row.height + ROW_GAP;
if (cursorY > listBottom + 80) break;
}
return image;
}
handleInput(event: DashboardInputEvent): void {
if (!hasCalendarPermission()) {
// Any tap on the prompt re-triggers the phone-side permission request.
if (event.type === "click") this.requestPermission();
return;
}
const rows = buildEventRows(readUpcomingEvents(MAX_EVENTS));
if (!rows.length) return;
if (event.type === "scroll-up") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
} else if (event.type === "scroll-down") {
this.selectedIndex = Math.min(rows.length - 1, this.selectedIndex + 1);
}
}
private paintPermissionPrompt(image: GrayImage, font: BdfFont, width: number, height: number): void {
const message = "Grant calendar permission on your phone to see your events on the glasses.";
const lines = wrapText(font, message, width - 48);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 72 + index * LINE_HEIGHT, lines[index]!, 190);
}
image.drawText(font, 24, height - 36, `${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`, 110);
}
}
function buildEventRows(events: CalendarEvent[]): EventRow[] {
const rows: EventRow[] = [];
let previousDayKey = "";
for (const event of events) {
const dayKey = dayKeyOf(event.startMs);
const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(event.startMs);
previousDayKey = dayKey;
const lines: string[] = [];
const timeLabel = event.allDay ? "All day" : formatEventTime(event.startMs);
lines.push(`${timeLabel} ${event.title || "(untitled)"}`);
if (event.location) {
lines.push(event.location);
}
rows.push({
event,
dayHeader,
lines,
height: (dayHeader ? DAY_HEADER_HEIGHT : 0) + 6 + lines.length * LINE_HEIGHT,
});
}
return rows;
}
function drawEventRow(
image: GrayImage,
font: BdfFont,
row: EventRow,
x: number,
y: number,
width: number,
selected: boolean,
): void {
let cursorY = y;
if (row.dayHeader) {
image.drawText(font, x, cursorY + 2, row.dayHeader, 150);
cursorY += DAY_HEADER_HEIGHT;
}
const bodyHeight = row.lines.length * LINE_HEIGHT + 4;
if (selected) {
image.fillRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 15, 6);
image.drawRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 90, 6);
}
const maxTextWidth = width - 2 * x;
for (let index = 0; index < row.lines.length; index++) {
const value = index === 0 ? (selected ? 235 : 205) : 160;
image.drawText(font, x, cursorY + 3 + index * LINE_HEIGHT, truncateText(font, row.lines[index]!, maxTextWidth), value);
}
}
function scrollForSelected(rows: EventRow[], selectedIndex: number, viewportHeight: number): number {
let selectedTop = 0;
for (let index = 0; index < selectedIndex; index++) {
selectedTop += rows[index]!.height + ROW_GAP;
}
const selectedBottom = selectedTop + rows[selectedIndex]!.height;
const contentHeight = rows.reduce((sum, row) => sum + row.height + ROW_GAP, 0);
const maxScroll = Math.max(0, contentHeight - viewportHeight);
const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2);
return clamp(centered | 0, 0, maxScroll);
}
function dayKeyOf(timestampMs: number): string {
const date = new Date(timestampMs);
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
}
function dayHeaderLabel(timestampMs: number): string {
const date = new Date(timestampMs);
const now = new Date();
const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
const dayDelta = Math.round((midnight(date) - midnight(now)) / (24 * 60 * 60 * 1000));
const base = `${WEEKDAYS[date.getDay()]} ${date.getDate()} ${MONTHS[date.getMonth()]}`;
if (dayDelta === 0) return `Today ${base}`;
if (dayDelta === 1) return `Tomorrow ${base}`;
return base;
}
function formatEventTime(timestampMs: number): string {
const date = new Date(timestampMs);
const minutes = String(date.getMinutes()).padStart(2, "0");
const hour24 = date.getHours();
if (timeFormatSetting.get() === "12h") {
const hour12 = ((hour24 + 11) % 12) + 1;
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
}
return `${String(hour24).padStart(2, "0")}:${minutes}`;
}
+143
View File
@@ -0,0 +1,143 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
// IMU report pace. This is NOT a literal Hz — the firmware only accepts the
// ImuReportPace codes 100..1000 (step 100); the real delivery rate is
// device-defined. An out-of-range value makes the firmware stream empty
// (all-zero) samples. 200 matches the reference EvenHub running-tracker app.
const IMU_REPORT_PACE = 200;
// How often to reconcile the IMU on/off state with window visibility.
const RECONCILE_INTERVAL_MS = 400;
/**
* Accelerometer demo: turns the IMU stream on while this page is visible (the
* Debug tests window is foregrounded and the screen is on), off otherwise, and
* shows the latest x/y/z reading with a simple per-axis bar.
*
* The IMU stream is experimental — the firmware may never populate it — so the
* page also reports how many samples have arrived.
*/
export class AccelerometerDemoLayer implements Layer {
private reading: ImuReading | null = null;
private sampleCount = 0;
private peak = 0;
private enabled = false;
private removed = false;
private reconcileTimer: ReturnType<typeof setInterval> | null = null;
private unsubscribe: (() => void) | null = null;
constructor(
private readonly windowId: string,
private readonly requestRender: () => void,
) {
this.start();
}
private start(): void {
this.unsubscribe = addImuListener((reading) => {
if (this.removed) return;
this.reading = reading;
this.sampleCount++;
this.peak = Math.max(this.peak, Math.abs(reading.x), Math.abs(reading.y), Math.abs(reading.z));
this.requestRender();
});
this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
this.reconcile();
}
/** Match the IMU on/off state to whether this page is currently visible. */
private reconcile(): void {
if (this.removed) return;
const visible = shell.isWindowVisible(this.windowId);
if (visible === this.enabled) return;
this.enabled = visible;
setImuReportEnabled(visible, IMU_REPORT_PACE);
this.requestRender();
}
onRemoved(): void {
this.removed = true;
if (this.reconcileTimer !== null) {
clearInterval(this.reconcileTimer);
this.reconcileTimer = null;
}
this.unsubscribe?.();
this.unsubscribe = null;
if (this.enabled) {
setImuReportEnabled(false, IMU_REPORT_PACE);
this.enabled = false;
}
}
paint(ctx: LayerContext): GrayImage {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(small, 20, 8, "Accelerometer", 220);
const status = this.enabled ? "IMU on" : "IMU off";
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
if (this.reading) {
const axes: Array<[string, number]> = [
["X", this.reading.x],
["Y", this.reading.y],
["Z", this.reading.z],
];
const barX = 118;
const barW = width - barX - 24;
const scale = Math.max(this.peak, 1e-6);
let y = 40;
for (const [label, value] of axes) {
image.drawText(medium, 22, y, label, 210);
image.drawText(medium, 44, y, value.toFixed(3), 235);
drawCenteredBar(image, barX, y + 2, barW, 12, clamp(value / scale, -1, 1));
y += 30;
}
image.drawText(
small,
22,
y + 6,
`source: ${imuSourceLabel(this.reading.source)} samples: ${this.sampleCount}`,
130,
);
} else {
const message = this.enabled
? "Waiting for IMU data…"
: "Paused (page not visible).";
image.drawText(small, 22, 44, message, 190);
image.drawText(small, 22, 66, "The firmware may not report the", 120);
image.drawText(small, 22, 82, "accelerometer at all — experimental.", 120);
}
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click") {
ctx.stack.pop();
}
}
}
/** Horizontal bar centered at its midpoint; frac in [-1, 1] fills left/right. */
function drawCenteredBar(image: GrayImage, x: number, y: number, width: number, height: number, frac: number): void {
const mid = x + Math.round(width / 2);
image.fillRect(x, y, width, height, 20);
image.setPixel(mid, y - 1, 90);
image.setPixel(mid, y + height, 90);
const half = width / 2;
const px = Math.round(clamp(frac, -1, 1) * half);
if (px >= 0) {
image.fillRect(mid, y, Math.max(1, px), height, 200);
} else {
image.fillRect(mid + px, y, -px, height, 200);
}
}
+108
View File
@@ -0,0 +1,108 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../../ui/layers";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
import {
buildSoundSequencePayload,
CFW_SEQ_MAX,
effectPhrases,
phraseDurationMs,
SOUND_EFFECTS,
type SoundEffect,
} from "../../ui/sound-effects";
const HEADER_HEIGHT = 30;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Buzzer demo: pick a sound effect from the catalog and play it on the
* glasses' piezo. Multi-phrase effects are paced phrase-by-phrase so each
* sequencer message lands as the previous phrase finishes.
*/
export class BuzzerDemoLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
private playing: string | null = null;
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, 20, 8, "Buzzer demo", 220);
const status = this.playing ? `Playing: ${this.playing}` : `${GESTURE_CLICK} play`;
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length);
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const effect = SOUND_EFFECTS[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
}
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
}
image.drawText(font, 20, height - 16, `${GESTURE_SCROLL} select ${GESTURE_CLICK} play ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) {
case "scroll-up":
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
return;
case "scroll-down":
this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1);
return;
case "click": {
const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)];
if (effect && !this.playing) {
// Fire-and-forget: playback paces itself with sleeps between
// phrases; awaiting it here would stall input for seconds.
void this.playEffect(effect, ctx.actions).catch((error) => {
console.warn(`buzzer effect failed: ${error}`);
});
}
return;
}
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
private async playEffect(effect: SoundEffect, actions: LayerActions): Promise<void> {
this.playing = effect.name;
actions.requestRender();
try {
for (const phrase of effectPhrases(effect.make())) {
// Safety net: chop any over-long phrase into <=48-step messages.
for (let index = 0; index < phrase.length; index += CFW_SEQ_MAX) {
const chunk = phrase.slice(index, index + CFW_SEQ_MAX);
await actions.playBuzzerSequence(buildSoundSequencePayload(chunk));
await sleep(phraseDurationMs(chunk));
}
}
} finally {
this.playing = null;
actions.requestRender();
}
}
}
+62
View File
@@ -0,0 +1,62 @@
import { MenuLayer } from "../../ui/menu";
import { ScreenTestLayer } from "./screen-test";
import { BuzzerDemoLayer } from "./buzzer-demo";
import { AccelerometerDemoLayer } from "./accelerometer-demo";
import { appViewportSize } from "../../ui/shell/geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
export const DEBUG_TESTS_WINDOW_ID = "debug-tests";
export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests";
/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */
export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow {
const menu = new MenuLayer(
"Debug tests",
[
{
label: "Screen test",
onSelect: (ctx) => {
ctx.stack.push(new ScreenTestLayer());
},
},
{
label: "Buzzer demo",
onSelect: (ctx) => {
ctx.stack.push(new BuzzerDemoLayer());
},
},
{
label: "Accelerometer demo",
onSelect: (ctx) => {
ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
},
},
],
{
x: 8,
y: 8,
width: 272,
minHeight: 0,
maxHeight: appViewportSize("min").height - 16,
},
);
return createInProcessWindow({
appId: "debug-tests",
windowId: DEBUG_TESTS_WINDOW_ID,
title: "Debug tests",
iconLetter: "Db",
icon: "flask-conical",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(menu),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
+73
View File
@@ -0,0 +1,73 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const COLOR_VALUE_COUNT = 256;
const COLOR_VALUE_STEP = 1;
const GRID_COLUMNS = 16;
const CELL_HEIGHT = 30;
const LABEL_Y_OFFSET = 0;
const SWATCH_Y_OFFSET = 12;
const SWATCH_WIDTH = 24;
const SWATCH_HEIGHT = 16;
export class ScreenTestLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
// Sized to the hosting stack (the Debug tests app viewport).
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const font = getDefaultSmallFont();
const cellWidth = Math.floor(width / GRID_COLUMNS);
const gridX = Math.floor((width - cellWidth * GRID_COLUMNS) / 2);
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
const slot = value / COLOR_VALUE_STEP;
const col = slot % GRID_COLUMNS;
const row = Math.floor(slot / GRID_COLUMNS);
const cellX = gridX + col * cellWidth;
const cellY = row * CELL_HEIGHT;
const label = String(value);
const labelX = cellX + Math.max(0, Math.floor((cellWidth - font.measureText(label)) / 2));
const swatchX = cellX + Math.floor((cellWidth - SWATCH_WIDTH) / 2);
image.drawText(font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
}
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "double-click") {
ctx.stack.pop();
}
}
}
function fillSwatch(image: GrayImage, x: number, y: number, width: number, height: number, value: number): void {
// Values will ultimately be rounded up to the nearest multiple of 16 (since they're transmitted as 4-bit)
// grayscale). So, for intermediate values, dither.
if (value % 16 !== 0) {
const baseValue = value & 0xf0;
const fillFraction = value & 0x0f;
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const ditheredValue = ditherPattern(j, i, fillFraction)
image.setPixel(x + j, y + i, ditheredValue ? baseValue + 16 : baseValue);
}
}
} else {
image.fillRect(x, y, width, height, value);
}
}
function ditherPattern(x: number, y: number, fillFraction: number): number {
const pattern = [
[0, 8, 2, 10],
[12, 4, 14, 6],
[3, 11, 1, 9],
[15, 7, 13, 5],
];
const patternValue = pattern[y % 4][x % 4];
return (fillFraction > patternValue) ? 1 : 0;
}
+591
View File
@@ -0,0 +1,591 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText, truncateLeft } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { ConfigSettingEnum } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import {
externalStorageRootPath,
hasAllFilesAccess,
listDirectory,
requestAllFilesAccess,
statPath,
type DirectoryEntry,
} from "../../native/file-access";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
const GRID_COLS = 5;
const GRID_VISIBLE_ROWS = 3;
const GRID_BOTTOM_MARGIN = 20;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
export type FilesViewMode = "icons" | "list";
export const filesViewModeSetting = new ConfigSettingEnum<FilesViewMode>({
id: "files-view-mode",
label: "View",
storageKey: "files.viewMode",
defaultValue: "icons",
values: ["icons", "list"],
formatValue: (value) => (value === "icons" ? "Icons" : "List"),
});
// Bookmarked paths, in bookmarking order, stored as a JSON string array.
const BOOKMARKS_KEY = "files.bookmarks";
export function getBookmarkedPaths(): string[] {
try {
const parsed = JSON.parse(getStringSetting(BOOKMARKS_KEY, "[]"));
return Array.isArray(parsed) ? parsed.filter((path): path is string => typeof path === "string") : [];
} catch {
return [];
}
}
export function isBookmarked(path: string): boolean {
return getBookmarkedPaths().includes(path);
}
export function toggleBookmark(path: string): void {
const paths = getBookmarkedPaths();
const next = paths.includes(path) ? paths.filter((p) => p !== path) : [...paths, path];
setStringSetting(BOOKMARKS_KEY, JSON.stringify(next));
}
const TEXT_EXT = /\.(txt|md|markdown|log|json|xml|csv|ini|conf|cfg|yaml|yml|ts|js|py|java|c|cpp|h|sh|html|css)$/i;
const IMAGE_EXT = /\.(png|jpe?g|gif|bmp|webp|heic|heif|svg)$/i;
const VIDEO_EXT = /\.(mp4|mkv|webm|avi|mov|3gp|m4v)$/i;
const AUDIO_EXT = /\.(mp3|m4a|aac|ogg|oga|opus|flac|wav|mid)$/i;
function fileIconName(name: string): IconName {
if (TEXT_EXT.test(name)) return "file-text";
if (IMAGE_EXT.test(name)) return "image";
if (VIDEO_EXT.test(name)) return "film";
if (AUDIO_EXT.test(name)) return "music";
return "file";
}
export type FileBrowserOptions = {
/** Files failing this are listed dimmed (not viewable); picking them still fires onFilePicked. */
isSupportedFile: (name: string) => boolean;
onFilePicked: (entry: DirectoryEntry, ctx: LayerContext) => void;
/** Double-click while already at the Places level (leave the browser). */
onLeave: () => void;
};
type EntryItem = {
kind: "entry";
label: string;
icon: IconName;
entry: DirectoryEntry;
supported: boolean;
/** A bookmark whose path no longer exists (listed only for unbookmarking). */
missing?: boolean;
/** An item on the Places level (a bookmark or a fixed storage root). */
place: boolean;
/** Fixed Places roots cannot be bookmarked or unbookmarked. */
fixed: boolean;
};
type BrowserItem = { kind: "grant"; label: string } | { kind: "info"; label: string } | EntryItem;
type IconRow =
| { kind: "special"; item: BrowserItem; flatIndex: number }
| { kind: "grid"; items: EntryItem[]; firstIndex: number };
type IconMode = "row" | "item";
/**
* Filesystem browser with two views. The top "Places" level lists bookmarks
* and the storage roots; descending browses real directories. Icons view is a
* 5-column grid with launcher-style two-level selection (scroll picks a row,
* click drops into the row, scroll picks the item); list view is a flat list.
* Click descends into a directory or picks a supported file; double-click
* backs out one level (item selection, then parent directory, then Places,
* then leaving the browser). Sized to its hosting stack.
*/
export class FileBrowserLayer implements Layer {
/** Current directory, or null at the Places level. */
private location: string | null = null;
/** The Places entry we descended through; going up from it returns to Places. */
private navRoot: string | null = null;
private entries: DirectoryEntry[] | null = null;
private listingFailed = false;
private listIndex = 0;
private selectedRow = 0;
private selectedCol = 0;
private iconMode: IconMode = "row";
private scrollRow = 0;
constructor(private readonly options: FileBrowserOptions) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const rows = this.flatRows();
image.drawText(font, 20, 8, "Files", 220);
const pathLabel = this.location === null ? "Places" : this.location;
image.drawText(font, 20, 22, truncateLeft(font, pathLabel, width - 40), 130);
if (filesViewModeSetting.get() === "list") {
this.paintList(image, font, rows, ctx);
} else {
this.paintIcons(image, font, rows, ctx);
}
return image;
}
private paintList(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1));
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.listIndex, visibleRows, rows.length);
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const row = rows[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const selected = index === this.listIndex;
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
}
const value = itemValue(row, selected);
image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} up / back`, 110);
}
private paintIcons(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const focused = ctx.stack.isFocused();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const gridTop = HEADER_HEIGHT;
const gridBottom = height - GRID_BOTTOM_MARGIN;
const rowH = Math.floor((gridBottom - gridTop) / GRID_VISIBLE_ROWS);
const colW = width / GRID_COLS;
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, GRID_VISIBLE_ROWS, iconRows.length);
const lastVisible = Math.min(iconRows.length, this.scrollRow + GRID_VISIBLE_ROWS);
for (let rowIndex = this.scrollRow; rowIndex < lastVisible; rowIndex++) {
const row = iconRows[rowIndex]!;
const y = gridTop + (rowIndex - this.scrollRow) * rowH;
const selected = rowIndex === this.selectedRow;
if (row.kind === "special") {
const textY = y + (((rowH - font.lineHeight) / 2) | 0);
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, textY - 2, width - 2 * LIST_X + 12, font.lineHeight + 4, focused, 4);
}
image.drawText(font, LIST_X, textY, truncateText(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected));
continue;
}
if (selected) {
if (this.iconMode === "row") {
drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6);
} else {
this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1);
drawSelectionHighlight(image, this.selectedCol * colW + 4, y + 1, colW - 8, rowH - 2, focused, 6);
}
}
for (let col = 0; col < row.items.length; col++) {
const item = row.items[col]!;
const centerX = col * colW + colW / 2;
const blockTop = y + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
const icon = renderIcon(item.icon, ICON_SIZE);
if (icon) {
image.bitBlt(item.supported ? icon : dimmedIcon(item.icon, icon), Math.round(centerX - icon.width / 2), Math.round(blockTop), {
transparentZero: true,
});
}
const label = truncateText(font, item.label, colW - 8);
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, item.supported ? 210 : 100);
}
}
if (iconRows.length > GRID_VISIBLE_ROWS) {
drawListScrollbar(image, width - 5, gridTop, GRID_VISIBLE_ROWS * rowH - 4, this.scrollRow, GRID_VISIBLE_ROWS, iconRows.length);
}
const hint =
this.iconMode === "row"
? `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick ${GESTURE_DOUBLE_CLICK} up / back`
: `${GESTURE_SCROLL} item ${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} row`;
image.drawText(font, 20, height - 16, hint, 110);
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (filesViewModeSetting.get() === "list") {
await this.handleListInput(event, ctx);
} else {
await this.handleIconsInput(event, ctx);
}
}
private async handleListInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
const rows = this.flatRows();
switch (event.type) {
case "scroll-up":
this.listIndex = Math.max(0, this.listIndex - 1);
return;
case "scroll-down":
this.listIndex = Math.min(Math.max(0, rows.length - 1), this.listIndex + 1);
return;
case "click": {
const row = rows[clamp(this.listIndex, 0, Math.max(0, rows.length - 1))];
if (row) await this.activateItem(row, ctx);
return;
}
case "double-click":
this.navigateUp();
return;
default:
return;
}
}
private async handleIconsInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const row = iconRows[this.selectedRow];
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.iconMode === "item") {
// Item selection traverses linearly: past a row's edge it continues
// onto the adjacent row (a special row counts as a single item).
if (row?.kind === "grid") {
const next = clamp(this.selectedCol, 0, row.items.length - 1) + delta;
if (next >= 0 && next < row.items.length) {
this.selectedCol = next;
return;
}
}
const adjacentIndex = this.selectedRow + delta;
if (adjacentIndex < 0 || adjacentIndex >= iconRows.length) return;
this.selectedRow = adjacentIndex;
const adjacent = iconRows[adjacentIndex]!;
if (adjacent.kind === "grid") {
this.selectedCol = delta > 0 ? 0 : adjacent.items.length - 1;
}
} else {
this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1));
}
return;
}
case "click": {
if (!row) return;
if (row.kind === "special") {
await this.activateItem(row.item, ctx);
return;
}
if (this.iconMode === "row") {
this.iconMode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(Math.floor(GRID_COLS / 2), row.items.length - 1);
} else {
const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)];
if (item) await this.activateItem(item, ctx);
}
return;
}
case "double-click":
if (this.iconMode === "item") {
this.iconMode = "row";
} else {
this.navigateUp();
}
return;
default:
return;
}
}
/**
* App-specific entries for the window long-press menu: the view switch,
* plus bookmark/unbookmark for the selected entry (in icons view only once
* an item, not just a row, is selected).
*/
buildMenuItems(): MenuItem[] {
const items: MenuItem[] = [];
const mode = filesViewModeSetting.get();
items.push({
label: mode === "icons" ? "View as list" : "View as icons",
onSelect: (ctx) => {
this.setViewMode(mode === "icons" ? "list" : "icons");
ctx.stack.pop();
},
});
const target = this.currentItem();
if (target && target.kind === "entry" && !target.fixed) {
const path = target.entry.path;
const name = truncateChars(target.entry.name || path, 24);
items.push({
label: isBookmarked(path) ? `Remove bookmark: ${name}` : `Bookmark: ${name}`,
onSelect: (ctx) => {
toggleBookmark(path);
const rows = this.flatRows();
this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1));
ctx.stack.pop();
},
});
}
return items;
}
/** The item an entry-specific action would apply to, if one is selected. */
private currentItem(): BrowserItem | null {
const rows = this.flatRows();
if (!rows.length) return null;
if (filesViewModeSetting.get() === "list") {
return rows[clamp(this.listIndex, 0, rows.length - 1)] ?? null;
}
const iconRows = buildIconRows(rows);
const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)];
if (!row) return null;
if (row.kind === "special") return row.item;
if (this.iconMode !== "item") return null;
return row.items[clamp(this.selectedCol, 0, row.items.length - 1)] ?? null;
}
private setViewMode(mode: FilesViewMode): void {
if (filesViewModeSetting.get() === mode) return;
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
if (mode === "list") {
// Carry the icon-grid selection into the flat list (clamped column when
// only a row was selected).
const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))];
if (row) {
this.listIndex =
row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1);
}
} else {
this.setGridSelectionFromFlatIndex(rows, clamp(this.listIndex, 0, Math.max(0, rows.length - 1)));
this.iconMode = "row";
}
filesViewModeSetting.set(mode);
this.scrollRow = 0;
}
private async activateItem(item: BrowserItem, ctx: LayerContext): Promise<void> {
if (item.kind === "grant") {
requestAllFilesAccess();
this.entries = null; // re-list after the user returns from Settings
return;
}
if (item.kind !== "entry" || item.missing) return;
if (item.entry.isDirectory) {
this.navigateTo(item.entry.path, item.place);
} else {
// Every real file opens the picked-file dialog, viewable or not (the
// dialog shows metadata; open actions depend on the type).
this.options.onFilePicked(item.entry, ctx);
}
}
private navigateTo(path: string, asRoot = false): void {
if (asRoot) this.navRoot = path;
this.location = path;
this.entries = null;
this.resetSelection();
}
private navigateUp(): void {
const from = this.location;
if (from === null) {
this.options.onLeave();
return;
}
if (from === this.navRoot || from === "/") {
this.location = null;
this.entries = null;
this.resetSelection();
this.selectPath(this.navRoot ?? from);
this.navRoot = null;
return;
}
const parentRaw = from.slice(0, from.lastIndexOf("/"));
this.navigateTo(parentRaw.length ? parentRaw : "/");
this.selectPath(from);
}
private resetSelection(): void {
this.listIndex = 0;
this.selectedRow = 0;
this.selectedCol = 0;
this.iconMode = "row";
this.scrollRow = 0;
}
/** Select the item with the given path (e.g. the directory just left). */
private selectPath(path: string): void {
const rows = this.flatRows();
const index = rows.findIndex((row) => row.kind === "entry" && row.entry.path === path);
if (index < 0) return;
this.listIndex = index;
this.setGridSelectionFromFlatIndex(rows, index);
}
private setGridSelectionFromFlatIndex(rows: BrowserItem[], index: number): void {
const specials = leadingSpecialCount(rows);
if (index < specials) {
this.selectedRow = index;
this.selectedCol = 0;
return;
}
const ordinal = index - specials;
this.selectedRow = specials + Math.floor(ordinal / GRID_COLS);
this.selectedCol = ordinal % GRID_COLS;
}
private flatRows(): BrowserItem[] {
const rows: BrowserItem[] = [];
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
if (this.location === null) {
for (const path of getBookmarkedPaths()) {
rows.push(this.bookmarkItem(path));
}
rows.push(this.placeItem("Internal storage", externalStorageRootPath()));
rows.push(this.placeItem("/", "/"));
return rows;
}
this.loadEntries();
for (const entry of this.entries ?? []) {
rows.push({
kind: "entry",
entry,
label: entry.isDirectory ? `${entry.name}/` : entry.name,
icon: entry.isDirectory ? "folder" : fileIconName(entry.name),
supported: entry.isDirectory || this.options.isSupportedFile(entry.name),
place: false,
fixed: false,
});
}
if (!rows.some((row) => row.kind === "entry")) {
rows.push({ kind: "info", label: this.listingFailed ? "(unreadable directory)" : "(empty directory)" });
}
return rows;
}
private placeItem(label: string, path: string): EntryItem {
return {
kind: "entry",
entry: { name: label, path, isDirectory: true, sizeBytes: 0, modifiedMs: 0 },
label,
icon: "hard-drive",
supported: true,
place: true,
fixed: true,
};
}
private bookmarkItem(path: string): EntryItem {
const stat = statPath(path);
if (!stat) {
const name = basename(path);
return {
kind: "entry",
entry: { name, path, isDirectory: false, sizeBytes: 0, modifiedMs: 0 },
label: `${name} (missing)`,
icon: "file",
supported: false,
missing: true,
place: true,
fixed: false,
};
}
const name = stat.name || path;
return {
kind: "entry",
entry: stat,
label: stat.isDirectory ? `${name}/` : name,
icon: stat.isDirectory ? "folder" : fileIconName(name),
supported: stat.isDirectory || this.options.isSupportedFile(name),
place: true,
fixed: false,
};
}
private loadEntries(): void {
if (this.entries !== null || this.location === null) return;
const listing = listDirectory(this.location);
this.listingFailed = listing === null;
this.entries = listing ?? [];
}
}
function buildIconRows(flat: BrowserItem[]): IconRow[] {
const rows: IconRow[] = [];
let index = 0;
// Special rows (grant prompt, empty/unreadable info) lead the list; each
// gets a full-width row of its own. Entries follow, chunked into grid rows.
while (index < flat.length && flat[index]!.kind !== "entry") {
rows.push({ kind: "special", item: flat[index]!, flatIndex: index });
index++;
}
for (; index < flat.length; index += GRID_COLS) {
rows.push({ kind: "grid", items: flat.slice(index, index + GRID_COLS) as EntryItem[], firstIndex: index });
}
return rows;
}
function leadingSpecialCount(rows: BrowserItem[]): number {
let count = 0;
while (count < rows.length && rows[count]!.kind !== "entry") count++;
return count;
}
function itemValue(item: BrowserItem, selected: boolean): number {
if (item.kind === "info") return 120;
if (item.kind === "grant") return selected ? 255 : 210;
if (!item.supported) return selected ? 140 : 100;
return selected ? 255 : 200;
}
// Unsupported files draw their grid icon at half brightness; rendered once
// per icon name and cached (all grid icons share one size).
const dimmedIconCache = new Map<IconName, GrayImage>();
function dimmedIcon(name: IconName, icon: GrayImage): GrayImage {
const cached = dimmedIconCache.get(name);
if (cached) return cached;
const dimmed = new GrayImage(icon.width, icon.height, 0);
for (let i = 0; i < icon.pixels.length; i++) {
dimmed.pixels[i] = icon.pixels[i]! >> 1;
}
dimmedIconCache.set(name, dimmed);
return dimmed;
}
function basename(path: string): string {
const parts = path.split("/").filter((part) => part.length > 0);
return parts[parts.length - 1] ?? path;
}
function truncateChars(text: string, maxLength: number): string {
return text.length <= maxLength ? text : `${text.slice(0, Math.max(0, maxLength - 3))}...`;
}
+119
View File
@@ -0,0 +1,119 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import type { DirectoryEntry } from "../../native/file-access";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawSelectionHighlight } from "../../ui/menu";
const DIALOG_X = 8;
const DIALOG_Y = 8;
const DIALOG_WIDTH = 272;
const PADDING = 10;
const INFO_LINE_STEP = 14;
const ACTION_ROW_HEIGHT = 20;
const MAX_NAME_LINES = 3;
export type FileInfoAction = {
label: string;
onSelect: (ctx: LayerContext) => Promise<void> | void;
};
/**
* The picked-file dialog: metadata (full untruncated name, size, modified
* date) followed by the open actions for viewable file types — possibly none
* for files nothing can open. Opened by the browser for every file;
* double-click closes it.
*/
export class FileInfoDialogLayer implements Layer {
private selectedIndex = 0;
constructor(
private readonly entry: DirectoryEntry,
private readonly actions: FileInfoAction[],
) {}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const textWidth = DIALOG_WIDTH - 2 * PADDING - 4;
const nameLines = wrapText(font, this.entry.name, textWidth, { breakLongWords: true }).slice(0, MAX_NAME_LINES);
const infoLines = [
`Size: ${formatSize(this.entry.sizeBytes)}`,
`Modified: ${formatDate(this.entry.modifiedMs)}`,
];
const bodyTop = PADDING + (nameLines.length + infoLines.length) * INFO_LINE_STEP + 6;
const bodyHeight = this.actions.length ? this.actions.length * ACTION_ROW_HEIGHT : INFO_LINE_STEP;
const height = Math.min(bodyTop + bodyHeight + PADDING, viewportHeight - 2 * DIALOG_Y);
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
// transparent color key when painting on the shell surface.
image.fillRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 1);
image.drawRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 72);
let y = DIALOG_Y + PADDING;
for (const line of nameLines) {
image.drawText(font, DIALOG_X + PADDING + 2, y, line, 230);
y += INFO_LINE_STEP;
}
for (const line of infoLines) {
image.drawText(font, DIALOG_X + PADDING + 2, y, line, 150);
y += INFO_LINE_STEP;
}
y += 6;
if (!this.actions.length) {
image.drawText(font, DIALOG_X + PADDING + 2, y + 3, `${GESTURE_DOUBLE_CLICK} close`, 110);
return image;
}
const focused = ctx.stack.isFocused();
for (let index = 0; index < this.actions.length; index++) {
const rowY = y + index * ACTION_ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, ACTION_ROW_HEIGHT - 1, focused, 8);
}
image.drawText(font, DIALOG_X + 22, rowY + 3, this.actions[index]!.label, selected ? 255 : 200);
}
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) {
case "scroll-up":
if (this.actions.length) {
this.selectedIndex = (this.selectedIndex + this.actions.length - 1) % this.actions.length;
}
return;
case "scroll-down":
if (this.actions.length) {
this.selectedIndex = (this.selectedIndex + 1) % this.actions.length;
}
return;
case "click":
await this.actions[this.selectedIndex]?.onSelect(ctx);
return;
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}
function formatDate(ms: number): string {
if (!ms) return "unknown";
const date = new Date(ms);
const pad = (n: number) => String(n).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
+153
View File
@@ -0,0 +1,153 @@
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
import { isDecodableImageFile } from "../../native/image-files";
import { FileBrowserLayer } from "./file-browser";
import { FileInfoDialogLayer, type FileInfoAction } from "./file-info-dialog";
import { ImageViewerLayer } from "./image-viewer";
import { TextViewerLayer } from "./text-viewer";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
export const FILES_WINDOW_ID = "files";
export const FILES_SURFACE_ID = "window:files";
const TEXT_FILE = /\.(txt|md|log)$/i;
export type FilesAppOptions = InProcessAppOptions & {
/** Open a text document as its own shell window (also used by the share intent). */
openDocumentWindow: (title: string, text: string) => void;
/** Open an image file as its own shell window. */
openImageWindow: (title: string, path: string) => void;
};
/**
* The Files app's launcher-opened window: a file browser over Places
* (bookmarks and storage roots). Picking any file opens an info dialog with
* metadata plus, for viewable types (text, images), the open actions.
*/
export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow {
let created: InProcessWindow | null = null;
const browser = new FileBrowserLayer({
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name),
// The browser handles double-click itself (up a level), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
onFilePicked: (entry, ctx) => {
ctx.stack.push(new FileInfoDialogLayer(entry, fileOpenActions(entry, options)));
},
});
created = createInProcessWindow({
appId: "files",
windowId: FILES_WINDOW_ID,
title: "Files",
iconLetter: "F",
icon: "folder",
closeable: true,
// Browser actions (view switch, bookmarks) only apply while the browser
// itself is on top; a pushed viewer or dialog gets just the defaults.
menuItems: () => (created && !created.stack.isAtBase() ? [] : browser.buildMenuItems()),
actions: options.actions,
baseLayer: browser,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
return created;
}
/** One opened text document as its own window (from the browser or a share intent). */
export function createTextDocumentWindow(
windowId: string,
title: string,
text: string,
options: InProcessAppOptions,
): InProcessWindow {
return createInProcessWindow({
appId: "files",
windowId,
title,
iconLetter: "F",
icon: "file-text",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new TextViewerLayer(text, title)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
/** One opened image file as its own window. */
export function createImageDocumentWindow(
windowId: string,
title: string,
path: string,
options: InProcessAppOptions,
): InProcessWindow {
return createInProcessWindow({
appId: "files",
windowId,
title,
iconLetter: "F",
icon: "image",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new ImageViewerLayer(path, title)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
/**
* The open actions for the picked-file dialog: View here / Open in new
* window for viewable types, empty for everything else (metadata only).
*/
function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileInfoAction[] {
if (TEXT_FILE.test(entry.name)) {
return [
{
label: "View here",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
ctx.stack.push(new TextViewerLayer(text ?? "(could not read file)", entry.name));
},
},
{
label: "Open in new window",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
options.openDocumentWindow(entry.name, text ?? "(could not read file)");
},
},
];
}
if (isDecodableImageFile(entry.name)) {
return [
{
label: "View here",
onSelect: (ctx) => {
ctx.stack.pop();
ctx.stack.push(new ImageViewerLayer(entry.path, entry.name));
},
},
{
label: "Open in new window",
onSelect: (ctx) => {
ctx.stack.pop();
options.openImageWindow(entry.name, entry.path);
},
},
];
}
return [];
}
+57
View File
@@ -0,0 +1,57 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { loadImageFileAsGray } from "../../native/image-files";
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const MARGIN_X = 18;
const TITLE_Y = 8;
const BODY_TOP = 24;
const BODY_MARGIN = 2;
/**
* Image viewer for a file on disk: title line, then the image decoded to
* grayscale and downscaled to fit the remaining area (never upscaled).
* Double-click closes it. Sized to its hosting stack (a Files document
* window, or pushed over the file browser for view-in-place).
*/
export class ImageViewerLayer implements Layer {
private image: GrayImage | null = null;
private loadAttempted = false;
constructor(
private readonly path: string,
private readonly title: string,
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const out = new GrayImage(width, height, 0);
out.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X), 220);
const bodyHeight = height - BODY_TOP - BODY_MARGIN;
if (!this.loadAttempted) {
this.loadAttempted = true;
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight);
}
if (!this.image) {
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160);
out.drawText(font, MARGIN_X, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return out;
}
out.bitBlt(
this.image,
Math.round((width - this.image.width) / 2),
BODY_TOP + Math.round((bodyHeight - this.image.height) / 2),
);
return out;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "double-click") {
ctx.stack.pop();
}
}
}
+96
View File
@@ -0,0 +1,96 @@
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const MARGIN_X = 18;
const TITLE_Y = 16;
const BODY_X = 18;
const BODY_Y = 44;
const LINE_STEP = 14;
const FOOTER_MARGIN = 36;
/**
* Paged text viewer for a document. Sized to its hosting stack (a Files
* document window, or pushed over the file browser for view-in-place).
*/
export class TextViewerLayer implements Layer {
private lines: string[] | null = null;
private wrappedForWidth = 0;
private firstLine = 0;
private bodyLineCount = 14;
constructor(
private readonly documentText: string,
private readonly title = "Text",
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / LINE_STEP));
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220);
const lines = this.getLines(font, width);
const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
for (let index = 0; index < visibleLines.length; index++) {
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230);
}
const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1;
image.drawText(font, BODY_X, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
switch (event.type) {
case "scroll-down":
this.scrollBy(this.pageStep());
return;
case "scroll-up":
this.scrollBy(-this.pageStep());
return;
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
private pageStep(): number {
return Math.max(1, this.bodyLineCount - 1);
}
private scrollBy(delta: number): void {
const lines = this.lines ?? [];
if (lines.length <= this.bodyLineCount) {
this.firstLine = 0;
return;
}
const maxFirstLine = Math.max(0, lines.length - 1);
this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta));
}
private getLines(font: BdfFont, width: number): string[] {
if (this.lines === null || this.wrappedForWidth !== width) {
const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, "");
this.lines = wrapText(font, normalized, width - BODY_X - 12, {
preserveLeadingWhitespace: true,
breakLongWords: true,
});
this.wrappedForWidth = width;
}
return this.lines;
}
private totalPageCount(lineCount: number): number {
if (lineCount <= this.bodyLineCount) {
return 1;
}
return Math.ceil((lineCount - this.bodyLineCount) / this.pageStep()) + 1;
}
}
+760
View File
@@ -0,0 +1,760 @@
/**
* Freecell solitaire app worker. One singleton window holds a standard
* 52-card Freecell game: 8 cascades, 4 free cells, 4 foundations. Red suits
* render dim and black suits bright, since the display has no color.
*
* Controls: scroll moves a cursor through the 16 locations (free cells,
* foundations, then cascades, wrapping). Click selects a source, then click
* on a destination moves there; cascade-to-cascade moves take the longest
* legal run that fits (supermoves via empty cells/columns). Double-click
* sends the card at the cursor to its foundation, or cancels a pending
* selection. Long-press opens the window menu (undo, new game, restart).
* Safe cards auto-play to the foundations after every move.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import type { MenuItem } from "../../ui/menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS } from "../../ui/gestures";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const labelFont = getFont("terminus16");
const smallFont = getDefaultSmallFont();
/** Cards are 0..51: suit = card % 4 (♠♥♣♦, alternating colors), rank 1..13. */
const SUIT_CHARS = ["♠", "♥", "♣", "♦"] as const;
const RANK_CHARS = ["", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] as const;
function suitOf(card: number): number {
return card % 4;
}
function rankOf(card: number): number {
return Math.floor(card / 4) + 1;
}
function isRed(card: number): boolean {
return (card & 1) === 1;
}
function cardLabel(card: number): string {
return RANK_CHARS[rankOf(card)]! + SUIT_CHARS[suitOf(card)]!;
}
/** Red suits render dimmer than black so card color survives grayscale. */
const BLACK_SHADE = 255;
const RED_SHADE = 150;
const CARD_W = 60;
const CARD_H = 36;
const TOP_Y = 4;
const CASCADE_Y = 48;
/**
* Cursor locations, in scroll order: free cells 0-3, foundations 0-3 (the
* top row left to right), then cascades 0-7.
*/
const LOC_CELL0 = 0;
const LOC_FOUNDATION0 = 4;
const LOC_CASCADE0 = 8;
const LOC_COUNT = 16;
/** Buzzer effects, tuned soft like the other games' (shared piezo). */
const SFX_SELECT: Step[] = [{ freq: 880, duty: 40, ms: 20 }];
const SFX_MOVE: Step[] = [{ freq: 660, duty: 40, ms: 15 }];
const SFX_FOUNDATION: Step[] = [{ freq: 1047, duty: 40, ms: 25 }];
const SFX_ERROR: Step[] = [{ freq: 220, duty: 45, ms: 60 }];
const SFX_UNDO: Step[] = [{ freq: 550, duty: 40, ms: 30 }];
/** Also the new-game and sound-toggled-on confirmation. */
const SFX_NEW_GAME: Step[] = [
{ freq: 880, duty: 40, ms: 60 },
{ freq: 1319, duty: 40, ms: 110 },
];
const SFX_WIN: Step[] = [
{ freq: 1047, ms: 55 },
{ freq: 1319, ms: 55 },
{ freq: 1568, ms: 55 },
{ freq: 2093, ms: 70 },
{ freq: 1, duty: 0, ms: 30 },
{ freq: 2093, ms: 160 },
];
type Snapshot = {
cascades: number[][];
cells: Array<number | null>;
foundations: Array<number | null>;
moves: number;
};
type FreecellWindow = {
windowId: string;
surfaceId: string;
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
phase: "playing" | "won";
cascades: number[][];
cells: Array<number | null>;
/** Top card of each foundation pile, or null while empty. */
foundations: Array<number | null>;
/** The shuffle behind the current game, kept so a deal can be restarted. */
dealOrder: number[];
undoStack: Snapshot[];
moves: number;
cursor: number;
/** Location of the pending source selection, if any. */
selected: number | null;
soundOn: boolean;
lastSubmittedFingerprint: string;
};
const windows = new Map<string, FreecellWindow>();
function post(message: WorkerAppReply): void {
global.postMessage(message);
}
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "open-window": {
const window: FreecellWindow = {
windowId: message.windowId,
surfaceId: message.surfaceId,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
phase: "playing",
cascades: [],
cells: [null, null, null, null],
foundations: [null, null, null, null],
dealOrder: [],
undoStack: [],
moves: 0,
cursor: LOC_CASCADE0,
selected: null,
soundOn: true,
lastSubmittedFingerprint: "",
};
newGame(window, false);
windows.set(message.windowId, window);
break;
}
case "close-window":
windows.delete(message.windowId);
break;
case "input": {
const window = windows.get(message.windowId);
if (!window) {
frameTimings.finishFrame(message.frameId, "discarded: unknown freecell window");
break;
}
window.focused = message.focused;
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
case "render": {
const window = windows.get(message.windowId);
if (!window) break;
window.focused = message.focused;
renderAndSubmit(window, 0);
break;
}
case "foreground": {
const window = windows.get(message.windowId);
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
if (window.foreground) renderAndSubmit(window, 0);
break;
}
}
};
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
*/
function playSfx(window: FreecellWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
} catch (error) {
console.warn(`freecell sfx failed: ${error}`);
}
}
/** The window's long-press menu (game actions + default entries). */
function openWindowMenu(window: FreecellWindow): void {
const items: MenuItem[] = [];
if (window.undoStack.length > 0 && window.phase === "playing") {
items.push({
label: `Undo (${window.undoStack.length})`,
onSelect: (ctx) => {
ctx.stack.pop();
undoMove(window);
},
});
}
items.push(
{
label: "New game",
onSelect: (ctx) => {
ctx.stack.pop();
newGame(window, false);
playSfx(window, SFX_NEW_GAME);
},
},
{
label: "Restart this deal",
onSelect: (ctx) => {
ctx.stack.pop();
newGame(window, true);
playSfx(window, SFX_NEW_GAME);
},
},
{
label: window.soundOn ? "Sound: on" : "Sound: off",
onSelect: (ctx) => {
ctx.stack.pop();
window.soundOn = !window.soundOn;
if (window.soundOn) playSfx(window, SFX_NEW_GAME);
},
},
);
windowMenu(window).open([...items, ...defaultWindowMenuItems(window.windowId, post)]);
}
function windowMenu(window: FreecellWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function handleInput(window: FreecellWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`freecell menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (window.phase === "playing") {
handlePlayingInput(window, event, frameId);
} else {
handleWonInput(window, event, frameId);
}
}
function handlePlayingInput(window: FreecellWindow, event: DashboardInputEvent, frameId: number): void {
switch (event.type) {
case "scroll-up":
window.cursor = (window.cursor + LOC_COUNT - 1) % LOC_COUNT;
break;
case "scroll-down":
window.cursor = (window.cursor + 1) % LOC_COUNT;
break;
case "click":
if (window.selected === null) {
if (locationHasCard(window, window.cursor)) {
window.selected = window.cursor;
playSfx(window, SFX_SELECT);
} else {
playSfx(window, SFX_ERROR);
}
} else if (window.selected === window.cursor) {
window.selected = null;
} else {
performMove(window, window.selected, window.cursor);
}
break;
case "double-click":
if (window.selected !== null) {
window.selected = null;
} else {
sendToFoundation(window, window.cursor);
}
break;
case "long-press":
openWindowMenu(window);
break;
default:
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
return;
}
renderAndSubmit(window, frameId);
}
function handleWonInput(window: FreecellWindow, event: DashboardInputEvent, frameId: number): void {
switch (event.type) {
case "click":
newGame(window, false);
playSfx(window, SFX_NEW_GAME);
break;
case "double-click":
frameTimings.finishFrame(frameId, "discarded: freecell yielded focus");
post({ type: "yield-focus", windowId: window.windowId });
return;
case "long-press":
openWindowMenu(window);
break;
default:
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
return;
}
renderAndSubmit(window, frameId);
}
function newGame(window: FreecellWindow, keepDeal: boolean): void {
if (!keepDeal || window.dealOrder.length !== 52) {
const order = Array.from({ length: 52 }, (_, i) => i);
for (let i = order.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[order[i], order[j]] = [order[j]!, order[i]!];
}
window.dealOrder = order;
}
window.cascades = Array.from({ length: 8 }, () => []);
for (let i = 0; i < 52; i++) {
window.cascades[i % 8]!.push(window.dealOrder[i]!);
}
window.cells = [null, null, null, null];
window.foundations = [null, null, null, null];
window.undoStack = [];
window.moves = 0;
window.cursor = LOC_CASCADE0;
window.selected = null;
window.phase = "playing";
}
function snapshot(window: FreecellWindow): Snapshot {
return {
cascades: window.cascades.map((cascade) => [...cascade]),
cells: [...window.cells],
foundations: [...window.foundations],
moves: window.moves,
};
}
function undoMove(window: FreecellWindow): void {
const previous = window.undoStack.pop();
if (!previous) return;
window.cascades = previous.cascades;
window.cells = previous.cells;
window.foundations = previous.foundations;
window.moves = previous.moves;
window.selected = null;
playSfx(window, SFX_UNDO);
}
function locationHasCard(window: FreecellWindow, location: number): boolean {
if (location < LOC_FOUNDATION0) return window.cells[location] !== null;
if (location < LOC_CASCADE0) return window.foundations[location - LOC_FOUNDATION0] !== null;
return window.cascades[location - LOC_CASCADE0]!.length > 0;
}
/** True if `card` can go on `onto` in a cascade (descending, alternating color). */
function canStack(card: number, onto: number): boolean {
return rankOf(onto) === rankOf(card) + 1 && isRed(onto) !== isRed(card);
}
function foundationCanAccept(pileTop: number | null, card: number): boolean {
if (pileTop === null) return rankOf(card) === 1;
return suitOf(pileTop) === suitOf(card) && rankOf(card) === rankOf(pileTop) + 1;
}
/** The foundation pile this card can legally go to right now, or -1. */
function foundationIndexFor(window: FreecellWindow, card: number): number {
for (let i = 0; i < 4; i++) {
const top = window.foundations[i]!;
if (top !== null && suitOf(top) === suitOf(card)) {
return rankOf(card) === rankOf(top) + 1 ? i : -1;
}
}
if (rankOf(card) === 1) {
return window.foundations.indexOf(null);
}
return -1;
}
/** Length of the movable tail run (descending, alternating colors). */
function tailRunLength(cascade: number[]): number {
let length = Math.min(1, cascade.length);
while (length < cascade.length && canStack(cascade[cascade.length - length]!, cascade[cascade.length - length - 1]!)) {
length++;
}
return length;
}
/**
* Supermove capacity: (empty free cells + 1) doubled per empty cascade. A
* move into an empty cascade can't count that cascade as a waypoint.
*/
function moveCapacity(window: FreecellWindow, excludeCascade: number): number {
const freeCells = window.cells.filter((cell) => cell === null).length;
let capacity = freeCells + 1;
for (let i = 0; i < 8; i++) {
if (i !== excludeCascade && window.cascades[i]!.length === 0) capacity *= 2;
}
return capacity;
}
/** The card a location would give up right now (cascade top), or null. */
function topCardAt(window: FreecellWindow, location: number): number | null {
if (location < LOC_FOUNDATION0) return window.cells[location]!;
if (location < LOC_CASCADE0) return window.foundations[location - LOC_FOUNDATION0]!;
const cascade = window.cascades[location - LOC_CASCADE0]!;
return cascade.length > 0 ? cascade[cascade.length - 1]! : null;
}
/** Remove the card `topCardAt` reported (single-card sources only). */
function takeCardFrom(window: FreecellWindow, location: number): void {
if (location < LOC_FOUNDATION0) {
window.cells[location] = null;
} else if (location < LOC_CASCADE0) {
const pile = location - LOC_FOUNDATION0;
const top = window.foundations[pile]!;
window.foundations[pile] = rankOf(top!) > 1 ? top! - 4 : null;
} else {
window.cascades[location - LOC_CASCADE0]!.pop();
}
}
/**
* Attempt a user move between two locations; on success commit it as one
* undoable step (including any safe auto-plays it unlocks).
*/
function performMove(window: FreecellWindow, src: number, dst: number): void {
const before = snapshot(window);
const result = attemptMove(window, src, dst);
if (!result.moved) {
playSfx(window, SFX_ERROR);
return;
}
window.undoStack.push(before);
if (window.undoStack.length > 200) window.undoStack.shift();
window.moves++;
window.selected = null;
playSfx(window, result.toFoundation ? SFX_FOUNDATION : SFX_MOVE);
autoSafeMoves(window);
checkWin(window);
}
function attemptMove(window: FreecellWindow, src: number, dst: number): { moved: boolean; toFoundation: boolean } {
const failed = { moved: false, toFoundation: false };
const srcIsCascade = src >= LOC_CASCADE0;
const dstIsCell = dst < LOC_FOUNDATION0;
const dstIsFoundation = !dstIsCell && dst < LOC_CASCADE0;
if (dstIsFoundation) {
if (src >= LOC_FOUNDATION0 && src < LOC_CASCADE0) return failed;
const card = topCardAt(window, src);
const pile = dst - LOC_FOUNDATION0;
if (card === null || !foundationCanAccept(window.foundations[pile]!, card)) return failed;
takeCardFrom(window, src);
window.foundations[pile] = card;
return { moved: true, toFoundation: true };
}
if (dstIsCell) {
if (window.cells[dst] !== null) return failed;
const card = topCardAt(window, src);
if (card === null) return failed;
takeCardFrom(window, src);
window.cells[dst] = card;
return { moved: true, toFoundation: false };
}
// Destination is a cascade.
const dstCascade = window.cascades[dst - LOC_CASCADE0]!;
const dstTop = dstCascade.length > 0 ? dstCascade[dstCascade.length - 1]! : null;
if (!srcIsCascade) {
const card = topCardAt(window, src);
if (card === null) return failed;
if (dstTop !== null && !canStack(card, dstTop)) return failed;
takeCardFrom(window, src);
dstCascade.push(card);
return { moved: true, toFoundation: false };
}
const srcCascade = window.cascades[src - LOC_CASCADE0]!;
if (srcCascade.length === 0) return failed;
const capacity = moveCapacity(window, dstTop === null ? dst - LOC_CASCADE0 : -1);
const maxRun = Math.min(tailRunLength(srcCascade), capacity);
// Take the longest legal run; onto an empty cascade any run length works.
for (let n = maxRun; n >= 1; n--) {
const bottom = srcCascade[srcCascade.length - n]!;
if (dstTop === null || canStack(bottom, dstTop)) {
dstCascade.push(...srcCascade.splice(srcCascade.length - n, n));
return { moved: true, toFoundation: false };
}
}
return failed;
}
/** Double-click convenience: the card at `location` goes to its foundation. */
function sendToFoundation(window: FreecellWindow, location: number): void {
if (location >= LOC_FOUNDATION0 && location < LOC_CASCADE0) {
playSfx(window, SFX_ERROR);
return;
}
const card = topCardAt(window, location);
const pile = card === null ? -1 : foundationIndexFor(window, card);
if (pile < 0) {
playSfx(window, SFX_ERROR);
return;
}
performMove(window, location, LOC_FOUNDATION0 + pile);
}
/** Foundation rank per suit (0 while that suit has no pile). */
function foundationRankBySuit(window: FreecellWindow): number[] {
const ranks = [0, 0, 0, 0];
for (const top of window.foundations) {
if (top !== null) ranks[suitOf(top)] = rankOf(top);
}
return ranks;
}
/**
* A card is safe to auto-play when no cascade card could still need it:
* rank 2 or less, or both opposite-color foundations at rank-1 or higher.
*/
function isSafeAutoPlay(window: FreecellWindow, card: number): boolean {
const rank = rankOf(card);
if (rank <= 2) return true;
const ranks = foundationRankBySuit(window);
const oppositeMin = isRed(card) ? Math.min(ranks[0]!, ranks[2]!) : Math.min(ranks[1]!, ranks[3]!);
return oppositeMin >= rank - 1;
}
/** Repeatedly move safe cards (cell or cascade top) to the foundations. */
function autoSafeMoves(window: FreecellWindow): void {
let moved = true;
while (moved) {
moved = false;
for (let location = 0; location < LOC_COUNT; location++) {
if (location >= LOC_FOUNDATION0 && location < LOC_CASCADE0) continue;
const card = topCardAt(window, location);
if (card === null || !isSafeAutoPlay(window, card)) continue;
const pile = foundationIndexFor(window, card);
if (pile < 0) continue;
takeCardFrom(window, location);
window.foundations[pile] = card;
moved = true;
}
}
}
function checkWin(window: FreecellWindow): void {
const done = window.foundations.every((top) => top !== null && rankOf(top) === 13);
if (!done) return;
window.phase = "won";
window.selected = null;
playSfx(window, SFX_WIN);
}
// ---------------------------------------------------------------------------
// Painting
type Layout = {
pitch: number;
marginX: number;
/** Vertical pixels per buried cascade card (its visible label strip). */
strip: (cascadeLength: number) => number;
};
function layoutFor(window: FreecellWindow): Layout {
const pitch = Math.floor((window.viewportWidth - 12) / 8);
const marginX = Math.floor((window.viewportWidth - (pitch * 8 - (pitch - CARD_W))) / 2);
const available = window.viewportHeight - CASCADE_Y - 2;
return {
pitch,
marginX,
strip: (cascadeLength: number) =>
cascadeLength <= 1 ? 0 : Math.min(16, Math.floor((available - CARD_H) / (cascadeLength - 1))),
};
}
function paint(window: FreecellWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: FreecellWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
const layout = layoutFor(window);
const colX = (i: number) => layout.marginX + i * layout.pitch;
// Top row: free cells then foundations, with a divider between the groups.
for (let i = 0; i < 4; i++) {
const card = window.cells[i]!;
if (card === null) {
image.drawRoundedRect(colX(i), TOP_Y, CARD_W, CARD_H, 70, 4);
} else {
paintCard(image, colX(i), TOP_Y, card, window.selected === LOC_CELL0 + i, true);
}
}
image.drawLine(colX(4) - 4, TOP_Y + 4, colX(4) - 4, TOP_Y + CARD_H - 4, 70);
for (let i = 0; i < 4; i++) {
const x = colX(4 + i);
const card = window.foundations[i]!;
if (card === null) {
image.drawRoundedRect(x, TOP_Y, CARD_W, CARD_H, 70, 4);
image.drawText(labelFont, x + Math.round((CARD_W - labelFont.measureText("A")) / 2), TOP_Y + 10, "A", 70);
} else {
paintCard(image, x, TOP_Y, card, window.selected === LOC_FOUNDATION0 + i, true);
}
}
for (let i = 0; i < 8; i++) {
paintCascade(image, window, layout, i);
}
paintCursor(image, window, layout);
if (window.phase === "won") paintWinOverlay(image, window);
return image;
}
function paintCascade(image: GrayImage, window: FreecellWindow, layout: Layout, index: number): void {
const x = layout.marginX + index * layout.pitch;
const cascade = window.cascades[index]!;
if (cascade.length === 0) {
image.drawRoundedRect(x, CASCADE_Y, CARD_W, CARD_H, 45, 4);
return;
}
const strip = layout.strip(cascade.length);
// How many tail cards the pending selection would move (highlighted).
let highlightFrom = cascade.length;
if (window.selected === LOC_CASCADE0 + index) {
const run = Math.min(tailRunLength(cascade), moveCapacity(window, -1));
highlightFrom = cascade.length - run;
}
for (let i = 0; i < cascade.length; i++) {
paintCard(image, x, CASCADE_Y + i * strip, cascade[i]!, i >= highlightFrom, i === cascade.length - 1);
}
}
function paintCard(
image: GrayImage,
x: number,
y: number,
card: number,
selected: boolean,
fullyVisible: boolean,
): void {
const shade = isRed(card) ? RED_SHADE : BLACK_SHADE;
image.fillRoundedRect(x, y, CARD_W, CARD_H, selected ? 60 : 25, 4);
image.drawRoundedRect(x, y, CARD_W, CARD_H, selected ? 255 : 160, 4);
if (selected) image.drawRoundedRect(x + 1, y + 1, CARD_W - 2, CARD_H - 2, 255, 3);
image.drawText(labelFont, x + 5, y + 2, cardLabel(card), shade);
if (fullyVisible) {
image.drawText(mediumFont, x + CARD_W - 18, y + CARD_H - 29, SUIT_CHARS[suitOf(card)]!, shade);
}
}
/** Bright double outline around the cursor's location, minesweeper-style. */
function paintCursor(image: GrayImage, window: FreecellWindow, layout: Layout): void {
if (window.phase !== "playing") return;
let x: number;
let y: number;
let height: number;
if (window.cursor < LOC_CASCADE0) {
x = layout.marginX + (window.cursor % 8) * layout.pitch;
y = TOP_Y;
height = CARD_H;
} else {
const index = window.cursor - LOC_CASCADE0;
const cascade = window.cascades[index]!;
x = layout.marginX + index * layout.pitch;
y = CASCADE_Y;
height = cascade.length === 0 ? CARD_H : (cascade.length - 1) * layout.strip(cascade.length) + CARD_H;
}
image.drawRect(x - 3, y - 3, CARD_W + 6, height + 6, 255);
image.drawRect(x - 2, y - 2, CARD_W + 4, height + 4, 255);
}
function paintWinOverlay(image: GrayImage, window: FreecellWindow): void {
const width = 320;
const height = 120;
const x = Math.round((window.viewportWidth - width) / 2);
const y = Math.round((window.viewportHeight - height) / 2);
image.fillRoundedRect(x, y, width, height, 0, 8);
image.drawRoundedRect(x, y, width, height, 200, 8);
drawCenteredIn(image, largeFont, x, width, y + 14, "You win!", 255);
drawCenteredIn(image, smallFont, x, width, y + 56, `${window.moves} moves`, 170);
drawCenteredIn(image, smallFont, x, width, y + 80, `${GESTURE_CLICK} new game ${GESTURE_DOUBLE_CLICK} leave`, 150);
drawCenteredIn(image, smallFont, x, width, y + 98, `${GESTURE_LONG_PRESS} menu`, 150);
}
function drawCenteredIn(
image: GrayImage,
font: typeof smallFont,
x: number,
width: number,
y: number,
text: string,
value: number,
): void {
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
}
function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: freecell content unchanged");
return;
}
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
0,
0,
image.width,
image.height,
fingerprint,
paintMs,
frameId,
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
frameTimings.finishFrame(frameId, "discarded: freecell render failed");
console.error(`freecell worker render failed: ${error}`);
}
}
+300
View File
@@ -0,0 +1,300 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
} from "../../ui/gestures";
import { DashboardInputEvent, Layer, LayerActions, LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { createInProcessWindow } from "../../ui/shell/in-process-window";
import { getFolderAssignments, getFolders, getFolderStateFingerprint } from "./launcher-folders";
import { shell, type ShellWindow } from "../../ui/shell/shell";
export type LauncherAppEntry = {
appId: string;
label: string;
icon: IconName;
};
export type LauncherOptions = {
actions: LayerActions;
apps: LauncherAppEntry[];
launchApp: (appId: string) => Promise<void> | void;
/** Submit a painted viewport-sized frame to this window's surface. */
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
/** Flip the launcher's compositor surface visibility on foreground changes. */
setSurfaceVisible: (visible: boolean) => void;
};
export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
const COLS = 5;
// Row height is sized so 3.5 rows fit the viewport: three full rows are
// always visible, and a fourth (half-clipped) row peeks in to signal that
// there are more apps to scroll to.
const VISIBLE_ROWS = 3.5;
const FULL_ROWS = Math.floor(VISIBLE_ROWS);
const GRID_TOP = 6;
const FOOTER_HEIGHT = 16;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
/** Extra top inset for the folder-name header while inside a folder. */
const FOLDER_HEADER_HEIGHT = 16;
type LauncherMode = "row" | "item";
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
| { kind: "app"; label: string; icon: IconName; appId: string }
| { kind: "folder"; label: string; name: string };
/**
* The launcher grid: app and folder icons with labels, arranged in a grid.
* Navigation has two levels so the max number of swipes to any app is halved:
* entering from the sidebar starts in "row" mode (scroll picks a row); a tap
* drops into "item" mode on that row, defaulting to the middle column (scroll
* picks the item); a tap launches an app or opens a folder (the same grid,
* restricted to that folder's apps). Double-click backs out one level (item →
* row, folder → top grid), and from the top level yields to the sidebar.
*
* The folder grouping lives in the settings store (see launcher-folders.ts)
* and is re-read every paint, so assistant folder tools take effect without
* the layer holding any copy of the state.
*/
class LauncherGridLayer implements Layer {
private mode: LauncherMode = "row";
private selectedRow = 0;
private selectedCol = 1;
private scrollRow = 0;
/** Folder whose contents the grid is showing, or null for the top grid. */
private currentFolder: string | null = null;
constructor(private readonly options: LauncherOptions) {}
/**
* The cells to show, computed fresh from the folder state. Self-heals
* currentFolder: if the open folder no longer exists (the assistant
* disbanded it), the view falls back to the top grid.
*/
private entries(): LauncherGridEntry[] {
const byAppId = new Map(this.options.apps.map((app) => [app.appId, app]));
if (this.currentFolder !== null) {
const members = (getFolders().get(this.currentFolder) ?? [])
.map((appId) => byAppId.get(appId))
.filter(Boolean) as LauncherAppEntry[];
if (members.length > 0) {
return members
.map((app): LauncherGridEntry => ({ kind: "app", label: app.label, icon: app.icon, appId: app.appId }))
.sort((a, b) => a.label.localeCompare(b.label));
}
this.currentFolder = null;
}
const assignments = getFolderAssignments();
const entries: LauncherGridEntry[] = [];
// A folder cell appears only when it holds at least one known app; stale
// assignments to removed apps are ignored.
for (const [name, members] of getFolders()) {
if (members.some((appId) => byAppId.has(appId))) {
entries.push({ kind: "folder", label: name, name });
}
}
for (const app of this.options.apps) {
if (!assignments[app.appId]) {
entries.push({ kind: "app", label: app.label, icon: app.icon, appId: app.appId });
}
}
return entries.sort((a, b) => a.label.localeCompare(b.label));
}
private rowCount(entryCount: number): number {
return Math.max(1, Math.ceil(entryCount / COLS));
}
private itemsInRow(entryCount: number, row: number): number {
return Math.max(0, Math.min(COLS, entryCount - row * COLS));
}
/** Leave the current folder, putting the selection back on its cell. */
private exitFolder(): void {
const folder = this.currentFolder;
this.currentFolder = null;
this.mode = "row";
const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder);
this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0;
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const focused = ctx.stack.isFocused();
const entries = this.entries();
const rows = this.rowCount(entries.length);
this.selectedRow = clamp(this.selectedRow, 0, rows - 1);
this.selectedCol = clamp(
this.selectedCol,
0,
Math.max(0, this.itemsInRow(entries.length, this.selectedRow) - 1),
);
const gridTop = this.currentFolder !== null ? GRID_TOP + FOLDER_HEADER_HEIGHT : GRID_TOP;
if (this.currentFolder !== null) {
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
const gridBottom = height - FOOTER_HEIGHT;
const rowH = (gridBottom - gridTop) / VISIBLE_ROWS;
const colW = width / COLS;
// Scroll to keep the selected row among the fully-visible rows.
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, FULL_ROWS, rows);
const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH;
// Selection highlight (row band, or a single cell in item mode).
const selY = rowY(this.selectedRow);
if (this.mode === "row") {
drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6);
} else {
drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6);
}
for (let index = 0; index < entries.length; index++) {
const entry = entries[index]!;
const row = Math.floor(index / COLS);
if (row < this.scrollRow) continue;
const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
if (blockTop >= gridBottom) break; // fully below the grid
const centerX = (index % COLS) * colW + colW / 2;
const icon = renderIcon(entry.kind === "app" ? entry.icon : "folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - blockTop));
image.bitBlt(icon, Math.round(centerX - icon.width / 2), Math.round(blockTop), {
height: clipHeight,
transparentZero: true,
});
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
const label = truncateText(font, entry.label, colW - 8);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210);
}
}
const selectedEntry = entries[this.selectedRow * COLS + this.selectedCol];
const pickVerb = selectedEntry?.kind === "folder" ? "open" : "launch";
const hint =
this.mode === "row"
? `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick ${GESTURE_DOUBLE_CLICK} back`
: `${GESTURE_SCROLL} item ${GESTURE_CLICK} ${pickVerb} ${GESTURE_DOUBLE_CLICK} row`;
image.drawText(font, 8, height - 14, hint, 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
const entries = this.entries();
const rows = this.rowCount(entries.length);
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.mode === "row") {
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
} else {
// Item selection traverses the grid linearly: past a row's edge it
// continues onto the adjacent row (stopping at the grid's ends).
const itemCount = this.itemsInRow(entries.length, this.selectedRow);
const next = clamp(this.selectedCol, 0, itemCount - 1) + delta;
if (next >= 0 && next < itemCount) {
this.selectedCol = next;
} else if (next < 0 && this.selectedRow > 0) {
this.selectedRow--;
this.selectedCol = this.itemsInRow(entries.length, this.selectedRow) - 1;
} else if (next >= itemCount && this.selectedRow < rows - 1) {
this.selectedRow++;
this.selectedCol = 0;
}
}
return;
}
case "click": {
if (this.mode === "row") {
this.mode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(
Math.floor(COLS / 2),
this.itemsInRow(entries.length, this.selectedRow) - 1,
);
} else {
const entry = entries[this.selectedRow * COLS + this.selectedCol];
if (!entry) return;
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.mode = "row";
this.selectedRow = 0;
this.scrollRow = 0;
} else {
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.mode = "row";
await this.options.launchApp(entry.appId);
}
}
return;
}
case "double-click":
if (this.mode === "item") {
this.mode = "row";
} else if (this.currentFolder !== null) {
this.exitFolder();
} else {
shell.yieldFocusToSidebar();
}
return;
default:
return;
}
}
}
/**
* The launcher: a pinned, uncloseable in-process window presenting the app
* grid. Selecting an app asks the controller to launch it and foregrounds the
* new window.
*/
export function createLauncherWindow(options: LauncherOptions): ShellWindow {
const created = createInProcessWindow({
appId: "launcher",
windowId: LAUNCHER_WINDOW_ID,
title: "Apps",
iconLetter: "A",
icon: "layout-grid",
closeable: false,
actions: options.actions,
// Not wrapped in YieldAtRootLayer: the grid handles double-click itself to
// back out of item selection before yielding to the sidebar.
baseLayer: new LauncherGridLayer(options),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
});
// The assistant's folder tools change the grouping from outside the window;
// the settings broadcast is the change signal, and the fingerprint check
// keeps every unrelated setting change from repainting the launcher. The
// launcher is pinned for the app's lifetime, so the subscription never needs
// tearing down.
let lastFolderState = getFolderStateFingerprint();
onAnySettingChanged(() => {
const state = getFolderStateFingerprint();
if (state === lastFolderState) return;
lastFolderState = state;
created.requestRender();
});
return created.window;
}
+105
View File
@@ -0,0 +1,105 @@
/**
* Launcher folder state: which apps are grouped into which named folder.
* Stored as one JSON string (appId -> folder name) in the settings store, so
* it persists and its changes broadcast to every isolate. An unset value means
* the default grouping; any edit persists the whole map, so clearing out the
* defaults sticks. Folders exist only while at least one app is assigned to
* them — there is no separate create/delete step.
*/
import { getStringSetting, setStringSetting } from "../../native/settings-store";
const STORAGE_KEY = "launcher.folders";
/** The out-of-the-box grouping, applied only while the setting is unset. */
const DEFAULT_ASSIGNMENTS: Record<string, string> = {
blocks: "Games",
freecell: "Games",
minesweeper: "Games",
pinball: "Games",
};
export const FOLDER_NAME_MAX_LENGTH = 24;
/** appId -> folder name. Malformed stored JSON reads as "no folders". */
export function getFolderAssignments(): Record<string, string> {
const raw = getStringSetting(STORAGE_KEY, "");
if (!raw) return { ...DEFAULT_ASSIGNMENTS };
try {
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
const assignments: Record<string, string> = {};
for (const appId of Object.keys(parsed)) {
const folder = parsed[appId];
if (typeof folder === "string" && folder.trim()) {
assignments[appId] = folder.trim();
}
}
return assignments;
} catch {
return {};
}
}
/** folder name -> appIds (assignment order), folder names sorted. */
export function getFolders(): Map<string, string[]> {
const assignments = getFolderAssignments();
const unsorted = new Map<string, string[]>();
for (const appId of Object.keys(assignments)) {
const folder = assignments[appId]!;
const members = unsorted.get(folder);
if (members) {
members.push(appId);
} else {
unsorted.set(folder, [appId]);
}
}
const folders = new Map<string, string[]>();
for (const name of Array.from(unsorted.keys()).sort((a, b) => a.localeCompare(b))) {
folders.set(name, unsorted.get(name)!);
}
return folders;
}
/** Assign an app to a folder, or (with null) return it to the top level. */
export function setAppFolder(appId: string, folder: string | null): void {
const assignments = getFolderAssignments();
if (folder) {
assignments[appId] = folder;
} else {
delete assignments[appId];
}
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
}
/** Move every app out of a folder. Returns how many apps were moved. */
export function disbandFolder(folder: string): number {
const assignments = getFolderAssignments();
let moved = 0;
for (const appId of Object.keys(assignments)) {
if (assignments[appId] === folder) {
delete assignments[appId];
moved++;
}
}
if (moved > 0) {
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
}
return moved;
}
/**
* Match an existing folder name case-insensitively (so "games" reuses
* "Games" instead of creating a near-duplicate), else the trimmed input.
*/
export function resolveFolderName(input: string): string {
const trimmed = input.trim();
for (const name of getFolders().keys()) {
if (name.toLowerCase() === trimmed.toLowerCase()) return name;
}
return trimmed;
}
/** Raw stored value, for cheap change detection by the launcher window. */
export function getFolderStateFingerprint(): string {
return getStringSetting(STORAGE_KEY, "");
}
@@ -0,0 +1,730 @@
/**
* Minesweeper app worker. One singleton window holds the whole game: a fixed
* 12×9 board with a difficulty setting that varies the mine count. Mines are
* placed on the first reveal (never under or adjacent to it), and the clock
* runs only while the game is live, foreground, and the screen is on.
*
* Controls (in play) are a two-layer selection. Row-select: scroll picks a
* row, click switches to column-select, double-click pauses. Column-select:
* scroll moves along the row, click reveals (or chords a satisfied number),
* long-press toggles a flag, double-click returns to row-select.
* Paused/won/lost: click resumes or starts a new game, double-click yields
* focus, long-press opens the window menu.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_LONG_PRESS,
GESTURE_SCROLL,
} from "../../ui/gestures";
import { clamp } from "../../util/numeric-util";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const smallFont = getDefaultSmallFont();
const COLS = 12;
const ROWS = 9;
const CELL = 26;
const BOARD_X = 14;
const BOARD_Y = 14;
const PANEL_X = BOARD_X + COLS * CELL + 28;
const DIFFICULTIES = [
{ name: "Easy", mines: 14 },
{ name: "Medium", mines: 20 },
{ name: "Hard", mines: 26 },
] as const;
/** Number glyph shades by adjacent-mine count; higher counts read brighter. */
const COUNT_SHADES = [0, 140, 170, 200, 220, 235, 245, 250, 250];
const CELL_HIDDEN = 0;
const CELL_REVEALED = 1;
const CELL_FLAGGED = 2;
/** Buzzer effects, tuned soft like the Blocks ones (shared piezo). */
const SFX_REVEAL: Step[] = [{ freq: 660, duty: 40, ms: 15 }];
const SFX_FLAG: Step[] = [{ freq: 880, duty: 40, ms: 30 }];
const SFX_UNFLAG: Step[] = [{ freq: 550, duty: 40, ms: 30 }];
const SFX_BOOM: Step[] = [
{ freq: 220, duty: 55, ms: 80 },
{ freq: 140, duty: 55, ms: 120 },
{ freq: 90, duty: 60, ms: 320 },
];
const SFX_WIN: Step[] = [
{ freq: 1047, ms: 55 },
{ freq: 1319, ms: 55 },
{ freq: 1568, ms: 55 },
{ freq: 2093, ms: 70 },
{ freq: 1, duty: 0, ms: 30 },
{ freq: 2093, ms: 160 },
];
const SFX_PAUSE: Step[] = [
{ freq: 1319, duty: 40, ms: 60 },
{ freq: 880, duty: 40, ms: 110 },
];
/** Also the new-game and sound-toggled-on confirmation. */
const SFX_RESUME: Step[] = [
{ freq: 880, duty: 40, ms: 60 },
{ freq: 1319, duty: 40, ms: 110 },
];
type GamePhase = "playing" | "paused" | "won" | "lost";
type MinesweeperWindow = {
windowId: string;
surfaceId: string;
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
phase: GamePhase;
difficultyIndex: number;
/** 1 = mine at this cell; empty until the first reveal places the mines. */
mines: Uint8Array;
/** Adjacent-mine counts, precomputed at placement. */
counts: Uint8Array;
/** Per-cell CELL_HIDDEN / CELL_REVEALED / CELL_FLAGGED. */
cellState: Uint8Array;
minesPlaced: boolean;
cursorX: number;
cursorY: number;
/** Two-layer selection: pick a row first, then a cell along it. */
selectMode: "row" | "column";
flagCount: number;
revealedCount: number;
/** The mine that ended the game, painted highlighted; -1 otherwise. */
explodedIndex: number;
/** Clock: accumulated play time plus the live segment since runningSinceMs. */
elapsedMs: number;
runningSinceMs: number | null;
tickTimer: ReturnType<typeof setInterval> | null;
soundOn: boolean;
lastSubmittedFingerprint: string;
};
const windows = new Map<string, MinesweeperWindow>();
let screenOn = true;
function post(message: WorkerAppReply): void {
global.postMessage(message);
}
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "open-window": {
const window: MinesweeperWindow = {
windowId: message.windowId,
surfaceId: message.surfaceId,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
phase: "playing",
difficultyIndex: 0,
mines: new Uint8Array(COLS * ROWS),
counts: new Uint8Array(COLS * ROWS),
cellState: new Uint8Array(COLS * ROWS),
minesPlaced: false,
cursorX: Math.floor(COLS / 2),
cursorY: Math.floor(ROWS / 2),
selectMode: "row",
flagCount: 0,
revealedCount: 0,
explodedIndex: -1,
elapsedMs: 0,
runningSinceMs: null,
tickTimer: null,
soundOn: true,
lastSubmittedFingerprint: "",
};
windows.set(message.windowId, window);
break;
}
case "close-window": {
const window = windows.get(message.windowId);
if (window?.tickTimer) clearInterval(window.tickTimer);
windows.delete(message.windowId);
break;
}
case "input": {
const window = windows.get(message.windowId);
if (!window) {
frameTimings.finishFrame(message.frameId, "discarded: unknown minesweeper window");
break;
}
window.focused = message.focused;
inferForeground(window, message.focused);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
case "render": {
const window = windows.get(message.windowId);
if (!window) break;
window.focused = message.focused;
inferForeground(window, message.focused);
renderAndSubmit(window, 0);
break;
}
case "foreground": {
const window = windows.get(message.windowId);
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
// The player can't see the board while backgrounded, so don't let the
// clock keep running.
if (!window.foreground && window.phase === "playing") window.phase = "paused";
syncClock(window);
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "screen":
screenOn = message.on;
for (const window of windows.values()) {
if (!screenOn && window.phase === "playing") window.phase = "paused";
syncClock(window);
}
break;
}
};
/**
* Input and render messages only ever target the shell's foreground window,
* so a focused message proves this window is foreground. This backstops the
* "foreground" message itself, which can be lost while a freshly spawned
* worker is still evaluating its bundle (see the Blocks worker).
*/
function inferForeground(window: MinesweeperWindow, focused: boolean): void {
if (!focused || window.foreground) return;
window.foreground = true;
syncClock(window);
}
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
*/
function playSfx(window: MinesweeperWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
} catch (error) {
console.warn(`minesweeper sfx failed: ${error}`);
}
}
/** The window's long-press menu (game actions + default entries). */
function openWindowMenu(window: MinesweeperWindow): void {
const nextDifficulty = DIFFICULTIES[(window.difficultyIndex + 1) % DIFFICULTIES.length]!;
windowMenu(window).open([
{
label: "New game",
onSelect: (ctx) => {
ctx.stack.pop();
resetGame(window);
playSfx(window, SFX_RESUME);
},
},
{
label: `Difficulty: ${DIFFICULTIES[window.difficultyIndex]!.name} → ${nextDifficulty.name}`,
onSelect: (ctx) => {
ctx.stack.pop();
window.difficultyIndex = (window.difficultyIndex + 1) % DIFFICULTIES.length;
resetGame(window);
playSfx(window, SFX_RESUME);
},
},
{
label: window.soundOn ? "Sound: on" : "Sound: off",
onSelect: (ctx) => {
ctx.stack.pop();
window.soundOn = !window.soundOn;
if (window.soundOn) playSfx(window, SFX_RESUME);
},
},
...defaultWindowMenuItems(window.windowId, post),
]);
}
function windowMenu(window: MinesweeperWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function handleInput(window: MinesweeperWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`minesweeper menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (window.phase === "playing") {
handlePlayingInput(window, event, frameId);
} else {
handleIdleInput(window, event, frameId);
}
}
function handlePlayingInput(window: MinesweeperWindow, event: DashboardInputEvent, frameId: number): void {
const rowMode = window.selectMode === "row";
switch (event.type) {
case "scroll-up":
if (rowMode) {
window.cursorY = clamp(window.cursorY - 1, 0, ROWS - 1);
} else {
window.cursorX = clamp(window.cursorX - 1, 0, COLS - 1);
}
break;
case "scroll-down":
if (rowMode) {
window.cursorY = clamp(window.cursorY + 1, 0, ROWS - 1);
} else {
window.cursorX = clamp(window.cursorX + 1, 0, COLS - 1);
}
break;
case "click":
if (rowMode) {
window.selectMode = "column";
} else {
revealAtCursor(window);
}
break;
case "long-press":
if (rowMode) {
frameTimings.finishFrame(frameId, "discarded: minesweeper ignored input");
return;
}
toggleFlag(window);
break;
case "double-click":
if (rowMode) {
window.phase = "paused";
syncClock(window);
playSfx(window, SFX_PAUSE);
} else {
window.selectMode = "row";
}
break;
default:
frameTimings.finishFrame(frameId, "discarded: minesweeper ignored input");
return;
}
renderAndSubmit(window, frameId);
}
/** Input while paused, won, or lost. */
function handleIdleInput(window: MinesweeperWindow, event: DashboardInputEvent, frameId: number): void {
switch (event.type) {
case "click":
if (window.phase === "paused") {
window.phase = "playing";
} else {
resetGame(window);
}
syncClock(window);
playSfx(window, SFX_RESUME);
break;
case "double-click":
frameTimings.finishFrame(frameId, "discarded: minesweeper yielded focus");
post({ type: "yield-focus", windowId: window.windowId });
return;
case "long-press":
openWindowMenu(window);
break;
default:
frameTimings.finishFrame(frameId, "discarded: minesweeper ignored input");
return;
}
renderAndSubmit(window, frameId);
}
function resetGame(window: MinesweeperWindow): void {
window.mines.fill(0);
window.counts.fill(0);
window.cellState.fill(CELL_HIDDEN);
window.minesPlaced = false;
window.flagCount = 0;
window.revealedCount = 0;
window.explodedIndex = -1;
window.elapsedMs = 0;
window.runningSinceMs = null;
window.phase = "playing";
window.selectMode = "row";
syncClock(window);
}
function mineCount(window: MinesweeperWindow): number {
return DIFFICULTIES[window.difficultyIndex]!.mines;
}
function forEachNeighbor(x: number, y: number, callback: (nx: number, ny: number) => void): void {
for (let ny = Math.max(0, y - 1); ny <= Math.min(ROWS - 1, y + 1); ny++) {
for (let nx = Math.max(0, x - 1); nx <= Math.min(COLS - 1, x + 1); nx++) {
if (nx !== x || ny !== y) callback(nx, ny);
}
}
}
/** Place mines everywhere except the 3×3 around the first reveal, then count. */
function placeMines(window: MinesweeperWindow, safeX: number, safeY: number): void {
const candidates: number[] = [];
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
if (Math.abs(x - safeX) <= 1 && Math.abs(y - safeY) <= 1) continue;
candidates.push(y * COLS + x);
}
}
for (let i = candidates.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[candidates[i], candidates[j]] = [candidates[j]!, candidates[i]!];
}
for (const index of candidates.slice(0, mineCount(window))) {
window.mines[index] = 1;
}
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
let count = 0;
forEachNeighbor(x, y, (nx, ny) => {
if (window.mines[ny * COLS + nx]) count++;
});
window.counts[y * COLS + x] = count;
}
}
window.minesPlaced = true;
syncClock(window);
}
/** Reveal a cell, flood-filling zero-count regions. Returns true on a mine. */
function revealCell(window: MinesweeperWindow, x: number, y: number): boolean {
const index = y * COLS + x;
if (window.cellState[index] !== CELL_HIDDEN) return false;
if (window.mines[index]) {
window.explodedIndex = index;
loseGame(window);
return true;
}
const stack = [index];
window.cellState[index] = CELL_REVEALED;
window.revealedCount++;
while (stack.length > 0) {
const current = stack.pop()!;
if (window.counts[current] !== 0) continue;
forEachNeighbor(current % COLS, Math.floor(current / COLS), (nx, ny) => {
const neighbor = ny * COLS + nx;
if (window.cellState[neighbor] !== CELL_HIDDEN) return;
window.cellState[neighbor] = CELL_REVEALED;
window.revealedCount++;
stack.push(neighbor);
});
}
return false;
}
function revealAtCursor(window: MinesweeperWindow): void {
const { cursorX, cursorY } = window;
const index = cursorY * COLS + cursorX;
if (window.cellState[index] === CELL_FLAGGED) return;
if (window.cellState[index] === CELL_REVEALED) {
chordAtCursor(window);
return;
}
if (!window.minesPlaced) placeMines(window, cursorX, cursorY);
if (!revealCell(window, cursorX, cursorY)) {
playSfx(window, SFX_REVEAL);
checkWin(window);
}
}
/**
* Chord: clicking a revealed number whose flag count is satisfied reveals its
* remaining hidden neighbors (classic both-buttons shortcut). Wrong flags
* still explode.
*/
function chordAtCursor(window: MinesweeperWindow): void {
const { cursorX, cursorY } = window;
const count = window.counts[cursorY * COLS + cursorX]!;
if (count === 0) return;
let flags = 0;
forEachNeighbor(cursorX, cursorY, (nx, ny) => {
if (window.cellState[ny * COLS + nx] === CELL_FLAGGED) flags++;
});
if (flags !== count) return;
let exploded = false;
let revealedAny = false;
forEachNeighbor(cursorX, cursorY, (nx, ny) => {
if (exploded || window.cellState[ny * COLS + nx] !== CELL_HIDDEN) return;
revealedAny = true;
if (revealCell(window, nx, ny)) exploded = true;
});
if (!exploded && revealedAny) {
playSfx(window, SFX_REVEAL);
checkWin(window);
}
}
function toggleFlag(window: MinesweeperWindow): void {
const index = window.cursorY * COLS + window.cursorX;
if (window.cellState[index] === CELL_REVEALED) return;
if (window.cellState[index] === CELL_FLAGGED) {
window.cellState[index] = CELL_HIDDEN;
window.flagCount--;
playSfx(window, SFX_UNFLAG);
} else {
window.cellState[index] = CELL_FLAGGED;
window.flagCount++;
playSfx(window, SFX_FLAG);
}
}
function checkWin(window: MinesweeperWindow): void {
if (window.revealedCount !== COLS * ROWS - mineCount(window)) return;
window.phase = "won";
// Flag the remaining mines for the player; the counter drops to zero.
for (let index = 0; index < window.mines.length; index++) {
if (window.mines[index] && window.cellState[index] === CELL_HIDDEN) {
window.cellState[index] = CELL_FLAGGED;
}
}
window.flagCount = mineCount(window);
syncClock(window);
playSfx(window, SFX_WIN);
}
function loseGame(window: MinesweeperWindow): void {
window.phase = "lost";
syncClock(window);
playSfx(window, SFX_BOOM);
}
/**
* Keep the clock and the once-a-second repaint interval running exactly when
* play time is accruing: game live (mines placed), foreground, screen on.
*/
function syncClock(window: MinesweeperWindow): void {
const shouldRun =
window.phase === "playing" && window.minesPlaced && window.foreground && screenOn;
const now = Date.now();
if (shouldRun && window.runningSinceMs === null) {
window.runningSinceMs = now;
} else if (!shouldRun && window.runningSinceMs !== null) {
window.elapsedMs += now - window.runningSinceMs;
window.runningSinceMs = null;
}
if (shouldRun && window.tickTimer === null) {
window.tickTimer = setInterval(() => renderAndSubmit(window, 0), 1000);
} else if (!shouldRun && window.tickTimer !== null) {
clearInterval(window.tickTimer);
window.tickTimer = null;
}
}
function elapsedMs(window: MinesweeperWindow): number {
const runningMs = window.runningSinceMs === null ? 0 : Date.now() - window.runningSinceMs;
return window.elapsedMs + runningMs;
}
function paint(window: MinesweeperWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: MinesweeperWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
image.drawRect(BOARD_X - 2, BOARD_Y - 2, COLS * CELL + 4, ROWS * CELL + 4, 120);
if (window.phase === "paused") {
// Hide the board so pausing can't be used to study it off the clock.
drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, BOARD_Y + 80, "PAUSED", 230);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 120, `${GESTURE_CLICK} resume`, 150);
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 140, `${GESTURE_DOUBLE_CLICK} leave`, 150);
} else {
paintBoard(image, window);
}
paintPanel(image, window);
return image;
}
function paintBoard(image: GrayImage, window: MinesweeperWindow): void {
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
paintCell(image, window, x, y);
}
}
// Cursor: a double bright outline so it stays visible over any cell. In
// row-select it spans the whole selected row; in column-select it marks the
// cell (games over show neither).
if (window.phase !== "playing") return;
const cx = BOARD_X + window.cursorX * CELL;
const cy = BOARD_Y + window.cursorY * CELL;
if (window.selectMode === "row") {
image.drawRect(BOARD_X - 1, cy - 1, COLS * CELL + 1, CELL + 1, 255);
image.drawRect(BOARD_X, cy, COLS * CELL - 1, CELL - 1, 255);
} else {
image.drawRect(cx - 1, cy - 1, CELL + 1, CELL + 1, 255);
image.drawRect(cx, cy, CELL - 1, CELL - 1, 255);
}
}
function paintCell(image: GrayImage, window: MinesweeperWindow, x: number, y: number): void {
const index = y * COLS + x;
const cx = BOARD_X + x * CELL;
const cy = BOARD_Y + y * CELL;
const state = window.cellState[index]!;
const isMine = window.mines[index] === 1;
const gameOver = window.phase === "lost";
if (state === CELL_REVEALED || (gameOver && isMine)) {
if (isMine) {
// The fatal mine gets an inverted cell so it stands out.
if (index === window.explodedIndex) image.fillRect(cx, cy, CELL - 1, CELL - 1, 200);
drawMine(image, cx, cy, index === window.explodedIndex ? 1 : 220);
return;
}
image.drawRect(cx, cy, CELL - 1, CELL - 1, 22);
const count = window.counts[index]!;
if (count > 0) {
const digit = String(count);
const dx = cx + Math.round((CELL - 1 - mediumFont.measureText(digit)) / 2);
image.drawText(mediumFont, dx, cy + 1, digit, COUNT_SHADES[count]!);
}
return;
}
// Hidden (or flagged) cell face.
image.fillRect(cx, cy, CELL - 1, CELL - 1, 55);
if (state === CELL_FLAGGED) {
drawFlag(image, cx, cy, 245);
// A flag that turned out wrong gets crossed out at game over.
if (gameOver && !isMine) {
image.drawLine(cx + 3, cy + 3, cx + CELL - 5, cy + CELL - 5, 255);
image.drawLine(cx + CELL - 5, cy + 3, cx + 3, cy + CELL - 5, 255);
}
}
}
function drawFlag(image: GrayImage, cx: number, cy: number, shade: number): void {
image.drawLine(cx + 10, cy + 5, cx + 10, cy + 20, shade);
image.fillRect(cx + 11, cy + 5, 7, 4, shade);
image.fillRect(cx + 11, cy + 9, 4, 3, shade);
image.drawLine(cx + 7, cy + 20, cx + 13, cy + 20, shade);
}
function drawMine(image: GrayImage, cx: number, cy: number, shade: number): void {
const centerX = cx + Math.floor((CELL - 1) / 2);
const centerY = cy + Math.floor((CELL - 1) / 2);
// A rounded blob plus four spikes reads as a mine at this size.
image.fillRoundedRect(centerX - 6, centerY - 6, 13, 13, shade, 6);
image.drawLine(centerX - 9, centerY, centerX + 9, centerY, shade);
image.drawLine(centerX, centerY - 9, centerX, centerY + 9, shade);
image.drawLine(centerX - 7, centerY - 7, centerX + 7, centerY + 7, shade);
image.drawLine(centerX - 7, centerY + 7, centerX + 7, centerY - 7, shade);
}
function paintPanel(image: GrayImage, window: MinesweeperWindow): void {
drawStat(image, PANEL_X, BOARD_Y, "Mines", String(mineCount(window) - window.flagCount));
drawStat(image, PANEL_X, BOARD_Y + 62, "Time", formatElapsed(elapsedMs(window)));
image.drawText(smallFont, PANEL_X, BOARD_Y + 126, DIFFICULTIES[window.difficultyIndex]!.name, 140);
if (window.phase === "won" || window.phase === "lost") {
image.drawText(mediumFont, PANEL_X, BOARD_Y + 152, window.phase === "won" ? "CLEARED!" : "BOOM!", 250);
image.drawText(smallFont, PANEL_X, BOARD_Y + 178, `${GESTURE_CLICK} new game`, 150);
image.drawText(smallFont, PANEL_X, BOARD_Y + 198, `${GESTURE_LONG_PRESS} menu`, 150);
} else if (window.selectMode === "row") {
image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick`, 115);
image.drawText(smallFont, PANEL_X, 222, `${GESTURE_DOUBLE_CLICK} pause`, 115);
} else {
image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} cell ${GESTURE_CLICK} dig`, 115);
image.drawText(smallFont, PANEL_X, 222, `${GESTURE_LONG_PRESS} flag ${GESTURE_DOUBLE_CLICK} rows`, 115);
}
}
function drawStat(image: GrayImage, x: number, y: number, label: string, value: string): void {
image.drawText(smallFont, x, y, label, 140);
image.drawText(largeFont, x, y + 18, value, 235);
}
function formatElapsed(totalMs: number): string {
const totalSeconds = Math.floor(totalMs / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${String(seconds).padStart(2, "0")}`;
}
function drawCenteredIn(
image: GrayImage,
font: typeof smallFont,
x: number,
width: number,
y: number,
text: string,
value: number,
): void {
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
}
function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: minesweeper content unchanged");
return;
}
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
0,
0,
image.width,
image.height,
fingerprint,
paintMs,
frameId,
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
frameTimings.finishFrame(frameId, "discarded: minesweeper render failed");
console.error(`minesweeper worker render failed: ${error}`);
}
}
+304
View File
@@ -0,0 +1,304 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import {
MenuLayer,
drawListScrollbar,
drawSelectionHighlight,
drawSubmenuIndicator,
scrollToKeepSelectionVisible,
} from "../../ui/menu";
import {
mediaBrowserBridge,
type MediaBrowseItem,
type MediaBrowserApp,
} from "../../native/media-browser";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
export type MediaBrowseOptions = {
app: MediaBrowserApp;
/** Called after playback of a picked item has been requested. */
onPlayed: (ctx: LayerContext) => void;
/** Double-click at the library root (leave the browser). */
onLeave: (ctx: LayerContext) => void;
};
type BrowseLevel = {
parentId: string;
title: string;
/** null while the listing is loading. */
items: MediaBrowseItem[] | null;
error: string;
selectedIndex: number;
scrollRow: number;
};
type BrowsePhase = "idle" | "connecting" | "ready" | "failed";
/**
* Library browser over another app's MediaBrowserService (the Android Auto
* mechanism): connecting starts the player's process if needed, so this works
* with no media session active and the phone locked. Scroll to select, click
* to open a folder or play an item, double-click to go up (leaving the browser
* when already at the root).
*/
export class MediaBrowseLayer implements Layer {
private phase: BrowsePhase = "idle";
private connectError = "";
private levels: BrowseLevel[] = [];
private removed = false;
constructor(private readonly options: MediaBrowseOptions) {}
paint(ctx: LayerContext): GrayImage {
if (this.phase === "idle") {
this.startConnect(ctx);
}
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220);
if (this.phase === "connecting" || this.phase === "idle") {
image.drawText(font, 24, HEADER_HEIGHT + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (this.phase === "failed") {
image.drawText(font, 24, HEADER_HEIGHT + 8, "Could not connect:", 180);
image.drawText(font, 24, HEADER_HEIGHT + 24, truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const level = this.currentLevel();
const path = this.levels.map((entry) => entry.title).join(" / ");
image.drawText(font, 20, 22, truncateLeft(font, path, width - 40), 130);
if (!level || level.items === null) {
image.drawText(font, 24, HEADER_HEIGHT + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const items = level.items;
if (!items.length) {
image.drawText(font, 24, HEADER_HEIGHT + 8, "(empty)", 120);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
const rowWidth = width - 2 * LIST_X;
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
for (let index = level.scrollRow; index < lastVisible; index++) {
const item = items[index]!;
const y = HEADER_HEIGHT + (index - level.scrollRow) * ROW_HEIGHT;
const selected = index === level.selectedIndex;
const highlightX = LIST_X - 6;
const highlightY = y - 1;
const highlightWidth = rowWidth + 12;
const highlightHeight = ROW_HEIGHT - 1;
if (selected) {
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
}
const value = selected ? 255 : 200;
const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16);
image.drawText(font, LIST_X, y + 1, title, value);
if (item.subtitle) {
const titleWidth = font.measureText(title);
const subtitleWidth = font.measureText(item.subtitle);
if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) {
image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110);
}
}
if (item.browsable) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110);
}
}
if (items.length > visibleRows) {
drawListScrollbar(
image,
width - 12,
HEADER_HEIGHT,
visibleRows * ROW_HEIGHT - 4,
level.scrollRow,
visibleRows,
items.length,
);
}
image.drawText(
font,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} open/play ${GESTURE_DOUBLE_CLICK} up / back`,
110,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (this.phase === "failed") {
if (event.type === "click") {
this.phase = "idle";
this.connectError = "";
} else if (event.type === "double-click") {
this.leave(ctx);
}
return;
}
if (event.type === "double-click") {
if (this.levels.length > 1) {
this.levels.pop();
} else {
this.leave(ctx);
}
return;
}
const level = this.currentLevel();
const items = level?.items;
if (!level || !items || !items.length) return;
switch (event.type) {
case "scroll-up":
level.selectedIndex = Math.max(0, level.selectedIndex - 1);
return;
case "scroll-down":
level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1);
return;
case "click": {
const item = items[clamp(level.selectedIndex, 0, items.length - 1)];
if (!item) return;
if (item.browsable && item.playable) {
ctx.stack.push(
new MenuLayer(truncateRight(getDefaultSmallFont(), item.title || "(untitled)", 230), [
{
label: "Play",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.play(menuCtx, item);
},
},
{
label: "Open",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.descendInto(menuCtx, item);
},
},
]),
);
} else if (item.browsable) {
this.descendInto(ctx, item);
} else if (item.playable) {
this.play(ctx, item);
}
return;
}
default:
return;
}
}
onRemoved(): void {
this.removed = true;
mediaBrowserBridge.disconnect();
}
private currentLevel(): BrowseLevel | undefined {
return this.levels[this.levels.length - 1];
}
private leave(ctx: LayerContext): void {
this.options.onLeave(ctx);
}
private play(ctx: LayerContext, item: MediaBrowseItem): void {
mediaBrowserBridge.playFromMediaId(item.mediaId);
this.options.onPlayed(ctx);
}
private descendInto(ctx: LayerContext, item: MediaBrowseItem): void {
const level: BrowseLevel = {
parentId: item.mediaId,
title: item.title || "(untitled)",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels.push(level);
this.loadLevel(ctx, level);
}
private startConnect(ctx: LayerContext): void {
this.phase = "connecting";
mediaBrowserBridge
.connect(this.options.app)
.then((rootId) => {
if (this.removed) return;
this.phase = "ready";
const root: BrowseLevel = {
parentId: rootId,
title: "Library",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels = [root];
this.loadLevel(ctx, root);
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
this.phase = "failed";
this.connectError = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
private loadLevel(ctx: LayerContext, level: BrowseLevel): void {
mediaBrowserBridge
.browse(level.parentId)
.then((items) => {
if (this.removed) return;
level.items = items;
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
level.error = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
out = out.slice(1);
}
return `...${out}`;
}
+494
View File
@@ -0,0 +1,494 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
GESTURE_SCROLL_DOWN,
GESTURE_SCROLL_UP,
} from "../../ui/gestures";
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu";
import {
mediaControllerBridge,
type MediaControllerState,
type MediaQueueItem,
} from "../../native/media-controller";
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
import { MediaBrowseLayer } from "./media-browse";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
export const MUSIC_WINDOW_ID = "music";
export const MUSIC_SURFACE_ID = "window:music";
const ART_SIZE = 96;
const ART_X = 22;
const ART_Y = 8;
const META_X = ART_X + ART_SIZE + 14;
const LIST_TOP = 120;
const ROW_HEIGHT = 16;
const ACTION_X = 22;
const ACTION_WIDTH = 154;
const COLUMN_DIVIDER_X = 190;
const QUEUE_X = 204;
const FOOTER_HEIGHT = 20;
const PLAYLIST_ACTION_INDEX = 1;
type MusicAction =
| { kind: "play-pause"; label: string; enabled: boolean }
| { kind: "previous"; label: string; enabled: boolean }
| { kind: "next"; label: string; enabled: boolean }
| { kind: "volume"; label: string; enabled: boolean }
| { kind: "playlist"; label: string; enabled: boolean }
| { kind: "browse"; label: string; enabled: boolean };
type FocusColumn = "actions" | "playlist";
/**
* Music controller app: metadata + album art + transport controls for the
* active Android media session (any player that publishes one), plus the
* player's queue when it exposes one (scroll to a track, click to jump).
*/
class MusicAppLayer implements Layer {
private focusColumn: FocusColumn = "actions";
private selectedActionIndex = 0;
private selectedQueueIndex = 0;
private queueScrollRow = 0;
private art: GrayImage | null = null;
private artKey = "";
isPlaylistFocused(): boolean {
return this.focusColumn === "playlist";
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const media = mediaControllerBridge.snapshot();
if (!media.accessEnabled) {
const lines = wrapText(
font,
"Notification access is required before Android exposes media sessions. Click to open settings.",
width - 48,
);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 16 + index * 14, lines[index]!, 180);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (!media.available) {
image.drawText(font, 24, 16, "No active media session.", 180);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 34, "Click to browse a music app's library,", 150);
image.drawText(font, 24, 48, "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse ${GESTURE_DOUBLE_CLICK} back`, 110);
} else {
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
}
return image;
}
this.drawArt(image, media);
const metaWidth = width - META_X - 24;
const titleLines = wrapText(font, media.title || "Unknown title", metaWidth).slice(0, 2);
for (let index = 0; index < titleLines.length; index++) {
image.drawText(font, META_X, ART_Y + 2 + index * 15, titleLines[index]!, 230);
}
image.drawText(font, META_X, ART_Y + 36, media.artist || "Unknown artist", 180);
if (media.album) {
image.drawText(font, META_X, ART_Y + 52, truncateText(font, media.album, metaWidth), 150);
}
image.drawText(font, META_X, ART_Y + 68, truncateText(font, media.appName || media.packageName, metaWidth), 110);
this.drawProgress(image, media, metaWidth);
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
if (this.selectedQueueIndex < this.queueScrollRow) {
this.queueScrollRow = this.selectedQueueIndex;
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
}
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
for (let index = 0; index < actions.length; index++) {
const action = actions[index]!;
const y = LIST_TOP + index * ROW_HEIGHT;
const selected = index === this.selectedActionIndex;
const highlightX = ACTION_X - 6;
const highlightY = y - 1;
const highlightWidth = ACTION_WIDTH + 8;
const highlightHeight = ROW_HEIGHT - 1;
if (selected) {
drawSelectionHighlight(
image,
highlightX,
highlightY,
highlightWidth,
highlightHeight,
ctx.stack.isFocused() && this.focusColumn === "actions",
4,
);
}
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value);
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
}
}
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - FOOTER_HEIGHT - 3, 45);
if (!queue.length) {
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
} else {
const queueWidth = width - QUEUE_X - 20;
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
for (let index = this.queueScrollRow; index < lastVisible; index++) {
const item = queue[index]!;
const y = LIST_TOP + (index - this.queueScrollRow) * ROW_HEIGHT;
const selected = index === this.selectedQueueIndex;
if (selected) {
drawSelectionHighlight(
image,
QUEUE_X - 6,
y - 1,
queueWidth + 8,
ROW_HEIGHT - 1,
ctx.stack.isFocused() && this.focusColumn === "playlist",
4,
);
}
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
}
if (queue.length > visibleRows) {
const trackHeight = visibleRows * ROW_HEIGHT - 4;
const trackX = width - 12;
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
const maxScrollRow = queue.length - visibleRows;
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
}
}
const backTarget = this.focusColumn === "playlist" ? "actions" : "back";
image.drawText(
font,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} ${backTarget}`,
110,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click" && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
return;
}
const media = mediaControllerBridge.snapshot();
if (!media.accessEnabled) {
if (event.type === "click") {
mediaControllerBridge.openNotificationAccessSettings();
} else if (event.type === "double-click") {
ctx.stack.pop();
}
return;
}
if (!media.available) {
if (event.type === "click") {
this.openBrowse(ctx);
}
return;
}
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
switch (event.type) {
case "scroll-up":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length;
}
return;
case "scroll-down":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length;
}
return;
case "click": {
if (this.focusColumn === "playlist") {
const item = queue[this.selectedQueueIndex];
if (item) {
await mediaControllerBridge.skipToQueueItem(item.id);
// The player rebuilds the queue with the chosen track at index 0.
this.selectedQueueIndex = 0;
this.queueScrollRow = 0;
}
return;
}
const action = actions[this.selectedActionIndex];
if (!action || !action.enabled) return;
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
else if (action.kind === "next") await mediaControllerBridge.skipNext();
else if (action.kind === "browse") this.openBrowse(ctx);
else if (action.kind === "volume") {
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
} else if (action.kind === "playlist") {
const activeIndex = queue.findIndex((item) => item.active);
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
this.focusColumn = "playlist";
}
return;
}
default:
return;
}
}
/**
* Open the media-library browser (the Android Auto MediaBrowserService
* path): with one browsable player installed go straight to its library,
* otherwise offer a picker first.
*/
private openBrowse(ctx: LayerContext): void {
const apps = mediaBrowserBridge.listBrowsableApps(true);
if (!apps.length) return;
const pushBrowser = (target: LayerContext, app: MediaBrowserApp) => {
target.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
};
if (apps.length === 1) {
pushBrowser(ctx, apps[0]!);
return;
}
ctx.stack.push(
new MenuLayer(
"Browse library",
apps.map((app) => ({
label: app.appName,
onSelect: (menuCtx: LayerContext) => {
menuCtx.stack.pop();
pushBrowser(menuCtx, app);
},
})),
),
);
}
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
const volume = mediaControllerBridge.getMediaVolumePercent();
return [
{
kind: "play-pause",
label: media.playbackState === "playing" ? "Pause" : "Play",
enabled: media.canPlayPause,
},
{ kind: "playlist", label: "Playlist", enabled: queue.length > 0 },
{ kind: "browse", label: "Browse library", enabled: mediaBrowserBridge.listBrowsableApps().length > 0 },
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
];
}
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
if (!queue.length && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
}
}
private drawProgress(image: GrayImage, media: MediaControllerState, width: number): void {
if (media.durationMs <= 0 || media.positionMs < 0) return;
const font = getDefaultSmallFont();
const y = ART_Y + 84;
const elapsed = formatMediaTime(media.positionMs);
const duration = formatMediaTime(media.durationMs);
image.drawText(font, META_X, y, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), y, duration, 140);
const barY = ART_Y + 100;
image.drawRect(META_X, barY, width, 5, 55);
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
image.fillRect(META_X + 1, barY + 1, Math.round((width - 2) * progress), 3, 170);
}
private drawArt(image: GrayImage, media: MediaControllerState): void {
const key = `${media.packageName}|${media.title}|${media.album}`;
if (key !== this.artKey) {
this.artKey = key;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
}
if (this.art) {
// Center within the art box.
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
const dy = ART_Y + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
image.bitBlt(this.art, dx, dy);
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
} else {
image.drawRect(ART_X, ART_Y, ART_SIZE, ART_SIZE, 60);
const font = getDefaultSmallFont();
image.drawText(font, ART_X + 22, ART_Y + ART_SIZE / 2 - 7, "no art", 90);
}
}
}
/** Centered volume control overlay; scroll gestures adjust a 0..100 target by two. */
class VolumeModalLayer implements Layer {
private volume: number;
constructor(initialVolume: number) {
this.volume = clamp(initialVolume, 0, 100);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const image = paintBelow();
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const { width, height } = ctx.stack.getBaseSize();
const boxWidth = 340;
const boxHeight = 150;
const x = ((width - boxWidth) / 2) | 0;
const y = ((height - boxHeight) / 2) | 0;
image.fillRoundedRect(x, y, boxWidth, boxHeight, 1, 8);
image.drawRoundedRect(x, y, boxWidth, boxHeight, 95, 8);
image.drawText(small, x + 18, y + 14, "Media volume", 180);
const value = `${this.volume} / 100`;
const valueX = x + (((boxWidth - medium.measureText(value)) / 2) | 0);
image.drawText(medium, valueX, y + 45, value, 245);
const barX = x + 28;
const barY = y + 83;
const barWidth = boxWidth - 56;
image.drawRect(barX, barY, barWidth, 9, 65);
image.fillRect(barX + 1, barY + 1, Math.round((barWidth - 2) * this.volume / 100), 7, 175);
const hint = `${GESTURE_SCROLL_UP} +2 ${GESTURE_SCROLL_DOWN} -2 ${GESTURE_DOUBLE_CLICK} done`;
const hintX = x + (((boxWidth - small.measureText(hint)) / 2) | 0);
image.drawText(small, hintX, y + 116, hint, 120);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "scroll-up") {
this.adjust(2);
} else if (event.type === "scroll-down") {
this.adjust(-2);
} else if (event.type === "double-click") {
ctx.stack.pop();
}
}
private adjust(delta: number): void {
this.volume = clamp(this.volume + delta, 0, 100);
mediaControllerBridge.setMediaVolumePercent(this.volume);
}
}
/** Let back leave the playlist column before the root wrapper yields to the shell. */
class MusicRootLayer implements Layer {
private readonly yieldAtRoot: YieldAtRootLayer;
constructor(private readonly music: MusicAppLayer) {
this.yieldAtRoot = new YieldAtRootLayer(music);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
return this.yieldAtRoot.paint(ctx, paintBelow);
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click" && this.music.isPlaylistFocused()) {
await this.music.handleInput(event, ctx);
return;
}
await this.yieldAtRoot.handleInput(event, ctx);
}
onRemoved(): void {
this.yieldAtRoot.onRemoved();
}
}
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
let unsubscribe: (() => void) | null = null;
let progressTimer: ReturnType<typeof setInterval> | null = null;
const musicLayer = new MusicAppLayer();
const stopProgressTimer = () => {
if (progressTimer !== null) {
clearInterval(progressTimer);
progressTimer = null;
}
};
const app = createInProcessWindow({
appId: "music",
windowId: MUSIC_WINDOW_ID,
title: "Music",
iconLetter: "M",
icon: "music",
closeable: true,
actions: options.actions,
baseLayer: new MusicRootLayer(musicLayer),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
stopProgressTimer();
unsubscribe?.();
unsubscribe = null;
options.onClosed();
},
});
unsubscribe = mediaControllerBridge.onStateChange((state) => {
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
progressTimer ??= setInterval(() => app.requestRender(), 1_000);
} else {
stopProgressTimer();
}
app.requestRender();
});
return app;
}
function formatMediaTime(milliseconds: number): string {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
return hours > 0
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
: `${minutes}:${String(seconds).padStart(2, "0")}`;
}
+184
View File
@@ -0,0 +1,184 @@
/**
* Maneuver arrow glyphs for turn-by-turn guidance, drawn with GrayImage
* primitives from the Directions maneuver type + modifier (no image assets).
*/
import { GrayImage } from "../../graphics/image";
/**
* Draw the glyph for a maneuver into a size x size box at (x, y).
* Roundabout exit numbers are the caller's job (drawn as text next to it).
*/
export function drawManeuverGlyph(
image: GrayImage,
x: number,
y: number,
size: number,
maneuverType: string,
modifier: string,
value = 245,
): void {
const type = maneuverType.toLowerCase();
if (type === "arrive") {
drawArriveFlag(image, x, y, size, value);
return;
}
if (type === "roundabout" || type === "rotary" || type === "roundabout turn" || type === "exit roundabout" || type === "exit rotary") {
drawRoundabout(image, x, y, size, value);
return;
}
drawArrow(image, x, y, size, modifier.toLowerCase(), value);
}
/** Normalized path points (0..1 box coords, y down) for each arrow shape. */
function arrowPath(modifier: string): Array<[number, number]> {
switch (modifier) {
case "uturn":
return [
[0.68, 0.92],
[0.68, 0.42],
[0.62, 0.28],
[0.5, 0.22],
[0.38, 0.28],
[0.32, 0.42],
[0.32, 0.88],
];
case "sharp left":
return [
[0.6, 0.92],
[0.6, 0.42],
[0.2, 0.72],
];
case "left":
return [
[0.62, 0.92],
[0.62, 0.42],
[0.14, 0.42],
];
case "slight left":
return [
[0.56, 0.92],
[0.56, 0.5],
[0.32, 0.16],
];
case "sharp right":
return [
[0.4, 0.92],
[0.4, 0.42],
[0.8, 0.72],
];
case "right":
return [
[0.38, 0.92],
[0.38, 0.42],
[0.86, 0.42],
];
case "slight right":
return [
[0.44, 0.92],
[0.44, 0.5],
[0.68, 0.16],
];
case "straight":
default:
return [
[0.5, 0.92],
[0.5, 0.12],
];
}
}
function drawArrow(image: GrayImage, x: number, y: number, size: number, modifier: string, value: number): void {
const path = arrowPath(modifier).map(([px, py]): [number, number] => [x + px * size, y + py * size]);
const thickness = Math.max(2, Math.round(size / 14));
for (let i = 1; i < path.length; i++) {
drawThickLine(image, path[i - 1]!, path[i]!, thickness, value);
}
const tip = path[path.length - 1]!;
const prev = path[path.length - 2]!;
drawArrowHead(image, prev, tip, Math.max(5, size * 0.22), thickness, value);
}
function drawRoundabout(image: GrayImage, x: number, y: number, size: number, value: number): void {
const cx = x + size * 0.5;
const cy = y + size * 0.58;
const radius = size * 0.24;
const thickness = Math.max(2, Math.round(size / 14));
// Circle from line segments.
const segments = 20;
let last: [number, number] | null = null;
for (let i = 0; i <= segments; i++) {
const angle = (i / segments) * Math.PI * 2;
const point: [number, number] = [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
if (last) drawThickLine(image, last, point, thickness, value);
last = point;
}
// Exit arrow upward out of the circle.
const from: [number, number] = [cx, cy - radius];
const to: [number, number] = [cx, y + size * 0.08];
drawThickLine(image, from, to, thickness, value);
drawArrowHead(image, from, to, Math.max(5, size * 0.2), thickness, value);
}
function drawArriveFlag(image: GrayImage, x: number, y: number, size: number, value: number): void {
const poleX = Math.round(x + size * 0.36);
const top = Math.round(y + size * 0.12);
const bottom = Math.round(y + size * 0.92);
const thickness = Math.max(2, Math.round(size / 16));
drawThickLine(image, [poleX, top], [poleX, bottom], thickness, value);
// Flag: two stacked rows of alternating blocks reads as checkered at 4bpp.
const flagWidth = Math.round(size * 0.42);
const rowHeight = Math.max(3, Math.round(size * 0.11));
const blocks = 3;
const blockWidth = Math.max(3, Math.round(flagWidth / blocks));
for (let row = 0; row < 2; row++) {
for (let block = 0; block < blocks; block++) {
const bright = (row + block) % 2 === 0;
image.fillRect(
poleX + thickness + block * blockWidth,
top + row * rowHeight,
blockWidth,
rowHeight,
bright ? value : Math.round(value * 0.35),
);
}
}
}
function drawArrowHead(
image: GrayImage,
from: [number, number],
tip: [number, number],
headLength: number,
thickness: number,
value: number,
): void {
const angle = Math.atan2(tip[1] - from[1], tip[0] - from[0]);
const spread = Math.PI / 5.5;
for (const side of [-1, 1]) {
const barbAngle = angle + Math.PI + side * spread;
const barb: [number, number] = [
tip[0] + headLength * Math.cos(barbAngle),
tip[1] + headLength * Math.sin(barbAngle),
];
drawThickLine(image, tip, barb, thickness, value);
}
}
function drawThickLine(
image: GrayImage,
from: [number, number],
to: [number, number],
thickness: number,
value: number,
): void {
// Stamp a small square along the line; plenty at glyph scale.
const dx = to[0] - from[0];
const dy = to[1] - from[1];
const steps = Math.max(1, Math.ceil(Math.max(Math.abs(dx), Math.abs(dy))));
const half = Math.floor(thickness / 2);
for (let i = 0; i <= steps; i++) {
const px = Math.round(from[0] + (dx * i) / steps);
const py = Math.round(from[1] + (dy * i) / steps);
image.fillRect(px - half, py - half, thickness, thickness, value);
}
}
+892
View File
@@ -0,0 +1,892 @@
/**
* Navigate app worker: voice/tool-set destination, Mapbox routing, and
* turn-by-turn guidance with a live map pane.
*
* The BLE frame budget shapes the layout (see notes/navigate-app-design.md):
* the map bitmap (left pane) refreshes on a slow staleness policy, while the
* guidance text (right pane) updates per GPS fix and ships as small deltas.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { ToolSpec, ToolResult } from "../../assistant/tool-registry";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
import {
fetchRoute,
fetchStaticMapGray,
geocodeForward,
isMapboxConfigured,
type GeocodeCandidate,
type Route,
type RouteProfile,
type StaticMapCamera,
} from "../../native/mapbox";
import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower";
import { drawManeuverGlyph } from "./maneuver-icons";
declare const global: any;
declare const com: any;
const MAP_SIZE = 260;
const PANEL_X = MAP_SIZE + 10;
/** Map refetch policy knobs (design doc: never per-fix, only when stale). */
const BEARING_BUCKET_DEG = 15;
const MOVE_REFRESH_FRACTION = 0.15;
const IDLE_REFRESH_MS = 5_000;
const IDLE_REFRESH_MIN_MOVE_M = 10;
const MAP_RETRY_BACKOFF_MS = 5_000;
const REROUTE_MIN_INTERVAL_MS = 20_000;
const FIRST_FIX_TIMEOUT_MS = 10_000;
const LOCATION_INTERVAL_MS = 1_000;
/** Above this speed a GPS bearing is trustworthy for heading-up. */
const BEARING_MIN_SPEED_MPS = 2.5;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const smallFont = getDefaultSmallFont();
type NavPhase = "idle" | "acquiring" | "routing" | "navigating" | "arrived";
type MapMode = "follow" | "overview";
type NavWindow = {
windowId: string;
surfaceId: string;
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
focused: boolean;
menu: WindowMenu | null;
lastSubmittedFingerprint: string;
};
const NAVIGATE_TOOLS: ToolSpec[] = [
{
name: "start_route",
description:
"Start turn-by-turn navigation to a destination. Give the destination as free text (a place name, business, or address); it is resolved near the user's current location. Returns the resolved destination, distance, and estimated time. If the wrong place was picked, call again with a more specific query.",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "Destination to search for, e.g. 'Golden Gate Park' or '410 Townsend St'." },
profile: {
type: "string",
enum: ["driving", "walking", "cycling"],
description: "Travel mode (default driving).",
},
},
required: ["query"],
additionalProperties: false,
},
availability: "open",
// Under the 15 s host backstop; first GPS fix + two API calls can be slow.
timeoutMs: 14_000,
},
{
name: "stop_route",
description: "Stop the current navigation session.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
availability: "open",
},
{
name: "route_status",
description:
"Get the current navigation status: next maneuver, distance to it, remaining distance/time, and ETA.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
availability: "open",
},
];
// Singleton window; navigation state outlives foreground/background flips.
let window: NavWindow | null = null;
let screenOn = true;
let phase: NavPhase = "idle";
let statusMessage = "";
let destinationName = "";
let destinationPlace = "";
let destination: { longitude: number; latitude: number } | null = null;
let profile: RouteProfile = "driving";
let follower: RouteFollower | null = null;
let progress: RouteProgress | null = null;
let lastFix: TrackedLocation | null = null;
let lastBearingDeg = 0;
let lastRerouteAtMs = 0;
let rerouteInFlight = false;
/** Bumped on every new route/mode so stale map fetches can be discarded. */
let routeGeneration = 0;
let mapMode: MapMode = "follow";
let zoomOffset = 0;
let mapImage: GrayImage | null = null;
let mapFetchedKey = "";
let mapInFlight = false;
let mapLastFetchAtMs = 0;
let mapLastFetchCenter: [number, number] | null = null;
let mapNextRetryAtMs = 0;
let mapLastError = "";
let tickTimer: ReturnType<typeof setInterval> | null = null;
let firstFixWaiters: Array<(fix: TrackedLocation) => void> = [];
const tracker = new LocationTracker({
onLocation: (fix) => handleFix(fix),
onError: (message) => {
statusMessage = message;
if (phase === "acquiring") phase = "idle";
render();
},
});
function post(message: WorkerAppReply): void {
global.postMessage(message);
}
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "open-window":
window = {
windowId: message.windowId,
surfaceId: message.surfaceId,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
lastSubmittedFingerprint: "",
};
post({ type: "set-tools", windowId: message.windowId, tools: NAVIGATE_TOOLS });
break;
case "close-window":
stopNavigation("");
window = null;
break;
case "input":
if (!window || window.windowId !== message.windowId) {
frameTimings.finishFrame(message.frameId, "discarded: unknown navigate window");
break;
}
window.focused = message.focused;
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
case "text-input":
// Voice input / typed text sets a destination directly.
if (message.text.trim()) {
void startNavigation(message.text.trim(), profile).catch(() => {});
}
break;
case "render":
if (!window) break;
window.focused = message.focused;
render();
break;
case "foreground":
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
if (window.foreground) {
maybeRefreshMap();
render();
}
break;
case "screen":
screenOn = message.on;
if (screenOn && window?.foreground) {
maybeRefreshMap();
render();
}
break;
case "tool-call": {
const callId = message.callId;
Promise.resolve(handleNavigateTool(message.name, message.args))
.then((result) => post({ type: "tool-result", callId, result }))
.catch((error) =>
post({
type: "tool-result",
callId,
result: { ok: false, error: String((error as Error)?.message ?? error) },
}),
);
break;
}
}
};
// ---------------------------------------------------------------------------
// Navigation state machine
async function startNavigation(query: string, requestedProfile: RouteProfile): Promise<string> {
if (!isMapboxConfigured()) {
statusMessage = "Set a Mapbox token in Settings > API Keys.";
phase = "idle";
render();
throw new Error(statusMessage);
}
const hadActiveRoute = phase === "navigating" && follower !== null;
const previous = {
destination,
destinationName,
destinationPlace,
profile,
};
try {
profile = requestedProfile;
phase = "acquiring";
statusMessage = `Locating you for "${query}"...`;
render();
ensureTracking();
const fix = await waitForFix();
phase = "routing";
statusMessage = `Finding ${query}...`;
render();
const candidates = await geocodeForward(query, fix, 5);
if (!candidates.length) {
throw new Error(`No places found matching "${query}".`);
}
const picked = candidates[0]!;
destination = { longitude: picked.longitude, latitude: picked.latitude };
destinationName = picked.name;
destinationPlace = picked.placeFormatted;
statusMessage = `Routing to ${picked.name}...`;
render();
const route = await fetchRoute(fix, destination, profile);
adoptRoute(route);
if (window) post({ type: "focus-window", windowId: window.windowId });
return startSummary(picked, candidates, route);
} catch (error) {
const message = String((error as Error)?.message ?? error);
statusMessage = message;
// A failed new destination shouldn't kill guidance that was already
// running; fall back to the ongoing route (and its destination, which
// the geocode step may have partially overwritten).
if (hadActiveRoute) {
phase = "navigating";
destination = previous.destination;
destinationName = previous.destinationName;
destinationPlace = previous.destinationPlace;
profile = previous.profile;
} else {
phase = "idle";
}
stopTrackingIfIdle();
render();
throw error;
}
}
function adoptRoute(route: Route): void {
follower = new RouteFollower(route);
progress = null;
phase = "navigating";
mapMode = "follow";
zoomOffset = 0;
statusMessage = "";
routeGeneration++;
resetMapState();
ensureTracking();
ensureTickTimer();
if (lastFix) handleFix(lastFix);
maybeRefreshMap();
render();
}
function stopNavigation(finalStatus: string): void {
phase = "idle";
statusMessage = finalStatus;
follower = null;
progress = null;
destination = null;
destinationName = "";
destinationPlace = "";
routeGeneration++;
resetMapState();
stopTrackingIfIdle();
ensureTickTimer();
}
function handleFix(fix: TrackedLocation): void {
lastFix = fix;
if (fix.bearingDeg !== null && (fix.speedMps ?? 0) >= BEARING_MIN_SPEED_MPS) {
lastBearingDeg = fix.bearingDeg;
}
for (const waiter of firstFixWaiters.splice(0)) waiter(fix);
if (phase !== "navigating" || !follower) return;
progress = follower.update(fix.longitude, fix.latitude);
if (progress.arrived) {
phase = "arrived";
statusMessage = "";
stopTrackingIfIdle();
ensureTickTimer();
render();
return;
}
if (progress.offRoute) {
void maybeReroute(fix);
}
maybeRefreshMap();
render();
}
async function maybeReroute(fix: TrackedLocation): Promise<void> {
if (rerouteInFlight || !destination) return;
if (Date.now() - lastRerouteAtMs < REROUTE_MIN_INTERVAL_MS) return;
rerouteInFlight = true;
lastRerouteAtMs = Date.now();
statusMessage = "Rerouting...";
render();
try {
const route = await fetchRoute(fix, destination, profile);
if (phase === "navigating") adoptRoute(route);
} catch (error) {
statusMessage = `Reroute failed: ${String((error as Error)?.message ?? error)}`;
render();
} finally {
rerouteInFlight = false;
}
}
function ensureTracking(): void {
if (!tracker.isRunning()) tracker.start(LOCATION_INTERVAL_MS);
}
function stopTrackingIfIdle(): void {
if (phase === "idle" || phase === "arrived") {
tracker.stop();
firstFixWaiters = [];
}
}
function waitForFix(): Promise<TrackedLocation> {
if (lastFix && Date.now() - lastFix.timestampMs < 60_000) {
return Promise.resolve(lastFix);
}
return new Promise<TrackedLocation>((resolve, reject) => {
const timer = setTimeout(() => {
firstFixWaiters = firstFixWaiters.filter((waiter) => waiter !== onFix);
reject(new Error("Couldn't get a GPS fix yet; try again in a moment."));
}, FIRST_FIX_TIMEOUT_MS);
const onFix = (fix: TrackedLocation) => {
clearTimeout(timer);
resolve(fix);
};
firstFixWaiters.push(onFix);
});
}
function ensureTickTimer(): void {
const shouldRun = phase === "navigating";
if (shouldRun && tickTimer === null) {
tickTimer = setInterval(() => {
maybeRefreshMap();
render(); // ETA/minute updates; fingerprint dedup keeps the wire quiet.
}, 2_000);
} else if (!shouldRun && tickTimer !== null) {
clearInterval(tickTimer);
tickTimer = null;
}
}
// ---------------------------------------------------------------------------
// Map pane
function resetMapState(): void {
mapImage = null;
mapFetchedKey = "";
mapInFlight = false;
mapLastFetchAtMs = 0;
mapLastFetchCenter = null;
mapNextRetryAtMs = 0;
mapLastError = "";
}
function currentPosition(): [number, number] | null {
if (progress) return progress.snapped;
if (lastFix) return [lastFix.longitude, lastFix.latitude];
return null;
}
function currentBearing(): number {
if (lastFix && lastFix.bearingDeg !== null && (lastFix.speedMps ?? 0) >= BEARING_MIN_SPEED_MPS) {
return lastFix.bearingDeg;
}
if (lastBearingDeg) return lastBearingDeg;
// Fall back to the direction the route heads from here.
if (follower && progress) {
const slice = follower.routeSliceAround(progress.alongMeters, 0, 120);
if (slice.length >= 2) return bearingDegrees(slice[0]!, slice[slice.length - 1]!);
}
return 0;
}
function currentZoom(): number {
let zoom: number;
if (profile === "walking") {
zoom = 16.5;
} else if (progress && progress.metersToNextManeuver < 250) {
zoom = 16.5;
} else if (progress && progress.metersToNextManeuver < 1000) {
zoom = 15.5;
} else {
zoom = 14.5;
}
return Math.max(11, Math.min(18, zoom + zoomOffset));
}
function metersPerPixel(zoom: number, latitude: number): number {
return (156543.03392 * Math.cos((latitude * Math.PI) / 180)) / Math.pow(2, zoom);
}
function desiredMapView(): { camera: StaticMapCamera; key: string; path: Array<[number, number]> } | null {
if (mapMode === "overview" && follower) {
return {
camera: { kind: "auto" },
key: `auto|${routeGeneration}`,
path: simplifyPath(follower.route.coordinates, 100),
};
}
const position = currentPosition();
if (!position) return null;
const zoom = currentZoom();
const bearing = Math.round(currentBearing() / BEARING_BUCKET_DEG) * BEARING_BUCKET_DEG;
const path =
follower && progress
? simplifyPath(follower.routeSliceAround(progress.alongMeters, 250, 3000), 80)
: [];
return {
camera: { kind: "center", longitude: position[0], latitude: position[1], zoom, bearingDeg: bearing },
key: `c|${zoom.toFixed(2)}|${bearing}|${routeGeneration}`,
path,
};
}
/**
* Refetch the map only when meaningfully stale: camera key changed (zoom /
* bearing bucket / mode / route), moved a good fraction of the viewport, or
* a few seconds passed while moving. Single-flight with failure backoff.
*/
function maybeRefreshMap(): void {
if (!window || !window.foreground || !screenOn) return;
if (mapInFlight || Date.now() < mapNextRetryAtMs) return;
if (!isMapboxConfigured()) return;
const view = desiredMapView();
if (!view) return;
let stale = mapImage === null || view.key !== mapFetchedKey;
if (!stale && view.camera.kind === "center" && mapLastFetchCenter) {
const moved = haversineMeters(mapLastFetchCenter, [view.camera.longitude, view.camera.latitude]);
const viewportMeters = MAP_SIZE * metersPerPixel(view.camera.zoom, view.camera.latitude);
if (moved > viewportMeters * MOVE_REFRESH_FRACTION) stale = true;
else if (Date.now() - mapLastFetchAtMs > IDLE_REFRESH_MS && moved > IDLE_REFRESH_MIN_MOVE_M) stale = true;
}
if (!stale) return;
mapInFlight = true;
const generation = routeGeneration;
fetchStaticMapGray({
camera: view.camera,
width: MAP_SIZE,
height: MAP_SIZE,
routePath: view.path.length >= 2 ? view.path : undefined,
})
.then((image) => {
mapInFlight = false;
if (generation !== routeGeneration) return; // Route/mode changed mid-fetch.
boostContrast(image);
mapImage = image;
mapFetchedKey = view.key;
mapLastFetchAtMs = Date.now();
mapLastFetchCenter = view.camera.kind === "center" ? [view.camera.longitude, view.camera.latitude] : null;
mapLastError = "";
render();
})
.catch((error) => {
mapInFlight = false;
mapNextRetryAtMs = Date.now() + MAP_RETRY_BACKOFF_MS;
mapLastError = String((error as Error)?.message ?? error);
render();
});
}
/** Keep every Nth point (ends always included) to bound the overlay URL size. */
function simplifyPath(path: Array<[number, number]>, maxPoints: number): Array<[number, number]> {
if (path.length <= maxPoints) return path;
const step = (path.length - 1) / (maxPoints - 1);
const out: Array<[number, number]> = [];
for (let i = 0; i < maxPoints; i++) {
out.push(path[Math.round(i * step)]!);
}
return out;
}
/** Stretch the dark-style render so roads survive 4bpp quantization. */
function boostContrast(image: GrayImage): void {
for (let i = 0; i < image.pixels.length; i++) {
const value = image.pixels[i]!;
image.pixels[i] = Math.max(0, Math.min(255, Math.round((value - 12) * 1.7)));
}
}
// ---------------------------------------------------------------------------
// Tools
async function handleNavigateTool(name: string, args: any): Promise<ToolResult> {
switch (name) {
case "start_route": {
const query = String(args?.query ?? "").trim();
if (!query) return { ok: false, error: "start_route requires a destination query" };
const requested = String(args?.profile ?? "driving");
const routeProfile: RouteProfile =
requested === "walking" || requested === "cycling" ? requested : "driving";
try {
const summary = await startNavigation(query, routeProfile);
return { ok: true, content: summary };
} catch (error) {
return { ok: false, error: String((error as Error)?.message ?? error) };
}
}
case "stop_route":
if (phase === "idle") return { ok: true, content: "Navigation was not active." };
stopNavigation("Navigation stopped.");
render();
return { ok: true, content: "Navigation stopped." };
case "route_status":
return { ok: true, content: describeRouteStatus() };
default:
return { ok: false, error: `Unknown navigate tool: ${name}` };
}
}
function startSummary(picked: GeocodeCandidate, candidates: GeocodeCandidate[], route: Route): string {
const parts = [
`Navigating to ${picked.name}${picked.placeFormatted ? ` (${picked.placeFormatted})` : ""}: ` +
`${formatDistance(route.distanceMeters)}, about ${formatDurationLong(route.durationSec)}, ` +
`arriving ${formatClock(Date.now() + route.durationSec * 1000)}.`,
];
const alternates = candidates.slice(1, 3);
if (alternates.length) {
parts.push(
`Other matches: ${alternates
.map((c) => `${c.name}${c.placeFormatted ? ` (${c.placeFormatted})` : ""}`)
.join("; ")}. Call start_route with a more specific query to pick one of these instead.`,
);
}
return parts.join(" ");
}
function describeRouteStatus(): string {
if (phase === "arrived") return `Arrived at ${destinationName}.`;
if (phase !== "navigating" || !follower) return "Navigation is not active.";
if (!progress) return `Navigating to ${destinationName}; waiting for a GPS fix.`;
const step = follower.route.steps[progress.nextStepIndex]!;
return (
`Next: ${step.instruction || step.maneuverType} in ${formatDistance(progress.metersToNextManeuver)}. ` +
`${formatDistance(progress.remainingMeters)} and ${formatDurationLong(progress.remainingSec)} remaining; ` +
`ETA ${formatClock(Date.now() + progress.remainingSec * 1000)}.`
);
}
// ---------------------------------------------------------------------------
// Input
function windowMenu(win: NavWindow): WindowMenu {
if (!win.menu) {
win.menu = new WindowMenu({
size: { width: win.viewportWidth, height: win.viewportHeight },
paintBase: () => paintContent(win),
isFocused: () => win.focused,
});
}
return win.menu;
}
function handleInput(win: NavWindow, event: DashboardInputEvent, frameId: number): void {
if (win.menu?.isOpen()) {
win.menu
.handleInput(event)
.catch((error) => console.error(`navigate menu input failed: ${error}`))
.then(() => renderAndSubmit(win, frameId));
return;
}
if (event.type === "long-press") {
const items = [...defaultWindowMenuItems(win.windowId, post)];
if (phase === "navigating" || phase === "arrived") {
items.unshift({
label: "Stop navigation",
onSelect: (ctx: any) => {
ctx.stack.pop();
stopNavigation("Navigation stopped.");
render();
},
});
}
windowMenu(win).open(items);
renderAndSubmit(win, frameId);
return;
}
if (event.type === "double-click") {
frameTimings.finishFrame(frameId, "discarded: navigate yielded focus");
post({ type: "yield-focus", windowId: win.windowId });
return;
}
if (event.type === "click") {
if (phase === "navigating") {
mapMode = mapMode === "follow" ? "overview" : "follow";
resetMapState();
maybeRefreshMap();
} else if (phase === "arrived") {
stopNavigation("");
} else {
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
return;
}
renderAndSubmit(win, frameId);
return;
}
if (event.type === "scroll-up" || event.type === "scroll-down") {
if (phase === "navigating" && mapMode === "follow") {
zoomOffset = Math.max(-3, Math.min(2, zoomOffset + (event.type === "scroll-up" ? 0.5 : -0.5)));
maybeRefreshMap();
renderAndSubmit(win, frameId);
} else {
frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll");
}
return;
}
frameTimings.finishFrame(frameId, "discarded: navigate ignored input");
}
// ---------------------------------------------------------------------------
// Painting
function paint(win: NavWindow): GrayImage {
if (win.menu?.isOpen()) return win.menu.paint();
return paintContent(win);
}
function paintContent(win: NavWindow): GrayImage {
const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0);
if (phase === "idle" || phase === "acquiring" || phase === "routing") {
paintIdle(image);
} else {
paintNavigating(image, win);
}
return image;
}
function paintIdle(image: GrayImage): void {
image.drawText(mediumFont, 24, 16, "Navigate", 245);
const busy = phase !== "idle";
const hint = isMapboxConfigured()
? "Ask the voice assistant to navigate somewhere, or use Voice input from the long-press menu to say a destination."
: "Set a Mapbox token in Settings > API Keys to enable navigation.";
image.drawTextWrapped({ font: smallFont, x: 24, y: 64, width: image.width - 48, text: hint, value: 170 });
if (statusMessage) {
image.drawTextWrapped({
font: smallFont,
x: 24,
y: 130,
width: image.width - 48,
text: statusMessage,
value: busy ? 220 : 190,
});
}
drawFooter(image, `${GESTURE_DOUBLE_CLICK} back`);
}
function paintNavigating(image: GrayImage, win: NavWindow): void {
// Map pane (left).
if (mapImage) {
image.bitBlt(mapImage, 0, 0);
if (mapMode === "follow") drawChevron(image, MAP_SIZE / 2, MAP_SIZE / 2);
} else {
image.drawRect(0, 0, MAP_SIZE, MAP_SIZE, 60);
const text = mapLastError ? "Map unavailable" : "Loading map...";
image.drawText(smallFont, 24, MAP_SIZE / 2 - 8, text, 120);
}
// Guidance panel (right) — its own region so text deltas never overlap the map's.
const panelWidth = win.viewportWidth - PANEL_X;
if (phase === "arrived") {
drawManeuverGlyph(image, PANEL_X + 8, 14, 56, "arrive", "", 245);
image.drawText(mediumFont, PANEL_X + 76, 26, "Arrived", 245);
image.drawTextWrapped({
font: smallFont,
x: PANEL_X + 8,
y: 84,
width: panelWidth - 16,
text: destinationName,
value: 200,
});
drawFooter(image, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} back`);
return;
}
const step = follower && progress ? follower.route.steps[progress.nextStepIndex] : null;
if (step && progress) {
drawManeuverGlyph(image, PANEL_X + 8, 14, 56, step.maneuverType, step.maneuverModifier, 245);
if (step.exit !== null && (step.maneuverType === "roundabout" || step.maneuverType === "rotary")) {
image.drawText(smallFont, PANEL_X + 30, 46, String(step.exit), 245);
}
image.drawText(largeFont, PANEL_X + 76, 24, formatDistanceShort(progress.metersToNextManeuver), 250);
image.drawTextWrapped({
font: mediumFont,
x: PANEL_X + 8,
y: 84,
width: panelWidth - 16,
text: step.instruction || step.name || step.maneuverType,
value: 220,
});
const eta = `${formatDistance(progress.remainingMeters)} · ${formatDurationShort(progress.remainingSec)} · ${formatClock(
Date.now() + progress.remainingSec * 1000,
)}`;
image.drawText(smallFont, PANEL_X + 8, win.viewportHeight - 62, eta, 170);
} else {
image.drawTextWrapped({
font: mediumFont,
x: PANEL_X + 8,
y: 24,
width: panelWidth - 16,
text: `Waiting for GPS...`,
value: 200,
});
}
if (statusMessage) {
image.drawText(smallFont, PANEL_X + 8, win.viewportHeight - 44, statusMessage, 200);
}
const modeHint = mapMode === "follow" ? "overview" : "follow";
drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} ${modeHint} ${GESTURE_DOUBLE_CLICK} back`, PANEL_X + 8);
}
/** Position chevron, fixed at the map center (heading-up: it always points up). */
function drawChevron(image: GrayImage, cx: number, cy: number): void {
// Black halo first so it reads over bright roads.
fillTriangle(image, cx, cy - 11, cx - 9, cy + 9, cx + 9, cy + 9, 0);
fillTriangle(image, cx, cy - 8, cx - 6, cy + 6, cx + 6, cy + 6, 255);
}
function fillTriangle(image: GrayImage, x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, value: number): void {
const minY = Math.max(0, Math.floor(Math.min(y0, y1, y2)));
const maxY = Math.min(image.height - 1, Math.ceil(Math.max(y0, y1, y2)));
for (let y = minY; y <= maxY; y++) {
let minX = Number.POSITIVE_INFINITY;
let maxX = Number.NEGATIVE_INFINITY;
const edges: Array<[[number, number], [number, number]]> = [
[[x0, y0], [x1, y1]],
[[x1, y1], [x2, y2]],
[[x2, y2], [x0, y0]],
];
for (const [[ax, ay], [bx, by]] of edges) {
if ((ay <= y && by >= y) || (by <= y && ay >= y)) {
if (Math.abs(by - ay) < 0.0001) {
minX = Math.min(minX, ax, bx);
maxX = Math.max(maxX, ax, bx);
} else {
const t = (y - ay) / (by - ay);
const x = ax + t * (bx - ax);
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
}
}
}
if (minX <= maxX) {
image.fillRect(Math.round(minX), y, Math.round(maxX - minX) + 1, 1, value);
}
}
}
function drawFooter(image: GrayImage, text: string, x = 16): void {
image.drawText(smallFont, x, image.height - 22, text, 115);
}
// ---------------------------------------------------------------------------
// Formatting (imperial; matches the rest of the UI)
function formatDistanceShort(meters: number): string {
const feet = meters * 3.28084;
if (feet < 900) return `${Math.max(0, Math.round(feet / 50) * 50)} ft`;
return `${(feet / 5280).toFixed(1)} mi`;
}
function formatDistance(meters: number): string {
const feet = meters * 3.28084;
if (feet < 1000) return `${Math.max(0, Math.round(feet / 50) * 50)} ft`;
const miles = feet / 5280;
return miles >= 10 ? `${Math.round(miles)} mi` : `${miles.toFixed(1)} mi`;
}
function formatDurationShort(seconds: number): string {
const minutes = Math.round(seconds / 60);
if (minutes < 60) return `${Math.max(1, minutes)} min`;
return `${Math.floor(minutes / 60)}h${String(minutes % 60).padStart(2, "0")}`;
}
function formatDurationLong(seconds: number): string {
const minutes = Math.round(seconds / 60);
if (minutes < 60) return `${Math.max(1, minutes)} minutes`;
const hours = Math.floor(minutes / 60);
return `${hours}h ${minutes % 60}m`;
}
function formatClock(timestampMs: number): string {
const date = new Date(timestampMs);
let hours = date.getHours();
const minutes = String(date.getMinutes()).padStart(2, "0");
const meridiem = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
if (hours === 0) hours = 12;
return `${hours}:${minutes} ${meridiem}`;
}
// ---------------------------------------------------------------------------
// Render plumbing
function render(): void {
if (window) renderAndSubmit(window, 0);
}
function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
if (!win.foreground && inputFrameId === 0) return;
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${win.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(win)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
if (fingerprint === win.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: navigate content unchanged");
return;
}
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
win.surfaceId,
0,
0,
image.width,
image.height,
fingerprint,
paintMs,
frameId,
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
frameTimings.finishFrame(frameId, "discarded: navigate render failed");
console.error(`navigate worker render failed: ${error}`);
}
}
+237
View File
@@ -0,0 +1,237 @@
/**
* Client-side route following: snap GPS fixes to the route polyline, track
* progress toward the next maneuver, and detect off-route/arrival. Pure
* geometry over the Directions response — no network, no Android.
*/
import type { Route } from "../../native/mapbox";
const EARTH_RADIUS_M = 6_371_000;
/** Perpendicular distance that counts as off the route. */
const OFF_ROUTE_THRESHOLD_M = 50;
/** Consecutive off-route fixes before declaring a reroute is needed. */
const OFF_ROUTE_FIX_COUNT = 3;
/** Within this many meters of the destination counts as arrived. */
const ARRIVAL_THRESHOLD_M = 35;
/** How far behind the last known progress the snap search may look. */
const SNAP_BACKTRACK_SEGMENTS = 8;
/** How far ahead of the last known progress the snap search may look. */
const SNAP_LOOKAHEAD_SEGMENTS = 120;
export type RouteProgress = {
/** Snapped position [lon, lat]. */
snapped: [number, number];
/** Meters traveled along the route. */
alongMeters: number;
/** Perpendicular meters from the fix to the route. */
crossTrackMeters: number;
/** Index of the upcoming maneuver in route.steps (its instruction is what to show). */
nextStepIndex: number;
/** Meters until the upcoming maneuver. */
metersToNextManeuver: number;
remainingMeters: number;
remainingSec: number;
offRoute: boolean;
arrived: boolean;
};
export class RouteFollower {
readonly route: Route;
/** Cumulative meters from the origin to each geometry vertex. */
private readonly vertexMeters: number[];
/** Meters along the route at which each step's maneuver occurs. */
private readonly stepStartMeters: number[];
private readonly totalMeters: number;
private lastSegmentIndex = 0;
private offRouteStreak = 0;
constructor(route: Route) {
this.route = route;
this.vertexMeters = new Array(route.coordinates.length);
let along = 0;
this.vertexMeters[0] = 0;
for (let i = 1; i < route.coordinates.length; i++) {
along += haversineMeters(route.coordinates[i - 1]!, route.coordinates[i]!);
this.vertexMeters[i] = along;
}
this.totalMeters = along;
// Each step's maneuver happens where the previous step's travel ends, so
// the maneuver point of step k sits at the sum of steps 0..k-1 distances.
// (Slight drift vs the geometry is fine at guidance granularity.)
this.stepStartMeters = new Array(route.steps.length);
let stepAlong = 0;
for (let i = 0; i < route.steps.length; i++) {
this.stepStartMeters[i] = Math.min(stepAlong, this.totalMeters);
stepAlong += route.steps[i]!.distanceMeters;
}
}
/** Ingest a GPS fix and return current guidance state. */
update(longitude: number, latitude: number): RouteProgress {
const fix: [number, number] = [longitude, latitude];
const snap = this.snapToRoute(fix);
this.lastSegmentIndex = snap.segmentIndex;
if (snap.crossTrackMeters > OFF_ROUTE_THRESHOLD_M) {
this.offRouteStreak++;
} else {
this.offRouteStreak = 0;
}
const remainingMeters = Math.max(0, this.totalMeters - snap.alongMeters);
const arrived =
remainingMeters < ARRIVAL_THRESHOLD_M &&
haversineMeters(fix, this.route.coordinates[this.route.coordinates.length - 1]!) <
ARRIVAL_THRESHOLD_M * 2;
// The next maneuver is the first one still ahead of us. Step 0 is
// "depart", which is behind us as soon as we move; arrival is last.
let nextStepIndex = this.route.steps.length - 1;
for (let i = 1; i < this.route.steps.length; i++) {
if (this.stepStartMeters[i]! > snap.alongMeters + 5) {
nextStepIndex = i;
break;
}
}
return {
snapped: snap.snapped,
alongMeters: snap.alongMeters,
crossTrackMeters: snap.crossTrackMeters,
nextStepIndex,
metersToNextManeuver: Math.max(0, this.stepStartMeters[nextStepIndex]! - snap.alongMeters),
remainingMeters,
remainingSec: this.remainingSeconds(snap.alongMeters, nextStepIndex),
offRoute: this.offRouteStreak >= OFF_ROUTE_FIX_COUNT,
arrived,
};
}
totalRouteMeters(): number {
return this.totalMeters;
}
/**
* The route geometry from just behind the current position to aheadMeters
* past it — the slice worth drawing on a follow-mode map.
*/
routeSliceAround(alongMeters: number, behindMeters: number, aheadMeters: number): Array<[number, number]> {
const from = Math.max(0, alongMeters - behindMeters);
const to = Math.min(this.totalMeters, alongMeters + aheadMeters);
const slice: Array<[number, number]> = [];
for (let i = 0; i < this.route.coordinates.length; i++) {
const at = this.vertexMeters[i]!;
if (at < from) continue;
if (at > to) break;
slice.push(this.route.coordinates[i]!);
}
return slice.length >= 2 ? slice : this.route.coordinates.slice(0, 2);
}
/**
* Nearest point on the polyline, biased to search near the last known
* segment first so parallel roads and overlaps resolve forward along the
* route. Falls back to a whole-route search when the local window misses.
*/
private snapToRoute(fix: [number, number]): {
snapped: [number, number];
alongMeters: number;
crossTrackMeters: number;
segmentIndex: number;
} {
const windowStart = Math.max(0, this.lastSegmentIndex - SNAP_BACKTRACK_SEGMENTS);
const windowEnd = Math.min(
this.route.coordinates.length - 2,
this.lastSegmentIndex + SNAP_LOOKAHEAD_SEGMENTS,
);
const local = this.nearestOnSegments(fix, windowStart, windowEnd);
if (local && local.crossTrackMeters <= OFF_ROUTE_THRESHOLD_M * 2) {
return local;
}
const global = this.nearestOnSegments(fix, 0, this.route.coordinates.length - 2);
return global ?? local ?? {
snapped: this.route.coordinates[0]!,
alongMeters: 0,
crossTrackMeters: Number.POSITIVE_INFINITY,
segmentIndex: 0,
};
}
private nearestOnSegments(
fix: [number, number],
firstSegment: number,
lastSegment: number,
): { snapped: [number, number]; alongMeters: number; crossTrackMeters: number; segmentIndex: number } | null {
let best: { snapped: [number, number]; alongMeters: number; crossTrackMeters: number; segmentIndex: number } | null =
null;
// Planar approximation in a locally scaled lon/lat frame; fine for
// segment-scale distances.
const cosLat = Math.cos((fix[1] * Math.PI) / 180);
for (let i = firstSegment; i <= lastSegment; i++) {
const a = this.route.coordinates[i]!;
const b = this.route.coordinates[i + 1]!;
const ax = (a[0] - fix[0]) * cosLat;
const ay = a[1] - fix[1];
const bx = (b[0] - fix[0]) * cosLat;
const by = b[1] - fix[1];
const dx = bx - ax;
const dy = by - ay;
const lengthSq = dx * dx + dy * dy;
const t = lengthSq > 0 ? clamp01(-(ax * dx + ay * dy) / lengthSq) : 0;
const px = ax + t * dx;
const py = ay + t * dy;
const distanceMeters = Math.sqrt(px * px + py * py) * ((Math.PI / 180) * EARTH_RADIUS_M);
if (best === null || distanceMeters < best.crossTrackMeters) {
const segmentLength = this.vertexMeters[i + 1]! - this.vertexMeters[i]!;
best = {
snapped: [a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])],
alongMeters: this.vertexMeters[i]! + t * segmentLength,
crossTrackMeters: distanceMeters,
segmentIndex: i,
};
}
}
return best;
}
private remainingSeconds(alongMeters: number, nextStepIndex: number): number {
// Whole steps still ahead, plus a proportional share of the step we're in.
let seconds = 0;
for (let i = nextStepIndex; i < this.route.steps.length; i++) {
seconds += this.route.steps[i]!.durationSec;
}
const currentStep = this.route.steps[nextStepIndex - 1];
if (currentStep && currentStep.distanceMeters > 0) {
const stepStart = this.stepStartMeters[nextStepIndex - 1]!;
const fractionRemaining = clamp01(1 - (alongMeters - stepStart) / currentStep.distanceMeters);
seconds += currentStep.durationSec * fractionRemaining;
}
return seconds;
}
}
export function haversineMeters(a: [number, number], b: [number, number]): number {
const dLat = ((b[1] - a[1]) * Math.PI) / 180;
const dLon = ((b[0] - a[0]) * Math.PI) / 180;
const lat1 = (a[1] * Math.PI) / 180;
const lat2 = (b[1] * Math.PI) / 180;
const h =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(h)));
}
/** Initial bearing (degrees clockwise from north) from a to b. */
export function bearingDegrees(a: [number, number], b: [number, number]): number {
const lat1 = (a[1] * Math.PI) / 180;
const lat2 = (b[1] * Math.PI) / 180;
const dLon = ((b[0] - a[0]) * Math.PI) / 180;
const y = Math.sin(dLon) * Math.cos(lat2);
const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
const degrees = (Math.atan2(y, x) * 180) / Math.PI;
return (degrees + 360) % 360;
}
function clamp01(value: number): number {
return Math.max(0, Math.min(1, value));
}
+103
View File
@@ -0,0 +1,103 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { NightscoutLayer } from "./nightscout";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
export const NIGHTSCOUT_SURFACE_ID = "window:nightscout";
const TRAY_ICON_ID = "nightscout";
const TRAY_HEIGHT = 24;
const TRAY_GRAPH_WIDTH = 48;
const TRAY_STALE_MS = 15 * 60 * 1000;
const TRAY_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
/**
* The Nightscout app: the full-screen glucose view (previously reached via
* the dashboard card) in its own in-process window. While the app is running
* it also publishes a top-bar tray icon: the latest blood-glucose value
* (struck through when stale) plus a minimal 48px history graph.
*/
export function createNightscoutAppWindow(options: InProcessAppOptions): InProcessWindow {
let unsubscribe: (() => void) | null = null;
const app = createInProcessWindow({
appId: "nightscout",
windowId: NIGHTSCOUT_WINDOW_ID,
title: "Nightscout",
iconLetter: "Ns",
icon: "nightscout",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new NightscoutLayer()),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
shell.setTrayIcon(TRAY_ICON_ID, null);
options.onClosed();
},
});
// onStateChange fires immediately with the current snapshot, so this also
// publishes the initial tray icon.
unsubscribe = nightscoutBridge.onStateChange((state) => {
shell.setTrayIcon(TRAY_ICON_ID, buildNightscoutTrayIcon(state));
app.requestRender();
});
return app;
}
/** BG value (struck through when stale) + minimal 2-hour line graph. */
function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
const font = getDefaultSmallFont();
const nowMs = Date.now();
const label = state.latest ? `${state.latest.sgv}` : "--";
const labelWidth = font.measureText(label);
const image = new GrayImage(labelWidth + 4 + TRAY_GRAPH_WIDTH, TRAY_HEIGHT, 0);
const textY = Math.max(0, ((TRAY_HEIGHT - font.lineHeight) / 2) | 0);
image.drawText(font, 0, textY, label, 220);
const stale = state.latest !== null && nowMs - state.latest.timestampMs > TRAY_STALE_MS;
if (stale) {
image.drawLine(0, TRAY_HEIGHT / 2, labelWidth, TRAY_HEIGHT / 2, 180);
}
drawTrayGraph(image, labelWidth + 4, state, nowMs);
return image;
}
function drawTrayGraph(image: GrayImage, x: number, state: NightscoutState, nowMs: number): void {
const windowStartMs = nowMs - TRAY_GRAPH_WINDOW_MS;
const points = state.history.filter(
(point) => point.timestampMs >= windowStartMs && point.timestampMs <= nowMs,
);
if (points.length === 0) return;
const values = points.map((point) => point.sgv);
const min = Math.min(...values, 60);
const max = Math.max(...values, 200);
const span = Math.max(20, max - min);
const plotted = points.map((point) => ({
timestampMs: point.timestampMs,
x: x + Math.round(((point.timestampMs - windowStartMs) / TRAY_GRAPH_WINDOW_MS) * (TRAY_GRAPH_WIDTH - 1)),
y: TRAY_HEIGHT - 1 - Math.round(((point.sgv - min) / span) * (TRAY_HEIGHT - 1)),
}));
for (let index = 1; index < plotted.length; index++) {
const previous = plotted[index - 1]!;
const current = plotted[index]!;
if (current.timestampMs - previous.timestampMs <= TRAY_STALE_MS) {
image.drawLine(previous.x, previous.y, current.x, current.y, 190);
} else {
image.fillRect(current.x, current.y, 1, 1, 190);
}
}
}
+404
View File
@@ -0,0 +1,404 @@
import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage, imageFromAsciiArt } from "../../graphics/image";
import { DashboardInputEvent, Layer, LayerContext } from "../../ui/layers";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import {
isNightscoutSettingsConfigured,
nightscoutApiTokenSetting,
nightscoutSiteUrlSetting,
textSettingMenuItem,
} from "../../ui/dashboard-settings";
import { formatAgeShortFromTimestamp, formatTimestamp } from "~/util/date-util";
import { MenuLayer } from "../../ui/menu";
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
const nightscoutLargeFont = getDefaultLargeFont();
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
function drawDirectionIndicator(
image: GrayImage,
font: BdfFont,
x: number,
y: number,
direction: string,
shade: number,
): void {
const label = directionGlyphLabel(font, direction);
if (label) {
image.drawText(font, x, y, label, shade);
return;
}
const art = directionAsciiArt(direction);
if (art) {
image.bitBlt(imageFromAsciiArt(art, shade), x, y + 1);
return;
}
if (direction) {
image.drawText(font, x, y, truncateLine(direction, 10), shade);
}
}
function directionGlyphLabel(font: BdfFont, direction: string): string {
switch (direction) {
case "DoubleUp":
return font.hasGlyph("↑".codePointAt(0)!) ? "↑↑" : "";
case "SingleUp":
return font.hasGlyph("↑".codePointAt(0)!) ? "↑" : "";
case "FortyFiveUp":
return font.hasGlyph("↗".codePointAt(0)!) ? "↗" : "";
case "Flat":
return font.hasGlyph("→".codePointAt(0)!) ? "→" : "";
case "FortyFiveDown":
return font.hasGlyph("↘".codePointAt(0)!) ? "↘" : "";
case "SingleDown":
return font.hasGlyph("↓".codePointAt(0)!) ? "↓" : "";
case "DoubleDown":
return font.hasGlyph("↓".codePointAt(0)!) ? "↓↓" : "";
default:
return "";
}
}
function directionAsciiArt(direction: string): readonly string[] | undefined {
switch (direction) {
case "FortyFiveUp":
return [" ##", " ###", " # ##", "# ##", " ##"];
case "FortyFiveDown":
return [" ##", "# ##", " # ##", " ###", " ##"];
default:
return undefined;
}
}
function truncateLine(text: string, maxChars: number): string {
if (text.length <= maxChars) return text;
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
}
function formatDelta(delta: number | null): string {
if (delta === null) return "--";
return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`;
}
function formatWholeNumber(value: number): string {
return `${Math.round(value)}`;
}
function formatBolusLabel(value: number): string {
return Number.isInteger(value) ? `${value}` : value.toFixed(1);
}
function drawNightscoutGraph(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
nightscout: NightscoutState,
nowMs: number,
font: BdfFont,
): void {
if (bounds.width <= 2 || bounds.height <= 2) {
return;
}
const graphNowMs = minuteBucketTimestampMs(nowMs);
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
const visibleHistory = nightscout.history.filter((point) => {
const pointTimestampMs = minuteBucketTimestampMs(point.timestampMs);
return pointTimestampMs >= windowStartMs && pointTimestampMs <= graphNowMs;
});
if (visibleHistory.length === 0) {
return;
}
const values = visibleHistory.map((point) => point.sgv);
const min = Math.min(...values, 60);
const max = Math.max(...values, 200);
const range = Math.max(20, max - min);
const paddedMin = min - range * 0.1;
const paddedMax = max + range * 0.1;
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 56);
drawNightscoutBasalOverlay(image, bounds, nightscout.basal, graphNowMs);
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 60, 40);
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 100, 40);
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 180, 40);
const plotted = visibleHistory.map((point) => ({
point,
x: timeToGraphX(bounds, windowStartMs, point.timestampMs),
y:
bounds.y +
bounds.height -
1 -
Math.round(((point.sgv - paddedMin) / Math.max(1, paddedMax - paddedMin)) * Math.max(1, bounds.height - 1)),
}));
for (let i = 0; i < plotted.length; i++) {
const current = plotted[i]!;
const previous = i > 0 ? plotted[i - 1]! : null;
const next = i + 1 < plotted.length ? plotted[i + 1]! : null;
const connectedToPrevious =
previous !== null && current.point.timestampMs - previous.point.timestampMs <= NIGHTSCOUT_STALE_MS;
const connectedToNext =
next !== null && next.point.timestampMs - current.point.timestampMs <= NIGHTSCOUT_STALE_MS;
if (connectedToPrevious && previous) {
image.drawLine(previous.x, previous.y, current.x, current.y, 220);
}
if (!connectedToPrevious && !connectedToNext) {
image.fillRect(current.x - 1, current.y - 1, 2, 2, 220);
}
}
drawNightscoutCarbMarkers(image, bounds, font, nightscout.carbs, graphNowMs);
drawNightscoutBolusMarkers(image, bounds, font, nightscout.boluses, graphNowMs);
}
function drawNightscoutBasalOverlay(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
basalEvents: NightscoutState["basal"],
nowMs: number,
): void {
const graphNowMs = minuteBucketTimestampMs(nowMs);
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
const visibleEvents = basalEvents.filter(
(event) =>
minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000) >= windowStartMs &&
minuteBucketTimestampMs(event.timestampMs) <= graphNowMs,
);
if (visibleEvents.length === 0) {
return;
}
const maxRate = Math.max(...visibleEvents.map((event) => event.rate), 0);
if (maxRate <= 0) {
return;
}
const overlayHeight = Math.max(8, Math.min(18, Math.floor(bounds.height * 0.22)));
const bottomY = bounds.y + bounds.height - 2;
for (const event of visibleEvents) {
const startMs = Math.max(windowStartMs, minuteBucketTimestampMs(event.timestampMs));
const endMs = Math.min(graphNowMs, minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000));
if (endMs <= startMs) {
continue;
}
const leftX = timeToGraphX(bounds, windowStartMs, startMs);
const rightX = timeToGraphX(bounds, windowStartMs, endMs);
const height = Math.max(1, Math.round((event.rate / maxRate) * overlayHeight));
image.fillRect(leftX, bottomY - height + 1, Math.max(1, rightX - leftX + 1), height, 15);
}
}
function drawNightscoutCarbMarkers(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
font: BdfFont,
carbEvents: NightscoutState["carbs"],
nowMs: number,
): void {
const graphNowMs = minuteBucketTimestampMs(nowMs);
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
const baselineY = bounds.y + bounds.height - 2;
for (const event of carbEvents) {
const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs);
if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) {
continue;
}
const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs);
image.drawLine(x, baselineY, x, baselineY - 6, 150);
drawTextCentered(image, font, x, Math.max(bounds.y + 2, baselineY - 18), `${Math.round(event.carbs)}`, 150, bounds);
}
}
function drawNightscoutBolusMarkers(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
font: BdfFont,
bolusEvents: NightscoutState["boluses"],
nowMs: number,
): void {
const graphNowMs = minuteBucketTimestampMs(nowMs);
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
const topY = bounds.y + 1;
for (const event of bolusEvents) {
const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs);
if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) {
continue;
}
const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs);
image.drawLine(x, topY, x, topY + 6, 144);
if (event.insulin >= 1) {
drawTextCentered(
image,
font,
x,
Math.min(bounds.y + bounds.height - font.lineHeight - 1, topY + 8),
formatBolusLabel(event.insulin),
144,
bounds,
);
}
}
}
function drawTextCentered(
image: GrayImage,
font: BdfFont,
centerX: number,
y: number,
text: string,
shade: number,
bounds: { x: number; y: number; width: number; height: number },
): void {
const textWidth = font.measureText(text);
const x = Math.max(bounds.x + 1, Math.min(bounds.x + bounds.width - textWidth - 1, centerX - Math.round(textWidth / 2)));
image.drawText(font, x, y, text, shade);
}
function timeToGraphX(
bounds: { x: number; y: number; width: number; height: number },
windowStartMs: number,
timestampMs: number,
): number {
const graphTimestampMs = minuteBucketTimestampMs(timestampMs);
return (
bounds.x +
Math.round(
((graphTimestampMs - windowStartMs) / Math.max(1, NIGHTSCOUT_GRAPH_WINDOW_MS)) * Math.max(1, bounds.width - 1),
)
);
}
function minuteBucketTimestampMs(timestampMs: number): number {
return Math.floor(timestampMs / NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS) * NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS;
}
function drawNightscoutReferenceLine(
image: GrayImage,
bounds: { x: number; y: number; width: number; height: number },
minValue: number,
maxValue: number,
value: number,
shade: number,
): void {
const y =
bounds.y +
bounds.height -
1 -
Math.round(((value - minValue) / Math.max(1, maxValue - minValue)) * Math.max(1, bounds.height - 1));
image.drawLine(bounds.x + 1, y, bounds.x + bounds.width - 2, y, shade);
}
export class NightscoutLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// Sized to the hosting stack (the Nightscout app viewport).
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const nightscout = nightscoutBridge.snapshot();
const nowMs = Date.now();
const footerY = height - 26;
image.drawText(font, 22, 16, "Nightscout", 220);
if (!isNightscoutSettingsConfigured() || nightscout.configurationMissing) {
image.drawText(font, 22, 44, "Nightscout needs configuration.", 180);
image.drawText(font, 22, 62, `Set the site URL and API token in ${GESTURE_CLICK} menu > Settings.`, 140);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (!nightscout.available || !nightscout.latest) {
image.drawText(font, 22, 44, "No Nightscout data available.", 180);
image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const latest = nightscout.latest;
const glucoseText = `${latest.sgv}`;
const glucoseX = 22;
const glucoseWidth = nightscoutLargeFont.measureText(glucoseText);
image.drawText(nightscoutLargeFont, glucoseX, 28, glucoseText, 230);
if (isNightscoutPointStale(latest, nowMs)) {
drawNightscoutValueStrikeThrough(image, glucoseX, 40, glucoseWidth);
}
image.drawText(font, glucoseX + glucoseWidth + 8, 36, nightscout.units, 140);
const trendText = `Delta ${formatDelta(nightscout.delta)} Trend `;
image.drawText(font, 22, 62, trendText, 180);
drawDirectionIndicator(image, font, 22 + font.measureText(trendText), 62, nightscout.direction, 180);
image.drawText(
font,
22,
78,
`IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(2)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)} Updated ${formatTimestamp(latest.timestampMs)}`,
160,
);
image.drawText(
font,
22,
94,
`CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)} Loop ${nightscout.openapsStatusShort}`,
160,
);
image.drawText(font, 22, 110, `Pump ${truncateLine(nightscout.pumpStatus || "--", 56)}`, 150);
const graphHeight = Math.max(40, height - 128 - 48);
drawNightscoutGraph(image, { x: 22, y: 128, width: width - 44, height: graphHeight }, nightscout, nowMs, font);
image.drawText(font, 22, height - 42, "2-hour glucose history with basal / carbs / boluses", 130);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) {
case "click":
ctx.stack.push(createNightscoutMenu());
return;
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
}
/** The app menu (opened with a click); MenuLayer self-closes on double-click. */
function createNightscoutMenu(): MenuLayer {
return new MenuLayer(
"Nightscout",
[
{
label: "Refresh",
onSelect: async (ctx) => {
ctx.stack.pop();
await nightscoutBridge.refreshNow();
},
},
{
label: "Settings",
onSelect: (ctx) => {
// Pop the menu first so closing the settings modal lands back on
// the glucose view, not this menu.
ctx.stack.pop();
openSettingsSubMenu(ctx, "Nightscout settings", [
textSettingMenuItem(nightscoutSiteUrlSetting),
textSettingMenuItem(nightscoutApiTokenSetting),
]);
},
},
],
{ x: 8, y: 8, width: 200, minHeight: 0 },
);
}
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
}
function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
image.drawLine(x, y, x + width, y, 180);
}
@@ -0,0 +1,32 @@
import { NotificationsListLayer } from "../../ui/notifications";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
export const NOTIFICATIONS_WINDOW_ID = "notifications";
export const NOTIFICATIONS_SURFACE_ID = "window:notifications";
/**
* The Notifications app: the Android-notification list (previously a
* dashboard page) in its own in-process window; selecting a notification
* opens the detail view with its quick actions.
*/
export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow {
return createInProcessWindow({
appId: "notifications",
windowId: NOTIFICATIONS_WINDOW_ID,
title: "Notifications",
iconLetter: "N",
icon: "bell",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new NotificationsListLayer()),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
File diff suppressed because it is too large. Load diff
+64
View File
@@ -0,0 +1,64 @@
import { GrayImage } from "../../graphics/image";
import { LayerActions } from "../../ui/layers";
import { EditTextSettingLayer } from "../../ui/dashboard-settings";
import { createSettingsPanelLayer } from "../../ui/dashboard/settings-menus";
import { createInProcessWindow } from "../../ui/shell/in-process-window";
import { type ShellWindow } from "../../ui/shell/shell";
export const SETTINGS_WINDOW_ID = "settings";
export const SETTINGS_SURFACE_ID = "window:settings";
export type SettingsAppOptions = {
actions: LayerActions;
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
setSurfaceVisible: (visible: boolean) => void;
removeSurface: () => void;
onClosed: () => void;
};
export type SettingsAppWindow = {
window: ShellWindow;
requestRender: () => void;
/** Select a section in the left column by label (e.g. "Terminal"). */
focusSection: (label: string) => void;
/** Whether the glasses-side text-setting editor is the top layer. */
isTextEditorOnTop: () => boolean;
/** Pop the text-setting editor if it is on top; returns whether it was. */
closeTextEditor: () => boolean;
};
/**
* The Settings app: the settings menu tree (previously a dashboard submenu)
* hosted in its own in-process window. In-process because the text-setting
* editor is synchronized with the phone-side TextField through the
* controller, which lives on the main thread.
*/
export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAppWindow {
const panel = createSettingsPanelLayer();
const { window, stack, requestRender } = createInProcessWindow({
appId: "settings",
windowId: SETTINGS_WINDOW_ID,
title: "Settings",
iconLetter: "Se",
icon: "settings",
closeable: true,
actions: options.actions,
// Not wrapped in YieldAtRootLayer: the panel routes double-click itself
// (right column -> left column, then left column -> sidebar).
baseLayer: panel,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
return {
window,
requestRender,
focusSection: (label) => {
panel.focusSection(label);
requestRender();
},
isTextEditorOnTop: () => stack.topMatches((layer) => layer instanceof EditTextSettingLayer),
closeTextEditor: () => stack.popIfTop((layer) => layer instanceof EditTextSettingLayer),
};
}
File diff suppressed because it is too large. Load diff
+81
View File
@@ -0,0 +1,81 @@
import type { Terminal as XtermTerminal } from "@xterm/headless";
// xterm-headless expects a few browser globals (navigator for platform
// sniffing, window for timer/scheduling lookups) that the NativeScript
// runtime doesn't define. Provide minimal stand-ins before the library is
// evaluated (hence require, not a hoisted import).
const runtimeGlobal = globalThis as any;
if (typeof runtimeGlobal.navigator === "undefined") {
runtimeGlobal.navigator = {
userAgent: "NativeScript",
platform: "Android",
language: "en",
languages: ["en"],
};
}
if (typeof runtimeGlobal.window === "undefined") {
runtimeGlobal.window = runtimeGlobal;
}
if (typeof runtimeGlobal.self === "undefined") {
runtimeGlobal.self = runtimeGlobal;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Terminal } = require("@xterm/headless") as typeof import("@xterm/headless");
/**
* Headless terminal emulator for g2mirror streams: feed it the raw
* VT100/xterm bytes from snapshot/output messages and read back the visible
* text grid. Formatting (color/bold/inverse) is deliberately dropped for
* now — we only mirror the text.
*/
export class TerminalEmulator {
private readonly term: XtermTerminal;
constructor(
readonly cols: number,
readonly rows: number,
) {
this.term = new Terminal({
cols,
rows,
// Retain witnessed lines that scroll off the top; the archive covers
// everything before this. Matches the wrapper's default history cap.
scrollback: 10000,
allowProposedApi: true,
});
}
/**
* Apply terminal bytes. onProcessed fires once the emulator has actually
* parsed them (xterm buffers writes internally), so schedule repaints there.
*/
write(data: Uint8Array, onProcessed: () => void): void {
this.term.write(data, onProcessed);
}
/** Full reset; snapshots are defined as repainting from a cleared screen. */
reset(): void {
this.term.reset();
}
/** Total lines held (witnessed scrollback + the live screen). */
bufferLength(): number {
return this.term.buffer.active.length;
}
/** Text of buffer line `index` (0 = oldest retained), trailing space trimmed. */
lineAt(index: number): string {
const line = this.term.buffer.active.getLine(index);
return line ? line.translateToString(true) : "";
}
/** Cursor row as an absolute buffer index (baseY + cursorY). */
cursorRow(): number {
const buffer = this.term.buffer.active;
return buffer.baseY + buffer.cursorY;
}
cursorCol(): number {
return this.term.buffer.active.cursorX;
}
}
+732
View File
@@ -0,0 +1,732 @@
/**
* Timer app worker. The singleton window contains a stopwatch plus a list of
* independently scheduled countdown timers. Android AlarmManager owns the
* durable expiry path; worker timeouts keep the live UI prompt.
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import {
cancelTimerNotification,
fireTimerNotification,
scheduleTimerNotification,
} from "../../native/timer-notifications";
import type { DashboardInputEvent } from "../../ui/layers";
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
} from "../../ui/gestures";
import { clamp } from "../../util/numeric-util";
declare const global: any;
declare const com: any;
const RENDER_INTERVAL_MS = 100;
const TIMER_ROW_HEIGHT = 34;
const TIMER_LIST_TOP = 52;
const FOOTER_HEIGHT = 34;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
const smallFont = getDefaultSmallFont();
type TimerView = "stopwatch" | "timers" | "editor";
type TimerWindow = {
windowId: string;
surfaceId: string;
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
view: TimerView;
stopwatchAction: number;
timerSelection: number;
editorField: number;
editorHours: number;
editorMinutes: number;
editorSeconds: number;
editorMessage: string;
renderTimer: ReturnType<typeof setInterval> | null;
lastSubmittedFingerprint: string;
};
type CountdownTimer = {
id: number;
durationMs: number;
endAtMs: number;
expired: boolean;
expiryTimeout: ReturnType<typeof setTimeout> | null;
};
const TIMER_TOOLS: ToolSpec[] = [
{
name: "set_timer",
description:
"Start a new countdown timer. Give the duration as hours/minutes/seconds; at least one must be nonzero. Returns the timer's number and when it will finish.",
inputSchema: {
type: "object",
properties: {
hours: { type: "number", description: "Hours component of the duration (default 0)." },
minutes: { type: "number", description: "Minutes component of the duration (default 0)." },
seconds: { type: "number", description: "Seconds component of the duration (default 0)." },
},
additionalProperties: false,
},
availability: "open",
},
{
name: "list_timers",
description:
"List the countdown timers: each timer's number, its original duration, and the time remaining (or that it has finished).",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
availability: "open",
},
{
name: "cancel_timer",
description:
"Cancel a running countdown timer, or dismiss a finished one. Give the timer number from list_timers; it may be omitted when only one timer exists. Pass all=true to clear every timer.",
inputSchema: {
type: "object",
properties: {
timer: { type: "number", description: "1-based timer number, as shown in the timer list." },
all: { type: "boolean", description: "Clear all timers instead of a single one." },
},
additionalProperties: false,
},
availability: "open",
},
];
const windows = new Map<string, TimerWindow>();
const timers: CountdownTimer[] = [];
let screenOn = true;
let stopwatchAccumulatedMs = 0;
let stopwatchRunningSinceMs: number | null = null;
let nextTimerSerial = 1;
function post(message: WorkerAppReply): void {
global.postMessage(message);
}
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "open-window":
windows.set(message.windowId, {
windowId: message.windowId,
surfaceId: message.surfaceId,
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
view: "stopwatch",
stopwatchAction: 0,
timerSelection: 0,
editorField: 0,
editorHours: 0,
editorMinutes: 5,
editorSeconds: 0,
editorMessage: "",
renderTimer: null,
lastSubmittedFingerprint: "",
});
post({ type: "set-tools", windowId: message.windowId, tools: TIMER_TOOLS });
syncExpiredTimers();
break;
case "close-window": {
const window = windows.get(message.windowId);
if (window?.renderTimer) clearInterval(window.renderTimer);
windows.delete(message.windowId);
break;
}
case "input": {
const window = windows.get(message.windowId);
if (!window) {
frameTimings.finishFrame(message.frameId, "discarded: unknown timer window");
break;
}
window.focused = message.focused;
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
case "render": {
const window = windows.get(message.windowId);
if (!window) break;
window.focused = message.focused;
renderAndSubmit(window, 0);
break;
}
case "foreground": {
const window = windows.get(message.windowId);
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
updateRenderTimer(window);
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "screen":
screenOn = message.on;
for (const window of windows.values()) updateRenderTimer(window);
break;
case "tool-call": {
let result: ToolResult;
try {
result = handleTimerTool(message.name, message.args);
} catch (error) {
result = { ok: false, error: String((error as Error)?.message ?? error) };
}
post({ type: "tool-result", callId: message.callId, result });
break;
}
}
};
// ---------------------------------------------------------------------------
// Tools
function handleTimerTool(name: string, args: any): ToolResult {
syncExpiredTimers();
switch (name) {
case "set_timer": {
const hours = toolDurationField(args?.hours, "hours");
const minutes = toolDurationField(args?.minutes, "minutes");
const seconds = toolDurationField(args?.seconds, "seconds");
const durationMs = Math.round(((hours * 60 + minutes) * 60 + seconds) * 1000);
if (durationMs <= 0) return { ok: false, error: "set_timer requires a duration longer than zero" };
addTimer(durationMs);
const timer = timers[timers.length - 1]!;
refreshWindowsAfterToolChange();
return {
ok: true,
content:
`Started a ${formatDuration(durationMs)} timer (timer ${timers.length}), ` +
`finishing at ${formatClock(timer.endAtMs)}.`,
};
}
case "list_timers": {
if (timers.length === 0) return { ok: true, content: "No timers are set." };
return { ok: true, content: timers.map(describeTimer).join("\n") };
}
case "cancel_timer": {
if (args?.all === true) {
if (timers.length === 0) return { ok: true, content: "No timers are set." };
const count = timers.length;
for (const timer of [...timers]) removeTimer(timer);
refreshWindowsAfterToolChange();
return { ok: true, content: count === 1 ? "Cleared the timer." : `Cleared ${count} timers.` };
}
if (timers.length === 0) return { ok: true, content: "No timers are set." };
let timer: CountdownTimer;
if (args?.timer === undefined || args?.timer === null) {
if (timers.length > 1) {
return {
ok: false,
error: `There are ${timers.length} timers; give the timer number.\n${timers.map(describeTimer).join("\n")}`,
};
}
timer = timers[0]!;
} else {
const index = Math.floor(Number(args.timer));
if (!Number.isFinite(index) || index < 1 || index > timers.length) {
return { ok: false, error: `No timer ${args.timer}; there are ${timers.length} timers.` };
}
timer = timers[index - 1]!;
}
const description = describeTimer(timer);
removeTimer(timer);
refreshWindowsAfterToolChange();
return { ok: true, content: `${timer.expired ? "Dismissed" : "Canceled"}: ${description}` };
}
default:
return { ok: false, error: `Unknown timer tool: ${name}` };
}
}
function toolDurationField(value: unknown, label: string): number {
if (value === undefined || value === null) return 0;
const parsed = Number(value);
if (!Number.isFinite(parsed) || parsed < 0) {
throw new Error(`Invalid ${label} value: ${value}`);
}
return parsed;
}
function describeTimer(timer: CountdownTimer): string {
const number = timers.indexOf(timer) + 1;
const duration = formatDuration(timer.durationMs);
if (timer.expired) return `Timer ${number}: ${duration} timer, finished.`;
return (
`Timer ${number}: ${duration} timer, ${formatCountdown(timer.endAtMs - Date.now())} remaining ` +
`(finishes at ${formatClock(timer.endAtMs)}).`
);
}
/** Repaint and re-gate every window after a tool call changed the timer list. */
function refreshWindowsAfterToolChange(): void {
for (const window of windows.values()) {
window.timerSelection = clamp(window.timerSelection, 0, timers.length + 1);
updateRenderTimer(window);
if (window.foreground && screenOn) renderAndSubmit(window, 0);
}
}
/** The window's long-press menu (default entries only), created lazily. */
function windowMenu(window: TimerWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function handleInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`timer menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (event.type === "long-press") {
windowMenu(window).open(defaultWindowMenuItems(window.windowId, post));
renderAndSubmit(window, frameId);
return;
}
if (event.type === "double-click") {
if (window.view === "editor") {
window.view = "timers";
window.editorMessage = "";
updateRenderTimer(window);
renderAndSubmit(window, frameId);
} else {
frameTimings.finishFrame(frameId, "discarded: timer yielded focus");
post({ type: "yield-focus", windowId: window.windowId });
}
return;
}
if (window.view === "stopwatch") {
handleStopwatchInput(window, event, frameId);
} else if (window.view === "timers") {
handleTimersInput(window, event, frameId);
} else {
handleEditorInput(window, event, frameId);
}
}
function handleStopwatchInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
if (event.type === "scroll-up" || event.type === "scroll-down") {
const delta = event.type === "scroll-down" ? 1 : -1;
window.stopwatchAction = clamp(window.stopwatchAction + delta, 0, 2);
renderAndSubmit(window, frameId);
return;
}
if (event.type !== "click") {
frameTimings.finishFrame(frameId, "discarded: timer stopwatch ignored input");
return;
}
if (window.stopwatchAction === 0) {
if (stopwatchRunningSinceMs === null) {
stopwatchRunningSinceMs = Date.now();
} else {
stopwatchAccumulatedMs += Date.now() - stopwatchRunningSinceMs;
stopwatchRunningSinceMs = null;
}
} else if (window.stopwatchAction === 1) {
stopwatchAccumulatedMs = 0;
stopwatchRunningSinceMs = null;
} else {
window.view = "timers";
window.timerSelection = 0;
}
updateRenderTimer(window);
renderAndSubmit(window, frameId);
}
function handleTimersInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
syncExpiredTimers();
const itemCount = timers.length + 2; // Stopwatch navigation + New timer + timers.
if (event.type === "scroll-up" || event.type === "scroll-down") {
const delta = event.type === "scroll-down" ? 1 : -1;
window.timerSelection = clamp(window.timerSelection + delta, 0, itemCount - 1);
renderAndSubmit(window, frameId);
return;
}
if (event.type !== "click") {
frameTimings.finishFrame(frameId, "discarded: timer list ignored input");
return;
}
if (window.timerSelection === 0) {
window.view = "stopwatch";
} else if (window.timerSelection === 1) {
openEditor(window);
} else {
const timer = timers[window.timerSelection - 2];
if (timer) removeTimer(timer);
window.timerSelection = clamp(window.timerSelection, 0, Math.max(0, timers.length + 1));
}
updateRenderTimer(window);
renderAndSubmit(window, frameId);
}
function handleEditorInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
if (event.type === "scroll-up" || event.type === "scroll-down") {
const delta = event.type === "scroll-up" ? 1 : -1;
if (window.editorField === 0) {
window.editorHours = clamp(window.editorHours + delta, 0, 23);
} else if (window.editorField === 1) {
window.editorMinutes = clamp(window.editorMinutes + delta, 0, 59);
} else if (window.editorField === 2) {
window.editorSeconds = clamp(window.editorSeconds + delta, 0, 59);
} else {
window.editorField = delta > 0 ? 2 : 3;
}
window.editorMessage = "";
renderAndSubmit(window, frameId);
return;
}
if (event.type !== "click") {
frameTimings.finishFrame(frameId, "discarded: timer editor ignored input");
return;
}
if (window.editorField < 3) {
window.editorField += 1;
} else {
const durationMs =
((window.editorHours * 60 + window.editorMinutes) * 60 + window.editorSeconds) * 1000;
if (durationMs <= 0) {
window.editorMessage = "Choose a duration";
window.editorField = 0;
} else {
addTimer(durationMs);
window.view = "timers";
window.timerSelection = timers.length + 1;
window.editorMessage = "";
}
}
updateRenderTimer(window);
renderAndSubmit(window, frameId);
}
function openEditor(window: TimerWindow): void {
window.view = "editor";
window.editorField = 0;
window.editorHours = 0;
window.editorMinutes = 5;
window.editorSeconds = 0;
window.editorMessage = "";
}
function addTimer(durationMs: number): void {
// Millisecond epoch plus a small serial is unique across worker restarts and
// still safely representable as a JavaScript/Java long bridge value.
const id = Date.now() * 100 + (nextTimerSerial++ % 100);
const endAtMs = Date.now() + durationMs;
const timer: CountdownTimer = {
id,
durationMs,
endAtMs,
expired: false,
expiryTimeout: null,
};
timers.push(timer);
const durationLabel = formatDuration(durationMs);
scheduleTimerNotification(id, endAtMs, durationLabel);
timer.expiryTimeout = setTimeout(() => expireTimer(timer), Math.max(0, endAtMs - Date.now()));
}
function expireTimer(timer: CountdownTimer, renderWindows = true): void {
if (timer.expired) return;
timer.expired = true;
timer.expiryTimeout = null;
fireTimerNotification(timer.id, formatDuration(timer.durationMs));
for (const window of windows.values()) {
post({ type: "set-attention", windowId: window.windowId, attention: true });
updateRenderTimer(window);
if (renderWindows && window.foreground && screenOn) renderAndSubmit(window, 0);
}
}
function removeTimer(timer: CountdownTimer): void {
if (timer.expiryTimeout) clearTimeout(timer.expiryTimeout);
cancelTimerNotification(timer.id);
const index = timers.indexOf(timer);
if (index >= 0) timers.splice(index, 1);
}
function syncExpiredTimers(): void {
const now = Date.now();
for (const timer of timers) {
// The caller is already about to paint, so avoid a nested render when a
// sleeping/backgrounded timer is first observed as finished.
if (!timer.expired && timer.endAtMs <= now) expireTimer(timer, false);
}
}
function updateRenderTimer(window: TimerWindow): void {
const activeCountdowns = timers.some((timer) => !timer.expired);
const dynamic =
(window.view === "stopwatch" && stopwatchRunningSinceMs !== null) ||
(window.view === "timers" && activeCountdowns);
const shouldRun = dynamic && window.foreground && screenOn;
if (shouldRun && window.renderTimer === null) {
window.renderTimer = setInterval(() => {
syncExpiredTimers();
renderAndSubmit(window, 0);
}, RENDER_INTERVAL_MS);
} else if (!shouldRun && window.renderTimer !== null) {
clearInterval(window.renderTimer);
window.renderTimer = null;
}
}
function paint(window: TimerWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: TimerWindow): GrayImage {
syncExpiredTimers();
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
if (window.view === "stopwatch") {
paintStopwatch(image, window);
} else if (window.view === "timers") {
paintTimers(image, window);
} else {
paintEditor(image, window);
}
return image;
}
function paintStopwatch(image: GrayImage, window: TimerWindow): void {
drawTabs(image, "stopwatch");
const elapsed = stopwatchElapsedMs();
const timeLabel = formatStopwatchElapsed(elapsed);
const timeX = Math.max(0, Math.round((window.viewportWidth - largeFont.measureText(timeLabel)) / 2));
image.drawText(largeFont, timeX, 66, timeLabel, 245);
const state = stopwatchRunningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
drawCenteredText(image, smallFont, 112, state, 145);
const labels = [stopwatchRunningSinceMs === null ? "Start" : "Pause", "Reset", `Timers (${timers.length})`];
drawButtonRow(image, labels, window.stopwatchAction, 154);
drawFooter(image, `${GESTURE_SCROLL} action ${GESTURE_CLICK} select ${GESTURE_DOUBLE_CLICK} back`);
}
function paintTimers(image: GrayImage, window: TimerWindow): void {
drawTabs(image, "timers");
const items = ["< Stopwatch", "+ New timer", ...timers.map(timerRowLabel)];
const listBottom = image.height - FOOTER_HEIGHT;
const visibleRows = Math.max(1, Math.floor((listBottom - TIMER_LIST_TOP) / TIMER_ROW_HEIGHT));
const first = clamp(window.timerSelection - visibleRows + 1, 0, Math.max(0, items.length - visibleRows));
for (let row = 0; row < visibleRows; row++) {
const index = first + row;
if (index >= items.length) break;
const y = TIMER_LIST_TOP + row * TIMER_ROW_HEIGHT;
const selected = index === window.timerSelection;
if (selected) {
image.fillRoundedRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 34, 5);
image.drawRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 105);
}
image.drawText(index >= 2 ? mediumFont : smallFont, 32, y + (index >= 2 ? 2 : 7), items[index]!, selected ? 245 : 180);
}
const selectedTimer = window.timerSelection >= 2 ? timers[window.timerSelection - 2] : null;
const action = selectedTimer ? (selectedTimer.expired ? "dismiss" : "cancel") : "select";
drawFooter(image, `${GESTURE_SCROLL} timer ${GESTURE_CLICK} ${action} ${GESTURE_DOUBLE_CLICK} back`);
}
function paintEditor(image: GrayImage, window: TimerWindow): void {
image.drawText(smallFont, 24, 14, "New timer", 190);
const fields = [pad2(window.editorHours), pad2(window.editorMinutes), pad2(window.editorSeconds)];
const labels = ["hours", "minutes", "seconds"];
const fieldWidth = 118;
const gap = 22;
const totalWidth = fieldWidth * 3 + gap * 2;
const startX = Math.round((image.width - totalWidth) / 2);
for (let index = 0; index < 3; index++) {
const x = startX + index * (fieldWidth + gap);
if (window.editorField === index) {
image.fillRoundedRect(x, 57, fieldWidth, 68, 35, 6);
image.drawRect(x, 57, fieldWidth, 68, 120);
}
drawCenteredIn(image, largeFont, x, fieldWidth, 65, fields[index]!, window.editorField === index ? 250 : 180);
drawCenteredIn(image, smallFont, x, fieldWidth, 105, labels[index]!, 125);
if (index < 2) image.drawText(mediumFont, x + fieldWidth + 5, 71, ":", 130);
}
const startSelected = window.editorField === 3;
const buttonWidth = 160;
const buttonX = Math.round((image.width - buttonWidth) / 2);
if (startSelected) {
image.fillRoundedRect(buttonX, 151, buttonWidth, 38, 40, 6);
image.drawRect(buttonX, 151, buttonWidth, 38, 120);
}
drawCenteredIn(image, smallFont, buttonX, buttonWidth, 162, "Start timer", startSelected ? 250 : 175);
if (window.editorMessage) drawCenteredText(image, smallFont, 202, window.editorMessage, 190);
drawFooter(image, `${GESTURE_SCROLL} adjust ${GESTURE_CLICK} next ${GESTURE_DOUBLE_CLICK} cancel`);
}
function drawTabs(image: GrayImage, active: "stopwatch" | "timers"): void {
const stopwatch = "Stopwatch";
const timerLabel = `Timers (${timers.length})`;
const left = 24;
const dividerX = left + smallFont.measureText(stopwatch) + 18;
image.drawText(smallFont, left, 15, stopwatch, active === "stopwatch" ? 245 : 105);
image.drawLine(left, 35, dividerX - 12, 35, active === "stopwatch" ? 200 : 30);
image.drawText(smallFont, dividerX, 15, timerLabel, active === "timers" ? 245 : 105);
image.drawLine(dividerX, 35, dividerX + smallFont.measureText(timerLabel), 35, active === "timers" ? 200 : 30);
}
function drawButtonRow(image: GrayImage, labels: string[], selected: number, y: number): void {
const gap = 12;
const margin = 24;
const width = Math.floor((image.width - margin * 2 - gap * (labels.length - 1)) / labels.length);
for (let index = 0; index < labels.length; index++) {
const x = margin + index * (width + gap);
if (index === selected) {
image.fillRoundedRect(x, y, width, 40, 38, 6);
image.drawRect(x, y, width, 40, 115);
}
drawCenteredIn(image, smallFont, x, width, y + 12, labels[index]!, index === selected ? 245 : 155);
}
}
function drawFooter(image: GrayImage, text: string): void {
image.drawLine(16, image.height - FOOTER_HEIGHT, image.width - 16, image.height - FOOTER_HEIGHT, 35);
drawCenteredText(image, smallFont, image.height - 22, text, 115);
}
function drawCenteredText(image: GrayImage, font: typeof smallFont, y: number, text: string, value: number): void {
image.drawText(font, Math.round((image.width - font.measureText(text)) / 2), y, text, value);
}
function drawCenteredIn(
image: GrayImage,
font: typeof smallFont,
x: number,
width: number,
y: number,
text: string,
value: number,
): void {
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
}
function timerRowLabel(timer: CountdownTimer): string {
const number = timers.indexOf(timer) + 1;
if (timer.expired) return `${number}. 00:00 Finished`;
return `${number}. ${formatCountdown(timer.endAtMs - Date.now())} Running`;
}
function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
try {
const paintStartedAtMs = Date.now();
const image = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => paint(window)),
);
const paintMs = Date.now() - paintStartedAtMs;
const fingerprint = image.fingerprint();
if (fingerprint === window.lastSubmittedFingerprint) {
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
return;
}
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
0,
0,
image.width,
image.height,
fingerprint,
paintMs,
frameId,
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
frameTimings.finishFrame(frameId, "discarded: timer render failed");
console.error(`timer worker render failed: ${error}`);
}
}
function stopwatchElapsedMs(): number {
const runningMs = stopwatchRunningSinceMs === null ? 0 : Date.now() - stopwatchRunningSinceMs;
return stopwatchAccumulatedMs + runningMs;
}
function pad2(value: number): string {
return String(value).padStart(2, "0");
}
function formatStopwatchElapsed(totalMs: number): string {
const totalTenths = Math.floor(totalMs / 100);
const tenths = totalTenths % 10;
const totalSeconds = Math.floor(totalTenths / 10);
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
return hours > 0
? `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`
: `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
}
function formatCountdown(remainingMs: number): string {
const totalSeconds = Math.max(0, Math.ceil(remainingMs / 1000));
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
return hours > 0 ? `${hours}:${pad2(minutes)}:${pad2(seconds)}` : `${pad2(minutes)}:${pad2(seconds)}`;
}
function formatClock(timestampMs: number): string {
const date = new Date(timestampMs);
let hours = date.getHours();
const minutes = String(date.getMinutes()).padStart(2, "0");
const meridiem = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
if (hours === 0) hours = 12;
return `${hours}:${minutes} ${meridiem}`;
}
function formatDuration(durationMs: number): string {
const totalSeconds = Math.round(durationMs / 1000);
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
if (hours > 0) return `${hours}h ${minutes}m ${seconds}s`;
if (minutes > 0) return `${minutes}m ${seconds}s`;
return `${seconds}s`;
}
+75
View File
@@ -0,0 +1,75 @@
import { imageFromAsciiArt } from "../../graphics/image";
import { TranscribeLayer } from "./transcribe";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
export const TRANSCRIBE_WINDOW_ID = "transcribe";
export const TRANSCRIBE_SURFACE_ID = "window:transcribe";
const TRAY_ICON_ID = "transcribe";
// 16x18 microphone glyph for the top-bar tray.
const MIC_ICON = imageFromAsciiArt(
[
" #### ",
" ###### ",
" # # ",
" # # ",
" # # ",
" # # ",
" # # ",
" # # # # ",
" # # # # ",
" # #### # ",
" # # ",
" ###### ",
" # ",
" # ",
" ###### ",
],
220,
);
export type TranscribeAppOptions = InProcessAppOptions & {
/** Begin continuous mic capture (kept on while the window is open). */
startContinuousVoiceCapture: () => void;
stopContinuousVoiceCapture: () => void;
};
/**
* The Transcribe app: live speech-to-text in its own window. While open it
* holds continuous mic capture (so push-to-talk can share the stream) and
* shows a microphone tray icon in the top bar.
*/
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
const layer = new TranscribeLayer();
const app = createInProcessWindow({
appId: "transcribe",
windowId: TRANSCRIBE_WINDOW_ID,
title: "Transcribe",
iconLetter: "Tr",
icon: "mic",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(layer),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
options.stopContinuousVoiceCapture();
shell.setTrayIcon(TRAY_ICON_ID, null);
options.onClosed();
},
});
// Wire async state changes to this window's renderer, rather than the
// controller action from which the window-specific actions were derived.
layer.start(app.requestRender);
options.startContinuousVoiceCapture();
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
return app;
}
+132
View File
@@ -0,0 +1,132 @@
import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { writeTextToDownloads } from "../../native/file-access";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
/**
* Live transcription view. Continuous mic capture is owned by the app wrapper
* (createTranscribeAppWindow); this layer accumulates the transcript, shows
* it, and saves it to Downloads on click.
*/
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
export class TranscribeLayer implements Layer {
private status = "Listening...";
// Finalized utterances, plus the live (replace-semantics) partial appended
// when painting.
private finalizedText = "";
private liveText = "";
private saveNotice = "";
private unsubscribeTranscript: (() => void) | null = null;
private unsubscribeStatus: (() => void) | null = null;
start(requestRender: () => void): void {
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
this.onTranscript(event);
requestRender();
});
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
this.status = state.status;
requestRender();
});
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const text = this.displayText() || "Listening...";
const wrapped = wrapTranscribeText(font, text, width - 64);
image.drawText(font, 24, 20, "Transcribe", 200);
image.drawText(font, 24, 40, this.saveNotice || this.status, 110);
const bodyTop = 62;
const footerY = height - 18;
const bodyLines = Math.max(1, Math.floor((footerY - bodyTop) / 16));
const firstLine = Math.max(0, wrapped.length - bodyLines);
for (let index = firstLine; index < wrapped.length; index++) {
const y = bodyTop + (index - firstLine) * 16;
image.drawText(font, 32, y, wrapped[index]!, 230);
}
image.drawText(font, 24, footerY, `${GESTURE_CLICK} save ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "click") {
this.saveTranscript();
ctx.actions.requestRender();
return;
}
if (event.type === "double-click") {
ctx.stack.pop();
}
}
onRemoved(): void {
this.unsubscribeTranscript?.();
this.unsubscribeTranscript = null;
this.unsubscribeStatus?.();
this.unsubscribeStatus = null;
}
private saveTranscript(): void {
const text = this.displayText().trim();
if (!text) {
this.saveNotice = "Nothing to save yet.";
return;
}
const filename = `transcript-${transcriptTimestamp()}.txt`;
const path = writeTextToDownloads(filename, `${text}\n`);
this.saveNotice = path ? `Saved ${filename}` : "Save failed (check file access).";
}
private displayText(): string {
if (!this.finalizedText) return this.liveText;
if (!this.liveText) return this.finalizedText;
return `${this.finalizedText} ${this.liveText}`;
}
private onTranscript(event: VoiceTranscriptEvent): void {
if (event.isFinal) {
const finalText = event.text.trim() || this.liveText.trim();
if (finalText) {
this.finalizedText = this.finalizedText ? `${this.finalizedText} ${finalText}` : finalText;
}
this.liveText = "";
} else {
// Replace semantics: the live partial is the whole current best transcript.
this.liveText = event.text.trim();
}
}
}
function transcriptTimestamp(): string {
const now = new Date();
const pad = (value: number) => String(value).padStart(2, "0");
return (
`${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}` +
`-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
);
}
function wrapTranscribeText(font: BdfFont, text: string, maxWidth: number): string[] {
const words = text.split(/\s+/).filter(Boolean);
const lines: string[] = [];
let line = "";
for (const word of words) {
const candidate = line ? `${line} ${word}` : word;
if (line && font.measureText(candidate) > maxWidth) {
lines.push(line);
line = word;
} else {
line = candidate;
}
}
if (line) {
lines.push(line);
}
return lines.length ? lines : [""];
}
+56
View File
@@ -0,0 +1,56 @@
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
import { weatherBridge } from "../../native/weather";
import { WeatherLayer } from "./weather";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
export const WEATHER_WINDOW_ID = "weather";
export const WEATHER_SURFACE_ID = "window:weather";
/** Local current conditions and forecast from the National Weather Service. */
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
let requestingPermission = false;
let unsubscribe: (() => void) | null = null;
let app: InProcessWindow;
const requestUpdate = () => {
if (requestingPermission) return;
if (hasLocationPermission()) {
weatherBridge.start();
return;
}
requestingPermission = true;
void ensureLocationPermission().then((granted) => {
requestingPermission = false;
if (granted) weatherBridge.start();
app.requestRender();
});
};
app = createInProcessWindow({
appId: "weather",
windowId: WEATHER_WINDOW_ID,
title: "Weather",
iconLetter: "W",
icon: "cloud-sun",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
weatherBridge.stop();
options.onClosed();
},
});
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
requestUpdate();
return app;
}
+191
View File
@@ -0,0 +1,191 @@
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
const PAGE_X = 18;
const HEADER_Y = 8;
const CURRENT_TOP = 34;
const CURRENT_TEXT_X = 112;
const FORECAST_HEADER_Y = 105;
const FORECAST_TOP = 124;
const FORECAST_ROW_HEIGHT = 23;
const FOOTER_HEIGHT = 18;
const FORECAST_NAME_WIDTH = 92;
const FORECAST_TEMP_X = 126;
const FORECAST_PRECIP_X = 184;
const FORECAST_SUMMARY_X = 244;
/** Weather's current-conditions summary and scrollable 12-hour forecast. */
export class WeatherLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
constructor(
private readonly state: () => WeatherState,
private readonly requestUpdate: () => void,
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const weather = this.state();
image.drawText(font, PAGE_X, HEADER_Y, "Weather", 225);
if (weather.locationName) {
image.drawText(
font,
width - PAGE_X - font.measureText(truncateText(font, weather.locationName, 220)),
HEADER_Y,
truncateText(font, weather.locationName, 220),
150,
);
}
if (weather.phase === "permission-required") {
this.drawMessage(
image,
font,
width,
height,
"Allow approximate location on your phone to get local weather.",
`${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`,
);
return image;
}
if (weather.phase === "locating" || weather.phase === "loading") {
this.drawMessage(image, font, width, height, weather.status, `${GESTURE_DOUBLE_CLICK} back`);
return image;
}
if (weather.phase === "error") {
this.drawMessage(
image,
font,
width,
height,
weather.status,
`${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`,
);
return image;
}
this.drawCurrent(image, weather, width);
this.drawForecast(image, weather.forecast, ctx, width, height);
return image;
}
handleInput(event: DashboardInputEvent): void {
const weather = this.state();
if (event.type === "click") {
this.requestUpdate();
return;
}
if (!weather.forecast.length || weather.phase !== "ready") return;
if (event.type === "scroll-up") {
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
} else if (event.type === "scroll-down") {
this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1);
}
}
private drawCurrent(image: GrayImage, weather: WeatherState, width: number): void {
const current = weather.current;
if (!current) return;
const large = getDefaultLargeFont();
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
image.drawText(large, PAGE_X, CURRENT_TOP + 8, temperature, 245);
image.drawText(
medium,
CURRENT_TEXT_X,
CURRENT_TOP + 5,
truncateText(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
220,
);
const details: string[] = [];
if (current.humidityPercent !== null) details.push(`Humidity ${Math.round(current.humidityPercent)}%`);
if (current.windSpeedMph !== null) {
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
}
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 35, details.join(" ") || "Current forecast", 170);
const source = current.observed ? "Observed" : "Forecast";
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 54, `${source}${age ? ` ${age}` : ""}`, 105);
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
}
private drawForecast(
image: GrayImage,
forecast: ForecastPeriod[],
ctx: LayerContext,
width: number,
height: number,
): void {
const font = getDefaultSmallFont();
if (!forecast.length) {
image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160);
return;
}
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT));
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length);
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
image.drawText(font, FORECAST_TEMP_X, FORECAST_HEADER_Y, "Temp", 105);
image.drawText(font, FORECAST_PRECIP_X, FORECAST_HEADER_Y, "Rain", 105);
image.drawText(font, width - PAGE_X - font.measureText(`${this.selectedIndex + 1}/${forecast.length}`), FORECAST_HEADER_Y, `${this.selectedIndex + 1}/${forecast.length}`, 105);
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < last; index++) {
const period = forecast[index]!;
const y = FORECAST_TOP + (index - this.scrollRow) * FORECAST_ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
}
const shade = selected ? 245 : 190;
image.drawText(font, PAGE_X, y + 2, truncateText(font, period.name, FORECAST_NAME_WIDTH), shade);
image.drawText(font, FORECAST_TEMP_X, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
image.drawText(font, FORECAST_PRECIP_X, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncateText(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
}
image.drawText(
font,
PAGE_X,
height - 15,
`${GESTURE_SCROLL} forecast ${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`,
105,
);
}
private drawMessage(
image: GrayImage,
font: BdfFont,
width: number,
height: number,
message: string,
footer: string,
): void {
const lines = wrapText(font, message, width - 2 * (PAGE_X + 6));
const top = Math.max(52, Math.round((height - lines.length * 15) / 2) - 8);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, PAGE_X + 6, top + index * 15, lines[index]!, 185);
}
image.drawText(font, PAGE_X, height - 20, footer, 110);
}
}
function formatAge(ageMs: number): string {
const minutes = Math.max(0, Math.round(ageMs / 60_000));
if (minutes < 1) return "just now";
if (minutes < 60) return `${minutes}m ago`;
return `${Math.round(minutes / 60)}h ago`;
}