forked from Blendman974/faceclaw
Merge branch 'main' into ble-bandwidth-test
This commit is contained in:
commit
50cbc3a756
198 files changed
+15988
-2899
No files matched your search
@@ -0,0 +1,166 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerContext } from "../../ui/layers";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { VoiceDraft } from "./voice-draft";
|
||||
|
||||
class AiChatLayer implements Layer {
|
||||
readonly acceptsDirectional = true;
|
||||
private firstLine: number | null = null; // null follows the streaming tail
|
||||
private maxFirstLine = 0;
|
||||
private selectedId = "";
|
||||
private requestRender = () => {};
|
||||
private readonly wrappedEntries = new WeakMap<AssistantTranscriptEntry, {
|
||||
font: UiFont; width: number; text: string; lines: string[];
|
||||
}>();
|
||||
readonly draft: VoiceDraft;
|
||||
|
||||
constructor(private readonly conversations: AssistantConversations, options: InProcessAppOptions) {
|
||||
this.draft = new VoiceDraft(options.actions, () => shell.prepareChatVoiceCapture(),
|
||||
() => this.requestRender(), (text) => this.send(text));
|
||||
}
|
||||
start(render: () => void): () => void {
|
||||
this.requestRender = render;
|
||||
return this.conversations.onChanged(() => render());
|
||||
}
|
||||
send(text: string): void {
|
||||
this.firstLine = null;
|
||||
shell.sendToAssistant(text, false);
|
||||
}
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const size = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(size.width, size.height);
|
||||
const font = getDefaultSmallFont();
|
||||
const step = font.lineHeight + 3;
|
||||
const left = 12;
|
||||
const width = size.width - 28;
|
||||
const record = this.conversations.current();
|
||||
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
const title = this.conversations.title();
|
||||
image.drawText(font, left, 6, truncateText(font, title, width), 180);
|
||||
const label = assistantBackendSetting.get() === "external" ? "External agent" :
|
||||
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
|
||||
const footer = wrapText(font, `${label} ${gestureHints([
|
||||
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
|
||||
])}`, width);
|
||||
const footerY = size.height - step * footer.length;
|
||||
const footerTop = footerY - step * 3 - 12;
|
||||
const historyTop = step + 16;
|
||||
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
|
||||
const lines: { text: string; color: number }[] = [];
|
||||
for (const entry of transcript) {
|
||||
if (!entry.text) continue;
|
||||
let wrapped = this.wrappedEntries.get(entry);
|
||||
if (!wrapped || wrapped.font !== font || wrapped.width !== width || wrapped.text !== entry.text) {
|
||||
wrapped = { font, width, text: entry.text,
|
||||
lines: wrapText(font, `${entry.role === "user" ? "You" : "AI"}: ${entry.text}`, width - 8) };
|
||||
this.wrappedEntries.set(entry, wrapped);
|
||||
}
|
||||
for (const text of wrapped.lines) {
|
||||
lines.push({ text, color: entry.role === "user" ? 175 : 245 });
|
||||
}
|
||||
lines.push({ text: "", color: 0 });
|
||||
}
|
||||
if (!lines.length) lines.push({ text: "Hold to talk to your assistant.", color: 175 },
|
||||
{ text: "Voice and chat share this conversation.", color: 150 });
|
||||
this.maxFirstLine = Math.max(0, lines.length - visible);
|
||||
const first = this.firstLine === null ? this.maxFirstLine : Math.min(this.firstLine, this.maxFirstLine);
|
||||
for (let row = 0; row < visible; row++) {
|
||||
const line = lines[first + row];
|
||||
if (line) image.drawText(font, left, historyTop + row * step, line.text, line.color);
|
||||
}
|
||||
if (this.maxFirstLine > 0) {
|
||||
const track = visible * step;
|
||||
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
|
||||
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
|
||||
}
|
||||
image.fillRect(left, footerTop, width, 1, 65);
|
||||
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
|
||||
if (state) image.drawText(font, left, footerTop + 5, truncateText(font, state, width), 160);
|
||||
const pending = wrapText(font, this.draft.text || (this.draft.active ? "Listening..." : "Hold to speak; release to send"), width);
|
||||
pending.slice(-2).forEach((text, i) => image.drawText(font, left, footerTop + step * (i + 1) + 5, text, 220));
|
||||
footer.forEach((text, i) => image.drawText(font, left + width - font.measureText(text), footerY + i * step, text, 120));
|
||||
return image;
|
||||
}
|
||||
handleInput(event: InputEvent): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up": case "swipe-up":
|
||||
this.firstLine = Math.max(0, (this.firstLine ?? this.maxFirstLine) - 3); return;
|
||||
case "scroll-down": case "swipe-down": {
|
||||
const next = (this.firstLine ?? this.maxFirstLine) + 3;
|
||||
this.firstLine = next >= this.maxFirstLine ? null : next; return;
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
case "long-press-release": this.draft.release(); return;
|
||||
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
|
||||
}
|
||||
}
|
||||
menuItems(): MenuItem[] {
|
||||
const record = this.conversations.current();
|
||||
const busy = () => this.draft.active || Boolean(record.session?.isTurnActive());
|
||||
const external = assistantBackendSetting.get() === "external";
|
||||
const submenu = (ctx: LayerContext, title: string, items: MenuItem[]) => ctx.stack.push(new WindowMenuLayer(title, items, true));
|
||||
return [
|
||||
...(external ? [{ label: "Sessions and model managed by bridge", disabled: true, onSelect: () => {} }] : []),
|
||||
{ label: "New session", disabled: () => external || busy(), onSelect: (ctx) => {
|
||||
if (this.conversations.create()) ctx.stack.clearToBase();
|
||||
} },
|
||||
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
|
||||
[...this.conversations.list()].reverse().map((item) => ({
|
||||
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
|
||||
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
|
||||
ASSISTANT_MODEL_VALUES.map((model) => ({
|
||||
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
|
||||
disabled: () => !this.conversations.available(model, record.reasoning),
|
||||
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
|
||||
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
|
||||
return external || busy() || !llm?.effort;
|
||||
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
|
||||
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
|
||||
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
|
||||
record.session?.cancel(); ctx.stack.clearToBase();
|
||||
} }] : []),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export function createAiChatWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const layer = new AiChatLayer(shell.getAssistantConversations(), options);
|
||||
let unsubscribe = () => {};
|
||||
const app = createInProcessWindow({
|
||||
...options,
|
||||
appId: "ai-chat", windowId: "ai-chat", title: "AI Chat", iconLetter: "AI", icon: "message-circle", closeable: true,
|
||||
baseLayer: layer, holdToTalk: true,
|
||||
isVoiceCapturing: () => layer.draft.active,
|
||||
onSystemMenuOpened: () => layer.draft.cancel(),
|
||||
onAppMenuOpened: () => layer.draft.cancel(),
|
||||
setScreenOn: (on) => { if (!on) layer.draft.cancel(); },
|
||||
setSurfaceVisible: (visible) => { if (!visible) layer.draft.cancel(); options.setSurfaceVisible(visible); },
|
||||
menuItems: () => layer.menuItems(),
|
||||
receiveTextInput: (text) => { if (!layer.draft.active) layer.send(text); },
|
||||
onClosed: () => { unsubscribe(); layer.draft.cancel(); options.onClosed(); },
|
||||
});
|
||||
unsubscribe = layer.start(app.requestRender);
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { AppDefinition } from "../app-definition";
|
||||
import { createAiChatWindow } from "./ai-chat-app";
|
||||
|
||||
const aiChatApp: AppDefinition = {
|
||||
appId: "ai-chat",
|
||||
title: "AI Chat",
|
||||
icon: "message-circle",
|
||||
launch: (ctx) => ctx.launchInProcessApp("ai-chat", "window:ai-chat", createAiChatWindow),
|
||||
};
|
||||
export default aiChatApp;
|
||||
@@ -0,0 +1,125 @@
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { voiceActivity } from "../../ui/shell/voice-activity";
|
||||
import type { LayerActions } from "../../ui/layers";
|
||||
|
||||
/** One hold/release cycle. Async startup and trailing cloud finals belong to this cycle only. */
|
||||
export class VoiceDraft {
|
||||
phase: "idle" | "preparing" | "listening" | "finishing" = "idle";
|
||||
text = "";
|
||||
status = "";
|
||||
private committed = "";
|
||||
private live = "";
|
||||
private generation = 0;
|
||||
private started = false;
|
||||
private ownsVoiceActivity = false;
|
||||
private starting = false;
|
||||
private unsubscribe: (() => void)[] = [];
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly actions: Pick<LayerActions, "startVoiceCapture" | "stopVoiceCapture">,
|
||||
private readonly prepare: () => Promise<boolean>,
|
||||
private readonly changed: () => void,
|
||||
private readonly send: (text: string) => void,
|
||||
) {}
|
||||
|
||||
get active(): boolean { return this.phase !== "idle" || this.starting; }
|
||||
|
||||
async start(): Promise<void> {
|
||||
if (this.active) return;
|
||||
const generation = ++this.generation;
|
||||
this.text = this.committed = this.live = "";
|
||||
this.phase = "preparing";
|
||||
this.status = "Preparing microphone...";
|
||||
this.changed();
|
||||
try {
|
||||
const ready = await this.prepare();
|
||||
if (generation !== this.generation) return;
|
||||
if (!ready) { this.cancel("Microphone unavailable"); return; }
|
||||
this.phase = "listening";
|
||||
this.status = "Listening... release to send";
|
||||
this.ownsVoiceActivity = true;
|
||||
voiceActivity.setActive(true);
|
||||
this.unsubscribe.push(voiceControlBridge.onTranscript((event) => {
|
||||
if (!this.active || generation !== this.generation) return;
|
||||
if (event.isFinal) {
|
||||
const text = event.text.trim();
|
||||
if (text) this.committed = [this.committed, text].filter(Boolean).join(" ");
|
||||
this.live = "";
|
||||
} else this.live = event.text.trim();
|
||||
this.text = [this.committed, this.live].filter(Boolean).join(" ");
|
||||
if (event.isFinal && this.phase === "finishing") this.finish();
|
||||
else this.changed();
|
||||
}));
|
||||
// onStatus immediately replays the previous capture's status.
|
||||
let subscribed = false;
|
||||
this.unsubscribe.push(voiceControlBridge.onStatus(({ status }) => {
|
||||
if (!subscribed) return;
|
||||
if (generation !== this.generation || !this.active) return;
|
||||
if (/ignored|failed|error|denied/i.test(status)) this.cancel(status);
|
||||
}));
|
||||
subscribed = true;
|
||||
if (generation !== this.generation) return;
|
||||
this.starting = true;
|
||||
this.changed();
|
||||
await this.actions.startVoiceCapture();
|
||||
this.starting = false;
|
||||
if (generation !== this.generation) {
|
||||
await this.actions.stopVoiceCapture();
|
||||
this.changed();
|
||||
return;
|
||||
}
|
||||
this.started = true;
|
||||
if (this.isFinishing()) this.stopAndFinalize();
|
||||
} catch (error) {
|
||||
this.starting = false;
|
||||
if (generation === this.generation) this.cancel(`Microphone: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
private isFinishing(): boolean { return this.phase === "finishing"; }
|
||||
|
||||
release(): void {
|
||||
if (this.phase === "preparing") { this.cancel(); return; }
|
||||
if (this.phase !== "listening") return;
|
||||
this.phase = "finishing";
|
||||
this.status = "Finishing transcription...";
|
||||
this.changed();
|
||||
if (!this.starting) this.stopAndFinalize();
|
||||
}
|
||||
|
||||
private stopAndFinalize(): void {
|
||||
// Install fallback first: stop may synchronously deliver the final.
|
||||
this.timer = setTimeout(() => this.finish(), 1500);
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch((error) => this.cancel(String(error)));
|
||||
}
|
||||
}
|
||||
|
||||
private finish(): void {
|
||||
if (this.phase !== "finishing") return;
|
||||
const text = this.text.trim();
|
||||
this.cancel(text ? "" : "No speech detected");
|
||||
if (text) this.send(text);
|
||||
}
|
||||
|
||||
cancel(status = ""): void {
|
||||
++this.generation;
|
||||
this.phase = "idle";
|
||||
this.status = status;
|
||||
this.text = this.live = this.committed = "";
|
||||
if (this.timer !== null) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
for (const unsubscribe of this.unsubscribe.splice(0)) unsubscribe();
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch(() => {});
|
||||
}
|
||||
if (this.ownsVoiceActivity) {
|
||||
this.ownsVoiceActivity = false;
|
||||
voiceActivity.setActive(false);
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type AppDefinition } from "./app-definition";
|
||||
import aiChatApp from "./ai-chat";
|
||||
import launcherApp from "./launcher";
|
||||
import timerApp from "./timer";
|
||||
import calculatorApp from "./calculator";
|
||||
@@ -7,6 +8,7 @@ import filesApp from "./files";
|
||||
import musicApp from "./music";
|
||||
import nightscoutApp from "./nightscout";
|
||||
import transcribeApp from "./transcribe";
|
||||
import teleprompterApp from "./teleprompter";
|
||||
import microphonesApp from "./microphones";
|
||||
import notificationsApp from "./notifications";
|
||||
import calendarApp from "./calendar";
|
||||
@@ -29,6 +31,7 @@ import settingsApp from "./settings";
|
||||
*/
|
||||
export const ALL_APPS: readonly AppDefinition[] = [
|
||||
launcherApp,
|
||||
aiChatApp,
|
||||
timerApp,
|
||||
calculatorApp,
|
||||
terminalApp,
|
||||
@@ -36,6 +39,7 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
musicApp,
|
||||
nightscoutApp,
|
||||
transcribeApp,
|
||||
teleprompterApp,
|
||||
microphonesApp,
|
||||
notificationsApp,
|
||||
calendarApp,
|
||||
|
||||
@@ -6,18 +6,19 @@
|
||||
* Controls (in play): scroll moves the piece, click rotates, long-press hard
|
||||
* drops, double-click pauses. Watch swipes are spatial: left/right move,
|
||||
* up rotates, down hard-drops. Paused: click resumes, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus, tap-then-hold opens the window menu.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -136,12 +137,13 @@ type GamePhase = "playing" | "paused" | "game-over";
|
||||
type BlocksWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: 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. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
/** Locked cells; 0 = empty, otherwise the piece's shade byte. */
|
||||
@@ -181,6 +183,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: BlocksWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -285,9 +288,9 @@ function playSfx(window: BlocksWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (sound toggle + default entries). */
|
||||
function openWindowMenu(window: BlocksWindow): void {
|
||||
windowMenu(window).open([
|
||||
/** The window's context menu (sound toggle), offered while paused or over; playing keeps long-press for hard drop. */
|
||||
function windowMenuItems(window: BlocksWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: window.soundOn ? "Sound: on" : "Sound: off",
|
||||
onSelect: (ctx) => {
|
||||
@@ -296,13 +299,17 @@ function openWindowMenu(window: BlocksWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: BlocksWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -353,6 +360,10 @@ function handlePlayingInput(window: BlocksWindow, event: InputEvent, frameId: nu
|
||||
case "long-press":
|
||||
hardDrop(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
window.phase = "paused";
|
||||
updateTickTimer(window);
|
||||
@@ -378,8 +389,8 @@ function handleIdleInput(window: BlocksWindow, event: InputEvent, frameId: numbe
|
||||
frameTimings.finishFrame(frameId, "discarded: blocks yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: blocks ignored input");
|
||||
@@ -554,10 +565,7 @@ function updateTickTimer(window: BlocksWindow): void {
|
||||
}
|
||||
|
||||
function paint(window: BlocksWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: BlocksWindow): GrayImage {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Calculator window wiring: the in-process window, its long-press menu (mode
|
||||
* Calculator window wiring: the in-process window, its context menu (mode
|
||||
* follow-ups, clear, settings), the assistant tools, and the voice/text
|
||||
* plumbing into the layer.
|
||||
*
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
/**
|
||||
* The compass calibration screen: a crosshair the wearer aims at a landmark
|
||||
* of known heading, plus raw/offset/calibrated readouts. Swipe adjusts the
|
||||
* offset by 1°, double-tap returns to the compass.
|
||||
* of known heading, plus raw/offset/declination/calibrated readouts. Swipe
|
||||
* adjusts the offset by 1°, double-tap returns to the compass.
|
||||
*
|
||||
* The landmark's heading must be in the frame the compass is showing: aiming
|
||||
* at a true heading in true mode leaves the wearer-fit offset free of
|
||||
* declination, which is what lets the calibration survive travel.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
@@ -10,18 +14,16 @@ import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { screenCenterInViewportX } from "../../ui/shell/geometry";
|
||||
import {
|
||||
formatOffset,
|
||||
getCompassOffset,
|
||||
markCompassCalibrated,
|
||||
normalizeHeading,
|
||||
setCompassOffset,
|
||||
} from "./calibration";
|
||||
import { formatOffset, getCompassOffset, markCompassCalibrated, setCompassOffset } from "./calibration";
|
||||
import { resolveHeading, type NorthReference } from "./heading";
|
||||
|
||||
const INSTRUCTIONS =
|
||||
const INSTRUCTIONS_PREAMBLE =
|
||||
"This compass uses the magnetometer in the right arm of the glasses; depending how it rests on your head, "
|
||||
+ "it may be slightly off. To calibrate, look at a distant object or down a street where you know the true "
|
||||
+ "compass heading, then adjust the calibration offset below";
|
||||
+ "it may be slightly off. To calibrate, look at a distant object or down a street where you know the ";
|
||||
const INSTRUCTIONS_BY_FRAME: Record<NorthReference, string> = {
|
||||
true: "true compass heading (as shown on a map), then adjust the calibration offset below",
|
||||
magnetic: "magnetic compass heading (as read by another compass), then adjust the calibration offset below",
|
||||
};
|
||||
|
||||
/** Margins around the instruction paragraph. */
|
||||
const TEXT_PADDING_X = 16;
|
||||
@@ -40,7 +42,12 @@ export class CompassCalibrationLayer implements Layer {
|
||||
const medium = getDefaultMediumFont();
|
||||
const smallStep = lineStep(small);
|
||||
|
||||
const lines = wrapText(small, INSTRUCTIONS, width - TEXT_PADDING_X * 2);
|
||||
const raw = this.getRawHeading();
|
||||
const resolved = raw === null ? null : resolveHeading(raw);
|
||||
// Before the first reading there is no frame to name yet; the true-mode
|
||||
// wording is the default and the readout will correct it once data arrives.
|
||||
const frame = resolved?.frame ?? "true";
|
||||
const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], width - TEXT_PADDING_X * 2);
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
image.drawText(small, TEXT_PADDING_X, TEXT_PADDING_TOP + i * smallStep, lines[i]!, 190);
|
||||
}
|
||||
@@ -49,13 +56,20 @@ export class CompassCalibrationLayer implements Layer {
|
||||
// Readouts along the bottom: dim label over a bright value, in thirds.
|
||||
const valueTop = height - medium.lineHeight;
|
||||
const labelTop = valueTop - small.lineHeight;
|
||||
const raw = this.getRawHeading();
|
||||
const offset = getCompassOffset();
|
||||
const columns: Array<[string, string]> = [
|
||||
["Raw", raw === null ? "--°" : `${Math.round(raw)}°`],
|
||||
["Offset", formatOffset(offset)],
|
||||
["Calibrated", raw === null ? "--°" : `${Math.round(normalizeHeading(raw + offset))}°`],
|
||||
["Offset", formatOffset(getCompassOffset())],
|
||||
];
|
||||
// Declination is only part of the sum in true mode, so only show it then;
|
||||
// in magnetic mode it would be a number that changes nothing on screen.
|
||||
if (frame === "true") {
|
||||
const declination = resolved?.declinationDegrees ?? null;
|
||||
columns.push(["Declination", declination === null ? "--°" : formatOffset(Math.round(declination))]);
|
||||
}
|
||||
columns.push([
|
||||
frame === "true" ? "True" : "Magnetic",
|
||||
resolved === null ? "--°" : `${Math.round(resolved.displayDegrees)}°`,
|
||||
]);
|
||||
const columnWidth = width / columns.length;
|
||||
for (let i = 0; i < columns.length; i++) {
|
||||
const [label, value] = columns[i]!;
|
||||
|
||||
+180
-17
@@ -7,6 +7,7 @@ import {
|
||||
addCompassListener,
|
||||
setCompassEnabled,
|
||||
type CompassEvent,
|
||||
type CompassDiagnostics,
|
||||
} from "../../native/compass";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
@@ -20,8 +21,12 @@ import {
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { calibrateHeading, isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||
import { compassDebugLines, isCompassDebugEnabled, setCompassDebugEnabled } from "./debug";
|
||||
|
||||
export const COMPASS_WINDOW_ID = "compass";
|
||||
export const COMPASS_SURFACE_ID = "window:compass";
|
||||
@@ -92,19 +97,56 @@ const WALL_LIT = 62;
|
||||
|
||||
class CompassLayer implements Layer {
|
||||
private rawHeading: number | null = null;
|
||||
private diagnostics: CompassDiagnostics | null = null;
|
||||
/** A firmware calibration message, shown until the next heading arrives. */
|
||||
private firmwareStatus: string | null = null;
|
||||
private enabled = false;
|
||||
private removed = false;
|
||||
private timer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private unsubscribeDeclination: (() => void) | null = null;
|
||||
private requestingPermission = false;
|
||||
private background: { key: string; image: GrayImage } | null = null;
|
||||
|
||||
constructor(private readonly requestRender: () => void) {}
|
||||
|
||||
start(): void {
|
||||
this.unsubscribe = addCompassListener((event) => this.onCompassEvent(event));
|
||||
this.unsubscribeDeclination = onDeclinationChanged(() => this.requestRender());
|
||||
this.timer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
|
||||
this.reconcile();
|
||||
this.ensureDeclination();
|
||||
}
|
||||
|
||||
/**
|
||||
* True north needs the phone's location. Ask for it the way Weather does,
|
||||
* on opening, but only while the wearer actually wants true north: a
|
||||
* magnetic-mode compass has no business raising a permission prompt.
|
||||
*/
|
||||
private ensureDeclination(): void {
|
||||
if (getNorthReference() !== "true") return;
|
||||
if (hasLocationPermission()) {
|
||||
refreshDeclination();
|
||||
return;
|
||||
}
|
||||
if (this.requestingPermission) return;
|
||||
this.requestingPermission = true;
|
||||
void ensureLocationPermission().then((granted) => {
|
||||
this.requestingPermission = false;
|
||||
if (granted) refreshDeclination();
|
||||
this.requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
toggleNorthReference(): void {
|
||||
setNorthReference(getNorthReference() === "true" ? "magnetic" : "true");
|
||||
this.ensureDeclination();
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
toggleDebugInfo(): void {
|
||||
setCompassDebugEnabled(!isCompassDebugEnabled());
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
@@ -114,6 +156,8 @@ class CompassLayer implements Layer {
|
||||
this.timer = null;
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.unsubscribeDeclination?.();
|
||||
this.unsubscribeDeclination = null;
|
||||
if (this.enabled) setCompassEnabled(false);
|
||||
this.enabled = false;
|
||||
}
|
||||
@@ -132,8 +176,11 @@ class CompassLayer implements Layer {
|
||||
const visible = shell.isWindowVisible(COMPASS_WINDOW_ID);
|
||||
if (visible === this.enabled) return;
|
||||
this.enabled = visible;
|
||||
this.diagnostics = null;
|
||||
setCompassEnabled(visible);
|
||||
this.firmwareStatus = null;
|
||||
// The stored fix may have aged out while the compass was hidden.
|
||||
if (visible && hasLocationPermission()) refreshDeclination();
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
@@ -141,6 +188,7 @@ class CompassLayer implements Layer {
|
||||
if (this.removed) return;
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||
this.diagnostics = event.diagnostics ?? null;
|
||||
this.firmwareStatus = null;
|
||||
} else if (event.command === COMPASS_CALIBRATION_STARTED) {
|
||||
this.firmwareStatus = "Calibrating — move the glasses";
|
||||
@@ -154,18 +202,29 @@ class CompassLayer implements Layer {
|
||||
if (this.firmwareStatus !== null) return this.firmwareStatus;
|
||||
if (!this.enabled) return "Compass paused";
|
||||
if (this.rawHeading === null) return "Waiting for compass data…";
|
||||
return isCompassCalibrated() ? "Magnetic heading" : "Uncalibrated - Tap to calibrate";
|
||||
if (!isCompassCalibrated()) return "Uncalibrated - Tap to calibrate";
|
||||
return frameStatusText();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const small = getDefaultSmallFont();
|
||||
const large = getDefaultLargeFont();
|
||||
const heading = this.rawHeading === null ? null : calibrateHeading(this.rawHeading);
|
||||
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
|
||||
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
|
||||
const smallStep = lineStep(small);
|
||||
const debugLines = isCompassDebugEnabled() ? compassDebugLines(this.diagnostics) : [];
|
||||
const debugWidth = Math.max(0, ...debugLines.map((line) => small.measureText(line)));
|
||||
const debugGap = debugLines.length ? 12 : 0;
|
||||
// Keep diagnostics beside the heading even in narrow windows. The rose
|
||||
// stays centred on the optical axis; the combined readout fits the viewport.
|
||||
const headingFont = large.measureText(headingText) + debugGap + debugWidth <= width - EDGE_PAD * 2
|
||||
? large : small;
|
||||
const headingWidth = headingFont.measureText(headingText);
|
||||
const readoutWidth = headingWidth + debugGap + debugWidth;
|
||||
const debugHeight = debugLines.length ? (debugLines.length - 1) * smallStep + small.lineHeight : 0;
|
||||
const readoutHeight = Math.max(headingFont.lineHeight, debugHeight);
|
||||
|
||||
// One column centred on the display's true centre, so the rose sits where
|
||||
// the wearer is looking rather than 32px right of it.
|
||||
@@ -173,7 +232,7 @@ class CompassLayer implements Layer {
|
||||
|
||||
// The rose is sized and placed first — it hangs off the bottom edge — and
|
||||
// the readout then floats in whatever room is left above it.
|
||||
const textHeight = large.lineHeight + 4 + statusLines.length * smallStep;
|
||||
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
|
||||
const radius = Math.max(
|
||||
24,
|
||||
Math.min(
|
||||
@@ -191,15 +250,30 @@ class CompassLayer implements Layer {
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
|
||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
image.drawText(large, Math.round(cx - large.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||
y += large.lineHeight + 4;
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const clipY = y + textHeight + 6;
|
||||
const backgroundKey = [width, height, cx, cy, radius, clipY, fade].join(",");
|
||||
if (this.background?.key !== backgroundKey) {
|
||||
this.background = {
|
||||
key: backgroundKey,
|
||||
image: createCompassBackground(width, height, cx, cy, radius, clipY, fade),
|
||||
};
|
||||
}
|
||||
const image = this.background.image.clone();
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2)));
|
||||
image.drawText(headingFont, readoutX, y + Math.floor((readoutHeight - headingFont.lineHeight) / 2),
|
||||
headingText, heading === null ? 150 : 255);
|
||||
for (let i = 0; i < debugLines.length; i++) {
|
||||
image.drawText(small, readoutX + headingWidth + debugGap,
|
||||
y + Math.floor((readoutHeight - debugHeight) / 2) + i * smallStep, debugLines[i]!, 175);
|
||||
}
|
||||
y += readoutHeight + 4;
|
||||
for (const line of statusLines) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
|
||||
y += smallStep;
|
||||
}
|
||||
|
||||
drawPlaneGrid(image, cx, cy, radius, y + 6);
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -231,6 +305,22 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
||||
layer.openCalibration(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: `North: ${northReferenceName(getNorthReference())}`,
|
||||
description: "True north is what maps use. Magnetic north matches a handheld compass; it needs no location.",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.toggleNorthReference();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: `Debug information: ${isCompassDebugEnabled() ? "On" : "Off"}`,
|
||||
description: "Show magnetic accuracy (0–3), anomaly flags (0–2), and orientation source beside the heading.",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.toggleDebugInfo();
|
||||
},
|
||||
},
|
||||
],
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
submitFrame: options.submitFrame,
|
||||
@@ -246,6 +336,27 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
||||
return app;
|
||||
}
|
||||
|
||||
export function northReferenceName(reference: NorthReference): string {
|
||||
return reference === "true" ? "True" : "Magnetic";
|
||||
}
|
||||
|
||||
/**
|
||||
* Name the frame the readout is in. When the wearer wants true north but the
|
||||
* phone can't supply declination, say that the reading is magnetic and why,
|
||||
* rather than quietly showing a magnetic heading under a "true" label.
|
||||
*/
|
||||
function frameStatusText(): string {
|
||||
if (getNorthReference() === "magnetic") return "Magnetic heading";
|
||||
switch (getDeclinationAvailability()) {
|
||||
case "available":
|
||||
return "True heading";
|
||||
case "no-permission":
|
||||
return "Magnetic heading - location permission needed for true north";
|
||||
case "no-fix":
|
||||
return "Magnetic heading - waiting for location";
|
||||
}
|
||||
}
|
||||
|
||||
function cardinalDirection(heading: number): string {
|
||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
@@ -272,15 +383,66 @@ function projectRose(cx: number, cy: number, scale: number, radius: number, angl
|
||||
};
|
||||
}
|
||||
|
||||
/** Draw the tilted rose, with its brightest point aimed at magnetic north. */
|
||||
/**
|
||||
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
||||
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
||||
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
||||
*/
|
||||
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
||||
x: number; y: number; width: number; height: number;
|
||||
} {
|
||||
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
||||
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
||||
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
||||
const x = Math.floor(cx - halfWidth);
|
||||
const y = Math.floor(cy - depth / (1 + perspective));
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
width: Math.ceil(cx + halfWidth) - x,
|
||||
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
||||
* immutable texture. The wire planner can omit them from each raster delta
|
||||
* and restore them with a cached image draw. Outside that rectangle the
|
||||
* background stays raster, since heading updates do not change it.
|
||||
*
|
||||
* The texture uses transparent zero for the empty disc interior, so it can
|
||||
* overlay the rotating points. Its only ink over the points is the fixed
|
||||
* heading tick, which belongs on top of them.
|
||||
*/
|
||||
function createCompassBackground(
|
||||
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
||||
): GrayImage {
|
||||
const image = new GrayImage(width, height, 0);
|
||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
|
||||
const bounds = rotatingRoseBounds(cx, cy, radius);
|
||||
const left = Math.max(0, bounds.x);
|
||||
const top = Math.max(0, bounds.y);
|
||||
const right = Math.min(width, bounds.x + bounds.width);
|
||||
const bottom = Math.min(height, bounds.y + bounds.height);
|
||||
if (right > left && bottom > top) {
|
||||
const texture = new GrayImage(right - left, bottom - top, 0);
|
||||
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
||||
image.fillRect(left, top, texture.width, texture.height, 0);
|
||||
image.drawImage(texture, left, top);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Draw only the rotating points, with the brightest one aimed at north. */
|
||||
function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||
// orientation, dimmed, rather than an empty ring.
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const bearing = heading ?? 0;
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||
// north is drawn at full brightness: the other seven are there to make the
|
||||
// rose read as a rose, and competing with north would defeat the point.
|
||||
@@ -411,14 +573,15 @@ function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixed mark at the top of the ring: the heading the wearer is facing. */
|
||||
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const top = projectRose(cx, cy, radius, 1, 0);
|
||||
const tickTop = top.y + 1;
|
||||
fillTriangle(
|
||||
image,
|
||||
{ x: cx, y: top.y - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { CompassDiagnostics } from "../../native/compass";
|
||||
import { getBooleanSetting, setBooleanSetting } from "../../native/settings-store";
|
||||
|
||||
const DEBUG_KEY = "compass.debugInfo";
|
||||
|
||||
export function isCompassDebugEnabled(): boolean {
|
||||
return getBooleanSetting(DEBUG_KEY, false);
|
||||
}
|
||||
|
||||
export function setCompassDebugEnabled(enabled: boolean): void {
|
||||
setBooleanSetting(DEBUG_KEY, enabled);
|
||||
}
|
||||
|
||||
export function compassDebugLines(diagnostics: CompassDiagnostics | null): string[] {
|
||||
if (!diagnostics) return ["Diagnostics", "unavailable"];
|
||||
if (!(diagnostics.flags & 0x80)) return ["Sample data", "unavailable"];
|
||||
const accuracy = diagnostics.magneticAccuracy < 0 ? "--" : `${diagnostics.magneticAccuracy}/3`;
|
||||
const anomalies = diagnostics.magneticAnomalies < 0 ? "--" : String(diagnostics.magneticAnomalies);
|
||||
const source = ["--", "GRV", "GMRV", "RV"][diagnostics.orientationSource] ?? "--";
|
||||
return [`Mag: ${accuracy}`, `Anom: ${anomalies}`, `Src: ${source}`];
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Local magnetic declination for the compass, so the app can show true north.
|
||||
*
|
||||
* Declination only varies over tens of kilometres, so the location it needs is
|
||||
* whatever the phone can cheaply produce: the same one-shot coarse lookup the
|
||||
* Weather app uses, refreshed no more than daily. The last fix is persisted so
|
||||
* a fresh start has a usable declination before location comes back, and the
|
||||
* model is re-evaluated at the current time when it does.
|
||||
*/
|
||||
import { hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
const LATITUDE_KEY = "compass.declination.latitude";
|
||||
const LONGITUDE_KEY = "compass.declination.longitude";
|
||||
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
|
||||
|
||||
/** A stored fix older than this is refreshed when the compass next runs. */
|
||||
const FIX_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
||||
/** Back-off between failed location attempts, so a denied or dark phone isn't polled. */
|
||||
const RETRY_INTERVAL_MS = 10 * 60 * 1000;
|
||||
|
||||
export type DeclinationAvailability = "available" | "no-permission" | "no-fix";
|
||||
|
||||
type StoredFix = { latitude: number; longitude: number; locatedAtMs: number };
|
||||
|
||||
let declination: number | null = null;
|
||||
let fix: StoredFix | null = null;
|
||||
let loaded = false;
|
||||
let refreshing = false;
|
||||
let lastAttemptMs = 0;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function loadStoredFix(): void {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
const latitude = parseFloat(getStringSetting(LATITUDE_KEY, ""));
|
||||
const longitude = parseFloat(getStringSetting(LONGITUDE_KEY, ""));
|
||||
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
|
||||
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
|
||||
fix = { latitude, longitude, locatedAtMs };
|
||||
declination = magneticDeclinationDegrees(latitude, longitude);
|
||||
}
|
||||
}
|
||||
|
||||
/** Declination in degrees (east positive), or null when no location is known. */
|
||||
export function getDeclinationDegrees(): number | null {
|
||||
loadStoredFix();
|
||||
return declination;
|
||||
}
|
||||
|
||||
/** Why declination is or isn't available, for the compass status line. */
|
||||
export function getDeclinationAvailability(): DeclinationAvailability {
|
||||
if (getDeclinationDegrees() !== null) return "available";
|
||||
return hasLocationPermission() ? "no-fix" : "no-permission";
|
||||
}
|
||||
|
||||
export function onDeclinationChanged(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a location and recompute declination if the stored fix is stale or
|
||||
* missing. Safe to call on every compass start; it is a no-op while a lookup
|
||||
* is in flight, while the fix is fresh, or shortly after a failed attempt.
|
||||
* Never prompts for permission: callers that want to ask do that themselves.
|
||||
*/
|
||||
export function refreshDeclination(): void {
|
||||
loadStoredFix();
|
||||
if (refreshing || !hasLocationPermission()) return;
|
||||
const now = Date.now();
|
||||
if (fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
|
||||
if (now - lastAttemptMs < RETRY_INTERVAL_MS) return;
|
||||
lastAttemptMs = now;
|
||||
refreshing = true;
|
||||
getCurrentLocation()
|
||||
.then((location) => {
|
||||
const value = magneticDeclinationDegrees(location.latitude, location.longitude);
|
||||
if (value === null) return;
|
||||
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
|
||||
declination = value;
|
||||
setStringSetting(LATITUDE_KEY, String(location.latitude));
|
||||
setStringSetting(LONGITUDE_KEY, String(location.longitude));
|
||||
setStringSetting(LOCATED_AT_KEY, String(now));
|
||||
for (const listener of listeners) listener();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn(`Compass declination location lookup failed: ${error}`);
|
||||
})
|
||||
.finally(() => {
|
||||
refreshing = false;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* The compass heading model: raw magnetometer reading → wearer-calibrated
|
||||
* magnetic heading → true heading, and which of the last two the wearer has
|
||||
* asked to see.
|
||||
*
|
||||
* The wearer-fit offset and declination are kept as separate terms on purpose.
|
||||
* The offset changes when the glasses are re-seated but not when the wearer
|
||||
* travels; declination is the reverse. One combined hand-tuned number would go
|
||||
* quietly wrong after a long trip.
|
||||
*/
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { calibrateHeading, normalizeHeading } from "./calibration";
|
||||
import { getDeclinationDegrees } from "./declination";
|
||||
|
||||
const NORTH_REFERENCE_KEY = "compass.northReference";
|
||||
|
||||
/** Which north the compass reads relative to. True is the default: it is what maps use. */
|
||||
export type NorthReference = "true" | "magnetic";
|
||||
|
||||
export type ResolvedHeading = {
|
||||
/** Raw reading corrected by the wearer-fit offset. */
|
||||
magneticDegrees: number;
|
||||
/** Local declination (east positive), or null when no location is known. */
|
||||
declinationDegrees: number | null;
|
||||
/** Magnetic heading plus declination, or null when declination is unknown. */
|
||||
trueDegrees: number | null;
|
||||
/** The heading to show, in whichever frame `frame` names. */
|
||||
displayDegrees: number;
|
||||
/** The frame actually shown: true if requested and available, else magnetic. */
|
||||
frame: NorthReference;
|
||||
};
|
||||
|
||||
export function getNorthReference(): NorthReference {
|
||||
return getStringSetting(NORTH_REFERENCE_KEY, "true") === "magnetic" ? "magnetic" : "true";
|
||||
}
|
||||
|
||||
export function setNorthReference(reference: NorthReference): void {
|
||||
setStringSetting(NORTH_REFERENCE_KEY, reference);
|
||||
}
|
||||
|
||||
export function resolveHeading(rawDegrees: number): ResolvedHeading {
|
||||
const magneticDegrees = calibrateHeading(rawDegrees);
|
||||
const declinationDegrees = getDeclinationDegrees();
|
||||
const trueDegrees = declinationDegrees === null ? null : normalizeHeading(magneticDegrees + declinationDegrees);
|
||||
const wantTrue = getNorthReference() === "true";
|
||||
const frame: NorthReference = wantTrue && trueDegrees !== null ? "true" : "magnetic";
|
||||
return {
|
||||
magneticDegrees,
|
||||
declinationDegrees,
|
||||
trueDegrees,
|
||||
displayDegrees: frame === "true" ? trueDegrees! : magneticDegrees,
|
||||
frame,
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark";
|
||||
import { LightSensorDemoLayer } from "./light-sensor-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
|
||||
import { type AppContext } from "../app-definition";
|
||||
@@ -68,6 +69,12 @@ function debugTestsMenu(): MenuLayer {
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Light sensor",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "BLE bandwidth",
|
||||
onSelect: (ctx) => {
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
addAmbientLightListener,
|
||||
queryAmbientLight,
|
||||
setAmbientLightPolling,
|
||||
type AmbientLightReport,
|
||||
} from "../../native/ambient-light";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
// Passive poll period requested from the firmware (it clamps to 100..5000 ms).
|
||||
const POLL_INTERVAL_MS = 250;
|
||||
// Report every poll (min delta 0) so the trace shows the raw reading; the
|
||||
// heartbeat only matters if the firmware ever starts filtering.
|
||||
const POLL_MIN_DELTA = 0;
|
||||
const POLL_HEARTBEAT_MS = 1_000;
|
||||
// How often to reconcile the polling on/off state with window visibility.
|
||||
const RECONCILE_INTERVAL_MS = 400;
|
||||
// Length of the on-screen trace.
|
||||
const HISTORY_MS = 30_000;
|
||||
const MAX_SAMPLES = Math.ceil(HISTORY_MS / 100) + 2;
|
||||
|
||||
type Sample = { atMs: number; value: number; peak: number };
|
||||
|
||||
/**
|
||||
* Light sensor demo: turns CFW passive light-sensor polling (mode 16) on while
|
||||
* this page is visible and off otherwise, and shows the latest OPT3001
|
||||
* reading, the stock driver's peak/target, and a 30 s trace.
|
||||
*
|
||||
* While passive polling is on the stock auto-brightness adjuster does not step
|
||||
* the panel, so the level shown here stays wherever it was set. Leaving the
|
||||
* page hands the sensor back to the stock firmware.
|
||||
*/
|
||||
export class LightSensorDemoLayer implements Layer {
|
||||
private report: AmbientLightReport | null = null;
|
||||
private readonly samples: Sample[] = [];
|
||||
private reportCount = 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 = addAmbientLightListener((report) => {
|
||||
if (this.removed) return;
|
||||
this.report = report;
|
||||
this.reportCount++;
|
||||
const atMs = Date.now();
|
||||
if (report.readOk || !report.passive) {
|
||||
this.samples.push({ atMs, value: report.alsValue, peak: report.peak });
|
||||
const cutoff = atMs - HISTORY_MS;
|
||||
while (this.samples.length > 1 && this.samples[0]!.atMs < cutoff) this.samples.shift();
|
||||
while (this.samples.length > MAX_SAMPLES) this.samples.shift();
|
||||
}
|
||||
this.requestRender();
|
||||
});
|
||||
// Show whatever the driver currently holds before polling has started.
|
||||
queryAmbientLight();
|
||||
this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
|
||||
this.reconcile();
|
||||
}
|
||||
|
||||
/** Match the polling 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;
|
||||
setAmbientLightPolling(visible, {
|
||||
intervalMs: POLL_INTERVAL_MS,
|
||||
minDelta: POLL_MIN_DELTA,
|
||||
heartbeatMs: POLL_HEARTBEAT_MS,
|
||||
bindToLease: true,
|
||||
});
|
||||
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) {
|
||||
setAmbientLightPolling(false);
|
||||
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, "Light sensor", 220);
|
||||
const status = this.enabled ? "polling" : "paused";
|
||||
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
|
||||
|
||||
const report = this.report;
|
||||
if (!report) {
|
||||
const message = this.enabled ? "Waiting for a report…" : "Paused (page not visible).";
|
||||
image.drawText(small, 22, 44, message, 190);
|
||||
image.drawText(small, 22, 66, "Needs custom firmware with the als16", 120);
|
||||
image.drawText(small, 22, 82, "extension (EVENCFW/18 or newer).", 120);
|
||||
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
// Headline reading. The stock driver's unit is roughly a tenth of a lux.
|
||||
const lux = (report.alsValue / 10).toFixed(1);
|
||||
image.drawText(medium, 22, 30, `${lux} lux`, 245);
|
||||
const peakText = `peak ${(report.peak / 10).toFixed(1)}`;
|
||||
image.drawText(small, 22 + medium.measureText(`${lux} lux`) + 14, 36, peakText, 170);
|
||||
|
||||
const flags: string[] = [];
|
||||
if (!report.opened) flags.push("sensor closed");
|
||||
if (report.passive) flags.push("passive");
|
||||
if (report.autoBrightness) flags.push("auto on");
|
||||
if (report.passive && !report.readOk) flags.push("read failed");
|
||||
image.drawText(small, 22, 58, flags.join(" ") || "stock polling", 150);
|
||||
|
||||
image.drawText(
|
||||
small,
|
||||
22,
|
||||
76,
|
||||
`brightness ${report.brightness} stock target ${report.stockTarget} gear ${report.gear} scale ${(report.scaleQ10 / 1024).toFixed(2)}`,
|
||||
170,
|
||||
);
|
||||
|
||||
// 30 s trace of the reading (bright) and the driver's 5-sample peak (dim).
|
||||
// The axis label sits between the stats line and the chart, so the chart
|
||||
// starts a full text line lower than the stats.
|
||||
const chartTop = 76 + small.lineHeight * 2 + 4;
|
||||
const bounds = { x: 20, y: chartTop, width: width - 40, height: height - chartTop - 34 };
|
||||
const maxValue = Math.max(...this.samples.map((sample) => Math.max(sample.value, sample.peak)), 10);
|
||||
const scale = roundUpScale(maxValue * 1.08);
|
||||
image.drawText(small, bounds.x, bounds.y - small.lineHeight - 2, `0–${(scale / 10).toFixed(0)} lux, last 30 s`, 120);
|
||||
drawTrace(image, this.samples, Date.now(), bounds, scale);
|
||||
|
||||
image.drawText(small, 20, height - 16, `reports: ${this.reportCount}`, 110);
|
||||
const back = `${GESTURE_DOUBLE_CLICK} back`;
|
||||
image.drawText(small, width - 20 - small.measureText(back), height - 16, back, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Round a trace ceiling up to 1/2/5 × 10^n so the axis label stays tidy. */
|
||||
function roundUpScale(value: number): number {
|
||||
const magnitude = 10 ** Math.floor(Math.log10(Math.max(value, 1)));
|
||||
for (const step of [1, 2, 5, 10]) {
|
||||
if (value <= step * magnitude) return step * magnitude;
|
||||
}
|
||||
return 10 * magnitude;
|
||||
}
|
||||
|
||||
function drawTrace(
|
||||
image: GrayImage,
|
||||
samples: readonly Sample[],
|
||||
nowMs: number,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
scale: number,
|
||||
): void {
|
||||
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 45);
|
||||
const startMs = nowMs - HISTORY_MS;
|
||||
const series: Array<[(sample: Sample) => number, number]> = [
|
||||
[(sample) => sample.peak, 90],
|
||||
[(sample) => sample.value, 235],
|
||||
];
|
||||
for (const [value, shade] of series) {
|
||||
let previous: { x: number; y: number } | null = null;
|
||||
for (const sample of samples) {
|
||||
if (sample.atMs < startMs) continue;
|
||||
const x = bounds.x + Math.round(clamp((sample.atMs - startMs) / HISTORY_MS, 0, 1) * (bounds.width - 1));
|
||||
const y = bounds.y + bounds.height - 1 - Math.round(clamp(value(sample) / scale, 0, 1) * (bounds.height - 1));
|
||||
if (previous) image.drawLine(previous.x, previous.y, x, y, shade);
|
||||
else image.setPixel(x, y, shade);
|
||||
previous = { x, y };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,46 @@
|
||||
* running EvenHub apps, mirroring the IMU router. Only the foreground app
|
||||
* receives headings, so a backgrounded app can't keep the sensor running.
|
||||
*
|
||||
* The compass is NOT pre-calibrated — the magnetometer is in the right arm,
|
||||
* which rests on the head with a wearer-dependent curl (see compass-app memory);
|
||||
* headings are relative, not true north.
|
||||
* The raw reading is NOT pre-calibrated — the magnetometer is in the right
|
||||
* arm, which rests on the head with a wearer-dependent curl (see compass-app
|
||||
* memory). Each delivery also carries the Compass app's wearer-calibrated
|
||||
* magnetic heading and, when the phone has a location, declination and the
|
||||
* true heading, so apps needn't redo that work.
|
||||
*/
|
||||
import { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from "../../native/compass";
|
||||
import { isCompassCalibrated } from "../compass/calibration";
|
||||
import { refreshDeclination } from "../compass/declination";
|
||||
import { resolveHeading } from "../compass/heading";
|
||||
|
||||
/** Wire shape of the `compass` extension event (faceclaw-extensions `CompassReading`). */
|
||||
export type EvenHubCompassReading = {
|
||||
headingDegrees: number;
|
||||
magneticHeadingDegrees: number;
|
||||
wearerCalibrated: boolean;
|
||||
declinationDegrees?: number;
|
||||
trueHeadingDegrees?: number;
|
||||
};
|
||||
|
||||
export type EvenHubCompassClient = {
|
||||
readonly windowId: string;
|
||||
isForeground(): boolean;
|
||||
deliverCompass(headingDegrees: number): void;
|
||||
deliverCompass(reading: EvenHubCompassReading): void;
|
||||
};
|
||||
|
||||
export function buildCompassReading(rawDegrees: number): EvenHubCompassReading {
|
||||
const resolved = resolveHeading(rawDegrees);
|
||||
const reading: EvenHubCompassReading = {
|
||||
headingDegrees: rawDegrees,
|
||||
magneticHeadingDegrees: resolved.magneticDegrees,
|
||||
wearerCalibrated: isCompassCalibrated(),
|
||||
};
|
||||
if (resolved.declinationDegrees !== null && resolved.trueDegrees !== null) {
|
||||
reading.declinationDegrees = resolved.declinationDegrees;
|
||||
reading.trueHeadingDegrees = resolved.trueDegrees;
|
||||
}
|
||||
return reading;
|
||||
}
|
||||
|
||||
class EvenHubCompassRouter {
|
||||
private readonly requesting = new Set<EvenHubCompassClient>();
|
||||
private active: EvenHubCompassClient | null = null;
|
||||
@@ -47,13 +75,16 @@ class EvenHubCompassRouter {
|
||||
this.unsubscribe = addCompassListener((event: CompassEvent) => {
|
||||
// Only heading updates carry a real heading; calibration events use -1.
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.active?.deliverCompass(event.headingDegrees);
|
||||
this.active?.deliverCompass(buildCompassReading(event.headingDegrees));
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!this.enabled) {
|
||||
setCompassEnabled(true);
|
||||
this.enabled = true;
|
||||
// Uses location only if the wearer already granted it; an app turning
|
||||
// on the compass is not a reason to raise a system permission prompt.
|
||||
refreshDeclination();
|
||||
}
|
||||
} else {
|
||||
if (this.enabled) {
|
||||
|
||||
@@ -26,9 +26,23 @@ export const EVENHUB_SCREEN_HEIGHT = 288;
|
||||
const TEXT_WHITE = 255;
|
||||
const LIST_ROW_PADDING = 4;
|
||||
|
||||
/**
|
||||
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
|
||||
* the only one apps got before 0.0.14, so it must stay exactly TEXT_WHITE.
|
||||
* Level 0 is the firmware's "fully dim" step, which over the black page
|
||||
* background means the text is effectively not shown.
|
||||
*/
|
||||
const TEXT_BRIGHTNESS_GREYS = [0, 64, 128, 191, 255];
|
||||
|
||||
function textGrey(level: number | undefined): number {
|
||||
return level === undefined ? TEXT_WHITE : (TEXT_BRIGHTNESS_GREYS[level] ?? TEXT_WHITE);
|
||||
}
|
||||
|
||||
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
|
||||
* skips unknown glyphs, matching that. */
|
||||
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
|
||||
// Only the text dims: textColor is a text-brightness level, and stock draws
|
||||
// the container's border at full brightness regardless.
|
||||
paintBorder(image, container);
|
||||
const inset = container.borderWidth + container.paddingLength;
|
||||
EvenHubFont.get().drawTextWrapped(
|
||||
@@ -37,7 +51,7 @@ function paintTextContainer(image: GrayImage, container: EvenHubTextContainer):
|
||||
container.y + inset,
|
||||
Math.max(1, container.width - 2 * inset),
|
||||
container.content,
|
||||
TEXT_WHITE,
|
||||
textGrey(container.textColor),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ export type EvenHubTextContainer = {
|
||||
/** Extended-layout: inherit content from a same-named container on replace. */
|
||||
preserve: boolean;
|
||||
content: string;
|
||||
/** SDK 0.0.14 textColor: brightness level 0..4; undefined means the device default (4). */
|
||||
textColor: number | undefined;
|
||||
};
|
||||
|
||||
export type EvenHubImageContainer = {
|
||||
@@ -63,11 +65,31 @@ export type EvenHubListContainer = {
|
||||
|
||||
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
|
||||
|
||||
/** One entry of the OS contextual menu an app registers via `menuObject`. */
|
||||
export type EvenHubMenuItem = {
|
||||
itemName: string;
|
||||
/** Non-zero uint32, unique within the menu; echoed back in menuItemClickEvent. */
|
||||
itemID: number;
|
||||
};
|
||||
|
||||
export type EvenHubPage = {
|
||||
/** In declaration order (lists, images, texts); z-sorted at paint time. */
|
||||
containers: EvenHubContainer[];
|
||||
/**
|
||||
* The page's contextual-menu entries (SDK 0.0.14 `menuObject`). Empty when
|
||||
* the app declared none, which on rebuild is how a menu is cleared.
|
||||
*/
|
||||
menuItems: EvenHubMenuItem[];
|
||||
};
|
||||
|
||||
/** Firmware brightness levels for text; 4 is the device default. */
|
||||
const MIN_TEXT_BRIGHTNESS = 0;
|
||||
const MAX_TEXT_BRIGHTNESS = 4;
|
||||
|
||||
/** Firmware limits on a contextual menu, enforced app-side by the SDK too. */
|
||||
const MENU_MAX_ITEMS = 10;
|
||||
const MENU_NAME_MAX_BYTES = 32;
|
||||
|
||||
function normalizeKey(key: string): string {
|
||||
return key.replace(/_/g, "").toLowerCase();
|
||||
}
|
||||
@@ -123,9 +145,62 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
|
||||
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
|
||||
preserve: readNumber(json, "preserve", 0) !== 0,
|
||||
content: readString(json, "content", ""),
|
||||
textColor: readTextBrightness(json),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A text container's brightness level, or undefined for the device default.
|
||||
* Out-of-range values are dropped rather than clamped: the SDK rejects the
|
||||
* whole call for them, so an app that sends one is already misbehaving and
|
||||
* the default level is the least surprising thing to draw.
|
||||
*/
|
||||
export function readTextBrightness(json: Record<string, unknown>): number | undefined {
|
||||
const value = readOptionalNumber(json, "textColor");
|
||||
if (value === undefined || !Number.isInteger(value)) return undefined;
|
||||
if (value < MIN_TEXT_BRIGHTNESS || value > MAX_TEXT_BRIGHTNESS) return undefined;
|
||||
return value;
|
||||
}
|
||||
|
||||
/** The raw `menuObject.menuItems` array as sent, before any rule is applied. */
|
||||
export function declaredMenuItems(data: Record<string, unknown>): unknown[] {
|
||||
const raw = pickLoose(asRecord(pickLoose(data, "menuObject")), "menuItems");
|
||||
return Array.isArray(raw) ? raw : [];
|
||||
}
|
||||
|
||||
function utf8ByteLength(value: string): number {
|
||||
let bytes = 0;
|
||||
for (const char of value) {
|
||||
const code = char.codePointAt(0) ?? 0;
|
||||
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse `menuObject.menuItems`. The SDK validates these app-side and refuses
|
||||
* to call the host at all on a violation, so anything invalid arriving here
|
||||
* came from an app bypassing the SDK; drop the offending entries (and any
|
||||
* past the tenth) rather than rejecting the whole page, which would leave the
|
||||
* app with no UI at all.
|
||||
*/
|
||||
export function parseMenuItems(data: Record<string, unknown>): EvenHubMenuItem[] {
|
||||
const items: EvenHubMenuItem[] = [];
|
||||
const seen = new Set<number>();
|
||||
for (const entry of declaredMenuItems(data)) {
|
||||
if (items.length >= MENU_MAX_ITEMS) break;
|
||||
const json = asRecord(entry);
|
||||
const itemID = readNumber(json, "itemID", 0);
|
||||
if (!Number.isInteger(itemID) || itemID <= 0 || itemID > 0xffffffff) continue;
|
||||
if (seen.has(itemID)) continue;
|
||||
const itemName = readString(json, "itemName", "");
|
||||
if (utf8ByteLength(itemName) > MENU_NAME_MAX_BYTES) continue;
|
||||
seen.add(itemID);
|
||||
items.push({ itemName, itemID });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
function parseImageContainer(json: Record<string, unknown>): EvenHubImageContainer {
|
||||
return {
|
||||
kind: "image",
|
||||
@@ -173,6 +248,9 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
|
||||
* page. Containers keep declaration order within and across the three arrays
|
||||
* (lists, then images, then texts — texts last so info panels and capture
|
||||
* containers sit above image tiles when no zOrderIndex is given).
|
||||
*
|
||||
* The whole page is replaced on rebuild, which is also what gives an omitted
|
||||
* menuObject its documented meaning: the previous contextual menu goes away.
|
||||
*/
|
||||
export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
const containers: EvenHubContainer[] = [];
|
||||
@@ -188,7 +266,7 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
if (Array.isArray(texts)) {
|
||||
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
|
||||
}
|
||||
return { containers };
|
||||
return { containers, menuItems: parseMenuItems(data) };
|
||||
}
|
||||
|
||||
/** The container that owns gesture events (exactly one per valid page). */
|
||||
|
||||
@@ -20,6 +20,7 @@ const API_HOST = "https://api.evenrealities.com";
|
||||
const PUBLIC_CDN_HOST = "https://cdn-pub.evenhub.evenrealities.com";
|
||||
const SIGN_KEY = "a7964f42c39200cfa25c258b7a311b106e20232173667e543c34ced91d63b404";
|
||||
const REQUEST_TIMEOUT_MS = 30_000;
|
||||
const PAGE_SIZE = 40;
|
||||
|
||||
export type EvenHubStoreApp = {
|
||||
id: number;
|
||||
@@ -80,17 +81,31 @@ export class EvenHubApiClient {
|
||||
private loginPromise: Promise<string> | null = null;
|
||||
|
||||
async listApps(page = 1, category = ""): Promise<EvenHubStorePage> {
|
||||
const query: Record<string, string | number> = { page, page_size: 40 };
|
||||
const query: Record<string, string | number> = { page, page_size: PAGE_SIZE };
|
||||
if (category) query.category = category;
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/leaderboard", { query });
|
||||
const root = asRecord(data);
|
||||
const list = Array.isArray(root.list) ? root.list : [];
|
||||
return {
|
||||
apps: list.map(parseStoreApp).filter((app): app is EvenHubStoreApp => app !== null),
|
||||
total: finiteNumber(root.total),
|
||||
page: finiteNumber(root.page) || page,
|
||||
pageSize: finiteNumber(root.page_size) || 40,
|
||||
};
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Public apps ordered by first publication, newest first. The official app
|
||||
* requests `/ranking?sort_by=first_published_at`; whether the endpoint pages
|
||||
* is unverified, so callers must tolerate a repeated (or complete) list and
|
||||
* a missing total.
|
||||
*/
|
||||
async listNewApps(page = 1): Promise<EvenHubStorePage> {
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/ranking", {
|
||||
query: { sort_by: "first_published_at", page, page_size: PAGE_SIZE },
|
||||
});
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
/** Full-text storefront search (`/search?q=`), with the same paging caveats as listNewApps. */
|
||||
async searchApps(queryText: string, page = 1): Promise<EvenHubStorePage> {
|
||||
const data = await this.authenticatedRequest("GET", "/v2/evenhub/search", {
|
||||
query: { q: queryText, page, page_size: PAGE_SIZE },
|
||||
});
|
||||
return parseStorePage(data, page);
|
||||
}
|
||||
|
||||
async getAppDetail(packageId: string): Promise<Record<string, unknown>> {
|
||||
@@ -254,8 +269,15 @@ function buildCommon(version: 1 | 3): string {
|
||||
const fields: Record<string, string | number> = {
|
||||
platform: "16",
|
||||
package: "com.even.sg",
|
||||
versionName: "2.2.6",
|
||||
build: "114",
|
||||
// versionName/build must be a real shipped pair of the official Android
|
||||
// app (versionName/versionCode from its manifest): 2.2.8 = versionCode 122
|
||||
// (captures/even-2.2.8-base.apk). Earlier this said 2.2.6 with build 114,
|
||||
// but 114 was actually 2.2.4's versionCode (see notes/firmware-download.md).
|
||||
// buildTime is not a literal in the app binary (computed at runtime), so
|
||||
// it still carries the value from the 2.2.4 capture; the API has accepted
|
||||
// the mismatch so far.
|
||||
versionName: "2.2.8",
|
||||
build: "122",
|
||||
brand: "Google",
|
||||
model: "Pixel 7a",
|
||||
osVersion: "16",
|
||||
@@ -357,6 +379,25 @@ export function getPhoneOpenUdid(): string {
|
||||
return openUdid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a list response. The leaderboard returns `{list, total, page,
|
||||
* page_size}`; the ranking/search endpoints were only recovered from the
|
||||
* client's request paths, so accept a bare array or the other common
|
||||
* wrapper keys too. A missing total comes back as 0 ("unknown").
|
||||
*/
|
||||
function parseStorePage(data: unknown, requestedPage: number): EvenHubStorePage {
|
||||
const root = asRecord(data);
|
||||
const list = Array.isArray(data)
|
||||
? data
|
||||
: [root.list, root.apps, root.items, root.data, root.results].find(Array.isArray) ?? [];
|
||||
return {
|
||||
apps: list.map(parseStoreApp).filter((app): app is EvenHubStoreApp => app !== null),
|
||||
total: finiteNumber(root.total),
|
||||
page: finiteNumber(root.page) || requestedPage,
|
||||
pageSize: finiteNumber(root.page_size) || PAGE_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
function parseStoreApp(value: unknown): EvenHubStoreApp | null {
|
||||
const item = asRecord(value);
|
||||
const version = asRecord(item.version);
|
||||
|
||||
@@ -35,9 +35,11 @@ class EvenHubAppLayer implements Layer {
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent): void {
|
||||
// Everything goes to the app; long-press never reaches here (the window
|
||||
// menu intercepts it), which is the guaranteed way out since EvenHub
|
||||
// apps own double-click.
|
||||
// Everything goes to the app; the menu gestures never reach here (the
|
||||
// shell keeps long-press for its system menu and the window's context
|
||||
// menu intercepts tap-then-hold), which is the guaranteed way out since
|
||||
// EvenHub apps own double-click. Tap-then-hold is still reported to the
|
||||
// app as LONG_PRESS_EVENT — see the handleInput wrapper below.
|
||||
this.session.handleGesture(event);
|
||||
}
|
||||
}
|
||||
@@ -61,9 +63,22 @@ export function createEvenHubWindow(
|
||||
fallbackIcon,
|
||||
),
|
||||
heightMode: "medium",
|
||||
// Glasses-first: the app runs without the phone showing it; this reveals
|
||||
// its phone UI (config pages, etc.) over the dashboard on demand.
|
||||
menuItems: () => [{ label: "Show phone UI", onSelect: (ctx) => { ctx.stack.pop(); onShowPhone(); } }],
|
||||
// The window's context menu doubles as the OS contextual menu EvenHub
|
||||
// apps register with `menuObject` (SDK 0.0.14): the app's own entries come
|
||||
// first, then the host entries. Evaluated at open time, so a rebuild that
|
||||
// changes or clears the menu is reflected on the next press.
|
||||
menuItems: () => [
|
||||
...session.contextMenuItems().map((item) => ({
|
||||
label: item.itemName,
|
||||
onSelect: (ctx: LayerContext) => {
|
||||
ctx.stack.pop();
|
||||
session.selectContextMenuItem(item.itemID);
|
||||
},
|
||||
})),
|
||||
// Glasses-first: the app runs without the phone showing it; this reveals
|
||||
// its phone UI (config pages, etc.) over the dashboard on demand.
|
||||
{ label: "Show phone UI", onSelect: (ctx: LayerContext) => { ctx.stack.pop(); onShowPhone(); } },
|
||||
],
|
||||
actions: options.actions,
|
||||
baseLayer: new EvenHubAppLayer(session),
|
||||
submitFrame: options.submitFrame,
|
||||
@@ -87,6 +102,20 @@ export function createEvenHubWindow(
|
||||
windowId,
|
||||
});
|
||||
|
||||
// LONG_PRESS_EVENT / LONG_PRESS_RELEASE_EVENT (SDK 0.0.14): the app hears
|
||||
// the context-menu gesture (tap-then-hold; a plain long-press is the
|
||||
// shell's and never arrives) as its long-press. Wrapped at the window
|
||||
// rather than handled in the base layer because the press opens the window
|
||||
// menu and so never reaches a layer, and because the release lands on that
|
||||
// menu once it is up.
|
||||
const baseHandleInput = created.window.handleInput;
|
||||
created.window.handleInput = async (event, frameId) => {
|
||||
if (event.type === "short-then-long-press" || event.type === "long-press-release") {
|
||||
session.handleGesture(event);
|
||||
}
|
||||
await baseHandleInput(event, frameId);
|
||||
};
|
||||
|
||||
// FOREGROUND_ENTER/EXIT for the app on shell focus changes.
|
||||
const baseSetForeground = created.window.setForeground;
|
||||
created.window.setForeground = (foreground) => {
|
||||
|
||||
+60
-33
@@ -14,6 +14,7 @@ import { launchPackage, launchUrl, normalizeAppUrl } from "./manager";
|
||||
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
|
||||
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
|
||||
import { type InstalledEvenHubApp } from "./installed-apps";
|
||||
|
||||
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
|
||||
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
|
||||
@@ -40,45 +41,71 @@ export function isLoadableAppUrl(url: string): boolean {
|
||||
return normalizeAppUrl(url) !== null;
|
||||
}
|
||||
|
||||
/** The open store window, so a second launch focuses it and deep links can reach its layer. */
|
||||
let storeWindow: { window: InProcessWindow; store: EvenHubStoreLayer } | null = null;
|
||||
|
||||
/** Open the store window, or focus the one already open. */
|
||||
async function ensureStoreWindow(ctx: AppContext): Promise<{ window: InProcessWindow; store: EvenHubStoreLayer }> {
|
||||
if (storeWindow && shell.getWindows().some((window) => window.windowId === EVENHUB_STORE_WINDOW_ID)) {
|
||||
shell.focusWindow(EVENHUB_STORE_WINDOW_ID);
|
||||
ctx.requestShellRender();
|
||||
return storeWindow;
|
||||
}
|
||||
let created: InProcessWindow | null = null;
|
||||
const store = new EvenHubStoreLayer({
|
||||
launchApp: (appId) => ctx.launchApp(appId),
|
||||
appendLog: ctx.appendLog,
|
||||
});
|
||||
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
|
||||
created = createInProcessWindow({
|
||||
appId: "evenhub",
|
||||
windowId: EVENHUB_STORE_WINDOW_ID,
|
||||
title: "EvenHub",
|
||||
iconLetter: "EH",
|
||||
icon: "package",
|
||||
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
|
||||
closeable: true,
|
||||
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
|
||||
actions: options.actions,
|
||||
// Dictated text runs a store search (when the store page itself is on top).
|
||||
receiveTextInput: (text) => {
|
||||
if (created?.stack.receiveTextInput(text)) created.requestRender();
|
||||
},
|
||||
baseLayer: store,
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
store.onWindowClosed(options.actions);
|
||||
if (storeWindow?.store === store) storeWindow = null;
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
return created;
|
||||
});
|
||||
if (!created) throw new Error("EvenHub store window was not created.");
|
||||
storeWindow = { window: created, store };
|
||||
return storeWindow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the store on an installed app's page. The launcher uses it when the
|
||||
* registry lists a package whose EHPK is gone (a settings import after a
|
||||
* reinstall), so the tap lands on a Reinstall button instead of failing.
|
||||
*/
|
||||
export async function openEvenHubStoreForPackage(ctx: AppContext, installed: InstalledEvenHubApp): Promise<void> {
|
||||
const { window, store } = await ensureStoreWindow(ctx);
|
||||
store.showInstalledPackage(window.stack, installed);
|
||||
window.requestRender();
|
||||
}
|
||||
|
||||
const evenhubApp: AppDefinition = {
|
||||
appId: "evenhub",
|
||||
title: "EvenHub",
|
||||
icon: "package",
|
||||
renderIcon: renderEvenRealitiesLogo,
|
||||
launch: async (ctx) => {
|
||||
const existing = shell.getWindows().find((window) => window.windowId === EVENHUB_STORE_WINDOW_ID);
|
||||
if (existing) {
|
||||
shell.focusWindow(existing.windowId);
|
||||
ctx.requestShellRender();
|
||||
return;
|
||||
}
|
||||
let created: InProcessWindow | null = null;
|
||||
const store = new EvenHubStoreLayer({
|
||||
launchApp: (appId) => ctx.launchApp(appId),
|
||||
appendLog: ctx.appendLog,
|
||||
});
|
||||
await ctx.launchInProcessApp(EVENHUB_STORE_WINDOW_ID, EVENHUB_STORE_SURFACE_ID, (options) => {
|
||||
created = createInProcessWindow({
|
||||
appId: "evenhub",
|
||||
windowId: EVENHUB_STORE_WINDOW_ID,
|
||||
title: "EvenHub",
|
||||
iconLetter: "EH",
|
||||
icon: "package",
|
||||
drawIcon: makeImageWindowIcon(renderEvenRealitiesLogo, windowIcon("package", "EH")),
|
||||
closeable: true,
|
||||
menuItems: () => (created?.stack.isAtBase() ? store.buildMenuItems() : []),
|
||||
actions: options.actions,
|
||||
baseLayer: store,
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
store.onWindowClosed(options.actions);
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
return created;
|
||||
});
|
||||
await ensureStoreWindow(ctx);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
|
||||
import { permissionDetail, permissionLabel, type EvenHubPermission } from "./permissions";
|
||||
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
@@ -19,10 +19,20 @@ const PERM_GAP = 4;
|
||||
* before installing it or running an uninstalled package. Allow proceeds;
|
||||
* Cancel (or double-click) backs out. A privacy-policy-only app still reaches
|
||||
* this dialog so the user can review the policy before allowing first run.
|
||||
*
|
||||
* When the list overflows the viewport the dialog scrolls: scroll events move
|
||||
* through the content row by row, and only once the bottom is fully visible do
|
||||
* further scrolls move the action selection. The actions are inert until then
|
||||
* (a click just scrolls), so the user can't Allow without having had the whole
|
||||
* list on screen.
|
||||
*/
|
||||
export class EvenHubPermissionDialogLayer implements Layer {
|
||||
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
|
||||
private selectedIndex = 0;
|
||||
/** First content row drawn; rows above it are scrolled off. */
|
||||
private firstRow = 0;
|
||||
/** Scroll position at which the last row is fully visible; set by paint. */
|
||||
private maxFirstRow = 0;
|
||||
private resolved = false;
|
||||
|
||||
constructor(
|
||||
@@ -33,6 +43,11 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
private readonly onCancel: () => void,
|
||||
) {}
|
||||
|
||||
/** Whether the dialog is scrolled to the bottom, making the actions live. */
|
||||
private atBottom(): boolean {
|
||||
return this.firstRow >= this.maxFirstRow;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { height: viewportHeight } = ctx.stack.getBaseSize();
|
||||
@@ -44,48 +59,88 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
const permLabelStep = lineStep(font) + 1;
|
||||
const permDetailStep = lineStep(font) - 1;
|
||||
const actionRowH = listRowHeight(font);
|
||||
const permsHeight = this.permissions.reduce(
|
||||
(sum, permission) => sum + permLabelStep + (permissionDetail(permission) ? permDetailStep : 0) + PERM_GAP,
|
||||
0,
|
||||
);
|
||||
const emptyPermissionsHeight = this.permissions.length === 0 ? permLabelStep + PERM_GAP : 0;
|
||||
const bodyTop = PADDING + headerStep + 6 + permsHeight + emptyPermissionsHeight + 6;
|
||||
const height = Math.min(bodyTop + this.actions().length * actionRowH + PADDING, viewportHeight - 2 * DIALOG_Y);
|
||||
const actions = this.actions();
|
||||
const focused = ctx.stack.isFocused();
|
||||
|
||||
// The dialog body as scrollable rows; each row's height includes the gap
|
||||
// below it, so scrolling and visibility checks work on whole rows.
|
||||
type Row = { height: number; draw: (top: number) => void };
|
||||
const rows: Row[] = [];
|
||||
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
|
||||
rows.push({
|
||||
height: headerStep + 6,
|
||||
draw: (top) => image.drawText(font, left, top, truncateText(font, heading, textWidth), 235),
|
||||
});
|
||||
if (this.permissions.length === 0) {
|
||||
rows.push({
|
||||
height: permLabelStep + PERM_GAP + 2,
|
||||
draw: (top) => image.drawText(font, left, top, "No special permissions requested.", 140),
|
||||
});
|
||||
}
|
||||
for (let index = 0; index < this.permissions.length; index++) {
|
||||
const permission = this.permissions[index]!;
|
||||
const detail = permissionDetail(permission);
|
||||
const gapBelow = PERM_GAP + (index === this.permissions.length - 1 ? 2 : 0);
|
||||
rows.push({
|
||||
height: permLabelStep + (detail ? 0 : gapBelow),
|
||||
draw: (top) => image.drawText(font, left, top, truncateText(font, permissionLabel(permission.name), textWidth), 220),
|
||||
});
|
||||
if (detail) {
|
||||
rows.push({
|
||||
height: permDetailStep + gapBelow,
|
||||
draw: (top) => image.drawText(font, left + 8, top, truncateText(font, detail, textWidth - 8), 140),
|
||||
});
|
||||
}
|
||||
}
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
rows.push({
|
||||
height: actionRowH,
|
||||
draw: (top) => {
|
||||
const selected = this.atBottom() && index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, top, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, top + 3, actions[index]!, selected ? 255 : 200);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
|
||||
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_Y);
|
||||
const visibleHeight = height - 2 * PADDING;
|
||||
|
||||
// The bottommost scroll position: the largest suffix of rows that fits.
|
||||
let maxFirstRow = rows.length;
|
||||
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
|
||||
const rowHeight = rows[maxFirstRow - 1]!.height;
|
||||
if (suffixHeight + rowHeight > visibleHeight) break;
|
||||
suffixHeight += rowHeight;
|
||||
}
|
||||
this.maxFirstRow = maxFirstRow;
|
||||
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
|
||||
|
||||
// Fill 1 (transparent color key is 0), outline for the dialog edge.
|
||||
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;
|
||||
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
|
||||
image.drawText(font, left, y, truncateText(font, heading, textWidth), 235);
|
||||
y += headerStep + 6;
|
||||
|
||||
if (this.permissions.length === 0) {
|
||||
image.drawText(font, left, y, "No special permissions requested.", 140);
|
||||
y += emptyPermissionsHeight;
|
||||
for (let index = this.firstRow; index < rows.length; index++) {
|
||||
const row = rows[index]!;
|
||||
if (y + row.height > DIALOG_Y + PADDING + visibleHeight) break;
|
||||
row.draw(y);
|
||||
y += row.height;
|
||||
}
|
||||
for (const permission of this.permissions) {
|
||||
image.drawText(font, left, y, truncateText(font, permissionLabel(permission.name), textWidth), 220);
|
||||
y += permLabelStep;
|
||||
const detail = permissionDetail(permission);
|
||||
if (detail) {
|
||||
image.drawText(font, left + 8, y, truncateText(font, detail, textWidth - 8), 140);
|
||||
y += permDetailStep;
|
||||
}
|
||||
y += PERM_GAP;
|
||||
}
|
||||
y += 2;
|
||||
|
||||
const focused = ctx.stack.isFocused();
|
||||
const actions = this.actions();
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const rowY = y + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
|
||||
if (maxFirstRow > 0) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
DIALOG_X + DIALOG_WIDTH - 7,
|
||||
DIALOG_Y + PADDING,
|
||||
visibleHeight,
|
||||
this.firstRow,
|
||||
rows.length - maxFirstRow,
|
||||
rows.length,
|
||||
);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
@@ -94,12 +149,24 @@ export class EvenHubPermissionDialogLayer implements Layer {
|
||||
const actions = this.actions();
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = (this.selectedIndex + actions.length - 1) % actions.length;
|
||||
if (this.atBottom() && this.selectedIndex > 0) {
|
||||
this.selectedIndex -= 1;
|
||||
} else if (this.firstRow > 0) {
|
||||
this.firstRow -= 1;
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = (this.selectedIndex + 1) % actions.length;
|
||||
if (!this.atBottom()) {
|
||||
this.firstRow += 1;
|
||||
} else {
|
||||
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
|
||||
}
|
||||
return;
|
||||
case "click":
|
||||
if (!this.atBottom()) {
|
||||
this.firstRow += 1;
|
||||
return;
|
||||
}
|
||||
if (actions[this.selectedIndex] === "Privacy policy") {
|
||||
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
|
||||
} else {
|
||||
|
||||
@@ -38,14 +38,17 @@ import {
|
||||
import { EvenHubApiKeyDialogLayer } from "./api-key-dialog";
|
||||
import {
|
||||
asRecord,
|
||||
declaredMenuItems,
|
||||
eventCaptureContainer,
|
||||
parsePage,
|
||||
readNumber,
|
||||
readOptionalNumber,
|
||||
readString,
|
||||
readTextBrightness,
|
||||
type EvenHubImageContainer,
|
||||
type EvenHubListContainer,
|
||||
type EvenHubContainer,
|
||||
type EvenHubMenuItem,
|
||||
type EvenHubPage,
|
||||
} from "./containers";
|
||||
import { compositePage } from "./compositor";
|
||||
@@ -53,7 +56,7 @@ import { type EvenHubManifest } from "./ehpk";
|
||||
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
|
||||
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
|
||||
import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router";
|
||||
import { evenHubCompassRouter, type EvenHubCompassClient } from "./compass-router";
|
||||
import { evenHubCompassRouter, type EvenHubCompassClient, type EvenHubCompassReading } from "./compass-router";
|
||||
import { type ImuReading } from "../../native/imu";
|
||||
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
@@ -73,6 +76,9 @@ const FOREGROUND_ENTER_EVENT = 4;
|
||||
const FOREGROUND_EXIT_EVENT = 5;
|
||||
const SYSTEM_EXIT_EVENT = 7;
|
||||
const IMU_DATA_REPORT_EVENT = 8;
|
||||
/** SDK 0.0.14: the press gesture, delivered like any other list/text/sys event. */
|
||||
const LONG_PRESS_EVENT = 9;
|
||||
const LONG_PRESS_RELEASE_EVENT = 10;
|
||||
|
||||
/**
|
||||
* Injected into every served HTML document at document start (before any app
|
||||
@@ -440,6 +446,24 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "double-click":
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source));
|
||||
break;
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
case "long-press-release": {
|
||||
// SDK 0.0.14 exposes press and release as ordinary events. The app's
|
||||
// press is the context-menu gesture (tap-then-hold; the shell keeps
|
||||
// the plain long-press for its system menu): the window still opens
|
||||
// its menu on it (that menu is where the app's contextual-menu items
|
||||
// live, and it is the way out of an app that owns double-click), so
|
||||
// an app hears the press as well as seeing the menu — it cannot be
|
||||
// given exclusive use of the gesture.
|
||||
const eventType = event.type === "long-press-release" ? LONG_PRESS_RELEASE_EVENT : LONG_PRESS_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, eventType);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, gestureSource(event.source));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "scroll-up":
|
||||
case "scroll-down": {
|
||||
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
|
||||
@@ -455,6 +479,20 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The contextual-menu entries the running page registered (SDK 0.0.14
|
||||
* `menuObject`), for the window to merge into its own menu. Empty when the
|
||||
* app declared none.
|
||||
*/
|
||||
contextMenuItems(): EvenHubMenuItem[] {
|
||||
return this.page?.menuItems ?? [];
|
||||
}
|
||||
|
||||
/** The user picked one of the app's contextual-menu entries. */
|
||||
selectContextMenuItem(itemID: number): void {
|
||||
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
|
||||
}
|
||||
|
||||
private scrollList(list: EvenHubListContainer, eventType: number): void {
|
||||
// Swipe up selects the previous item, swipe down the next (stock direction).
|
||||
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
|
||||
@@ -542,10 +580,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return this.screenOn;
|
||||
}
|
||||
|
||||
/** A decoded mic frame from the router: hand it to the app as an audioEvent. */
|
||||
/**
|
||||
* A decoded mic frame from the router: hand it to the app as an audioEvent.
|
||||
*
|
||||
* SDK 0.0.14 added per-frame `direction` and `speakerRole`. Neither is
|
||||
* available here — the router hands over plain PCM with no beamforming tag,
|
||||
* and speakerRole is an app-level diarization result the stock host computes
|
||||
* — so both are sent as the fallbacks the SDK documents for a phone mic and
|
||||
* for older hosts.
|
||||
*/
|
||||
deliverAudioPcm(pcm: Uint8Array): void {
|
||||
const audioPcm = Array.from(pcm);
|
||||
this.pushMessage("evenHubEvent", { type: "audioEvent", jsonData: { audioPcm } });
|
||||
this.pushMessage("evenHubEvent", {
|
||||
type: "audioEvent",
|
||||
jsonData: { audioPcm, direction: null, speakerRole: "unknown" },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -757,7 +806,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return 1;
|
||||
}
|
||||
const page = parsePage(data);
|
||||
this.warnOnInvalidPage(page);
|
||||
this.warnOnInvalidPage(page, data);
|
||||
this.page = page;
|
||||
this.pageCreated = true;
|
||||
// The launch focus arrived before the page existed; deliver the ENTER now.
|
||||
@@ -772,14 +821,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private rebuildPage(data: Record<string, unknown>): boolean {
|
||||
// Also serves as create for apps that fall back to rebuild-on-error.
|
||||
const page = parsePage(data);
|
||||
this.warnOnInvalidPage(page);
|
||||
this.warnOnInvalidPage(page, data);
|
||||
this.page = page;
|
||||
this.pageCreated = true;
|
||||
this.windowHooks?.requestRender();
|
||||
return true;
|
||||
}
|
||||
|
||||
private warnOnInvalidPage(page: EvenHubPage): void {
|
||||
private warnOnInvalidPage(page: EvenHubPage, data: Record<string, unknown>): void {
|
||||
const declaredMenuCount = declaredMenuItems(data).length;
|
||||
if (declaredMenuCount > page.menuItems.length) {
|
||||
this.log(
|
||||
`evenhub: dropped ${declaredMenuCount - page.menuItems.length} of ${declaredMenuCount} contextual menu ` +
|
||||
"items (max 10, itemID must be non-zero and unique, itemName at most 32 UTF-8 bytes)",
|
||||
);
|
||||
}
|
||||
// Stock rejects these outright; we accept-and-log until rejection is
|
||||
// proven necessary for compatibility.
|
||||
const captures = page.containers.filter((c) => c.kind !== "image" && c.isEventCapture).length;
|
||||
@@ -813,6 +869,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
} else {
|
||||
container.content = content;
|
||||
}
|
||||
// SDK 0.0.14: textColor on an incremental update sets the container's
|
||||
// brightness; omitting it keeps whatever level the container has.
|
||||
const brightness = readTextBrightness(data);
|
||||
if (brightness !== undefined) container.textColor = brightness;
|
||||
this.windowHooks?.requestRender();
|
||||
return true;
|
||||
}
|
||||
@@ -953,8 +1013,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
|
||||
// ----- compass (EvenHubCompassClient) -----
|
||||
|
||||
deliverCompass(headingDegrees: number): void {
|
||||
this.pushExtEvent("compass", { headingDegrees });
|
||||
deliverCompass(reading: EvenHubCompassReading): void {
|
||||
this.pushExtEvent("compass", reading);
|
||||
}
|
||||
|
||||
private setCompass(enable: boolean): void {
|
||||
|
||||
@@ -8,13 +8,12 @@ import { TextViewerLayer } from "../files/text-viewer";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
getInstalledEvenHubApp,
|
||||
installEvenHubPackageBytes,
|
||||
installedEvenHubAppId,
|
||||
readEvenHubPackageManifestBytes,
|
||||
setInstalledEvenHubIcon,
|
||||
type EvenHubInstallIcon,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
import { EvenHubPermissionDialogLayer } from "./permission-dialog";
|
||||
import { cleanError, installStoreApp } from "./store-install";
|
||||
import { describeUpdate } from "./updates";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const X = 18;
|
||||
@@ -22,6 +21,8 @@ const X = 18;
|
||||
export type EvenHubStoreDetailOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
/** A package was installed, updated, or reinstalled from this page. */
|
||||
onInstalled?: (app: InstalledEvenHubApp) => void;
|
||||
};
|
||||
|
||||
/** Storefront metadata and the Install/Launch action for one public app. */
|
||||
@@ -46,7 +47,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionRowH = listRowHeight(font) + 4;
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : installed ? "Launch" : "Install"];
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)];
|
||||
|
||||
// Detail lines as (text, shade, extra gap below); built per candidate
|
||||
// width so the layout choice below can measure before drawing.
|
||||
@@ -70,7 +71,13 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
lines.push({ text: truncateText(font, line, textWidth), value: 125, gapAfter: 0 });
|
||||
}
|
||||
if (installed && !this.status) {
|
||||
lines.push({ text: `Installed version ${installed.version}`, value: 155, gapAfter: 0 });
|
||||
const standing = describeUpdate(installed, this.app);
|
||||
const note = standing.packageMissing
|
||||
? " · package missing, reinstall"
|
||||
: standing.updateAvailable
|
||||
? " · update available"
|
||||
: "";
|
||||
lines.push({ text: truncateText(font, `Installed version ${installed.version}${note}`, textWidth), value: 155, gapAfter: 0 });
|
||||
}
|
||||
if (this.status) {
|
||||
lines.push({ text: truncateText(font, this.status, textWidth), value: 180, gapAfter: 0 });
|
||||
@@ -138,77 +145,39 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
}
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
if (installed) {
|
||||
if (installed && this.primaryActionLabel(installed) === "Launch") {
|
||||
await this.options.launchApp(installedEvenHubAppId(installed.packageId));
|
||||
return;
|
||||
}
|
||||
|
||||
await this.prepareInstall(ctx);
|
||||
await this.install(ctx);
|
||||
}
|
||||
|
||||
private async prepareInstall(ctx: LayerContext): Promise<void> {
|
||||
/** Install for a new app; Update / Reinstall when the installed copy is stale or gone. */
|
||||
private primaryActionLabel(installed: InstalledEvenHubApp | null): string {
|
||||
if (!installed) return "Install";
|
||||
const standing = describeUpdate(installed, this.app);
|
||||
if (standing.packageMissing) return "Reinstall";
|
||||
return standing.updateAvailable ? "Update" : "Launch";
|
||||
}
|
||||
|
||||
private async install(ctx: LayerContext): Promise<void> {
|
||||
this.working = true;
|
||||
this.status = `Downloading ${this.app.name}...`;
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const [download, icon] = await Promise.all([
|
||||
evenHubApi.downloadApp(this.app.packageId),
|
||||
this.app.iconPath
|
||||
? evenHubApi.downloadPublicAsset(this.app.iconPath).catch((error) => {
|
||||
this.options.appendLog(`evenhub store: icon unavailable for ${this.app.packageId}: ${cleanError(error)}`);
|
||||
return undefined;
|
||||
})
|
||||
: Promise.resolve(undefined),
|
||||
]);
|
||||
const manifest = readEvenHubPackageManifestBytes(download.bytes);
|
||||
if (!manifest) throw new Error("The downloaded EHPK manifest could not be read.");
|
||||
const privacyPolicyUrl = download.privacyPolicyUrl || manifest.privacyPolicyUrl;
|
||||
this.working = false;
|
||||
this.status = "";
|
||||
ctx.actions.requestRender();
|
||||
if (manifest.permissions.length > 0 || privacyPolicyUrl) {
|
||||
ctx.stack.push(
|
||||
new EvenHubPermissionDialogLayer(
|
||||
manifest.name,
|
||||
manifest.permissions,
|
||||
privacyPolicyUrl,
|
||||
() => {
|
||||
void this.installAndLaunch(ctx, download.bytes, icon);
|
||||
},
|
||||
() => {
|
||||
this.status = "Installation canceled.";
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
),
|
||||
);
|
||||
const result = await installStoreApp(ctx, this.app, {
|
||||
appendLog: this.options.appendLog,
|
||||
onStatus: (status) => {
|
||||
this.status = status;
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
if (!result) {
|
||||
this.status = "Installation canceled.";
|
||||
return;
|
||||
}
|
||||
await this.installAndLaunch(ctx, download.bytes, icon);
|
||||
} catch (error) {
|
||||
this.working = false;
|
||||
this.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private async installAndLaunch(
|
||||
ctx: LayerContext,
|
||||
bytes: Uint8Array,
|
||||
icon: EvenHubInstallIcon | undefined,
|
||||
): Promise<void> {
|
||||
this.working = true;
|
||||
this.status = "Installing...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const result = installEvenHubPackageBytes(bytes, {
|
||||
expectedPackageId: this.app.packageId,
|
||||
icon,
|
||||
});
|
||||
this.options.appendLog(
|
||||
`evenhub store: installed ${result.packageId} ${result.version} (${bytes.length} bytes)`,
|
||||
);
|
||||
this.status = "Installed";
|
||||
this.options.onInstalled?.(result);
|
||||
ctx.actions.requestRender();
|
||||
await this.options.launchApp(installedEvenHubAppId(result.packageId));
|
||||
} catch (error) {
|
||||
@@ -258,7 +227,3 @@ function formatBytes(value: number): string {
|
||||
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`;
|
||||
return `${(value / (1024 * 1024)).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* The storefront install pipeline shared by the app detail page and the
|
||||
* Updates tab: download the package and its icon, confirm permissions when
|
||||
* that is warranted, close running instances, and store the package.
|
||||
*/
|
||||
import { type LayerContext } from "../../ui/layers";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import { type EvenHubManifest } from "./ehpk";
|
||||
import {
|
||||
installEvenHubPackageBytes,
|
||||
installedEvenHubPackagePath,
|
||||
readEvenHubPackageManifest,
|
||||
readEvenHubPackageManifestBytes,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
import { closeRunningPackage } from "./manager";
|
||||
import { EvenHubPermissionDialogLayer } from "./permission-dialog";
|
||||
|
||||
export type StoreInstallOptions = {
|
||||
appendLog: (message: string) => void;
|
||||
/** Progress text for the caller's status line ("Downloading X...", "Installing..."). */
|
||||
onStatus: (status: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Install (or update/reinstall) one storefront app. Resolves to the new
|
||||
* registry entry, or null when the user declined the permission dialog.
|
||||
* Failures throw with a user-presentable message.
|
||||
*
|
||||
* The permission dialog is shown for a first install, and for an update
|
||||
* whose manifest declares permissions the currently installed package did
|
||||
* not (or a privacy policy it did not); an update that asks for nothing new
|
||||
* goes straight through.
|
||||
*/
|
||||
export async function installStoreApp(
|
||||
ctx: LayerContext,
|
||||
app: EvenHubStoreApp,
|
||||
options: StoreInstallOptions,
|
||||
): Promise<InstalledEvenHubApp | null> {
|
||||
options.onStatus(`Downloading ${app.name}...`);
|
||||
const [download, icon] = await Promise.all([
|
||||
evenHubApi.downloadApp(app.packageId),
|
||||
app.iconPath
|
||||
? evenHubApi.downloadPublicAsset(app.iconPath).catch((error) => {
|
||||
options.appendLog(`evenhub store: icon unavailable for ${app.packageId}: ${cleanError(error)}`);
|
||||
return undefined;
|
||||
})
|
||||
: Promise.resolve(undefined),
|
||||
]);
|
||||
const manifest = readEvenHubPackageManifestBytes(download.bytes);
|
||||
if (!manifest) throw new Error("The downloaded EHPK manifest could not be read.");
|
||||
const privacyPolicyUrl = download.privacyPolicyUrl || manifest.privacyPolicyUrl;
|
||||
const needsPrompt = (manifest.permissions.length > 0 || privacyPolicyUrl)
|
||||
&& !alreadyGranted(app.packageId, manifest);
|
||||
if (needsPrompt) {
|
||||
options.onStatus("");
|
||||
ctx.actions.requestRender();
|
||||
const accepted = await new Promise<boolean>((resolve) => {
|
||||
ctx.stack.push(
|
||||
new EvenHubPermissionDialogLayer(
|
||||
manifest.name,
|
||||
manifest.permissions,
|
||||
privacyPolicyUrl,
|
||||
() => resolve(true),
|
||||
() => resolve(false),
|
||||
),
|
||||
);
|
||||
});
|
||||
if (!accepted) return null;
|
||||
}
|
||||
|
||||
options.onStatus("Installing...");
|
||||
ctx.actions.requestRender();
|
||||
// A running instance would keep serving the old unpacked runtime; every
|
||||
// launch re-unpacks from the stored EHPK, so closing it is enough.
|
||||
closeRunningPackage(app.packageId);
|
||||
const result = installEvenHubPackageBytes(download.bytes, { expectedPackageId: app.packageId, icon });
|
||||
options.appendLog(`evenhub store: installed ${result.packageId} ${result.version} (${download.bytes.length} bytes)`);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the installed package already declares every permission the new
|
||||
* manifest asks for. (The store's privacy-policy link is not compared: the
|
||||
* download metadata carries it as a CDN path while the installed app.json
|
||||
* usually has none, so comparing would re-prompt on every update.)
|
||||
*/
|
||||
function alreadyGranted(packageId: string, manifest: EvenHubManifest): boolean {
|
||||
const current = readEvenHubPackageManifest(installedEvenHubPackagePath(packageId));
|
||||
if (!current) return false;
|
||||
const granted = new Set(current.permissions);
|
||||
return manifest.permissions.every((permission) => granted.has(permission));
|
||||
}
|
||||
|
||||
export function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
+594
-128
@@ -3,39 +3,119 @@ import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp } from "./even-api";
|
||||
import { ConfigSettingString } from "../../ui/dashboard-settings";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api";
|
||||
import { clearEvenHubLoginForm, clearEvenHubSession, clearTransientEvenHubSession, evenHubLoginEmailSetting, evenHubLoginPasswordSetting, evenHubRememberMeSetting, resetEvenHubLoginForm } from "./credentials";
|
||||
import { getInstalledEvenHubApps, uninstallEvenHubPackage, type InstalledEvenHubApp } from "./installed-apps";
|
||||
import { EvenHubStoreDetailLayer } from "./store-detail-layer";
|
||||
import { cleanError, installStoreApp } from "./store-install";
|
||||
import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
|
||||
const LIST_X = 18;
|
||||
/** Horizontal gap between tab labels. */
|
||||
const TAB_GAP = 18;
|
||||
|
||||
/** Header: title line, then the status/subtitle line, then a small gap. */
|
||||
/** Header: title + tabs line, then the status/subtitle line, then a small gap. */
|
||||
function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight + 6;
|
||||
}
|
||||
|
||||
/** Last search typed on the phone; kept so reopening the store shows it again. */
|
||||
export const evenHubSearchQuerySetting = new ConfigSettingString({
|
||||
id: "evenhub-search-query",
|
||||
label: "Search",
|
||||
storageKey: "evenhub.store.searchQuery",
|
||||
defaultValue: "",
|
||||
editorTitle: "Search EvenHub",
|
||||
glassesEditTitle: "Search EvenHub",
|
||||
normalize: (value) => (value ?? "").replace(/\s+/g, " ").trim(),
|
||||
});
|
||||
|
||||
type StoreTab = "top" | "new" | "search" | "updates";
|
||||
|
||||
const TABS: { id: StoreTab; label: string }[] = [
|
||||
{ id: "top", label: "Top" },
|
||||
{ id: "new", label: "New" },
|
||||
{ id: "search", label: "Search" },
|
||||
{ id: "updates", label: "Updates" },
|
||||
];
|
||||
|
||||
type StoreRow =
|
||||
/** A storefront app; `update` is set on the Updates tab. */
|
||||
| { kind: "app"; app: EvenHubStoreApp; update?: EvenHubAppUpdate }
|
||||
/** The Search tab's first row: shows the query and opens the phone editor. */
|
||||
| { kind: "search-query" }
|
||||
/** A registry entry whose package is gone and which the store no longer lists. */
|
||||
| { kind: "orphan"; update: EvenHubAppUpdate };
|
||||
|
||||
type TabState = {
|
||||
rows: StoreRow[];
|
||||
/** Server-reported total, or 0 when unknown. */
|
||||
total: number;
|
||||
nextPage: number;
|
||||
/** No further pages: the total was reached, or a page added nothing new. */
|
||||
exhausted: boolean;
|
||||
/** A load has completed (or failed) at least once. */
|
||||
loaded: boolean;
|
||||
loading: boolean;
|
||||
/** Progress or error text for the subtitle line. */
|
||||
status: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Search only: the query the rows belong to. */
|
||||
query: string;
|
||||
};
|
||||
|
||||
function emptyTab(): TabState {
|
||||
return {
|
||||
rows: [],
|
||||
total: 0,
|
||||
nextPage: 1,
|
||||
exhausted: false,
|
||||
loaded: false,
|
||||
loading: false,
|
||||
status: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
query: "",
|
||||
};
|
||||
}
|
||||
|
||||
export type EvenHubStoreLayerOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
/** Browse the public EvenHub leaderboard and download one package to run. */
|
||||
/**
|
||||
* The EvenHub storefront: Top (leaderboard), New (by first publication),
|
||||
* Search, and Updates (installed apps that are stale or whose package is
|
||||
* missing). The tab row is its own focus level above the list: double-click
|
||||
* in the list focuses the tabs, scrolling there switches tabs, and a click
|
||||
* returns to the list.
|
||||
*/
|
||||
export class EvenHubStoreLayer implements Layer {
|
||||
private apps: EvenHubStoreApp[] = [];
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private total = 0;
|
||||
private nextPage = 1;
|
||||
private readonly tabs: Record<StoreTab, TabState> = {
|
||||
top: emptyTab(),
|
||||
new: emptyTab(),
|
||||
search: emptyTab(),
|
||||
updates: emptyTab(),
|
||||
};
|
||||
private activeTab: StoreTab = "top";
|
||||
private focus: "list" | "tabs" = "list";
|
||||
private started = false;
|
||||
private loading = false;
|
||||
private showingLogin = !isEvenHubStoreConfigured();
|
||||
private status = this.showingLogin ? "Sign in to browse public apps." : "";
|
||||
private credentialEditorOpen = false;
|
||||
private loginStatus = this.showingLogin ? "Sign in to browse public apps." : "";
|
||||
private loginBusy = false;
|
||||
private phoneEditor: "none" | "credentials" | "search" = "none";
|
||||
private closed = false;
|
||||
/** Serializes the Update All action; also blocks per-row installs meanwhile. */
|
||||
private updatingAll = false;
|
||||
/** An app page requested while the login form was up; opened once signed in. */
|
||||
private pendingPackage: InstalledEvenHubApp | null = null;
|
||||
|
||||
constructor(private readonly options: EvenHubStoreLayerOptions) {
|
||||
if (this.showingLogin) resetEvenHubLoginForm();
|
||||
@@ -47,7 +127,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
if (this.showingLogin) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else {
|
||||
void this.reload(ctx);
|
||||
void this.ensureTabLoaded(ctx, this.activeTab);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,11 +137,9 @@ export class EvenHubStoreLayer implements Layer {
|
||||
const headerH = headerHeight(font);
|
||||
image.drawText(font, LIST_X, 8, "EvenHub", 220);
|
||||
|
||||
const subtitle = this.status || (this.total ? `${this.apps.length} of ${this.total} public apps` : "Public apps");
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125);
|
||||
|
||||
if (this.showingLogin) {
|
||||
const message = this.loading
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, this.loginStatus, width - LIST_X * 2), 125);
|
||||
const message = this.loginBusy
|
||||
? "Signing in..."
|
||||
: "Enter your Even account email and password in the phone app.";
|
||||
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
|
||||
@@ -71,69 +149,181 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
if (this.apps.length === 0) {
|
||||
const message = this.loading ? "Loading apps..." : this.status || "No apps returned.";
|
||||
image.drawText(font, LIST_X, headerH + 22, truncateText(font, message, width - LIST_X * 2), 190);
|
||||
} else {
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, this.apps.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
this.scrollRow = scrollToKeepSelectionVisible(
|
||||
this.scrollRow,
|
||||
this.selectedIndex,
|
||||
visibleRows,
|
||||
this.apps.length,
|
||||
);
|
||||
const last = Math.min(this.apps.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < last; index++) {
|
||||
const app = this.apps[index]!;
|
||||
const y = headerH + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, ctx.stack.isFocused(), 5);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, app.name, width - LIST_X * 2), 215);
|
||||
const detail = app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`;
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (this.apps.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, this.scrollRow, visibleRows, this.apps.length);
|
||||
this.paintTabs(image, font, ctx);
|
||||
|
||||
const tab = this.tabs[this.activeTab];
|
||||
const subtitle = tab.status || this.subtitle(this.activeTab, tab);
|
||||
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125);
|
||||
|
||||
if (tab.rows.length === 0) {
|
||||
const message = tab.loading ? "Loading..." : this.emptyMessage(this.activeTab, tab);
|
||||
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).slice(0, 3).entries()) {
|
||||
image.drawText(font, LIST_X, headerH + 22 + index * lineStep(font), line, 190);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length);
|
||||
const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows);
|
||||
const listFocused = ctx.stack.isFocused() && this.focus === "list";
|
||||
for (let index = tab.scrollRow; index < last; index++) {
|
||||
const row = tab.rows[index]!;
|
||||
const y = headerH + (index - tab.scrollRow) * rowH;
|
||||
if (index === tab.selectedIndex) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5);
|
||||
}
|
||||
const { title, detail, titleValue } = this.rowText(row);
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue);
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (tab.rows.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Tab labels to the right of the title; the active one is bright and underlined. */
|
||||
private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext): void {
|
||||
const tabsFocused = ctx.stack.isFocused() && this.focus === "tabs";
|
||||
let x = LIST_X + font.measureText("EvenHub") + 28;
|
||||
for (const tab of TABS) {
|
||||
const label = this.tabLabel(tab.id);
|
||||
const w = font.measureText(label);
|
||||
const active = tab.id === this.activeTab;
|
||||
if (active) {
|
||||
if (tabsFocused) {
|
||||
drawSelectionHighlight(image, x - 6, 5, w + 12, font.lineHeight + 6, true, 4);
|
||||
} else {
|
||||
image.fillRect(x, 8 + font.lineHeight + 1, w, 1, 150);
|
||||
}
|
||||
}
|
||||
image.drawText(font, x, 8, label, active ? 235 : 120);
|
||||
x += w + TAB_GAP;
|
||||
}
|
||||
}
|
||||
|
||||
private tabLabel(tab: StoreTab): string {
|
||||
const label = TABS.find((entry) => entry.id === tab)!.label;
|
||||
if (tab !== "updates") return label;
|
||||
const updates = this.tabs.updates;
|
||||
const count = updates.rows.filter((row) => row.kind !== "search-query").length;
|
||||
return updates.loaded && count > 0 ? `${label} (${count})` : label;
|
||||
}
|
||||
|
||||
private subtitle(tab: StoreTab, state: TabState): string {
|
||||
const count = state.rows.length;
|
||||
switch (tab) {
|
||||
case "top":
|
||||
return state.total ? `${count} of ${state.total} public apps` : count ? `${count} public apps` : "Public apps by popularity";
|
||||
case "new":
|
||||
return count ? `${count} newest apps` : "Recently published apps";
|
||||
case "search": {
|
||||
const results = count - 1;
|
||||
if (!state.query) return "Search public apps";
|
||||
return results === 1 ? `1 result for "${state.query}"` : `${results} results for "${state.query}"`;
|
||||
}
|
||||
case "updates":
|
||||
if (!state.loaded) return "Installed apps with newer versions";
|
||||
return count === 0 ? "All installed apps are up to date" : count === 1 ? "1 app needs attention" : `${count} apps need attention`;
|
||||
}
|
||||
}
|
||||
|
||||
private emptyMessage(tab: StoreTab, state: TabState): string {
|
||||
if (state.status) return state.status;
|
||||
switch (tab) {
|
||||
case "updates":
|
||||
return getInstalledEvenHubApps().length === 0
|
||||
? "No apps are installed from EvenHub."
|
||||
: "Every installed app is at its latest version.";
|
||||
default:
|
||||
return "No apps returned.";
|
||||
}
|
||||
}
|
||||
|
||||
private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.showingLogin) {
|
||||
if (event.type === "click" && !this.loading) {
|
||||
if (event.type === "click" && !this.loginBusy) {
|
||||
this.openCredentialEditor(ctx);
|
||||
} else if (event.type === "double-click") {
|
||||
shell.yieldFocusToSidebar();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this.focus === "tabs") {
|
||||
await this.handleTabInput(event, ctx);
|
||||
return;
|
||||
}
|
||||
const tab = this.tabs[this.activeTab];
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
tab.selectedIndex = Math.max(0, tab.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(Math.max(0, this.apps.length - 1), this.selectedIndex + 1);
|
||||
if (this.selectedIndex >= this.apps.length - 4 && this.apps.length < this.total) {
|
||||
void this.loadNextPage(ctx);
|
||||
}
|
||||
tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1);
|
||||
if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
return;
|
||||
case "click":
|
||||
if (!this.loading) {
|
||||
const app = this.apps[this.selectedIndex];
|
||||
if (app) {
|
||||
ctx.stack.push(
|
||||
new EvenHubStoreDetailLayer(app, {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
}),
|
||||
);
|
||||
}
|
||||
await this.activateRow(ctx, tab.rows[tab.selectedIndex]);
|
||||
return;
|
||||
case "double-click":
|
||||
this.focus = "tabs";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async handleTabInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
const index = TABS.findIndex((tab) => tab.id === this.activeTab);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.switchTab(ctx, TABS[Math.max(0, index - 1)]!.id);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.switchTab(ctx, TABS[Math.min(TABS.length - 1, index + 1)]!.id);
|
||||
return;
|
||||
case "click":
|
||||
this.focus = "list";
|
||||
if (this.activeTab === "search" && !evenHubSearchQuerySetting.get()) {
|
||||
this.openSearchEditor(ctx);
|
||||
}
|
||||
return;
|
||||
case "double-click":
|
||||
@@ -144,107 +334,366 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
if (this.showingLogin) return [];
|
||||
return [
|
||||
{
|
||||
label: "Refresh",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.reload(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Log Out",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearEvenHubSession();
|
||||
this.enterLogin(ctx, "Signed out.");
|
||||
},
|
||||
},
|
||||
];
|
||||
private switchTab(ctx: LayerContext, tab: StoreTab): void {
|
||||
if (tab === this.activeTab) return;
|
||||
if (this.activeTab === "search") this.closePhoneEditor(ctx.actions, "search");
|
||||
this.activeTab = tab;
|
||||
void this.ensureTabLoaded(ctx, tab);
|
||||
}
|
||||
|
||||
closeCredentialEditor(actions: Pick<LayerActions, "endTextSettingEdit">): void {
|
||||
if (!this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = false;
|
||||
void actions.endTextSettingEdit();
|
||||
private async activateRow(ctx: LayerContext, row: StoreRow | undefined): Promise<void> {
|
||||
if (!row) return;
|
||||
switch (row.kind) {
|
||||
case "search-query":
|
||||
this.openSearchEditor(ctx);
|
||||
return;
|
||||
case "orphan": {
|
||||
// Nothing can be reinstalled; the only useful action is to drop the
|
||||
// dead registry entry so the launcher stops offering it.
|
||||
const { packageId, name } = row.update.installed;
|
||||
if (uninstallEvenHubPackage(packageId)) {
|
||||
this.options.appendLog(`evenhub store: forgot missing package ${packageId}`);
|
||||
this.removeUpdateRow(packageId);
|
||||
this.tabs.updates.status = `Forgot ${name}.`;
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "app":
|
||||
if (this.updatingAll) return;
|
||||
ctx.stack.push(
|
||||
new EvenHubStoreDetailLayer(row.app, {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (installed) => this.onInstalled(installed),
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jump to an installed app's store page (the launcher's route for a package
|
||||
* whose file is missing). Any page already open above the list is replaced.
|
||||
* Store metadata is filled in by the detail page itself; until then a
|
||||
* placeholder built from the registry entry stands in.
|
||||
*/
|
||||
showInstalledPackage(stack: LayerStack, installed: InstalledEvenHubApp): void {
|
||||
if (this.showingLogin) {
|
||||
this.pendingPackage = installed;
|
||||
return;
|
||||
}
|
||||
this.pendingPackage = null;
|
||||
stack.clearToBase();
|
||||
stack.push(
|
||||
new EvenHubStoreDetailLayer(placeholderStoreApp(installed), {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (result) => this.onInstalled(result),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/** Dictated text becomes a search (the shell's "Type Into App" / voice input). */
|
||||
receiveTextInput(text: string, ctx: LayerContext): void {
|
||||
if (this.showingLogin) return;
|
||||
const query = evenHubSearchQuerySetting.set(text);
|
||||
if (!query) return;
|
||||
this.closePhoneEditor(ctx.actions, "search");
|
||||
this.activeTab = "search";
|
||||
this.focus = "list";
|
||||
void this.runSearch(ctx, query);
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
if (this.showingLogin) return [];
|
||||
const items: MenuItem[] = [];
|
||||
if (this.activeTab === "search") {
|
||||
items.push({
|
||||
label: "Search...",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
this.focus = "list";
|
||||
this.openSearchEditor(ctx);
|
||||
},
|
||||
});
|
||||
}
|
||||
if (this.activeTab === "updates" && this.updatableRows().length > 0) {
|
||||
items.push({
|
||||
label: this.updatingAll ? "Updating..." : "Update All",
|
||||
disabled: () => this.updatingAll,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.updateAll(ctx);
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: this.activeTab === "updates" ? "Check Again" : "Refresh",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
void this.reload(ctx, this.activeTab);
|
||||
},
|
||||
});
|
||||
items.push({
|
||||
label: "Log Out",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearEvenHubSession();
|
||||
this.enterLogin(ctx, "Signed out.");
|
||||
},
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
onWindowClosed(actions: Pick<LayerActions, "endTextSettingEdit">): void {
|
||||
this.closed = true;
|
||||
this.closeCredentialEditor(actions);
|
||||
this.closePhoneEditor(actions);
|
||||
clearTransientEvenHubSession();
|
||||
}
|
||||
|
||||
private async reload(ctx: LayerContext): Promise<void> {
|
||||
if (this.loading) return;
|
||||
this.apps = [];
|
||||
this.total = 0;
|
||||
this.nextPage = 1;
|
||||
this.selectedIndex = 0;
|
||||
this.scrollRow = 0;
|
||||
this.status = "";
|
||||
// ---- loading -----------------------------------------------------------
|
||||
|
||||
private ensureTabLoaded(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
const state = this.tabs[tab];
|
||||
if (state.loaded || state.loading) return Promise.resolve();
|
||||
return this.reload(ctx, tab);
|
||||
}
|
||||
|
||||
private async reload(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
if (!isEvenHubStoreConfigured()) {
|
||||
this.enterLogin(ctx);
|
||||
return;
|
||||
}
|
||||
await this.loadNextPage(ctx);
|
||||
if (this.tabs[tab].loading) return;
|
||||
switch (tab) {
|
||||
case "top":
|
||||
case "new":
|
||||
this.tabs[tab] = emptyTab();
|
||||
await this.loadNextPage(ctx, tab);
|
||||
return;
|
||||
case "search":
|
||||
await this.runSearch(ctx, evenHubSearchQuerySetting.get());
|
||||
return;
|
||||
case "updates":
|
||||
await this.checkUpdates(ctx);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async loadNextPage(ctx: LayerContext): Promise<void> {
|
||||
if (this.loading || (this.total > 0 && this.apps.length >= this.total)) return;
|
||||
this.loading = true;
|
||||
this.status = this.apps.length ? "Loading more apps..." : "Loading apps...";
|
||||
private async loadNextPage(ctx: LayerContext, tab: StoreTab): Promise<void> {
|
||||
const state = this.tabs[tab];
|
||||
if (state.loading || state.exhausted) return;
|
||||
if (tab === "search" && !state.query) return;
|
||||
if (tab === "updates") return;
|
||||
state.loading = true;
|
||||
state.status = state.rows.length ? "Loading more apps..." : "Loading apps...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
const page = await evenHubApi.listApps(this.nextPage);
|
||||
const seen = new Set(this.apps.map((app) => app.packageId));
|
||||
this.apps.push(...page.apps.filter((app) => !seen.has(app.packageId)));
|
||||
this.total = page.total;
|
||||
this.nextPage = page.page + 1;
|
||||
this.status = "";
|
||||
} catch (error) {
|
||||
const message = cleanError(error);
|
||||
if (error instanceof EvenHubAuthenticationError) {
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
return;
|
||||
const page = await this.fetchPage(tab, state.nextPage, state.query);
|
||||
const seen = new Set(state.rows.map((row) => (row.kind === "app" ? row.app.packageId : "")));
|
||||
const fresh = page.apps.filter((app) => !seen.has(app.packageId));
|
||||
state.rows.push(...fresh.map((app): StoreRow => ({ kind: "app", app })));
|
||||
state.total = page.total;
|
||||
state.nextPage = page.page + 1;
|
||||
// A page that adds nothing (or a short page with no known total) means
|
||||
// the endpoint has no more to give — or ignores paging altogether.
|
||||
state.exhausted = (state.total > 0 && state.rows.length >= state.total)
|
||||
|| fresh.length === 0
|
||||
|| (state.total === 0 && page.apps.length < page.pageSize);
|
||||
state.status = "";
|
||||
if (tab === "top" && state.nextPage === 2 && !this.tabs.updates.loaded && !this.tabs.updates.loading) {
|
||||
// Quietly find out whether the Updates tab deserves a count.
|
||||
void this.checkUpdates(ctx, true);
|
||||
}
|
||||
this.status = message;
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
} catch (error) {
|
||||
if (this.handleAuthError(ctx, error)) return;
|
||||
state.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${state.status}`);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
state.loaded = true;
|
||||
state.loading = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private fetchPage(tab: StoreTab, page: number, query: string): Promise<EvenHubStorePage> {
|
||||
switch (tab) {
|
||||
case "new":
|
||||
return evenHubApi.listNewApps(page);
|
||||
case "search":
|
||||
return evenHubApi.searchApps(query, page);
|
||||
default:
|
||||
return evenHubApi.listApps(page);
|
||||
}
|
||||
}
|
||||
|
||||
private async runSearch(ctx: LayerContext, query: string): Promise<void> {
|
||||
const state = emptyTab();
|
||||
state.query = query;
|
||||
state.rows = [{ kind: "search-query" }];
|
||||
state.loaded = !query;
|
||||
// Keep the cursor on the first result once there are some.
|
||||
state.selectedIndex = query ? 1 : 0;
|
||||
this.tabs.search = state;
|
||||
ctx.actions.requestRender();
|
||||
if (query) await this.loadNextPage(ctx, "search");
|
||||
}
|
||||
|
||||
private async checkUpdates(ctx: LayerContext, quiet = false): Promise<void> {
|
||||
const state = this.tabs.updates;
|
||||
if (state.loading) return;
|
||||
state.loading = true;
|
||||
state.status = "";
|
||||
const installedCount = getInstalledEvenHubApps().length;
|
||||
if (!quiet) {
|
||||
state.status = installedCount ? `Checking ${installedCount} installed apps...` : "";
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
try {
|
||||
const updates = await checkForEvenHubUpdates((done, total) => {
|
||||
if (quiet) return;
|
||||
state.status = `Checking installed apps (${done}/${total})...`;
|
||||
ctx.actions.requestRender();
|
||||
});
|
||||
const rows: StoreRow[] = updates.map((update) =>
|
||||
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
|
||||
);
|
||||
state.rows = rows;
|
||||
state.selectedIndex = 0;
|
||||
state.scrollRow = 0;
|
||||
state.status = "";
|
||||
state.exhausted = true;
|
||||
} catch (error) {
|
||||
if (this.handleAuthError(ctx, error)) return;
|
||||
state.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: update check failed: ${state.status}`);
|
||||
} finally {
|
||||
state.loaded = true;
|
||||
state.loading = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private updatableRows(): Extract<StoreRow, { kind: "app" }>[] {
|
||||
return this.tabs.updates.rows.filter(
|
||||
(row): row is Extract<StoreRow, { kind: "app" }> => row.kind === "app" && row.update !== undefined,
|
||||
);
|
||||
}
|
||||
|
||||
/** Install every pending update in turn; a declined permission dialog skips that app. */
|
||||
private async updateAll(ctx: LayerContext): Promise<void> {
|
||||
if (this.updatingAll) return;
|
||||
this.updatingAll = true;
|
||||
const state = this.tabs.updates;
|
||||
const pending = this.updatableRows();
|
||||
let installed = 0;
|
||||
const failures: string[] = [];
|
||||
try {
|
||||
for (const [index, row] of pending.entries()) {
|
||||
if (this.closed) return;
|
||||
const prefix = `${index + 1}/${pending.length} ${row.app.name}: `;
|
||||
try {
|
||||
const result = await installStoreApp(ctx, row.app, {
|
||||
appendLog: this.options.appendLog,
|
||||
onStatus: (status) => {
|
||||
state.status = prefix + (status || "Confirm permissions on the glasses...");
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
if (result) {
|
||||
installed++;
|
||||
this.onInstalled(result);
|
||||
} else {
|
||||
failures.push(`${row.app.name} (declined)`);
|
||||
}
|
||||
} catch (error) {
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: update of ${row.app.packageId} failed: ${message}`);
|
||||
failures.push(row.app.name);
|
||||
}
|
||||
}
|
||||
state.status = failures.length
|
||||
? `Updated ${installed}; skipped ${failures.join(", ")}`
|
||||
: installed === 1 ? "Updated 1 app." : `Updated ${installed} apps.`;
|
||||
} finally {
|
||||
this.updatingAll = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
/** After any install, drop the Updates row it satisfied (from the detail page or Update All). */
|
||||
private onInstalled(installed: InstalledEvenHubApp): void {
|
||||
const row = this.tabs.updates.rows.find(
|
||||
(entry) => entry.kind === "app" && entry.app.packageId === installed.packageId,
|
||||
);
|
||||
if (row?.kind !== "app" || !row.update) return;
|
||||
const standing = describeUpdate(installed, row.app);
|
||||
if (!standing.updateAvailable && !standing.packageMissing) this.removeUpdateRow(installed.packageId);
|
||||
}
|
||||
|
||||
private removeUpdateRow(packageId: string): void {
|
||||
const state = this.tabs.updates;
|
||||
state.rows = state.rows.filter((row) =>
|
||||
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
|
||||
);
|
||||
state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1));
|
||||
}
|
||||
|
||||
// ---- login and phone editors --------------------------------------------
|
||||
|
||||
/** Route a rejected session back to the login form; true if it was one. */
|
||||
private handleAuthError(ctx: LayerContext, error: unknown): boolean {
|
||||
if (!(error instanceof EvenHubAuthenticationError)) return false;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void {
|
||||
const wasShowingLogin = this.showingLogin;
|
||||
this.closePhoneEditor(ctx.actions, "search");
|
||||
this.showingLogin = true;
|
||||
this.loading = false;
|
||||
this.apps = [];
|
||||
this.total = 0;
|
||||
this.status = status;
|
||||
this.loginBusy = false;
|
||||
this.loginStatus = status;
|
||||
for (const tab of TABS) this.tabs[tab.id] = emptyTab();
|
||||
this.focus = "list";
|
||||
if (!wasShowingLogin) resetEvenHubLoginForm();
|
||||
this.openCredentialEditor(ctx);
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
|
||||
private openCredentialEditor(ctx: LayerContext): void {
|
||||
if (this.credentialEditorOpen) return;
|
||||
this.credentialEditorOpen = true;
|
||||
if (this.phoneEditor !== "none") return;
|
||||
this.phoneEditor = "credentials";
|
||||
void ctx.actions.startTextSettingsEdit(
|
||||
[evenHubLoginEmailSetting, evenHubLoginPasswordSetting],
|
||||
"Sign in to EvenHub",
|
||||
() => {
|
||||
this.credentialEditorOpen = false;
|
||||
this.phoneEditor = "none";
|
||||
void this.submitCredentials(ctx);
|
||||
},
|
||||
{ setting: evenHubRememberMeSetting, label: "Remember me" },
|
||||
);
|
||||
}
|
||||
|
||||
private openSearchEditor(ctx: LayerContext): void {
|
||||
if (this.phoneEditor !== "none") return;
|
||||
this.phoneEditor = "search";
|
||||
void ctx.actions.startTextSettingsEdit([evenHubSearchQuerySetting], "Search EvenHub", () => {
|
||||
this.phoneEditor = "none";
|
||||
if (this.closed) return;
|
||||
void this.runSearch(ctx, evenHubSearchQuerySetting.get());
|
||||
});
|
||||
}
|
||||
|
||||
/** Close the phone editor if one is open (optionally only a given kind). */
|
||||
private closePhoneEditor(actions: Pick<LayerActions, "endTextSettingEdit">, kind?: "credentials" | "search"): void {
|
||||
if (this.phoneEditor === "none" || (kind && this.phoneEditor !== kind)) return;
|
||||
this.phoneEditor = "none";
|
||||
void actions.endTextSettingEdit();
|
||||
}
|
||||
|
||||
private async submitCredentials(ctx: LayerContext): Promise<void> {
|
||||
const email = evenHubLoginEmailSetting.get();
|
||||
const password = evenHubLoginPasswordSetting.get();
|
||||
@@ -252,8 +701,8 @@ export class EvenHubStoreLayer implements Layer {
|
||||
this.enterLogin(ctx, "Email and password are required.");
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
this.status = "Signing in...";
|
||||
this.loginBusy = true;
|
||||
this.loginStatus = "Signing in...";
|
||||
ctx.actions.requestRender();
|
||||
try {
|
||||
await evenHubApi.signIn(email, password, evenHubRememberMeSetting.get());
|
||||
@@ -263,26 +712,43 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
this.showingLogin = false;
|
||||
clearEvenHubLoginForm();
|
||||
this.loading = false;
|
||||
await this.reload(ctx);
|
||||
this.loginBusy = false;
|
||||
const pending = this.pendingPackage;
|
||||
if (pending) this.showInstalledPackage(ctx.stack, pending);
|
||||
await this.reload(ctx, this.activeTab);
|
||||
} catch (error) {
|
||||
if (this.closed) return;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.status = `Login failed: ${message}`;
|
||||
this.loginStatus = `Login failed: ${message}`;
|
||||
evenHubLoginPasswordSetting.set("");
|
||||
this.openCredentialEditor(ctx);
|
||||
} finally {
|
||||
evenHubLoginPasswordSetting.set("");
|
||||
this.loading = false;
|
||||
this.loginBusy = false;
|
||||
if (!this.closed) ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
/** A registry entry dressed as a store record, for pages opened before the store record arrives. */
|
||||
function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp {
|
||||
return {
|
||||
id: 0,
|
||||
packageId: installed.packageId,
|
||||
name: installed.name,
|
||||
creatorName: "",
|
||||
tagline: "",
|
||||
description: "",
|
||||
categories: [],
|
||||
installCount: 0,
|
||||
likeCount: 0,
|
||||
firstPublishedAt: "",
|
||||
iconPath: "",
|
||||
version: "",
|
||||
changelog: "",
|
||||
fileSize: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function formatCount(value: number): string {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Update detection for installed EvenHub apps.
|
||||
*
|
||||
* Two situations need the user's attention: the store has a newer version
|
||||
* than the one installed, and the registry lists a package whose EHPK is no
|
||||
* longer on disk (a settings export imported after a reinstall carries the
|
||||
* registry but not the files).
|
||||
*/
|
||||
import { statPath } from "../../native/file-access";
|
||||
import { evenHubApi, EvenHubAuthenticationError, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
getInstalledEvenHubApps,
|
||||
installedEvenHubPackagePath,
|
||||
type InstalledEvenHubApp,
|
||||
} from "./installed-apps";
|
||||
|
||||
export type EvenHubAppUpdate = {
|
||||
installed: InstalledEvenHubApp;
|
||||
/** Storefront record, or null when the store no longer lists the package. */
|
||||
latest: EvenHubStoreApp | null;
|
||||
/** The registry entry has no package file behind it. */
|
||||
packageMissing: boolean;
|
||||
/** The store's version is newer than the installed one. */
|
||||
updateAvailable: boolean;
|
||||
/** Why the store lookup failed, when it did. */
|
||||
error?: string;
|
||||
};
|
||||
|
||||
/** Whether an installed package's EHPK is actually present on disk. */
|
||||
export function isInstalledPackagePresent(packageId: string): boolean {
|
||||
const entry = statPath(installedEvenHubPackagePath(packageId));
|
||||
return entry !== null && !entry.isDirectory && entry.sizeBytes > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `candidate` is a newer version than `current`. Dotted numeric
|
||||
* components compare numerically; non-numeric versions (or ones that agree
|
||||
* numerically but differ textually, e.g. "1.0.0-beta" vs "1.0.0") are not
|
||||
* treated as newer, so a stray tag never nags forever.
|
||||
*/
|
||||
export function isNewerVersion(candidate: string, current: string): boolean {
|
||||
const a = versionComponents(candidate);
|
||||
const b = versionComponents(current);
|
||||
if (a.length === 0 || b.length === 0) return false;
|
||||
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
||||
const delta = (a[i] ?? 0) - (b[i] ?? 0);
|
||||
if (delta !== 0) return delta > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function versionComponents(version: string): number[] {
|
||||
const match = version.trim().match(/^v?(\d+(?:\.\d+)*)/i);
|
||||
return match ? match[1]!.split(".").map(Number) : [];
|
||||
}
|
||||
|
||||
/** Describe an installed app's standing against the store record it was fetched with. */
|
||||
export function describeUpdate(installed: InstalledEvenHubApp, latest: EvenHubStoreApp | null): EvenHubAppUpdate {
|
||||
return {
|
||||
installed,
|
||||
latest,
|
||||
packageMissing: !isInstalledPackagePresent(installed.packageId),
|
||||
updateAvailable: latest !== null && isNewerVersion(latest.version, installed.version),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up every installed app in the store and return the ones needing
|
||||
* action (a newer version, or a missing package). Lookups run a few at a
|
||||
* time; `onProgress` reports how many have completed so far.
|
||||
*/
|
||||
export async function checkForEvenHubUpdates(
|
||||
onProgress?: (done: number, total: number) => void,
|
||||
): Promise<EvenHubAppUpdate[]> {
|
||||
const installed = getInstalledEvenHubApps();
|
||||
const results: EvenHubAppUpdate[] = new Array(installed.length);
|
||||
let next = 0;
|
||||
let done = 0;
|
||||
const worker = async () => {
|
||||
while (next < installed.length) {
|
||||
const index = next++;
|
||||
const app = installed[index]!;
|
||||
try {
|
||||
results[index] = describeUpdate(app, await evenHubApi.getStoreAppDetail(app.packageId));
|
||||
} catch (error) {
|
||||
// A rejected session affects every lookup: let the caller re-login.
|
||||
if (error instanceof EvenHubAuthenticationError) throw error;
|
||||
results[index] = { ...describeUpdate(app, null), error: cleanError(error) };
|
||||
}
|
||||
done++;
|
||||
onProgress?.(done, installed.length);
|
||||
}
|
||||
};
|
||||
await Promise.all(Array.from({ length: Math.min(4, installed.length) }, worker));
|
||||
return results.filter((update) => update.updateAvailable || update.packageMissing);
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error).replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { type BdfFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { loadImageFileAsGray } from "../../native/image-files";
|
||||
import { loadImageFileAsGray, PHOTO_TONE } from "../../native/image-files";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
|
||||
@@ -13,8 +13,9 @@ 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
|
||||
* grayscale with the shared photo tone (sRGB gamma, dithered onto the 16
|
||||
* display levels) 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 {
|
||||
@@ -35,7 +36,7 @@ export class ImageViewerLayer implements Layer {
|
||||
const bodyHeight = height - BODY_TOP - BODY_MARGIN;
|
||||
if (!this.loadAttempted) {
|
||||
this.loadAttempted = true;
|
||||
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight);
|
||||
this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight, PHOTO_TONE);
|
||||
}
|
||||
if (!this.image) {
|
||||
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* 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).
|
||||
* selection. Tap-then-hold opens the window menu (undo, new game, restart).
|
||||
* Watch swipes move the cursor spatially: left/right within the row, up/down
|
||||
* between the top row (cells + foundations) and the cascades.
|
||||
* Safe cards auto-play to the foundations after every move.
|
||||
@@ -22,7 +22,7 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures";
|
||||
@@ -100,12 +100,13 @@ type Snapshot = {
|
||||
type FreecellWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: 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. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: "playing" | "won";
|
||||
cascades: number[][];
|
||||
@@ -142,6 +143,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: FreecellWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -212,8 +214,8 @@ function playSfx(window: FreecellWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: FreecellWindow): void {
|
||||
/** The window's context menu (game actions). */
|
||||
function windowMenuItems(window: FreecellWindow): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
if (window.undoStack.length > 0 && window.phase === "playing") {
|
||||
items.push({
|
||||
@@ -250,12 +252,16 @@ function openWindowMenu(window: FreecellWindow): void {
|
||||
},
|
||||
},
|
||||
);
|
||||
windowMenu(window).open([...items, ...defaultWindowMenuItems(window.windowId, post)]);
|
||||
return items;
|
||||
}
|
||||
|
||||
function windowMenu(window: FreecellWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => windowMenuItems(window),
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -326,8 +332,8 @@ function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId:
|
||||
sendToFoundation(window, window.cursor);
|
||||
}
|
||||
break;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
|
||||
@@ -346,8 +352,8 @@ function handleWonInput(window: FreecellWindow, event: InputEvent, frameId: numb
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: freecell ignored input");
|
||||
@@ -629,19 +635,18 @@ function layoutFor(window: FreecellWindow): Layout {
|
||||
}
|
||||
|
||||
function paint(window: FreecellWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
const planes = singlePlane(paintContent(window));
|
||||
if (window.phase === "won") {
|
||||
// The win box goes on its own plane: card labels are deferred glyphs that
|
||||
// render above their own image's raster, so an overlay drawn into the
|
||||
// board image could not cover them.
|
||||
const overlay = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
paintWinOverlay(overlay, window);
|
||||
planes.push({ image: overlay, x: 0, y: 0 });
|
||||
}
|
||||
return planes;
|
||||
return windowMenu(window).paint(() => {
|
||||
const planes = singlePlane(paintContent(window));
|
||||
if (window.phase === "won") {
|
||||
// The win box goes on its own plane: card labels are deferred glyphs
|
||||
// that render above their own image's raster, so an overlay drawn into
|
||||
// the board image could not cover them.
|
||||
const overlay = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
paintWinOverlay(overlay, window);
|
||||
planes.push({ image: overlay, x: 0, y: 0 });
|
||||
}
|
||||
return planes;
|
||||
});
|
||||
}
|
||||
|
||||
function paintContent(window: FreecellWindow): GrayImage {
|
||||
|
||||
@@ -169,7 +169,7 @@ class LauncherGridLayer implements Layer {
|
||||
}
|
||||
|
||||
/**
|
||||
* App-specific entries for the window's long-press menu. Only item mode
|
||||
* App-specific entries for the window's context menu. Only item mode
|
||||
* pins the selection to a single cell, and only apps can move to folders,
|
||||
* so anywhere else the menu falls back to just the defaults.
|
||||
*/
|
||||
|
||||
@@ -16,6 +16,10 @@ const DEFAULT_ASSIGNMENTS: Record<string, string> = {
|
||||
freecell: "Games",
|
||||
minesweeper: "Games",
|
||||
pinball: "Games",
|
||||
calculator: "Prototypes",
|
||||
microphones: "Prototypes",
|
||||
nightscout: "Prototypes",
|
||||
roam: "Prototypes",
|
||||
};
|
||||
|
||||
export const FOLDER_NAME_MAX_LENGTH = 24;
|
||||
|
||||
@@ -15,14 +15,15 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -94,12 +95,13 @@ type GamePhase = "playing" | "paused" | "won" | "lost";
|
||||
type MinesweeperWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: 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. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
difficultyIndex: number;
|
||||
@@ -146,6 +148,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: MinesweeperWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -249,10 +252,10 @@ function playSfx(window: MinesweeperWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: MinesweeperWindow): void {
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for flagging. */
|
||||
function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
const nextDifficulty = DIFFICULTIES[(window.difficultyIndex + 1) % DIFFICULTIES.length]!;
|
||||
windowMenu(window).open([
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
@@ -278,13 +281,17 @@ function openWindowMenu(window: MinesweeperWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: MinesweeperWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -359,6 +366,10 @@ function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameI
|
||||
}
|
||||
toggleFlag(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
if (rowMode || watch) {
|
||||
window.phase = "paused";
|
||||
@@ -391,8 +402,8 @@ function handleIdleInput(window: MinesweeperWindow, event: InputEvent, frameId:
|
||||
frameTimings.finishFrame(frameId, "discarded: minesweeper yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: minesweeper ignored input");
|
||||
@@ -588,10 +599,7 @@ function elapsedMs(window: MinesweeperWindow): number {
|
||||
}
|
||||
|
||||
function paint(window: MinesweeperWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: MinesweeperWindow): GrayImage {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
import { MusicSettingsLayer } from "./music-settings";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
|
||||
@@ -480,6 +481,13 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
iconLetter: "M",
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new MusicSettingsLayer());
|
||||
},
|
||||
}],
|
||||
actions: options.actions,
|
||||
baseLayer: new MusicRootLayer(musicLayer),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { mediaBrowserBridge } from "../../native/media-browser";
|
||||
import { isMediaAppEnabled, readMediaApps, setMediaAppEnabled } from "../../native/media-apps";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
|
||||
/** Discovered apps stay visible even when ignored or no longer publishing a session. */
|
||||
export class MusicSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private signature = "";
|
||||
private selectedPackage = "";
|
||||
|
||||
constructor() {
|
||||
// Refresh installed browser services, remembering ignored services too.
|
||||
mediaBrowserBridge.listBrowsableApps(true);
|
||||
}
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
const apps = readMediaApps().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]);
|
||||
if (!this.menu || signature !== this.signature) {
|
||||
this.signature = signature;
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = app.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
this.menu = new MenuLayer("Music settings", items, {
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = enabled Off = ignored",
|
||||
}).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage)));
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* Saved and recent destinations for the Navigate app: Home and Work (plain
|
||||
* address settings, also editable in the Settings app), custom named
|
||||
* destinations, and a recency list of resolved places. Pure settings-store
|
||||
* accessors, usable from the worker and the main thread alike.
|
||||
*/
|
||||
import {
|
||||
navigateHomeAddressSetting,
|
||||
navigateRecentDestinationsSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateSavedDestinationsSetting,
|
||||
navigateWorkAddressSetting,
|
||||
} from "../../ui/dashboard-settings";
|
||||
|
||||
export const HOME_DESTINATION_ID = "home";
|
||||
export const WORK_DESTINATION_ID = "work";
|
||||
|
||||
/** A named place the user set up; the address is resolved when navigating. */
|
||||
export type SavedDestination = {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
/** Home and Work are fixed slots (renaming/removal disabled); others are custom. */
|
||||
builtin: boolean;
|
||||
};
|
||||
|
||||
/** A place the user actually navigated to, already geocoded. */
|
||||
export type RecentDestination = {
|
||||
name: string;
|
||||
place: string;
|
||||
longitude: number;
|
||||
latitude: number;
|
||||
atMs: number;
|
||||
};
|
||||
|
||||
const MAX_RECENT = 8;
|
||||
const MAX_CUSTOM = 20;
|
||||
|
||||
/** Every saved destination in display order: Home, Work (when set), then custom ones. */
|
||||
export function loadSavedDestinations(): SavedDestination[] {
|
||||
const out: SavedDestination[] = [];
|
||||
const home = navigateHomeAddressSetting.get();
|
||||
if (home) out.push({ id: HOME_DESTINATION_ID, name: "Home", address: home, builtin: true });
|
||||
const work = navigateWorkAddressSetting.get();
|
||||
if (work) out.push({ id: WORK_DESTINATION_ID, name: "Work", address: work, builtin: true });
|
||||
out.push(...loadCustomDestinations());
|
||||
return out;
|
||||
}
|
||||
|
||||
export function loadCustomDestinations(): SavedDestination[] {
|
||||
const parsed = parseJsonArray(navigateSavedDestinationsSetting.get());
|
||||
const out: SavedDestination[] = [];
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const record = entry as Record<string, unknown>;
|
||||
const id = typeof record.id === "string" ? record.id : "";
|
||||
const name = typeof record.name === "string" ? record.name.trim() : "";
|
||||
const address = typeof record.address === "string" ? record.address.trim() : "";
|
||||
if (!id || !name) continue;
|
||||
out.push({ id, name, address, builtin: false });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function saveCustomDestinations(destinations: SavedDestination[]): void {
|
||||
navigateSavedDestinationsSetting.set(
|
||||
JSON.stringify(destinations.slice(0, MAX_CUSTOM).map(({ id, name, address }) => ({ id, name, address }))),
|
||||
);
|
||||
}
|
||||
|
||||
/** Add a custom destination; returns its id. */
|
||||
export function addCustomDestination(name: string, address: string): string {
|
||||
const id = `d${Date.now().toString(36)}`;
|
||||
saveCustomDestinations([...loadCustomDestinations(), { id, name, address, builtin: false }]);
|
||||
return id;
|
||||
}
|
||||
|
||||
export function updateCustomDestination(id: string, patch: { name?: string; address?: string }): void {
|
||||
saveCustomDestinations(
|
||||
loadCustomDestinations().map((destination) =>
|
||||
destination.id === id
|
||||
? { ...destination, name: patch.name ?? destination.name, address: patch.address ?? destination.address }
|
||||
: destination,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function removeCustomDestination(id: string): void {
|
||||
saveCustomDestinations(loadCustomDestinations().filter((destination) => destination.id !== id));
|
||||
}
|
||||
|
||||
/** Set a destination's address; Home/Work go to their own settings. */
|
||||
export function setDestinationAddress(id: string, address: string): void {
|
||||
if (id === HOME_DESTINATION_ID) navigateHomeAddressSetting.set(address);
|
||||
else if (id === WORK_DESTINATION_ID) navigateWorkAddressSetting.set(address);
|
||||
else updateCustomDestination(id, { address });
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a saved destination by name ("home", "work", or a custom name),
|
||||
* case-insensitively, for voice/assistant queries such as "navigate home".
|
||||
*/
|
||||
export function findSavedDestinationByName(query: string): SavedDestination | null {
|
||||
const wanted = query.trim().toLowerCase();
|
||||
if (!wanted) return null;
|
||||
return loadSavedDestinations().find((destination) => destination.name.toLowerCase() === wanted) ?? null;
|
||||
}
|
||||
|
||||
export function loadRecentDestinations(): RecentDestination[] {
|
||||
if (!navigateRememberRecentSetting.get()) return [];
|
||||
const parsed = parseJsonArray(navigateRecentDestinationsSetting.get());
|
||||
const out: RecentDestination[] = [];
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const record = entry as Record<string, unknown>;
|
||||
const name = typeof record.name === "string" ? record.name : "";
|
||||
const longitude = Number(record.longitude);
|
||||
const latitude = Number(record.latitude);
|
||||
if (!name || !Number.isFinite(longitude) || !Number.isFinite(latitude)) continue;
|
||||
out.push({
|
||||
name,
|
||||
place: typeof record.place === "string" ? record.place : "",
|
||||
longitude,
|
||||
latitude,
|
||||
atMs: Number(record.atMs) || 0,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Record a navigated-to place at the head of the recent list (no-op when the setting is off). */
|
||||
export function rememberRecentDestination(destination: Omit<RecentDestination, "atMs">): void {
|
||||
if (!navigateRememberRecentSetting.get()) return;
|
||||
const others = loadRecentDestinations().filter((recent) => !isSamePlace(recent, destination));
|
||||
const next = [{ ...destination, atMs: Date.now() }, ...others].slice(0, MAX_RECENT);
|
||||
navigateRecentDestinationsSetting.set(JSON.stringify(next));
|
||||
}
|
||||
|
||||
export function removeRecentDestination(destination: RecentDestination): void {
|
||||
const remaining = loadRecentDestinations().filter((recent) => !isSamePlace(recent, destination));
|
||||
navigateRecentDestinationsSetting.set(JSON.stringify(remaining));
|
||||
}
|
||||
|
||||
export function clearRecentDestinations(): void {
|
||||
navigateRecentDestinationsSetting.set("[]");
|
||||
}
|
||||
|
||||
function isSamePlace(
|
||||
a: { name: string; place: string; longitude: number; latitude: number },
|
||||
b: { name: string; place: string; longitude: number; latitude: number },
|
||||
): boolean {
|
||||
// Within ~10 m, or the same name at the same formatted place: a repeat
|
||||
// visit, not a new entry. (Two branches of one chain stay distinct.)
|
||||
return (
|
||||
(a.name === b.name && a.place === b.place) ||
|
||||
(Math.abs(a.longitude - b.longitude) < 0.0001 && Math.abs(a.latitude - b.latitude) < 0.0001)
|
||||
);
|
||||
}
|
||||
|
||||
function parseJsonArray(text: string): unknown[] {
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -8,16 +8,60 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import {
|
||||
drawListScrollbar,
|
||||
drawRightValueMenuItem,
|
||||
drawSelectionHighlight,
|
||||
drawSubmenuIndicator,
|
||||
drawToggleMenuItem,
|
||||
scrollToKeepSelectionVisible,
|
||||
type MenuItem,
|
||||
} from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
||||
import type { LayerContext } from "../../ui/layers";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import {
|
||||
navigateDestinationAddressDraftSetting,
|
||||
navigateDestinationNameDraftSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
enumSettingMenuItem,
|
||||
type ConfigSettingString,
|
||||
} from "../../ui/dashboard-settings";
|
||||
import {
|
||||
addCustomDestination,
|
||||
clearRecentDestinations,
|
||||
findSavedDestinationByName,
|
||||
HOME_DESTINATION_ID,
|
||||
loadRecentDestinations,
|
||||
loadSavedDestinations,
|
||||
removeCustomDestination,
|
||||
rememberRecentDestination,
|
||||
removeRecentDestination,
|
||||
setDestinationAddress,
|
||||
updateCustomDestination,
|
||||
WORK_DESTINATION_ID,
|
||||
type RecentDestination,
|
||||
type SavedDestination,
|
||||
} from "./destinations";
|
||||
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, type InputEvent } from "../../ui/gestures";
|
||||
import {
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_SCROLL,
|
||||
GESTURE_SHORT_THEN_LONG_PRESS,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
||||
import {
|
||||
fetchRoute,
|
||||
@@ -29,6 +73,9 @@ import {
|
||||
type RouteProfile,
|
||||
type StaticMapCamera,
|
||||
} from "../../native/mapbox";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "../../native/compass";
|
||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
||||
import { calibrateHeading, normalizeHeading } from "../compass/calibration";
|
||||
import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower";
|
||||
import { drawManeuverGlyph } from "./maneuver-icons";
|
||||
|
||||
@@ -46,8 +93,19 @@ 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;
|
||||
/**
|
||||
* A fix older than this is not where the user is now. The tracker seeds each
|
||||
* start with the platform's cached fix, and after a previous trip that can be
|
||||
* the old destination: routing from it, or feeding it to the follower, ends a
|
||||
* fresh navigation with "Arrived" before a live fix ever comes in.
|
||||
*/
|
||||
const FIX_MAX_AGE_MS = 60_000;
|
||||
/** Above this speed a GPS bearing is trustworthy for heading-up. */
|
||||
const BEARING_MIN_SPEED_MPS = 2.5;
|
||||
/** The head-direction chevron only redraws once the wearer has turned this far. */
|
||||
const HEADING_STEP_DEG = 4;
|
||||
/** Our claim on the shared glasses magnetometer (see setCompassEnabled). */
|
||||
const COMPASS_OWNER = "navigate";
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
@@ -59,6 +117,7 @@ type MapMode = "follow" | "overview";
|
||||
type NavWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
@@ -71,7 +130,7 @@ 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.",
|
||||
"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. The name of one of the user's saved destinations (e.g. 'home', 'work') is used directly. 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: {
|
||||
@@ -123,6 +182,35 @@ let rerouteInFlight = false;
|
||||
/** Bumped on every new route/mode so stale map fetches can be discarded. */
|
||||
let routeGeneration = 0;
|
||||
|
||||
/**
|
||||
* Where to go: free text to geocode (optionally shown under a saved
|
||||
* destination's label), or an already-resolved place (a recent destination).
|
||||
*/
|
||||
type NavTarget =
|
||||
| { kind: "query"; query: string; label?: string }
|
||||
| { kind: "place"; name: string; place: string; longitude: number; latitude: number };
|
||||
|
||||
/** An entry on the idle page's destination list. */
|
||||
type IdleEntry =
|
||||
| { kind: "saved"; destination: SavedDestination }
|
||||
| { kind: "recent"; destination: RecentDestination };
|
||||
|
||||
/** Idle-page list selection (index into idleEntries()). */
|
||||
let idleSelection = 0;
|
||||
let idleScrollRow = 0;
|
||||
|
||||
/**
|
||||
* In-progress destination edit: the phone text editor is open on `setting`
|
||||
* (a staging draft); click confirms with the typed value, double-click
|
||||
* cancels. `onDone` may start another edit (name, then address).
|
||||
*/
|
||||
type DestinationEdit = {
|
||||
setting: ConfigSettingString;
|
||||
title: string;
|
||||
onDone: (value: string) => void;
|
||||
};
|
||||
let editing: DestinationEdit | null = null;
|
||||
|
||||
let mapMode: MapMode = "follow";
|
||||
let zoomOffset = 0;
|
||||
let mapImage: GrayImage | null = null;
|
||||
@@ -132,6 +220,17 @@ let mapLastFetchAtMs = 0;
|
||||
let mapLastFetchCenter: [number, number] | null = null;
|
||||
let mapNextRetryAtMs = 0;
|
||||
let mapLastError = "";
|
||||
/** Bearing the current map image was rendered with (its "up"), for follow mode. */
|
||||
let mapFetchedBearingDeg = 0;
|
||||
|
||||
// Glasses compass: the same firmware feed and wearer calibration as the
|
||||
// Compass app, so the position chevron shows where the wearer is looking
|
||||
// relative to the map rather than always pointing up the direction of travel.
|
||||
let compassActive = false;
|
||||
let unsubscribeCompass: (() => void) | null = null;
|
||||
/** Wearer's true heading (degrees clockwise from north) as last drawn, or null before compass data arrives. */
|
||||
let headHeadingDeg: number | null = null;
|
||||
let declinationCache: { latitude: number; longitude: number; degrees: number | null } | null = null;
|
||||
|
||||
let tickTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let firstFixWaiters: Array<(fix: TrackedLocation) => void> = [];
|
||||
@@ -149,6 +248,12 @@ function post(message: WorkerAppReply): void {
|
||||
global.postMessage(message);
|
||||
}
|
||||
|
||||
// Destinations edited on the phone (the text editor, or the Settings app's
|
||||
// Home/Work rows) repaint the idle list / the edit screen live.
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key.startsWith("navigate.")) render();
|
||||
});
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||
// evaluation is synchronous, so the handler below is installed before any
|
||||
@@ -162,6 +267,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
window = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -171,9 +277,24 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
};
|
||||
post({ type: "set-tools", windowId: message.windowId, tools: NAVIGATE_TOOLS });
|
||||
break;
|
||||
case "resize-window":
|
||||
if (!window || window.windowId !== message.windowId) break;
|
||||
if (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize(message.viewport);
|
||||
window.lastSubmittedFingerprint = "";
|
||||
render();
|
||||
break;
|
||||
case "close-window":
|
||||
if (editing) {
|
||||
// Window closed mid-edit: shut the phone editor down.
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
}
|
||||
stopNavigation("");
|
||||
window = null;
|
||||
reconcileCompass();
|
||||
break;
|
||||
case "input":
|
||||
if (!window || window.windowId !== message.windowId) {
|
||||
@@ -187,8 +308,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
case "text-input":
|
||||
// Voice input / typed text sets a destination directly.
|
||||
if (message.text.trim()) {
|
||||
if (editing) {
|
||||
// Voice input as an alternative to the phone keyboard.
|
||||
editing.setting.set(message.text.trim());
|
||||
render();
|
||||
} else if (message.text.trim()) {
|
||||
// Voice input / typed text sets a destination directly.
|
||||
void startNavigation(message.text.trim(), profile).catch(() => {});
|
||||
}
|
||||
break;
|
||||
@@ -201,6 +326,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
window.focused = message.focused;
|
||||
reconcileCompass();
|
||||
if (window.foreground) {
|
||||
maybeRefreshMap();
|
||||
render();
|
||||
@@ -208,6 +334,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
break;
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
reconcileCompass();
|
||||
if (screenOn && window?.foreground) {
|
||||
maybeRefreshMap();
|
||||
render();
|
||||
@@ -232,7 +359,20 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Navigation state machine
|
||||
|
||||
async function startNavigation(query: string, requestedProfile: RouteProfile): Promise<string> {
|
||||
/**
|
||||
* Navigate to free text: a saved destination's name ("home") routes to its
|
||||
* address under that label; anything else is geocoded as typed.
|
||||
*/
|
||||
function startNavigation(query: string, requestedProfile: RouteProfile): Promise<string> {
|
||||
const saved = findSavedDestinationByName(query);
|
||||
return startNavigationTo(
|
||||
saved ? { kind: "query", query: saved.address, label: saved.name } : { kind: "query", query },
|
||||
requestedProfile,
|
||||
);
|
||||
}
|
||||
|
||||
async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise<string> {
|
||||
const query = target.kind === "query" ? target.query : target.name;
|
||||
if (!isMapboxConfigured()) {
|
||||
statusMessage = "Set a Mapbox token in Settings > API Keys.";
|
||||
phase = "idle";
|
||||
@@ -255,21 +395,44 @@ async function startNavigation(query: string, requestedProfile: RouteProfile): P
|
||||
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}".`);
|
||||
let picked: GeocodeCandidate;
|
||||
let candidates: GeocodeCandidate[] = [];
|
||||
if (target.kind === "place") {
|
||||
picked = {
|
||||
name: target.name,
|
||||
placeFormatted: target.place,
|
||||
longitude: target.longitude,
|
||||
latitude: target.latitude,
|
||||
distanceMeters: null,
|
||||
};
|
||||
} else {
|
||||
statusMessage = `Finding ${query}...`;
|
||||
render();
|
||||
candidates = await geocodeForward(query, fix, 5);
|
||||
if (!candidates.length) {
|
||||
throw new Error(`No places found matching "${query}".`);
|
||||
}
|
||||
picked = candidates[0]!;
|
||||
}
|
||||
const picked = candidates[0]!;
|
||||
destination = { longitude: picked.longitude, latitude: picked.latitude };
|
||||
destinationName = picked.name;
|
||||
destinationName = target.kind === "query" && target.label ? target.label : picked.name;
|
||||
destinationPlace = picked.placeFormatted;
|
||||
|
||||
statusMessage = `Routing to ${picked.name}...`;
|
||||
statusMessage = `Routing to ${destinationName}...`;
|
||||
render();
|
||||
const route = await fetchRoute(fix, destination, profile);
|
||||
adoptRoute(route);
|
||||
// Saved destinations already have a row of their own on the idle page;
|
||||
// everything else (voice, assistant, a re-picked recent) goes on the
|
||||
// recent list, most recent first.
|
||||
if (!(target.kind === "query" && target.label)) {
|
||||
rememberRecentDestination({
|
||||
name: picked.name,
|
||||
place: picked.placeFormatted,
|
||||
longitude: picked.longitude,
|
||||
latitude: picked.latitude,
|
||||
});
|
||||
}
|
||||
if (window) post({ type: "focus-window", windowId: window.windowId });
|
||||
return startSummary(picked, candidates, route);
|
||||
} catch (error) {
|
||||
@@ -323,8 +486,16 @@ function stopNavigation(finalStatus: string): void {
|
||||
ensureTickTimer();
|
||||
}
|
||||
|
||||
function isFreshFix(fix: TrackedLocation): boolean {
|
||||
return Date.now() - fix.timestampMs < FIX_MAX_AGE_MS;
|
||||
}
|
||||
|
||||
function handleFix(fix: TrackedLocation): void {
|
||||
lastFix = fix;
|
||||
if (!lastFix || fix.timestampMs >= lastFix.timestampMs) lastFix = fix;
|
||||
// A stale fix (a cached seed from a previous outing) may position the map
|
||||
// until something better arrives, but it must not stand in for the user's
|
||||
// current location: it neither starts a route nor advances guidance.
|
||||
if (!isFreshFix(fix)) return;
|
||||
if (fix.bearingDeg !== null && (fix.speedMps ?? 0) >= BEARING_MIN_SPEED_MPS) {
|
||||
lastBearingDeg = fix.bearingDeg;
|
||||
}
|
||||
@@ -378,7 +549,7 @@ function stopTrackingIfIdle(): void {
|
||||
}
|
||||
|
||||
function waitForFix(): Promise<TrackedLocation> {
|
||||
if (lastFix && Date.now() - lastFix.timestampMs < 60_000) {
|
||||
if (lastFix && isFreshFix(lastFix)) {
|
||||
return Promise.resolve(lastFix);
|
||||
}
|
||||
return new Promise<TrackedLocation>((resolve, reject) => {
|
||||
@@ -394,6 +565,59 @@ function waitForFix(): Promise<TrackedLocation> {
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Head heading (glasses compass)
|
||||
|
||||
/** Hold the magnetometer only while its reading is actually on screen. */
|
||||
function reconcileCompass(): void {
|
||||
const wanted =
|
||||
window !== null && window.foreground && screenOn && phase === "navigating" && mapMode === "follow";
|
||||
if (wanted === compassActive) return;
|
||||
compassActive = wanted;
|
||||
if (wanted) {
|
||||
unsubscribeCompass = addCompassListener(handleCompassEvent);
|
||||
setCompassEnabled(true, COMPASS_OWNER);
|
||||
} else {
|
||||
setCompassEnabled(false, COMPASS_OWNER);
|
||||
unsubscribeCompass?.();
|
||||
unsubscribeCompass = null;
|
||||
headHeadingDeg = null;
|
||||
}
|
||||
}
|
||||
|
||||
function handleCompassEvent(event: CompassEvent): void {
|
||||
if (!compassActive || event.command !== COMPASS_CHANGED || event.headingDegrees < 0) return;
|
||||
// Wearer-fit offset from the Compass app's calibration, then declination
|
||||
// from our own GPS fix: the map is always true-north referenced.
|
||||
const magnetic = calibrateHeading(event.headingDegrees);
|
||||
const heading = normalizeHeading(magnetic + (currentDeclination() ?? 0));
|
||||
if (headHeadingDeg !== null && angularDistance(heading, headHeadingDeg) < HEADING_STEP_DEG) return;
|
||||
headHeadingDeg = heading;
|
||||
render();
|
||||
}
|
||||
|
||||
/** Declination at the last fix; it only varies over tens of kilometres, so cache per coarse position. */
|
||||
function currentDeclination(): number | null {
|
||||
if (!lastFix) return null;
|
||||
if (
|
||||
!declinationCache ||
|
||||
Math.abs(declinationCache.latitude - lastFix.latitude) > 0.5 ||
|
||||
Math.abs(declinationCache.longitude - lastFix.longitude) > 0.5
|
||||
) {
|
||||
declinationCache = {
|
||||
latitude: lastFix.latitude,
|
||||
longitude: lastFix.longitude,
|
||||
degrees: magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude),
|
||||
};
|
||||
}
|
||||
return declinationCache.degrees;
|
||||
}
|
||||
|
||||
function angularDistance(a: number, b: number): number {
|
||||
const delta = Math.abs(normalizeHeading(a) - normalizeHeading(b));
|
||||
return Math.min(delta, 360 - delta);
|
||||
}
|
||||
|
||||
function ensureTickTimer(): void {
|
||||
const shouldRun = phase === "navigating";
|
||||
if (shouldRun && tickTimer === null) {
|
||||
@@ -512,9 +736,10 @@ function maybeRefreshMap(): void {
|
||||
.then((image) => {
|
||||
mapInFlight = false;
|
||||
if (generation !== routeGeneration) return; // Route/mode changed mid-fetch.
|
||||
boostContrast(image);
|
||||
levelMap(image);
|
||||
mapImage = image;
|
||||
mapFetchedKey = view.key;
|
||||
mapFetchedBearingDeg = view.camera.kind === "center" ? view.camera.bearingDeg : 0;
|
||||
mapLastFetchAtMs = Date.now();
|
||||
mapLastFetchCenter = view.camera.kind === "center" ? [view.camera.longitude, view.camera.latitude] : null;
|
||||
mapLastError = "";
|
||||
@@ -539,11 +764,25 @@ function simplifyPath(path: Array<[number, number]>, maxPoints: number): Array<[
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Stretch the dark-style render so roads survive 4bpp quantization. */
|
||||
function boostContrast(image: GrayImage): void {
|
||||
/**
|
||||
* Level the render for the lens. The dominant tone is the land fill: black
|
||||
* when the style override took, dark gray otherwise. Either way it becomes
|
||||
* the black point, and everything above it is stretched so roads survive
|
||||
* 4bpp quantization.
|
||||
*/
|
||||
function levelMap(image: GrayImage): void {
|
||||
const histogram = new Uint32Array(256);
|
||||
for (let i = 0; i < image.pixels.length; i++) histogram[image.pixels[i]!]++;
|
||||
let black = 0;
|
||||
for (let value = 1; value < 256; value++) {
|
||||
if (histogram[value]! > histogram[black]!) black = value;
|
||||
}
|
||||
// A bright dominant tone isn't a floor (a large plaza, a fetch that came
|
||||
// back mostly label); flattening it would eat the roads.
|
||||
if (black > 64) black = 0;
|
||||
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)));
|
||||
image.pixels[i] = value <= black ? 0 : Math.min(255, Math.round((value - black) * 1.7));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -609,9 +848,272 @@ function describeRouteStatus(): string {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input
|
||||
|
||||
/**
|
||||
* The window's context menu: Stop navigation while a route is up, then the
|
||||
* display and saved-destination settings (Home, Work, custom named places), and the
|
||||
* recent-destinations preference.
|
||||
*/
|
||||
function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
if (phase === "navigating" || phase === "arrived") {
|
||||
items.push({
|
||||
label: "Stop navigation",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
stopNavigation("Navigation stopped.");
|
||||
render();
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push(
|
||||
enumSettingMenuItem(navigateDisplayModeSetting),
|
||||
enumSettingMenuItem(navigateVerticalPositionSetting),
|
||||
);
|
||||
items.push({
|
||||
label: "Saved destinations",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new WindowMenuLayer("Saved destinations", savedDestinationMenuItems()));
|
||||
},
|
||||
render: ({ image, x, y, width, height, text }) => {
|
||||
image.drawText(smallFont, x, y + LIST_ROW_TEXT_INSET, text, 200);
|
||||
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
|
||||
},
|
||||
});
|
||||
items.push({
|
||||
label: navigateRememberRecentSetting.label,
|
||||
onSelect: () => {
|
||||
const enabled = navigateRememberRecentSetting.toggle();
|
||||
if (!enabled) clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
},
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
drawToggleMenuItem(
|
||||
image,
|
||||
smallFont,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
navigateRememberRecentSetting.label,
|
||||
navigateRememberRecentSetting.get(),
|
||||
selected,
|
||||
);
|
||||
},
|
||||
});
|
||||
const selectedEntry = phase === "idle" ? idleEntries()[idleSelection] : undefined;
|
||||
if (selectedEntry?.kind === "recent") {
|
||||
const recent = selectedEntry.destination;
|
||||
items.push({
|
||||
label: `Forget ${truncateText(smallFont, recent.name, 160)}`,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
removeRecentDestination(recent);
|
||||
clampIdleSelection();
|
||||
},
|
||||
});
|
||||
}
|
||||
if (loadRecentDestinations().length) {
|
||||
items.push({
|
||||
label: "Clear recent destinations",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
},
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** Home, Work, each custom destination (its own action submenu), and Add. */
|
||||
function savedDestinationMenuItems(): MenuItem[] {
|
||||
const items: MenuItem[] = [];
|
||||
const saved = loadSavedDestinations();
|
||||
const home = saved.find((d) => d.id === HOME_DESTINATION_ID);
|
||||
const work = saved.find((d) => d.id === WORK_DESTINATION_ID);
|
||||
items.push(addressMenuItem("Home", HOME_DESTINATION_ID, home?.address ?? ""));
|
||||
items.push(addressMenuItem("Work", WORK_DESTINATION_ID, work?.address ?? ""));
|
||||
for (const destination of saved) {
|
||||
if (destination.builtin) continue;
|
||||
items.push({
|
||||
label: destination.name,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new WindowMenuLayer(destination.name, customDestinationMenuItems(destination)));
|
||||
},
|
||||
render: ({ image, x, y, width, height }) => {
|
||||
drawAddressRow(image, x, y, width, destination.name, destination.address, true);
|
||||
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: "Add destination...",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddDestination());
|
||||
},
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
/** A Home/Work row: shows the address, opens the address editor. */
|
||||
function addressMenuItem(label: string, id: string, address: string): MenuItem {
|
||||
return {
|
||||
label,
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddressEdit(id, label, address));
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawAddressRow(image, x, y, width, label, address, false);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "Navigate there",
|
||||
disabled: !destination.address,
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
void startNavigationTo({ kind: "query", query: destination.address, label: destination.name }, profile).catch(
|
||||
() => {},
|
||||
);
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Set address",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => beginAddressEdit(destination.id, destination.name, destination.address));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Rename",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
navigateDestinationNameDraftSetting.set(destination.name);
|
||||
beginEdit(navigateDestinationNameDraftSetting, `Rename ${destination.name}`, (name) => {
|
||||
if (name) updateCustomDestination(destination.id, { name });
|
||||
});
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Remove",
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
removeCustomDestination(destination.id);
|
||||
clampIdleSelection();
|
||||
});
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function drawAddressRow(
|
||||
image: GrayImage,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
label: string,
|
||||
address: string,
|
||||
hasSubmenu: boolean,
|
||||
): void {
|
||||
const valueWidth = Math.max(40, width - smallFont.measureText(label) - (hasSubmenu ? 28 : 12));
|
||||
const value = truncateText(smallFont, address || "(not set)", valueWidth);
|
||||
drawRightValueMenuItem(image, smallFont, x, y, width - (hasSubmenu ? 16 : 0), label, value);
|
||||
}
|
||||
|
||||
/** Close the whole menu stack, then run an action that takes over the window. */
|
||||
function closeMenusAnd(ctx: LayerContext, action: () => void): void {
|
||||
ctx.stack.clearToBase();
|
||||
action();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Destination editing (phone text editor + voice input)
|
||||
|
||||
/**
|
||||
* Open the phone text editor on a staging setting. The editor writes the
|
||||
* draft live (repainted via the settings listener); click here confirms.
|
||||
*/
|
||||
function beginEdit(setting: ConfigSettingString, title: string, onDone: (value: string) => void): void {
|
||||
editing = { setting, title, onDone };
|
||||
post({ type: "start-text-setting-edit", settingId: setting.id });
|
||||
render();
|
||||
}
|
||||
|
||||
function finishEdit(confirmed: boolean): void {
|
||||
const current = editing;
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
if (current && confirmed) current.onDone(current.setting.get());
|
||||
// onDone may have started the next step; only fully leave when it didn't.
|
||||
if (!editing) {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
navigateDestinationAddressDraftSetting.set("");
|
||||
clampIdleSelection();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
function beginAddressEdit(id: string, name: string, current: string): void {
|
||||
navigateDestinationAddressDraftSetting.set(current);
|
||||
beginEdit(navigateDestinationAddressDraftSetting, `${name} address`, (address) => {
|
||||
if (address) setDestinationAddress(id, address);
|
||||
});
|
||||
}
|
||||
|
||||
/** Two-step add: a name, then its address. An empty answer cancels. */
|
||||
function beginAddDestination(): void {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
beginEdit(navigateDestinationNameDraftSetting, "New destination name", (name) => {
|
||||
if (!name) return;
|
||||
navigateDestinationAddressDraftSetting.set("");
|
||||
beginEdit(navigateDestinationAddressDraftSetting, `${name} address`, (address) => {
|
||||
if (address) addCustomDestination(name, address);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Idle page destination list
|
||||
|
||||
function idleEntries(): IdleEntry[] {
|
||||
const entries: IdleEntry[] = loadSavedDestinations()
|
||||
.filter((destination) => destination.address)
|
||||
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
||||
for (const destination of loadRecentDestinations()) {
|
||||
entries.push({ kind: "recent", destination });
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
function clampIdleSelection(): void {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, count - 1));
|
||||
}
|
||||
|
||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||
const target: NavTarget =
|
||||
entry.kind === "saved"
|
||||
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
||||
: {
|
||||
kind: "place",
|
||||
name: entry.destination.name,
|
||||
place: entry.destination.place,
|
||||
longitude: entry.destination.longitude,
|
||||
latitude: entry.destination.latitude,
|
||||
};
|
||||
void startNavigationTo(target, profile).catch(() => {});
|
||||
}
|
||||
|
||||
function windowMenu(win: NavWindow): WindowMenu {
|
||||
if (!win.menu) {
|
||||
win.menu = new WindowMenu({
|
||||
windowId: win.windowId,
|
||||
post,
|
||||
title: () => win.title,
|
||||
items: () => windowMenuItems(win),
|
||||
size: { width: win.viewportWidth, height: win.viewportHeight },
|
||||
paintBase: () => paintContent(win),
|
||||
isFocused: () => win.focused,
|
||||
@@ -628,19 +1130,20 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
.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();
|
||||
},
|
||||
});
|
||||
if (editing) {
|
||||
if (event.type === "click") {
|
||||
finishEdit(true);
|
||||
} else if (event.type === "double-click") {
|
||||
finishEdit(false);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate edit ignored input");
|
||||
return;
|
||||
}
|
||||
windowMenu(win).open(items);
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(win).open();
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -656,6 +1159,13 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
maybeRefreshMap();
|
||||
} else if (phase === "arrived") {
|
||||
stopNavigation("");
|
||||
} else if (phase === "idle") {
|
||||
const entry = idleEntries()[idleSelection];
|
||||
if (!entry) {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
|
||||
return;
|
||||
}
|
||||
navigateToIdleEntry(entry);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
|
||||
return;
|
||||
@@ -668,6 +1178,10 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
zoomOffset = Math.max(-3, Math.min(2, zoomOffset + (event.type === "scroll-up" ? 0.5 : -0.5)));
|
||||
maybeRefreshMap();
|
||||
renderAndSubmit(win, frameId);
|
||||
} else if (phase === "idle" && idleEntries().length > 1) {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(count - 1, idleSelection + (event.type === "scroll-down" ? 1 : -1)));
|
||||
renderAndSubmit(win, frameId);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll");
|
||||
}
|
||||
@@ -680,45 +1194,133 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
// Painting
|
||||
|
||||
function paint(win: NavWindow): Plane[] {
|
||||
if (win.menu?.isOpen()) return win.menu.paint();
|
||||
return singlePlane(paintContent(win));
|
||||
return windowMenu(win).paint();
|
||||
}
|
||||
|
||||
function paintContent(win: NavWindow): GrayImage {
|
||||
const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0);
|
||||
if (phase === "idle" || phase === "acquiring" || phase === "routing") {
|
||||
paintIdle(image);
|
||||
if (editing) {
|
||||
paintEdit(image, editing);
|
||||
} else if (phase === "idle" || phase === "acquiring" || phase === "routing") {
|
||||
paintIdle(image, win);
|
||||
} else {
|
||||
paintNavigating(image, win);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
function paintIdle(image: GrayImage): void {
|
||||
/** The "type on the phone" screen for a destination name/address edit. */
|
||||
function paintEdit(image: GrayImage, edit: DestinationEdit): void {
|
||||
const step = smallFont.lineHeight + 2;
|
||||
image.drawText(mediumFont, 24, 16, edit.title, 245);
|
||||
const messageY = 64;
|
||||
const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", image.width - 48);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(smallFont, 24, messageY + index * step, lines[index]!, 190);
|
||||
}
|
||||
const value = edit.setting.get();
|
||||
image.drawText(
|
||||
smallFont,
|
||||
24,
|
||||
messageY + (lines.length + 1) * step,
|
||||
truncateText(smallFont, value || "(empty)", image.width - 48),
|
||||
value ? 225 : 130,
|
||||
);
|
||||
drawFooter(image, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} cancel`);
|
||||
}
|
||||
|
||||
function paintIdle(image: GrayImage, win: NavWindow): 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 });
|
||||
const entries = busy ? [] : idleEntries();
|
||||
const hint = !isMapboxConfigured()
|
||||
? "Set a Mapbox token in Settings > API Keys to enable navigation."
|
||||
: entries.length
|
||||
? "Pick a destination below, or say one via Voice input (system menu, long-press)."
|
||||
: "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu.";
|
||||
const hintLines = wrapText(smallFont, hint, image.width - 48);
|
||||
const hintStep = smallFont.lineHeight + 2;
|
||||
for (let index = 0; index < hintLines.length; index++) {
|
||||
image.drawText(smallFont, 24, 52 + index * hintStep, hintLines[index]!, 170);
|
||||
}
|
||||
let y = 52 + hintLines.length * hintStep + 6;
|
||||
if (statusMessage) {
|
||||
image.drawTextWrapped({
|
||||
font: smallFont,
|
||||
x: 24,
|
||||
y: 130,
|
||||
width: image.width - 48,
|
||||
text: statusMessage,
|
||||
value: busy ? 220 : 190,
|
||||
});
|
||||
const statusLines = wrapText(smallFont, statusMessage, image.width - 48);
|
||||
for (let index = 0; index < statusLines.length; index++) {
|
||||
image.drawText(smallFont, 24, y + index * hintStep, statusLines[index]!, busy ? 220 : 190);
|
||||
}
|
||||
y += statusLines.length * hintStep + 6;
|
||||
}
|
||||
if (entries.length) {
|
||||
paintIdleList(image, win, entries, y);
|
||||
drawFooter(
|
||||
image,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} go ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||
);
|
||||
} else {
|
||||
drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Saved destinations (name + address) then recent places, one selectable row each. */
|
||||
function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], top: number): void {
|
||||
const rowHeight = listRowHeight(smallFont);
|
||||
const listBottom = image.height - 30;
|
||||
const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight));
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, entries.length - 1));
|
||||
idleScrollRow = scrollToKeepSelectionVisible(idleScrollRow, idleSelection, visibleRows, entries.length);
|
||||
const rowX = 20;
|
||||
const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0);
|
||||
const labelWidth = Math.floor(rowWidth * 0.4);
|
||||
for (let row = 0; row < visibleRows; row++) {
|
||||
const index = idleScrollRow + row;
|
||||
const entry = entries[index];
|
||||
if (!entry) break;
|
||||
const y = top + row * rowHeight;
|
||||
const selected = index === idleSelection;
|
||||
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
||||
const textX = rowX + 6;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (entry.kind === "saved") {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.destination.address, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
const detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)";
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (entries.length > visibleRows) {
|
||||
drawListScrollbar(image, rowX + rowWidth + 4, top, visibleRows * rowHeight - 2, idleScrollRow, visibleRows, entries.length);
|
||||
}
|
||||
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);
|
||||
if (mapMode === "follow") {
|
||||
// Head heading relative to the map's up (its render bearing); no
|
||||
// compass reading yet means "up", i.e. the direction of travel.
|
||||
const angle = headHeadingDeg === null ? 0 : headHeadingDeg - mapFetchedBearingDeg;
|
||||
drawChevron(image, MAP_SIZE / 2, MAP_SIZE / 2, angle);
|
||||
}
|
||||
} else {
|
||||
image.drawRect(0, 0, MAP_SIZE, MAP_SIZE, 60);
|
||||
const text = mapLastError ? "Map unavailable" : "Loading map...";
|
||||
@@ -778,11 +1380,21 @@ function paintNavigating(image: GrayImage, win: NavWindow): void {
|
||||
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 {
|
||||
/**
|
||||
* Position chevron, fixed at the map center and rotated `angleDeg` clockwise
|
||||
* from straight up (the map's render bearing) to the wearer's head heading.
|
||||
*/
|
||||
function drawChevron(image: GrayImage, cx: number, cy: number, angleDeg: number): void {
|
||||
const radians = (angleDeg * Math.PI) / 180;
|
||||
const cos = Math.cos(radians);
|
||||
const sin = Math.sin(radians);
|
||||
const triangle = (points: Array<[number, number]>, value: number): void => {
|
||||
const [a, b, c] = points.map(([x, y]): [number, number] => [cx + x * cos - y * sin, cy + x * sin + y * cos]);
|
||||
fillTriangle(image, a![0], a![1], b![0], b![1], c![0], c![1], value);
|
||||
};
|
||||
// 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);
|
||||
triangle([[0, -11], [-9, 9], [9, 9]], 0);
|
||||
triangle([[0, -8], [-6, 6], [6, 6]], 255);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, value: number): void {
|
||||
@@ -866,6 +1478,9 @@ function render(): void {
|
||||
}
|
||||
|
||||
function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
|
||||
// Every state change funnels through here, so it doubles as the point where
|
||||
// the compass claim tracks foreground/phase/mode.
|
||||
reconcileCompass();
|
||||
if (!win.foreground && inputFrameId === 0) return;
|
||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${win.windowId}`);
|
||||
try {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createNightscoutAppWindow, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app";
|
||||
import { createNightscoutAppWindow, startNightscoutTrayIcon, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app";
|
||||
|
||||
const nightscoutApp: AppDefinition = {
|
||||
appId: "nightscout",
|
||||
title: "Nightscout",
|
||||
icon: "nightscout",
|
||||
boot: () => startNightscoutTrayIcon(),
|
||||
launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
export type NightscoutAlertValues = {
|
||||
cageTimestampMs: number | null;
|
||||
reservoirUnits: number | null;
|
||||
batteryVoltage: number | null;
|
||||
loopTimestampMs: number | null;
|
||||
};
|
||||
|
||||
export type NightscoutThresholds = {
|
||||
maxCannulaAgeHours: number;
|
||||
cartridgeLowUnits: number;
|
||||
batteryLowVoltage: number;
|
||||
maxLoopAgeMinutes: number;
|
||||
};
|
||||
|
||||
/** Zero disables a threshold. Missing measurements do not imply a breach. */
|
||||
export function evaluateNightscoutAlerts(values: NightscoutAlertValues, limits: NightscoutThresholds, nowMs: number) {
|
||||
const known = (value: number | null): value is number => value !== null && Number.isFinite(value) && value >= 0;
|
||||
const enabled = (value: number): boolean => Number.isFinite(value) && value > 0;
|
||||
const tooOld = (timestamp: number | null, limit: number, unitMs: number): boolean =>
|
||||
enabled(limit) && known(timestamp) && timestamp > 0 && nowMs - timestamp > limit * unitMs;
|
||||
const tooLow = (value: number | null, limit: number): boolean => enabled(limit) && known(value) && value < limit;
|
||||
const cannula = tooOld(values.cageTimestampMs, limits.maxCannulaAgeHours, 3_600_000);
|
||||
const cartridge = tooLow(values.reservoirUnits, limits.cartridgeLowUnits);
|
||||
const battery = tooLow(values.batteryVoltage, limits.batteryLowVoltage);
|
||||
const loop = tooOld(values.loopTimestampMs, limits.maxLoopAgeMinutes, 60_000);
|
||||
return { cannula, cartridge, battery, loop, any: cannula || cartridge || battery || loop };
|
||||
}
|
||||
|
||||
export function normalizeNightscoutThreshold(value: string | null | undefined): string {
|
||||
const text = (value ?? "").trim();
|
||||
const number = Number(text);
|
||||
return /^(\d+(\.\d*)?|\.\d+)$/.test(text) && Number.isFinite(number) && number >= 0 ? String(number) : "0";
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import {
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { evaluateNightscoutAlerts } from "./nightscout-alerts";
|
||||
import { loadNightscoutThresholds, nightscoutAlwaysShowInTopBarSetting, onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
|
||||
@@ -19,14 +21,34 @@ 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.
|
||||
*/
|
||||
const windowRenders = new Set<() => void>();
|
||||
let trayStarted = false;
|
||||
let trayTick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
function syncNightscout(): void {
|
||||
const visible = windowRenders.size > 0 || nightscoutAlwaysShowInTopBarSetting.get();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, visible ? buildNightscoutTrayIcon(nightscoutBridge.snapshot()) : null);
|
||||
for (const render of windowRenders) render();
|
||||
// Age warnings and stale glucose must advance even during a stalled fetch.
|
||||
if (visible && trayTick === null) trayTick = setInterval(syncNightscout, 60_000);
|
||||
if (!visible && trayTick !== null) {
|
||||
clearInterval(trayTick);
|
||||
trayTick = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Shell-lifetime subscription so the tray can be enabled before any window opens. */
|
||||
export function startNightscoutTrayIcon(): void {
|
||||
if (trayStarted) return;
|
||||
trayStarted = true;
|
||||
nightscoutBridge.onStateChange(syncNightscout);
|
||||
onAnySettingChanged(syncNightscout);
|
||||
}
|
||||
|
||||
/** Full-screen glucose view with a shared, optionally persistent tray readout. */
|
||||
export function createNightscoutAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
startNightscoutTrayIcon();
|
||||
const render = () => app.requestRender();
|
||||
const app = createInProcessWindow({
|
||||
appId: "nightscout",
|
||||
windowId: NIGHTSCOUT_WINDOW_ID,
|
||||
@@ -41,28 +63,25 @@ export function createNightscoutAppWindow(options: InProcessAppOptions): InProce
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
windowRenders.delete(render);
|
||||
syncNightscout();
|
||||
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();
|
||||
});
|
||||
windowRenders.add(render);
|
||||
syncNightscout();
|
||||
return app;
|
||||
}
|
||||
|
||||
/** BG value (struck through when stale) + minimal 2-hour line graph. */
|
||||
/** BG value, 2-hour graph, then a warning triangle when any threshold is breached. */
|
||||
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 warning = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs).any;
|
||||
const graphEnd = labelWidth + 4 + TRAY_GRAPH_WIDTH;
|
||||
const image = new GrayImage(graphEnd + (warning ? 24 : 0), TRAY_HEIGHT, 0);
|
||||
|
||||
const textY = Math.max(0, ((TRAY_HEIGHT - font.lineHeight) / 2) | 0);
|
||||
image.drawText(font, 0, textY, label, 220);
|
||||
@@ -72,6 +91,14 @@ function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
|
||||
}
|
||||
|
||||
drawTrayGraph(image, labelWidth + 4, state, nowMs);
|
||||
if (warning) {
|
||||
const x = graphEnd + 4;
|
||||
image.drawLine(x + 9, 3, x, 20, 255);
|
||||
image.drawLine(x, 20, x + 18, 20, 255);
|
||||
image.drawLine(x + 18, 20, x + 9, 3, 255);
|
||||
image.fillRect(x + 8, 9, 2, 6, 255);
|
||||
image.fillRect(x + 8, 17, 2, 2, 255);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { evaluateNightscoutAlerts } from "./nightscout-alerts";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
@@ -6,6 +7,13 @@ import { Layer, LayerContext } from "../../ui/layers";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
|
||||
import {
|
||||
isNightscoutSettingsConfigured,
|
||||
loadNightscoutThresholds,
|
||||
nightscoutMaxCannulaAgeSetting,
|
||||
nightscoutCartridgeLowSetting,
|
||||
nightscoutBatteryLowSetting,
|
||||
nightscoutMaxLoopAgeSetting,
|
||||
nightscoutAlwaysShowInTopBarSetting,
|
||||
toggleSettingMenuItem,
|
||||
nightscoutApiTokenSetting,
|
||||
nightscoutSiteUrlSetting,
|
||||
textSettingMenuItem,
|
||||
@@ -332,21 +340,43 @@ export class NightscoutLayer implements Layer {
|
||||
|
||||
const statusX = 170;
|
||||
const statusWidth = width - 22 - statusX;
|
||||
const statusLines: { text: string; shade: number; direction?: string }[] = [
|
||||
const alerts = evaluateNightscoutAlerts(nightscout, loadNightscoutThresholds(), nowMs);
|
||||
const statusLines: { text: string; shade: number; direction?: string; fields?: { text: string; warning?: boolean }[] }[] = [
|
||||
{ text: `Delta ${formatDelta(nightscout.delta)} Trend `, shade: 180, direction: nightscout.direction },
|
||||
{
|
||||
text: `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(2)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)} Updated ${formatTimestamp(latest.timestampMs)}`,
|
||||
shade: 160,
|
||||
},
|
||||
{
|
||||
text: `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)} Loop ${nightscout.openapsStatusShort}`,
|
||||
shade: 160,
|
||||
text: "", shade: 160,
|
||||
fields: [
|
||||
{ text: `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)}`, warning: alerts.cannula },
|
||||
{ text: `Loop ${formatAgeShortFromTimestamp(nightscout.loopTimestampMs, nowMs)}`, warning: alerts.loop },
|
||||
],
|
||||
},
|
||||
{
|
||||
text: "", shade: 150,
|
||||
fields: [
|
||||
{ text: "Pump" },
|
||||
{ text: nightscout.reservoirUnits === null ? "--U" : `${Math.round(nightscout.reservoirUnits)}U`, warning: alerts.cartridge },
|
||||
{ text: nightscout.batteryVoltage === null ? "--V" : `${nightscout.batteryVoltage.toFixed(2)}V`, warning: alerts.battery },
|
||||
{ text: nightscout.pumpStatus || "--" },
|
||||
],
|
||||
},
|
||||
{ text: `Pump ${nightscout.pumpStatus || "--"}`, shade: 150 },
|
||||
];
|
||||
let statusY = graphTop - 8 - statusLines.length * step;
|
||||
for (const line of statusLines) {
|
||||
if (line.direction !== undefined) {
|
||||
if (line.fields) {
|
||||
let x = statusX;
|
||||
for (const field of line.fields) {
|
||||
const text = truncateText(font, field.text, Math.max(0, statusX + statusWidth - x));
|
||||
const textWidth = font.measureText(text);
|
||||
if (field.warning && textWidth > 0) image.fillRect(x - 2, statusY - 1, textWidth + 4, font.lineHeight + 2, 230);
|
||||
image.drawText(font, x, statusY, text, field.warning ? 0 : line.shade);
|
||||
x += textWidth + font.measureText(" ");
|
||||
if (x >= statusX + statusWidth) break;
|
||||
}
|
||||
} else if (line.direction !== undefined) {
|
||||
image.drawText(font, statusX, statusY, line.text, line.shade);
|
||||
drawDirectionIndicator(image, font, statusX + font.measureText(line.text), statusY, line.direction, line.shade);
|
||||
} else {
|
||||
@@ -390,6 +420,11 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
openSettingsSubMenu(ctx, "Nightscout settings", [
|
||||
textSettingMenuItem(nightscoutSiteUrlSetting),
|
||||
textSettingMenuItem(nightscoutApiTokenSetting),
|
||||
textSettingMenuItem(nightscoutMaxCannulaAgeSetting),
|
||||
textSettingMenuItem(nightscoutCartridgeLowSetting),
|
||||
textSettingMenuItem(nightscoutBatteryLowSetting),
|
||||
textSettingMenuItem(nightscoutMaxLoopAgeSetting),
|
||||
toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting),
|
||||
]);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NotificationsListLayer } from "../../ui/notifications";
|
||||
import { NotificationFilterLayer } from "../../ui/notification-filter";
|
||||
import { onAndroidNotificationPosted } from "../../native/notification-icons";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
@@ -25,6 +26,13 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr
|
||||
iconLetter: "N",
|
||||
icon: "bell",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
label: "Notification filter",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new NotificationFilterLayer());
|
||||
},
|
||||
}],
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new NotificationsListLayer()),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
@@ -29,7 +29,8 @@ import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
@@ -71,7 +72,7 @@ const GRAVITY = 240;
|
||||
const DRAG_PER_S = 0.12;
|
||||
const MAX_SPEED = 600;
|
||||
const PHYSICS_DT = 1 / 120;
|
||||
const RENDER_TICK_MS = 110;
|
||||
const RENDER_TICK_MS = 50;
|
||||
/** Launch speed by power setting (index = power - 1). */
|
||||
const LAUNCH_SPEEDS = [320, 380, 440, 500, 560] as const;
|
||||
const BUMPER_KICK = 340;
|
||||
@@ -242,12 +243,13 @@ type Flipper = {
|
||||
type PinballWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: 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. */
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
/** "ready" = parked on the plunger awaiting launch; "live" = in play. */
|
||||
@@ -301,6 +303,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const window: PinballWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -443,9 +446,9 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's long-press menu (game actions + default entries). */
|
||||
function openWindowMenu(window: PinballWindow): void {
|
||||
windowMenu(window).open([
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for the nudge. */
|
||||
function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
@@ -462,13 +465,17 @@ function openWindowMenu(window: PinballWindow): void {
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
...defaultWindowMenuItems(window.windowId, post),
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: PinballWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -547,6 +554,10 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
}
|
||||
nudge(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
@@ -573,8 +584,8 @@ function handleIdleInput(window: PinballWindow, event: InputEvent, frameId: numb
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
case "long-press":
|
||||
openWindowMenu(window);
|
||||
case "short-then-long-press":
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball ignored input");
|
||||
@@ -945,10 +956,7 @@ function drawFlipper(image: GrayImage, flipper: Flipper): void {
|
||||
}
|
||||
|
||||
function paint(window: PinballWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: PinballWindow): GrayImage {
|
||||
|
||||
@@ -104,7 +104,7 @@ async function roamPost(endpoint: "q" | "pull" | "write", body: object): Promise
|
||||
const graph = roamGraphNameSetting.get();
|
||||
const token = roamApiTokenSetting.get();
|
||||
if (!graph || !token) {
|
||||
throw new Error("Set the Roam graph name and API token from the Roam app's long-press menu.");
|
||||
throw new Error("Set the Roam graph name and API token from the Roam app's context menu (tap, then hold).");
|
||||
}
|
||||
const path = `/api/graph/${encodeURIComponent(graph)}/${endpoint}`;
|
||||
const payload = JSON.stringify(body);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* todo or follows a [[page]] link, double-click walks back through visited
|
||||
* pages then yields focus. Edits (check/uncheck, add todo, edit block) go
|
||||
* through the Roam backend API and are also exposed as assistant tools.
|
||||
* The graph name and API token are configured here too, from the long-press
|
||||
* The graph name and API token are configured here too, from the context
|
||||
* menu, via the phone app's text editor.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
@@ -17,7 +17,7 @@ import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { 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, type InputEvent } from "../../ui/gestures";
|
||||
@@ -64,6 +64,7 @@ const smallFont = getDefaultSmallFont();
|
||||
type RoamWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
title: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
@@ -168,6 +169,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
window = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
@@ -327,6 +329,10 @@ function maybeRefreshStalePage(): void {
|
||||
function windowMenu(win: RoamWindow): WindowMenu {
|
||||
if (!win.menu) {
|
||||
win.menu = new WindowMenu({
|
||||
windowId: win.windowId,
|
||||
post,
|
||||
title: () => win.title,
|
||||
items: () => menuItems(win),
|
||||
size: { width: win.viewportWidth, height: win.viewportHeight },
|
||||
paintBase: () => paintContent(win),
|
||||
isFocused: () => win.focused,
|
||||
@@ -377,7 +383,7 @@ function menuItems(win: RoamWindow): MenuItem[] {
|
||||
beginSettingEdit(roamApiTokenSetting);
|
||||
},
|
||||
});
|
||||
return [...items, ...defaultWindowMenuItems(win.windowId, post)];
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -417,8 +423,8 @@ function handleInput(win: RoamWindow, event: InputEvent, frameId: number): void
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
windowMenu(win).open(menuItems(win));
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(win).open();
|
||||
renderAndSubmit(win, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -519,7 +525,7 @@ async function addTodoFromText(text: string): Promise<void> {
|
||||
|
||||
async function handleRoamTool(name: string, args: any): Promise<ToolResult> {
|
||||
if (!isRoamConfigured()) {
|
||||
return { ok: false, error: "Roam is not configured; set the graph name and API token from the Roam app's long-press menu." };
|
||||
return { ok: false, error: "Roam is not configured; set the graph name and API token from the Roam app's context menu (tap, then hold)." };
|
||||
}
|
||||
switch (name) {
|
||||
case "read_page": {
|
||||
@@ -580,9 +586,9 @@ async function handleRoamTool(name: string, args: any): Promise<ToolResult> {
|
||||
// Painting
|
||||
|
||||
function paint(win: RoamWindow): Plane[] {
|
||||
if (win.menu?.isOpen()) return win.menu.paint();
|
||||
if (editingSetting) return singlePlane(paintSettingEdit(win, editingSetting));
|
||||
return singlePlane(paintContent(win));
|
||||
return windowMenu(win).paint(() =>
|
||||
singlePlane(editingSetting ? paintSettingEdit(win, editingSetting) : paintContent(win)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The "type on the phone" screen shown while a Roam setting is being edited. */
|
||||
@@ -628,7 +634,7 @@ function paintContent(win: RoamWindow): GrayImage {
|
||||
image.drawLine(DOC_MARGIN, HEADER_HEIGHT - 4, win.viewportWidth - DOC_MARGIN, HEADER_HEIGHT - 4, 40);
|
||||
|
||||
if (!isRoamConfigured()) {
|
||||
drawBodyMessage(image, win, "Set the Roam graph name and API token: long-press for the menu, then pick Set graph name and Set API token.");
|
||||
drawBodyMessage(image, win, "Set the Roam graph name and API token: tap, then hold, for the menu; pick Set graph name and Set API token.");
|
||||
} else if (errorMessage) {
|
||||
drawBodyMessage(image, win, `${errorMessage}\n\n${GESTURE_CLICK} retry`);
|
||||
} else if (currentPage && currentPage.children.length === 0 && !loading) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createTeleprompterAppWindow, TELEPROMPTER_SURFACE_ID, TELEPROMPTER_WINDOW_ID } from "./teleprompter-app";
|
||||
|
||||
const teleprompterApp: AppDefinition = {
|
||||
appId: "teleprompter",
|
||||
title: "Teleprompter",
|
||||
icon: "scroll-text",
|
||||
launch: (ctx) =>
|
||||
ctx.launchInProcessApp(TELEPROMPTER_WINDOW_ID, TELEPROMPTER_SURFACE_ID, (options) =>
|
||||
createTeleprompterAppWindow({
|
||||
...options,
|
||||
startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture(),
|
||||
appendLog: (message) => ctx.appendLog(message),
|
||||
}),
|
||||
),
|
||||
};
|
||||
|
||||
export default teleprompterApp;
|
||||
@@ -0,0 +1,300 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { ScriptTracker, tokenizeScript, type TokenizedScript } from "./script-tracker";
|
||||
|
||||
const MARGIN_X = 18;
|
||||
const BODY_TOP = 6;
|
||||
/** Gap between the last body line and the footer. */
|
||||
const FOOTER_GAP = 4;
|
||||
/** Lines per ring scroll tick. */
|
||||
const SCROLL_STEP_LINES = 2;
|
||||
|
||||
/** Brightness of words already spoken, still to come, and the next one. */
|
||||
const PAST_VALUE = 90;
|
||||
const UPCOMING_VALUE = 200;
|
||||
const NEXT_VALUE = 255;
|
||||
|
||||
type LayoutSpan = { tokenIndex: number; x: number; width: number; text: string };
|
||||
type LayoutLine = { spans: LayoutSpan[] };
|
||||
|
||||
export type ReaderHooks = {
|
||||
requestRender: () => void;
|
||||
/** Hold continuous mic capture while tracking is on. */
|
||||
startCapture: () => void;
|
||||
stopCapture: () => void;
|
||||
/** The reader closed (or paused) at this word position. */
|
||||
savePosition: (position: number) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The teleprompter's reading view. Lays the script out word by word in the
|
||||
* medium UI font, follows the speaker through it from the live transcript
|
||||
* (see ScriptTracker), auto-scrolls to keep the next word about a third of
|
||||
* the way down, and highlights that word. Ring scrolls move the view by hand
|
||||
* and re-anchor the tracker to what is now at the anchor row, so scrolling
|
||||
* doubles as "I'm here now". Click pauses/resumes tracking; double-click
|
||||
* goes back to the file list.
|
||||
*/
|
||||
export class TeleprompterReaderLayer implements Layer {
|
||||
private readonly script: TokenizedScript;
|
||||
private readonly tracker: ScriptTracker;
|
||||
private lines: LayoutLine[] | null = null;
|
||||
/** Line index of each word (parallel to script.words). */
|
||||
private wordLines: number[] = [];
|
||||
private layoutKey = "";
|
||||
private firstLine = 0;
|
||||
/** True after the tracker moved: the next paint re-centers on the position. */
|
||||
private autoScrollPending = true;
|
||||
private tracking = true;
|
||||
private status = "";
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
// Geometry from the last paint, for the input handlers.
|
||||
private visibleRows = 10;
|
||||
private anchorRow = 3;
|
||||
|
||||
constructor(
|
||||
text: string,
|
||||
private readonly title: string,
|
||||
private readonly hooks: ReaderHooks,
|
||||
initialPosition = 0,
|
||||
) {
|
||||
this.script = tokenizeScript(text);
|
||||
this.tracker = new ScriptTracker(this.script.words);
|
||||
this.tracker.anchor(initialPosition);
|
||||
}
|
||||
|
||||
/** Start listening. Called once the layer is on its stack. */
|
||||
attach(): void {
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.hooks.requestRender();
|
||||
});
|
||||
this.startTracking();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.hooks.savePosition(this.resumePosition());
|
||||
this.stopTracking();
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
}
|
||||
|
||||
get position(): number {
|
||||
return this.tracker.position;
|
||||
}
|
||||
|
||||
buildMenuItems(): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: this.tracking ? "Pause voice tracking" : "Resume voice tracking",
|
||||
onSelect: (ctx) => {
|
||||
this.toggleTracking();
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Restart from the top",
|
||||
onSelect: (ctx) => {
|
||||
this.tracker.anchor(0);
|
||||
this.firstLine = 0;
|
||||
this.autoScrollPending = true;
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultMediumFont();
|
||||
const smallFont = getDefaultSmallFont();
|
||||
const step = lineStep(font);
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
const footerY = height - smallFont.lineHeight - 4;
|
||||
this.visibleRows = Math.max(1, Math.floor((footerY - FOOTER_GAP - BODY_TOP) / step));
|
||||
this.anchorRow = Math.floor(this.visibleRows / 3);
|
||||
const lines = this.layout(font, width - 2 * MARGIN_X);
|
||||
|
||||
if (this.autoScrollPending) {
|
||||
this.autoScrollPending = false;
|
||||
this.firstLine = this.clampFirstLine(this.lineOfPosition() - this.anchorRow);
|
||||
} else {
|
||||
this.firstLine = this.clampFirstLine(this.firstLine);
|
||||
}
|
||||
|
||||
const position = this.tracker.position;
|
||||
const nextToken = position < this.script.words.length ? this.script.wordTokens[position]! : this.script.tokens.length;
|
||||
const lastLine = Math.min(lines.length, this.firstLine + this.visibleRows);
|
||||
for (let lineIndex = this.firstLine; lineIndex < lastLine; lineIndex++) {
|
||||
const y = BODY_TOP + (lineIndex - this.firstLine) * step;
|
||||
for (const span of lines[lineIndex]!.spans) {
|
||||
const isNext = span.tokenIndex === nextToken;
|
||||
const value = isNext ? NEXT_VALUE : span.tokenIndex < nextToken ? PAST_VALUE : UPCOMING_VALUE;
|
||||
image.drawText(font, MARGIN_X + span.x, y, span.text, value);
|
||||
if (isNext) {
|
||||
image.fillRect(MARGIN_X + span.x, y + font.lineHeight - 2, span.width, 2, NEXT_VALUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.paintFooter(image, smallFont, width, footerY);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "scroll-down":
|
||||
this.scrollBy(SCROLL_STEP_LINES);
|
||||
return;
|
||||
case "scroll-up":
|
||||
this.scrollBy(-SCROLL_STEP_LINES);
|
||||
return;
|
||||
case "click":
|
||||
this.toggleTracking();
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private paintFooter(image: GrayImage, font: UiFont, width: number, y: number): void {
|
||||
const total = this.script.words.length;
|
||||
const percent = total === 0 ? 0 : Math.round((100 * this.tracker.position) / total);
|
||||
const right = `${this.tracking ? this.status || "Listening" : "Paused"} ${percent}%`;
|
||||
const rightWidth = font.measureText(right);
|
||||
image.drawText(font, width - MARGIN_X - rightWidth, y, right, 110);
|
||||
const titleMax = width - 2 * MARGIN_X - rightWidth - 12;
|
||||
if (titleMax > 20) {
|
||||
image.drawText(font, MARGIN_X, y, truncateText(font, this.title, titleMax), 110);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hand-scroll by whole lines. The tracker is re-anchored to the first word
|
||||
* at (or after) the anchor row, so the highlight follows the view and
|
||||
* speech matching resumes from there.
|
||||
*/
|
||||
private scrollBy(deltaLines: number): void {
|
||||
const lines = this.lines;
|
||||
if (!lines) return;
|
||||
this.autoScrollPending = false;
|
||||
this.firstLine = this.clampFirstLine(this.firstLine + deltaLines);
|
||||
const anchorLine = this.firstLine + this.anchorRow;
|
||||
const word = this.firstWordAtOrAfterLine(anchorLine);
|
||||
this.tracker.anchor(word);
|
||||
}
|
||||
|
||||
private clampFirstLine(value: number): number {
|
||||
const lineCount = this.lines?.length ?? 0;
|
||||
// Scroll far enough that the last line can reach the anchor row.
|
||||
return clamp(value, 0, Math.max(0, lineCount - this.anchorRow - 1));
|
||||
}
|
||||
|
||||
private lineOfPosition(): number {
|
||||
const position = this.tracker.position;
|
||||
if (this.wordLines.length === 0) return 0;
|
||||
if (position >= this.wordLines.length) return this.wordLines[this.wordLines.length - 1]!;
|
||||
return this.wordLines[position]!;
|
||||
}
|
||||
|
||||
private firstWordAtOrAfterLine(line: number): number {
|
||||
for (let word = 0; word < this.wordLines.length; word++) {
|
||||
if (this.wordLines[word]! >= line) return word;
|
||||
}
|
||||
return this.wordLines.length;
|
||||
}
|
||||
|
||||
/** Where to resume next time: a script read to the end restarts from the top. */
|
||||
private resumePosition(): number {
|
||||
return this.tracker.position >= this.script.words.length ? 0 : this.tracker.position;
|
||||
}
|
||||
|
||||
private toggleTracking(): void {
|
||||
if (this.tracking) {
|
||||
this.stopTracking();
|
||||
this.hooks.savePosition(this.resumePosition());
|
||||
} else {
|
||||
this.startTracking();
|
||||
}
|
||||
}
|
||||
|
||||
private startTracking(): void {
|
||||
this.tracking = true;
|
||||
if (!this.unsubscribeTranscript) {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event));
|
||||
}
|
||||
this.hooks.startCapture();
|
||||
}
|
||||
|
||||
private stopTracking(): void {
|
||||
this.tracking = false;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.hooks.stopCapture();
|
||||
}
|
||||
|
||||
private onTranscript(event: VoiceTranscriptEvent): void {
|
||||
if (!this.tracking) return;
|
||||
if (this.tracker.feed(event.text)) {
|
||||
this.autoScrollPending = true;
|
||||
this.hooks.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
/** Word-by-word greedy line layout; cached per font and width. */
|
||||
private layout(font: UiFont, maxWidth: number): LayoutLine[] {
|
||||
const key = `${font.fingerprintId}:${maxWidth}`;
|
||||
if (this.lines && this.layoutKey === key) return this.lines;
|
||||
const spaceWidth = font.measureText(" ");
|
||||
const lines: LayoutLine[] = [];
|
||||
const wordLines: number[] = [];
|
||||
let current: LayoutLine = { spans: [] };
|
||||
let currentWidth = 0;
|
||||
let paragraph = -1;
|
||||
const flush = () => {
|
||||
lines.push(current);
|
||||
current = { spans: [] };
|
||||
currentWidth = 0;
|
||||
};
|
||||
for (let tokenIndex = 0; tokenIndex < this.script.tokens.length; tokenIndex++) {
|
||||
const token = this.script.tokens[tokenIndex]!;
|
||||
if (token.paragraph !== paragraph) {
|
||||
// New paragraph: end the current line, and leave one empty line per
|
||||
// blank source line (paragraph numbers count every newline).
|
||||
if (paragraph >= 0) {
|
||||
flush();
|
||||
for (let blank = paragraph + 1; blank < token.paragraph; blank++) flush();
|
||||
}
|
||||
paragraph = token.paragraph;
|
||||
}
|
||||
const wordWidth = font.measureText(token.text);
|
||||
const x = current.spans.length === 0 ? 0 : currentWidth + spaceWidth;
|
||||
if (current.spans.length > 0 && x + wordWidth > maxWidth) {
|
||||
flush();
|
||||
}
|
||||
const spanX = current.spans.length === 0 ? 0 : currentWidth + spaceWidth;
|
||||
current.spans.push({ tokenIndex, x: spanX, width: wordWidth, text: token.text });
|
||||
currentWidth = spanX + wordWidth;
|
||||
if (token.wordIndex >= 0) {
|
||||
wordLines[token.wordIndex] = lines.length;
|
||||
}
|
||||
}
|
||||
if (current.spans.length > 0 || lines.length === 0) flush();
|
||||
this.lines = lines;
|
||||
this.wordLines = wordLines;
|
||||
this.layoutKey = key;
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
/**
|
||||
* The Teleprompter's recently opened scripts, most recent first, with the
|
||||
* word position the reader was at when it closed (so reopening resumes
|
||||
* there). Stored as a JSON array in the settings store.
|
||||
*/
|
||||
export type RecentScript = {
|
||||
path: string;
|
||||
name: string;
|
||||
/** Word index the reader was at when last closed; 0 = start. */
|
||||
position: number;
|
||||
openedMs: number;
|
||||
};
|
||||
|
||||
const RECENTS_KEY = "teleprompter.recents";
|
||||
const MAX_RECENTS = 12;
|
||||
|
||||
export function getRecentScripts(): RecentScript[] {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(getStringSetting(RECENTS_KEY, "[]"));
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed
|
||||
.filter((entry): entry is RecentScript => !!entry && typeof entry === "object" && typeof entry.path === "string")
|
||||
.map((entry) => ({
|
||||
path: entry.path,
|
||||
name: typeof entry.name === "string" && entry.name ? entry.name : baseName(entry.path),
|
||||
position: typeof entry.position === "number" && entry.position >= 0 ? entry.position : 0,
|
||||
openedMs: typeof entry.openedMs === "number" ? entry.openedMs : 0,
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function saveRecentScripts(recents: RecentScript[]): void {
|
||||
setStringSetting(RECENTS_KEY, JSON.stringify(recents.slice(0, MAX_RECENTS)));
|
||||
}
|
||||
|
||||
/** Move (or add) a script to the top of the list, keeping its saved position. */
|
||||
export function touchRecentScript(path: string, name: string): RecentScript {
|
||||
const recents = getRecentScripts();
|
||||
const existing = recents.find((entry) => entry.path === path);
|
||||
const entry: RecentScript = {
|
||||
path,
|
||||
name,
|
||||
position: existing?.position ?? 0,
|
||||
openedMs: Date.now(),
|
||||
};
|
||||
saveRecentScripts([entry, ...recents.filter((other) => other.path !== path)]);
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function saveRecentScriptPosition(path: string, position: number): void {
|
||||
const recents = getRecentScripts();
|
||||
const entry = recents.find((other) => other.path === path);
|
||||
if (!entry) return;
|
||||
entry.position = Math.max(0, position);
|
||||
saveRecentScripts(recents);
|
||||
}
|
||||
|
||||
export function removeRecentScript(path: string): void {
|
||||
saveRecentScripts(getRecentScripts().filter((entry) => entry.path !== path));
|
||||
}
|
||||
|
||||
export function clearRecentScripts(): void {
|
||||
saveRecentScripts([]);
|
||||
}
|
||||
|
||||
export function baseName(path: string): string {
|
||||
const slash = path.lastIndexOf("/");
|
||||
return slash >= 0 ? path.slice(slash + 1) : path;
|
||||
}
|
||||
|
||||
/** The last directory segment of a file path, for a dim "where it lives" hint. */
|
||||
export function parentDirName(path: string): string {
|
||||
const slash = path.lastIndexOf("/");
|
||||
if (slash <= 0) return "";
|
||||
return baseName(path.slice(0, slash));
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
/**
|
||||
* Follows a speaker through a script from live speech-to-text. Pure logic
|
||||
* (no NativeScript imports) so it runs under plain node for the tests.
|
||||
*
|
||||
* The script is tokenized once into display tokens; the subset with a
|
||||
* non-empty normalized key are the "words" the tracker aligns against. The
|
||||
* tracker keeps `position`: the index (in word space) of the next word the
|
||||
* speaker is expected to say. Each transcript update (replace semantics: the
|
||||
* whole current utterance, partial or final) re-aligns the tail of the
|
||||
* transcript against a window of script words around the current position,
|
||||
* with a local alignment that tolerates misrecognized, skipped and ad-libbed
|
||||
* words, and moves the position to just after the last aligned script word.
|
||||
*/
|
||||
|
||||
export type ScriptToken = {
|
||||
/** The token as written, for display. */
|
||||
text: string;
|
||||
/** Paragraph number (newline-separated), for layout. */
|
||||
paragraph: number;
|
||||
/** Lowercased alphanumeric key, or "" for punctuation-only tokens. */
|
||||
key: string;
|
||||
/** Index into the tracker's word list, or -1 when key is empty. */
|
||||
wordIndex: number;
|
||||
};
|
||||
|
||||
export type TokenizedScript = {
|
||||
tokens: ScriptToken[];
|
||||
/** Normalized keys of the trackable tokens, in order. */
|
||||
words: string[];
|
||||
/** Token index of each word. */
|
||||
wordTokens: number[];
|
||||
};
|
||||
|
||||
/** Lowercase, strip everything but letters/digits, fold common apostrophes. */
|
||||
export function normalizeWord(raw: string): string {
|
||||
return raw
|
||||
.normalize("NFKD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "");
|
||||
}
|
||||
|
||||
export function tokenizeScript(text: string): TokenizedScript {
|
||||
const tokens: ScriptToken[] = [];
|
||||
const words: string[] = [];
|
||||
const wordTokens: number[] = [];
|
||||
const paragraphs = text.replace(/\r/g, "").split("\n");
|
||||
for (let paragraph = 0; paragraph < paragraphs.length; paragraph++) {
|
||||
for (const raw of paragraphs[paragraph]!.split(/\s+/)) {
|
||||
if (!raw) continue;
|
||||
const key = normalizeWord(raw);
|
||||
const wordIndex = key ? words.length : -1;
|
||||
if (key) {
|
||||
words.push(key);
|
||||
wordTokens.push(tokens.length);
|
||||
}
|
||||
tokens.push({ text: raw, paragraph, key, wordIndex });
|
||||
}
|
||||
}
|
||||
return { tokens, words, wordTokens };
|
||||
}
|
||||
|
||||
/** Split a transcript into normalized keys, dropping empties. */
|
||||
export function transcriptKeys(text: string): string[] {
|
||||
return text
|
||||
.split(/\s+/)
|
||||
.map(normalizeWord)
|
||||
.filter((key) => key.length > 0);
|
||||
}
|
||||
|
||||
// Function words match so often that on their own they say little about
|
||||
// where the speaker is; they count for less than content words.
|
||||
const STOP_WORDS = new Set([
|
||||
"a", "an", "the", "and", "or", "but", "of", "to", "in", "on", "at", "for", "is", "it", "its",
|
||||
"i", "we", "you", "he", "she", "they", "that", "this", "as", "be", "are", "was", "were",
|
||||
"with", "by", "so", "if", "not", "no", "do", "have", "has", "had", "will", "can",
|
||||
]);
|
||||
|
||||
const MATCH_SCORE = 3;
|
||||
const STOP_WORD_MATCH_SCORE = 1.5;
|
||||
/** A fuzzy (edit-distance 1) or prefix match: real evidence, but weaker. */
|
||||
const FUZZY_MATCH_SCORE = 2;
|
||||
const MISMATCH_SCORE = -2;
|
||||
const GAP_SCORE = -1.5;
|
||||
/** The alignment must be worth at least this to move the position at all. */
|
||||
const MIN_ALIGNMENT_SCORE = 4.5;
|
||||
const MIN_MATCHED_WORDS = 2;
|
||||
/** Per-word cost of ending an alignment away from the expected position (a tie-breaker). */
|
||||
const FORWARD_DISTANCE_COST = 0.02;
|
||||
const BACKWARD_DISTANCE_COST = 0.1;
|
||||
|
||||
export type TrackerOptions = {
|
||||
/** Script words before the current position that stay in play (repeats, backtracking). */
|
||||
back?: number;
|
||||
/** Script words after the current position that stay in play (skipping ahead). */
|
||||
ahead?: number;
|
||||
/** Only the last this-many transcript words are aligned. */
|
||||
tailWords?: number;
|
||||
};
|
||||
|
||||
const DEFAULT_OPTIONS: Required<TrackerOptions> = { back: 30, ahead: 160, tailWords: 20 };
|
||||
|
||||
/**
|
||||
* Edit distance capped at 1 (true when the strings are within one edit),
|
||||
* which covers the typical STT slip: a dropped/extra letter or a substitution.
|
||||
*/
|
||||
export function withinOneEdit(a: string, b: string): boolean {
|
||||
if (a === b) return true;
|
||||
const lengthDelta = a.length - b.length;
|
||||
if (lengthDelta > 1 || lengthDelta < -1) return false;
|
||||
const [short, long] = a.length <= b.length ? [a, b] : [b, a];
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
let edits = 0;
|
||||
while (i < short.length && j < long.length) {
|
||||
if (short[i] === long[j]) {
|
||||
i++;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (++edits > 1) return false;
|
||||
if (short.length === long.length) {
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return edits + (long.length - j) <= 1;
|
||||
}
|
||||
|
||||
/** Score of aligning transcript key `spoken` with script key `expected`. */
|
||||
function matchScore(spoken: string, expected: string, isLastSpoken: boolean): number {
|
||||
if (spoken === expected) {
|
||||
return STOP_WORDS.has(expected) ? STOP_WORD_MATCH_SCORE : MATCH_SCORE;
|
||||
}
|
||||
// The final transcript word is often still being recognized: a prefix of
|
||||
// the script word counts, once it is long enough not to be noise.
|
||||
if (isLastSpoken && spoken.length >= 2 && expected.length > spoken.length && expected.startsWith(spoken)) {
|
||||
return FUZZY_MATCH_SCORE;
|
||||
}
|
||||
if (spoken.length >= 5 && expected.length >= 5 && withinOneEdit(spoken, expected)) {
|
||||
return FUZZY_MATCH_SCORE;
|
||||
}
|
||||
return MISMATCH_SCORE;
|
||||
}
|
||||
|
||||
export type AlignmentResult = {
|
||||
/** Index (in the script word list) of the word after the last aligned one; the new position. */
|
||||
position: number;
|
||||
score: number;
|
||||
matchedWords: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Align the transcript tail against script words [windowStart, windowEnd).
|
||||
* Local alignment (free start on both sides) that must consume the last
|
||||
* transcript word and may end anywhere in the window. Returns null when the
|
||||
* best alignment is too weak to trust.
|
||||
*/
|
||||
export function alignTail(
|
||||
words: readonly string[],
|
||||
spoken: readonly string[],
|
||||
windowStart: number,
|
||||
windowEnd: number,
|
||||
expectedPosition: number,
|
||||
): AlignmentResult | null {
|
||||
const m = spoken.length;
|
||||
const n = windowEnd - windowStart;
|
||||
if (m === 0 || n <= 0) return null;
|
||||
|
||||
// H[i][j]: best score of an alignment ending with spoken[i-1] / window[j-1]
|
||||
// (or a gap), clamped at 0 (free start). Matches counts how many spoken
|
||||
// words matched along the path that produced H[i][j].
|
||||
const width = n + 1;
|
||||
const score = new Float64Array((m + 1) * width);
|
||||
const matches = new Uint16Array((m + 1) * width);
|
||||
for (let i = 1; i <= m; i++) {
|
||||
const spokenWord = spoken[i - 1]!;
|
||||
const isLast = i === m;
|
||||
for (let j = 1; j <= n; j++) {
|
||||
const here = i * width + j;
|
||||
const s = matchScore(spokenWord, words[windowStart + j - 1]!, isLast);
|
||||
const diagonal = score[here - width - 1]! + s;
|
||||
const skipScript = score[here - 1]! + GAP_SCORE;
|
||||
const skipSpoken = score[here - width]! + GAP_SCORE;
|
||||
let best = 0;
|
||||
let bestMatches = 0;
|
||||
if (diagonal > best) {
|
||||
best = diagonal;
|
||||
bestMatches = matches[here - width - 1]! + (s > 0 ? 1 : 0);
|
||||
}
|
||||
if (skipScript > best) {
|
||||
best = skipScript;
|
||||
bestMatches = matches[here - 1]!;
|
||||
}
|
||||
if (skipSpoken > best) {
|
||||
best = skipSpoken;
|
||||
bestMatches = matches[here - width]!;
|
||||
}
|
||||
score[here] = best;
|
||||
matches[here] = bestMatches;
|
||||
}
|
||||
}
|
||||
|
||||
let bestColumn = -1;
|
||||
let bestAdjusted = -Infinity;
|
||||
for (let j = 1; j <= n; j++) {
|
||||
const raw = score[m * width + j]!;
|
||||
if (raw <= 0) continue;
|
||||
const position = windowStart + j;
|
||||
const distance = position - expectedPosition;
|
||||
const adjusted = raw - (distance >= 0 ? distance * FORWARD_DISTANCE_COST : -distance * BACKWARD_DISTANCE_COST);
|
||||
if (adjusted > bestAdjusted) {
|
||||
bestAdjusted = adjusted;
|
||||
bestColumn = j;
|
||||
}
|
||||
}
|
||||
if (bestColumn < 0) return null;
|
||||
const rawScore = score[m * width + bestColumn]!;
|
||||
const matchedWords = matches[m * width + bestColumn]!;
|
||||
if (rawScore < MIN_ALIGNMENT_SCORE || matchedWords < MIN_MATCHED_WORDS) return null;
|
||||
return { position: windowStart + bestColumn, score: rawScore, matchedWords };
|
||||
}
|
||||
|
||||
export class ScriptTracker {
|
||||
private readonly options: Required<TrackerOptions>;
|
||||
/** Index of the next word the speaker is expected to say (0..words.length). */
|
||||
position = 0;
|
||||
|
||||
constructor(
|
||||
private readonly words: readonly string[],
|
||||
options: TrackerOptions = {},
|
||||
) {
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
}
|
||||
|
||||
get wordCount(): number {
|
||||
return this.words.length;
|
||||
}
|
||||
|
||||
/** Jump the expected position (manual scroll, restart). */
|
||||
anchor(position: number): void {
|
||||
this.position = Math.max(0, Math.min(this.words.length, position));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a transcript update (the whole current utterance, partial or
|
||||
* final). Returns true when the position moved.
|
||||
*/
|
||||
feed(text: string): boolean {
|
||||
const keys = transcriptKeys(text);
|
||||
if (keys.length === 0) return false;
|
||||
const tail = keys.length > this.options.tailWords ? keys.slice(-this.options.tailWords) : keys;
|
||||
const windowStart = Math.max(0, this.position - this.options.back);
|
||||
const windowEnd = Math.min(this.words.length, this.position + this.options.ahead);
|
||||
const result = alignTail(this.words, tail, windowStart, windowEnd, this.position);
|
||||
if (!result || result.position === this.position) return false;
|
||||
this.position = result.position;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
|
||||
import { LIST_ROW_TEXT_INSET } from "../../ui/metrics";
|
||||
import { MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { FileBrowserLayer } from "../files/file-browser";
|
||||
import { TeleprompterReaderLayer } from "./reader";
|
||||
import {
|
||||
baseName,
|
||||
clearRecentScripts,
|
||||
getRecentScripts,
|
||||
parentDirName,
|
||||
removeRecentScript,
|
||||
saveRecentScriptPosition,
|
||||
touchRecentScript,
|
||||
} from "./recent-files";
|
||||
|
||||
export const TELEPROMPTER_WINDOW_ID = "teleprompter";
|
||||
export const TELEPROMPTER_SURFACE_ID = "window:teleprompter";
|
||||
|
||||
const SCRIPT_FILE = /\.(txt|md|markdown|text|log)$/i;
|
||||
|
||||
const HOME_LAYOUT = {
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
showBorder: false,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
opaque: true,
|
||||
};
|
||||
|
||||
export type TeleprompterAppOptions = InProcessAppOptions & {
|
||||
/** Hold continuous mic capture while a script is being tracked. */
|
||||
startContinuousVoiceCapture: () => void;
|
||||
stopContinuousVoiceCapture: () => void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The Teleprompter app window. The home page is a Browse entry plus the
|
||||
* recently opened scripts; Browse pushes the Files app's browser to pick a
|
||||
* text file, and either path pushes the reader (see TeleprompterReaderLayer)
|
||||
* over the home page.
|
||||
*/
|
||||
export function createTeleprompterAppWindow(options: TeleprompterAppOptions): InProcessWindow {
|
||||
// The home menu's items are rebuilt in place whenever the recents change
|
||||
// (MenuLayer reads the array each paint).
|
||||
const homeItems: MenuItem[] = [];
|
||||
const home = new MenuLayer("Teleprompter", homeItems, HOME_LAYOUT);
|
||||
let created: InProcessWindow | null = null;
|
||||
let notice = "";
|
||||
|
||||
const requestRender = () => created?.requestRender();
|
||||
|
||||
/** The layer currently on top of the window's stack (the home menu at base). */
|
||||
const topLayer = (): Layer => {
|
||||
let top: Layer = home;
|
||||
created?.stack.topMatches((layer) => {
|
||||
top = layer;
|
||||
return true;
|
||||
});
|
||||
return top;
|
||||
};
|
||||
|
||||
const openScript = (ctx: LayerContext, path: string, name: string) => {
|
||||
const text = readTextFile(path);
|
||||
if (text === null) {
|
||||
removeRecentScript(path);
|
||||
notice = `Could not read ${name}`;
|
||||
options.appendLog(`teleprompter: could not read ${path}`);
|
||||
refreshHome();
|
||||
return;
|
||||
}
|
||||
const recent = touchRecentScript(path, name);
|
||||
notice = "";
|
||||
refreshHome();
|
||||
const layer = new TeleprompterReaderLayer(
|
||||
text,
|
||||
name,
|
||||
{
|
||||
requestRender,
|
||||
startCapture: () => options.startContinuousVoiceCapture(),
|
||||
stopCapture: () => options.stopContinuousVoiceCapture(),
|
||||
savePosition: (position) => saveRecentScriptPosition(path, position),
|
||||
},
|
||||
recent.position,
|
||||
);
|
||||
ctx.stack.push(layer);
|
||||
layer.attach();
|
||||
};
|
||||
|
||||
const openBrowser = (ctx: LayerContext) => {
|
||||
const stack = ctx.stack;
|
||||
const picker = new FileBrowserLayer({
|
||||
isSupportedFile: (name) => SCRIPT_FILE.test(name),
|
||||
onLeave: () => stack.pop(),
|
||||
onFilePicked: (entry: DirectoryEntry, pickCtx) => {
|
||||
if (!SCRIPT_FILE.test(entry.name)) {
|
||||
notice = "Pick a text file (.txt, .md)";
|
||||
return;
|
||||
}
|
||||
pickCtx.stack.pop();
|
||||
openScript(pickCtx, entry.path, entry.name);
|
||||
},
|
||||
});
|
||||
stack.push(picker);
|
||||
};
|
||||
|
||||
function refreshHome(): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const items: MenuItem[] = [
|
||||
{
|
||||
label: "Browse for a script...",
|
||||
onSelect: (ctx) => openBrowser(ctx),
|
||||
},
|
||||
];
|
||||
for (const recent of getRecentScripts()) {
|
||||
const folder = parentDirName(recent.path);
|
||||
items.push({
|
||||
label: recent.name || baseName(recent.path),
|
||||
onSelect: (ctx) => openScript(ctx, recent.path, recent.name),
|
||||
render: ({ image, x, y, width, selected, text }) => {
|
||||
const value = selected ? 255 : 200;
|
||||
const folderWidth = folder ? font.measureText(folder) : 0;
|
||||
const nameMax = folder ? width - folderWidth - 12 : width;
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, truncateText(font, text, Math.max(40, nameMax)), value);
|
||||
if (folder && nameMax >= 40) {
|
||||
image.drawText(font, x + width - folderWidth, y + LIST_ROW_TEXT_INSET, folder, 90);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
if (notice) {
|
||||
items.push({ label: notice, disabled: true, onSelect: () => {} });
|
||||
}
|
||||
homeItems.length = 0;
|
||||
homeItems.push(...items);
|
||||
// Land on the most recent script (one click resumes it), else on Browse.
|
||||
home.selectItem(getRecentScripts().length > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
refreshHome();
|
||||
|
||||
created = createInProcessWindow({
|
||||
appId: "teleprompter",
|
||||
windowId: TELEPROMPTER_WINDOW_ID,
|
||||
title: "Teleprompter",
|
||||
iconLetter: "Tp",
|
||||
icon: "scroll-text",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
menuItems: () => {
|
||||
const top = topLayer();
|
||||
if (top instanceof TeleprompterReaderLayer) return top.buildMenuItems();
|
||||
if (top instanceof FileBrowserLayer) return top.buildMenuItems();
|
||||
if (top !== home) return [];
|
||||
return [
|
||||
{
|
||||
label: "Clear recent scripts",
|
||||
onSelect: (ctx) => {
|
||||
clearRecentScripts();
|
||||
refreshHome();
|
||||
ctx.stack.pop();
|
||||
},
|
||||
},
|
||||
];
|
||||
},
|
||||
onFocus: (lastInput) => {
|
||||
const top = topLayer();
|
||||
if (top instanceof FileBrowserLayer) top.onFocus(lastInput);
|
||||
},
|
||||
baseLayer: new YieldAtRootLayer(home),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
return created;
|
||||
}
|
||||
@@ -32,11 +32,11 @@
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { TERMINAL_ICON_GLYPHS } from "../../graphics/icons";
|
||||
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -48,7 +48,7 @@ import { connectionDisplayName, loadConnections, parseConnectionString, saveConn
|
||||
import { TerminalEmulator } from "./terminal-emulator";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
@@ -88,6 +88,8 @@ const NEW_CONNECTION_DRAFT_KEY = "terminal.newConnectionDraft";
|
||||
type BaseWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
/** Shell-side window title, echoed back in the open-window message. */
|
||||
title: string;
|
||||
/** Content viewport from the shell's open-window message; grid = viewport / cell. */
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
@@ -283,7 +285,7 @@ const sessionRecency = new Map<string, number>();
|
||||
// continues, so 5s of slack keeps the indicator lit through the gaps.
|
||||
const sessionActivity = new Map<string, number>();
|
||||
const ACTIVITY_ACTIVE_MS = 5_000;
|
||||
// Alternation period of the hub's activity indicator.
|
||||
// Alternation period shared by hub rows and sidebar cursors.
|
||||
const HUB_ANIMATION_STEP_MS = 800;
|
||||
|
||||
function recencyKey(connectionId: string, socket: string): string {
|
||||
@@ -304,8 +306,32 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "open-window":
|
||||
openWindow(message.windowId, message.surfaceId, message.viewport);
|
||||
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
|
||||
break;
|
||||
case "resize-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize(message.viewport);
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.kind === "view") {
|
||||
window.gridCols = Math.floor(window.viewportWidth / window.cellWidth);
|
||||
window.gridRows = Math.floor(window.viewportHeight / window.cellHeight);
|
||||
if (window.reconnectTimer) clearTimeout(window.reconnectTimer);
|
||||
window.reconnectTimer = null;
|
||||
window.client.stop();
|
||||
window.client.setViewport(window.gridCols, window.gridRows);
|
||||
window.emulator.dispose();
|
||||
window.emulator = new TerminalEmulator(window.gridCols, window.gridRows);
|
||||
window.receivedData = false;
|
||||
window.archive = [];
|
||||
window.scrollTop = null;
|
||||
reconnectView(window);
|
||||
}
|
||||
scheduleRender(window);
|
||||
break;
|
||||
}
|
||||
case "close-window":
|
||||
closeWindow(message.windowId);
|
||||
break;
|
||||
@@ -430,11 +456,12 @@ function cancelPendingReconnects(): void {
|
||||
}
|
||||
}
|
||||
|
||||
function openWindow(windowId: string, surfaceId: string, viewport: { width: number; height: number }): void {
|
||||
function openWindow(windowId: string, surfaceId: string, title: string, viewport: { width: number; height: number }): void {
|
||||
const pendingView = pendingViews.get(windowId);
|
||||
if (pendingView) {
|
||||
pendingViews.delete(windowId);
|
||||
windows.set(windowId, createViewWindow(windowId, surfaceId, viewport, pendingView));
|
||||
windows.set(windowId, createViewWindow(windowId, surfaceId, title, viewport, pendingView));
|
||||
updateHubAnimation();
|
||||
renderHubWindows();
|
||||
return;
|
||||
}
|
||||
@@ -443,6 +470,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
|
||||
kind: "hub",
|
||||
windowId,
|
||||
surfaceId,
|
||||
title,
|
||||
viewportWidth: viewport.width,
|
||||
viewportHeight: viewport.height,
|
||||
gridCols: Math.floor(viewport.width / hubCell.cellWidth),
|
||||
@@ -463,6 +491,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
|
||||
if (!controlsInitialized) {
|
||||
syncControlsFromSettings();
|
||||
}
|
||||
updateHubAnimation();
|
||||
}
|
||||
|
||||
function closeWindow(windowId: string): void {
|
||||
@@ -483,6 +512,8 @@ function closeWindow(windowId: string): void {
|
||||
endAddConnection(window);
|
||||
}
|
||||
windows.delete(windowId);
|
||||
windowIconActivity.delete(windowId);
|
||||
updateHubAnimation();
|
||||
// Auto-reconnect only runs while at least one terminal window is open.
|
||||
if (windows.size === 0) {
|
||||
for (const control of controls.values()) {
|
||||
@@ -693,42 +724,47 @@ function renderHubWindows(): void {
|
||||
}
|
||||
}
|
||||
|
||||
// Hub activity animation: while a foregrounded hub lists at least one active
|
||||
// session, a timer re-renders it so the per-row indicator alternates. The
|
||||
// timer stops itself once every session's activity ages out (its final tick
|
||||
// renders the rows indicator-free) or the hub leaves the foreground.
|
||||
// One animation clock for foreground hub rows and every terminal sidebar icon.
|
||||
// Keep ticking while the sidebar can show activity, even with the hub closed.
|
||||
let hubAnimationPhase = 0;
|
||||
let hubAnimationTimer: ReturnType<typeof setInterval> | null = null;
|
||||
const windowIconActivity = new Map<string, IconActivity>();
|
||||
|
||||
function isSessionActive(connectionId: string, socket: string): boolean {
|
||||
const at = sessionActivity.get(recencyKey(connectionId, socket));
|
||||
return at !== undefined && Date.now() - at < ACTIVITY_ACTIVE_MS;
|
||||
}
|
||||
|
||||
function hubAnimationShouldRun(): boolean {
|
||||
if (!screenOn) return false;
|
||||
let hubVisible = false;
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "hub" && window.foreground) hubVisible = true;
|
||||
}
|
||||
if (!hubVisible) return false;
|
||||
function isWindowSessionActive(window: TerminalWindow): boolean {
|
||||
if (window.kind === "view") return isSessionActive(window.connectionId, window.socket);
|
||||
const now = Date.now();
|
||||
for (const at of sessionActivity.values()) {
|
||||
if (now - at < ACTIVITY_ACTIVE_MS) return true;
|
||||
}
|
||||
return false;
|
||||
return [...sessionActivity.values()].some((at) => now - at < ACTIVITY_ACTIVE_MS);
|
||||
}
|
||||
|
||||
/** Start or stop the animation timer to match the current state. */
|
||||
function hubAnimationShouldRun(): boolean {
|
||||
return screenOn && [...windows.values()].some(isWindowSessionActive);
|
||||
}
|
||||
|
||||
function syncWindowIconActivity(): void {
|
||||
for (const window of windows.values()) {
|
||||
const activity: IconActivity = screenOn && isWindowSessionActive(window)
|
||||
? (hubAnimationPhase === 0 ? "on" : "off") : "idle";
|
||||
if ((windowIconActivity.get(window.windowId) ?? "idle") === activity) continue;
|
||||
windowIconActivity.set(window.windowId, activity);
|
||||
post({ type: "set-icon-activity", windowId: window.windowId, activity });
|
||||
}
|
||||
}
|
||||
|
||||
/** Start or stop animation and publish changed icons, including expiry/wake. */
|
||||
function updateHubAnimation(): void {
|
||||
if (hubAnimationShouldRun()) {
|
||||
const shouldRun = hubAnimationShouldRun();
|
||||
if (shouldRun && !hubAnimationTimer) hubAnimationPhase = 0;
|
||||
syncWindowIconActivity();
|
||||
if (shouldRun) {
|
||||
if (hubAnimationTimer) return;
|
||||
hubAnimationTimer = setInterval(() => {
|
||||
hubAnimationPhase = (hubAnimationPhase + 1) % 2;
|
||||
if (!hubAnimationShouldRun() && hubAnimationTimer) {
|
||||
clearInterval(hubAnimationTimer);
|
||||
hubAnimationTimer = null;
|
||||
}
|
||||
updateHubAnimation();
|
||||
// Render even on the stopping tick, to clear expired indicators.
|
||||
renderHubWindows();
|
||||
}, HUB_ANIMATION_STEP_MS);
|
||||
@@ -753,6 +789,7 @@ function noteSessionActivity(connectionId: string, socket: string): void {
|
||||
function createViewWindow(
|
||||
windowId: string,
|
||||
surfaceId: string,
|
||||
title: string,
|
||||
viewport: { width: number; height: number },
|
||||
view: PendingView,
|
||||
): ViewWindow {
|
||||
@@ -768,6 +805,7 @@ function createViewWindow(
|
||||
kind: "view",
|
||||
windowId,
|
||||
surfaceId,
|
||||
title,
|
||||
viewportWidth: viewport.width,
|
||||
viewportHeight: viewport.height,
|
||||
gridCols,
|
||||
@@ -896,10 +934,14 @@ function reconnectView(window: ViewWindow): void {
|
||||
scheduleRender(window);
|
||||
}
|
||||
|
||||
/** The window's long-press menu, created lazily so window literals stay simple. */
|
||||
/** The window's context menu, created lazily so window literals stay simple. */
|
||||
function windowMenu(window: TerminalWindow): WindowMenu {
|
||||
if (!window.menu) {
|
||||
window.menu = new WindowMenu({
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => windowMenuItems(window),
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
isFocused: () => window.focused,
|
||||
@@ -978,7 +1020,6 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
|
||||
},
|
||||
);
|
||||
}
|
||||
items.push(...defaultWindowMenuItems(window.windowId, post));
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -992,8 +1033,8 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number)
|
||||
.then(() => renderAndSubmit(window, frameId));
|
||||
return;
|
||||
}
|
||||
if (event.type === "long-press") {
|
||||
windowMenu(window).open(windowMenuItems(window));
|
||||
if (event.type === "short-then-long-press") {
|
||||
windowMenu(window).open();
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
@@ -1426,8 +1467,7 @@ function openViewWindow(control: ControlConnection, socket: string, label: strin
|
||||
icon: "terminal",
|
||||
iconGlyph: glyph || undefined,
|
||||
focus: true,
|
||||
// Terminal views are the one full-height window kind: more rows matter
|
||||
// more than a small on-screen footprint.
|
||||
// Default to tall sessions; the app display setting can override this.
|
||||
heightMode: "max",
|
||||
});
|
||||
}
|
||||
@@ -1451,10 +1491,7 @@ async function launchAndOpenView(control: ControlConnection, preset: string): Pr
|
||||
}
|
||||
|
||||
function paint(window: TerminalWindow): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(paintContent(window));
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: TerminalWindow): GrayImage {
|
||||
|
||||
@@ -53,6 +53,10 @@ export class TerminalEmulator {
|
||||
this.term.write(data, onProcessed);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.term.dispose();
|
||||
}
|
||||
|
||||
/** Full reset; snapshots are defined as repainting from a cleared screen. */
|
||||
reset(): void {
|
||||
this.term.reset();
|
||||
|
||||
+35
-10
@@ -1,17 +1,42 @@
|
||||
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { registerTimerTools } from "../../assistant/timer-tools";
|
||||
import { timerEngine, timersWakeSetting } from "./timer-engine";
|
||||
import { createTimerAppWindow, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
|
||||
|
||||
/**
|
||||
* Timers, stopwatch and alarms. The clock engine boots with the shell so
|
||||
* timers and alarms fire (and the assistant's timer.* / alarm.* tools work)
|
||||
* with no window open; launching only opens the view on it.
|
||||
*/
|
||||
const timerApp: AppDefinition = {
|
||||
appId: "timer",
|
||||
title: "Timer",
|
||||
title: "Timers",
|
||||
icon: "timer",
|
||||
launch: (ctx) =>
|
||||
launchWorkerAppWindow(ctx, {
|
||||
createWorker: () => new Worker("./timer-app.worker"),
|
||||
windowId: "timer:main",
|
||||
title: "Timer",
|
||||
iconLetter: "T",
|
||||
icon: "timer",
|
||||
}),
|
||||
boot: (ctx) => {
|
||||
timerEngine.boot({
|
||||
playBuzzer: (payload) => ctx.actions.playBuzzerSequence(payload),
|
||||
onRing: (item) => {
|
||||
// Bring the ringing item on screen: focus the Timers window, then
|
||||
// wake the display if it is off (a setting, since a wearer may
|
||||
// prefer the phone alone while the glasses sleep). Only once it is
|
||||
// actually showing does the phone start its acknowledgement clock;
|
||||
// otherwise the phone adds its own sound within seconds.
|
||||
void ctx
|
||||
.launchApp("timer")
|
||||
.then(() => {
|
||||
if (timersWakeSetting.get() && !shell.isScreenOn()) shell.wake("window");
|
||||
if (shell.isScreenOn() && shell.foregroundWindow()?.windowId === TIMER_WINDOW_ID) {
|
||||
timerEngine.markDeliveredToGlasses(item);
|
||||
}
|
||||
})
|
||||
.catch((error) => console.error(`timers ring launch failed: ${error}`));
|
||||
},
|
||||
});
|
||||
startTimersTrayIcon();
|
||||
registerTimerTools((appId) => ctx.launchApp(appId));
|
||||
},
|
||||
launch: (ctx) => ctx.launchInProcessApp(TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow),
|
||||
};
|
||||
|
||||
export default timerApp;
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,745 +0,0 @@
|
||||
/**
|
||||
* 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 { type UiFont, GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import {
|
||||
cancelTimerNotification,
|
||||
fireTimerNotification,
|
||||
scheduleTimerNotification,
|
||||
} from "../../native/timer-notifications";
|
||||
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, type InputEvent } 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");
|
||||
// Resolved per use so a font-setting change applies without a worker restart.
|
||||
function smallFont(): UiFont {
|
||||
return 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);
|
||||
}
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
||||
// evaluation is synchronous, so the handler below is installed before any
|
||||
// queued message can be delivered.
|
||||
post({ type: "worker-ready" });
|
||||
|
||||
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;
|
||||
// Marks the main-thread -> worker hop, which is otherwise an
|
||||
// unexplained gap inside the shell's handle-input span.
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, 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: InputEvent, 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: InputEvent, 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: InputEvent, 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: InputEvent, 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): Plane[] {
|
||||
if (window.menu?.isOpen()) {
|
||||
return window.menu.paint();
|
||||
}
|
||||
return singlePlane(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: UiFont, 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: UiFont,
|
||||
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 planes = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const fingerprint = planesFingerprint(planes);
|
||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
|
||||
return;
|
||||
}
|
||||
const communicator = getActiveDisplay();
|
||||
if (!communicator) {
|
||||
frameTimings.finishFrame(frameId, "discarded: no active display");
|
||||
return;
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
);
|
||||
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`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,692 @@
|
||||
/**
|
||||
* The Timers app's clock engine: a main-thread singleton that owns the
|
||||
* timers / stopwatch / alarms state, persists it, schedules the durable
|
||||
* phone alarms behind every expiry, rings the glasses buzzer, and tells
|
||||
* listeners (the window, the tray icon, the assistant tools) when anything
|
||||
* changes. It is booted at shell start, so everything here works with the
|
||||
* window closed; the window is only a view on it.
|
||||
*
|
||||
* Two expiry paths run side by side: a JS deadline for prompt on-glasses
|
||||
* ringing while the process is awake, and an AlarmManager alarm-clock alarm
|
||||
* (FaceclawAlarms.java) that rings on the phone at the right moment even if
|
||||
* the process is asleep or dead. The phone shows the item silently first
|
||||
* and adds its own sound when the glasses cannot carry it (not connected,
|
||||
* not worn, charging, not shown within seconds, or not acknowledged within
|
||||
* 30 s); this engine keeps the phone told about the glasses, reports
|
||||
* delivery and acknowledgement, and replays what the wearer did on the
|
||||
* phone (dismiss / snooze) into its own state.
|
||||
*/
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum, timeFormatSetting } from "../../ui/dashboard-settings";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import {
|
||||
acknowledgePhoneAlarm,
|
||||
cancelPhoneAlarm,
|
||||
checkPhoneAlarmReliability,
|
||||
drainPhoneAlarmJournal,
|
||||
onPhoneAlarmAction,
|
||||
openPhoneAlarmReliabilityFix,
|
||||
phoneAlarmDeliveredToGlasses,
|
||||
ringPhoneAlarm,
|
||||
schedulePhoneAlarm,
|
||||
setPhoneAlarmGlassesStatus,
|
||||
type AlarmReliabilityIssue,
|
||||
type PhoneAlarmAction,
|
||||
} from "../../native/alarms";
|
||||
import { getGlassesPresence, glassesCanCarryAlert, onGlassesPresenceChanged } from "../../g2/glasses-presence";
|
||||
import { buildSoundSequencePayload, findSoundEffect, effectPhrases } from "../../ui/sound-effects";
|
||||
import {
|
||||
alarmNextRingMs,
|
||||
createAlarm,
|
||||
createTimer,
|
||||
emptyTimersState,
|
||||
formatDurationWords,
|
||||
formatTimeOfDay,
|
||||
parseTimersState,
|
||||
pushRecentDuration,
|
||||
serializeTimersState,
|
||||
stopwatchElapsedMs,
|
||||
timerRemainingMs,
|
||||
type Alarm,
|
||||
type CountdownTimer,
|
||||
type TimersState,
|
||||
} from "./timer-model";
|
||||
|
||||
const STATE_KEY = "timers.state";
|
||||
|
||||
/**
|
||||
* An expiry that passed longer ago than this while the process was down
|
||||
* (phone off, app killed) is marked rung silently rather than blaring on
|
||||
* the glasses hours late; the phone notification already fired on time.
|
||||
*/
|
||||
const RING_GRACE_MS = 5 * 60_000;
|
||||
/** How often the buzzer repeats while something is ringing. */
|
||||
const RING_REPEAT_MS = 3_000;
|
||||
/** Stop buzzing an item this long after it started ringing (it stays "rung"). */
|
||||
const RING_MAX_MS = 2 * 60_000;
|
||||
/**
|
||||
* Upper bound between deadline checks even with nothing due, so a JS timer
|
||||
* delayed by Doze or a clock jump can't leave a due item unnoticed for long.
|
||||
*/
|
||||
const MAX_ARM_MS = 30_000;
|
||||
/** How often the phone is reminded of the glasses' status (it distrusts stale reports). */
|
||||
const GLASSES_STATUS_HEARTBEAT_MS = 60_000;
|
||||
/** How often the phone-side reliability self-check reruns while something is armed. */
|
||||
const RELIABILITY_CHECK_MS = 5 * 60_000;
|
||||
|
||||
export const timersSoundSetting = new ConfigSettingBoolean({
|
||||
id: "timersSound",
|
||||
label: "Sound on glasses",
|
||||
storageKey: "timers.soundOnGlasses",
|
||||
defaultValue: true,
|
||||
description: "Play the glasses buzzer while a timer or alarm is ringing.",
|
||||
});
|
||||
|
||||
export const timersWakeSetting = new ConfigSettingBoolean({
|
||||
id: "timersWake",
|
||||
label: "Wake glasses when ringing",
|
||||
storageKey: "timers.wakeGlasses",
|
||||
defaultValue: true,
|
||||
description: "Turn the display on and show the Timers app when a timer or alarm goes off.",
|
||||
});
|
||||
|
||||
export type SnoozeMinutes = "5" | "10" | "15";
|
||||
export const timersSnoozeSetting = new ConfigSettingEnum<SnoozeMinutes>({
|
||||
id: "timersSnooze",
|
||||
label: "Snooze length",
|
||||
storageKey: "timers.snoozeMinutes",
|
||||
defaultValue: "10",
|
||||
values: ["5", "10", "15"],
|
||||
formatValue: (value) => `${value} min`,
|
||||
});
|
||||
|
||||
export type RingingItem = { kind: "timer"; timer: CountdownTimer } | { kind: "alarm"; alarm: Alarm };
|
||||
|
||||
export type TimerEngineHooks = {
|
||||
/** Play one buzzer sequencer payload on the glasses (no-op when disconnected). */
|
||||
playBuzzer: (payload: Uint8Array) => Promise<void> | void;
|
||||
/** Something started ringing audibly: bring the app forward, wake the glasses. */
|
||||
onRing: (item: RingingItem) => void;
|
||||
};
|
||||
|
||||
const noopHooks: TimerEngineHooks = { playBuzzer: () => {}, onRing: () => {} };
|
||||
|
||||
export class TimerEngine {
|
||||
state: TimersState;
|
||||
private hooks: TimerEngineHooks = noopHooks;
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private deadlineTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private ringTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private booted = false;
|
||||
private reliabilityIssues: AlarmReliabilityIssue[] = [];
|
||||
private reliabilityCheckedAtMs = 0;
|
||||
|
||||
constructor(private readonly now: () => number = () => Date.now()) {
|
||||
this.state = emptyTimersState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Install the side-effect hooks and reconcile persisted state against the
|
||||
* clock: anything that came due while the process was down rings (or is
|
||||
* marked rung silently past the grace window), and the Android alarms are
|
||||
* re-armed for what is still pending.
|
||||
*/
|
||||
boot(hooks: TimerEngineHooks): void {
|
||||
this.hooks = hooks;
|
||||
if (this.booted) return;
|
||||
this.booted = true;
|
||||
// Loaded here rather than at module evaluation, so the settings store is
|
||||
// certainly up before the first read.
|
||||
this.state = parseTimersState(getStringSetting(STATE_KEY, ""));
|
||||
// What the wearer did on the phone while this side was down comes first,
|
||||
// so a dismissed alarm is not rung again by the check below.
|
||||
for (const action of drainPhoneAlarmJournal()) this.applyPhoneAction(action);
|
||||
onPhoneAlarmAction((action) => {
|
||||
this.applyPhoneAction(action);
|
||||
this.commit();
|
||||
});
|
||||
this.pushGlassesStatus();
|
||||
onGlassesPresenceChanged(() => this.pushGlassesStatus());
|
||||
setInterval(() => this.pushGlassesStatus(), GLASSES_STATUS_HEARTBEAT_MS);
|
||||
this.check();
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.endsAtMs !== null && timer.rungAtMs === null) this.schedulePhoneNotification(timer);
|
||||
}
|
||||
for (const alarm of this.state.alarms) this.schedulePhoneAlarm(alarm);
|
||||
this.save();
|
||||
this.refreshReliability(true);
|
||||
setInterval(() => this.refreshReliability(false), RELIABILITY_CHECK_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* The glasses are showing a ringing item (the window is up and the screen
|
||||
* on). The phone's 30-second acknowledgement clock starts from here; without
|
||||
* this, the phone adds its sound within seconds.
|
||||
*/
|
||||
markDeliveredToGlasses(item: RingingItem): void {
|
||||
if (!glassesCanCarryAlert()) return;
|
||||
phoneAlarmDeliveredToGlasses(item.kind === "timer" ? item.timer.id : item.alarm.id);
|
||||
}
|
||||
|
||||
private pushGlassesStatus(): void {
|
||||
const presence = getGlassesPresence();
|
||||
setPhoneAlarmGlassesStatus(presence.connected, presence.worn === true, presence.charging);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replay a phone-side dismiss or snooze. The item may be ringing here too,
|
||||
* may not have been noticed yet (the phone rang it while this side slept),
|
||||
* or may already be gone; each case lands on the same end state.
|
||||
*/
|
||||
private applyPhoneAction(action: PhoneAlarmAction): void {
|
||||
const at = action.atMs || this.now();
|
||||
const timer = this.findTimer(action.id);
|
||||
if (timer) {
|
||||
if (action.action === "dismiss") {
|
||||
this.state.timers = this.state.timers.filter((candidate) => candidate.id !== timer.id);
|
||||
} else {
|
||||
const extra = Math.max(1, action.minutes) * 60_000;
|
||||
timer.rungAtMs = null;
|
||||
timer.pausedRemainingMs = null;
|
||||
timer.durationMs = extra;
|
||||
timer.endsAtMs = at + extra;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const alarm = this.findAlarm(action.id);
|
||||
if (!alarm) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.lastFiredAtMs = Math.max(alarm.lastFiredAtMs ?? 0, at);
|
||||
if (action.action === "dismiss") {
|
||||
alarm.snoozedUntilMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
} else {
|
||||
alarm.snoozedUntilMs = at + Math.max(1, action.minutes) * 60_000;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Phone reliability
|
||||
|
||||
/** Conditions under which the phone may fail to ring, most serious first (cached). */
|
||||
phoneReliabilityIssues(): AlarmReliabilityIssue[] {
|
||||
return this.reliabilityIssues;
|
||||
}
|
||||
|
||||
/**
|
||||
* One-line warning for the UI, or "" when nothing is armed that could
|
||||
* suffer (no enabled alarm, no running timer) or nothing is wrong.
|
||||
*/
|
||||
phoneReliabilityMessage(): string {
|
||||
if (this.reliabilityIssues.length === 0) return "";
|
||||
const armed =
|
||||
this.state.alarms.some((alarm) => alarm.enabled) ||
|
||||
this.state.timers.some((timer) => timer.endsAtMs !== null && timer.rungAtMs === null);
|
||||
if (!armed) return "";
|
||||
return `The phone may not ring for alarms: ${this.reliabilityIssues[0]!.message}`;
|
||||
}
|
||||
|
||||
/** Open the system screen for the most serious fixable issue, if any. */
|
||||
openPhoneReliabilityFix(): void {
|
||||
const issue = this.reliabilityIssues.find((candidate) => candidate.fixable);
|
||||
if (issue) openPhoneAlarmReliabilityFix(issue.code);
|
||||
}
|
||||
|
||||
/** Re-run the phone self-check (rate-limited unless forced) and notify on change. */
|
||||
refreshReliability(force: boolean): void {
|
||||
const now = this.now();
|
||||
if (!force && now - this.reliabilityCheckedAtMs < 30_000) return;
|
||||
this.reliabilityCheckedAtMs = now;
|
||||
let issues: AlarmReliabilityIssue[];
|
||||
try {
|
||||
issues = checkPhoneAlarmReliability();
|
||||
} catch (error) {
|
||||
console.warn(`alarm reliability check failed: ${error}`);
|
||||
return;
|
||||
}
|
||||
const changed =
|
||||
issues.length !== this.reliabilityIssues.length ||
|
||||
issues.some((issue, index) => issue.code !== this.reliabilityIssues[index]?.code);
|
||||
this.reliabilityIssues = issues;
|
||||
if (changed) this.notify();
|
||||
}
|
||||
|
||||
onChange(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
/** True while anything on screen changes by itself once a second. */
|
||||
isDynamic(): boolean {
|
||||
return (
|
||||
this.state.stopwatch.runningSinceMs !== null ||
|
||||
this.state.timers.some((timer) => timer.endsAtMs !== null && timer.rungAtMs === null) ||
|
||||
this.ringingItems().length > 0
|
||||
);
|
||||
}
|
||||
|
||||
ringingItems(): RingingItem[] {
|
||||
const items: RingingItem[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.rungAtMs !== null) items.push({ kind: "timer", timer });
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) items.push({ kind: "alarm", alarm });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Timers
|
||||
|
||||
findTimer(id: number): CountdownTimer | undefined {
|
||||
return this.state.timers.find((timer) => timer.id === id);
|
||||
}
|
||||
|
||||
startTimer(durationMs: number, label = ""): CountdownTimer {
|
||||
const timer = createTimer(durationMs, label, this.now());
|
||||
this.state.timers.push(timer);
|
||||
this.state.recentDurationsMs = pushRecentDuration(this.state.recentDurationsMs, durationMs);
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
return timer;
|
||||
}
|
||||
|
||||
pauseTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer || timer.endsAtMs === null || timer.rungAtMs !== null) return;
|
||||
timer.pausedRemainingMs = Math.max(0, timer.endsAtMs - this.now());
|
||||
timer.endsAtMs = null;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
resumeTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer || timer.pausedRemainingMs === null) return;
|
||||
timer.endsAtMs = this.now() + timer.pausedRemainingMs;
|
||||
timer.pausedRemainingMs = null;
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Add time to a timer; a rung timer becomes a running one with just the added time. */
|
||||
addTimerTime(id: number, deltaMs: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
if (timer.rungAtMs !== null) {
|
||||
timer.rungAtMs = null;
|
||||
timer.durationMs = deltaMs;
|
||||
timer.endsAtMs = this.now() + deltaMs;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.schedulePhoneNotification(timer);
|
||||
} else if (timer.pausedRemainingMs !== null) {
|
||||
timer.pausedRemainingMs += deltaMs;
|
||||
timer.durationMs += deltaMs;
|
||||
} else if (timer.endsAtMs !== null) {
|
||||
timer.endsAtMs += deltaMs;
|
||||
timer.durationMs += deltaMs;
|
||||
this.schedulePhoneNotification(timer);
|
||||
}
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Start the timer over at its set duration ("Again" on a rung timer). */
|
||||
restartTimer(id: number): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
timer.rungAtMs = null;
|
||||
timer.pausedRemainingMs = null;
|
||||
timer.endsAtMs = this.now() + timer.durationMs;
|
||||
cancelPhoneAlarm(timer.id);
|
||||
this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setTimerLabel(id: number, label: string): void {
|
||||
const timer = this.findTimer(id);
|
||||
if (!timer) return;
|
||||
timer.label = label.trim();
|
||||
if (timer.endsAtMs !== null && timer.rungAtMs === null) this.schedulePhoneNotification(timer);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Cancel a timer, or dismiss a rung one. */
|
||||
removeTimer(id: number): void {
|
||||
const index = this.state.timers.findIndex((timer) => timer.id === id);
|
||||
if (index < 0) return;
|
||||
acknowledgePhoneAlarm(id);
|
||||
this.state.timers.splice(index, 1);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Stopwatch
|
||||
|
||||
stopwatchToggle(): void {
|
||||
const stopwatch = this.state.stopwatch;
|
||||
const now = this.now();
|
||||
if (stopwatch.runningSinceMs === null) {
|
||||
stopwatch.runningSinceMs = now;
|
||||
} else {
|
||||
stopwatch.accumulatedMs += Math.max(0, now - stopwatch.runningSinceMs);
|
||||
stopwatch.runningSinceMs = null;
|
||||
}
|
||||
this.commit();
|
||||
}
|
||||
|
||||
stopwatchStart(): void {
|
||||
if (this.state.stopwatch.runningSinceMs === null) this.stopwatchToggle();
|
||||
}
|
||||
|
||||
stopwatchStop(): void {
|
||||
if (this.state.stopwatch.runningSinceMs !== null) this.stopwatchToggle();
|
||||
}
|
||||
|
||||
stopwatchLap(): void {
|
||||
const stopwatch = this.state.stopwatch;
|
||||
if (stopwatch.runningSinceMs === null) return;
|
||||
stopwatch.lapsMs.push(stopwatchElapsedMs(stopwatch, this.now()));
|
||||
this.commit();
|
||||
}
|
||||
|
||||
stopwatchReset(): void {
|
||||
this.state.stopwatch = { accumulatedMs: 0, runningSinceMs: null, lapsMs: [] };
|
||||
this.commit();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Alarms
|
||||
|
||||
findAlarm(id: number): Alarm | undefined {
|
||||
return this.state.alarms.find((alarm) => alarm.id === id);
|
||||
}
|
||||
|
||||
addAlarm(hour: number, minute: number, days: number, label = ""): Alarm {
|
||||
const alarm = createAlarm(hour, minute, days, label, this.now());
|
||||
this.state.alarms.push(alarm);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
return alarm;
|
||||
}
|
||||
|
||||
setAlarmTime(id: number, hour: number, minute: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.hour = hour;
|
||||
alarm.minute = minute;
|
||||
alarm.enabled = true;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.lastFiredAtMs = null;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmDays(id: number, days: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.days = days;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmEnabled(id: number, enabled: boolean): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.enabled = enabled;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.ringingSinceMs = null;
|
||||
if (enabled) alarm.lastFiredAtMs = null;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
setAlarmLabel(id: number, label: string): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm) return;
|
||||
alarm.label = label.trim();
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
deleteAlarm(id: number): void {
|
||||
const index = this.state.alarms.findIndex((alarm) => alarm.id === id);
|
||||
if (index < 0) return;
|
||||
cancelPhoneAlarm(id);
|
||||
this.state.alarms.splice(index, 1);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
snoozeAlarm(id: number, minutes = Number(timersSnoozeSetting.get())): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm || alarm.ringingSinceMs === null) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.snoozedUntilMs = this.now() + minutes * 60_000;
|
||||
acknowledgePhoneAlarm(alarm.id);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Stop a ringing alarm; a one-off alarm switches itself off. */
|
||||
dismissAlarm(id: number): void {
|
||||
const alarm = this.findAlarm(id);
|
||||
if (!alarm || alarm.ringingSinceMs === null) return;
|
||||
alarm.ringingSinceMs = null;
|
||||
alarm.snoozedUntilMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
acknowledgePhoneAlarm(alarm.id);
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/** Dismiss whatever is ringing (timers and alarms alike). Returns how many. */
|
||||
dismissAllRinging(): number {
|
||||
const items = this.ringingItems();
|
||||
for (const item of items) {
|
||||
if (item.kind === "timer") this.removeTimer(item.timer.id);
|
||||
else this.dismissAlarm(item.alarm.id);
|
||||
}
|
||||
return items.length;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Deadlines and ringing
|
||||
|
||||
/**
|
||||
* Fire anything due and re-arm for the next deadline. Called on every
|
||||
* change, on the deadline timer, and by the window before it paints, so a
|
||||
* timer never shows 0:00 without also being rung.
|
||||
*/
|
||||
check(): void {
|
||||
const now = this.now();
|
||||
let changed = false;
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null || timer.endsAtMs > now) continue;
|
||||
timer.rungAtMs = timer.endsAtMs;
|
||||
timer.endsAtMs = null;
|
||||
changed = true;
|
||||
// Late (found due after a restart): the phone rang or missed it on
|
||||
// its own schedule; only a fresh expiry starts the phone flow here.
|
||||
const late = now - timer.rungAtMs > RING_GRACE_MS;
|
||||
if (!late) {
|
||||
ringPhoneAlarm(timer.id, "Timer finished", timerNotificationText(timer), "timer", snoozeMinutes());
|
||||
this.startRinging({ kind: "timer", timer });
|
||||
}
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) continue;
|
||||
const due = this.alarmDueMs(alarm, now);
|
||||
if (due === null || due > now) continue;
|
||||
alarm.ringingSinceMs = now;
|
||||
alarm.snoozedUntilMs = null;
|
||||
alarm.lastFiredAtMs = due;
|
||||
changed = true;
|
||||
const late = now - due > RING_GRACE_MS;
|
||||
if (late) {
|
||||
// Missed by too much to ring now: fall through to the next occurrence.
|
||||
alarm.ringingSinceMs = null;
|
||||
if (alarm.days === 0) alarm.enabled = false;
|
||||
this.schedulePhoneAlarm(alarm);
|
||||
} else {
|
||||
ringPhoneAlarm(alarm.id, "Alarm", alarmNotificationText(alarm), "alarm", snoozeMinutes());
|
||||
this.startRinging({ kind: "alarm", alarm });
|
||||
}
|
||||
}
|
||||
this.arm(now);
|
||||
this.syncRingLoop(now);
|
||||
if (changed) this.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* The occurrence an alarm should ring for next: the pending snooze, or the
|
||||
* first occurrence after (now - grace) that it has not already rung for.
|
||||
* Inside the grace window this returns a time in the past, which check()
|
||||
* fires; beyond it the missed occurrence is skipped.
|
||||
*/
|
||||
private alarmDueMs(alarm: Alarm, now: number): number | null {
|
||||
return alarmNextRingMs(alarm, now - RING_GRACE_MS);
|
||||
}
|
||||
|
||||
private startRinging(item: RingingItem): void {
|
||||
try {
|
||||
this.hooks.onRing(item);
|
||||
} catch (error) {
|
||||
console.warn(`timers onRing hook failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-arm the single deadline timer for the soonest pending expiry. */
|
||||
private arm(now: number): void {
|
||||
if (this.deadlineTimer !== null) {
|
||||
clearTimeout(this.deadlineTimer);
|
||||
this.deadlineTimer = null;
|
||||
}
|
||||
const deadlines: number[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
if (timer.rungAtMs === null && timer.endsAtMs !== null) deadlines.push(timer.endsAtMs);
|
||||
}
|
||||
for (const alarm of this.state.alarms) {
|
||||
if (alarm.ringingSinceMs !== null) continue;
|
||||
const due = this.alarmDueMs(alarm, now);
|
||||
if (due !== null) deadlines.push(due);
|
||||
}
|
||||
if (deadlines.length === 0) return;
|
||||
const delay = Math.min(MAX_ARM_MS, Math.max(0, Math.min(...deadlines) - now));
|
||||
this.deadlineTimer = setTimeout(() => {
|
||||
this.deadlineTimer = null;
|
||||
this.check();
|
||||
}, delay);
|
||||
}
|
||||
|
||||
/** Run the buzzer repeat loop while anything is (still audibly) ringing. */
|
||||
private syncRingLoop(now: number): void {
|
||||
const audible = this.ringingItems().some((item) => this.ringAgeMs(item, now) < RING_MAX_MS);
|
||||
if (audible && this.ringTimer === null) {
|
||||
this.playRingSound();
|
||||
this.ringTimer = setInterval(() => {
|
||||
const at = this.now();
|
||||
if (!this.ringingItems().some((item) => this.ringAgeMs(item, at) < RING_MAX_MS)) {
|
||||
this.syncRingLoop(at);
|
||||
return;
|
||||
}
|
||||
this.playRingSound();
|
||||
}, RING_REPEAT_MS);
|
||||
} else if (!audible && this.ringTimer !== null) {
|
||||
clearInterval(this.ringTimer);
|
||||
this.ringTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private ringAgeMs(item: RingingItem, now: number): number {
|
||||
const since = item.kind === "timer" ? item.timer.rungAtMs : item.alarm.ringingSinceMs;
|
||||
return since === null ? Infinity : Math.max(0, now - since);
|
||||
}
|
||||
|
||||
private playRingSound(): void {
|
||||
if (!timersSoundSetting.get()) return;
|
||||
const effect = findSoundEffect("alarm");
|
||||
if (!effect) return;
|
||||
const phrase = effectPhrases(effect.make())[0];
|
||||
if (!phrase) return;
|
||||
try {
|
||||
void this.hooks.playBuzzer(buildSoundSequencePayload(phrase));
|
||||
} catch (error) {
|
||||
console.warn(`timers buzzer failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Phone notifications
|
||||
|
||||
private schedulePhoneNotification(timer: CountdownTimer): void {
|
||||
if (timer.endsAtMs === null) return;
|
||||
schedulePhoneAlarm(timer.id, timer.endsAtMs, "Timer finished", timerNotificationText(timer), "timer", snoozeMinutes());
|
||||
}
|
||||
|
||||
/** (Re)arm or cancel the phone alarm for an alarm's next occurrence. */
|
||||
private schedulePhoneAlarm(alarm: Alarm): void {
|
||||
const due = alarm.ringingSinceMs === null ? this.alarmDueMs(alarm, this.now()) : null;
|
||||
if (due === null) {
|
||||
cancelPhoneAlarm(alarm.id);
|
||||
return;
|
||||
}
|
||||
schedulePhoneAlarm(alarm.id, due, "Alarm", alarmNotificationText(alarm), "alarm", snoozeMinutes());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Change plumbing
|
||||
|
||||
private commit(): void {
|
||||
this.save();
|
||||
this.check();
|
||||
this.notify();
|
||||
if (this.booted) this.refreshReliability(false);
|
||||
}
|
||||
|
||||
private save(): void {
|
||||
try {
|
||||
setStringSetting(STATE_KEY, serializeTimersState(this.state));
|
||||
} catch (error) {
|
||||
console.warn(`timers state save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of Array.from(this.listeners)) {
|
||||
try {
|
||||
listener();
|
||||
} catch (error) {
|
||||
console.warn(`timers listener failed: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function snoozeMinutes(): number {
|
||||
return Number(timersSnoozeSetting.get()) || 10;
|
||||
}
|
||||
|
||||
function timerNotificationText(timer: CountdownTimer): string {
|
||||
const duration = formatDurationWords(timer.durationMs);
|
||||
return timer.label ? `${timer.label} (${duration}) is up` : `${duration} timer is up`;
|
||||
}
|
||||
|
||||
function alarmNotificationText(alarm: Alarm): string {
|
||||
const time = formatTimeOfDay(alarm.hour, alarm.minute, timeFormatSetting.get() === "12h");
|
||||
return alarm.label ? `${time} · ${alarm.label}` : time;
|
||||
}
|
||||
|
||||
/** Remaining time of the timer that ends soonest, for the tray icon; null when none runs. */
|
||||
export function soonestRunningTimerRemainingMs(engine: TimerEngine, now = Date.now()): number | null {
|
||||
let soonest: number | null = null;
|
||||
for (const timer of engine.state.timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
|
||||
const remaining = timerRemainingMs(timer, now);
|
||||
if (soonest === null || remaining < soonest) soonest = remaining;
|
||||
}
|
||||
return soonest;
|
||||
}
|
||||
|
||||
export const timerEngine = new TimerEngine();
|
||||
@@ -0,0 +1,460 @@
|
||||
/**
|
||||
* Pure data model for the Timers app: countdown timers, the stopwatch, and
|
||||
* alarms, plus the formatting and scheduling arithmetic they need. No
|
||||
* NativeScript imports, so tests/timer-model.test.cjs can run it under plain
|
||||
* node. Side effects (persistence, Android alarms, the buzzer, waking the
|
||||
* glasses) live in timer-engine.ts; drawing lives in timer-app.ts.
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
|
||||
/**
|
||||
* A countdown timer. Exactly one of the three phases holds at a time:
|
||||
* running (endsAtMs set), paused (pausedRemainingMs set), or rung
|
||||
* (rungAtMs set). A rung timer stays until dismissed so a missed one still
|
||||
* reads "Time's up".
|
||||
*/
|
||||
export type CountdownTimer = {
|
||||
/** Unique across restarts and safe as a Java long (see newItemId). */
|
||||
id: number;
|
||||
/** Wearer-given name; "" for an unlabelled timer (shown by duration). */
|
||||
label: string;
|
||||
durationMs: number;
|
||||
endsAtMs: number | null;
|
||||
pausedRemainingMs: number | null;
|
||||
rungAtMs: number | null;
|
||||
createdAtMs: number;
|
||||
};
|
||||
|
||||
export type StopwatchState = {
|
||||
/** Time accumulated over previous running stretches. */
|
||||
accumulatedMs: number;
|
||||
/** Start of the current running stretch, or null while stopped. */
|
||||
runningSinceMs: number | null;
|
||||
/** Cumulative elapsed time at each lap mark, oldest first. */
|
||||
lapsMs: number[];
|
||||
};
|
||||
|
||||
/** Day-of-week bits for Alarm.days, Monday first (Mon = bit 0 ... Sun = bit 6). */
|
||||
export const DAY_BITS = [1, 2, 4, 8, 16, 32, 64] as const;
|
||||
export const DAY_LETTERS = ["M", "T", "W", "T", "F", "S", "S"] as const;
|
||||
export const DAY_NAMES = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] as const;
|
||||
export const DAY_FULL_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] as const;
|
||||
export const WEEKDAYS_MASK = 1 | 2 | 4 | 8 | 16;
|
||||
export const WEEKEND_MASK = 32 | 64;
|
||||
export const EVERY_DAY_MASK = 127;
|
||||
|
||||
export type Alarm = {
|
||||
id: number;
|
||||
hour: number;
|
||||
minute: number;
|
||||
/** Repeat days as DAY_BITS; 0 means a one-off alarm (disabled after it rings). */
|
||||
days: number;
|
||||
label: string;
|
||||
enabled: boolean;
|
||||
/** Set while a snooze is pending; the alarm rings again at this time. */
|
||||
snoozedUntilMs: number | null;
|
||||
/** Set while ringing; cleared by dismiss or snooze. */
|
||||
ringingSinceMs: number | null;
|
||||
/**
|
||||
* For a repeating alarm: the occurrence most recently rung (or dismissed
|
||||
* from ringing), so the same occurrence does not ring twice after a
|
||||
* dismiss that lands before the minute is over.
|
||||
*/
|
||||
lastFiredAtMs: number | null;
|
||||
};
|
||||
|
||||
export type TimersState = {
|
||||
timers: CountdownTimer[];
|
||||
stopwatch: StopwatchState;
|
||||
alarms: Alarm[];
|
||||
/** Durations the wearer started most recently, newest first. */
|
||||
recentDurationsMs: number[];
|
||||
};
|
||||
|
||||
export function emptyTimersState(): TimersState {
|
||||
return {
|
||||
timers: [],
|
||||
stopwatch: { accumulatedMs: 0, runningSinceMs: null, lapsMs: [] },
|
||||
alarms: [],
|
||||
recentDurationsMs: [],
|
||||
};
|
||||
}
|
||||
|
||||
/** How many recent durations to keep for the "New timer" menu. */
|
||||
export const RECENT_DURATION_COUNT = 3;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Ids
|
||||
|
||||
let idSerial = 1;
|
||||
|
||||
/**
|
||||
* Millisecond epoch plus a small serial: unique across restarts, ordered by
|
||||
* creation, and still exactly representable through the JS -> Java long
|
||||
* bridge.
|
||||
*/
|
||||
export function newItemId(nowMs: number): number {
|
||||
return nowMs * 100 + (idSerial++ % 100);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Duration ladder (the "New timer" dial)
|
||||
|
||||
const SECOND = 1000;
|
||||
const MINUTE = 60 * SECOND;
|
||||
const HOUR = 60 * MINUTE;
|
||||
|
||||
/**
|
||||
* The durations the dial scrolls through: fine steps where timers are
|
||||
* usually short, coarser steps as they get long, so anything from ten
|
||||
* seconds to a day is a few dozen scroll clicks away at most.
|
||||
*/
|
||||
export const DURATION_LADDER_MS: readonly number[] = (() => {
|
||||
const ladder: number[] = [10 * SECOND, 15 * SECOND, 20 * SECOND, 30 * SECOND, 45 * SECOND, MINUTE, 90 * SECOND];
|
||||
for (let minutes = 2; minutes <= 20; minutes++) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 25; minutes <= 60; minutes += 5) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 75; minutes <= 180; minutes += 15) ladder.push(minutes * MINUTE);
|
||||
for (let minutes = 210; minutes <= 360; minutes += 30) ladder.push(minutes * MINUTE);
|
||||
for (let hours = 7; hours <= 12; hours++) ladder.push(hours * HOUR);
|
||||
ladder.push(18 * HOUR, 24 * HOUR);
|
||||
return ladder;
|
||||
})();
|
||||
|
||||
/** Index of the ladder rung closest to a duration (ties go to the shorter). */
|
||||
export function nearestLadderIndex(durationMs: number): number {
|
||||
let best = 0;
|
||||
let bestDistance = Infinity;
|
||||
for (let index = 0; index < DURATION_LADDER_MS.length; index++) {
|
||||
const distance = Math.abs(DURATION_LADDER_MS[index]! - durationMs);
|
||||
if (distance < bestDistance) {
|
||||
best = index;
|
||||
bestDistance = distance;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
export const DEFAULT_TIMER_DURATION_MS = 10 * MINUTE;
|
||||
export const MAX_TIMER_DURATION_MS = 24 * HOUR;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Timers
|
||||
|
||||
export function timerRemainingMs(timer: CountdownTimer, nowMs: number): number {
|
||||
if (timer.pausedRemainingMs !== null) return timer.pausedRemainingMs;
|
||||
if (timer.endsAtMs !== null) return Math.max(0, timer.endsAtMs - nowMs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
export type TimerPhase = "running" | "paused" | "rung";
|
||||
|
||||
export function timerPhase(timer: CountdownTimer): TimerPhase {
|
||||
if (timer.rungAtMs !== null) return "rung";
|
||||
if (timer.pausedRemainingMs !== null) return "paused";
|
||||
return "running";
|
||||
}
|
||||
|
||||
/** 0..1 progress toward the end (1 once rung). */
|
||||
export function timerProgress(timer: CountdownTimer, nowMs: number): number {
|
||||
if (timer.rungAtMs !== null || timer.durationMs <= 0) return 1;
|
||||
return Math.min(1, Math.max(0, 1 - timerRemainingMs(timer, nowMs) / timer.durationMs));
|
||||
}
|
||||
|
||||
export function createTimer(durationMs: number, label: string, nowMs: number): CountdownTimer {
|
||||
return {
|
||||
id: newItemId(nowMs),
|
||||
label,
|
||||
durationMs,
|
||||
endsAtMs: nowMs + durationMs,
|
||||
pausedRemainingMs: null,
|
||||
rungAtMs: null,
|
||||
createdAtMs: nowMs,
|
||||
};
|
||||
}
|
||||
|
||||
/** Running timers soonest first, then paused, then rung (oldest ring first). */
|
||||
export function sortTimers(timers: readonly CountdownTimer[], nowMs: number): CountdownTimer[] {
|
||||
const rank = (timer: CountdownTimer) => (timer.rungAtMs !== null ? 0 : timer.pausedRemainingMs !== null ? 2 : 1);
|
||||
return [...timers].sort((a, b) => {
|
||||
const rankDelta = rank(a) - rank(b);
|
||||
if (rankDelta !== 0) return rankDelta;
|
||||
if (a.rungAtMs !== null && b.rungAtMs !== null) return a.rungAtMs - b.rungAtMs;
|
||||
return timerRemainingMs(a, nowMs) - timerRemainingMs(b, nowMs) || a.createdAtMs - b.createdAtMs;
|
||||
});
|
||||
}
|
||||
|
||||
export function pushRecentDuration(recent: readonly number[], durationMs: number): number[] {
|
||||
return [durationMs, ...recent.filter((value) => value !== durationMs)].slice(0, RECENT_DURATION_COUNT);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stopwatch
|
||||
|
||||
export function stopwatchElapsedMs(stopwatch: StopwatchState, nowMs: number): number {
|
||||
const running = stopwatch.runningSinceMs === null ? 0 : Math.max(0, nowMs - stopwatch.runningSinceMs);
|
||||
return stopwatch.accumulatedMs + running;
|
||||
}
|
||||
|
||||
export function stopwatchIsRunning(stopwatch: StopwatchState): boolean {
|
||||
return stopwatch.runningSinceMs !== null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Alarms
|
||||
|
||||
export function createAlarm(hour: number, minute: number, days: number, label: string, nowMs: number): Alarm {
|
||||
return {
|
||||
id: newItemId(nowMs),
|
||||
hour,
|
||||
minute,
|
||||
days,
|
||||
label,
|
||||
enabled: true,
|
||||
snoozedUntilMs: null,
|
||||
ringingSinceMs: null,
|
||||
lastFiredAtMs: null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Monday-first weekday index (0..6) of a Date, from JS's Sunday-first getDay. */
|
||||
export function mondayFirstDay(date: Date): number {
|
||||
return (date.getDay() + 6) % 7;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the alarm next goes off, given the current time: the pending snooze
|
||||
* if there is one, otherwise the next wall-clock occurrence of hour:minute
|
||||
* on an enabled day (any day for a one-off), strictly after `nowMs` and
|
||||
* after the occurrence it last rang for. Null when the alarm is disabled.
|
||||
* Wall-clock arithmetic runs in the local zone via Date, so DST changes
|
||||
* land on the right local time.
|
||||
*/
|
||||
export function alarmNextRingMs(alarm: Alarm, nowMs: number): number | null {
|
||||
if (!alarm.enabled) return null;
|
||||
if (alarm.snoozedUntilMs !== null) return alarm.snoozedUntilMs;
|
||||
const floor = Math.max(nowMs, alarm.lastFiredAtMs ?? 0);
|
||||
const start = new Date(nowMs);
|
||||
for (let dayOffset = 0; dayOffset <= 7; dayOffset++) {
|
||||
const candidate = new Date(start.getFullYear(), start.getMonth(), start.getDate() + dayOffset, alarm.hour, alarm.minute, 0, 0);
|
||||
if (candidate.getTime() <= floor) continue;
|
||||
if (alarm.days !== 0 && !(alarm.days & DAY_BITS[mondayFirstDay(candidate)]!)) continue;
|
||||
return candidate.getTime();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Alarms by time of day, then by creation. */
|
||||
export function sortAlarms(alarms: readonly Alarm[]): Alarm[] {
|
||||
return [...alarms].sort((a, b) => a.hour * 60 + a.minute - (b.hour * 60 + b.minute) || a.id - b.id);
|
||||
}
|
||||
|
||||
export function formatDays(days: number): string {
|
||||
if (days === 0) return "Once";
|
||||
if (days === EVERY_DAY_MASK) return "Every day";
|
||||
if (days === WEEKDAYS_MASK) return "Weekdays";
|
||||
if (days === WEEKEND_MASK) return "Weekends";
|
||||
return DAY_NAMES.filter((_, index) => days & DAY_BITS[index]!).join(" ");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting
|
||||
|
||||
function pad2(value: number): string {
|
||||
return String(value).padStart(2, "0");
|
||||
}
|
||||
|
||||
/** Countdown / elapsed digits: m:ss below an hour, h:mm:ss above. */
|
||||
export function formatClockDigits(totalSeconds: number): string {
|
||||
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)}` : `${minutes}:${pad2(seconds)}`;
|
||||
}
|
||||
|
||||
/** Remaining time as a countdown reads it: rounds up, so 0:00 means done. */
|
||||
export function formatCountdown(remainingMs: number): string {
|
||||
return formatClockDigits(Math.max(0, Math.ceil(remainingMs / 1000)));
|
||||
}
|
||||
|
||||
/** Elapsed time as a stopwatch reads it: rounds down, plus optional tenths. */
|
||||
export function formatElapsed(elapsedMs: number, tenths: boolean): string {
|
||||
const clamped = Math.max(0, elapsedMs);
|
||||
const digits = formatClockDigits(Math.floor(clamped / 1000));
|
||||
return tenths ? `${digits}.${Math.floor(clamped / 100) % 10}` : digits;
|
||||
}
|
||||
|
||||
/** A set duration in words: "20 min", "1h 30m", "45s", "2m 30s". */
|
||||
export function formatDurationWords(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 minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
||||
if (minutes > 0) return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes} min`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
/** A span until something: "9h 12m", "12m", "45s". Coarse on purpose. */
|
||||
export function formatSpanWords(spanMs: number): string {
|
||||
const totalSeconds = Math.max(0, Math.round(spanMs / 1000));
|
||||
if (totalSeconds < 60) return `${totalSeconds}s`;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
if (hours === 0) return `${minutes}m`;
|
||||
if (hours < 24) return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
|
||||
const days = Math.floor(hours / 24);
|
||||
const restHours = hours % 24;
|
||||
return restHours > 0 ? `${days}d ${restHours}h` : `${days}d`;
|
||||
}
|
||||
|
||||
/** Wall-clock time of day in the wearer's chosen format. */
|
||||
export function formatTimeOfDay(hour24: number, minute: number, twelveHour: boolean): string {
|
||||
const minutes = pad2(minute);
|
||||
if (!twelveHour) return `${hour24}:${minutes}`;
|
||||
const hour12 = ((hour24 + 11) % 12) + 1;
|
||||
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
||||
}
|
||||
|
||||
export function formatClockAt(timestampMs: number, twelveHour: boolean): string {
|
||||
const date = new Date(timestampMs);
|
||||
return formatTimeOfDay(date.getHours(), date.getMinutes(), twelveHour);
|
||||
}
|
||||
|
||||
/** Row/stage label for a timer: its label, else its set duration. */
|
||||
export function timerDisplayName(timer: CountdownTimer): string {
|
||||
return timer.label || formatDurationWords(timer.durationMs);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Persistence shape
|
||||
|
||||
/**
|
||||
* Parse a persisted state blob defensively: anything malformed falls back to
|
||||
* a field default rather than failing the whole load, so a bad record loses
|
||||
* itself and nothing else.
|
||||
*/
|
||||
export function parseTimersState(raw: string | null | undefined): TimersState {
|
||||
const state = emptyTimersState();
|
||||
if (!raw) return state;
|
||||
let parsed: any;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
return state;
|
||||
}
|
||||
if (!parsed || typeof parsed !== "object") return state;
|
||||
if (Array.isArray(parsed.timers)) {
|
||||
for (const item of parsed.timers) {
|
||||
if (!item || typeof item !== "object" || !isFiniteNumber(item.id) || !isFiniteNumber(item.durationMs)) continue;
|
||||
state.timers.push({
|
||||
id: item.id,
|
||||
label: typeof item.label === "string" ? item.label : "",
|
||||
durationMs: item.durationMs,
|
||||
endsAtMs: isFiniteNumber(item.endsAtMs) ? item.endsAtMs : null,
|
||||
pausedRemainingMs: isFiniteNumber(item.pausedRemainingMs) ? item.pausedRemainingMs : null,
|
||||
rungAtMs: isFiniteNumber(item.rungAtMs) ? item.rungAtMs : null,
|
||||
createdAtMs: isFiniteNumber(item.createdAtMs) ? item.createdAtMs : 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
const stopwatch = parsed.stopwatch;
|
||||
if (stopwatch && typeof stopwatch === "object") {
|
||||
state.stopwatch = {
|
||||
accumulatedMs: isFiniteNumber(stopwatch.accumulatedMs) ? Math.max(0, stopwatch.accumulatedMs) : 0,
|
||||
runningSinceMs: isFiniteNumber(stopwatch.runningSinceMs) ? stopwatch.runningSinceMs : null,
|
||||
lapsMs: Array.isArray(stopwatch.lapsMs) ? stopwatch.lapsMs.filter(isFiniteNumber) : [],
|
||||
};
|
||||
}
|
||||
if (Array.isArray(parsed.alarms)) {
|
||||
for (const item of parsed.alarms) {
|
||||
if (!item || typeof item !== "object" || !isFiniteNumber(item.id)) continue;
|
||||
if (!isFiniteNumber(item.hour) || !isFiniteNumber(item.minute)) continue;
|
||||
state.alarms.push({
|
||||
id: item.id,
|
||||
hour: Math.min(23, Math.max(0, Math.floor(item.hour))),
|
||||
minute: Math.min(59, Math.max(0, Math.floor(item.minute))),
|
||||
days: isFiniteNumber(item.days) ? item.days & EVERY_DAY_MASK : 0,
|
||||
label: typeof item.label === "string" ? item.label : "",
|
||||
enabled: item.enabled !== false,
|
||||
snoozedUntilMs: isFiniteNumber(item.snoozedUntilMs) ? item.snoozedUntilMs : null,
|
||||
ringingSinceMs: isFiniteNumber(item.ringingSinceMs) ? item.ringingSinceMs : null,
|
||||
lastFiredAtMs: isFiniteNumber(item.lastFiredAtMs) ? item.lastFiredAtMs : null,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (Array.isArray(parsed.recentDurationsMs)) {
|
||||
state.recentDurationsMs = parsed.recentDurationsMs.filter(isFiniteNumber).slice(0, RECENT_DURATION_COUNT);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
export function serializeTimersState(state: TimersState): string {
|
||||
return JSON.stringify(state);
|
||||
}
|
||||
|
||||
function isFiniteNumber(value: unknown): value is number {
|
||||
return typeof value === "number" && Number.isFinite(value);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tool-argument parsing (shared by the assistant tools)
|
||||
|
||||
/**
|
||||
* Parse a time of day from the forms an assistant is likely to pass:
|
||||
* "7:30", "07:30", "7:30 am", "7pm", "19:30", "noon", "midnight".
|
||||
* Returns null when unparseable.
|
||||
*/
|
||||
export function parseTimeOfDay(text: string): { hour: number; minute: number } | null {
|
||||
const trimmed = text.trim().toLowerCase();
|
||||
if (trimmed === "noon") return { hour: 12, minute: 0 };
|
||||
if (trimmed === "midnight") return { hour: 0, minute: 0 };
|
||||
const match = /^(\d{1,2})(?::(\d{2}))?\s*(a\.?m\.?|p\.?m\.?)?$/.exec(trimmed);
|
||||
if (!match) return null;
|
||||
let hour = parseInt(match[1]!, 10);
|
||||
const minute = match[2] ? parseInt(match[2], 10) : 0;
|
||||
const meridiem = match[3]?.replace(/\./g, "");
|
||||
if (minute > 59) return null;
|
||||
if (meridiem) {
|
||||
if (hour < 1 || hour > 12) return null;
|
||||
hour = hour % 12;
|
||||
if (meridiem === "pm") hour += 12;
|
||||
} else if (hour > 23) {
|
||||
return null;
|
||||
}
|
||||
return { hour, minute };
|
||||
}
|
||||
|
||||
/** Parse repeat days from names ("mon", "tuesday"), "weekdays", "weekends", "daily"/"every day", "once". */
|
||||
export function parseRepeatDays(values: readonly string[] | string | null | undefined): number | null {
|
||||
if (values === null || values === undefined) return 0;
|
||||
const list =
|
||||
typeof values === "string"
|
||||
? values.toLowerCase().replace(/every\s*day/g, "daily").split(/[,\s]+/)
|
||||
: values.map((value) => String(value).toLowerCase().replace(/every\s*day/g, "daily"));
|
||||
let mask = 0;
|
||||
for (const raw of list) {
|
||||
const value = String(raw).trim().toLowerCase();
|
||||
if (!value || value === "once" || value === "none") continue;
|
||||
if (value === "weekdays") {
|
||||
mask |= WEEKDAYS_MASK;
|
||||
continue;
|
||||
}
|
||||
if (value === "weekends" || value === "weekend") {
|
||||
mask |= WEEKEND_MASK;
|
||||
continue;
|
||||
}
|
||||
if (value === "daily" || value === "every day" || value === "everyday" || value === "all") {
|
||||
mask |= EVERY_DAY_MASK;
|
||||
continue;
|
||||
}
|
||||
const index = DAY_FULL_NAMES.findIndex((name) => name.toLowerCase().startsWith(value.slice(0, 3)));
|
||||
if (index < 0 || value.length < 2) return null;
|
||||
mask |= DAY_BITS[index]!;
|
||||
}
|
||||
return mask;
|
||||
}
|
||||
@@ -57,10 +57,18 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
menuItems: () => [{
|
||||
label: "Save",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.saveTranscript();
|
||||
},
|
||||
}],
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
layer.onRemoved();
|
||||
options.stopContinuousVoiceCapture();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
options.onClosed();
|
||||
|
||||
@@ -1,33 +1,35 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { writeTextToDownloads } from "../../native/file-access";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { TranscriptModel, TranscriptScroll, wrapTranscribeText } from "./transcript-model";
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* it, and saves it to Downloads from the app menu.
|
||||
*/
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } 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 readonly transcript = new TranscriptModel();
|
||||
private readonly scroll = new TranscriptScroll();
|
||||
private saveNotice = "";
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribePause: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||
this.onTranscript(event);
|
||||
this.transcript.accept(event);
|
||||
requestRender();
|
||||
});
|
||||
this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause());
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.saveNotice = "";
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
@@ -36,29 +38,34 @@ export class TranscribeLayer implements Layer {
|
||||
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);
|
||||
const text = this.transcript.text || "Listening...";
|
||||
const wrapped = wrapTranscribeText((text) => font.measureText(text), text, width - 64);
|
||||
|
||||
image.drawText(font, 24, 20, "Transcribe", 200);
|
||||
image.drawText(font, 24, 40, this.saveNotice || this.status, 110);
|
||||
|
||||
const step = lineStep(font) + 2;
|
||||
const bodyTop = 62;
|
||||
const footerY = height - font.lineHeight - 6;
|
||||
const bodyLines = Math.max(1, Math.floor((footerY - bodyTop) / step));
|
||||
const firstLine = Math.max(0, wrapped.length - bodyLines);
|
||||
for (let index = firstLine; index < wrapped.length; index++) {
|
||||
const bodyLines = Math.max(1, Math.floor((height - 6 - bodyTop - font.lineHeight) / step) + 1);
|
||||
const firstLine = this.scroll.layout(wrapped.length, bodyLines);
|
||||
for (let index = firstLine; index < Math.min(wrapped.length, firstLine + bodyLines); index++) {
|
||||
const y = bodyTop + (index - firstLine) * step;
|
||||
image.drawText(font, 32, y, wrapped[index]!, 230);
|
||||
}
|
||||
|
||||
image.drawText(font, 24, footerY, `${GESTURE_CLICK} save ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
if (wrapped.length > bodyLines) {
|
||||
const trackHeight = height - bodyTop - 6;
|
||||
const thumbHeight = Math.max(8, Math.round(trackHeight * bodyLines / wrapped.length));
|
||||
const thumbY = bodyTop + Math.round((trackHeight - thumbHeight) * firstLine / (wrapped.length - bodyLines));
|
||||
image.fillRect(width - 14, bodyTop, 2, trackHeight, 45);
|
||||
image.fillRect(width - 14, thumbY, 2, thumbHeight, 140);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "click") {
|
||||
this.saveTranscript();
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
this.scroll.scroll(event.type === "scroll-up" ? -3 : 3);
|
||||
ctx.actions.requestRender();
|
||||
return;
|
||||
}
|
||||
@@ -68,14 +75,16 @@ export class TranscribeLayer implements Layer {
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.unsubscribePause?.();
|
||||
this.unsubscribePause = null;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
}
|
||||
|
||||
private saveTranscript(): void {
|
||||
const text = this.displayText().trim();
|
||||
saveTranscript(): void {
|
||||
const text = this.transcript.text.trim();
|
||||
if (!text) {
|
||||
this.saveNotice = "Nothing to save yet.";
|
||||
return;
|
||||
@@ -84,25 +93,6 @@ export class TranscribeLayer implements Layer {
|
||||
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 {
|
||||
@@ -113,22 +103,3 @@ function transcriptTimestamp(): string {
|
||||
`-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
|
||||
);
|
||||
}
|
||||
|
||||
function wrapTranscribeText(font: UiFont, 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 : [""];
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
type TranscriptEvent = {
|
||||
text: string;
|
||||
isFinal: boolean;
|
||||
transcribeText?: string;
|
||||
paragraphBreakAfter?: boolean;
|
||||
};
|
||||
|
||||
/** Text accumulation belongs to this app, not the shared dictation stream. */
|
||||
export class TranscriptModel {
|
||||
private finalized = "";
|
||||
private live = "";
|
||||
private separator = " ";
|
||||
private breakAfterLive = false;
|
||||
|
||||
get text(): string {
|
||||
return this.finalized && this.live ? this.finalized + this.separator + this.live : this.finalized || this.live;
|
||||
}
|
||||
|
||||
accept(event: TranscriptEvent): void {
|
||||
const text = (event.transcribeText ?? event.text).trim();
|
||||
if (!event.isFinal) {
|
||||
this.live = text;
|
||||
return;
|
||||
}
|
||||
const finalText = text || this.live;
|
||||
if (finalText) this.finalized = this.finalized ? this.finalized + this.separator + finalText : finalText;
|
||||
this.live = "";
|
||||
if (finalText) this.separator = event.paragraphBreakAfter || this.breakAfterLive ? "\n" : " ";
|
||||
this.breakAfterLive = false;
|
||||
}
|
||||
|
||||
/** The onboard recognizer may finalize just before or just after the pause. */
|
||||
pause(): void {
|
||||
if (this.live) this.breakAfterLive = true;
|
||||
else if (this.finalized) this.separator = "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/** An absolute history position, with an explicit follow-tail mode. */
|
||||
export class TranscriptScroll {
|
||||
firstLine = 0;
|
||||
private maximum = 0;
|
||||
private following = true;
|
||||
|
||||
layout(totalLines: number, visibleLines: number): number {
|
||||
this.maximum = Math.max(0, totalLines - visibleLines);
|
||||
this.firstLine = this.following ? this.maximum : Math.min(this.firstLine, this.maximum);
|
||||
return this.firstLine;
|
||||
}
|
||||
|
||||
scroll(delta: number): void {
|
||||
this.firstLine = Math.max(0, Math.min(this.maximum, this.firstLine + delta));
|
||||
this.following = this.firstLine === this.maximum;
|
||||
}
|
||||
}
|
||||
|
||||
export function wrapTranscribeText(measure: (text: string) => number, text: string, maxWidth: number): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const paragraph of text.split("\n")) {
|
||||
let line = "";
|
||||
for (const word of paragraph.split(/\s+/).filter(Boolean)) {
|
||||
const candidate = line ? `${line} ${word}` : word;
|
||||
if (line && measure(candidate) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = "";
|
||||
}
|
||||
if (measure(word) <= maxWidth) {
|
||||
line = line ? `${line} ${word}` : word;
|
||||
continue;
|
||||
}
|
||||
// Long URLs and unspaced languages must not run off the display.
|
||||
for (const char of (line ? " " : "") + word) {
|
||||
if (line && measure(line + char) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = "";
|
||||
}
|
||||
line += char;
|
||||
}
|
||||
}
|
||||
lines.push(line);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
Reference in new issue
Block a user