Merge branch 'main' into ble-bandwidth-test

This commit is contained in:
James Babcock committed 2026-09-09 21:59:38 -07:00
commit 50cbc3a756
198 files changed
+15988 -2899

No files matched your search

+36 -7
View File
@@ -98,6 +98,13 @@ Button.-primary {
font-size: 13;
}
/* Developer BLE-bandwidth totals overlaid on the main page's bottom edge. */
.ble-bandwidth-indicator {
color: #777777;
font-size: 12;
padding: 2 8;
}
/*
* Stands in for the lens preview when showing it would be misleading (silent
* mode, or a disconnect explained by a flat battery). Sized by the view model
@@ -175,11 +182,12 @@ Button.-primary {
background-color: #424242;
color: #ffffff;
}
/* The wakeword button at the tab bar's right edge: icon-only, narrower than
* the tabs, and never in the selected state (it isn't a tab). It is a
* button-styled GridLayout (Buttons can't hold images), so it borrows the
* .tab-button look and adds the elevation MaterialButtons get for free. */
.tab-mic-button {
/* The wakeword and keyboard buttons at the tab bar's right edge: icon-only,
* narrower than the tabs, and never in the selected state (they aren't tabs).
* Each is a button-styled GridLayout (Buttons can't hold images), so it
* borrows the .tab-button look and adds the elevation MaterialButtons get for
* free. */
.tab-icon-button {
width: 56;
/* The real MaterialButtons render 48dp tall inside a 60dp layout box (6dp
* insets top and bottom); the margins and explicit height reproduce that.
@@ -189,11 +197,32 @@ Button.-primary {
margin: 6 2;
android-elevation: 2;
}
.tab-mic-icon {
.tab-icon {
height: 20;
tint-color: #333333;
}
.ns-dark .tab-mic-icon {
/* Landscape uses the full page height and a shared left edge for tabs/pad. */
.landscape-layout .remote-controls {
margin: 0;
}
.landscape-layout .controls-tab-bar {
margin-bottom: 2;
}
.landscape-layout .tab-button {
margin-left: 1;
margin-right: 1;
min-width: 0;
}
.landscape-layout .tab-button[col="0"] {
margin-left: 0;
}
.landscape-layout .tab-icon-button {
width: 48;
}
.landscape-layout .tab-icon-button[col="4"] {
margin-right: 0;
}
.ns-dark .tab-icon {
tint-color: #cccccc;
}
.ns-dark .tab-button {
+2
View File
@@ -7,9 +7,11 @@ purpose of the file is to pass control to the app’s first module.
import { Application } from '@nativescript/core'
import { registerShareIntentHandler } from './native/share-intents'
import { installNativeUserAgent } from './util/http'
import { registerPhoneRotation } from './native/phone-rotation'
installNativeUserAgent()
registerShareIntentHandler()
registerPhoneRotation()
Application.run({ moduleName: 'app-root' })
+166
View File
@@ -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;
}
+10
View File
@@ -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;
+125
View File
@@ -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();
}
}
+4
View File
@@ -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,
+23 -15
View File
@@ -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 -1
View File
@@ -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.
*
+31 -17
View File
@@ -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
View File
@@ -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,
);
}
+21
View File
@@ -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}`];
}
+97
View File
@@ -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;
});
}
+54
View File
@@ -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,
};
}
+7
View File
@@ -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) => {
+199
View File
@@ -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 };
}
}
}
+36 -5
View File
@@ -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) {
+15 -1
View File
@@ -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),
);
}
+79 -1
View File
@@ -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). */
+52 -11
View File
@@ -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 -6
View File
@@ -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
View File
@@ -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);
},
};
+104 -37
View File
@@ -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 {
+68 -8
View File
@@ -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 {
+34 -69
View File
@@ -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();
}
+97
View File
@@ -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
View File
@@ -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 {
+100
View File
@@ -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();
}
+5 -4
View File
@@ -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);
+28 -23
View File
@@ -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 {
+1 -1
View File
@@ -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.
*/
+4
View File
@@ -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;
+22 -14
View File
@@ -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
View File
@@ -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,
+57
View File
@@ -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);
}
}
+167
View File
@@ -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 [];
}
}
+671 -56
View File
@@ -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 {
+2 -1
View File
@@ -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),
};
+33
View File
@@ -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";
}
+45 -18
View File
@@ -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;
}
+40 -5
View File
@@ -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,
+23 -15
View File
@@ -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 {
+1 -1
View File
@@ -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);
+16 -10
View File
@@ -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) {
+19
View File
@@ -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;
+300
View File
@@ -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;
}
}
+80
View File
@@ -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));
}
+260
View File
@@ -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;
}
}
+188
View File
@@ -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;
}
+75 -38
View File
@@ -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 {
+4
View File
@@ -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
View File
@@ -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
-745
View File
@@ -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`;
}
+692
View File
@@ -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();
+460
View File
@@ -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;
}
+8
View File
@@ -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();
+29 -58
View File
@@ -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 : [""];
}
+83
View File
@@ -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;
}
+95
View File
@@ -0,0 +1,95 @@
import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session";
import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models";
export type ReasoningLevel = "default" | "low" | "medium" | "high";
export type Conversation = {
id: string;
model: AssistantModel;
reasoning: ReasoningLevel;
session: AssistantSession | null;
history?: AssistantSessionHistory;
};
type SavedConversation = Omit<Conversation, "session">;
type SavedConversations = { selectedId: string; conversations: SavedConversation[] };
/** Shared selection and history for every assistant entry point. No credentials are saved. */
export class AssistantConversations {
private records: Conversation[] = [];
private selectedId = "";
private readonly listeners = new Set<() => void>();
constructor(
private readonly resolve: (model: AssistantModel, reasoning: ReasoningLevel) => AssistantBackendConfig | null,
private readonly defaultModel: () => AssistantModel,
private readonly persist: (value: string) => void,
saved = "",
) {
try {
const data: SavedConversations = JSON.parse(saved);
if (Array.isArray(data.conversations)) {
this.records = data.conversations.filter((record) =>
typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) &&
["default", "low", "medium", "high"].includes(record.reasoning) &&
Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript),
).map((record) => ({ ...record, session: null }));
this.selectedId = data.selectedId;
}
} catch { /* First launch or invalid saved state. */ }
if (!this.records.length) this.records.push(this.makeConversation());
if (!this.records.some((record) => record.id === this.selectedId)) this.selectedId = this.records[0]!.id;
}
current(): Conversation { return this.records.find((record) => record.id === this.selectedId)!; }
list(): readonly Conversation[] { return this.records; }
title(record = this.current()): string {
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
return transcript.find((entry) => entry.role === "user")?.text.replace(/\s+/g, " ").slice(0, 60) || "New session";
}
onChanged(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
private changed(save = true): void {
if (save) this.persist(JSON.stringify({ selectedId: this.selectedId, conversations: this.records.map(
({ session, ...record }) => ({ ...record, history: session?.history() ?? record.history ?? { messages: [], transcript: [] } }),
) }));
for (const listener of this.listeners) listener();
}
private makeConversation(): Conversation {
return { id: `${Date.now()}-${Math.random().toString(36).slice(2)}`, model: this.defaultModel(), reasoning: "default", session: null };
}
create(): boolean {
if (this.current().session?.isTurnActive()) return false;
const record = this.makeConversation();
this.records.push(record);
this.selectedId = record.id;
this.changed();
return true;
}
select(id: string): boolean {
if (this.current().session?.isTurnActive() || !this.records.some((record) => record.id === id)) return false;
this.selectedId = id;
this.changed();
return true;
}
configure(model: AssistantModel, reasoning: ReasoningLevel): boolean {
const record = this.current();
if (record.session?.isTurnActive() || !this.resolve(model, reasoning)) return false;
record.model = model;
record.reasoning = reasoning;
this.ensureSession();
this.changed();
return true;
}
available(model: AssistantModel, reasoning: ReasoningLevel): boolean { return this.resolve(model, reasoning) !== null; }
ensureSession(): AssistantSession | null {
const record = this.current();
const config = this.resolve(record.model, record.reasoning);
if (!config) return null;
if (!record.session) {
record.session = new AssistantSession(config, undefined, record.history);
record.session.onChanged(() => this.changed(!record.session!.isTurnActive()));
} else record.session.configure(config);
return record.session;
}
}
+5 -1
View File
@@ -50,6 +50,7 @@ export class DirectAssistantBackend {
const startMs = Date.now();
let cancelled = false;
let streamHandle: LlmStreamHandle | null = null;
let priorText = "";
const finishError = (message: string) => {
if (cancelled) return;
@@ -78,7 +79,7 @@ export class DirectAssistantBackend {
tools: options.buildTools(),
onTextDelta: (delta, textSoFar) => {
if (cancelled) return;
options.callbacks.onTextDelta(delta, textSoFar);
options.callbacks.onTextDelta(delta, priorText + textSoFar);
},
onDone: (result) => {
streamHandle = null;
@@ -87,6 +88,9 @@ export class DirectAssistantBackend {
finishError("The assistant declined this request");
return;
}
const fullText = priorText + result.text;
options.callbacks.onTextDelta("", fullText);
priorText = fullText ? `${fullText}\n\n` : "";
// Record what the model produced this iteration.
const assistantContent: LlmContentBlock[] = result.content.length
? result.content
+1 -1
View File
@@ -23,7 +23,7 @@ export function registerNavigateTools(
{
name: "nav.start_navigation",
description:
"Start turn-by-turn navigation on the glasses to a destination given as free text (place name, business, or address near the user). Opens the Navigate app. Returns the resolved destination, distance, and ETA; if the wrong place was resolved, call again with a more specific query.",
"Start turn-by-turn navigation on the glasses to a destination given as free text (place name, business, or address near the user), or the name of one of the user's saved destinations such as 'home' or 'work'. Opens the Navigate app. Returns the resolved destination, distance, and ETA; if the wrong place was resolved, call again with a more specific query.",
inputSchema: {
type: "object",
properties: {
+1 -1
View File
@@ -57,7 +57,7 @@ async function callAppTool(
args: unknown,
): Promise<ToolResult> {
if (roamGraphNameSetting.get().length === 0 || roamApiTokenSetting.get().length === 0) {
return err("Roam is not configured; the user must set the graph name and API token from the Roam app's long-press menu.");
return err("Roam is not configured; the user must set the graph name and API token from the Roam app's context menu (tap, then hold).");
}
return callAppToolWithLaunch(registry, launchApp, "roam", unprefixedName, args);
}
+113 -47
View File
@@ -15,26 +15,20 @@ import type {
AssistantTurnHandle,
} from "./types";
/**
* One assistant conversation. Owns the message history and turn state and
* drives either the on-phone provider loop (direct) or the external agent
* bridge. The UI talks only to this surface, so AssistantLayer is
* backend-agnostic.
*
* Sessions are cheap and idle-expire (see isExpired): the shell starts a fresh
* one for a new wakeword and reuses the current one for a follow-up. In
* external mode the conversation history lives on the agent's machine (one
* long-lived session there), so expiry here only affects the overlay UI.
*/
/** One shared conversation, independent of the voice overlay or chat window. */
export type AssistantTranscriptEntry = { role: "user" | "assistant"; text: string };
export type AssistantSessionHistory = {
messages: LlmMessage[];
transcript: AssistantTranscriptEntry[];
/** Model identity for safe replay after Auto resolves differently on restart. */
engine?: string;
};
/** Which engine answers utterances, plus what it needs to do so. */
export type AssistantBackendConfig =
| { kind: "direct"; llm: ResolvedAssistantModel }
| { kind: "external"; bridge: AssistantBridgeConfig };
/** A new PTT after this much idle starts a fresh conversation. */
const SESSION_IDLE_MS = 10 * 60 * 1000;
/** Trim history from the head once it grows past this many messages. */
const MAX_HISTORY_MESSAGES = 40;
@@ -42,38 +36,61 @@ export class AssistantSession {
private readonly directBackend = new DirectAssistantBackend();
private readonly messages: LlmMessage[] = [];
private turnHandle: AssistantTurnHandle | null = null;
private lastActivityMs = Date.now();
readonly transcript: AssistantTranscriptEntry[] = [];
status = "";
private readonly listeners = new Set<() => void>();
private turnGeneration = 0;
// API-safe tool name -> canonical registry name. Provider APIs restrict tool
// names, so dotted registry names are sanitized and mapped back on calls.
private readonly toolNameMap = new Map<string, string>();
constructor(
private readonly config: AssistantBackendConfig,
private config: AssistantBackendConfig,
private readonly registry: ToolRegistry = toolRegistry,
) {}
matchesConfiguration(config: AssistantBackendConfig): boolean {
if (this.config.kind !== config.kind) return false;
if (this.config.kind === "direct" && config.kind === "direct") {
return (
this.config.llm.provider === config.llm.provider &&
this.config.llm.model === config.llm.model &&
this.config.llm.apiKey === config.llm.apiKey
);
history?: AssistantSessionHistory,
) {
if (history) {
this.messages.push(...history.messages);
this.transcript.push(...history.transcript);
if (history.engine !== this.engine()) this.rebuildTextHistory();
}
if (this.config.kind === "external" && config.kind === "external") {
return (
this.config.bridge.host === config.bridge.host &&
this.config.bridge.port === config.bridge.port &&
this.config.bridge.token === config.bridge.token
);
}
return false;
}
/** Whether this session has been idle long enough to retire. */
isExpired(nowMs = Date.now()): boolean {
return nowMs - this.lastActivityMs > SESSION_IDLE_MS;
onChanged(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
private changed(): void {
for (const listener of this.listeners) {
try { listener(); } catch (error) { console.warn("assistant history listener failed", error); }
}
}
history(): AssistantSessionHistory {
return { messages: this.messages, transcript: this.transcript, engine: this.engine() };
}
configure(config: AssistantBackendConfig): void {
if (this.isTurnActive()) return;
// Opaque reasoning/tool provider items are only replayable on their
// original model. Preserve visible conversation when switching engines.
if (this.config.kind !== config.kind ||
(this.config.kind === "direct" && config.kind === "direct" &&
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider))) {
this.rebuildTextHistory();
}
this.config = config;
}
private engine(): string {
return this.config.kind === "direct" ? `${this.config.llm.provider}:${this.config.llm.model}` : "external";
}
private rebuildTextHistory(): void {
this.messages.splice(0, this.messages.length, ...this.transcript
.filter((entry) => entry.text.trim())
.map((entry) => ({ role: entry.role, content: entry.text })));
}
isTurnActive(): boolean {
@@ -86,20 +103,44 @@ export class AssistantSession {
callbacks.onError("The assistant is still working on the previous request");
return;
}
this.lastActivityMs = Date.now();
text = text.trim();
if (!text) return;
const generation = ++this.turnGeneration;
const reply: AssistantTranscriptEntry = { role: "assistant", text: "" };
this.transcript.push({ role: "user", text }, reply);
this.status = "Thinking...";
// Install a sentinel before calling providers: some failures are synchronous.
this.turnHandle = { cancel: () => {} };
this.changed();
const finish = () => {
++this.turnGeneration;
this.turnHandle = null;
this.lastActivityMs = Date.now();
this.changed();
};
const wrappedCallbacks: AssistantTurnCallbacks = {
onTextDelta: callbacks.onTextDelta,
onToolActivity: callbacks.onToolActivity,
onTextDelta: (delta, full) => {
if (generation !== this.turnGeneration) return;
reply.text = full;
this.status = "Thinking...";
this.changed();
callbacks.onTextDelta(delta, full);
},
onToolActivity: (label) => {
if (generation !== this.turnGeneration) return;
this.status = `→ ${label}`;
this.changed();
callbacks.onToolActivity(label);
},
onTurnDone: (result) => {
if (generation !== this.turnGeneration) return;
this.status = "";
finish();
callbacks.onTurnDone(result);
},
onError: (message) => {
if (generation !== this.turnGeneration) return;
this.status = message;
finish();
callbacks.onError(message);
},
@@ -108,7 +149,8 @@ export class AssistantSession {
if (this.config.kind === "external") {
// History and the agent loop live on the agent's machine; the phone just
// streams this turn. The overlay keeps its own display state.
this.turnHandle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
const handle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
return;
}
@@ -123,24 +165,48 @@ export class AssistantSession {
content: isLocal ? `${text}\n\n(${describeAssistantContext(ctx)})` : text,
});
this.trimHistory();
this.turnHandle = this.directBackend.runTurn({
// Work on a turn-local array: cancelled tools may finish later, and must
// never append orphan results into a subsequent turn's context.
const turnMessages = this.messages.slice();
const handle = this.directBackend.runTurn({
provider: llm.provider,
apiKey: llm.apiKey,
model: llm.model,
effort: llm.effort,
system: isLocal ? ASSISTANT_SYSTEM_PROMPT_BASE : buildAssistantSystemPrompt(ctx),
messages: this.messages,
messages: turnMessages,
buildTools: () => this.buildToolDefinitions(),
registry: this.registry,
resolveToolName: (apiName) => this.toolNameMap.get(apiName) ?? apiName,
callbacks: wrappedCallbacks,
callbacks: {
...wrappedCallbacks,
onTurnDone: (result) => {
if (generation !== this.turnGeneration) return;
this.messages.splice(0, this.messages.length, ...turnMessages);
wrappedCallbacks.onTurnDone(result);
},
onError: (message) => {
if (generation !== this.turnGeneration) return;
// Retain the visible partial answer, without incomplete tool pairs.
if (reply.text) this.messages.push({ role: "assistant", content: reply.text });
wrappedCallbacks.onError(message);
},
},
});
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
}
cancel(): void {
this.turnHandle?.cancel();
if (!this.turnHandle) return;
++this.turnGeneration;
this.turnHandle.cancel();
this.turnHandle = null;
this.lastActivityMs = Date.now();
const reply = this.transcript[this.transcript.length - 1];
if (reply?.role === "assistant" && reply.text) {
this.messages.push({ role: "assistant", content: reply.text });
}
this.status = "Cancelled";
this.changed();
}
private buildToolDefinitions(): LlmToolDefinition[] {
+383 -26
View File
@@ -1,76 +1,433 @@
/**
* Shell-side timer.* assistant tools. The Timer app's own tools are
* window-scoped ("open" tier), but "start a 5 minute timer" must work with no
* Timer window open — so these always-available wrappers launch the app,
* wait for its tools to register, and forward the call. Timer state lives in
* the app's worker, which outlives its window, so list/cancel also route
* through the app rather than answering "no timers" from the shell.
* The assistant's timer.* / stopwatch.* / alarm.* tools. They talk to the
* Timers app's engine directly (it boots with the shell), so every one works
* with no Timers window open; timer.set and alarm.set also bring the window
* forward so the wearer sees what was set.
*/
import { toolRegistry, type ToolRegistry } from "./tool-registry";
import { callAppToolWithLaunch } from "./launch-on-call";
import {
alarmNextRingMs,
formatClockAt,
formatCountdown,
formatDays,
formatDurationWords,
formatElapsed,
formatSpanWords,
formatTimeOfDay,
MAX_TIMER_DURATION_MS,
parseRepeatDays,
parseTimeOfDay,
sortAlarms,
sortTimers,
stopwatchElapsedMs,
stopwatchIsRunning,
timerDisplayName,
timerPhase,
timerRemainingMs,
type Alarm,
type CountdownTimer,
} from "../apps/timer/timer-model";
import { timerEngine } from "../apps/timer/timer-engine";
import { timeFormatSetting } from "../ui/dashboard-settings";
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
let registered = false;
// Wrapper timeout: covers a first-launch worker spawn (up to the 5 s
// tool-appear wait in callAppToolWithLaunch) plus the forwarded call's own
// 10 s default budget.
const TIMER_WRAPPER_TIMEOUT_MS = 20_000;
function twelveHour(): boolean {
return timeFormatSetting.get() === "12h";
}
export function registerTimerTools(
launchApp: (appId: string) => Promise<void>,
registry: ToolRegistry = toolRegistry,
): void {
/** Timers in the order the app lists them, so "timer 2" means the same thing in both places. */
function listedTimers(): CountdownTimer[] {
return sortTimers(timerEngine.state.timers, Date.now());
}
function listedAlarms(): Alarm[] {
return sortAlarms(timerEngine.state.alarms);
}
function describeTimer(timer: CountdownTimer, number: number, now: number): string {
const name = timerDisplayName(timer);
switch (timerPhase(timer)) {
case "rung":
return `Timer ${number} (${name}): finished ${formatSpanWords(now - timer.rungAtMs!)} ago, still ringing until dismissed.`;
case "paused":
return `Timer ${number} (${name}): paused with ${formatCountdown(timerRemainingMs(timer, now))} left.`;
default:
return `Timer ${number} (${name}): ${formatCountdown(timerRemainingMs(timer, now))} left, ends at ${formatClockAt(timer.endsAtMs!, twelveHour())}.`;
}
}
function describeAlarm(alarm: Alarm, number: number, now: number): string {
const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour());
const name = alarm.label ? ` (${alarm.label})` : "";
if (alarm.ringingSinceMs !== null) return `Alarm ${number}: ${time}${name}, ringing now.`;
if (!alarm.enabled) return `Alarm ${number}: ${time}${name}, ${formatDays(alarm.days).toLowerCase()}, off.`;
const next = alarmNextRingMs(alarm, now);
const when = next === null ? "" : `, rings in ${formatSpanWords(next - now)}`;
const snoozed = alarm.snoozedUntilMs !== null ? " (snoozed)" : "";
return `Alarm ${number}: ${time}${name}, ${formatDays(alarm.days).toLowerCase()}${when}${snoozed}.`;
}
/** Resolve a 1-based "timer"/"alarm" argument, or the only item when omitted. */
function pickItem<T>(items: T[], raw: unknown, noun: string, describe: (item: T, number: number) => string): T | ToolResult {
if (items.length === 0) return { ok: false, error: `No ${noun}s are set.` };
if (raw === undefined || raw === null) {
if (items.length === 1) return items[0]!;
return {
ok: false,
error: `There are ${items.length} ${noun}s; give the ${noun} number.\n${items.map(describe).join("\n")}`,
};
}
const index = Math.floor(Number(raw));
if (!Number.isFinite(index) || index < 1 || index > items.length) {
return { ok: false, error: `No ${noun} ${raw}; there are ${items.length} ${noun}s.` };
}
return items[index - 1]!;
}
function isResult(value: unknown): value is ToolResult {
return !!value && typeof value === "object" && "ok" in (value as object) && !("id" in (value as object));
}
function durationField(value: unknown, label: string): number {
if (value === undefined || value === null || value === "") return 0;
const parsed = Number(value);
if (!Number.isFinite(parsed) || parsed < 0) throw new Error(`Invalid ${label} value: ${value}`);
return parsed;
}
const NUMBER_ARG = (noun: string, listTool: string) => ({
type: "number",
description: `1-based ${noun} number, as shown by ${listTool}. May be omitted when only one ${noun} exists.`,
});
export function registerTimerTools(launchApp: (appId: string) => Promise<void>, registry: ToolRegistry = toolRegistry): void {
if (registered) return;
registered = true;
const showApp = () => {
void launchApp("timer").catch((error) => console.warn(`timer tool launch failed: ${error}`));
};
// --- timers ---
registry.registerSystemTool(
{
name: "timer.set",
description:
"Start a countdown timer on the glasses. Give the duration as hours/minutes/seconds; at least one must be nonzero. Opens the Timer app if it isn't already open. Returns the timer's number and when it will finish.",
"Start a countdown timer on the glasses. Give the duration as hours/minutes/seconds (at least one nonzero) and optionally a label such as \"pasta\". 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)." },
label: { type: "string", description: "Optional short name for the timer." },
},
additionalProperties: false,
},
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
proactive: true,
},
(args) => callAppToolWithLaunch(registry, launchApp, "timer", "set_timer", args ?? {}),
(args) => {
const durationMs = Math.round(
((durationField(args?.hours, "hours") * 60 + durationField(args?.minutes, "minutes")) * 60 +
durationField(args?.seconds, "seconds")) *
1000,
);
if (durationMs <= 0) return { ok: false, error: "timer.set requires a duration longer than zero." };
if (durationMs > MAX_TIMER_DURATION_MS) return { ok: false, error: "Timers can run for at most 24 hours." };
const label = typeof args?.label === "string" ? args.label.trim() : "";
const timer = timerEngine.startTimer(durationMs, label);
showApp();
const number = listedTimers().indexOf(timer) + 1;
return {
ok: true,
content: `Started a ${formatDurationWords(durationMs)} timer${label ? ` "${label}"` : ""} (timer ${number}), finishing at ${formatClockAt(timer.endsAtMs!, twelveHour())}.`,
};
},
);
registry.registerSystemTool(
{
name: "timer.list",
description:
"List the countdown timers on the glasses: each timer's number, its original duration, and the time remaining (or that it has finished). Opens the Timer app if it isn't already open — timers keep running while its window is closed.",
"List the countdown timers on the glasses: each timer's number, name, time remaining (or paused / finished), and end time.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
proactive: true,
},
() => callAppToolWithLaunch(registry, launchApp, "timer", "list_timers", {}),
() => {
const timers = listedTimers();
if (timers.length === 0) return { ok: true, content: "No timers are set." };
const now = Date.now();
return { ok: true, content: timers.map((timer, index) => describeTimer(timer, index + 1, now)).join("\n") };
},
);
registry.registerSystemTool(
{
name: "timer.cancel",
description:
"Cancel a running countdown timer on the glasses, or dismiss a finished one. Give the timer number from timer.list; it may be omitted when only one timer exists. Pass all=true to clear every timer. Opens the Timer app if it isn't already open.",
"Cancel a countdown timer, or dismiss a finished one. Give the timer number from timer.list (omit it when only one exists), or all=true to clear every timer.",
inputSchema: {
type: "object",
properties: {
timer: { type: "number", description: "1-based timer number, as shown by timer.list." },
timer: NUMBER_ARG("timer", "timer.list"),
all: { type: "boolean", description: "Clear all timers instead of a single one." },
},
additionalProperties: false,
},
timeoutMs: TIMER_WRAPPER_TIMEOUT_MS,
proactive: true,
},
(args) => callAppToolWithLaunch(registry, launchApp, "timer", "cancel_timer", args ?? {}),
(args) => {
const timers = listedTimers();
if (args?.all === true) {
if (timers.length === 0) return { ok: true, content: "No timers are set." };
for (const timer of timers) timerEngine.removeTimer(timer.id);
return { ok: true, content: timers.length === 1 ? "Cleared the timer." : `Cleared ${timers.length} timers.` };
}
const now = Date.now();
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
if (isResult(picked)) return picked;
const description = describeTimer(picked, timers.indexOf(picked) + 1, now);
timerEngine.removeTimer(picked.id);
return { ok: true, content: `${timerPhase(picked) === "rung" ? "Dismissed" : "Canceled"}: ${description}` };
},
);
registry.registerSystemTool(
{
name: "timer.pause",
description: "Pause a running countdown timer, or resume a paused one. Give the timer number from timer.list (omit it when only one exists).",
inputSchema: {
type: "object",
properties: {
timer: NUMBER_ARG("timer", "timer.list"),
resume: { type: "boolean", description: "true to resume a paused timer instead of pausing." },
},
additionalProperties: false,
},
proactive: true,
},
(args) => {
const timers = listedTimers();
const now = Date.now();
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
if (isResult(picked)) return picked;
const phase = timerPhase(picked);
if (phase === "rung") return { ok: false, error: "That timer has already finished; dismiss it with timer.cancel." };
const resume = args?.resume === true || phase === "paused";
if (resume) timerEngine.resumeTimer(picked.id);
else timerEngine.pauseTimer(picked.id);
const updated = timerEngine.findTimer(picked.id)!;
return { ok: true, content: `${resume ? "Resumed" : "Paused"} ${describeTimer(updated, timers.indexOf(picked) + 1, Date.now())}` };
},
);
registry.registerSystemTool(
{
name: "timer.add_time",
description: "Add minutes to a countdown timer (a finished one restarts with just the added time). Give the timer number from timer.list (omit it when only one exists).",
inputSchema: {
type: "object",
properties: {
timer: NUMBER_ARG("timer", "timer.list"),
minutes: { type: "number", description: "Minutes to add (default 1)." },
},
additionalProperties: false,
},
proactive: true,
},
(args) => {
const timers = listedTimers();
const now = Date.now();
const picked = pickItem(timers, args?.timer, "timer", (timer, number) => describeTimer(timer, number, now));
if (isResult(picked)) return picked;
const minutes = args?.minutes === undefined ? 1 : Number(args.minutes);
if (!Number.isFinite(minutes) || minutes <= 0) return { ok: false, error: `Invalid minutes value: ${args?.minutes}` };
timerEngine.addTimerTime(picked.id, Math.round(minutes * 60_000));
const updated = timerEngine.findTimer(picked.id)!;
return { ok: true, content: `Added ${formatDurationWords(minutes * 60_000)}. ${describeTimer(updated, listedTimers().indexOf(updated) + 1, Date.now())}` };
},
);
registry.registerSystemTool(
{
name: "timer.dismiss",
description: "Stop whatever is ringing on the glasses right now: finished timers are cleared and ringing alarms dismissed.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
() => {
const count = timerEngine.dismissAllRinging();
return { ok: true, content: count === 0 ? "Nothing is ringing." : `Dismissed ${count === 1 ? "it" : `${count} items`}.` };
},
);
// --- stopwatch ---
registry.registerSystemTool(
{
name: "stopwatch.control",
description:
"Control the stopwatch on the glasses: action start, stop, lap, reset, or read (report the elapsed time and laps without changing anything).",
inputSchema: {
type: "object",
properties: {
action: { type: "string", enum: ["start", "stop", "lap", "reset", "read"], description: "What to do." },
},
required: ["action"],
additionalProperties: false,
},
proactive: true,
},
(args) => {
const action = String(args?.action ?? "read");
switch (action) {
case "start":
timerEngine.stopwatchStart();
showApp();
break;
case "stop":
timerEngine.stopwatchStop();
break;
case "lap":
if (!stopwatchIsRunning(timerEngine.state.stopwatch)) return { ok: false, error: "The stopwatch is not running." };
timerEngine.stopwatchLap();
break;
case "reset":
timerEngine.stopwatchReset();
break;
case "read":
break;
default:
return { ok: false, error: `Unknown stopwatch action: ${action}` };
}
const stopwatch = timerEngine.state.stopwatch;
const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, Date.now()), true);
const state = stopwatchIsRunning(stopwatch) ? "running" : "stopped";
const laps = stopwatch.lapsMs.length > 0 ? ` Laps: ${stopwatch.lapsMs.map((lap) => formatElapsed(lap, true)).join(", ")}.` : "";
return { ok: true, content: `Stopwatch ${state} at ${elapsed}.${laps}` };
},
);
// --- alarms ---
registry.registerSystemTool(
{
name: "alarm.set",
description:
"Set an alarm on the glasses. Give the time of day (e.g. \"7:30\", \"7:30 am\", \"19:30\"), optional repeat days (day names, \"weekdays\", \"weekends\", \"every day\"; default once), and an optional label.",
inputSchema: {
type: "object",
properties: {
time: { type: "string", description: "Time of day, 12- or 24-hour." },
days: {
type: "array",
items: { type: "string" },
description: "Repeat days: day names or weekdays / weekends / every day. Omit for a one-off alarm.",
},
label: { type: "string", description: "Optional short name for the alarm." },
},
required: ["time"],
additionalProperties: false,
},
proactive: true,
},
(args) => {
const time = parseTimeOfDay(String(args?.time ?? ""));
if (!time) return { ok: false, error: `Could not understand the time "${args?.time}". Use a form like 7:30 am or 19:30.` };
const days = parseRepeatDays(args?.days);
if (days === null) return { ok: false, error: `Could not understand the repeat days ${JSON.stringify(args?.days)}.` };
const label = typeof args?.label === "string" ? args.label.trim() : "";
const alarm = timerEngine.addAlarm(time.hour, time.minute, days, label);
showApp();
const now = Date.now();
return { ok: true, content: `Set. ${describeAlarm(alarm, listedAlarms().indexOf(alarm) + 1, now)}` };
},
);
registry.registerSystemTool(
{
name: "alarm.list",
description: "List the alarms on the glasses: each alarm's number, time, repeat days, label, and whether it is on.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
() => {
const alarms = listedAlarms();
if (alarms.length === 0) return { ok: true, content: "No alarms are set." };
const now = Date.now();
return { ok: true, content: alarms.map((alarm, index) => describeAlarm(alarm, index + 1, now)).join("\n") };
},
);
registry.registerSystemTool(
{
name: "alarm.enable",
description: "Turn an alarm on or off without deleting it. Give the alarm number from alarm.list (omit it when only one exists).",
inputSchema: {
type: "object",
properties: {
alarm: NUMBER_ARG("alarm", "alarm.list"),
enabled: { type: "boolean", description: "true to turn the alarm on, false to turn it off." },
},
required: ["enabled"],
additionalProperties: false,
},
proactive: true,
},
(args) => {
const alarms = listedAlarms();
const now = Date.now();
const picked = pickItem(alarms, args?.alarm, "alarm", (alarm, number) => describeAlarm(alarm, number, now));
if (isResult(picked)) return picked;
timerEngine.setAlarmEnabled(picked.id, args?.enabled !== false);
const updated = timerEngine.findAlarm(picked.id)!;
return { ok: true, content: describeAlarm(updated, alarms.indexOf(picked) + 1, Date.now()) };
},
);
registry.registerSystemTool(
{
name: "alarm.delete",
description: "Delete an alarm. Give the alarm number from alarm.list (omit it when only one exists).",
inputSchema: {
type: "object",
properties: { alarm: NUMBER_ARG("alarm", "alarm.list") },
additionalProperties: false,
},
proactive: true,
},
(args) => {
const alarms = listedAlarms();
const now = Date.now();
const picked = pickItem(alarms, args?.alarm, "alarm", (alarm, number) => describeAlarm(alarm, number, now));
if (isResult(picked)) return picked;
const description = describeAlarm(picked, alarms.indexOf(picked) + 1, now);
timerEngine.deleteAlarm(picked.id);
return { ok: true, content: `Deleted: ${description}` };
},
);
registry.registerSystemTool(
{
name: "alarm.snooze",
description: "Snooze the alarm that is ringing now. Optionally give the snooze length in minutes (default: the Timers app's snooze setting).",
inputSchema: {
type: "object",
properties: { minutes: { type: "number", description: "Snooze length in minutes." } },
additionalProperties: false,
},
proactive: true,
},
(args) => {
const ringing = timerEngine.state.alarms.filter((alarm) => alarm.ringingSinceMs !== null);
if (ringing.length === 0) return { ok: false, error: "No alarm is ringing." };
const minutes = args?.minutes === undefined ? undefined : Number(args.minutes);
if (minutes !== undefined && (!Number.isFinite(minutes) || minutes <= 0)) return { ok: false, error: `Invalid minutes value: ${args?.minutes}` };
for (const alarm of ringing) timerEngine.snoozeAlarm(alarm.id, minutes);
const updated = timerEngine.findAlarm(ringing[0]!.id)!;
return { ok: true, content: `Snoozed until ${formatClockAt(updated.snoozedUntilMs!, twelveHour())}.` };
},
);
}
+189 -18
View File
@@ -12,7 +12,8 @@ import * as frameTimings from "../native/frame-timings";
import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service";
import { mediaControllerBridge } from "../native/media-controller";
import { nightscoutBridge } from "../native/nightscout-bridge";
import { onAndroidNotificationPosted } from "../native/notification-icons";
import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, readActiveNotifications } from "../native/notification-icons";
import { shouldShowNotificationOnGlasses } from "../native/notification-sources";
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
import { grayImageToPreviewSource } from "../native/gray-image-preview";
import { firmwareIncompatibilityMessage } from "./firmware-compat";
@@ -52,11 +53,12 @@ import { getDefaultMediumFont } from "../graphics/ui-fonts";
import { wrapText } from "../graphics/textwrap";
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
import { registerSystemTools } from "../assistant/system-tools";
import { updateGlassesPresence } from "./glasses-presence";
import { timerEngine } from "../apps/timer/timer-engine";
import { registerNavigateTools } from "../assistant/navigate-tools";
import { registerRoamTools } from "../assistant/roam-tools";
import { assistantBridge } from "../assistant/bridge-client";
import { registerWindowTools } from "../assistant/window-tools";
import { registerTimerTools } from "../assistant/timer-tools";
import { WorkerAppHost } from "../ui/shell/worker-window";
import { ALL_APPS } from "../apps/all-apps";
import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition";
@@ -64,7 +66,8 @@ import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-p
import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence";
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry";
import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers";
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
import {
getInstalledEvenHubAppById,
@@ -72,6 +75,8 @@ import {
uninstallEvenHubPackage,
} from "../apps/evenhub/installed-apps";
import { closeRunningPackage, launchInstalledPackage } from "../apps/evenhub/manager";
import { openEvenHubStoreForPackage } from "../apps/evenhub";
import { isInstalledPackagePresent } from "../apps/evenhub/updates";
import { wearerVerificationOptions } from "../apps/microphones/speakers";
import { micSession } from "../apps/microphones/mic-session";
import { glassesDisplayLabel } from "./glasses-display-state";
@@ -102,6 +107,13 @@ export type DashboardSnapshot = {
activeTextEditorToggleLabel: string;
activeTextEditorToggleValue: boolean;
activeTextEditorToggleVisible: boolean;
/**
* The keyboard dialog is up on the glasses (opened by the phone's keyboard
* button): the phone shows its typing panel, with a send button per
* destination in the glasses menu's row order.
*/
keyboardInputActive: boolean;
keyboardInputTargets: ReadonlyArray<{ id: string; label: string }>;
evenAppConflictMessage: string;
evenAppConflictWarningVisible: boolean;
firmwareWarningMessage: string;
@@ -109,6 +121,8 @@ export type DashboardSnapshot = {
screenRecordingActive: boolean;
batteryOptimizationWarningVisible: boolean;
fontsMissingWarningVisible: boolean;
/** Why the phone may fail to ring for a set alarm; "" when nothing is wrong or armed. */
alarmReliabilityMessage: string;
/**
* True while the headless preview display is standing in for a glasses
* connection (preview-only mode): the mirror is live and interactive, but
@@ -122,6 +136,8 @@ type DashboardListener = (snapshot: DashboardSnapshot) => void;
// The shell chrome (sidebar + top bar + overlays) composites above all app
// window surfaces with color-key transparency.
const SHELL_SURFACE_ID = "shell";
/** The shell chrome composites above every window surface (zOrder 0). */
const SHELL_SURFACE_Z_ORDER = 1;
const LOCK_SCREEN_SURFACE_ID = "lock-screen";
const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses.";
// Top-bar clock refresh; the phone-side preview polls the Java composite so
@@ -210,6 +226,7 @@ class DashboardController {
private firmwareWarningMessage = "";
private batteryOptimizationWarningVisible = false;
private fontsMissingWarningVisible = false;
private alarmReliabilityMessage = "";
// Sticky positive: the extracted-font file doesn't vanish mid-session, so a
// successful check spares later refreshes the file read and JSON parse.
private extractedFontsConfirmed = false;
@@ -265,11 +282,16 @@ class DashboardController {
/** Input frame that requested the next shell render; see requestShellRender. */
private pendingShellRenderCauseFrameId = 0;
private nextShellRenderWantsFreshData = false;
/** The under-shell dim last sent to the display (see Shell.underlayDim); 1 = none. */
private appliedUnderlayDim = 1;
// One shared worker per app hosts all its windows; spawned on first launch.
private readonly appHosts = new Map<string, WorkerAppHost>();
// The window hosting the on-glasses text-setting editor (the Settings app
// registers itself); edit flows from the phone UI reach it through this.
private textEditorHost: TextEditorHost | null = null;
// The open keyboard dialog's session (see Shell.startKeyboardInput); the
// phone's typing panel feeds it.
private keyboardInput: KeyboardInputSession | null = null;
// Other in-process singleton apps, keyed by windowId.
private readonly inProcessApps = new Map<string, InProcessWindow>();
private sharedActions!: Omit<LayerActions, "requestRender">;
@@ -297,7 +319,7 @@ class DashboardController {
toggle?: TextSettingsEditToggle,
) => this.startTextSettingsEdit(settings, title, onFinish, toggle),
endTextSettingEdit: () => this.endTextSettingEdit(),
startVoiceCapture: () => this.startVoiceCapture(),
startVoiceCapture: (endpointing = false) => this.startVoiceCapture(endpointing),
stopVoiceCapture: () => this.stopVoiceCapture(),
startContinuousVoiceCapture: () => this.startContinuousVoiceCapture(),
stopContinuousVoiceCapture: () => this.stopContinuousVoiceCapture(),
@@ -311,8 +333,6 @@ class DashboardController {
registerNavigateTools((appId) => this.launchApp(appId));
// roam.* tools launch the Roam app on demand likewise.
registerRoamTools((appId) => this.launchApp(appId));
// timer.* tools launch the Timer app on demand likewise.
registerTimerTools((appId) => this.launchApp(appId));
// apps.* tools mirror the launcher grid and sidebar (launch, focus, close).
registerWindowTools({
apps: LAUNCHABLE_APPS,
@@ -329,6 +349,10 @@ class DashboardController {
getScreenTimeoutMs: () => screenTimeoutSettingToMs(screenTimeoutSetting.get()),
requestShellRender: () => this.requestShellRender(),
prepareVoiceCapture: () => this.prepareVoiceCapture(),
onKeyboardInputChanged: (session) => {
this.keyboardInput = session;
this.emit();
},
onWindowsChanged: () => {
this.persistOpenApps();
// The foreground title is mirrored on both remote-control faces.
@@ -345,11 +369,13 @@ class DashboardController {
for (const app of ALL_APPS) {
app.boot?.(this.buildAppContext(app));
}
timerEngine.onChange(() => this.refreshAlarmReliabilityWarning());
this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => {
void this.handleAndroidNotificationPosted(notificationKey).catch((error) => {
this.appendLog(`notification wake failed: ${this.formatError(error)}`);
});
});
readActiveNotifications(ALL_NOTIFICATIONS);
// Settings toggled from the glasses can change what the phone UI shows
// (e.g. the text-setting editor), so re-emit the snapshot on any change.
onAnySettingChanged(() => {
@@ -360,6 +386,7 @@ class DashboardController {
this.syncEvenHubScreenOffSetting();
this.applyVerticalPositionIfChanged();
this.applyDisplayModeIfChanged();
this.applyAppLayoutsIfChanged();
this.syncAssistantBridgeIfChanged();
this.syncLockScreenSettingIfChanged();
// A Preview color change should show on the mirror at once, not at the
@@ -437,8 +464,8 @@ class DashboardController {
/**
* Display mode changed (Settings > Display, or the phone page's picker):
* every window's viewport size changes. In-process windows re-measure in
* place; worker windows get their canvas once at open, so they are closed
* and launched again at the new size.
* place; workers that support resizing receive the new viewport. Other
* worker windows are closed and launched again at the new size.
*/
private applyDisplayModeIfChanged(): void {
const mode = displayModeSetting.get();
@@ -474,6 +501,31 @@ class DashboardController {
});
}
private lastAppLayouts = this.appLayouts();
private appLayouts(): Record<string, string> {
return {
navigate: `${navigateDisplayModeSetting.get()}:${navigateVerticalPositionSetting.get()}`,
terminal: `${terminalDisplayModeSetting.get()}:${terminalVerticalPositionSetting.get()}`,
};
}
private applyAppLayoutsIfChanged(): void {
const layouts = this.appLayouts();
const changed = Object.keys(layouts).filter((appId) => layouts[appId] !== this.lastAppLayouts[appId]);
if (!changed.length) return;
this.lastAppLayouts = layouts;
void (async () => {
for (const window of Array.from(shell.getWindows())) {
if (!changed.includes(window.appId)) continue;
await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode);
window.relayout?.();
}
shell.foregroundWindow()?.requestRender();
this.requestShellRender();
})().catch((error) => this.appendLog(`app layout change failed: ${this.formatError(error)}`));
}
private applyVerticalPositionIfChanged(): void {
const position = verticalPositionSetting.get();
if (position === this.lastVerticalPosition) return;
@@ -546,6 +598,7 @@ class DashboardController {
private handleWearState(wearing: boolean): void {
this.glassesWorn = wearing;
updateGlassesPresence({ worn: wearing });
this.appendLog(wearing ? "glasses wear state: ON_HEAD" : "glasses wear state: OFF_HEAD");
this.wearRemote?.schedulePublish();
if (!wearing && this.phoneLocked && lockScreenEnabledSetting.get()) {
@@ -905,6 +958,8 @@ class DashboardController {
activeTextEditorToggleLabel: this.activeTextEditorToggle?.label ?? "",
activeTextEditorToggleValue: this.activeTextEditorToggle?.setting.get() ?? false,
activeTextEditorToggleVisible: this.activeTextEditorToggle !== null,
keyboardInputActive: this.keyboardInput !== null,
keyboardInputTargets: this.keyboardInput?.targets ?? [],
evenAppConflictMessage: this.evenAppConflictMessage,
evenAppConflictWarningVisible: this.evenAppConflictMessage.length > 0,
firmwareWarningMessage: this.firmwareWarningMessage,
@@ -912,6 +967,7 @@ class DashboardController {
screenRecordingActive: this.screenRecordingActive,
batteryOptimizationWarningVisible: this.batteryOptimizationWarningVisible,
fontsMissingWarningVisible: this.fontsMissingWarningVisible,
alarmReliabilityMessage: this.alarmReliabilityMessage,
previewMode: this.isPreviewDisplayActive(),
};
}
@@ -1003,9 +1059,35 @@ class DashboardController {
}
}
/**
* Mirror the Timers engine's phone self-check into the warnings modal. The
* engine re-checks on its own schedule and on every timer/alarm change;
* a page load forces a fresh check so a just-fixed setting clears at once.
*/
refreshAlarmReliabilityWarning(force = false): void {
if (force) timerEngine.refreshReliability(true);
const message = timerEngine.phoneReliabilityMessage();
if (message !== this.alarmReliabilityMessage) {
this.alarmReliabilityMessage = message;
this.emit();
}
}
openAlarmReliabilityFix(): void {
timerEngine.openPhoneReliabilityFix();
// The system screen is asynchronous with no result; poll briefly so the
// warning clears once the setting is changed.
let checksLeft = 12;
const poll = setInterval(() => {
this.refreshAlarmReliabilityWarning(true);
if (!this.alarmReliabilityMessage || --checksLeft <= 0) clearInterval(poll);
}, 5_000);
}
refreshEvenAppStatus(): void {
this.refreshBatteryOptimizationStatus();
this.refreshEvenHubFontStatus();
this.refreshAlarmReliabilityWarning(true);
const state = readEvenAppNotificationState();
const wasActive = this.evenNotificationActive;
this.evenNotificationActive = state.evenNotificationActive;
@@ -1041,6 +1123,39 @@ class DashboardController {
this.previewOrRenderAfterTextSettingChange();
}
/**
* The phone's keyboard button: open the keyboard dialog on the glasses
* (the typed twin of the mic button's wakeword). Needs a display to draw
* on and, like ring input, does nothing while the glasses are locked.
*/
startKeyboardInput(): void {
if (this.phase !== "connected" && this.phase !== "charging" && !this.isPreviewDisplayActive()) return;
if (this.glassesLocked) {
this.appendLog("keyboard input ignored while the glasses are locked");
return;
}
shell.startKeyboardInput();
}
/** The phone's typing panel changed: mirror the text on the glasses. */
setKeyboardInputText(text: string): void {
shell.noteUserActivity();
this.keyboardInput?.setText(text);
}
/** Send to one destination, or (no id: the IME's send key) the one highlighted on the glasses. */
sendKeyboardInput(targetId?: string): void {
if (targetId) {
this.keyboardInput?.sendTo(targetId);
} else {
this.keyboardInput?.send();
}
}
discardKeyboardInput(): void {
this.keyboardInput?.discard();
}
setActiveTextEditorToggleValue(value: boolean): void {
if (!this.activeTextEditorToggle || this.activeTextEditorToggle.setting.get() === value) return;
this.activeTextEditorToggle.setting.set(value);
@@ -1084,9 +1199,13 @@ class DashboardController {
y: 0,
width: G2_LENS_WIDTH,
height: G2_LENS_HEIGHT,
zOrder: 1,
zOrder: SHELL_SURFACE_Z_ORDER,
transparency: "color-key",
});
// A fresh (or reused) compositor starts undimmed; the shell render
// loop re-applies the current dim from here.
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1;
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) {
await this.configureWindowSurface(
@@ -1233,6 +1352,7 @@ class DashboardController {
// the transport comes back, so do not make lock decisions from a
// stale pre-disconnect value in the meantime.
this.glassesWorn = null;
updateGlassesPresence({ worn: null });
// The mic enable was session-scoped too: park any live capture so
// the next session restarts it, instead of leaving a holder that
// makes every later request think the mic is already running.
@@ -1270,10 +1390,16 @@ class DashboardController {
// especially while the glasses remain reachable in their case.
const hasBatteryLevel = Number.isInteger(state.battery) && state.battery >= 0 && state.battery <= 100;
if (hasBatteryLevel) this.lastHeadsetBattery = state.battery;
const ringBattery = state.ringBattery;
const hasRingBattery = typeof ringBattery === "number" && Number.isInteger(ringBattery)
&& ringBattery >= 0 && ringBattery <= 100;
shell.setBatteryLevels({
headset: hasBatteryLevel ? state.battery : this.lastHeadsetBattery,
headsetCharging: state.chargingStatus > 0,
ring: hasRingBattery ? ringBattery : null,
ringCharging: hasRingBattery ? state.ringChargingStatus === 1 : null,
});
updateGlassesPresence({ charging: state.chargingStatus > 0 || this.phase === "charging" });
if ((this.phase === "connected" || this.phase === "charging") && this.communicator) {
// Repaint the top bar (battery indicators live in the shell chrome).
this.requestShellRender();
@@ -1364,9 +1490,13 @@ class DashboardController {
y: 0,
width: G2_LENS_WIDTH,
height: G2_LENS_HEIGHT,
zOrder: 1,
zOrder: SHELL_SURFACE_Z_ORDER,
transparency: "color-key",
});
// A fresh (or reused) compositor starts undimmed; the shell render
// loop re-applies the current dim from here.
await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1;
await this.configureLockSurface(communicator);
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) {
@@ -1684,7 +1814,7 @@ class DashboardController {
}
const x = Math.round(Math.min(1, Math.max(0, nx)) * G2_LENS_WIDTH);
const y = Math.round(Math.min(1, Math.max(0, ny)) * G2_LENS_HEIGHT);
const stripShown = sidebarStripVisible(shell.getFocus());
const stripShown = sidebarStripVisible(shell.getFocus(), shell.foregroundWindow()?.appId);
this.appendLog(`mirror tap at ${x},${y}`);
if (!shell.hasOverlay() && stripShown && x < SIDEBAR_WIDTH) {
const target = shell.windowAtSidebarPoint(x, y);
@@ -1698,7 +1828,7 @@ class DashboardController {
}
const window = shell.foregroundWindow();
if (window && !shell.hasOverlay()) {
const rect = appViewportRect(window.heightMode);
const rect = appViewportRect(window.heightMode, window.appId);
const inside = x >= rect.x && x < rect.x + rect.width && y >= rect.y && y < rect.y + rect.height;
if (shell.getFocus() !== "window") {
shell.focusWindow(window.windowId);
@@ -1749,11 +1879,14 @@ class DashboardController {
* push-to-talk and the Transcribe app. Android mic permission is the consent
* gate even though the audio source is the G2 mic over BLE.
*/
private startVoiceCapture(endpointing = false): void {
this.beginVoiceCapture("ptt", endpointing);
private pttCaptureGeneration = 0;
private startVoiceCapture(endpointing = false): Promise<void> {
return this.beginVoiceCapture("ptt", endpointing);
}
private stopVoiceCapture(): void {
++this.pttCaptureGeneration;
voiceControlBridge.stopPushToTalk();
}
@@ -1819,7 +1952,8 @@ class DashboardController {
return granted;
}
private beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): void {
private async beginVoiceCapture(kind: "ptt" | "continuous", endpointing = false): Promise<void> {
const pttGeneration = kind === "ptt" ? ++this.pttCaptureGeneration : 0;
// Preview mode captures from the phone mic (voiceCaptureOptions with a
// null communicator); otherwise a live glasses session must be the source.
const previewCapture = this.isPreviewDisplayActive();
@@ -1827,8 +1961,9 @@ class DashboardController {
return;
}
const communicator = this.communicator;
void ensureVoicePermissions()
await ensureVoicePermissions()
.then(() => {
if (kind === "ptt" && pttGeneration !== this.pttCaptureGeneration) return;
if (previewCapture) {
if (!this.isPreviewDisplayActive()) return;
} else if (this.phase !== "connected" || this.communicator !== communicator) {
@@ -2156,6 +2291,13 @@ class DashboardController {
const installed = getInstalledEvenHubAppById(appId);
if (installed) {
const host = ALL_APPS.find((entry) => entry.appId === "evenhub")!;
if (!isInstalledPackagePresent(installed.packageId)) {
// The registry knows the app but its package is gone (a settings
// import after a reinstall): offer the store's Reinstall page.
this.appendLog(`evenhub: package missing for ${installed.packageId}; opening store page`);
await openEvenHubStoreForPackage(this.buildAppContext(host), installed);
return;
}
await launchInstalledPackage(this.buildAppContext({ ...host, appId }), installed);
return;
}
@@ -2184,7 +2326,7 @@ class DashboardController {
): Promise<void> {
if (!target) return;
await target.configureSurface(surfaceId, {
...appViewportRect(heightMode),
...appViewportRect(heightMode, shell.getWindows().find((window) => window.surfaceId === surfaceId)?.appId),
zOrder: 0,
transparency: "opaque",
});
@@ -2292,6 +2434,14 @@ class DashboardController {
frameTimings.finishFrame(frameId, "discarded: shell render with no display target");
return;
}
// A shell overlay that dims what it covers (a context menu) must dim the
// window surfaces too, which live below the shell surface in the
// compositor: forward the factor before this frame composites.
const underlayDim = shell.underlayDim();
if (underlayDim !== this.appliedUnderlayDim) {
this.appliedUnderlayDim = underlayDim;
await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim);
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
@@ -2326,7 +2476,8 @@ class DashboardController {
}
private async handleAndroidNotificationPosted(notificationKey: string): Promise<void> {
if (!notificationKey) {
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
if (!notification || !shouldShowNotificationOnGlasses(notification.packageName)) {
this.requestShellRender();
return;
}
@@ -2386,6 +2537,14 @@ class DashboardController {
private setPhase(phase: ConnectionPhase): void {
if (this.phase === phase) return;
this.phase = phase;
if (phase !== "connected" && phase !== "charging") {
shell.setBatteryLevels({ ring: null, ringCharging: null });
}
// "charging" is a live BLE link with the glasses in their case.
updateGlassesPresence({
connected: phase === "connected" || phase === "charging",
charging: phase === "charging" || (shell.getBatteryLevels().headsetCharging ?? false),
});
if (phase === "disconnected") {
// Kept across "connecting": silent mode blocks app launches, so it can
// itself cause the reconnect churn, and Java re-reports it either way.
@@ -2494,6 +2653,7 @@ class DashboardController {
| "scroll-down"
| "long-press"
| "long-press-release"
| "short-then-long-press"
| "wakeword"
| "swipe-left"
| "swipe-right"
@@ -2553,6 +2713,17 @@ class DashboardController {
systemExitReasonCode: 0,
frameId,
};
case "short-then-long-press":
// Discrete, unlike a hold: the firmware's later generic release is a
// hardware artifact the shell ignores, so synthetic sources send none.
return {
kind: "sys-event",
containerName: "",
eventType: OsEventTypeList.SHORT_THEN_LONG_PRESS_EVENT,
eventSource,
systemExitReasonCode: 0,
frameId,
};
case "click":
return {
kind: "sys-event",
+3
View File
@@ -69,6 +69,8 @@ export const OsEventTypeList = {
IMU_DATA_REPORT: 8,
RING_LONG_PRESS_EVENT: 9,
RING_LONG_PRESS_RELEASE_EVENT: 10,
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
SHORT_THEN_LONG_PRESS_EVENT: 11,
} as const;
export const OsEventTypeName: Record<number, string> = {
@@ -83,4 +85,5 @@ export const OsEventTypeName: Record<number, string> = {
8: "IMU_DATA_REPORT",
9: "RING_LONG_PRESS_EVENT",
10: "RING_LONG_PRESS_RELEASE_EVENT",
11: "SHORT_THEN_LONG_PRESS_EVENT",
};
+11 -4
View File
@@ -1,6 +1,7 @@
/**
* Builds the Faceclaw custom firmware on-device: downloads the stock Even
* Realities G2 2.2.6.10 image from Even's CDN, verifies its SHA-256, applies
* Realities G2 image the patch set was built against (CFW_PATCH_SET.base,
* currently 2.2.9.22) from Even's CDN, verifies its SHA-256, applies
* the committed byte-patch set (cfw-patches.ts), extracts the stock EvenHub
* fonts for phone-side rendering, verifies the patched SHA-256, and writes the
* result to app storage.
@@ -24,9 +25,15 @@ import { EvenHubFont } from "../graphics/evenhub-font";
declare const com: any;
const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/e28738432d7b612d625331b00383149b.bin";
const CFW_OUTPUT_FILENAME = "g2_2.2.6.10_cfw.bin";
const STOCK_OUTPUT_FILENAME = "g2_2.2.6.10.bin";
// The CDN names firmware files by MD5. This must be the image whose SHA-256 is
// CFW_PATCH_SET.baseSha256 — keep it in sync with FW_URL in g2flash/build_cfw.sh
// whenever cfw-patches.ts is regenerated against a new stock base.
// (2.2.9.22 = fc250b05…; the older 2.2.6.10 base was e2873843….)
const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/fc250b05e98a9ff998b4b68f5f99f994.bin";
// Output names follow the patch set's base name (e.g. g2_2.2.9.22.bin →
// g2_2.2.9.22_cfw.bin) so a rebase can't leave stale version numbers here.
const STOCK_OUTPUT_FILENAME = CFW_PATCH_SET.base;
const CFW_OUTPUT_FILENAME = CFW_PATCH_SET.base.replace(/\.bin$/, "") + "_cfw.bin";
export type FirmwareProgress =
| { phase: "downloading" }
+29 -6
View File
@@ -13,8 +13,10 @@ const REQUIRED_FIRMWARE_EXTENSIONS = ["img640", "fbguard", "wearnotify"] as cons
// The stock firmware release Faceclaw's custom image is built from. Stock at or
// below this can be flashed with our patched image; a newer stock version is
// unrecognized (its layout may differ from what our patch set targets).
export const FLASHABLE_STOCK_VERSION = [2, 2, 9, 22];
export const FLASHABLE_STOCK_VERSION_TEXT = FLASHABLE_STOCK_VERSION.join(".");
export const BASE_STOCK_VERSION = [2, 2, 9, 22];
export const BASE_STOCK_VERSION_TEXT = BASE_STOCK_VERSION.join(".");
export const VALIDATED_STOCK_VERSION = [2, 2, 10, 10];
export const VALIDATED_STOCK_VERSION_TEXT = VALIDATED_STOCK_VERSION.join(".");
function parseDottedVersion(version: string): number[] {
return version
@@ -89,6 +91,15 @@ export function hasMicControl(info: FirmwareInfo): boolean {
return info.capabilities.trim().split(/\s+/).includes("micctl");
}
/**
* True when the firmware advertises the optional ambient-light extension:
* image-handler mode 16 (query / passive polling of the OPT3001 light sensor)
* with field-105 reports, used to drive a phone-side brightness policy.
*/
export function hasAmbientLightSensor(info: FirmwareInfo): boolean {
return info.capabilities.trim().split(/\s+/).includes("als16");
}
/** The higher of the two arms' reported versions, or "" if none reported. */
export function reportedFirmwareVersion(info: FirmwareInfo): string {
const versions = [info.leftVersion, info.rightVersion].map((v) => v.trim()).filter(Boolean);
@@ -102,10 +113,12 @@ export function reportedFirmwareVersion(info: FirmwareInfo): string {
* How the pre-flash firmware check should treat the connected glasses:
* - "custom": Faceclaw's firmware is already installed — nothing to flash.
* - "flashable-stock": stock firmware at or below the version we build from.
* - "newer-stock": stock firmware newer than we recognize — flash only on override.
* - "newer-stock-validated": stock firmware newer than the version we build
* from, on which the downgrade has been tested successfully
* - "newer-stock-unvalidated": stock firmware newer than we recognize — flash only on override.
* - "unknown": no version could be read (treated as a probe/connection failure).
*/
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock" | "unknown";
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock-validated" | "newer-stock-unvalidated" | "unknown";
export function classifyOnboardingFirmware(info: FirmwareInfo): {
kind: OnboardingFirmwareKind;
@@ -118,6 +131,16 @@ export function classifyOnboardingFirmware(info: FirmwareInfo): {
if (!version) {
return { kind: "unknown", version: "" };
}
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
if (compareVersions(parseDottedVersion(version), BASE_STOCK_VERSION) <= 0) {
return { kind: "flashable-stock", version };
} else if (compareVersions(parseDottedVersion(version), VALIDATED_STOCK_VERSION) <= 0) {
return { kind: "newer-stock-validated", version };
} else {
return { kind: "newer-stock-unvalidated", version };
}
}
/** Cached R1 battery reports in settings field 106 and query mode 17. */
export function hasRingBattery(info: FirmwareInfo): boolean {
return info.capabilities.trim().split(/\s+/).includes("ringbat17");
}
+9 -3
View File
@@ -21,10 +21,16 @@ type FontSpec = {
};
// Priority order is the stock EvenHub fallback chain (before the CJK font).
// These are lv_font_fmt_txt_dsc_t addresses in the stock image the patch set
// is built against (CFW_PATCH_SET.base), so they move on every firmware
// rebase: run scripts/find-firmware-fonts.py against the new stock .bin and
// match rows by cmap count / code-point count (12/8081, 9/9566, 4/11590).
// 2.2.9.22: 0x007a8954, 0x007a8968, 0x007a8878
// 2.2.6.10: 0x0077f414, 0x0077f428, 0x0077f374
const FONT_SPECS: FontSpec[] = [
{ descriptor: 0x0077f414, lineHeight: 27, baseLine: 5 },
{ descriptor: 0x0077f428, lineHeight: 26, baseLine: 5 },
{ descriptor: 0x0077f374, lineHeight: 20, baseLine: 2 },
{ descriptor: 0x007a8954, lineHeight: 27, baseLine: 5 },
{ descriptor: 0x007a8968, lineHeight: 26, baseLine: 5 },
{ descriptor: 0x007a8878, lineHeight: 20, baseLine: 2 },
];
type ExtractedGlyph = {
+93 -81
View File
@@ -1,6 +1,6 @@
// AUTO-GENERATED from g2flash/patches/cfw_patches.json — do not edit by hand.
// Regenerate with scripts/update_deltas.sh.
// Turns the stock G2 2.2.6.10 image into the Faceclaw custom firmware.
// Turns the stock G2 2.2.9.22 image into the Faceclaw custom firmware.
export type FirmwarePatchOp = {
offset: number;
@@ -17,164 +17,176 @@ export type FirmwarePatchSet = {
};
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.2.6.10.bin",
"baseSha256": "f4dfb0b49ad3de3c2daf17f8a27a157c3dc98411d6a0d3ab2cfd0918f41b9afa",
"outputSha256": "d4054ab1519c7184fcc4450104de18f9ff714180c80b3a579f91106536a274cd",
"base": "g2_2.2.9.22.bin",
"baseSha256": "a03fbea9f68a9de6bc271daabb9f3a41c59053d1086622c76a4e990f829cc561",
"outputSha256": "12e1669fe091a94016ecc31b3940eaffb9ac1fbcb1e7807003c1f68e7f85ead3",
"patches": [
{
"offset": 1089903,
"offset": 1124178,
"old": "5ff43432",
"new": "5ff43332",
"desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor"
},
{
"offset": 1449549,
"offset": 1524180,
"old": "bdf82c10",
"new": "40f24120",
"desc": "container width <= 576"
},
{
"offset": 1449749,
"offset": 1524380,
"old": "bdf82e00",
"new": "40f22111",
"desc": "container height movw #0x121"
},
{
"offset": 1449753,
"offset": 1524384,
"old": "9128",
"new": "8842",
"desc": "container height cmp r0,r1"
},
{
"offset": 1447919,
"old": "7ef7fefd",
"new": "bcf23afb",
"desc": "bl snapshot_side @ 0x4db968"
"offset": 1516784,
"old": "70f775ff",
"new": "d6f2fffa",
"desc": "bl snapshot_side @ 0x4ec0ee (shared image-complete helper)"
},
{
"offset": 1448931,
"old": "7ef704fc",
"new": "bcf240f9",
"desc": "bl snapshot_side @ 0x4dbd5c"
"offset": 1516816,
"old": "30d0",
"new": "1cd0",
"desc": "image-complete success -> immediate ACK (pipelining-safe)"
},
{
"offset": 1165461,
"old": "45f0cefd",
"new": "01f3ebfa",
"offset": 1222660,
"old": "49f0daff",
"new": "1ef379f9",
"desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)"
},
{
"offset": 1186287,
"old": "d9f7d4ff",
"new": "fdf21afa",
"offset": 1242342,
"old": "d4f790fb",
"new": "1af320fd",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1183983,
"old": "f4f75aff",
"new": "fdf2bafb",
"offset": 1240038,
"old": "f5f710f9",
"new": "1af3b6fe",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 926945,
"old": "08f068fa",
"new": "3cf3ddf8",
"desc": "bl faceclaw_display_start @ 0x45c65a (fail-open double-tap takeover)"
"offset": 939336,
"old": "0bf02af9",
"new": "64f321f9",
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open double-tap takeover)"
},
{
"offset": 927137,
"old": "08f008fa",
"new": "3cf37df8",
"desc": "bl faceclaw_display_start @ 0x45c71a (fail-open double-tap takeover)"
"offset": 939528,
"old": "0bf0caf8",
"new": "64f3c1f8",
"desc": "bl faceclaw_display_start @ 0x45f206 (fail-open double-tap takeover)"
},
{
"offset": 822553,
"old": "28f003f8",
"new": "56f3e5fd",
"desc": "bl evenhub_longpress (Faceclaw lease gates custom event vs stock dialog)"
"offset": 831036,
"old": "1df057fc",
"new": "80f33df9",
"desc": "bl gesture_press (Faceclaw lease gates event 9 vs stock UI event 8)"
},
{
"offset": 823369,
"old": "1cf09bfb",
"new": "56f371fc",
"desc": "bl ring_release (forward source-qualified release-long-press)"
"offset": 831042,
"old": "24f023f9",
"new": "80f3def9",
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
},
{
"offset": 1474137,
"offset": 831800,
"old": "1df0d9fa",
"new": "7ff3c3ff",
"desc": "bl gesture_release (source-qualified event 10 vs stock UI event 0x4a)"
},
{
"offset": 1553752,
"old": "7fb50600",
"new": "b6f2d3bf",
"new": "cef2d5bc",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
"offset": 1022741,
"old": "f8f7c1fe",
"new": "20f30ffc",
"desc": "bl display_copy_hook @ 0x473c8e (640x480 direct framebuffer)"
"offset": 1047796,
"old": "f6f7edff",
"new": "45f33df9",
"desc": "bl display_copy_hook @ 0x4798f2 (640x480 direct framebuffer)"
},
{
"offset": 1022959,
"old": "f8f754fe",
"new": "20f3a2fb",
"desc": "bl display_copy_hook @ 0x473d68 (640x480 direct framebuffer)"
"offset": 1048112,
"old": "f6f74fff",
"new": "45f39ff8",
"desc": "bl display_copy_hook @ 0x479a2e (640x480 direct framebuffer)"
},
{
"offset": 1198787,
"old": "dff770fb",
"new": "f9f2f4fc",
"desc": "bl faceclaw_send_wear_event @ 0x49ec3c (outside onboarding)"
"offset": 1255404,
"old": "d9f758ff",
"new": "16f3cdfe",
"desc": "bl faceclaw_send_wear_event @ 0x4ac3ea (outside onboarding)"
},
{
"offset": 1198881,
"old": "dff741fb",
"new": "f9f2c5fc",
"desc": "bl faceclaw_send_wear_event @ 0x49ec9a (outside onboarding)"
"offset": 1255504,
"old": "d9f726ff",
"new": "16f39bfe",
"desc": "bl faceclaw_send_wear_event @ 0x4ac44e (outside onboarding)"
},
{
"offset": 823567,
"old": "1cf038fb",
"new": "51f3d2f8",
"desc": "bl compass_event_forward (global IMU heading -> stock nav BLE notifier)"
"offset": 1297700,
"old": "65f0affd",
"new": "0cf3a1f9",
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
},
{
"offset": 4301227,
"offset": 1296176,
"old": "fff759fc",
"new": "0cf315fd",
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
},
{
"offset": 4476518,
"old": "",
"new": "02fb01f000e000bfb0b546f27424c2f22a040146206846f23f52c5f28802b2eb500f40f2543044f281124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f2543044f29f22c2f20000c0f24802104770b500284ed001684ef66862ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984746f27021c2f22a010e6831466ff39601b1f1005f0ad131684ef66862ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f9db046f2702ac2f22a0adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66867ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f2b870c2f20000d0f8008004a802f0caffb8f1000f00f01982032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f0708196f8dc10002000298df8140000f0e88105a90a5c002a00f0bb810a185378002b00f079819378002b00f07781d378002b00f075811379002b00f073815379002b00f071819379002b00f06f81d379002b00f06d81137a002b00f06b81537a002b00f06981937a002b00f06781d37a002b00f06581137b002b00f06381537b002b00f06181937b002b00f05f81d37b002b00f05d81137c002b00f05b81537c002b00f05981937c002b00f05781d37c002b00f05581137d002b00f05381537d002b00f05181937d002b00f04f81d37d002b00f04d81137e002b00f04b81537e002b00f04981937e002b00f04781d37e002b00f04581137f002b00f04381537f002b00f04181937f002b00f03f81d27f002a00f03d81203060287ff47baf5ce19bf82a0270b1dbf82c0240b144f63421c2f207010968401a002800f3ea8000208bf82a024cf61520c0f246001db0bde8f04f00474ff41b7004f063fe002800f0bd804ff69451cff6ff71002200bf43183e3183f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f8752283f8762283f8772283f8782283f8792283f87a2283f87b2283f87c2283f87d2283f87e2283f87f2283f8802283f8812283f8822283f8832283f8842283f8852283f8862283f8872283f8882283f8892283f88a2283f88b2283f88c2283f88d2283f88e2283f88f2283f8902283f8912283f8922283f8932283f8942283f8952283f8962283f8972283f8982283f8992283f89a2283f89b2283f89c2283f89d2283f89e2283f89f2283f8a02283f8a12283f8a22283f8a32283f8a42283f8a52283f8a62283f8a72283f8a82283f8a9227ff480af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf829024cf61520c0f24600804715e00020caf8000000208bf82802cbf824024ff4163045f20f11069005a8c0f24701cdf81480884701208bf82a0204a802f0aafecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e004313Line truncated
"new": "02fb01f000e000bfb0b54ff2ac44c2f229040146206846f23f52c5f28802b2eb500f40f258304cf2e9124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304cf20732c2f20000c0f24802104770b500284ed001684ef66a62ccf2ff02914218bf70bd90f8081190f8095146f6756c8d42c0f2510c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b142f64d42c0f244022146bde87040104770bd00bf2de9f04f9db04ff2a84ac2f2290adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66a67ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f6b400c2f20000d0f8008004a802f0d4ffb8f1000f00f02382032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f07a8196f8dc10002000298df8140000f0f28105a90a5c002a00f0c5810a185378002b00f083819378002b00f08181d378002b00f07f811379002b00f07d815379002b00f07b819379002b00f07981d379002b00f07781137a002b00f07581537a002b00f07381937a002b00f07181d37a002b00f06f81137b002b00f06d81537b002b00f06b81937b002b00f06981d37b002b00f06781137c002b00f06581537c002b00f06381937c002b00f06181d37c002b00f05f81137d002b00f05d81537d002b00f05b81937d002b00f05981d37d002b00f05781137e002b00f05581537e002b00f05381937e002b00f05181d37e002b00f04f81137f002b00f04d81537f002b00f04b81937f002b00f04981d27f002a00f04781203060287ff47baf66e19bf82a0270b1dbf82c0240b146f68051c2f207010968401a002800f3f48000208bf82a0240f6d100c0f247001db0bde8f04f00474ff4497005f0e5f8002800f0c7804ff6dc41cff6ff71002200bf4318433183f8242383f8252383f8262383f8272383f8282383f8292383f82a2383f82b2383f82c2383f82d2383f82e2383f82f2383f8302383f8312383f8322383f8332383f8342383f8352383f8362383f8372383f8382383f8392383f83a2383f83b2383f83c2383f83d2383f83e2383f83f2383f8402383f8412383f8422383f8432383f8442383f8452383f8462383f8472383f8482383f8492383f84a2383f84b2383f84c2383f84d2383f84e2383f84f2383f8502383f8512383f8522383f8532383f8542383f8552383f8562383f8572383f8582383f8592383f85a2383f85b2383f85c2383f85d2383f85e2383f85f2383f8602383f8612383f8622383f8632383f8642383f8652383f866237ff476af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf8290240f6d100c0f24700804715e00020caf8000000208bf82802cbf824024ff416304cf60361069005a8c0f24701cdf81480884701208bf82a0204a802f0b4fecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 777711,
"old": "44c33500",
"new": "4a5c3600",
"offset": 779114,
"old": "846a3800",
"new": "86113900",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "c4c33500",
"new": "ca5c3600",
"old": "046b3800",
"new": "06123900",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 777831,
"old": "44c33504",
"new": "4a5c3604",
"offset": 779234,
"old": "846a3804",
"new": "86113904",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 777835,
"old": "df8110c9",
"new": "0aa0bcf3",
"offset": 779238,
"old": "85c355a2",
"new": "1a7d445b",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c1be0fb1",
"new": "01fcf86d",
"old": "ed292ee4",
"new": "a7b82e22",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 777715,
"old": "c1be0fb1",
"new": "01fcf86d",
"offset": 779118,
"old": "ed292ee4",
"new": "a7b82e22",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
+46
View File
@@ -0,0 +1,46 @@
/**
* Whether the glasses could carry an alert right now: connected over BLE, on
* a head, and not charging. Fed by the dashboard controller from the
* transport's phase, wear-state and battery reports; consumed by the Timers
* engine, which mirrors it to the phone's alarm clock so the phone knows
* when to add its own sound.
*/
export type GlassesPresence = {
connected: boolean;
/** null until the firmware has reported a wear state this session. */
worn: boolean | null;
charging: boolean;
};
let presence: GlassesPresence = { connected: false, worn: null, charging: false };
const listeners = new Set<(presence: GlassesPresence) => void>();
export function getGlassesPresence(): GlassesPresence {
return presence;
}
/** True when a fresh, positive report says the glasses are worn and usable. */
export function glassesCanCarryAlert(current: GlassesPresence = presence): boolean {
return current.connected && current.worn === true && !current.charging;
}
export function updateGlassesPresence(patch: Partial<GlassesPresence>): void {
const next = { ...presence, ...patch };
if (next.connected === presence.connected && next.worn === presence.worn && next.charging === presence.charging) return;
presence = next;
for (const listener of Array.from(listeners)) {
try {
listener(presence);
} catch (error) {
console.warn(`glasses presence listener failed: ${error}`);
}
}
}
export function onGlassesPresenceChanged(listener: (presence: GlassesPresence) => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
+2
View File
@@ -44,6 +44,7 @@ export type WearRemoteInputKind =
| "long-press"
| "long-press-start"
| "long-press-release"
| "short-then-long-press"
| "wakeword"
| "swipe-left"
| "swipe-right"
@@ -58,6 +59,7 @@ const INPUT_KINDS: readonly WearRemoteInputKind[] = [
"long-press",
"long-press-start",
"long-press-release",
"short-then-long-press",
"wakeword",
"swipe-left",
"swipe-right",
+62 -23
View File
@@ -1,20 +1,36 @@
import { GrayImage, imageFromAsciiArt } from "./image";
const EMPTY_BATTERY_ICON = imageFromAsciiArt(
[
" ################## ",
" #................# ",
" #................###",
" #................###",
" #................###",
" #................###",
" #................###",
" #................###",
" #................# ",
" ################## ",
],
120,
);
/**
* Top-bar battery gauge: a 10px-tall outlined body with a nub on the right,
* filled left-to-right in proportion to the charge. The fill is drawn as
* BATTERY_BAR_COUNT bars of BATTERY_BAR_WIDTH px separated by dimmed
* BATTERY_BAR_GAP-px columns; the body outline is sized from those
* constants so they can be tuned freely. The fill level is continuous (not
* quantized to whole bars): the rightmost lit bar is cut to a partial width.
*/
export const BATTERY_BAR_COUNT = 4;
export const BATTERY_BAR_WIDTH = 3;
export const BATTERY_BAR_GAP = 1;
const OUTLINE_VALUE = 120;
const BAR_VALUE = 190;
/** The separator columns inside the lit portion of the fill. */
const BAR_GAP_VALUE = 70;
const ICON_HEIGHT = 10;
/** Outline stroke plus one clear pixel between it and the fill, per side. */
const BODY_INSET = 2;
const NUB_WIDTH = 2;
const NUB_TOP = 2;
const NUB_HEIGHT = 6;
const FILL_X = BODY_INSET;
const FILL_Y = BODY_INSET;
const FILL_WIDTH = BATTERY_BAR_COUNT * BATTERY_BAR_WIDTH + (BATTERY_BAR_COUNT - 1) * BATTERY_BAR_GAP;
const FILL_HEIGHT = ICON_HEIGHT - 2 * BODY_INSET;
const BODY_WIDTH = FILL_WIDTH + 2 * BODY_INSET;
export const BATTERY_ICON_WIDTH = BODY_WIDTH + NUB_WIDTH;
const BATTERY_BOLT_ICON = imageFromAsciiArt(
[
@@ -29,25 +45,48 @@ const BATTERY_BOLT_ICON = imageFromAsciiArt(
255,
);
export const BATTERY_ICON_WIDTH = EMPTY_BATTERY_ICON.width;
/** The empty outline, built once from the geometry constants. */
const EMPTY_BATTERY_ICON = buildEmptyBattery();
const BATTERY_FILL_X = 3;
const BATTERY_FILL_Y = 2;
const BATTERY_FILL_WIDTH = 14;
const BATTERY_FILL_HEIGHT = 6;
function buildEmptyBattery(): GrayImage {
const icon = new GrayImage(BATTERY_ICON_WIDTH, ICON_HEIGHT, 0);
icon.fillRect(0, 0, BODY_WIDTH, 1, OUTLINE_VALUE);
icon.fillRect(0, ICON_HEIGHT - 1, BODY_WIDTH, 1, OUTLINE_VALUE);
icon.fillRect(0, 0, 1, ICON_HEIGHT, OUTLINE_VALUE);
icon.fillRect(BODY_WIDTH - 1, 0, 1, ICON_HEIGHT, OUTLINE_VALUE);
icon.fillRect(BODY_WIDTH, NUB_TOP, NUB_WIDTH, NUB_HEIGHT, OUTLINE_VALUE);
return icon;
}
export function drawBattery(percentCharge: number, isCharging: boolean): GrayImage {
const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0);
icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0);
const clamped = Math.max(0, Math.min(100, percentCharge));
const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100);
icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190);
const litWidth = Math.round((FILL_WIDTH * clamped) / 100);
drawSegmentedFill(icon, litWidth);
if (isCharging) {
overlayImage(icon, BATTERY_BOLT_ICON, 7, 1, clamped > 50 ? 0 : 255);
const boltX = FILL_X + (((FILL_WIDTH - BATTERY_BOLT_ICON.width) / 2) | 0);
overlayImage(icon, BATTERY_BOLT_ICON, boltX, 1, clamped > 50 ? 0 : 255);
}
return icon;
}
/**
* Light the leftmost `litWidth` columns of the fill area as bars with dim
* separator columns. Columns past the lit width stay clear, so a partial
* charge ends mid-bar rather than snapping to a bar boundary. A separator is
* only drawn between two lit bars: when the lit width ends exactly on one it
* is left clear rather than dangling past the last bar.
*/
function drawSegmentedFill(icon: GrayImage, litWidth: number): void {
const pitch = BATTERY_BAR_WIDTH + BATTERY_BAR_GAP;
for (let column = 0; column < litWidth; column++) {
const isBar = column % pitch < BATTERY_BAR_WIDTH;
if (!isBar && column + BATTERY_BAR_GAP >= litWidth) continue;
icon.fillRect(FILL_X + column, FILL_Y, 1, FILL_HEIGHT, isBar ? BAR_VALUE : BAR_GAP_VALUE);
}
}
function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number, value: number): void {
for (let y = 0; y < source.height; y++) {
for (let x = 0; x < source.width; x++) {
+24 -7
View File
@@ -18,6 +18,8 @@ const ICON_STROKE_WIDTH = 2;
// Lucide icons (MIT/ISC licensed). Kept verbatim so they can be diffed
// against upstream if an icon needs updating.
export const ICON_SVGS = {
"message-circle":
'<svg viewBox="0 0 24 24" fill="none"><path d="M21 11.5a8.4 8.4 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.4 8.4 0 0 1-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.4 8.4 0 0 1 3.8-.9h.5a8.5 8.5 0 0 1 8 8z"/></svg>',
"layout-grid":
'<svg viewBox="0 0 24 24" fill="none"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></svg>',
timer:
@@ -36,6 +38,8 @@ export const ICON_SVGS = {
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 18v4"/><path d="M2 14.499a5.5 5.5 0 0 0 9.591 3.675.6.6 0 0 1 .818.001A5.5 5.5 0 0 0 22 14.5c0-2.29-1.5-4-3-5.5l-5.492-5.312a2 2 0 0 0-3-.02L5 8.999c-1.5 1.5-3 3.2-3 5.5"/></svg>',
terminal:
'<svg viewBox="0 0 24 24" fill="none"><path d="m7 11 2-2-2-2"/><path d="M11 13h4"/><rect width="18" height="18" x="3" y="3" rx="2" ry="2"/></svg>',
"scroll-text":
'<svg viewBox="0 0 24 24" fill="none"><path d="M15 12h-5"/><path d="M15 8h-5"/><path d="M19 17V5a2 2 0 0 0-2-2H4"/><path d="M8 21h12a2 2 0 0 0 2-2v-1a1 1 0 0 0-1-1H11a1 1 0 0 0-1 1v1a2 2 0 1 1-4 0V5a2 2 0 1 0-4 0v2a1 1 0 0 0 1 1h3"/></svg>',
"file-text":
'<svg viewBox="0 0 24 24" fill="none"><path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z"/><path d="M14 2v5a1 1 0 0 0 1 1h5"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></svg>',
file:
@@ -87,9 +91,11 @@ export const ICON_SVGS = {
} as const;
export type IconName = keyof typeof ICON_SVGS;
/** Idle prompt, or the visible/hidden phases of an activity cursor. */
export type IconActivity = "idle" | "on" | "off";
// The "_" element of the terminal icon, swapped out for a session glyph in
// renderIconWithGlyph.
// Prompt and session-marker elements replaced by renderIconWithGlyph.
const TERMINAL_PROMPT = '<path d="m7 11 2-2-2-2"/>';
const TERMINAL_UNDERSCORE = '<path d="M11 13h4"/>';
/**
@@ -155,12 +161,23 @@ export function renderSvgIcon(cacheName: string, svg: string, size: number): Gra
/**
* Render an icon with a glyph character substituted in — currently only the
* terminal icon, whose "_" becomes the glyph (">3" instead of ">_"). Falls
* back to the plain icon for other names or unsupported characters.
* back to the plain marker for unsupported characters. Active terminal icons
* replace the prompt with a blinking cursor, leaving the marker in place.
*/
export function renderIconWithGlyph(name: IconName, glyph: string, size: number): GrayImage | null {
const shape = name === "terminal" ? TERMINAL_GLYPH_SHAPES[glyph] : undefined;
if (!shape) return renderIcon(name, size);
return renderSvgCached(`${name}[${glyph}]`, ICON_SVGS.terminal.replace(TERMINAL_UNDERSCORE, shape), size);
export function renderIconWithGlyph(name: IconName, glyph: string, size: number, activity: IconActivity = "idle"): GrayImage | null {
if (name !== "terminal") return renderIcon(name, size);
const shape = TERMINAL_GLYPH_SHAPES[glyph];
if (!shape && activity === "idle") return renderIcon(name, size);
let svg: string = ICON_SVGS.terminal;
if (shape) svg = svg.replace(TERMINAL_UNDERSCORE, shape);
if (activity !== "idle") {
// IconRenderer strokes all elements at width 2; the narrow rectangle
// therefore becomes a solid cursor without needing per-element fills.
svg = svg.replace(TERMINAL_PROMPT, activity === "on"
? '<rect x="7.5" y="8" width="1" height="6"/>'
: "");
}
return renderSvgCached(`${name}[${shape ? glyph : ""}]:${activity}`, svg, size);
}
function renderSvgCached(cacheName: string, svg: string, size: number): GrayImage | null {
+36
View File
@@ -520,6 +520,37 @@ export class GrayImage {
}
}
/**
* A copy of this image with its brightness scaled by `factor` (0..1):
* raster and glyph / firmware-text draws alike, keeping the draws deferred
* so they stay on the cached-glyph wire path. Transparent (0) pixels stay
* transparent and anything visible stays at least 1 (black), so the
* color-key convention survives. Image draws (icons) are baked into the
* raster first: their sources are immutable, content-addressed cache
* entries, and a dimmed variant of each would only pollute that cache.
*/
dimmed(factor: number): GrayImage {
const scale = Math.max(0, Math.min(1, factor));
const copy = new GrayImage(this.width, this.height, 0);
copy.pixels.set(this.pixels);
for (const placed of this.drawList) {
if (placed.kind === "image") {
copy.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
}
}
const pixels = copy.pixels;
for (let i = 0; i < pixels.length; i++) {
const value = pixels[i]!;
if (value !== 0) pixels[i] = dimValue(value, scale);
}
for (const placed of this.drawList) {
if (placed.kind !== "image") {
copy.drawList.push({ ...placed, value: dimValue(placed.value, scale) });
}
}
return copy;
}
/**
* This image with its deferred draws rasterized into the pixel buffer.
* Returns this image unchanged when there are none, otherwise a baked copy
@@ -612,6 +643,11 @@ function rasterizeGlyph(
}
}
/** Scale a visible (non-zero) brightness, never below 1 (the color-key black). */
function dimValue(value: number, scale: number): number {
return Math.max(1, Math.round(value * scale));
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}
+5
View File
@@ -30,6 +30,11 @@ export function singlePlane(image: GrayImage): Plane[] {
return [{ image, x: 0, y: 0 }];
}
/** The planes with their brightness scaled by `factor` (see GrayImage.dimmed). */
export function dimPlanes(planes: readonly Plane[], factor: number): Plane[] {
return planes.map((plane) => ({ ...plane, image: plane.image.dimmed(factor) }));
}
/**
* Flatten planes into one image: for each plane in order, blend its raster
* (0 = transparent) and then its glyphs on top. A later plane's raster covers
+2 -2
View File
@@ -34,10 +34,10 @@ const MAX_SIZE_GROWTH = 48;
/**
* Guaranteed line-height range of getDefaultSmallFont, which UI layouts may
* rely on (getDefaultMediumFont's range follows: 16..29). The font picker
* disables sizes outside it, and resolution falls back to the bitmap face if
* hides sizes outside it, and resolution falls back to the bitmap face if
* a stored selection somehow lands outside (e.g. the font file changed).
*/
export const SMALL_FONT_MIN_LINE_HEIGHT = 12;
export const SMALL_FONT_MIN_LINE_HEIGHT = 8;
export const SMALL_FONT_MAX_LINE_HEIGHT = 21;
/** Whether a small-font selection at this path+size satisfies the UI bound. */
+154
View File
@@ -0,0 +1,154 @@
import { Utils } from "@nativescript/core";
declare const com: any;
/**
* Bridge to the Java alarm clock (FaceclawAlarms / FaceclawAlarmService):
* the durable phone side of the Timers app's timers and alarms. The phone
* schedules an exact alarm-clock alarm per item, re-arms them after reboots
* and time changes, and when one comes due shows it on the phone (silently
* at first) and adds phone sound if the glasses cannot carry it. Ids are the
* engine's item ids.
*/
export type AlarmKind = "timer" | "alarm";
export type PhoneAlarmAction = { id: number; action: "dismiss" | "snooze"; minutes: number; atMs: number };
export type AlarmReliabilityIssue = { code: string; message: string; fixable: boolean };
function getContext(): android.content.Context | null {
if (!global.isAndroid) return null;
return Utils.android.getApplicationContext() ?? null;
}
/** Schedule (or move) the phone alarm that rings `title` / `text` at triggerAtMs. */
export function schedulePhoneAlarm(
itemId: number,
triggerAtMs: number,
title: string,
text: string,
kind: AlarmKind,
snoozeMinutes: number,
): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.schedule(context, itemId, triggerAtMs, title, text, kind, snoozeMinutes);
}
/** Cancel the pending phone alarm, stop it if ringing, and clear its notification. */
export function cancelPhoneAlarm(itemId: number): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.cancel(context, itemId);
}
/** The engine's own expiry path: start the phone ringing flow (deduplicated against the alarm's). */
export function ringPhoneAlarm(itemId: number, title: string, text: string, kind: AlarmKind, snoozeMinutes: number): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.ring(context, itemId, title, text, kind, snoozeMinutes);
}
/** The glasses are showing the item: the phone starts its 30-second acknowledgement clock. */
export function phoneAlarmDeliveredToGlasses(itemId: number): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.deliveredToGlasses(context, itemId);
}
/** The wearer dealt with the item on the glasses: the phone goes quiet. */
export function acknowledgePhoneAlarm(itemId: number): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.acknowledge(context, itemId);
}
/** Tell the phone whether the glasses could carry an alarm right now. */
export function setPhoneAlarmGlassesStatus(connected: boolean, worn: boolean, charging: boolean): void {
if (!global.isAndroid) return;
com.faceclaw.app.FaceclawAlarms.setGlassesStatus(connected, worn, charging);
}
let retainedListenerProxy: any = null;
const actionListeners = new Set<(action: PhoneAlarmAction) => void>();
/** Live phone-side dismiss / snooze events (journaled ones arrive through drainPhoneAlarmJournal). */
export function onPhoneAlarmAction(listener: (action: PhoneAlarmAction) => void): () => void {
if (retainedListenerProxy === null && global.isAndroid) {
retainedListenerProxy = new com.faceclaw.app.FaceclawAlarmListener({
onPhoneAction: (id: number, action: string, minutes: number) => {
const event: PhoneAlarmAction = {
id: Number(id),
action: String(action) === "snooze" ? "snooze" : "dismiss",
minutes: Number(minutes),
atMs: Date.now(),
};
for (const registered of Array.from(actionListeners)) {
try {
registered(event);
} catch (error) {
console.warn(`phone alarm action listener failed: ${error}`);
}
}
},
});
com.faceclaw.app.FaceclawAlarms.setListener(retainedListenerProxy);
}
actionListeners.add(listener);
return () => {
actionListeners.delete(listener);
};
}
/** Phone-side actions taken while the engine was not listening, oldest first; cleared on read. */
export function drainPhoneAlarmJournal(): PhoneAlarmAction[] {
const context = getContext();
if (!context) return [];
let parsed: any;
try {
parsed = JSON.parse(String(com.faceclaw.app.FaceclawAlarms.drainJournal(context)));
} catch {
return [];
}
if (!Array.isArray(parsed)) return [];
return parsed
.filter((entry) => entry && typeof entry === "object" && Number.isFinite(Number(entry.id)))
.map((entry) => ({
id: Number(entry.id),
action: entry.action === "snooze" ? "snooze" : "dismiss",
minutes: Number(entry.minutes) || 0,
atMs: Number(entry.at) || 0,
}));
}
/** Conditions under which the phone may fail to ring, most serious first. */
export function checkPhoneAlarmReliability(): AlarmReliabilityIssue[] {
const context = getContext();
if (!context) return [];
let parsed: any;
try {
parsed = JSON.parse(String(com.faceclaw.app.FaceclawAlarms.checkReliability(context)));
} catch (error) {
console.warn(`alarm reliability check failed: ${error}`);
return [];
}
if (!Array.isArray(parsed)) return [];
return parsed
.filter((entry) => entry && typeof entry === "object" && typeof entry.code === "string")
.map((entry) => ({ code: entry.code, message: String(entry.message ?? ""), fixable: entry.fixable !== false }));
}
/** Open the system screen where a reliability issue can be fixed. */
export function openPhoneAlarmReliabilityFix(code: string): void {
const context = getContext();
if (!context) return;
com.faceclaw.app.FaceclawAlarms.openReliabilityFix(context, code);
}
/** The phone's ring / miss / action log, one line per entry, newest last. */
export function readPhoneAlarmLog(): string {
const context = getContext();
if (!context) return "";
return String(com.faceclaw.app.FaceclawAlarms.readLog(context));
}
+157
View File
@@ -0,0 +1,157 @@
/**
* Ambient light sensor access through the custom firmware (CFW image-handler
* mode 16, capability token "als16"; see g2flash/patches/als_sensor.c).
*
* The stock firmware only reads its OPT3001 light sensor while auto-brightness
* is on, and then steps the panel brightness itself (visible flicker). The CFW
* adds two things:
*
* - QUERY: one report from the driver's current state.
* - PASSIVE polling: the firmware opens and polls the sensor at our interval,
* the stock adjuster never touches the panel, and a report arrives when the
* reading moved by at least `minDelta` or `heartbeatMs` elapsed. The phone
* then applies brightness itself (setBrightness with autoAdjust=false), e.g.
* only while the display is blank or during a UI transition.
*
* Reports are 24-byte ['A','L',version,...] records on settings field 105.
*/
import { toUint8Array } from "../util/array-util";
declare const com: any;
declare const global: any;
export const ALS_REPORT_VERSION = 1;
export type AmbientLightReason = "query" | "started" | "poll" | "stopped";
export type AmbientLightReport = {
reason: AmbientLightReason;
/** The sensor driver is open (readings are live). */
opened: boolean;
/** CFW passive polling is installed (stock auto-brightness is not stepping). */
passive: boolean;
/** The user's stock auto-brightness setting is on. */
autoBrightness: boolean;
/** The last passive read succeeded (only meaningful while passive). */
readOk: boolean;
/** Stock driver status: 1 start-read, 2 adjusting, 3 polling. */
status: number;
/** Ambient light in the stock driver's units (roughly tenths of a lux). */
alsValue: number;
/** Max of the last five readings (what the stock curve is applied to). */
peak: number;
/** Brightness level (0..100) the stock curve would choose for `peak`. */
stockTarget: number;
/** Current brightness setting (0..100). */
brightness: number;
/** Row of the stock brightness curve (0..5) for `peak`. */
gear: number;
/** Stock learned scale factor, Q10 (1024 = 1.0). */
scaleQ10: number;
/** Firmware millisecond tick when the report was built. */
tickMs: number;
};
const REASONS: AmbientLightReason[] = ["query", "started", "poll", "stopped"];
function rd16(b: Uint8Array, i: number): number {
return b[i] | (b[i + 1] << 8);
}
function rd32(b: Uint8Array, i: number): number {
return (b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)) >>> 0;
}
/** Decode a field-105 record body, or null when it isn't one. */
export function decodeAmbientLightReport(body: Uint8Array): AmbientLightReport | null {
if (body.length < 24 || body[0] !== 0x41 || body[1] !== 0x4c || body[2] !== ALS_REPORT_VERSION) {
return null;
}
const flags = body[4];
return {
reason: REASONS[body[3]] ?? "poll",
opened: (flags & 0x01) !== 0,
passive: (flags & 0x02) !== 0,
autoBrightness: (flags & 0x04) !== 0,
readOk: (flags & 0x08) !== 0,
status: body[5],
alsValue: rd32(body, 6),
peak: rd32(body, 10),
stockTarget: rd16(body, 14),
brightness: body[16],
gear: body[17],
scaleQ10: rd16(body, 18),
tickMs: rd32(body, 20),
};
}
export type AmbientLightPollingOptions = {
/** Poll period in ms; the firmware clamps to 100..5000. Default 500. */
intervalMs?: number;
/** Report when the reading moved by at least this much. Default 1. */
minDelta?: number;
/** Also report after this many ms without one (0 = never). Default 5000. */
heartbeatMs?: number;
/**
* Stop automatically when the Faceclaw framebuffer lease is released or
* lapses (the phone went away). Default true.
*/
bindToLease?: boolean;
};
function activeCommunicator(): any {
if (!global.isAndroid) return null;
try {
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
} catch {
return null;
}
}
/** Ask the firmware for one ambient-light report (delivered to listeners). */
export function queryAmbientLight(): void {
try {
activeCommunicator()?.queryAmbientLight();
} catch (error) {
console.warn(`queryAmbientLight failed: ${error}`);
}
}
/**
* Start or stop CFW passive light-sensor polling. Starting is idempotent and
* re-opens the sensor if the stock firmware closed it, so it is safe to resend
* on every session start.
*/
export function setAmbientLightPolling(enabled: boolean, options: AmbientLightPollingOptions = {}): void {
try {
activeCommunicator()?.setAmbientLightPolling(
enabled,
options.intervalMs ?? 500,
options.minDelta ?? 1,
options.heartbeatMs ?? 5000,
options.bindToLease ?? true,
);
} catch (error) {
console.warn(`setAmbientLightPolling failed: ${error}`);
}
}
/** Subscribe to decoded ambient-light reports. Delivered on the main thread. */
export function addAmbientLightListener(listener: (report: AmbientLightReport) => void): () => void {
const active = activeCommunicator();
if (!active) return () => {};
const proxy = new com.faceclaw.app.FaceclawAmbientLightListener({
onAmbientLight: (body: any) => {
const report = decodeAmbientLightReport(toUint8Array(body));
if (report) listener(report);
},
});
active.addAmbientLightListener(proxy);
return () => {
try {
activeCommunicator()?.removeAmbientLightListener(proxy);
} catch {
// The communicator may have been replaced during a disconnect.
}
};
}
+16
View File
@@ -0,0 +1,16 @@
declare const com: any;
export interface BleTrafficSample {
/** Outbound messages (writeFrames calls) since process start. */
messages: number;
/** Outbound payload bytes (sum of frame sizes) since process start. */
bytes: number;
/** Display frames whose last message was acked, since process start. */
frames: number;
}
/** Running totals of outbound BLE traffic, counted process-wide in FaceclawBleManager. */
export function sampleBleTraffic(): BleTrafficSample {
const raw = com.faceclaw.app.FaceclawBleManager.sampleOutboundTraffic();
return { messages: Number(raw[0]), bytes: Number(raw[1]), frames: Number(raw[2]) };
}
+7
View File
@@ -9,6 +9,9 @@
export type CloudSttTranscriptEvent = {
text: string;
isFinal: boolean;
/** Optional presentation for Transcribe only; dictation uses plain text. */
transcribeText?: string;
paragraphBreakAfter?: boolean;
};
export type CloudSttOptions = {
@@ -16,6 +19,8 @@ export type CloudSttOptions = {
onTranscript: (event: CloudSttTranscriptEvent) => void;
onStatus: (status: string) => void;
onError: (message: string) => void;
onReady?: () => void;
onDisconnected?: (message: string) => void;
};
export interface CloudSttClient {
@@ -25,6 +30,8 @@ export interface CloudSttClient {
acceptPcm(pcm: Uint8Array): void;
/** End of utterance: produce a final transcript. */
finish(): void;
/** Commit a pause boundary while keeping the audio session open. */
commitSegment?(): void;
/** Abandon the session without finalizing. */
stop(): void;
}
+34 -6
View File
@@ -5,10 +5,23 @@ export const COMPASS_CHANGED = 15;
export const COMPASS_CALIBRATION_STARTED = 16;
export const COMPASS_CALIBRATION_COMPLETE = 17;
export type CompassDiagnostics = {
/** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
magneticAccuracy: number;
/** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
magneticAnomalies: number;
/** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
orientationSource: number;
flags: number;
sampleTimeMs: number;
};
export type CompassEvent = {
command: number;
/** Magnetic heading in degrees, or -1 for calibration-only events. */
headingDegrees: number;
/** Absent with stock/older CFW or an unrecognized diagnostic extension. */
diagnostics?: CompassDiagnostics;
};
function activeCommunicator(): any {
@@ -20,22 +33,37 @@ function activeCommunicator(): any {
}
}
/** Request CFW mode 10 to start/stop the stock firmware compass. */
export function setCompassEnabled(enabled: boolean): void {
/**
* Request CFW mode 10 to start/stop the stock firmware compass on behalf of
* `owner`. The magnetometer is shared: it runs while any owner wants it and
* stops once the last one releases it, so apps can't switch each other off.
*/
export function setCompassEnabled(enabled: boolean, owner = "compass"): void {
try {
activeCommunicator()?.setCompassEnabled(enabled);
activeCommunicator()?.setCompassEnabled(owner, enabled);
} catch (error) {
console.warn(`setCompassEnabled failed: ${error}`);
}
}
/** Subscribe to stock compass heading/calibration notifications. */
/**
* Subscribe to stock compass heading/calibration notifications. Events are
* delivered on the calling thread's Looper, so this works from app workers.
*/
export function addCompassListener(listener: (event: CompassEvent) => void): () => void {
const active = activeCommunicator();
if (!active) return () => {};
const proxy = new com.faceclaw.app.FaceclawCompassListener({
onCompassEvent: (command: number, headingDegrees: number) => {
listener({ command: Number(command), headingDegrees: Number(headingDegrees) });
onCompassEvent: (command: number, headingDegrees: number, magneticAccuracy: number,
magneticAnomalies: number, orientationSource: number, diagnosticFlags: number, sampleTimeMs: number) => {
listener({
command: Number(command), headingDegrees: Number(headingDegrees),
diagnostics: diagnosticFlags >= 0 ? {
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags),
sampleTimeMs: Number(sampleTimeMs),
} : undefined,
});
},
});
active.addCompassListener(proxy);
+45 -44
View File
@@ -1,4 +1,4 @@
import { encodeBase64 } from "./cloud-stt";
import { type CloudSttOptions, type CloudSttTranscriptEvent, encodeBase64 } from "./cloud-stt";
declare const com: any;
@@ -18,17 +18,8 @@ const WS_URL = "wss://api.elevenlabs.io/v1/speech-to-text/realtime";
const MODEL_ID = "scribe_v2_realtime";
const SAMPLE_RATE = 16000;
export type ElevenLabsTranscriptEvent = {
text: string;
isFinal: boolean;
};
export type ElevenLabsSttOptions = {
apiKey: string;
onTranscript: (event: ElevenLabsTranscriptEvent) => void;
onStatus: (status: string) => void;
onError: (message: string) => void;
};
export type ElevenLabsTranscriptEvent = CloudSttTranscriptEvent;
export type ElevenLabsSttOptions = CloudSttOptions;
export class ElevenLabsSttClient {
private ws: any = null;
@@ -38,26 +29,32 @@ export class ElevenLabsSttClient {
// PCM that arrived before the socket finished opening; flushed on open.
private readonly pendingChunks: string[] = [];
private latestText = "";
private readonly commits: boolean[] = [];
constructor(private readonly options: ElevenLabsSttOptions) {}
start(): void {
if (this.ws) return;
if (this.closed || this.ws) return;
const url = `${WS_URL}?model_id=${MODEL_ID}&audio_format=pcm_${SAMPLE_RATE}&commit_strategy=manual`;
this.listenerProxy = new com.faceclaw.app.FaceclawWebSocketListener({
onOpen: () => {
if (this.closed) return;
this.open = true;
for (const chunk of this.pendingChunks.splice(0)) {
this.sendChunk(chunk);
}
if (!this.closed) this.options.onReady?.();
},
onTextMessage: (message: string) => {
if (!this.closed) this.handleMessage(String(message));
},
onTextMessage: (message: string) => this.handleMessage(String(message)),
onClosed: () => {
this.open = false;
if (!this.closed) this.options.onDisconnected?.("ElevenLabs connection closed.");
},
onFailure: (message: string) => {
if (this.closed) return;
this.options.onError(`ElevenLabs connection failed: ${String(message)}`);
this.options.onDisconnected?.(`ElevenLabs connection failed: ${String(message)}`);
},
});
try {
@@ -67,7 +64,7 @@ export class ElevenLabsSttClient {
this.ws = new com.faceclaw.app.FaceclawWebSocket(url, this.listenerProxy, "xi-api-key", key);
this.options.onStatus("Connecting to ElevenLabs...");
} catch (error) {
this.options.onError(`ElevenLabs connection failed: ${String((error as Error)?.message ?? error)}`);
this.options.onDisconnected?.(`ElevenLabs connection failed: ${String((error as Error)?.message ?? error)}`);
}
}
@@ -82,24 +79,25 @@ export class ElevenLabsSttClient {
}
}
commitSegment(): void {
this.commit(true);
}
/** End of utterance: flush and commit for a final transcript. */
finish(): void {
this.commit(false);
}
private commit(paragraphBreakAfter: boolean): void {
if (this.closed) return;
const commitMessage = JSON.stringify({
message_type: "input_audio_chunk",
audio_base_64: "",
commit: true,
sample_rate: SAMPLE_RATE,
});
if (this.open) {
this.trySend(commitMessage);
} else {
this.pendingChunks.push("__commit__");
}
this.commits.push(paragraphBreakAfter);
if (this.open) this.sendChunk("__commit__");
else this.pendingChunks.push("__commit__");
}
stop(): void {
this.closed = true;
this.open = false;
if (this.ws) {
try {
this.ws.close(1000, "bye");
@@ -109,28 +107,26 @@ export class ElevenLabsSttClient {
this.ws = null;
}
this.listenerProxy = null;
this.pendingChunks.length = 0;
}
private sendChunk(base64OrCommit: string): void {
if (base64OrCommit === "__commit__") {
this.finish();
return;
}
this.trySend(
JSON.stringify({
message_type: "input_audio_chunk",
audio_base_64: base64OrCommit,
commit: false,
sample_rate: SAMPLE_RATE,
}),
);
const commit = base64OrCommit === "__commit__";
this.trySend(JSON.stringify({
message_type: "input_audio_chunk",
audio_base_64: commit ? "" : base64OrCommit,
commit,
sample_rate: SAMPLE_RATE,
}));
}
private trySend(message: string): void {
try {
this.ws?.sendText(message);
if (this.ws?.sendText(message) === false) {
this.options.onDisconnected?.("ElevenLabs send failed.");
}
} catch (error) {
console.warn("elevenlabs send failed", error);
this.options.onDisconnected?.(`ElevenLabs send failed: ${String(error)}`);
}
}
@@ -150,14 +146,19 @@ export class ElevenLabsSttClient {
this.options.onTranscript({ text: this.latestText, isFinal: false });
return;
case "committed_transcript":
case "committed_transcript_with_timestamps":
this.latestText = String(message.text ?? this.latestText);
this.options.onTranscript({ text: this.latestText, isFinal: true });
this.options.onTranscript({ text: this.latestText, isFinal: true, paragraphBreakAfter: this.commits.shift() ?? false });
this.latestText = "";
return;
case "committed_transcript_with_timestamps":
// The timestamp event follows the plain commit; do not append twice.
return;
case "rate_limited":
this.options.onDisconnected?.("ElevenLabs rate limited.");
return;
case "error":
case "auth_error":
case "quota_exceeded":
case "rate_limited":
case "input_error":
this.options.onError(`ElevenLabs: ${String(message.error ?? message.message_type)}`);
return;
+22 -1
View File
@@ -22,6 +22,8 @@ export type CommunicatorState = {
export type HeadsetBatteryState = {
battery: number;
chargingStatus: number;
ringBattery?: number;
ringChargingStatus?: number;
};
export type FrameMetrics = {
@@ -120,6 +122,11 @@ function nonNegativeNumber(value: number): number {
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
}
/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */
export function dimFactor256(factor: number): number {
return Math.round(Math.max(0, Math.min(1, factor)) * 256);
}
export class FaceclawCommunicatorBridge {
private readonly communicator: any;
private readonly listenerProxy: any;
@@ -184,10 +191,12 @@ export class FaceclawCommunicatorBridge {
frameTimings.logFrame(event.frameId, "input event received on JS side");
this.emitAsync(this.ringListeners, event);
},
onBatteryState: (headsetBattery: number, headsetCharging: number) => {
onBatteryState: (headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number) => {
const state = {
battery: Number(headsetBattery),
chargingStatus: Number(headsetCharging),
ringBattery: Number(ringBattery),
ringChargingStatus: Number(ringCharging),
};
this.emitAsync(this.batteryListeners, state);
},
@@ -524,6 +533,18 @@ export class FaceclawCommunicatorBridge {
});
}
/**
* Dim every compositor surface whose zOrder is below `belowZOrder` to
* `factor` (0..1; 1 = no dimming) of its brightness; takes effect at the
* next composite. How a shell overlay's Layer.dimUnderneath reaches the
* window surfaces beneath the shell surface.
*/
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
await this.enqueueJavaCall(() => {
this.communicator.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
});
}
/** Show or hide a compositor surface; takes effect at the next composite. */
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
await this.enqueueJavaCall(() => {
+33 -4
View File
@@ -6,20 +6,32 @@ export type FlashPromptState =
| "connecting"
| "connected"
| "prompting"
| "battery"
| "result"
| "cancelled"
| "timeout"
| "disconnected"
| "error";
/** Per-arm battery percent; null when that arm didn't answer. */
export type FlashPromptBattery = { right: number | null; left: number | null };
function normalizePercent(value: number): number | null {
const n = Number(value);
return Number.isFinite(n) && n >= 0 ? n : null;
}
/**
* TS wrapper around the native FaceclawFlashPromptCommunicator — the
* stock-firmware-compatible BLE path that shows the pre-flash confirmation on
* the glasses and reports the user's Yes/No choice. Separate from
* FaceclawCommunicatorBridge on purpose (different, minimal protocol subset).
*
* Talks to the right arm only: the lenses relay messages between themselves,
* and acks/events always come from the right arm.
* Connects and authenticates both arms (so any OS pairing prompts happen up
* front, before flashing), shows the prompt via the right arm (the lenses relay
* messages between themselves, and acks/events always come from the right
* arm), then reads each arm's battery. With `skipPrompt` the on-glasses
* confirmation is omitted and only the battery is read.
*/
export class FlashPromptCommunicator {
private readonly communicator: any;
@@ -27,20 +39,32 @@ export class FlashPromptCommunicator {
private readonly logListeners = new Set<(line: string) => void>();
private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>();
private readonly resultListeners = new Set<(approved: boolean) => void>();
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>();
constructor(rightAddress: string, warningText: string) {
constructor(
addresses: { right: string; left: string },
warningText: string,
options?: { skipPrompt?: boolean },
) {
const context = Utils.android.getApplicationContext();
if (!context) throw new Error("Android application context unavailable");
this.communicator = new com.faceclaw.app.FaceclawFlashPromptCommunicator(
context,
rightAddress,
addresses.right,
addresses.left,
warningText,
Boolean(options?.skipPrompt),
);
this.listenerProxy = new com.faceclaw.app.FaceclawFlashPromptListener({
onLog: (line: string) => this.emit(this.logListeners, String(line)),
onState: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? "")),
onBattery: (rightPercent: number, leftPercent: number) =>
this.emit(this.batteryListeners, {
right: normalizePercent(rightPercent),
left: normalizePercent(leftPercent),
}),
onResult: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
});
this.communicator.setListener(this.listenerProxy);
@@ -56,6 +80,11 @@ export class FlashPromptCommunicator {
return () => this.stateListeners.delete(listener);
}
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
this.batteryListeners.add(listener);
return () => this.batteryListeners.delete(listener);
}
onResult(listener: (approved: boolean) => void): () => void {
this.resultListeners.add(listener);
return () => this.resultListeners.delete(listener);
+13 -4
View File
@@ -84,6 +84,7 @@ type TerminalDataKind = "snapshot" | "output";
export class G2MirrorClient {
private ws: any = null;
private listenerProxy: any = null;
private connectionGeneration = 0;
private phase: G2MirrorPhase = "idle";
private status = "Not connected.";
private sessions: G2MirrorSession[] = [];
@@ -168,14 +169,21 @@ export class G2MirrorClient {
return () => this.titleListeners.delete(listener);
}
/** Grid dimensions for the next connection's handshake. */
setViewport(cols: number, rows: number): void {
this.options.cols = cols;
this.options.rows = rows;
}
start(): void {
if (this.ws) return;
this.stopped = false;
const generation = ++this.connectionGeneration;
const url = `${this.options.secure ? "wss" : "ws"}://${this.options.host}:${this.options.port}`;
this.setState("connecting", `Connecting to ${this.options.host}:${this.options.port}...`);
this.listenerProxy = new com.faceclaw.app.FaceclawWebSocketListener({
onOpen: () => {
if (this.stopped) return;
if (this.stopped || generation !== this.connectionGeneration) return;
this.setState("connecting", "Authenticating...");
this.send({
type: "init",
@@ -187,15 +195,15 @@ export class G2MirrorClient {
});
},
onTextMessage: (message: string) => {
if (this.stopped) return;
if (this.stopped || generation !== this.connectionGeneration) return;
this.handleMessage(String(message));
},
onClosed: (code: number, reason: string) => {
if (this.stopped) return;
if (this.stopped || generation !== this.connectionGeneration) return;
this.handleConnectionLost(`Connection closed (${Number(code)}${reason ? `: ${String(reason)}` : ""}).`);
},
onFailure: (message: string) => {
if (this.stopped) return;
if (this.stopped || generation !== this.connectionGeneration) return;
this.handleConnectionLost(`Connection failed: ${shortenError(String(message))}`);
},
});
@@ -209,6 +217,7 @@ export class G2MirrorClient {
stop(): void {
this.stopped = true;
++this.connectionGeneration;
this.rejectAllPendingLaunches("client stopped");
this.clearListRefreshTimer();
if (this.ws) {
+27
View File
@@ -0,0 +1,27 @@
/**
* Magnetic declination from Android's built-in World Magnetic Model. Declination
* is the angle from magnetic north to true north (east positive), so
* `true = magnetic + declination`. No network access is involved; the model is
* baked into the platform and is accurate to well under a degree, which is far
* inside the magnetometer's own noise.
*/
declare const android: any;
declare const global: any;
/** Declination in degrees at a place and time, or null off-Android. */
export function magneticDeclinationDegrees(
latitude: number,
longitude: number,
timestampMs: number = Date.now(),
altitudeMeters = 0,
): number | null {
if (!global.isAndroid) return null;
try {
const field = new android.hardware.GeomagneticField(latitude, longitude, altitudeMeters, timestampMs);
const declination = Number(field.getDeclination());
return Number.isFinite(declination) ? declination : null;
} catch (error) {
console.warn(`GeomagneticField failed: ${error}`);
return null;
}
}
+43 -2
View File
@@ -15,6 +15,39 @@ export function isDecodableImageFile(name: string): boolean {
return DECODABLE_IMAGE.test(name);
}
/**
* How source gray maps onto the display's 16 levels when converting to a
* GrayImage. The default (gamma 1, no dither) is a flat conversion with
* per-pixel rounding, right for UI raster such as icons and map tiles.
*/
export type GrayImageTone = {
/**
* Tone curve out = in^gamma (in 0..1). gamma > 1 darkens midtones: source
* pixels are sRGB-encoded but the G2 drives its levels roughly linearly,
* so untouched photos read washed out.
*/
gamma?: number;
/**
* Floyd-Steinberg error diffusion onto the 16 display levels instead of
* rounding each pixel independently, which bands smooth gradients.
*/
dither?: boolean;
};
/**
* Gamma for continuous-tone images (photos, album art): 2.2 undoes the sRGB
* encoding outright. Lower it toward ~1.8 if shadows crush.
*/
export const PHOTO_GAMMA = 2.2;
/** Tone preset for continuous-tone images: sRGB gamma plus dithering. */
export const PHOTO_TONE: Readonly<Required<GrayImageTone>> = { gamma: PHOTO_GAMMA, dither: true };
/** Resolve a tone to the (gamma, dither) pair the Java helpers take. */
export function resolveGrayImageTone(tone?: GrayImageTone): { gamma: number; dither: boolean } {
return { gamma: tone?.gamma ?? 1, dither: tone?.dither ?? false };
}
/**
* Decode a gray packet ([widthLo, widthHi, heightLo, heightHi, pixels...],
* the format the Java helpers return) into a GrayImage; null when empty or
@@ -34,12 +67,20 @@ export function grayImageFromPacket(raw: ArrayLike<number> | null | undefined):
/**
* Load an image file as grayscale, downscaled to fit maxWidth x maxHeight
* (aspect preserved, never upscaled); null when unreadable or undecodable.
* Pass PHOTO_TONE for continuous-tone images; the default is a flat
* conversion for UI raster.
*/
export function loadImageFileAsGray(path: string, maxWidth: number, maxHeight: number): GrayImage | null {
export function loadImageFileAsGray(
path: string,
maxWidth: number,
maxHeight: number,
tone?: GrayImageTone,
): GrayImage | null {
if (!global.isAndroid) return null;
try {
const { gamma, dither } = resolveGrayImageTone(tone);
return grayImageFromPacket(
com.faceclaw.app.ImageFileLoader.loadGray(path, Math.round(maxWidth), Math.round(maxHeight)),
com.faceclaw.app.ImageFileLoader.loadGray(path, Math.round(maxWidth), Math.round(maxHeight), gamma, dither),
);
} catch (error) {
console.warn(`loadImageFileAsGray failed for ${path}: ${error}`);
+49 -9
View File
@@ -18,6 +18,23 @@ const DIRECTIONS_ROOT = "https://api.mapbox.com/directions/v5";
const STATIC_ROOT = "https://api.mapbox.com/styles/v1";
/** Classic dark style; readable after gray conversion. Swappable for a custom Studio style later. */
const MAP_STYLE = "mapbox/dark-v11";
/**
* Render-time style override: a black background inserted beneath the road
* stack so land, landuse, water, and building fills all come out black on the
* lens (dark-v11's own fills are dark gray, which the lens shows as a lit
* haze). Static Images allows one added layer per request; `before_layer`
* must name a layer in the style, so candidates are tried in order and a
* rejected one is skipped for the rest of the session, ending with the plain
* style (the worker's levelling pass then does what it can client-side).
*/
const STYLE_OVERRIDE_LAYER = JSON.stringify({
id: "faceclaw-blackout",
type: "background",
paint: { "background-color": "#000000" },
});
/** First road layer of dark-v11, then a layer common to every Streets-based style. */
const STYLE_OVERRIDE_BEFORE_LAYERS = ["tunnel-path-trail", "building"];
let styleOverrideIndex = 0;
const FETCH_TIMEOUT_MS = 15_000;
export type RouteProfile = "driving" | "walking" | "cycling";
@@ -188,18 +205,32 @@ export async function fetchStaticMapGray(options: {
: `${round6(options.camera.longitude)},${round6(options.camera.latitude)},` +
`${options.camera.zoom.toFixed(2)},${Math.round(normalizeBearing(options.camera.bearingDeg))}`;
const size = `${Math.round(options.width)}x${Math.round(options.height)}`;
const params = queryString({
access_token: requireToken(),
logo: "false",
attribution: "false",
});
const url = `${STATIC_ROOT}/${MAP_STYLE}/static/${overlay}${camera}/${size}?${params}`;
let bytes: ArrayBuffer;
for (;;) {
const beforeLayer = STYLE_OVERRIDE_BEFORE_LAYERS[styleOverrideIndex];
const params: Record<string, string> = {
access_token: requireToken(),
logo: "false",
attribution: "false",
};
if (beforeLayer !== undefined) {
params.addlayer = STYLE_OVERRIDE_LAYER;
params.before_layer = beforeLayer;
}
const url = `${STATIC_ROOT}/${MAP_STYLE}/static/${overlay}${camera}/${size}?${queryString(params)}`;
const response = await fetchWithTimeout(url, "Mapbox static map");
if (!response.ok) {
const response = await fetchWithTimeout(url, "Mapbox static map");
if (response.ok) {
bytes = await response.arrayBuffer();
break;
}
if (beforeLayer !== undefined && isStyleOverrideRejection(response.status)) {
console.warn(`Mapbox rejected the map style override (before_layer=${beforeLayer}, HTTP ${response.status}); falling back`);
styleOverrideIndex++;
continue;
}
throw new Error(`Mapbox static map failed (HTTP ${response.status}).`);
}
const bytes = await response.arrayBuffer();
if (!global.isAndroid) throw new Error("Map decoding is only available on Android.");
const image = grayImageFromPacket(
com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, Math.round(options.width), Math.round(options.height)),
@@ -281,6 +312,15 @@ async function fetchWithTimeout(url: string, label: string): Promise<Response> {
}
}
/**
* A client error that plausibly blames the added layer (an unknown
* before_layer, or a style without it), as opposed to token or quota trouble
* that the plain request would hit just the same.
*/
function isStyleOverrideRejection(status: number): boolean {
return status >= 400 && status < 500 && status !== 401 && status !== 403 && status !== 429;
}
function normalizeBearing(bearingDeg: number): number {
return ((bearingDeg % 360) + 360) % 360;
}
+83
View File
@@ -0,0 +1,83 @@
import { getStringSetting, setStringSetting } from "./settings-store";
export const MEDIA_APPS_KEY = "music.apps";
// Exact package IDs only: browsers, social feeds and messaging apps commonly
// publish sessions for embedded clips. Users can explicitly enable any of them.
export const DEFAULT_IGNORED_MEDIA_APPS: readonly string[] = [
"com.android.chrome",
"org.mozilla.firefox",
"com.microsoft.emmx",
"com.sec.android.app.sbrowser",
"com.facebook.katana",
"com.facebook.orca",
"com.instagram.android",
"com.whatsapp",
"org.telegram.messenger",
"com.snapchat.android",
"com.reddit.frontpage",
"com.twitter.android",
"com.discord",
];
export type MediaApp = {
packageName: string;
appName: string;
/** Absent until the user chooses; built-in defaults can evolve independently. */
enabled?: boolean;
};
/** Keep discovered apps after their sessions end, including ignored apps. */
export function readMediaApps(): MediaApp[] {
try {
const parsed = JSON.parse(getStringSetting(MEDIA_APPS_KEY, "[]"));
if (!Array.isArray(parsed)) return [];
const apps = new Map<string, MediaApp>();
for (const app of parsed) {
if (!app || typeof app.packageName !== "string" || !app.packageName) continue;
apps.set(app.packageName, {
packageName: app.packageName,
appName: typeof app.appName === "string" && app.appName ? app.appName : app.packageName,
...(typeof app.enabled === "boolean" ? { enabled: app.enabled } : {}),
});
}
return Array.from(apps.values());
} catch {
return [];
}
}
export function rememberMediaApps(discovered: { packageName: string; appName: string }[]): void {
const apps = new Map(readMediaApps().map((app) => [app.packageName, app]));
let changed = false;
for (const app of discovered) {
if (!app.packageName) continue;
const previous = apps.get(app.packageName);
const appName = app.appName || previous?.appName || app.packageName;
if (!previous || previous.appName !== appName) {
apps.set(app.packageName, { ...previous, packageName: app.packageName, appName });
changed = true;
}
}
if (changed) setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
}
export function ignoredMediaPackages(): string[] {
const ignored = new Set(DEFAULT_IGNORED_MEDIA_APPS);
for (const app of readMediaApps()) {
if (app.enabled === true) ignored.delete(app.packageName);
else if (app.enabled === false) ignored.add(app.packageName);
}
return Array.from(ignored).sort();
}
export function isMediaAppEnabled(packageName: string): boolean {
return !ignoredMediaPackages().includes(packageName);
}
export function setMediaAppEnabled(app: MediaApp, enabled: boolean): void {
if (!app.packageName) return;
const apps = new Map(readMediaApps().map((item) => [item.packageName, item]));
apps.set(app.packageName, { packageName: app.packageName, appName: app.appName || app.packageName, enabled });
setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
}
+24 -2
View File
@@ -1,4 +1,6 @@
import { Utils } from "@nativescript/core";
import { ignoredMediaPackages, isMediaAppEnabled, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
import { onSettingsStoreChanged } from "./settings-store";
declare const com: any;
@@ -34,11 +36,12 @@ export class FaceclawMediaBrowserBridge {
private readonly pendingConnects = new Map<number, PendingRequest<string>>();
private readonly pendingBrowses = new Map<number, PendingRequest<MediaBrowseItem[]>>();
private appsCache: MediaBrowserApp[] | null = null;
private connectedPackage = "";
/** Installed apps exposing a media browser service; cached after the first call. */
listBrowsableApps(refresh = false): MediaBrowserApp[] {
if (!global.isAndroid) return [];
if (this.appsCache && !refresh) return this.appsCache;
if (this.appsCache && !refresh) return this.enabledApps();
this.ensureBrowser();
if (!this.browser) return [];
try {
@@ -52,11 +55,17 @@ export class FaceclawMediaBrowserBridge {
serviceClass: String(app.serviceClass ?? ""),
appName: String(app.appName ?? ""),
}));
rememberMediaApps(this.appsCache);
} catch (error) {
console.warn(`media browser app list parse failed: ${error}`);
this.appsCache = [];
}
return this.appsCache;
return this.enabledApps();
}
private enabledApps(): MediaBrowserApp[] {
const ignored = new Set(ignoredMediaPackages());
return (this.appsCache ?? []).filter((app) => !ignored.has(app.packageName));
}
/**
@@ -65,9 +74,11 @@ export class FaceclawMediaBrowserBridge {
*/
connect(app: MediaBrowserApp): Promise<string> {
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
if (!isMediaAppEnabled(app.packageName)) return Promise.reject(new Error("App is ignored in Music settings"));
this.ensureBrowser();
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
const requestId = this.nextRequestId++;
this.connectedPackage = app.packageName;
return new Promise<string>((resolve, reject) => {
this.pendingConnects.set(requestId, { resolve, reject });
this.browser.connect(requestId, app.packageName, app.serviceClass);
@@ -77,6 +88,10 @@ export class FaceclawMediaBrowserBridge {
/** Fetch one level of the connected service's content tree. */
browse(parentId: string): Promise<MediaBrowseItem[]> {
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
if (this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
this.disconnect();
return Promise.reject(new Error("App is ignored in Music settings"));
}
this.ensureBrowser();
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
const requestId = this.nextRequestId++;
@@ -89,6 +104,7 @@ export class FaceclawMediaBrowserBridge {
/** Start playback of a browsed item through the connected player's session. */
playFromMediaId(mediaId: string): void {
if (!global.isAndroid) return;
if (!this.connectedPackage || !isMediaAppEnabled(this.connectedPackage)) return;
this.browser?.playFromMediaId(mediaId);
}
@@ -96,6 +112,7 @@ export class FaceclawMediaBrowserBridge {
disconnect(): void {
if (!global.isAndroid) return;
this.rejectAllPending("Disconnected");
this.connectedPackage = "";
this.browser?.disconnect();
}
@@ -115,6 +132,11 @@ export class FaceclawMediaBrowserBridge {
throw new Error("Android application context unavailable");
}
this.browser = new com.faceclaw.app.FaceclawMediaBrowser(context);
onSettingsStoreChanged((key) => {
if (key === MEDIA_APPS_KEY && this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
this.disconnect();
}
});
this.listenerProxy = new com.faceclaw.app.FaceclawMediaBrowserListener({
onConnectResult: (requestId: number, connected: boolean, rootId: string, error: string) => {
const pending = this.pendingConnects.get(Number(requestId));
+22 -13
View File
@@ -1,17 +1,11 @@
import { Utils } from "@nativescript/core";
import { GrayImage } from "../graphics/image";
import { grayImageFromPacket } from "./image-files";
import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
import { ignoredMediaPackages, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
import { onSettingsStoreChanged } from "./settings-store";
declare const com: any;
/**
* Tone curve applied to album art before it is dithered down to the display's
* 16 gray levels (out = in^gamma, in 0..1). Cover art is sRGB-encoded but the
* G2 drives its levels roughly linearly, so untouched art reads washed out;
* 2.2 undoes the sRGB encoding. Lower it toward ~1.8 if shadows crush.
*/
const ALBUM_ART_GAMMA = 2.2;
export type MediaQueueItem = {
id: number;
title: string;
@@ -141,16 +135,16 @@ export class FaceclawMediaControllerBridge {
}
/**
* Album art for the current item, grayscale (gamma-corrected and dithered
* onto the display's 16 levels), scaled to fit maxSize; null when the
* player provides none.
* Album art for the current item, grayscale with the shared photo tone
* (gamma-corrected and dithered onto the display's 16 levels), scaled to
* fit maxSize; null when the player provides none.
*/
getAlbumArt(maxSize: number): GrayImage | null {
if (!global.isAndroid) return null;
this.ensureController();
if (!this.controller) return null;
return grayImageFromPacket(
this.controller.getAlbumArtGray(Math.round(maxSize), ALBUM_ART_GAMMA),
this.controller.getAlbumArtGray(Math.round(maxSize), PHOTO_TONE.gamma, PHOTO_TONE.dither),
);
}
@@ -181,7 +175,22 @@ export class FaceclawMediaControllerBridge {
throw new Error("Android application context unavailable");
}
this.controller = new com.faceclaw.app.FaceclawMediaController(context);
// Install defaults and saved overrides before starting session discovery.
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
onSettingsStoreChanged((key) => {
if (key === MEDIA_APPS_KEY) {
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
}
});
this.listenerProxy = new com.faceclaw.app.FaceclawMediaControllerListener({
onSessionAppsChanged: (appsJson: string) => {
try {
const apps = JSON.parse(String(appsJson));
rememberMediaApps(apps);
} catch (error) {
console.warn(`media session apps parse failed: ${error}`);
}
},
onStateChange: (
playbackState: string,
packageName: string,
+13 -9
View File
@@ -1,4 +1,4 @@
import { loadNightscoutSettings, nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
import { fetchWithUserAgent } from "../util/http";
export type NightscoutPoint = {
@@ -33,6 +33,10 @@ export type NightscoutState = {
iob: number | null;
cob: number | null;
cageTimestampMs: number | null;
reservoirUnits: number | null;
batteryVoltage: number | null;
loopTimestampMs: number | null;
/** Pump communication age and operating state; measurements are separate. */
pumpStatus: string;
openapsStatusShort: string;
carbs: NightscoutCarbEvent[];
@@ -121,6 +125,9 @@ const DEFAULT_NIGHTSCOUT_STATE: NightscoutState = {
iob: null,
cob: null,
cageTimestampMs: null,
reservoirUnits: null,
batteryVoltage: null,
loopTimestampMs: null,
pumpStatus: "",
openapsStatusShort: "--",
carbs: [],
@@ -259,6 +266,9 @@ export class NightscoutBridge {
iob: Number.isFinite(iob) ? iob : null,
cob,
cageTimestampMs: latestSiteChange > 0 ? latestSiteChange : null,
reservoirUnits: firstFiniteNumber(latestDeviceStatus?.pump?.reservoir),
batteryVoltage: firstFiniteNumber(latestDeviceStatus?.pump?.battery?.voltage),
loopTimestampMs: loopTimestampMs > 0 ? loopTimestampMs : null,
pumpStatus,
openapsStatusShort: loopTimestampMs > 0 ? formatElapsedShort(nowMs - loopTimestampMs) : "--",
carbs: parsedTreatments.carbs,
@@ -359,6 +369,8 @@ function parseTimestampMs(value: unknown): number {
function firstFiniteNumber(...values: unknown[]): number | null {
for (const value of values) {
if (typeof value !== "number" && typeof value !== "string") continue;
if (typeof value === "string" && !value.trim()) continue;
const numeric = Number(value);
if (Number.isFinite(numeric)) {
return numeric;
@@ -371,8 +383,6 @@ function formatPumpStatus(status: NightscoutDeviceStatusResponse | undefined, no
if (!status?.pump) {
return "--";
}
const reservoir = firstFiniteNumber(status.pump.reservoir);
const voltage = firstFiniteNumber(status.pump.battery?.voltage);
const commTimestampMs = Math.max(
parseTimestampMs(status.pump.status?.timestamp),
parseTimestampMs(status.pump.clock),
@@ -380,12 +390,6 @@ function formatPumpStatus(status: NightscoutDeviceStatusResponse | undefined, no
);
const stateLabel = summarizePumpState(status);
const parts: string[] = [];
if (reservoir !== null) {
parts.push(`${Math.round(reservoir)}U`);
}
if (voltage !== null) {
parts.push(`${voltage.toFixed(2)}v`);
}
if (commTimestampMs > 0) {
parts.push(`${formatElapsedShort(nowMs - commTimestampMs)} ago`);
}
+6 -1
View File
@@ -1,10 +1,13 @@
import { GrayImage } from "../graphics/image";
import { logCurrent, spanCurrent } from "./frame-timings";
import { toUint8Array } from "../util/array-util";
import { rememberNotificationSources } from "./notification-sources";
declare const com: any;
const ICON_SIZE = 24;
/** Ask the native listener for every active source, including those below the list's display limit. */
export const ALL_NOTIFICATIONS = 0x7fffffff;
// Backstop TTL only: the icon caches are invalidated eagerly whenever a
// notification is posted or dismissed (see invalidateIconCaches callers), so
// the tray is kept fresh by invalidation, not by expiry. A short TTL just
@@ -158,7 +161,9 @@ export function readActiveNotifications(maxNotifications = 50): AndroidNotificat
);
const parsed = JSON.parse(json);
if (!Array.isArray(parsed)) return [];
return parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item));
const notifications = parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item));
rememberNotificationSources(notifications);
return notifications;
} catch {
return [];
}
+59
View File
@@ -0,0 +1,59 @@
import { getStringSetting, setStringSetting } from "./settings-store";
const SOURCES_KEY = "notifications.sources";
export type NotificationSource = {
packageName: string;
appName: string;
showOnGlasses: boolean;
};
/** Sources outlive their active notifications so a muted app can always be re-enabled. */
export function readNotificationSources(): NotificationSource[] {
try {
const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]"));
if (!Array.isArray(parsed)) return [];
return parsed.filter((source) => source && typeof source.packageName === "string" && source.packageName)
.map((source) => ({
packageName: source.packageName,
appName: typeof source.appName === "string" && source.appName ? source.appName : source.packageName,
showOnGlasses: source.showOnGlasses !== false,
}));
} catch {
return [];
}
}
export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void {
const sources = new Map(readNotificationSources().map((source) => [source.packageName, source]));
let changed = false;
for (const notification of notifications) {
if (!notification.packageName) continue;
const previous = sources.get(notification.packageName);
const appName = notification.appName || previous?.appName || notification.packageName;
if (!previous || previous.appName !== appName) {
sources.set(notification.packageName, {
packageName: notification.packageName,
appName,
showOnGlasses: previous?.showOnGlasses ?? true,
});
changed = true;
}
}
if (changed) setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
}
export function shouldShowNotificationOnGlasses(packageName: string): boolean {
return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses ?? true;
}
export function setNotificationSourceEnabled(source: { packageName: string; appName: string }, enabled: boolean): void {
if (!source.packageName) return;
const sources = new Map(readNotificationSources().map((item) => [item.packageName, item]));
sources.set(source.packageName, {
packageName: source.packageName,
appName: source.appName || source.packageName,
showOnGlasses: enabled,
});
setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
}
+74 -16
View File
@@ -30,28 +30,34 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
// Base64 audio (or the commit sentinel) queued until the socket opens.
private readonly pendingChunks: string[] = [];
private readonly upsampler = new PcmUpsampler();
private latestText = "";
private readonly items = new Map<string, { text: string; final: boolean; paragraphBreakAfter: boolean }>();
private readonly commits: boolean[] = [];
private committed = false;
constructor(private readonly options: OpenAiSttOptions) {}
start(): void {
if (this.ws) return;
if (this.closed || this.ws) return;
this.listenerProxy = new com.faceclaw.app.FaceclawWebSocketListener({
onOpen: () => {
if (this.closed) return;
this.open = true;
this.trySend(JSON.stringify(sessionConfig()));
for (const chunk of this.pendingChunks.splice(0)) {
this.sendChunk(chunk);
}
if (!this.closed) this.options.onReady?.();
},
onTextMessage: (message: string) => {
if (!this.closed) this.handleMessage(String(message));
},
onTextMessage: (message: string) => this.handleMessage(String(message)),
onClosed: () => {
this.open = false;
if (!this.closed) this.options.onDisconnected?.("OpenAI connection closed.");
},
onFailure: (message: string) => {
if (this.closed) return;
this.options.onError(`OpenAI connection failed: ${String(message)}`);
this.options.onDisconnected?.(`OpenAI connection failed: ${String(message)}`);
},
});
try {
@@ -63,7 +69,7 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
);
this.options.onStatus("Connecting to OpenAI...");
} catch (error) {
this.options.onError(`OpenAI connection failed: ${String((error as Error)?.message ?? error)}`);
this.options.onDisconnected?.(`OpenAI connection failed: ${String((error as Error)?.message ?? error)}`);
}
}
@@ -80,9 +86,19 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
}
}
commitSegment(): void {
this.commit(true);
}
/** End of utterance: commit the buffer for a final transcript. */
finish(): void {
this.committed = true;
this.commit(false);
}
private commit(paragraphBreakAfter: boolean): void {
if (this.closed) return;
this.commits.push(paragraphBreakAfter);
if (this.open) {
this.sendChunk("__commit__");
} else {
@@ -92,6 +108,7 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
stop(): void {
this.closed = true;
this.open = false;
if (this.ws) {
try {
this.ws.close(1000, "bye");
@@ -101,11 +118,11 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
this.ws = null;
}
this.listenerProxy = null;
this.pendingChunks.length = 0;
}
private sendChunk(base64OrCommit: string): void {
if (base64OrCommit === "__commit__") {
this.committed = true;
this.trySend(JSON.stringify({ type: "input_audio_buffer.commit" }));
return;
}
@@ -114,9 +131,11 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
private trySend(message: string): void {
try {
this.ws?.sendText(message);
if (this.ws?.sendText(message) === false) {
this.options.onDisconnected?.("OpenAI send failed.");
}
} catch (error) {
console.warn("openai send failed", error);
this.options.onDisconnected?.(`OpenAI send failed: ${String(error)}`);
}
}
@@ -132,23 +151,62 @@ export class OpenAiRealtimeSttClient implements CloudSttClient {
case "session.updated":
this.options.onStatus("Listening (OpenAI)...");
return;
case "conversation.item.input_audio_transcription.delta":
this.latestText += String(message.delta ?? "");
this.options.onTranscript({ text: this.latestText, isFinal: false });
case "input_audio_buffer.committed": {
this.item(String(message.item_id)).paragraphBreakAfter = this.commits.shift() ?? false;
return;
case "conversation.item.input_audio_transcription.completed":
this.latestText = String(message.transcript ?? this.latestText);
this.options.onTranscript({ text: this.latestText, isFinal: true });
// The utterance is done; nothing further is coming after the commit.
if (this.committed) this.stop();
}
case "conversation.item.input_audio_transcription.delta": {
this.item(String(message.item_id)).text += String(message.delta ?? "");
this.emitItems();
return;
}
case "conversation.item.input_audio_transcription.completed": {
const item = this.item(String(message.item_id));
item.text = String(message.transcript ?? item.text);
item.final = true;
this.emitItems();
if (this.committed && !this.items.size && !this.commits.length) this.stop();
return;
}
case "error":
if (message.error?.code === "rate_limit_exceeded" || message.error?.type === "server_error") {
this.options.onDisconnected?.(`OpenAI: ${String(message.error?.message)}`);
return;
}
this.options.onError(`OpenAI: ${String(message.error?.message ?? "unknown error")}`);
return;
default:
return;
}
}
private item(id: string): { text: string; final: boolean; paragraphBreakAfter: boolean } {
let item = this.items.get(id);
if (!item) {
item = { text: "", final: false, paragraphBreakAfter: false };
this.items.set(id, item);
}
return item;
}
private emitItems(): void {
// Committed notifications precede transcription events, so Map insertion
// order is audio order even when completion events arrive out of order.
for (const [id, item] of this.items) {
if (!item.final) break;
this.items.delete(id);
this.options.onTranscript({ text: item.text, isFinal: true, paragraphBreakAfter: item.paragraphBreakAfter });
}
if (this.items.size) {
const items = [...this.items.values()];
const text = items.map((item) => item.text.trim()).filter(Boolean).join(" ");
const transcribeText = items.map((item, index) =>
(index && items[index - 1]!.paragraphBreakAfter ? "\n" : index ? " " : "") + item.text.trim(),
).join("");
this.options.onTranscript({ text, transcribeText, isFinal: false });
}
}
}
function sessionConfig(): object {
+30
View File
@@ -0,0 +1,30 @@
import { Application, isAndroid } from "@nativescript/core";
import { getStringSetting, onSettingsStoreChanged } from "./settings-store";
// Shared with phoneRotationSetting; keep startup independent of the glasses UI.
const ROTATION_KEY = "phone.rotation";
/** Installed once on the UI thread; also covers navigation and activity recreation. */
export function registerPhoneRotation(): void {
if (!isAndroid) return;
const apply = (activity = Application.android.foregroundActivity ?? Application.android.startActivity): void => {
if (!activity) return;
const orientations = android.content.pm.ActivityInfo;
const requested = {
auto: orientations.SCREEN_ORIENTATION_UNSPECIFIED,
portrait: orientations.SCREEN_ORIENTATION_PORTRAIT,
landscape: orientations.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
}[getStringSetting(ROTATION_KEY, "auto")] ?? orientations.SCREEN_ORIENTATION_UNSPECIFIED;
if (activity.getRequestedOrientation() !== requested) {
activity.setRequestedOrientation(requested);
}
};
Application.android.on(Application.android.activityCreatedEvent, (args) => apply(args.activity));
Application.android.on(Application.android.activityResumedEvent, (args) => apply(args.activity));
onSettingsStoreChanged((key) => {
if (key === ROTATION_KEY) apply();
});
apply();
}
+6 -1
View File
@@ -1,5 +1,5 @@
import { ImageSource, Utils } from "@nativescript/core";
import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator";
import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator";
declare const com: any;
declare const java: any;
@@ -19,6 +19,7 @@ export type DisplayTarget = Pick<
| "configureSurface"
| "removeSurface"
| "setSurfaceVisible"
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "waitForFrameFinished"
@@ -96,6 +97,10 @@ export class PreviewDisplayTarget implements DisplayTarget {
this.compositor.setSurfaceVisible(id, Boolean(visible));
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
this.compositor.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
}
async setScreenBlanked(blanked: boolean): Promise<void> {
this.compositor.setScreenBlanked(Boolean(blanked));
}
+141
View File
@@ -0,0 +1,141 @@
import type { CloudSttClient, CloudSttOptions, CloudSttTranscriptEvent } from "./cloud-stt";
import { SpeechPauseDetector } from "./speech-pause";
// Ten seconds of unsent PCM. Never replay audio already sent: that would
// duplicate words already shown. An extended outage drops the oldest audio.
const MAX_PENDING_BYTES = 16000 * 2 * 10;
/** Owns retries and audio buffering independently of the provider protocol. */
export class ReconnectingSttClient implements CloudSttClient {
private client: CloudSttClient | null = null;
private generation = 0;
private stopped = false;
private finishing = false;
private ready = false;
private retry: ReturnType<typeof setTimeout> | null = null;
private finishTimeout: ReturnType<typeof setTimeout> | null = null;
private attempt = 0;
private pending: Uint8Array[] = [];
private pendingBytes = 0;
private latest: CloudSttTranscriptEvent | null = null;
private readonly pause = new SpeechPauseDetector();
constructor(
private readonly create: (options: CloudSttOptions) => CloudSttClient,
private readonly options: CloudSttOptions,
private readonly continuous: () => boolean,
) {}
start(): void {
if (this.stopped || this.client || this.retry !== null) return;
const generation = ++this.generation;
const current = () => !this.stopped && generation === this.generation;
this.client = this.create({
...this.options,
onReady: () => {
if (!current()) return;
this.ready = true;
const chunks = this.pending;
this.pending = [];
this.pendingBytes = 0;
for (const chunk of chunks) this.acceptPcm(chunk);
if (this.finishing) this.client?.finish();
},
onTranscript: (event) => {
if (!current()) return;
// A handshake alone can immediately be followed by another failure.
this.attempt = 0;
this.latest = event.isFinal ? null : event;
this.options.onTranscript(event);
},
onStatus: (status) => { if (current()) this.options.onStatus(status); },
onError: (message) => {
if (!current()) return;
this.stop();
this.options.onError(message);
},
onDisconnected: (message) => {
if (current()) this.disconnected(message);
},
});
try {
this.client.start();
} catch (error) {
this.disconnected(`Transcription connection failed: ${String(error)}`);
}
}
acceptPcm(pcm: Uint8Array): void {
if (this.stopped || !pcm.length) return;
if (!this.ready) {
const copy = pcm.slice(-MAX_PENDING_BYTES);
this.pending.push(copy);
this.pendingBytes += copy.length;
while (this.pendingBytes > MAX_PENDING_BYTES) this.pendingBytes -= this.pending.shift()!.length;
return;
}
this.client?.acceptPcm(pcm);
// A rejected send synchronously reports disconnection. Keep that packet
// with the unsent audio instead of losing it at the outage boundary.
if (!this.ready && !this.stopped) this.acceptPcm(pcm);
if (this.ready && !this.finishing && this.continuous() && this.pause.accept(pcm)) this.client?.commitSegment?.();
}
finish(): void {
if (this.stopped || this.finishing) return;
this.finishing = true;
// Releasing the last mic holder must not leave a reconnect loop alive.
if (this.retry !== null) {
this.preservePartial();
this.stop();
return;
}
this.finishTimeout = setTimeout(() => this.stop(), 5000);
if (this.ready) this.client?.finish();
}
stop(): void {
this.stopped = true;
++this.generation;
if (this.retry !== null) clearTimeout(this.retry);
if (this.finishTimeout !== null) clearTimeout(this.finishTimeout);
this.retry = this.finishTimeout = null;
this.client?.stop();
this.client = null;
this.ready = false;
this.pending = [];
this.pendingBytes = 0;
this.latest = null;
}
private preservePartial(): void {
if (this.latest?.text.trim()) this.options.onTranscript({ ...this.latest, isFinal: true });
this.latest = null;
}
private disconnected(message: string): void {
if (this.stopped) return;
++this.generation; // Ignore late finals / closes from the abandoned socket.
this.ready = false;
this.client?.stop();
this.client = null;
this.pause.reset();
this.preservePartial();
if (this.stopped) return; // A transcript listener can release capture.
if (/\b(401|402|403)\b/.test(message)) {
this.stop();
this.options.onError(message);
return;
}
if (this.finishing) {
this.stop();
return;
}
const delay = Math.min(1000 * 2 ** Math.min(this.attempt++, 5), 30000);
this.options.onStatus(`Connection lost. Reconnecting in ${delay / 1000}s...`);
this.retry = setTimeout(() => {
this.retry = null;
this.start();
}, delay);
}
}
Loaded 100 of 198 files, more files were not shown because too many files have changed in this diff. Show more