forked from Blendman974/faceclaw
Merge main into ios-port and update the iOS CFW transport
This commit is contained in:
commit
e8e144f3f2
156 files changed
+10528
-1679
No files matched your search
+10
-1
@@ -52,7 +52,8 @@ TextField {
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
.system-name-modal TextField {
|
||||
.system-name-modal TextField,
|
||||
.system-name-modal TextView {
|
||||
background-color: #ffffff;
|
||||
color: #222222;
|
||||
}
|
||||
@@ -132,6 +133,14 @@ Button.-primary {
|
||||
border-radius: 8;
|
||||
}
|
||||
|
||||
/* The modal stays light in dark mode (setting editor and keyboard-input
|
||||
* panel); the .ns-dark pairing outguns the theme's dark-mode recolour of
|
||||
* Label/.h2 so the title doesn't go white-on-light-gray. */
|
||||
.system-name-modal Label,
|
||||
.ns-dark .system-name-modal Label {
|
||||
color: #222222;
|
||||
}
|
||||
|
||||
/* Modal behind the action bar's warning triangle: the problem-status warnings
|
||||
* that used to sit inline on the main page, each with its fix-it button.
|
||||
* The width is set from the view model (near-full-width on phones, capped on
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
|
||||
import type { AssistantTranscriptEntry } from "../../assistant/session";
|
||||
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
|
||||
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerContext } from "../../ui/layers";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { VoiceDraft } from "./voice-draft";
|
||||
|
||||
class AiChatLayer implements Layer {
|
||||
readonly acceptsDirectional = true;
|
||||
private firstLine: number | null = null; // null follows the streaming tail
|
||||
private maxFirstLine = 0;
|
||||
private selectedId = "";
|
||||
private requestRender = () => {};
|
||||
private readonly wrappedEntries = new WeakMap<AssistantTranscriptEntry, {
|
||||
font: UiFont; width: number; text: string; lines: string[];
|
||||
}>();
|
||||
readonly draft: VoiceDraft;
|
||||
|
||||
constructor(private readonly conversations: AssistantConversations, options: InProcessAppOptions) {
|
||||
this.draft = new VoiceDraft(options.actions, () => shell.prepareChatVoiceCapture(),
|
||||
() => this.requestRender(), (text) => this.send(text));
|
||||
}
|
||||
start(render: () => void): () => void {
|
||||
this.requestRender = render;
|
||||
return this.conversations.onChanged(() => render());
|
||||
}
|
||||
send(text: string): void {
|
||||
this.firstLine = null;
|
||||
shell.sendToAssistant(text, false);
|
||||
}
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const size = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(size.width, size.height);
|
||||
const font = getDefaultSmallFont();
|
||||
const step = font.lineHeight + 3;
|
||||
const left = 12;
|
||||
const width = size.width - 28;
|
||||
const record = this.conversations.current();
|
||||
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
const title = this.conversations.title();
|
||||
image.drawText(font, left, 6, truncateText(font, title, width), 180);
|
||||
const label = assistantBackendSetting.get() === "external" ? "External agent" :
|
||||
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
|
||||
const footer = wrapText(font, `${label} ${gestureHints([
|
||||
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
|
||||
])}`, width);
|
||||
const footerY = size.height - step * footer.length;
|
||||
const footerTop = footerY - step * 3 - 12;
|
||||
const historyTop = step + 16;
|
||||
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
|
||||
const lines: { text: string; color: number }[] = [];
|
||||
for (const entry of transcript) {
|
||||
if (!entry.text) continue;
|
||||
let wrapped = this.wrappedEntries.get(entry);
|
||||
if (!wrapped || wrapped.font !== font || wrapped.width !== width || wrapped.text !== entry.text) {
|
||||
wrapped = { font, width, text: entry.text,
|
||||
lines: wrapText(font, `${entry.role === "user" ? "You" : "AI"}: ${entry.text}`, width - 8) };
|
||||
this.wrappedEntries.set(entry, wrapped);
|
||||
}
|
||||
for (const text of wrapped.lines) {
|
||||
lines.push({ text, color: entry.role === "user" ? 175 : 245 });
|
||||
}
|
||||
lines.push({ text: "", color: 0 });
|
||||
}
|
||||
if (!lines.length) lines.push({ text: "Hold to talk to your assistant.", color: 175 },
|
||||
{ text: "Voice and chat share this conversation.", color: 150 });
|
||||
this.maxFirstLine = Math.max(0, lines.length - visible);
|
||||
const first = this.firstLine === null ? this.maxFirstLine : Math.min(this.firstLine, this.maxFirstLine);
|
||||
for (let row = 0; row < visible; row++) {
|
||||
const line = lines[first + row];
|
||||
if (line) image.drawText(font, left, historyTop + row * step, line.text, line.color);
|
||||
}
|
||||
if (this.maxFirstLine > 0) {
|
||||
const track = visible * step;
|
||||
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
|
||||
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
|
||||
}
|
||||
image.fillRect(left, footerTop, width, 1, 65);
|
||||
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
|
||||
if (state) image.drawText(font, left, footerTop + 5, truncateText(font, state, width), 160);
|
||||
const pending = wrapText(font, this.draft.text || (this.draft.active ? "Listening..." : "Hold to speak; release to send"), width);
|
||||
pending.slice(-2).forEach((text, i) => image.drawText(font, left, footerTop + step * (i + 1) + 5, text, 220));
|
||||
footer.forEach((text, i) => image.drawText(font, left + width - font.measureText(text), footerY + i * step, text, 120));
|
||||
return image;
|
||||
}
|
||||
handleInput(event: InputEvent): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up": case "swipe-up":
|
||||
this.firstLine = Math.max(0, (this.firstLine ?? this.maxFirstLine) - 3); return;
|
||||
case "scroll-down": case "swipe-down": {
|
||||
const next = (this.firstLine ?? this.maxFirstLine) + 3;
|
||||
this.firstLine = next >= this.maxFirstLine ? null : next; return;
|
||||
}
|
||||
case "long-press":
|
||||
if (!this.conversations.current().session?.isTurnActive()) {
|
||||
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
|
||||
void this.draft.start();
|
||||
}
|
||||
return;
|
||||
case "long-press-release": this.draft.release(); return;
|
||||
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
|
||||
}
|
||||
}
|
||||
menuItems(): MenuItem[] {
|
||||
const record = this.conversations.current();
|
||||
const busy = () => this.draft.active || Boolean(record.session?.isTurnActive());
|
||||
const external = assistantBackendSetting.get() === "external";
|
||||
const submenu = (ctx: LayerContext, title: string, items: MenuItem[]) => ctx.stack.push(new WindowMenuLayer(title, items, true));
|
||||
return [
|
||||
...(external ? [{ label: "Sessions and model managed by bridge", disabled: true, onSelect: () => {} }] : []),
|
||||
{ label: "New session", disabled: () => external || busy(), onSelect: (ctx) => {
|
||||
if (this.conversations.create()) ctx.stack.clearToBase();
|
||||
} },
|
||||
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
|
||||
[...this.conversations.list()].reverse().map((item) => ({
|
||||
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
|
||||
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
|
||||
ASSISTANT_MODEL_VALUES.map((model) => ({
|
||||
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
|
||||
disabled: () => !this.conversations.available(model, record.reasoning),
|
||||
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
|
||||
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
|
||||
return external || busy() || !llm?.effort;
|
||||
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
|
||||
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
|
||||
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
|
||||
}))) },
|
||||
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
|
||||
record.session?.cancel(); ctx.stack.clearToBase();
|
||||
} }] : []),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export function createAiChatWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const layer = new AiChatLayer(shell.getAssistantConversations(), options);
|
||||
let unsubscribe = () => {};
|
||||
const app = createInProcessWindow({
|
||||
...options,
|
||||
appId: "ai-chat", windowId: "ai-chat", title: "AI Chat", iconLetter: "AI", icon: "message-circle", closeable: true,
|
||||
baseLayer: layer, holdToTalk: true,
|
||||
isVoiceCapturing: () => layer.draft.active,
|
||||
onSystemMenuOpened: () => layer.draft.cancel(),
|
||||
onAppMenuOpened: () => layer.draft.cancel(),
|
||||
setScreenOn: (on) => { if (!on) layer.draft.cancel(); },
|
||||
setSurfaceVisible: (visible) => { if (!visible) layer.draft.cancel(); options.setSurfaceVisible(visible); },
|
||||
menuItems: () => layer.menuItems(),
|
||||
receiveTextInput: (text) => { if (!layer.draft.active) layer.send(text); },
|
||||
onClosed: () => { unsubscribe(); layer.draft.cancel(); options.onClosed(); },
|
||||
});
|
||||
unsubscribe = layer.start(app.requestRender);
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { AppDefinition } from "../app-definition";
|
||||
import { createAiChatWindow } from "./ai-chat-app";
|
||||
|
||||
const aiChatApp: AppDefinition = {
|
||||
appId: "ai-chat",
|
||||
title: "AI Chat",
|
||||
icon: "message-circle",
|
||||
launch: (ctx) => ctx.launchInProcessApp("ai-chat", "window:ai-chat", createAiChatWindow),
|
||||
};
|
||||
export default aiChatApp;
|
||||
@@ -0,0 +1,136 @@
|
||||
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 {
|
||||
const generation = this.generation;
|
||||
const afterStop = () => {
|
||||
if (generation !== this.generation || !this.isFinishing()) return;
|
||||
this.timer = setTimeout(() => this.finish(), 1500);
|
||||
};
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
// Native recognition can take longer than the synchronous stop join.
|
||||
// Its final callback may finish the draft before this promise resolves.
|
||||
const stopped = this.actions.stopVoiceCapture();
|
||||
if (!stopped) { afterStop(); return; }
|
||||
void stopped.then(afterStop, (error) => {
|
||||
if (generation === this.generation) this.cancel(String(error));
|
||||
});
|
||||
} else {
|
||||
afterStop();
|
||||
}
|
||||
}
|
||||
|
||||
private finish(): void {
|
||||
if (this.phase !== "finishing") return;
|
||||
const text = this.text.trim();
|
||||
this.cancel(text ? "" : "No speech detected");
|
||||
if (text) this.send(text);
|
||||
}
|
||||
|
||||
cancel(status = ""): void {
|
||||
++this.generation;
|
||||
this.phase = "idle";
|
||||
this.status = status;
|
||||
this.text = this.live = this.committed = "";
|
||||
if (this.timer !== null) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
for (const unsubscribe of this.unsubscribe.splice(0)) unsubscribe();
|
||||
if (this.started) {
|
||||
this.started = false;
|
||||
void Promise.resolve(this.actions.stopVoiceCapture()).catch(() => {});
|
||||
}
|
||||
if (this.ownsVoiceActivity) {
|
||||
this.ownsVoiceActivity = false;
|
||||
voiceActivity.setActive(false);
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type AppDefinition } from "./app-definition";
|
||||
import aiChatApp from "./ai-chat";
|
||||
import launcherApp from "./launcher";
|
||||
import timerApp from "./timer";
|
||||
import calculatorApp from "./calculator";
|
||||
@@ -19,8 +20,10 @@ import blocksApp from "./blocks";
|
||||
import minesweeperApp from "./minesweeper";
|
||||
import freecellApp from "./freecell";
|
||||
import pinballApp from "./pinball";
|
||||
import flappyApp from "./flappy";
|
||||
import developerApp from "./developer";
|
||||
import evenhubApp from "./evenhub";
|
||||
import glanceboardApp from "./glanceboard";
|
||||
import settingsApp from "./settings";
|
||||
|
||||
/**
|
||||
@@ -30,6 +33,7 @@ import settingsApp from "./settings";
|
||||
*/
|
||||
export const ALL_APPS: readonly AppDefinition[] = [
|
||||
launcherApp,
|
||||
aiChatApp,
|
||||
timerApp,
|
||||
calculatorApp,
|
||||
terminalApp,
|
||||
@@ -49,7 +53,9 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
minesweeperApp,
|
||||
freecellApp,
|
||||
pinballApp,
|
||||
flappyApp,
|
||||
developerApp,
|
||||
evenhubApp,
|
||||
glanceboardApp,
|
||||
settingsApp,
|
||||
];
|
||||
@@ -32,6 +32,39 @@ export type AppDefinition = {
|
||||
showInLauncher?: boolean;
|
||||
/** Present on the app that handles text shared via the Android share intent. */
|
||||
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
|
||||
/**
|
||||
* Present on the app that supplies the Glanceboard: the sleep-time display
|
||||
* the controller shows on a tap or hold while the shell is asleep (see
|
||||
* app/g2/glance-host.ts). At most one app provides it.
|
||||
*/
|
||||
glanceboard?: GlanceboardProvider;
|
||||
};
|
||||
|
||||
/** A board the glance host can show while the shell sleeps. */
|
||||
export type GlanceboardProvider = {
|
||||
/** Board pixel size; the host centres it in the standard 576x288 band. */
|
||||
size: { width: number; height: number };
|
||||
/** Whether sleep-time gestures should show the board at all (user setting). */
|
||||
isEnabled: () => boolean;
|
||||
/** Whether a single tap shows the board (for tapTimeoutMs). */
|
||||
showOnTap: () => boolean;
|
||||
/** How long a tap or head-tilt keeps the board up. */
|
||||
tapTimeoutMs: () => number;
|
||||
/** Whether a long-press holds the board up until released. */
|
||||
showOnLongPress: () => boolean;
|
||||
/** Whether the head-tilt wake goes to the board rather than the regular UI. */
|
||||
showOnHeadTilt: () => boolean;
|
||||
/** A fresh board; requestRender asks the host to repaint it. */
|
||||
createBoard: (requestRender: () => void) => GlanceBoardInstance;
|
||||
};
|
||||
|
||||
export type GlanceBoardInstance = {
|
||||
/** The board is about to show: start widgets (subscriptions, hardware). */
|
||||
start: () => void;
|
||||
/** The board was hidden: stop everything start began. */
|
||||
stop: () => void;
|
||||
/** Paint the board at its size. */
|
||||
paint: () => GrayImage;
|
||||
};
|
||||
|
||||
export type AppLaunchParams = {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
/**
|
||||
* Blocks app worker: a falling-blocks game. One singleton window holds the
|
||||
* whole game; a gravity interval drives piece descent while the window is
|
||||
* foreground and the screen is on (losing either auto-pauses).
|
||||
* foreground, the screen is on, and the window holds input focus (losing any
|
||||
* of them auto-pauses: backgrounding, screen-off, the system menu or a
|
||||
* notification modal opening over the game, focus going to the sidebar).
|
||||
*
|
||||
* Controls (in play): scroll moves the piece, click rotates, long-press hard
|
||||
* drops, double-click pauses. Watch swipes are spatial: left/right move,
|
||||
@@ -17,6 +19,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 { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -43,11 +46,16 @@ const BOARD_X = 60;
|
||||
const BOARD_Y = 10;
|
||||
const PANEL_X = 230;
|
||||
|
||||
/** Gravity starts here and speeds up with score (see dropIntervalMs). */
|
||||
const BASE_DROP_MS = 1200;
|
||||
const MIN_DROP_MS = 200;
|
||||
/** Every this many points, gravity gets one 12% step faster. */
|
||||
const SPEED_STEP_SCORE = 300;
|
||||
/**
|
||||
* Gravity starts here and speeds up with score (see dropIntervalMs). Slow
|
||||
* by desktop standards: the glasses round trip is ~250 ms, so a piece must
|
||||
* hang around long enough to be steered through it.
|
||||
*/
|
||||
const BASE_DROP_MS = 1500;
|
||||
const MIN_DROP_MS = 350;
|
||||
/** Every this many points, gravity gets one SPEED_STEP_FACTOR step faster. */
|
||||
const SPEED_STEP_SCORE = 500;
|
||||
const SPEED_STEP_FACTOR = 0.92;
|
||||
/** Points per cleared-line count (index = simultaneous lines), times level. */
|
||||
const LINE_SCORES = [0, 100, 300, 500, 800];
|
||||
const HARD_DROP_POINTS_PER_ROW = 2;
|
||||
@@ -201,7 +209,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
lines: 0,
|
||||
tickTimer: null,
|
||||
tickIntervalMs: BASE_DROP_MS,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("blocks"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -248,6 +256,20 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away from the game (the system menu, a
|
||||
// notification modal, the voice dialog, focus back to the sidebar)
|
||||
// pauses it: the player can't steer a piece they aren't in control of.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
updateTickTimer(window);
|
||||
// Still on screen under a shell overlay, so show the pause.
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -296,6 +318,7 @@ function windowMenuItems(window: BlocksWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("blocks", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -547,7 +570,7 @@ function level(window: BlocksWindow): number {
|
||||
|
||||
function dropIntervalMs(window: BlocksWindow): number {
|
||||
const steps = Math.floor(window.score / SPEED_STEP_SCORE);
|
||||
return Math.max(MIN_DROP_MS, Math.round(BASE_DROP_MS * Math.pow(0.88, steps)));
|
||||
return Math.max(MIN_DROP_MS, Math.round(BASE_DROP_MS * Math.pow(SPEED_STEP_FACTOR, steps)));
|
||||
}
|
||||
|
||||
/** Keep the gravity interval running exactly when the game is live and visible. */
|
||||
|
||||
@@ -178,7 +178,7 @@ function dayKeyOf(timestampMs: number): string {
|
||||
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||
}
|
||||
|
||||
function dayHeaderLabel(timestampMs: number): string {
|
||||
export function dayHeaderLabel(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
const now = new Date();
|
||||
const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
@@ -189,7 +189,7 @@ function dayHeaderLabel(timestampMs: number): string {
|
||||
return base;
|
||||
}
|
||||
|
||||
function formatEventTime(timestampMs: number): string {
|
||||
export function formatEventTime(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
const minutes = String(date.getMinutes()).padStart(2, "0");
|
||||
const hour24 = date.getHours();
|
||||
|
||||
+10
-322
@@ -23,6 +23,7 @@ import {
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||
@@ -41,59 +42,6 @@ const EDGE_PAD = 8;
|
||||
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
|
||||
const MAX_ROSE_RADIUS = 98;
|
||||
|
||||
/**
|
||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||
* oval — wider and taller at the bottom — and give the rose's points their
|
||||
* foreshortening for free.
|
||||
*/
|
||||
const TILT_SQUASH = 0.42;
|
||||
const TILT_PERSPECTIVE = 0.22;
|
||||
/** Screen height of the projected ring, per unit of radius. */
|
||||
const RING_HEIGHT_PER_RADIUS =
|
||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||
/** Screen half-width of the projected ring, per unit of radius. */
|
||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||
|
||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||
const CARDINAL_TIP = 0.9;
|
||||
const INTERCARDINAL_TIP = 0.54;
|
||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||
const POINT_BASE_RADIUS = 0.17;
|
||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||
/**
|
||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||
* middle into the heading tick.
|
||||
*/
|
||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||
const PLANE_SPOKE_STEP = 30;
|
||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||
const PLANE_NEAR_VALUE = 78;
|
||||
const PLANE_FALLOFF = 0.55;
|
||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||
const PLANE_CUTOFF = 22;
|
||||
/** Height over which the grid fades out as it approaches the readout. */
|
||||
const PLANE_FADE_SPAN = 45;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
const TICK_HEIGHT = 7;
|
||||
const TICK_HALF_WIDTH = 4;
|
||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||
const DISC_DEPTH = 0.1;
|
||||
/**
|
||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||
*/
|
||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||
const WALL_DARK = 20;
|
||||
const WALL_LIT = 62;
|
||||
|
||||
class CompassLayer implements Layer {
|
||||
private rawHeading: number | null = null;
|
||||
@@ -233,21 +181,15 @@ 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 = readoutHeight + 4 + statusLines.length * smallStep;
|
||||
const radius = Math.max(
|
||||
24,
|
||||
Math.min(
|
||||
MAX_ROSE_RADIUS,
|
||||
Math.floor(
|
||||
(height - BOTTOM_PAD - TOP_PAD - textHeight - STACK_GAP - TICK_HEIGHT)
|
||||
/ (RING_HEIGHT_PER_RADIUS + DISC_DEPTH),
|
||||
),
|
||||
Math.floor((Math.min(cx, width - cx) - EDGE_PAD) / RING_HALF_WIDTH_PER_RADIUS),
|
||||
),
|
||||
);
|
||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||
// from the bottom padding rather than on it.
|
||||
const cy = height - BOTTOM_PAD - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
const { cy, radius, ringTop } = layoutCompassRose({
|
||||
width,
|
||||
height,
|
||||
cx,
|
||||
topClearance: TOP_PAD + textHeight + STACK_GAP,
|
||||
bottomPad: BOTTOM_PAD,
|
||||
edgePad: EDGE_PAD,
|
||||
maxRadius: MAX_ROSE_RADIUS,
|
||||
});
|
||||
|
||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
@@ -356,257 +298,3 @@ function frameStatusText(): string {
|
||||
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]!;
|
||||
}
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||
* and `radius` is a fraction of the rose's outer ring.
|
||||
*/
|
||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||
const radians = (angle * Math.PI) / 180;
|
||||
const across = radius * Math.sin(radians);
|
||||
const away = radius * Math.cos(radians);
|
||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||
return {
|
||||
x: cx + (across * scale) / depth,
|
||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
// 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.
|
||||
for (const degrees of [45, 135, 225, 315]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||
}
|
||||
for (const degrees of [90, 180, 270]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||
}
|
||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||
}
|
||||
|
||||
/**
|
||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||
* shaded facet, which is what keeps the eight points readable once perspective
|
||||
* has squashed the ones lying across the wearer's line of sight.
|
||||
*/
|
||||
function drawRosePoint(
|
||||
image: GrayImage,
|
||||
cx: number,
|
||||
cy: number,
|
||||
radius: number,
|
||||
angle: number,
|
||||
tipRadius: number,
|
||||
litValue: number,
|
||||
shadedValue: number,
|
||||
): void {
|
||||
const centre = { x: cx, y: cy };
|
||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||
*/
|
||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||
const depth = discDepth(radius);
|
||||
const segments = 48;
|
||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||
const rimValue = 105 * fade;
|
||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||
const point = projectRose(cx, cy, radius, 1, angle);
|
||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||
// runs nearly horizontal across the bottom of the oval.
|
||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||
const top = { x: previous.x, y: previous.y };
|
||||
const next = { x: point.x, y: point.y };
|
||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||
previous = point;
|
||||
}
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
}
|
||||
|
||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||
function wallFacing(angle: number): number {
|
||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||
* rather than stopping at a hard edge.
|
||||
*/
|
||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||
const shade = (point: Point, ringRadius: number): number => {
|
||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||
return value >= PLANE_CUTOFF ? value : 0;
|
||||
};
|
||||
const trace = (
|
||||
steps: number,
|
||||
at: (step: number) => { point: Point; ringRadius: number },
|
||||
): void => {
|
||||
let previous: Point | null = null;
|
||||
let previousValue = 0;
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
const { point, ringRadius } = at(i);
|
||||
const value = shade(point, ringRadius);
|
||||
if (previous !== null && previousValue > 0 && value > 0) {
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||
}
|
||||
previous = point;
|
||||
previousValue = value;
|
||||
}
|
||||
};
|
||||
|
||||
for (const ringRadius of PLANE_RINGS) {
|
||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||
}
|
||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||
// closes on itself instead of trailing off into loose ends.
|
||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||
trace(24, (step) => {
|
||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||
function discDepth(radius: number): number {
|
||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||
}
|
||||
|
||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const segments = 72;
|
||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||
previous = point;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||
const shade = Math.round(value);
|
||||
for (let y = top; y <= bottom; y++) {
|
||||
for (let x = left; x <= right; x++) {
|
||||
const point = { x: x + 0.5, y: y + 0.5 };
|
||||
const ab = edge(a, b, point);
|
||||
const bc = edge(b, c, point);
|
||||
const ca = edge(c, a, point);
|
||||
// Accept either winding, so callers don't have to order their vertices.
|
||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||
image.setPixel(x, y, shade);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||
function edge(a: Point, b: Point, p: Point): number {
|
||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* The compass rose renderer: a tilted disc with eight rotating points, drawn
|
||||
* from a heading into any GrayImage. Pure drawing code shared by the Compass
|
||||
* app's full-window view and the Glanceboard's compass widget; the callers
|
||||
* own the magnetometer lifecycle and the readout text around it.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { normalizeHeading } from "./calibration";
|
||||
|
||||
/**
|
||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||
* oval — wider and taller at the bottom — and give the rose's points their
|
||||
* foreshortening for free.
|
||||
*/
|
||||
const TILT_SQUASH = 0.42;
|
||||
const TILT_PERSPECTIVE = 0.22;
|
||||
/** Screen height of the projected ring, per unit of radius. */
|
||||
const RING_HEIGHT_PER_RADIUS =
|
||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||
/** Screen half-width of the projected ring, per unit of radius. */
|
||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||
|
||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||
const CARDINAL_TIP = 0.9;
|
||||
const INTERCARDINAL_TIP = 0.54;
|
||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||
const POINT_BASE_RADIUS = 0.17;
|
||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||
/**
|
||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||
* middle into the heading tick.
|
||||
*/
|
||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||
const PLANE_SPOKE_STEP = 30;
|
||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||
const PLANE_NEAR_VALUE = 78;
|
||||
const PLANE_FALLOFF = 0.55;
|
||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||
const PLANE_CUTOFF = 22;
|
||||
/** Height over which the grid fades out as it approaches the readout. */
|
||||
const PLANE_FADE_SPAN = 45;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
const TICK_HEIGHT = 7;
|
||||
const TICK_HALF_WIDTH = 4;
|
||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||
const DISC_DEPTH = 0.1;
|
||||
/**
|
||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||
*/
|
||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||
const WALL_DARK = 20;
|
||||
const WALL_LIT = 62;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
export { TICK_HEIGHT };
|
||||
|
||||
/**
|
||||
* Fit a rose into a box: the largest radius whose projected ring plus disc
|
||||
* wall fits between `topClearance` (the bottom of whatever text sits above)
|
||||
* and the box's bottom padding, and whose width clears the side padding
|
||||
* around `cx`. Returns the centre and radius to hand to drawCompassRose.
|
||||
*/
|
||||
export function layoutCompassRose(options: {
|
||||
width: number;
|
||||
height: number;
|
||||
cx: number;
|
||||
/** Lowest y already used above the rose (readout text, its gap). */
|
||||
topClearance: number;
|
||||
bottomPad: number;
|
||||
edgePad: number;
|
||||
maxRadius: number;
|
||||
minRadius?: number;
|
||||
}): { cx: number; cy: number; radius: number; ringTop: number } {
|
||||
const { width, height, cx } = options;
|
||||
const radius = Math.max(
|
||||
options.minRadius ?? 24,
|
||||
Math.min(
|
||||
options.maxRadius,
|
||||
Math.floor((height - options.bottomPad - options.topClearance - TICK_HEIGHT) / (RING_HEIGHT_PER_RADIUS + DISC_DEPTH)),
|
||||
Math.floor((Math.min(cx, width - cx) - options.edgePad) / RING_HALF_WIDTH_PER_RADIUS),
|
||||
),
|
||||
);
|
||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||
// from the bottom padding rather than on it.
|
||||
const cy = height - options.bottomPad - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
return { cx, cy, radius, ringTop };
|
||||
}
|
||||
|
||||
export function cardinalDirection(heading: number): string {
|
||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
}
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||
* and `radius` is a fraction of the rose's outer ring.
|
||||
*/
|
||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||
const radians = (angle * Math.PI) / 180;
|
||||
const across = radius * Math.sin(radians);
|
||||
const away = radius * Math.cos(radians);
|
||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||
return {
|
||||
x: cx + (across * scale) / depth,
|
||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export 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. */
|
||||
export 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;
|
||||
// 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.
|
||||
for (const degrees of [45, 135, 225, 315]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||
}
|
||||
for (const degrees of [90, 180, 270]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||
}
|
||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||
}
|
||||
|
||||
/**
|
||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||
* shaded facet, which is what keeps the eight points readable once perspective
|
||||
* has squashed the ones lying across the wearer's line of sight.
|
||||
*/
|
||||
function drawRosePoint(
|
||||
image: GrayImage,
|
||||
cx: number,
|
||||
cy: number,
|
||||
radius: number,
|
||||
angle: number,
|
||||
tipRadius: number,
|
||||
litValue: number,
|
||||
shadedValue: number,
|
||||
): void {
|
||||
const centre = { x: cx, y: cy };
|
||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||
*/
|
||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||
const depth = discDepth(radius);
|
||||
const segments = 48;
|
||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||
const rimValue = 105 * fade;
|
||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||
const point = projectRose(cx, cy, radius, 1, angle);
|
||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||
// runs nearly horizontal across the bottom of the oval.
|
||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||
const top = { x: previous.x, y: previous.y };
|
||||
const next = { x: point.x, y: point.y };
|
||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||
previous = point;
|
||||
}
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
}
|
||||
|
||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||
function wallFacing(angle: number): number {
|
||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||
* rather than stopping at a hard edge.
|
||||
*/
|
||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||
const shade = (point: Point, ringRadius: number): number => {
|
||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||
return value >= PLANE_CUTOFF ? value : 0;
|
||||
};
|
||||
const trace = (
|
||||
steps: number,
|
||||
at: (step: number) => { point: Point; ringRadius: number },
|
||||
): void => {
|
||||
let previous: Point | null = null;
|
||||
let previousValue = 0;
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
const { point, ringRadius } = at(i);
|
||||
const value = shade(point, ringRadius);
|
||||
if (previous !== null && previousValue > 0 && value > 0) {
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||
}
|
||||
previous = point;
|
||||
previousValue = value;
|
||||
}
|
||||
};
|
||||
|
||||
for (const ringRadius of PLANE_RINGS) {
|
||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||
}
|
||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||
// closes on itself instead of trailing off into loose ends.
|
||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||
trace(24, (step) => {
|
||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||
function discDepth(radius: number): number {
|
||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||
}
|
||||
|
||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const segments = 72;
|
||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||
previous = point;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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: tickTop + TICK_HEIGHT - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||
const shade = Math.round(value);
|
||||
for (let y = top; y <= bottom; y++) {
|
||||
for (let x = left; x <= right; x++) {
|
||||
const point = { x: x + 0.5, y: y + 0.5 };
|
||||
const ab = edge(a, b, point);
|
||||
const bc = edge(b, c, point);
|
||||
const ca = edge(c, a, point);
|
||||
// Accept either winding, so callers don't have to order their vertices.
|
||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||
image.setPixel(x, y, shade);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||
function edge(a: Point, b: Point, p: Point): number {
|
||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
cancelBandwidthBenchmark,
|
||||
getBandwidthBenchmarkStatus,
|
||||
startBandwidthBenchmark,
|
||||
type BandwidthBenchmarkStatus,
|
||||
} from "../../native/bandwidth-benchmark";
|
||||
|
||||
const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800];
|
||||
const WINDOW_SIZES = [1, 2, 3, 4, 6];
|
||||
const LINK_MODES = ["Current", "Request HIGH", "Request 2M", "HIGH + 2M"];
|
||||
const DURATION_MS = 15_000;
|
||||
// After painting the "running" screen, wait for that frame to reach the
|
||||
// glasses before the benchmark blocks normal image sends — otherwise the
|
||||
// status frame would sit queued behind the whole 15-second run.
|
||||
const ARM_DELAY_MS = 800;
|
||||
const POLL_INTERVAL_MS = 500;
|
||||
// Give up polling if the run somehow never reports done (duration + ack
|
||||
// timeout drain + slack).
|
||||
const POLL_DEADLINE_MS = DURATION_MS + 15_000;
|
||||
|
||||
const HEADER_HEIGHT = 30;
|
||||
const LIST_X = 20;
|
||||
const VALUE_X = 150;
|
||||
|
||||
type Phase = "config" | "arming" | "running" | "done";
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark: pick a message size and pipeline window, stream
|
||||
* no-op image payloads for 15 seconds, and report throughput. The glasses
|
||||
* screen is intentionally static while the run is active — the benchmark
|
||||
* stream is the only image traffic, so the numbers are clean.
|
||||
*/
|
||||
export class BandwidthBenchmarkLayer implements Layer {
|
||||
private phase: Phase = "config";
|
||||
private selectedRow = 0; // size, window, link preference, start
|
||||
private sizeIndex = 2;
|
||||
private windowIndex = 2;
|
||||
private linkModeIndex = 0;
|
||||
private result: BandwidthBenchmarkStatus | null = null;
|
||||
private error: string | null = null;
|
||||
private armTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private pollStartedAtMs = 0;
|
||||
|
||||
constructor(private readonly requestRender: () => void) {}
|
||||
|
||||
onRemoved(): void {
|
||||
this.clearTimers();
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
cancelBandwidthBenchmark();
|
||||
}
|
||||
}
|
||||
|
||||
private clearTimers(): void {
|
||||
if (this.armTimer !== null) {
|
||||
clearTimeout(this.armTimer);
|
||||
this.armTimer = null;
|
||||
}
|
||||
if (this.pollTimer !== null) {
|
||||
clearInterval(this.pollTimer);
|
||||
this.pollTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private startRun(): void {
|
||||
this.result = null;
|
||||
this.error = null;
|
||||
this.phase = "arming";
|
||||
this.requestRender();
|
||||
this.armTimer = setTimeout(() => {
|
||||
this.armTimer = null;
|
||||
const started = startBandwidthBenchmark(
|
||||
MESSAGE_SIZES[this.sizeIndex]!,
|
||||
WINDOW_SIZES[this.windowIndex]!,
|
||||
DURATION_MS,
|
||||
this.linkModeIndex,
|
||||
);
|
||||
if (!started) {
|
||||
this.phase = "config";
|
||||
this.error = "Could not start (not connected?)";
|
||||
this.requestRender();
|
||||
return;
|
||||
}
|
||||
this.phase = "running";
|
||||
this.pollStartedAtMs = Date.now();
|
||||
// No renders while polling: a repaint would queue an image update that
|
||||
// stays blocked behind the benchmark anyway. Render once on completion.
|
||||
this.pollTimer = setInterval(() => this.poll(), POLL_INTERVAL_MS);
|
||||
}, ARM_DELAY_MS);
|
||||
}
|
||||
|
||||
private poll(): void {
|
||||
const status = getBandwidthBenchmarkStatus();
|
||||
if (status && (status.state === "starting" || status.state === "running")) {
|
||||
if (Date.now() - this.pollStartedAtMs < POLL_DEADLINE_MS) return;
|
||||
cancelBandwidthBenchmark();
|
||||
return; // next poll picks up the cancelled run's "done"
|
||||
}
|
||||
this.clearTimers();
|
||||
if (status && status.state === "done") {
|
||||
this.result = status;
|
||||
this.phase = "done";
|
||||
} else {
|
||||
this.phase = "config";
|
||||
this.error = "Benchmark lost (disconnected?)";
|
||||
}
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
image.drawText(font, 20, 8, "BLE bandwidth", 220);
|
||||
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 8, "Running: streaming no-op packets for 15 s...", 200);
|
||||
image.drawText(
|
||||
font,
|
||||
LIST_X,
|
||||
HEADER_HEIGHT + 30,
|
||||
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}, ${LINK_MODES[this.linkModeIndex]}`,
|
||||
150,
|
||||
);
|
||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
const rowH = tightRowHeight(font) + 4;
|
||||
const rows: Array<[string, string]> = [
|
||||
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
||||
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
||||
["Link request", LINK_MODES[this.linkModeIndex]!],
|
||||
[this.phase === "done" ? "Run again" : "Start", ""],
|
||||
];
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const [label, value] = rows[index]!;
|
||||
const y = HEADER_HEIGHT + index * rowH;
|
||||
const selected = index === this.selectedRow;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
|
||||
let y = HEADER_HEIGHT + rows.length * rowH + 10;
|
||||
if (this.error) {
|
||||
image.drawText(font, LIST_X, y, this.error, 180);
|
||||
} else if (this.result) {
|
||||
const r = this.result;
|
||||
const seconds = r.elapsedMs / 1000;
|
||||
const payloadRate = seconds > 0 ? r.payloadBytesAcked / seconds : 0;
|
||||
const wireRate = seconds > 0 ? r.wireBytesAcked / seconds : 0;
|
||||
const lines = [
|
||||
`Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`,
|
||||
`Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`,
|
||||
`Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""),
|
||||
`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}`,
|
||||
];
|
||||
for (const line of lines) {
|
||||
image.drawText(font, LIST_X, y, line, 190);
|
||||
y += 20;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
// A static screen during the run: only allow bailing out.
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedRow = Math.min(3, this.selectedRow + 1);
|
||||
return;
|
||||
case "click":
|
||||
if (this.selectedRow === 0) {
|
||||
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
||||
} else if (this.selectedRow === 1) {
|
||||
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
||||
} else if (this.selectedRow === 2) {
|
||||
this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length;
|
||||
} else {
|
||||
this.startRun();
|
||||
}
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu";
|
||||
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";
|
||||
@@ -80,6 +81,12 @@ function debugTestsMenu(): MenuLayer {
|
||||
ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "BLE bandwidth",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new BandwidthBenchmarkLayer(ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
MENU_LAYOUT,
|
||||
);
|
||||
|
||||
@@ -113,8 +113,7 @@ export class LightSensorDemoLayer implements Layer {
|
||||
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, 22, 66, "Needs Faceclaw's custom firmware.", 120);
|
||||
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,777 @@
|
||||
/**
|
||||
* Flappy app worker: a Flappy Bird-style side scroller. One singleton
|
||||
* window; the bird sits a third of the way across, pipes scroll in from the
|
||||
* right, a flap counters gravity, and the score is one point per pipe pair
|
||||
* cleared.
|
||||
*
|
||||
* The BLE pipeline runs ~250 ms input-to-pixels and a handful of fps, so the
|
||||
* game is tuned latency-tolerant: slow scroll, gentle gravity, a wide gap,
|
||||
* and a forgiving hitbox. Physics runs at a 60 Hz fixed step inside a ~12 fps
|
||||
* render tick.
|
||||
*
|
||||
* Bandwidth: everything that moves is a sprite placed with drawImage, so the
|
||||
* wire pipeline can ship it as an on-glasses cached-image draw (a 9-byte
|
||||
* record) rather than pixels. The bird is pre-rendered at a few tilt angles
|
||||
* and wing positions; pipes are a cap plus vertically uniform body tiles;
|
||||
* the ground is one tile repeated. Sprites hanging off the top or left edge
|
||||
* fall back to raster (the planner needs a non-negative origin), which is
|
||||
* small and rare.
|
||||
*
|
||||
* Controls (a watch swipe in either direction is the primary flap):
|
||||
* swipe-up, swipe-down, click, or scroll-up flaps; double-click pauses;
|
||||
* tap-then-hold opens the window menu. Ready / paused / game over: click or
|
||||
* a swipe starts or resumes, double-click yields focus. Losing input focus
|
||||
* mid-flight (a shell overlay such as the system menu or a notification,
|
||||
* focus to the sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
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 { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import {
|
||||
directionalFallback,
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
BIRD_ANGLES,
|
||||
BIRD_SPRITE_H,
|
||||
BIRD_SPRITE_W,
|
||||
BIRD_WING_FRAMES,
|
||||
createBirdSprite,
|
||||
createGroundTile,
|
||||
createPipeBodyTile,
|
||||
createPipeCap,
|
||||
GROUND_H,
|
||||
GROUND_TILE_W,
|
||||
PIPE_BODY_TILE_H,
|
||||
PIPE_CAP_H,
|
||||
PIPE_CAP_OVERHANG,
|
||||
PIPE_CAP_W,
|
||||
PIPE_W,
|
||||
} from "./flappy-sprites";
|
||||
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
|
||||
const HIGH_SCORE_KEY = "flappy.highScore";
|
||||
|
||||
// --- Tuning (pixels, seconds) ------------------------------------------------
|
||||
/** Bird center x; fixed, the world scrolls past it. */
|
||||
const BIRD_X = 150;
|
||||
/** Gentle on purpose: a flap-to-flap cycle is well over the glasses round trip. */
|
||||
const GRAVITY = 520;
|
||||
const FLAP_VY = -215;
|
||||
const MAX_FALL_SPEED = 330;
|
||||
const SCROLL_SPEED = 76;
|
||||
const GAP_H = 104;
|
||||
const PIPE_SPACING = 236;
|
||||
/** Least body height either pipe of a pair keeps, so gaps never hug an edge. */
|
||||
const MIN_PIPE_BODY = 30;
|
||||
/** Forgiving hitbox half-extents around the bird center. */
|
||||
const HITBOX_HALF_W = 9;
|
||||
const HITBOX_HALF_H = 7;
|
||||
const PHYSICS_DT = 1 / 60;
|
||||
const RENDER_TICK_MS = 30;
|
||||
/** Physics steps per wing frame while flying. */
|
||||
const WING_STEPS_PER_FRAME = 6;
|
||||
|
||||
const SFX_FLAP: Step[] = [{ freq: 880, duty: 40, ms: 20 }, { freq: 1175, duty: 40, ms: 25 }];
|
||||
const SFX_SCORE: Step[] = [{ freq: 1568, duty: 40, ms: 40 }, { freq: 2093, duty: 40, ms: 60 }];
|
||||
const SFX_HIT: Step[] = [{ freq: 220, duty: 55, ms: 80 }, { freq: 165, duty: 55, ms: 120 }];
|
||||
const SFX_GAME_OVER: Step[] = [
|
||||
{ freq: 494, duty: 45, ms: 150 },
|
||||
{ freq: 440, duty: 45, ms: 150 },
|
||||
{ freq: 392, duty: 45, ms: 150 },
|
||||
{ freq: 294, duty: 50, ms: 320 },
|
||||
];
|
||||
const SFX_PAUSE: Step[] = [
|
||||
{ freq: 1319, duty: 40, ms: 60 },
|
||||
{ freq: 880, duty: 40, ms: 110 },
|
||||
];
|
||||
const SFX_RESUME: Step[] = [
|
||||
{ freq: 880, duty: 40, ms: 60 },
|
||||
{ freq: 1319, duty: 40, ms: 110 },
|
||||
];
|
||||
const MINOR_SFX_MIN_GAP_MS = 120;
|
||||
|
||||
/**
|
||||
* ready: bird hovering, waiting for the first flap. playing: scrolling.
|
||||
* dying: hit something, bird tumbling to the ground with the world frozen.
|
||||
* game-over / paused: static screens with a dialog.
|
||||
*/
|
||||
type GamePhase = "ready" | "playing" | "dying" | "game-over" | "paused";
|
||||
|
||||
type Pipe = {
|
||||
/** Body left edge in viewport coordinates. */
|
||||
x: number;
|
||||
/** Bottom of the top pipe's cap; the gap runs from here to gapTop + GAP_H. */
|
||||
gapTop: number;
|
||||
scored: boolean;
|
||||
};
|
||||
|
||||
type FlappyWindow = {
|
||||
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;
|
||||
/** Tap-then-hold window menu; created on first open. */
|
||||
menu: WindowMenu | null;
|
||||
phase: GamePhase;
|
||||
birdY: number;
|
||||
birdVy: number;
|
||||
/** Physics steps since the game started; drives the wing animation. */
|
||||
stepCount: number;
|
||||
/** World scroll distance so far, for the ground tiling phase. */
|
||||
scrollX: number;
|
||||
pipes: Pipe[];
|
||||
score: number;
|
||||
highScore: number;
|
||||
tickTimer: ReturnType<typeof setInterval> | null;
|
||||
lastTickAtMs: number;
|
||||
lastMinorSfxAtMs: number;
|
||||
soundOn: boolean;
|
||||
lastSubmittedFingerprint: string;
|
||||
};
|
||||
|
||||
const windows = new Map<string, FlappyWindow>();
|
||||
let screenOn = true;
|
||||
|
||||
// --- Sprites (immutable, built once per worker) -----------------------------
|
||||
|
||||
const pipeBodyTile = createPipeBodyTile();
|
||||
const pipeCap = createPipeCap();
|
||||
const groundTile = createGroundTile();
|
||||
/** [angleIndex][wingFrame] */
|
||||
const birdSprites: GrayImage[][] = BIRD_ANGLES.map((angle) =>
|
||||
Array.from({ length: BIRD_WING_FRAMES }, (_, frame) => createBirdSprite(angle, frame)),
|
||||
);
|
||||
|
||||
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": {
|
||||
const window: FlappyWindow = {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
title: message.title,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
focused: false,
|
||||
menu: null,
|
||||
phase: "ready",
|
||||
birdY: 0,
|
||||
birdVy: 0,
|
||||
stepCount: 0,
|
||||
scrollX: 0,
|
||||
pipes: [],
|
||||
score: 0,
|
||||
highScore: loadHighScore(),
|
||||
tickTimer: null,
|
||||
lastTickAtMs: 0,
|
||||
lastMinorSfxAtMs: 0,
|
||||
soundOn: loadSoundEnabled("flappy"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
resetGame(window);
|
||||
windows.set(message.windowId, window);
|
||||
break;
|
||||
}
|
||||
case "resize-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight });
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "close-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window?.tickTimer) clearInterval(window.tickTimer);
|
||||
windows.delete(message.windowId);
|
||||
break;
|
||||
}
|
||||
case "input": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) {
|
||||
frameTimings.finishFrame(message.frameId, "discarded: unknown flappy window");
|
||||
break;
|
||||
}
|
||||
window.focused = message.focused;
|
||||
inferForeground(window, message.focused);
|
||||
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;
|
||||
inferForeground(window, message.focused);
|
||||
renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "foreground": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
window.focused = message.focused;
|
||||
// The player can't see the bird while backgrounded, so don't let it
|
||||
// keep falling.
|
||||
if (!window.foreground && window.phase === "playing") window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses the
|
||||
// flight; a death tumble is allowed to finish.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
if (!screenOn && window.phase === "playing") window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Input and render messages only ever target the shell's foreground window,
|
||||
* so a focused message proves this window is foreground. This backstops the
|
||||
* "foreground" message itself, which can be lost while a freshly spawned
|
||||
* worker is still evaluating its bundle (see the Blocks worker).
|
||||
*/
|
||||
function inferForeground(window: FlappyWindow, focused: boolean): void {
|
||||
if (!focused || window.foreground) return;
|
||||
window.foreground = true;
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function loadHighScore(): number {
|
||||
try {
|
||||
const parsed = parseInt(getStringSetting(HIGH_SCORE_KEY, "0"), 10);
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function saveHighScore(window: FlappyWindow): void {
|
||||
if (window.score <= window.highScore) return;
|
||||
window.highScore = window.score;
|
||||
try {
|
||||
setStringSetting(HIGH_SCORE_KEY, String(window.highScore));
|
||||
} catch (error) {
|
||||
console.warn(`flappy high-score save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
|
||||
* steps on its own timer, and the Java call is safe from the worker thread.
|
||||
* Minor (frequent) effects are dropped when they'd arrive within the
|
||||
* rate-limit gap; newsworthy ones always play.
|
||||
*/
|
||||
function playSfx(window: FlappyWindow, steps: Step[], minor = false): void {
|
||||
if (!window.soundOn || steps.length === 0) return;
|
||||
const now = Date.now();
|
||||
if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return;
|
||||
if (minor) window.lastMinorSfxAtMs = now;
|
||||
try {
|
||||
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
if (!communicator) return;
|
||||
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
|
||||
} catch (error) {
|
||||
console.warn(`flappy sfx failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
function windowMenuItems(window: FlappyWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: "New game",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
resetGame(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: window.soundOn ? "Sound: on" : "Sound: off",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("flappy", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function windowMenu(window: FlappyWindow): 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,
|
||||
});
|
||||
}
|
||||
return window.menu;
|
||||
}
|
||||
|
||||
// --- Input ------------------------------------------------------------------
|
||||
|
||||
function handleInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
// An open window menu owns all input (it closes itself via pop); menus are
|
||||
// list UIs, so watch swipes take their standard fallback meanings there.
|
||||
if (window.menu?.isOpen()) {
|
||||
window.menu
|
||||
.handleInput(directionalFallback(event))
|
||||
.catch((error) => console.error(`flappy menu input failed: ${error}`))
|
||||
.then(() => renderAndSubmit(window, frameId));
|
||||
return;
|
||||
}
|
||||
|
||||
// Opening the menu mid-flight would leave the bird falling under it.
|
||||
if (event.type === "short-then-long-press") {
|
||||
if (window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
}
|
||||
windowMenu(window).open();
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (window.phase) {
|
||||
case "ready":
|
||||
case "playing":
|
||||
handleFlightInput(window, event, frameId);
|
||||
break;
|
||||
case "dying":
|
||||
// Let the tumble finish; a flap mashed during it must not restart.
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input while dying");
|
||||
break;
|
||||
default:
|
||||
handleIdleInput(window, event, frameId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Input while ready or flying: the flap gestures, plus pause. */
|
||||
function handleFlightInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
switch (event.type) {
|
||||
// Either swipe direction flaps: on the watch the flap is a reflex, and
|
||||
// which way the thumb went shouldn't matter.
|
||||
case "swipe-up":
|
||||
case "swipe-down":
|
||||
case "click":
|
||||
case "scroll-up":
|
||||
flap(window);
|
||||
break;
|
||||
case "double-click":
|
||||
if (window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
playSfx(window, SFX_PAUSE);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input");
|
||||
return;
|
||||
}
|
||||
syncTickTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
/** Input while paused or game over. Swipes take their standard fallback meanings. */
|
||||
function handleIdleInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
|
||||
switch (directionalFallback(event).type) {
|
||||
case "click":
|
||||
if (window.phase === "game-over") {
|
||||
resetGame(window);
|
||||
} else {
|
||||
window.phase = "playing";
|
||||
}
|
||||
syncTickTimer(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
break;
|
||||
case "double-click":
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: flappy ignored input");
|
||||
return;
|
||||
}
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
function groundTop(window: FlappyWindow): number {
|
||||
return window.viewportHeight - GROUND_H;
|
||||
}
|
||||
|
||||
function resetGame(window: FlappyWindow): void {
|
||||
window.phase = "ready";
|
||||
window.birdY = Math.round(groundTop(window) * 0.42);
|
||||
window.birdVy = 0;
|
||||
window.stepCount = 0;
|
||||
window.scrollX = 0;
|
||||
window.pipes = [];
|
||||
window.score = 0;
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function flap(window: FlappyWindow): void {
|
||||
if (window.phase === "ready") {
|
||||
window.phase = "playing";
|
||||
window.lastTickAtMs = Date.now();
|
||||
}
|
||||
window.birdVy = FLAP_VY;
|
||||
playSfx(window, SFX_FLAP, true);
|
||||
}
|
||||
|
||||
// --- Simulation -------------------------------------------------------------
|
||||
|
||||
function isAnimating(window: FlappyWindow): boolean {
|
||||
return window.phase === "playing" || window.phase === "dying";
|
||||
}
|
||||
|
||||
/** Keep the render tick running exactly while something is visibly moving. */
|
||||
function syncTickTimer(window: FlappyWindow): void {
|
||||
const shouldRun = window.foreground && screenOn && isAnimating(window);
|
||||
if (!shouldRun && window.tickTimer !== null) {
|
||||
clearInterval(window.tickTimer);
|
||||
window.tickTimer = null;
|
||||
}
|
||||
if (shouldRun && window.tickTimer === null) {
|
||||
window.lastTickAtMs = Date.now();
|
||||
window.tickTimer = setInterval(() => tick(window), RENDER_TICK_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/** One render tick: catch the fixed-step physics up to real time, repaint. */
|
||||
function tick(window: FlappyWindow): void {
|
||||
if (!isAnimating(window)) {
|
||||
syncTickTimer(window);
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
const elapsedMs = Math.min(now - window.lastTickAtMs, 300);
|
||||
window.lastTickAtMs = now;
|
||||
const steps = clamp(Math.round(elapsedMs / (PHYSICS_DT * 1000)), 1, 20);
|
||||
for (let i = 0; i < steps && isAnimating(window); i++) {
|
||||
stepPhysics(window);
|
||||
}
|
||||
renderAndSubmit(window, 0);
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
function stepPhysics(window: FlappyWindow): void {
|
||||
const dt = PHYSICS_DT;
|
||||
window.stepCount++;
|
||||
window.birdVy = Math.min(window.birdVy + GRAVITY * dt, MAX_FALL_SPEED);
|
||||
window.birdY += window.birdVy * dt;
|
||||
const floor = groundTop(window) - HITBOX_HALF_H;
|
||||
|
||||
if (window.phase === "dying") {
|
||||
if (window.birdY >= floor) {
|
||||
window.birdY = floor;
|
||||
finishGame(window);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Ceiling: stop rather than die, like the original.
|
||||
if (window.birdY < HITBOX_HALF_H) {
|
||||
window.birdY = HITBOX_HALF_H;
|
||||
window.birdVy = Math.max(0, window.birdVy);
|
||||
}
|
||||
|
||||
const advance = SCROLL_SPEED * dt;
|
||||
window.scrollX += advance;
|
||||
for (const pipe of window.pipes) pipe.x -= advance;
|
||||
if (window.pipes.length && window.pipes[0]!.x + PIPE_W + PIPE_CAP_OVERHANG < 0) {
|
||||
window.pipes.shift();
|
||||
}
|
||||
const last = window.pipes[window.pipes.length - 1];
|
||||
if (!last || last.x <= window.viewportWidth - PIPE_SPACING) {
|
||||
spawnPipe(window, last ? last.x + PIPE_SPACING : window.viewportWidth + 40);
|
||||
}
|
||||
|
||||
for (const pipe of window.pipes) {
|
||||
if (!pipe.scored && pipe.x + PIPE_W < BIRD_X - HITBOX_HALF_W) {
|
||||
pipe.scored = true;
|
||||
window.score++;
|
||||
playSfx(window, SFX_SCORE);
|
||||
}
|
||||
}
|
||||
|
||||
if (window.birdY >= floor) {
|
||||
window.birdY = floor;
|
||||
playSfx(window, SFX_HIT);
|
||||
finishGame(window);
|
||||
return;
|
||||
}
|
||||
if (window.pipes.some((pipe) => hitsPipe(window, pipe))) {
|
||||
window.phase = "dying";
|
||||
window.birdVy = Math.max(window.birdVy, 40);
|
||||
playSfx(window, SFX_HIT);
|
||||
}
|
||||
}
|
||||
|
||||
function spawnPipe(window: FlappyWindow, x: number): void {
|
||||
const minGapTop = PIPE_CAP_H + MIN_PIPE_BODY;
|
||||
const maxGapTop = groundTop(window) - GAP_H - PIPE_CAP_H - MIN_PIPE_BODY;
|
||||
const gapTop = Math.round(minGapTop + Math.random() * Math.max(0, maxGapTop - minGapTop));
|
||||
window.pipes.push({ x, gapTop, scored: false });
|
||||
}
|
||||
|
||||
function hitsPipe(window: FlappyWindow, pipe: Pipe): boolean {
|
||||
const left = BIRD_X - HITBOX_HALF_W;
|
||||
const right = BIRD_X + HITBOX_HALF_W;
|
||||
const top = window.birdY - HITBOX_HALF_H;
|
||||
const bottom = window.birdY + HITBOX_HALF_H;
|
||||
const capLeft = pipe.x - PIPE_CAP_OVERHANG;
|
||||
const capRight = capLeft + PIPE_CAP_W;
|
||||
const gapBottom = pipe.gapTop + GAP_H;
|
||||
// The cap is wider than the body, so test the whole pipe at cap width for
|
||||
// the cap rows and at body width elsewhere.
|
||||
const withinBody = right > pipe.x && left < pipe.x + PIPE_W;
|
||||
const withinCap = right > capLeft && left < capRight;
|
||||
if (withinCap) {
|
||||
if (bottom > pipe.gapTop - PIPE_CAP_H && top < pipe.gapTop) return true;
|
||||
if (bottom > gapBottom && top < gapBottom + PIPE_CAP_H) return true;
|
||||
}
|
||||
if (withinBody) {
|
||||
if (top < pipe.gapTop - PIPE_CAP_H) return true;
|
||||
if (bottom > gapBottom + PIPE_CAP_H) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function finishGame(window: FlappyWindow): void {
|
||||
window.phase = "game-over";
|
||||
saveHighScore(window);
|
||||
playSfx(window, SFX_GAME_OVER);
|
||||
syncTickTimer(window);
|
||||
}
|
||||
|
||||
// --- Painting ---------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Place a sprite. A whole sprite with a non-negative origin goes through the
|
||||
* deferred-image path (texture cacheable); anything hanging off the top or
|
||||
* left edge, or vertically clipped, falls back to a raster blit, since
|
||||
* cached draws are whole-image only with an unsigned origin.
|
||||
*/
|
||||
function stamp(image: GrayImage, sprite: GrayImage, x: number, y: number, clipHeight?: number): void {
|
||||
const ix = Math.round(x);
|
||||
const iy = Math.round(y);
|
||||
if (ix >= 0 && iy >= 0 && (clipHeight === undefined || clipHeight >= sprite.height)) {
|
||||
image.drawImage(sprite, ix, iy);
|
||||
} else {
|
||||
image.bitBlt(sprite, ix, iy, { height: clipHeight, transparentZero: true });
|
||||
}
|
||||
}
|
||||
|
||||
function paint(window: FlappyWindow): Plane[] {
|
||||
return windowMenu(window).paint();
|
||||
}
|
||||
|
||||
function paintContent(window: FlappyWindow): GrayImage {
|
||||
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
const ground = groundTop(window);
|
||||
|
||||
for (const pipe of window.pipes) paintPipe(image, pipe, ground);
|
||||
|
||||
// Ground: tiles phased by the scroll distance so the hatching moves.
|
||||
const groundPhase = -(Math.round(window.scrollX) % GROUND_TILE_W);
|
||||
for (let x = groundPhase; x < window.viewportWidth; x += GROUND_TILE_W) {
|
||||
stamp(image, groundTile, x, ground);
|
||||
}
|
||||
|
||||
// Bird last, so it sits over the pipes when they meet.
|
||||
const sprite = birdSprites[birdAngleIndex(window)]![birdWingFrame(window)]!;
|
||||
stamp(image, sprite, BIRD_X - BIRD_SPRITE_W / 2, window.birdY - BIRD_SPRITE_H / 2);
|
||||
|
||||
paintHud(image, window);
|
||||
return image;
|
||||
}
|
||||
|
||||
function paintPipe(image: GrayImage, pipe: Pipe, ground: number): void {
|
||||
const capX = pipe.x - PIPE_CAP_OVERHANG;
|
||||
// Top pipe: cap just above the gap, body tiles stacked upward from it. The
|
||||
// tile that would start above the top edge is clipped to raster.
|
||||
const topBodyBottom = pipe.gapTop - PIPE_CAP_H;
|
||||
for (let y = topBodyBottom - PIPE_BODY_TILE_H; y > -PIPE_BODY_TILE_H; y -= PIPE_BODY_TILE_H) {
|
||||
stamp(image, pipeBodyTile, pipe.x, y);
|
||||
}
|
||||
stamp(image, pipeCap, capX, topBodyBottom);
|
||||
|
||||
// Bottom pipe: cap just below the gap, body tiles stacked down to the
|
||||
// ground; the final partial tile is clipped to raster.
|
||||
const gapBottom = pipe.gapTop + GAP_H;
|
||||
stamp(image, pipeCap, capX, gapBottom);
|
||||
for (let y = gapBottom + PIPE_CAP_H; y < ground; y += PIPE_BODY_TILE_H) {
|
||||
stamp(image, pipeBodyTile, pipe.x, y, Math.min(PIPE_BODY_TILE_H, ground - y));
|
||||
}
|
||||
}
|
||||
|
||||
/** Nose-up while rising, nose-down the faster the fall; steepest during the death tumble. */
|
||||
function birdAngleIndex(window: FlappyWindow): number {
|
||||
if (window.phase === "ready") return 1;
|
||||
if (window.phase === "dying") return BIRD_ANGLES.length - 1;
|
||||
const target = window.birdVy < 0 ? -25 : (window.birdVy / MAX_FALL_SPEED) * 55;
|
||||
let best = 0;
|
||||
for (let i = 1; i < BIRD_ANGLES.length - 1; i++) {
|
||||
if (Math.abs(BIRD_ANGLES[i]! - target) < Math.abs(BIRD_ANGLES[best]! - target)) best = i;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
function birdWingFrame(window: FlappyWindow): number {
|
||||
if (window.phase !== "playing") return 1;
|
||||
// Up, mid, down, mid, ... so the wing beats rather than snaps.
|
||||
const cycle = [0, 1, 2, 1];
|
||||
return cycle[Math.floor(window.stepCount / WING_STEPS_PER_FRAME) % cycle.length]!;
|
||||
}
|
||||
|
||||
function paintHud(image: GrayImage, window: FlappyWindow): void {
|
||||
const width = window.viewportWidth;
|
||||
if (window.phase !== "ready") {
|
||||
drawCenteredIn(image, largeFont, 0, width, 10, String(window.score), 245);
|
||||
}
|
||||
switch (window.phase) {
|
||||
case "ready":
|
||||
drawCenteredIn(image, mediumFont, 0, width, 14, "FLAPPY", 245);
|
||||
if (window.highScore > 0) {
|
||||
drawCenteredIn(image, smallFont, 0, width, 44, `best ${window.highScore}`, 150);
|
||||
}
|
||||
drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, `swipe / ${GESTURE_CLICK} flap`, 150);
|
||||
break;
|
||||
case "paused":
|
||||
paintDialog(image, window, "PAUSED", [`${GESTURE_CLICK} resume`, `${GESTURE_DOUBLE_CLICK} leave`]);
|
||||
break;
|
||||
case "game-over":
|
||||
paintDialog(image, window, "GAME OVER", [
|
||||
`score ${window.score} best ${window.highScore}`,
|
||||
`${GESTURE_CLICK} new game ${GESTURE_DOUBLE_CLICK} leave`,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function paintDialog(image: GrayImage, window: FlappyWindow, title: string, lines: string[]): void {
|
||||
const width = 260;
|
||||
const height = 46 + lines.length * 20;
|
||||
const x = Math.round((window.viewportWidth - width) / 2);
|
||||
const y = Math.round((groundTop(window) - height) / 2);
|
||||
image.fillRect(x, y, width, height, 0);
|
||||
image.drawRect(x + 3, y + 3, width - 6, height - 6, 150);
|
||||
drawCenteredIn(image, mediumFont, x, width, y + 12, title, 245);
|
||||
lines.forEach((line, i) => {
|
||||
drawCenteredIn(image, smallFont, x, width, y + 40 + i * 20, line, 150);
|
||||
});
|
||||
}
|
||||
|
||||
function drawCenteredIn(
|
||||
image: GrayImage,
|
||||
font: typeof smallFont,
|
||||
x: number,
|
||||
width: number,
|
||||
y: number,
|
||||
text: string,
|
||||
value: number,
|
||||
): void {
|
||||
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
|
||||
}
|
||||
|
||||
function renderAndSubmit(window: FlappyWindow, 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: flappy 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: flappy render failed");
|
||||
console.error(`flappy worker render failed: ${error}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Sprite assets for the Flappy game, built once per worker and never mutated
|
||||
* afterwards, so every frame can place them with GrayImage.drawImage and the
|
||||
* wire pipeline can ship them as on-glasses cached-image draws (see
|
||||
* graphics/glyph-wire.ts). A frame then costs a handful of 9-byte draw
|
||||
* records plus whatever raster is left, instead of re-sending the sprite
|
||||
* pixels every tick.
|
||||
*
|
||||
* Constraints the pipeline puts on the art (kept here so the worker doesn't
|
||||
* have to know):
|
||||
* - a cached image is at most 255x255, so pipes are built from short body
|
||||
* tiles under a cap rather than one tall image;
|
||||
* - the body tile is vertically uniform (stripes only), so overlapping two
|
||||
* tiles at any vertical offset writes identical pixels and both still
|
||||
* match the composite the planner checks against;
|
||||
* - value 0 is transparent (color key), so sprites use 1 for opaque black.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
|
||||
export const PIPE_W = 44;
|
||||
export const PIPE_CAP_W = 52;
|
||||
export const PIPE_CAP_H = 16;
|
||||
/** How far the cap overhangs the body on each side. */
|
||||
export const PIPE_CAP_OVERHANG = (PIPE_CAP_W - PIPE_W) / 2;
|
||||
export const PIPE_BODY_TILE_H = 32;
|
||||
|
||||
export const GROUND_TILE_W = 96;
|
||||
export const GROUND_H = 18;
|
||||
|
||||
export const BIRD_SPRITE_W = 30;
|
||||
export const BIRD_SPRITE_H = 26;
|
||||
/** Bird rotation angles the sprites are pre-rendered at, in degrees (positive = nose down). */
|
||||
export const BIRD_ANGLES = [-25, 0, 25, 50, 80] as const;
|
||||
export const BIRD_WING_FRAMES = 3;
|
||||
|
||||
const OPAQUE_BLACK = 1;
|
||||
|
||||
/** Vertical stripe shading across the pipe body: highlight left, shadow right. */
|
||||
function pipeColumnShade(col: number, width: number): number {
|
||||
if (col === 0) return 90;
|
||||
if (col <= 3) return 210;
|
||||
if (col >= width - 1) return 60;
|
||||
if (col >= width - 4) return 95;
|
||||
return 150;
|
||||
}
|
||||
|
||||
export function createPipeBodyTile(): GrayImage {
|
||||
const image = new GrayImage(PIPE_W, PIPE_BODY_TILE_H, 0);
|
||||
for (let col = 0; col < PIPE_W; col++) {
|
||||
image.fillRect(col, 0, 1, PIPE_BODY_TILE_H, pipeColumnShade(col, PIPE_W));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Vertically symmetric, so one tile serves as both the top and the bottom pipe's cap. */
|
||||
export function createPipeCap(): GrayImage {
|
||||
const image = new GrayImage(PIPE_CAP_W, PIPE_CAP_H, 0);
|
||||
for (let col = 0; col < PIPE_CAP_W; col++) {
|
||||
image.fillRect(col, 0, 1, PIPE_CAP_H, pipeColumnShade(col, PIPE_CAP_W));
|
||||
}
|
||||
image.fillRect(0, 0, PIPE_CAP_W, 2, 220);
|
||||
image.fillRect(0, PIPE_CAP_H - 2, PIPE_CAP_W, 2, 220);
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Ground strip: a bright surface line over diagonal hatching that tiles at GROUND_TILE_W. */
|
||||
export function createGroundTile(): GrayImage {
|
||||
const image = new GrayImage(GROUND_TILE_W, GROUND_H, 0);
|
||||
image.fillRect(0, 0, GROUND_TILE_W, 2, 200);
|
||||
const period = 24; // divides GROUND_TILE_W, so the hatching wraps seamlessly
|
||||
for (let y = 3; y < GROUND_H; y++) {
|
||||
for (let x = 0; x < GROUND_TILE_W; x++) {
|
||||
const phase = ((x + y) % period + period) % period;
|
||||
image.setPixel(x, y, phase < 10 ? 110 : 45);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* The bird, rendered by sampling shapes in bird-local space so any rotation
|
||||
* and wing position comes out of the same geometry. Local x points along the
|
||||
* beak, y down; the sprite is centered on the body center.
|
||||
*/
|
||||
export function createBirdSprite(angleDeg: number, wingFrame: number): GrayImage {
|
||||
const image = new GrayImage(BIRD_SPRITE_W, BIRD_SPRITE_H, 0);
|
||||
const angle = (angleDeg * Math.PI) / 180;
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
const cx = BIRD_SPRITE_W / 2;
|
||||
const cy = BIRD_SPRITE_H / 2;
|
||||
// Wing pivot sits behind the body center; the tip swings up/mid/down.
|
||||
const wingTipY = [-5, 0, 4][wingFrame % BIRD_WING_FRAMES]!;
|
||||
for (let py = 0; py < BIRD_SPRITE_H; py++) {
|
||||
for (let px = 0; px < BIRD_SPRITE_W; px++) {
|
||||
const sx = px + 0.5 - cx;
|
||||
const sy = py + 0.5 - cy;
|
||||
// Sprite space -> bird-local space (inverse rotation).
|
||||
const x = sx * cos + sy * sin;
|
||||
const y = -sx * sin + sy * cos;
|
||||
const value = birdShade(x, y, wingTipY);
|
||||
if (value > 0) image.setPixel(px, py, value);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Gray value of the bird at a bird-local point, or 0 outside it. */
|
||||
function birdShade(x: number, y: number, wingTipY: number): number {
|
||||
// Eye: white with a pupil, drawn first so it sits on top of the body.
|
||||
const eyeX = 4.5;
|
||||
const eyeY = -2.5;
|
||||
if (inEllipse(x - eyeX - 1.1, y - eyeY, 1.3, 1.3)) return OPAQUE_BLACK;
|
||||
if (inEllipse(x - eyeX, y - eyeY, 3.2, 3.0)) return 255;
|
||||
// Beak: a wedge off the front of the body.
|
||||
if (x >= 7 && x <= 14 && Math.abs(y - 1.5) <= 2.6 * (1 - (x - 7) / 8.5)) {
|
||||
return y < 1.5 ? 235 : 190;
|
||||
}
|
||||
// Wing: an angled lobe behind the center, swinging with the frame.
|
||||
const wx = x + 3;
|
||||
const wy = y - wingTipY * 0.5 - wingTipY * 0.35 * (wx / 6);
|
||||
if (inEllipse(wx, wy, 6.5, 3.2)) {
|
||||
return inEllipse(wx, wy, 5.0, 2.0) ? 120 : 175;
|
||||
}
|
||||
// Body: lighter belly, darker back, dark outline ring.
|
||||
if (inEllipse(x, y, 11, 8.5)) {
|
||||
if (!inEllipse(x, y, 10, 7.5)) return 110;
|
||||
return y > 2 ? 255 : 205;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function inEllipse(x: number, y: number, rx: number, ry: number): boolean {
|
||||
return (x * x) / (rx * rx) + (y * y) / (ry * ry) <= 1;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
|
||||
|
||||
const flappyApp: AppDefinition = {
|
||||
appId: "flappy",
|
||||
title: "Flappy",
|
||||
icon: "bird",
|
||||
launch: (ctx) =>
|
||||
launchWorkerAppWindow(ctx, {
|
||||
createWorker: () => new Worker("./flappy-app.worker"),
|
||||
windowId: "flappy:main",
|
||||
title: "Flappy",
|
||||
iconLetter: "B",
|
||||
icon: "bird",
|
||||
acceptsDirectional: true,
|
||||
}),
|
||||
};
|
||||
|
||||
export default flappyApp;
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Freecell solitaire app worker. One singleton window holds a standard
|
||||
* 52-card Freecell game: 8 cascades, 4 free cells, 4 foundations. Red suits
|
||||
* render dim and black suits bright, since the display has no color.
|
||||
* have a bright marker beside their label, since the display has no color.
|
||||
*
|
||||
* Controls: scroll moves a cursor through the 16 locations (free cells,
|
||||
* foundations, then cascades, wrapping). Click selects a source, then click
|
||||
@@ -11,6 +11,10 @@
|
||||
* 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.
|
||||
* The cursor skips locations that can't take part in the move being built:
|
||||
* with nothing selected, empty spots and the foundations (never a source);
|
||||
* with a source selected, filled free cells and foundations that can't
|
||||
* accept the card (see isCursorEligible).
|
||||
* Safe cards auto-play to the foundations after every move.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
@@ -18,10 +22,12 @@ import { 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 { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts";
|
||||
import { TtfFont } from "../../graphics/ttf-font";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -30,10 +36,13 @@ import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
const labelFont = getFont("terminus16");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
ensurePreinstalledFonts();
|
||||
const fontPath = installedFontPath("Roboto-Regular.ttf");
|
||||
const largeFont = TtfFont.load(fontPath, 28) ?? getFont("terminus32");
|
||||
const mediumFont = TtfFont.load(fontPath, 18) ?? getFont("terminus24");
|
||||
const labelFont = TtfFont.load(fontPath, 14) ?? getFont("terminus16");
|
||||
const suitFont = TtfFont.load(fontPath, 10) ?? getFont("terminus16");
|
||||
const smallFont = TtfFont.load(fontPath, 12) ?? getFont("terminus12");
|
||||
|
||||
/** Cards are 0..51: suit = card % 4 (♠♥♣♦, alternating colors), rank 1..13. */
|
||||
const SUIT_CHARS = ["♠", "♥", "♣", "♦"] as const;
|
||||
@@ -52,10 +61,6 @@ function cardLabel(card: number): string {
|
||||
return RANK_CHARS[rankOf(card)]! + SUIT_CHARS[suitOf(card)]!;
|
||||
}
|
||||
|
||||
/** Red suits render dimmer than black so card color survives grayscale. */
|
||||
const BLACK_SHADE = 255;
|
||||
const RED_SHADE = 150;
|
||||
|
||||
const CARD_W = 60;
|
||||
const CARD_H = 36;
|
||||
const TOP_Y = 4;
|
||||
@@ -158,7 +163,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
moves: 0,
|
||||
cursor: LOC_CASCADE0,
|
||||
selected: null,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("freecell"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
newGame(window, false);
|
||||
@@ -248,6 +253,7 @@ function windowMenuItems(window: FreecellWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("freecell", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_NEW_GAME);
|
||||
},
|
||||
},
|
||||
@@ -291,25 +297,25 @@ function handleInput(window: FreecellWindow, event: InputEvent, frameId: number)
|
||||
function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId: number): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
window.cursor = (window.cursor + LOC_COUNT - 1) % LOC_COUNT;
|
||||
moveCursor(window, -1);
|
||||
break;
|
||||
case "scroll-down":
|
||||
window.cursor = (window.cursor + 1) % LOC_COUNT;
|
||||
moveCursor(window, 1);
|
||||
break;
|
||||
// Watch swipes are spatial over the two rows of eight columns: up/down
|
||||
// switch between the top row (free cells + foundations) and the cascades
|
||||
// keeping the column, left/right move within the row.
|
||||
// landing on the nearest usable column, left/right move within the row.
|
||||
case "swipe-up":
|
||||
if (window.cursor >= LOC_CASCADE0) window.cursor -= LOC_CASCADE0;
|
||||
if (window.cursor >= LOC_CASCADE0) moveCursorToRow(window, LOC_CELL0);
|
||||
break;
|
||||
case "swipe-down":
|
||||
if (window.cursor < LOC_CASCADE0) window.cursor += LOC_CASCADE0;
|
||||
if (window.cursor < LOC_CASCADE0) moveCursorToRow(window, LOC_CASCADE0);
|
||||
break;
|
||||
case "swipe-left":
|
||||
if (window.cursor % LOC_CASCADE0 > 0) window.cursor--;
|
||||
moveCursorInRow(window, -1);
|
||||
break;
|
||||
case "swipe-right":
|
||||
if (window.cursor % LOC_CASCADE0 < LOC_CASCADE0 - 1) window.cursor++;
|
||||
moveCursorInRow(window, 1);
|
||||
break;
|
||||
case "click":
|
||||
if (window.selected === null) {
|
||||
@@ -410,6 +416,72 @@ function locationHasCard(window: FreecellWindow, location: number): boolean {
|
||||
return window.cascades[location - LOC_CASCADE0]!.length > 0;
|
||||
}
|
||||
|
||||
function isFoundation(location: number): boolean {
|
||||
return location >= LOC_FOUNDATION0 && location < LOC_CASCADE0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the cursor should stop at a location, given the move being built.
|
||||
* Picking a source: it must hold a card, and foundations are never a source.
|
||||
* Picking a destination: an empty free cell, a foundation that accepts the
|
||||
* selected card, any cascade (a full legality check would hide the
|
||||
* cascades the player wants to compare), or the source itself (clicking it
|
||||
* again cancels the selection).
|
||||
*/
|
||||
function isCursorEligible(window: FreecellWindow, location: number): boolean {
|
||||
if (window.selected === null) {
|
||||
return !isFoundation(location) && locationHasCard(window, location);
|
||||
}
|
||||
if (location === window.selected) return true;
|
||||
if (location < LOC_FOUNDATION0) return window.cells[location] === null;
|
||||
if (isFoundation(location)) {
|
||||
const card = topCardAt(window, window.selected);
|
||||
return card !== null && foundationCanAccept(window.foundations[location - LOC_FOUNDATION0]!, card);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Step the cursor through the location list (wrapping), skipping ineligible spots. */
|
||||
function moveCursor(window: FreecellWindow, step: 1 | -1): void {
|
||||
let location = window.cursor;
|
||||
for (let i = 0; i < LOC_COUNT; i++) {
|
||||
location = (location + step + LOC_COUNT) % LOC_COUNT;
|
||||
if (isCursorEligible(window, location)) {
|
||||
window.cursor = location;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Step the cursor within its row (no wrapping), skipping ineligible spots. */
|
||||
function moveCursorInRow(window: FreecellWindow, step: 1 | -1): void {
|
||||
const rowStart = window.cursor < LOC_CASCADE0 ? LOC_CELL0 : LOC_CASCADE0;
|
||||
for (let location = window.cursor + step; location >= rowStart && location < rowStart + 8; location += step) {
|
||||
if (isCursorEligible(window, location)) {
|
||||
window.cursor = location;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jump to the other row, keeping the column when it is eligible and
|
||||
* otherwise taking the nearest eligible column; stays put when the row has
|
||||
* none.
|
||||
*/
|
||||
function moveCursorToRow(window: FreecellWindow, rowStart: number): void {
|
||||
const column = window.cursor % 8;
|
||||
for (let distance = 0; distance < 8; distance++) {
|
||||
for (const candidate of [column - distance, column + distance]) {
|
||||
if (candidate < 0 || candidate >= 8) continue;
|
||||
if (isCursorEligible(window, rowStart + candidate)) {
|
||||
window.cursor = rowStart + candidate;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** True if `card` can go on `onto` in a cascade (descending, alternating color). */
|
||||
function canStack(card: number, onto: number): boolean {
|
||||
return rankOf(onto) === rankOf(card) + 1 && isRed(onto) !== isRed(card);
|
||||
@@ -710,13 +782,27 @@ function paintCard(
|
||||
selected: boolean,
|
||||
fullyVisible: boolean,
|
||||
): void {
|
||||
const shade = isRed(card) ? RED_SHADE : BLACK_SHADE;
|
||||
image.fillRoundedRect(x, y, CARD_W, CARD_H, selected ? 60 : 25, 4);
|
||||
image.fillRoundedRect(x, y, CARD_W, CARD_H, 0, 4);
|
||||
image.drawRoundedRect(x, y, CARD_W, CARD_H, selected ? 255 : 160, 4);
|
||||
if (selected) image.drawRoundedRect(x + 1, y + 1, CARD_W - 2, CARD_H - 2, 255, 3);
|
||||
image.drawText(labelFont, x + 5, y + 2, cardLabel(card), shade);
|
||||
const rankText: string = RANK_CHARS[rankOf(card)]!
|
||||
const suitText: string = SUIT_CHARS[suitOf(card)]!;
|
||||
const labelText = rankText + suitText;
|
||||
image.drawText(labelFont, x + 5, y, rankText, 255);
|
||||
image.drawText(suitFont, x + 5 + labelFont.measureText(rankText), y + 3, suitText, 150);
|
||||
if (isRed(card)) {
|
||||
// Keep the marker within the top strip, clear of even a two-digit rank.
|
||||
const markerX = x + 28;
|
||||
image.fillRect(markerX, y + 1, Math.max(0, x + CARD_W - 4 - markerX), 13, 10);
|
||||
}
|
||||
if (fullyVisible) {
|
||||
image.drawText(mediumFont, x + CARD_W - 18, y + CARD_H - 29, SUIT_CHARS[suitOf(card)]!, shade);
|
||||
image.drawText(
|
||||
mediumFont,
|
||||
x + CARD_W - 4 - Math.ceil(mediumFont.measureText(suitText)),
|
||||
y + CARD_H - mediumFont.lineHeight - 2,
|
||||
suitText,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { glanceLayout, glanceShowLinesSetting, glanceSlotSettings } from "./glanceboard-settings";
|
||||
import { resolveGlanceRegions, slotDividers, type GlanceLayout, type GlanceRegion } from "./layout";
|
||||
import { type GlanceWidget } from "./widget";
|
||||
import { findGlanceWidget, glanceWidgetSpans } from "./widgets";
|
||||
|
||||
/** Brightness of the hairlines between regions: the dimmest shade that survives 4bpp quantization. */
|
||||
const DIVIDER_VALUE = 20;
|
||||
|
||||
type LiveRegion = { region: GlanceRegion; key: string; widget: GlanceWidget | null };
|
||||
|
||||
/** Identity of a region for diffing: which widget, in which slots. */
|
||||
function regionKey(region: GlanceRegion): string {
|
||||
return `${region.choice}@${region.slots.join(",")}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A board: the layout's slots filled with the widgets the settings choose,
|
||||
* painted as one image. A widget chosen for two vertically adjacent slots
|
||||
* (when it supports that) gets one double-height region with no divider
|
||||
* between the slots. Widgets are created and started when the board starts
|
||||
* and stopped when it stops; a settings change meanwhile restarts only the
|
||||
* regions that changed.
|
||||
*/
|
||||
export class GlanceBoard {
|
||||
private regions: LiveRegion[] = [];
|
||||
private started = false;
|
||||
private unsubscribeSettings: (() => void) | null = null;
|
||||
private currentLayout: GlanceLayout;
|
||||
|
||||
constructor(
|
||||
private readonly requestRender: () => void,
|
||||
private readonly fixedLayout?: GlanceLayout,
|
||||
) {
|
||||
this.currentLayout = fixedLayout ?? glanceLayout();
|
||||
}
|
||||
|
||||
get layout(): GlanceLayout {
|
||||
return this.currentLayout;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
return this.layout.width;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return this.layout.height;
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.started) return;
|
||||
this.started = true;
|
||||
this.applySlotSettings();
|
||||
this.unsubscribeSettings = onAnySettingChanged(() => this.applySlotSettings());
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (!this.started) return;
|
||||
this.started = false;
|
||||
this.unsubscribeSettings?.();
|
||||
this.unsubscribeSettings = null;
|
||||
for (const live of this.regions) live.widget?.stop();
|
||||
this.regions = [];
|
||||
}
|
||||
|
||||
private currentRegions(): GlanceRegion[] {
|
||||
const choices = glanceSlotSettings(this.layout).map((setting) => setting.get());
|
||||
return resolveGlanceRegions(this.layout, choices, glanceWidgetSpans);
|
||||
}
|
||||
|
||||
/** Rebuild the live regions from the settings, keeping widgets whose region is unchanged. */
|
||||
private applySlotSettings(): void {
|
||||
if (!this.started) return;
|
||||
const layout = this.fixedLayout ?? glanceLayout();
|
||||
const layoutChanged = layout !== this.currentLayout;
|
||||
this.currentLayout = layout;
|
||||
const previous = new Map(this.regions.map((live) => [live.key, live]));
|
||||
const next: LiveRegion[] = [];
|
||||
let changed = layoutChanged;
|
||||
for (const region of this.currentRegions()) {
|
||||
const key = regionKey(region);
|
||||
const kept = previous.get(key);
|
||||
if (kept) {
|
||||
previous.delete(key);
|
||||
next.push(kept);
|
||||
continue;
|
||||
}
|
||||
const widget = findGlanceWidget(region.choice)?.create() ?? null;
|
||||
widget?.start(this.requestRender);
|
||||
next.push({ region, key, widget });
|
||||
changed = true;
|
||||
}
|
||||
for (const removed of previous.values()) {
|
||||
removed.widget?.stop();
|
||||
changed = true;
|
||||
}
|
||||
this.regions = next;
|
||||
if (changed) this.requestRender();
|
||||
}
|
||||
|
||||
/** Paint the whole board (layout-sized). Widgets each get a fresh canvas of their region's size. */
|
||||
paint(): GrayImage {
|
||||
const image = new GrayImage(this.layout.width, this.layout.height, 0);
|
||||
for (const live of this.regions) {
|
||||
if (!live.widget) continue;
|
||||
const { rect } = live.region;
|
||||
const canvas = new GrayImage(rect.width, rect.height, 0);
|
||||
try {
|
||||
live.widget.paint(canvas);
|
||||
} catch (error) {
|
||||
console.warn(`glanceboard widget ${live.region.choice} paint failed`, error);
|
||||
}
|
||||
canvas.composeInto(image, rect.x, rect.y);
|
||||
}
|
||||
if (glanceShowLinesSetting.get()) {
|
||||
drawDividers(image, this.layout, this.regions.map((live) => live.region));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
function drawDividers(image: GrayImage, layout: GlanceLayout, regions: readonly GlanceRegion[]): void {
|
||||
for (const line of slotDividers(layout, regions)) {
|
||||
image.drawLine(line.x0, line.y0, line.x1, line.y1, DIVIDER_VALUE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { GlanceBoard } from "./board";
|
||||
import {
|
||||
clearConflictingSlots,
|
||||
glanceboardEnabledSetting,
|
||||
glanceLayout,
|
||||
glanceLayoutSetting,
|
||||
glanceShowLinesSetting,
|
||||
glanceShowOnHeadTiltSetting,
|
||||
glanceShowOnLongPressSetting,
|
||||
glanceSlotChoiceLabel,
|
||||
glanceSlotSettings,
|
||||
glanceTapDurationSetting,
|
||||
type GlanceSlotChoice,
|
||||
} from "./glanceboard-settings";
|
||||
import { resolveGlanceRegions, slotDividers } from "./layout";
|
||||
import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
|
||||
export const GLANCEBOARD_SURFACE_ID = "window:glanceboard";
|
||||
|
||||
const TITLE_X = 18;
|
||||
const TITLE_Y = 10;
|
||||
const PARAGRAPH_X = 24;
|
||||
/** The entry page splits at the middle: menu left, slot preview right. */
|
||||
const PREVIEW_MARGIN = 8;
|
||||
const PREVIEW_MIN_LINE_VALUE = 20;
|
||||
const PREVIEW_LINE_VALUE = 100;
|
||||
|
||||
const INTRO =
|
||||
"Your Glanceboard is a display for things you want to look at quickly. Starting " +
|
||||
"from the screen being off, you can show the glanceboard with a tap or " +
|
||||
"long-press-and-hold. It will close automatically.";
|
||||
|
||||
/** Full-page list layout shared by the sub-screens. */
|
||||
function pageMenuLayout(width: number) {
|
||||
return { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false } as const;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard app's entry page. Left half: title, a short description
|
||||
* and the menu (enable toggle, live preview, slot contents, settings). Right
|
||||
* half: a scaled preview of the board's slots with their dividers and the
|
||||
* name of the widget in each. "Select Contents" moves focus into that
|
||||
* preview, where scroll picks a slot and a tap opens the picker for it;
|
||||
* a double-tap returns focus to the menu.
|
||||
*/
|
||||
class GlanceboardHomeLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private menuTop = 0;
|
||||
private menuHeight = 0;
|
||||
private focus: "menu" | "preview" = "menu";
|
||||
private selectedSlot = 0;
|
||||
|
||||
constructor(private readonly setPreviewVisible: (visible: boolean) => void) {}
|
||||
|
||||
private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) {
|
||||
const half = (width / 2) | 0;
|
||||
const items: MenuItem[] = [
|
||||
toggleSettingMenuItem(glanceboardEnabledSetting),
|
||||
{
|
||||
label: "Preview",
|
||||
onSelect: (menuCtx) => {
|
||||
menuCtx.stack.push(new GlancePreviewLayer(
|
||||
() => menuCtx.actions.requestRender(),
|
||||
() => this.setPreviewVisible(false),
|
||||
));
|
||||
this.setPreviewVisible(true);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Select Contents",
|
||||
onSelect: () => {
|
||||
this.focus = "preview";
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Settings",
|
||||
onSelect: (menuCtx) => menuCtx.stack.push(new GlanceSettingsLayer()),
|
||||
},
|
||||
];
|
||||
this.menuTop = menuTop;
|
||||
this.menuHeight = height;
|
||||
this.menu = new MenuLayer(null, items, {
|
||||
x: 8, y: menuTop, width: half - 16,
|
||||
showBorder: false, minHeight: 0, maxHeight: height - menuTop,
|
||||
});
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
/** Where the scaled board sits: the right half, board aspect, vertically centred. */
|
||||
private previewRect(width: number, height: number): { x: number; y: number; width: number; height: number } {
|
||||
const half = (width / 2) | 0;
|
||||
const availableWidth = width - half - 2 * PREVIEW_MARGIN;
|
||||
const layout = glanceLayout();
|
||||
const scale = Math.min(availableWidth / layout.width, (height - 2 * PREVIEW_MARGIN) / layout.height);
|
||||
const previewWidth = Math.round(layout.width * scale);
|
||||
const previewHeight = Math.round(layout.height * scale);
|
||||
return {
|
||||
x: half + PREVIEW_MARGIN + (((availableWidth - previewWidth) / 2) | 0),
|
||||
y: ((height - previewHeight) / 2) | 0,
|
||||
width: previewWidth,
|
||||
height: previewHeight,
|
||||
};
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const half = (width / 2) | 0;
|
||||
image.drawText(font, TITLE_X, TITLE_Y, "Glanceboard", 220);
|
||||
const step = lineStep(font);
|
||||
let y = TITLE_Y + step + 6;
|
||||
for (const line of wrapText(font, INTRO, half - PARAGRAPH_X - 12)) {
|
||||
image.drawText(font, PARAGRAPH_X, y, line, 160);
|
||||
y += step;
|
||||
}
|
||||
// The menu paints onto this page below the paragraph (its fill is raster,
|
||||
// so the deferred paragraph glyphs above stay untouched either way).
|
||||
// While the preview has focus the menu keeps its outline-only selection.
|
||||
const menu = this.currentMenu(ctx, y + 4);
|
||||
const menuFocused = ctx.stack.isFocused() && this.focus === "menu";
|
||||
const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) };
|
||||
menu.paint(menuCtx, () => image);
|
||||
|
||||
this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1);
|
||||
const preview = this.previewRect(width, height);
|
||||
drawSlotPreview(image, preview, {
|
||||
selectedSlot: this.selectedSlot,
|
||||
selectionFocused: ctx.stack.isFocused() && this.focus === "preview",
|
||||
});
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.focus === "preview") {
|
||||
const slotCount = glanceLayout().slots.length;
|
||||
this.selectedSlot = Math.min(this.selectedSlot, slotCount - 1);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedSlot = (this.selectedSlot + slotCount - 1) % slotCount;
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedSlot = (this.selectedSlot + 1) % slotCount;
|
||||
return;
|
||||
case "click":
|
||||
openSlotPicker(ctx, this.selectedSlot);
|
||||
return;
|
||||
case "double-click":
|
||||
// Back from the preview is back to the menu, not out of the app;
|
||||
// this layer is the window's root and handles the yield itself
|
||||
// (see createGlanceboardAppWindow), so the gesture arrives here.
|
||||
this.focus = "menu";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (event.type === "double-click") {
|
||||
shell.yieldFocusToSidebar();
|
||||
return;
|
||||
}
|
||||
await this.currentMenu(ctx, this.menuTop).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The board's slots scaled into `rect`: dividers (full brightness, or the
|
||||
* dimmest dotted line when "Show lines" is off), the widget name centred in
|
||||
* each region (a merged double-height region has one name and no line
|
||||
* across it), and the selected slot highlighted.
|
||||
*/
|
||||
function drawSlotPreview(
|
||||
image: GrayImage,
|
||||
rect: { x: number; y: number; width: number; height: number },
|
||||
options: { selectedSlot: number; selectionFocused: boolean },
|
||||
): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const layout = glanceLayout();
|
||||
const settings = glanceSlotSettings(layout);
|
||||
const choices = settings.map((setting) => setting.get());
|
||||
const regions = resolveGlanceRegions(layout, choices, glanceWidgetSpans);
|
||||
const sx = rect.width / layout.width;
|
||||
const sy = rect.height / layout.height;
|
||||
const scaled = (r: { x: number; y: number; width: number; height: number }) => ({
|
||||
x: rect.x + Math.round(r.x * sx),
|
||||
y: rect.y + Math.round(r.y * sy),
|
||||
width: Math.round(r.width * sx),
|
||||
height: Math.round(r.height * sy),
|
||||
});
|
||||
|
||||
image.drawRect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2, 72);
|
||||
const selected = scaled(layout.slots[options.selectedSlot]!.rect);
|
||||
drawSelectionHighlight(image, selected.x + 1, selected.y + 1, selected.width - 2, selected.height - 2, options.selectionFocused, 4);
|
||||
|
||||
const showLines = glanceShowLinesSetting.get();
|
||||
for (const line of slotDividers(layout, regions)) {
|
||||
const x0 = rect.x + Math.round(line.x0 * sx);
|
||||
const y0 = rect.y + Math.round(line.y0 * sy);
|
||||
const x1 = rect.x + Math.round(line.x1 * sx);
|
||||
const y1 = rect.y + Math.round(line.y1 * sy);
|
||||
if (showLines) {
|
||||
image.drawLine(x0, y0, x1, y1, PREVIEW_LINE_VALUE);
|
||||
} else {
|
||||
// Every other pixel at the dimmest visible shade: the line is there to
|
||||
// read the layout by, but says "off".
|
||||
drawDottedLine(image, x0, y0, x1, y1, PREVIEW_MIN_LINE_VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
// Names: one per region, "Empty" in slots no region covers.
|
||||
const covered = new Set<number>();
|
||||
const label = (r: { x: number; y: number; width: number; height: number }, text: string, value: number) => {
|
||||
const clipped = truncateText(font, text, Math.max(0, r.width - 8));
|
||||
image.drawText(font, r.x + (((r.width - font.measureText(clipped)) / 2) | 0), r.y + (((r.height - font.lineHeight) / 2) | 0), clipped, value);
|
||||
};
|
||||
for (const region of regions) {
|
||||
for (const index of region.slots) covered.add(index);
|
||||
label(scaled(region.rect), glanceSlotChoiceLabel(region.choice as GlanceSlotChoice), 210);
|
||||
}
|
||||
layout.slots.forEach((slot, index) => {
|
||||
if (!covered.has(index)) label(scaled(slot.rect), "Empty", 90);
|
||||
});
|
||||
}
|
||||
|
||||
/** A horizontal or vertical line lit on every other pixel. */
|
||||
function drawDottedLine(image: GrayImage, x0: number, y0: number, x1: number, y1: number, value: number): void {
|
||||
if (y0 === y1) {
|
||||
for (let x = Math.min(x0, x1); x <= Math.max(x0, x1); x += 2) image.setPixel(x, y0, value);
|
||||
} else {
|
||||
for (let y = Math.min(y0, y1); y <= Math.max(y0, y1); y += 2) image.setPixel(x0, y, value);
|
||||
}
|
||||
}
|
||||
|
||||
/** The picker for one slot: every widget, the current one marked; choosing applies the one-per-board rule. */
|
||||
function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
|
||||
const layout = glanceLayout();
|
||||
const setting = glanceSlotSettings(layout)[slotIndex];
|
||||
if (!setting) return;
|
||||
const current = setting.get();
|
||||
const items: MenuItem[] = setting.values.map((value) => ({
|
||||
label: setting.displayValue(value),
|
||||
onSelect: (menuCtx) => {
|
||||
setting.set(value);
|
||||
clearConflictingSlots(layout, slotIndex, value);
|
||||
menuCtx.stack.pop();
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(getDefaultSmallFont(), x, y + LIST_ROW_TEXT_INSET, `${setting.displayValue(value)}${setting.get() === value ? " *" : ""}`, 200);
|
||||
},
|
||||
}));
|
||||
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
}
|
||||
|
||||
/** Settings: layout, sleep gestures, duration, and slot lines. */
|
||||
class GlanceSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
if (!this.menu) {
|
||||
const { width } = ctx.stack.getBaseSize();
|
||||
const items: MenuItem[] = [
|
||||
enumSettingMenuItem(glanceLayoutSetting),
|
||||
enumSettingMenuItem(glanceTapDurationSetting),
|
||||
toggleSettingMenuItem(glanceShowOnLongPressSetting),
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
];
|
||||
this.menu = new MenuLayer("Glanceboard settings", items, pageMenuLayout(width));
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/** The live board in the window; any click or double-click returns to the list. */
|
||||
class GlancePreviewLayer implements Layer {
|
||||
private readonly board: GlanceBoard;
|
||||
|
||||
constructor(requestRender: () => void, private readonly onClose: () => void) {
|
||||
this.board = new GlanceBoard(requestRender);
|
||||
this.board.start();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const board = this.board.paint();
|
||||
if (board.width === width && board.height === height) return board;
|
||||
// A viewport of another size (a per-app display override) shows the board
|
||||
// top-left, cropped or padded, rather than scaled.
|
||||
const image = new GrayImage(width, height, 0);
|
||||
board.composeInto(image, 0, 0);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "click" || event.type === "double-click") ctx.stack.pop();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.board.stop();
|
||||
this.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
export function createGlanceboardAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let previewVisible = false;
|
||||
const heightMode = () => previewVisible && glanceLayoutSetting.get() === "2x3" ? "max" as const : "medium" as const;
|
||||
let unsubscribeSettings: (() => void) | undefined;
|
||||
const app = createInProcessWindow({
|
||||
appId: "glanceboard",
|
||||
windowId: GLANCEBOARD_WINDOW_ID,
|
||||
title: "Glanceboard",
|
||||
iconLetter: "Gb",
|
||||
icon: "eye",
|
||||
closeable: true,
|
||||
// Settings and the contents picker stay in the default 576x288 viewport;
|
||||
// only the live six-slot preview needs the taller window.
|
||||
heightMode: heightMode(),
|
||||
actions: options.actions,
|
||||
// Not wrapped in YieldAtRootLayer: the home page routes double-click
|
||||
// itself (preview focus -> menu focus, menu focus -> sidebar).
|
||||
baseLayer: new GlanceboardHomeLayer((visible) => {
|
||||
previewVisible = visible;
|
||||
app.setHeightMode(heightMode());
|
||||
}),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
reconfigureSurface: options.reconfigureSurface,
|
||||
onClosed: () => {
|
||||
unsubscribeSettings?.();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribeSettings = onAnySettingChanged(() => app.setHeightMode(heightMode()));
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
|
||||
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
|
||||
import { type GlanceWidgetId } from "./widget";
|
||||
import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export type GlanceSlotChoice = GlanceWidgetId | "none";
|
||||
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "nightscout", "compass", "music"];
|
||||
|
||||
const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||
none: "Empty",
|
||||
"system-card": "System card",
|
||||
calendar: "Calendar",
|
||||
terminal: "Terminal",
|
||||
nightscout: "Nightscout",
|
||||
compass: "Compass",
|
||||
music: "Music",
|
||||
};
|
||||
|
||||
export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
|
||||
return SLOT_CHOICE_LABELS[choice] ?? choice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Slot order follows QUADRANT_LAYOUT: top left, top right, bottom left, bottom
|
||||
* right. Calendar in both right slots merges into one double-height region.
|
||||
*/
|
||||
const DEFAULT_QUADRANT_CHOICES: readonly GlanceSlotChoice[] = ["system-card", "calendar", "music", "calendar"];
|
||||
|
||||
export const glanceLayoutSetting = new ConfigSettingEnum<"2x2" | "2x3">({
|
||||
id: "glanceboard-layout",
|
||||
label: "Layout",
|
||||
storageKey: "glanceboard.layout",
|
||||
defaultValue: "2x2",
|
||||
values: ["2x2", "2x3"],
|
||||
description: "Choose two columns with two or three rows of widgets.",
|
||||
});
|
||||
|
||||
export function glanceLayout(): GlanceLayout {
|
||||
return glanceLayoutSetting.get() === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether sleep-time gestures show the board at all. Off restores the plain
|
||||
* behaviour: a tap, hold or head-tilt while asleep does nothing (a head-tilt
|
||||
* wakes the regular UI), double-tap wakes.
|
||||
*/
|
||||
export const glanceboardEnabledSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-enabled",
|
||||
label: "Enable Glanceboard",
|
||||
storageKey: "glanceboard.enabled",
|
||||
defaultValue: false,
|
||||
description:
|
||||
"While the display is asleep, a tap, a long-press or a head-tilt shows the Glanceboard instead of the regular UI. Double-tap still wakes the regular UI.",
|
||||
});
|
||||
|
||||
export type GlanceTapDuration = "off" | "3s" | "5s" | "7s" | "10s";
|
||||
|
||||
const DEFAULT_TAP_DURATION: GlanceTapDuration = "5s";
|
||||
|
||||
/**
|
||||
* How long a tap (or head-tilt) keeps the board up; each further tap restarts
|
||||
* it. "off" means a single tap does not show the board at all; a head-tilt
|
||||
* then uses the default duration.
|
||||
*/
|
||||
export const glanceTapDurationSetting = new ConfigSettingEnum<GlanceTapDuration>({
|
||||
id: "glanceboard-tap-duration",
|
||||
label: "Show on tap",
|
||||
storageKey: "glanceboard.tapDuration",
|
||||
defaultValue: DEFAULT_TAP_DURATION,
|
||||
values: ["off", "3s", "5s", "7s", "10s"],
|
||||
formatValue: (value) => (value === "off" ? "Disabled" : value.replace("s", " s")),
|
||||
description:
|
||||
"How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.",
|
||||
});
|
||||
|
||||
/** Whether a single tap while asleep shows the board. */
|
||||
export function glanceShowOnTap(): boolean {
|
||||
return glanceTapDurationSetting.get() !== "off";
|
||||
}
|
||||
|
||||
export function glanceTapTimeoutMs(): number {
|
||||
const duration = glanceTapDurationSetting.get();
|
||||
const seconds = duration === "off" ? DEFAULT_TAP_DURATION : duration;
|
||||
return Number.parseInt(seconds, 10) * 1000;
|
||||
}
|
||||
|
||||
/** A long-press holds the board up until the press is released. */
|
||||
export const glanceShowOnLongPressSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-on-long-press",
|
||||
label: "Show on long press",
|
||||
storageKey: "glanceboard.showOnLongPress",
|
||||
defaultValue: true,
|
||||
description: "A long-press or short-then-long-press while asleep shows the Glanceboard until the press is released.",
|
||||
});
|
||||
|
||||
/** The head-tilt wake shows the board (for the tap duration) instead of the regular UI. */
|
||||
export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-on-head-tilt",
|
||||
label: "Show on head tilt",
|
||||
storageKey: "glanceboard.showOnHeadTilt",
|
||||
defaultValue: true,
|
||||
description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.",
|
||||
});
|
||||
|
||||
/** Hairlines between the board's slots. */
|
||||
export const glanceShowLinesSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-lines",
|
||||
label: "Show lines",
|
||||
storageKey: "glanceboard.showLines",
|
||||
defaultValue: true,
|
||||
description: "Draw the separator lines between the Glanceboard's slots.",
|
||||
});
|
||||
|
||||
/**
|
||||
* Both grid sizes share the original storage keys so changing row count
|
||||
* preserves the first four choices and remembers the hidden bottom row.
|
||||
*/
|
||||
export function glanceSlotSettings(layout: GlanceLayout = glanceLayout()): ConfigSettingEnum<GlanceSlotChoice>[] {
|
||||
const grid = layout === QUADRANT_LAYOUT || layout === SIX_SLOT_LAYOUT;
|
||||
const storageId = grid ? QUADRANT_LAYOUT.id : layout.id;
|
||||
return layout.slots.map(
|
||||
(slot, index) =>
|
||||
new ConfigSettingEnum<GlanceSlotChoice>({
|
||||
id: `glanceboard-${layout.id}-slot-${index}`,
|
||||
label: slot.label,
|
||||
storageKey: `glanceboard.${storageId}.slot.${index}`,
|
||||
defaultValue: (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
|
||||
values: SLOT_CHOICES,
|
||||
formatValue: glanceSlotChoiceLabel,
|
||||
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A slot was just set to `choice`: each widget appears on the board once, so
|
||||
* clear every other slot holding the same choice, except a vertically
|
||||
* adjacent one when the widget can span two slots (it then renders at
|
||||
* double height). Returns the indices cleared.
|
||||
*/
|
||||
export function clearConflictingSlots(layout: GlanceLayout, changedIndex: number, choice: GlanceSlotChoice): number[] {
|
||||
if (choice === "none") return [];
|
||||
const settings = glanceSlotSettings(layout);
|
||||
const spans = glanceWidgetSpans(choice);
|
||||
const cleared: number[] = [];
|
||||
let keptPartner = false;
|
||||
settings.forEach((setting, index) => {
|
||||
if (index === changedIndex || setting.get() !== choice) return;
|
||||
if (spans && !keptPartner && slotsVerticallyAdjacent(layout, changedIndex, index)) {
|
||||
keptPartner = true;
|
||||
return;
|
||||
}
|
||||
setting.set("none");
|
||||
cleared.push(index);
|
||||
});
|
||||
return cleared;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { GlanceBoard } from "./board";
|
||||
import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app";
|
||||
import {
|
||||
glanceboardEnabledSetting,
|
||||
glanceLayout,
|
||||
glanceShowOnHeadTiltSetting,
|
||||
glanceShowOnLongPressSetting,
|
||||
glanceShowOnTap,
|
||||
glanceTapTimeoutMs,
|
||||
} from "./glanceboard-settings";
|
||||
|
||||
const glanceboardApp: AppDefinition = {
|
||||
appId: "glanceboard",
|
||||
title: "Glanceboard",
|
||||
icon: "eye",
|
||||
launch: (ctx) => ctx.launchInProcessApp(GLANCEBOARD_WINDOW_ID, GLANCEBOARD_SURFACE_ID, createGlanceboardAppWindow),
|
||||
glanceboard: {
|
||||
get size() {
|
||||
const layout = glanceLayout();
|
||||
return { width: layout.width, height: layout.height };
|
||||
},
|
||||
isEnabled: () => glanceboardEnabledSetting.get(),
|
||||
showOnTap: glanceShowOnTap,
|
||||
tapTimeoutMs: glanceTapTimeoutMs,
|
||||
showOnLongPress: () => glanceShowOnLongPressSetting.get(),
|
||||
showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(),
|
||||
createBoard: (requestRender) => new GlanceBoard(requestRender),
|
||||
},
|
||||
};
|
||||
|
||||
export default glanceboardApp;
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Board geometry. A layout names its slots and places them on the board;
|
||||
* the Glanceboard app's slot pickers and the board renderer are both driven
|
||||
* by it, so a replacement layout (different slot count, sizes, positions) is
|
||||
* a matter of swapping this object out and reusing the same widgets.
|
||||
*/
|
||||
export const GLANCEBOARD_WIDTH = 576;
|
||||
export const GLANCEBOARD_HEIGHT = 288;
|
||||
|
||||
export type GlanceSlotRect = { x: number; y: number; width: number; height: number };
|
||||
|
||||
export type GlanceLayout = {
|
||||
id: string;
|
||||
width: number;
|
||||
height: number;
|
||||
slots: readonly { label: string; rect: GlanceSlotRect }[];
|
||||
};
|
||||
|
||||
/** The first layout: four equal quadrants, each 288x144. */
|
||||
export const QUADRANT_LAYOUT: GlanceLayout = {
|
||||
id: "quadrants",
|
||||
width: GLANCEBOARD_WIDTH,
|
||||
height: GLANCEBOARD_HEIGHT,
|
||||
slots: [
|
||||
{ label: "Top left", rect: { x: 0, y: 0, width: 288, height: 144 } },
|
||||
{ label: "Top right", rect: { x: 288, y: 0, width: 288, height: 144 } },
|
||||
{ label: "Bottom left", rect: { x: 0, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Bottom right", rect: { x: 288, y: 144, width: 288, height: 144 } },
|
||||
],
|
||||
};
|
||||
|
||||
/** Six 288x144 slots: the same first two rows, plus a third at the bottom. */
|
||||
export const SIX_SLOT_LAYOUT: GlanceLayout = {
|
||||
id: "six-slots",
|
||||
width: GLANCEBOARD_WIDTH,
|
||||
height: 432,
|
||||
slots: [
|
||||
...QUADRANT_LAYOUT.slots.slice(0, 2),
|
||||
{ label: "Middle left", rect: { x: 0, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Middle right", rect: { x: 288, y: 144, width: 288, height: 144 } },
|
||||
{ label: "Bottom left", rect: { x: 0, y: 288, width: 288, height: 144 } },
|
||||
{ label: "Bottom right", rect: { x: 288, y: 288, width: 288, height: 144 } },
|
||||
],
|
||||
};
|
||||
|
||||
/** Whether two slots stack vertically with matching width (one region if merged). */
|
||||
export function slotsVerticallyAdjacent(layout: GlanceLayout, a: number, b: number): boolean {
|
||||
const first = layout.slots[a]?.rect;
|
||||
const second = layout.slots[b]?.rect;
|
||||
if (!first || !second || a === b) return false;
|
||||
if (first.x !== second.x || first.width !== second.width) return false;
|
||||
return first.y + first.height === second.y || second.y + second.height === first.y;
|
||||
}
|
||||
|
||||
/** A painted area of the board: one slot, or two vertically adjacent slots merged. */
|
||||
export type GlanceRegion = {
|
||||
/** The slot choice (widget id) filling the region. */
|
||||
choice: string;
|
||||
rect: GlanceSlotRect;
|
||||
/** Indices into layout.slots, ascending. */
|
||||
slots: number[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn per-slot choices into regions. A widget chosen for two vertically
|
||||
* adjacent slots, when `canSpan` says it supports it, becomes one
|
||||
* double-height region; every other non-empty slot is a region of its own
|
||||
* (a duplicate that cannot merge simply paints twice). Regions come out in
|
||||
* slot order.
|
||||
*/
|
||||
export function resolveGlanceRegions(
|
||||
layout: GlanceLayout,
|
||||
choices: readonly string[],
|
||||
canSpan: (choice: string) => boolean,
|
||||
): GlanceRegion[] {
|
||||
const regions: GlanceRegion[] = [];
|
||||
const consumed = new Set<number>();
|
||||
layout.slots.forEach((slot, index) => {
|
||||
if (consumed.has(index)) return;
|
||||
const choice = choices[index] ?? "none";
|
||||
if (choice === "none") return;
|
||||
consumed.add(index);
|
||||
let rect = { ...slot.rect };
|
||||
const slots = [index];
|
||||
if (canSpan(choice)) {
|
||||
const partner = layout.slots.findIndex(
|
||||
(_, other) => !consumed.has(other) && choices[other] === choice && slotsVerticallyAdjacent(layout, index, other),
|
||||
);
|
||||
if (partner >= 0) {
|
||||
consumed.add(partner);
|
||||
slots.push(partner);
|
||||
const other = layout.slots[partner]!.rect;
|
||||
const top = Math.min(rect.y, other.y);
|
||||
rect = { x: rect.x, y: top, width: rect.width, height: Math.max(rect.y + rect.height, other.y + other.height) - top };
|
||||
}
|
||||
}
|
||||
regions.push({ choice, rect, slots });
|
||||
});
|
||||
return regions;
|
||||
}
|
||||
|
||||
/** A divider line between slots, in board coordinates (inclusive endpoints). */
|
||||
export type GlanceDivider = { x0: number; y0: number; x1: number; y1: number };
|
||||
|
||||
/**
|
||||
* The hairlines between slots: every slot's left edge (when not at the
|
||||
* board's left) and top edge (when not at the top), except the top edge of
|
||||
* the lower slot of a merged double-height region, which is that region's
|
||||
* interior. Shared by the board painter and the config app's preview.
|
||||
*/
|
||||
export function slotDividers(layout: GlanceLayout, regions: readonly GlanceRegion[]): GlanceDivider[] {
|
||||
const dividers: GlanceDivider[] = [];
|
||||
layout.slots.forEach(({ rect }, index) => {
|
||||
if (rect.x > 0) dividers.push({ x0: rect.x, y0: rect.y, x1: rect.x, y1: rect.y + rect.height - 1 });
|
||||
if (rect.y === 0) return;
|
||||
const interior = regions.some(
|
||||
(region) => region.slots.length > 1 && region.slots.includes(index) && region.rect.y < rect.y,
|
||||
);
|
||||
if (!interior) dividers.push({ x0: rect.x, y0: rect.y, x1: rect.x + rect.width - 1, y1: rect.y });
|
||||
});
|
||||
return dividers;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { type GrayImage } from "../../graphics/image";
|
||||
|
||||
/**
|
||||
* A Glanceboard widget: a small self-contained view that paints into a slot
|
||||
* of the board. Widgets are sized by the layout (the first layout is four
|
||||
* 288x144 quadrants) and own their data subscriptions and any hardware they
|
||||
* need between start and stop, so a board that is not showing costs nothing.
|
||||
*
|
||||
* Layouts are expected to be swapped out and iterated on; widgets are the
|
||||
* stable part, so keep them independent of any particular board geometry
|
||||
* beyond the slot size they are handed.
|
||||
*/
|
||||
export interface GlanceWidget {
|
||||
/**
|
||||
* The board became visible (or this widget was added to a visible board):
|
||||
* subscribe to data, start timers, enable hardware. `requestRender` asks
|
||||
* the board to repaint when something changed.
|
||||
*/
|
||||
start(requestRender: () => void): void;
|
||||
/** Undo start. Called once for every start; must be safe to call twice. */
|
||||
stop(): void;
|
||||
/**
|
||||
* Paint the widget into `image`, a fresh 0-filled canvas of the slot's
|
||||
* size. Deferred draws (text, drawImage) are welcome and keep the cheap
|
||||
* texture-cache wire path.
|
||||
*/
|
||||
paint(image: GrayImage): void;
|
||||
}
|
||||
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal";
|
||||
|
||||
export type GlanceWidgetDefinition = {
|
||||
id: GlanceWidgetId;
|
||||
/** Picker label in the Glanceboard app. */
|
||||
label: string;
|
||||
/**
|
||||
* The widget can fill two vertically adjacent slots as one double-height
|
||||
* region (chosen for both left or both right quadrants). Its paint then
|
||||
* gets the taller canvas; the divider between the slots is not drawn.
|
||||
*/
|
||||
tall?: boolean;
|
||||
create: () => GlanceWidget;
|
||||
};
|
||||
@@ -0,0 +1,137 @@
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { hasCalendarPermission } from "../../../g2/android-permissions";
|
||||
import { readUpcomingEvents, type CalendarEvent } from "../../../native/calendar";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { dayHeaderLabel, formatEventTime } from "../../calendar/calendar";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
||||
const BOTTOM_MARGIN = 2;
|
||||
const MINUTE_MS = 60_000;
|
||||
const HOUR_MS = 60 * MINUTE_MS;
|
||||
|
||||
/**
|
||||
* What is next on the calendar: the current or next event large, with how
|
||||
* far off it is (or when it ends), then the following events grouped under
|
||||
* day headers, as many as fit. Reads the same cached provider query as the
|
||||
* Calendar app; a minute tick keeps the countdown honest and lets the
|
||||
* 30-second cache refresh on its own.
|
||||
*/
|
||||
export class CalendarWidget implements GlanceWidget {
|
||||
private tick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.tick = setInterval(requestRender, MINUTE_MS);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.tick !== null) clearInterval(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const step = lineStep(small);
|
||||
const textWidth = image.width - 2 * PAD;
|
||||
|
||||
if (!hasCalendarPermission()) {
|
||||
image.drawText(small, PAD, PAD, "Calendar", 150);
|
||||
image.drawText(small, PAD, PAD + step + 4, "Calendar permission needed", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, "Open the Calendar app to grant it", 100);
|
||||
return;
|
||||
}
|
||||
const nowMs = Date.now();
|
||||
// Events still in progress count; the provider's window starts at "now",
|
||||
// so the list can include an event that began earlier and is still on.
|
||||
const events = readUpcomingEvents().filter((event) => event.endMs > nowMs);
|
||||
if (!events.length) {
|
||||
image.drawText(small, PAD, PAD, "Calendar", 150);
|
||||
image.drawText(small, PAD, PAD + step + 4, "No upcoming events", 140);
|
||||
return;
|
||||
}
|
||||
|
||||
// Headline: the current or next event, with its day when it is not today.
|
||||
const next = events[0]!;
|
||||
let y = PAD;
|
||||
const headline = truncateText(medium, next.title || "(untitled)", textWidth);
|
||||
image.drawText(medium, PAD, y, headline, 230);
|
||||
y += medium.lineHeight + 2;
|
||||
image.drawText(small, PAD, y, truncateText(small, headlineDetail(next, nowMs), textWidth), 170);
|
||||
y += step + 6;
|
||||
|
||||
// The rest, grouped by day. A day header only where the day changes from
|
||||
// the previous listed event (the headline's day included).
|
||||
let previousDayKey = dayKeyOf(next.startMs);
|
||||
for (let index = 1; index < events.length; index++) {
|
||||
const event = events[index]!;
|
||||
const dayKey = dayKeyOf(event.startMs);
|
||||
const needsHeader = dayKey !== previousDayKey;
|
||||
// The header and its row are placed a step apart; the row only needs
|
||||
// its line height to fit above the bottom margin.
|
||||
if (y + (needsHeader ? step : 0) + small.lineHeight > image.height - BOTTOM_MARGIN) break;
|
||||
if (needsHeader) {
|
||||
image.drawText(small, PAD, y, truncateText(small, dayHeaderLabel(event.startMs), textWidth), 120);
|
||||
y += step;
|
||||
previousDayKey = dayKey;
|
||||
}
|
||||
drawEventLine(image, small, PAD, y, textWidth, event, 190);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** "Now, ends 14:30", "In 25 min", "Tomorrow 09:00", "All day"... for the headline. */
|
||||
function headlineDetail(event: CalendarEvent, nowMs: number): string {
|
||||
const dayDelta = dayDeltaFromToday(event.startMs);
|
||||
const when = event.allDay
|
||||
? "All day"
|
||||
: `${formatEventTime(event.startMs)} - ${formatEventTime(event.endMs)}`;
|
||||
if (event.startMs <= nowMs) {
|
||||
if (event.allDay) return dayDelta === 0 ? "All day today" : `All day, ${dayHeaderLabel(event.startMs)}`;
|
||||
return `Now, ends ${formatEventTime(event.endMs)}`;
|
||||
}
|
||||
const untilMs = event.startMs - nowMs;
|
||||
if (dayDelta === 0 && untilMs < 12 * HOUR_MS) {
|
||||
return `In ${formatDuration(untilMs)}, ${when}`;
|
||||
}
|
||||
const day = dayDelta === 0 ? "Today" : dayDelta === 1 ? "Tomorrow" : dayHeaderLabel(event.startMs);
|
||||
return `${day}, ${when}`;
|
||||
}
|
||||
|
||||
function drawEventLine(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
event: CalendarEvent,
|
||||
value: number,
|
||||
): void {
|
||||
const time = event.allDay ? "All day" : formatEventTime(event.startMs);
|
||||
const timeWidth = font.measureText(time);
|
||||
image.drawText(font, x, y, time, value - 50);
|
||||
const titleX = x + timeWidth + 8;
|
||||
image.drawText(font, titleX, y, truncateText(font, event.title || "(untitled)", Math.max(0, x + width - titleX)), value);
|
||||
}
|
||||
|
||||
function formatDuration(ms: number): string {
|
||||
const minutes = Math.max(1, Math.round(ms / MINUTE_MS));
|
||||
if (minutes < 60) return `${minutes} min`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const rest = minutes % 60;
|
||||
return rest ? `${hours} h ${rest} min` : `${hours} h`;
|
||||
}
|
||||
|
||||
function dayKeyOf(timestampMs: number): string {
|
||||
const date = new Date(timestampMs);
|
||||
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
|
||||
}
|
||||
|
||||
function dayDeltaFromToday(timestampMs: number): number {
|
||||
const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
return Math.round((midnight(new Date(timestampMs)) - midnight(new Date())) / (24 * HOUR_MS));
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { hasLocationPermission } from "../../../g2/android-permissions";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
|
||||
import { getNorthReference, resolveHeading } from "../../compass/heading";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
/** Owner token for the refcounted magnetometer, distinct from the Compass app's. */
|
||||
const COMPASS_OWNER = "glanceboard";
|
||||
const TOP_PAD = 4;
|
||||
const BOTTOM_PAD = 4;
|
||||
const EDGE_PAD = 8;
|
||||
const MAX_ROSE_RADIUS = 60;
|
||||
|
||||
/**
|
||||
* The Compass app's rose at card size: heading readout above, tilted rose
|
||||
* below. Turns the magnetometer on for as long as the board shows it (the
|
||||
* Compass app keeps its own hold, so the two never switch each other off).
|
||||
* Never prompts for location: true north is used when a declination is
|
||||
* already known, otherwise the readout says it is magnetic.
|
||||
*/
|
||||
export class CompassWidget implements GlanceWidget {
|
||||
private rawHeading: number | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private unsubscribeDeclination: (() => void) | null = null;
|
||||
private enabled = false;
|
||||
private background: { key: string; image: GrayImage } | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = addCompassListener((event) => {
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||
requestRender();
|
||||
}
|
||||
});
|
||||
this.unsubscribeDeclination = onDeclinationChanged(requestRender);
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
setCompassEnabled(true, COMPASS_OWNER);
|
||||
}
|
||||
if (getNorthReference() === "true" && hasLocationPermission()) refreshDeclination();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.unsubscribeDeclination?.();
|
||||
this.unsubscribeDeclination = null;
|
||||
if (this.enabled) {
|
||||
this.enabled = false;
|
||||
setCompassEnabled(false, COMPASS_OWNER);
|
||||
}
|
||||
}
|
||||
|
||||
private statusText(): string | null {
|
||||
if (this.rawHeading === null) return "Waiting for compass";
|
||||
if (!isCompassCalibrated()) return "Uncalibrated";
|
||||
if (getNorthReference() === "true" && getDeclinationAvailability() !== "available") return "Magnetic";
|
||||
return null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const { width, height } = image;
|
||||
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
|
||||
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||
const status = this.statusText();
|
||||
const textHeight = medium.lineHeight + (status ? small.lineHeight + 2 : 0);
|
||||
const cx = width / 2;
|
||||
const { cy, radius, ringTop } = layoutCompassRose({
|
||||
width,
|
||||
height,
|
||||
cx,
|
||||
topClearance: TOP_PAD + textHeight + 4,
|
||||
bottomPad: BOTTOM_PAD,
|
||||
edgePad: EDGE_PAD,
|
||||
maxRadius: MAX_ROSE_RADIUS,
|
||||
minRadius: 16,
|
||||
});
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
const clipY = y + textHeight + 4;
|
||||
const key = [width, height, cx, cy, radius, clipY, fade].join(",");
|
||||
if (this.background?.key !== key) {
|
||||
this.background = { key, image: createCompassBackground(width, height, cx, cy, radius, clipY, fade) };
|
||||
}
|
||||
// composeInto keeps the background's cached interior texture as a
|
||||
// deferred image draw, so the wire planner can restore it from cache.
|
||||
this.background.image.composeInto(image, 0, 0);
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
image.drawText(medium, Math.round(cx - medium.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||
if (status) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(status) / 2), y + medium.lineHeight + 2, status, 125);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { type GlanceWidgetDefinition, type GlanceWidgetId } from "../widget";
|
||||
import { CalendarWidget } from "./calendar-widget";
|
||||
import { CompassWidget } from "./compass-widget";
|
||||
import { MusicWidget } from "./music-widget";
|
||||
import { NightscoutWidget } from "./nightscout-widget";
|
||||
import { SystemCardWidget } from "./system-card";
|
||||
import { TerminalWidget } from "./terminal-widget";
|
||||
|
||||
/** Every widget a board slot can hold. */
|
||||
export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
{ id: "system-card", label: "System card", create: () => new SystemCardWidget() },
|
||||
{ id: "calendar", label: "Calendar", tall: true, create: () => new CalendarWidget() },
|
||||
{ id: "terminal", label: "Terminal", tall: true, create: () => new TerminalWidget() },
|
||||
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||
];
|
||||
|
||||
export function findGlanceWidget(id: GlanceWidgetId | string): GlanceWidgetDefinition | null {
|
||||
return GLANCE_WIDGETS.find((widget) => widget.id === id) ?? null;
|
||||
}
|
||||
|
||||
/** Whether a slot choice may span two vertically adjacent slots (see GlanceWidgetDefinition.tall). */
|
||||
export function glanceWidgetSpans(choice: string): boolean {
|
||||
return Boolean(findGlanceWidget(choice)?.tall);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import { GrayImage } from "../../../graphics/image";
|
||||
import { renderIcon } from "../../../graphics/icons";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
|
||||
import { clamp } from "../../../util/numeric-util";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const ART_SIZE = 96;
|
||||
/** The idle note: the Music app's icon, at this size and the dimmest shade. */
|
||||
const IDLE_NOTE_SIZE = 64;
|
||||
const IDLE_NOTE_VALUE = 20;
|
||||
let idleNote: GrayImage | null | undefined;
|
||||
|
||||
/**
|
||||
* The music icon reduced to a single dim shade: every pixel the renderer
|
||||
* lit at least half becomes IDLE_NOTE_VALUE, the rest stay transparent
|
||||
* (scaling the antialiased original down would quantize its edges away).
|
||||
* Built once and kept, so drawImage's content-addressed cache holds.
|
||||
*/
|
||||
function idleNoteImage(): GrayImage | null {
|
||||
if (idleNote !== undefined) return idleNote;
|
||||
const icon = renderIcon("music", IDLE_NOTE_SIZE);
|
||||
if (!icon) {
|
||||
idleNote = null;
|
||||
return null;
|
||||
}
|
||||
const note = new GrayImage(icon.width, icon.height, 0);
|
||||
for (let y = 0; y < icon.height; y++) {
|
||||
for (let x = 0; x < icon.width; x++) {
|
||||
if (icon.getPixel(x, y) >= 128) note.setPixel(x, y, IDLE_NOTE_VALUE);
|
||||
}
|
||||
}
|
||||
idleNote = note;
|
||||
return note;
|
||||
}
|
||||
const ART_X = PAD;
|
||||
const PROGRESS_BAR_HEIGHT = 5;
|
||||
|
||||
/**
|
||||
* Now playing: album art, title/artist/album and a progress bar for the
|
||||
* active Android media session, the Music app's header at card size. The
|
||||
* media bridge runs for the whole session; the widget subscribes and ticks
|
||||
* once a second only while something is playing.
|
||||
*/
|
||||
export class MusicWidget implements GlanceWidget {
|
||||
private media: MediaControllerState = mediaControllerBridge.snapshot();
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||
this.media = state;
|
||||
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
|
||||
this.progressTimer ??= setInterval(requestRender, 1_000);
|
||||
} else {
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
|
||||
private stopProgressTimer(): void {
|
||||
if (this.progressTimer !== null) clearInterval(this.progressTimer);
|
||||
this.progressTimer = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
this.media = media;
|
||||
if (!media.available || (!media.title && !media.artist)) {
|
||||
if (!media.accessEnabled) {
|
||||
// Actionable, so it stays as text.
|
||||
image.drawText(small, PAD, PAD, "Music", 150);
|
||||
image.drawText(small, PAD, PAD + small.lineHeight + 6, "Notification access needed", 140);
|
||||
return;
|
||||
}
|
||||
// Nothing playing: a dim note, centred, and nothing else.
|
||||
const note = idleNoteImage();
|
||||
if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
|
||||
this.drawArt(image, media, artY);
|
||||
|
||||
const textX = ART_X + ART_SIZE + 12;
|
||||
const textWidth = image.width - textX - PAD;
|
||||
let y = artY;
|
||||
image.drawText(medium, textX, y, truncateText(medium, media.title || "Unknown title", textWidth), 230);
|
||||
y += medium.lineHeight + 2;
|
||||
if (media.artist) {
|
||||
image.drawText(small, textX, y, truncateText(small, media.artist, textWidth), 170);
|
||||
y += small.lineHeight + 2;
|
||||
}
|
||||
if (media.album) {
|
||||
image.drawText(small, textX, y, truncateText(small, media.album, textWidth), 120);
|
||||
}
|
||||
|
||||
// Progress pinned to the art's bottom edge; the state label sits beside it.
|
||||
const barY = artY + ART_SIZE - PROGRESS_BAR_HEIGHT;
|
||||
const timeY = barY - small.lineHeight - 3;
|
||||
const stateLabel = media.playbackState === "playing" ? "" : media.playbackState === "paused" ? "Paused" : media.playbackState;
|
||||
if (media.durationMs > 0 && media.positionMs >= 0) {
|
||||
const elapsed = formatMediaTime(media.positionMs);
|
||||
const duration = formatMediaTime(media.durationMs);
|
||||
image.drawText(small, textX, timeY, elapsed, 140);
|
||||
image.drawText(small, textX + textWidth - small.measureText(duration), timeY, duration, 140);
|
||||
if (stateLabel) {
|
||||
image.drawText(small, textX + (((textWidth - small.measureText(stateLabel)) / 2) | 0), timeY, stateLabel, 140);
|
||||
}
|
||||
image.drawRect(textX, barY, textWidth, PROGRESS_BAR_HEIGHT, 55);
|
||||
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
|
||||
image.fillRect(textX + 1, barY + 1, Math.round((textWidth - 2) * progress), PROGRESS_BAR_HEIGHT - 2, 170);
|
||||
} else if (stateLabel) {
|
||||
image.drawText(small, textX, timeY, stateLabel, 140);
|
||||
}
|
||||
}
|
||||
|
||||
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
|
||||
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||
// Retry while null: players often publish metadata before the bitmap.
|
||||
if (key !== this.artKey || this.art === null) {
|
||||
this.artKey = key;
|
||||
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||
}
|
||||
if (this.art) {
|
||||
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
|
||||
const dy = artY + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
|
||||
image.bitBlt(this.art, dx, dy);
|
||||
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
|
||||
} else {
|
||||
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
|
||||
const font = getDefaultSmallFont();
|
||||
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
|
||||
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function formatMediaTime(milliseconds: number): string {
|
||||
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
return hours > 0
|
||||
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
|
||||
: `${minutes}:${String(seconds).padStart(2, "0")}`;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
|
||||
import { isNightscoutSettingsConfigured, loadNightscoutThresholds } from "../../../ui/dashboard-settings";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { formatAgeShortFromTimestamp } from "~/util/date-util";
|
||||
import { evaluateNightscoutAlerts } from "../../nightscout/nightscout-alerts";
|
||||
import {
|
||||
drawDirectionIndicator,
|
||||
drawNightscoutGraph,
|
||||
drawNightscoutValueStrikeThrough,
|
||||
formatDelta,
|
||||
isNightscoutPointStale,
|
||||
} from "../../nightscout/nightscout";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
/** Width of the readout column left of the graph. */
|
||||
const READOUT_WIDTH = 96;
|
||||
|
||||
/**
|
||||
* Current glucose (struck through once older than the app's staleness
|
||||
* limit) with delta, trend, age and IOB beside the Nightscout app's 2-hour
|
||||
* graph, plus any of cannula age, reservoir, pump battery and loop age that
|
||||
* breach the limits in Nightscout settings, inverted as the app shows them. The bridge polls on its own for the whole session, so the
|
||||
* widget only subscribes; a minute tick keeps the age and staleness moving
|
||||
* during a stalled fetch.
|
||||
*/
|
||||
export class NightscoutWidget implements GlanceWidget {
|
||||
private state: NightscoutState = nightscoutBridge.snapshot();
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private tick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = nightscoutBridge.onStateChange((state) => {
|
||||
this.state = state;
|
||||
requestRender();
|
||||
});
|
||||
this.tick = setInterval(requestRender, 60_000);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.tick !== null) clearInterval(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const small = getDefaultSmallFont();
|
||||
const large = getDefaultLargeFont();
|
||||
const state = this.state;
|
||||
const nowMs = Date.now();
|
||||
const step = lineStep(small);
|
||||
image.drawText(small, PAD, PAD, "Nightscout", 150);
|
||||
|
||||
if (!isNightscoutSettingsConfigured() || state.configurationMissing) {
|
||||
image.drawText(small, PAD, PAD + step + 4, "Not configured", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, "Set the site URL in the Nightscout app", 100);
|
||||
return;
|
||||
}
|
||||
const latest = state.latest;
|
||||
if (!state.available || !latest) {
|
||||
image.drawText(small, PAD, PAD + step + 4, "No data", 140);
|
||||
image.drawText(small, PAD, PAD + 2 * step + 4, truncateText(small, state.status, image.width - 2 * PAD), 100);
|
||||
return;
|
||||
}
|
||||
|
||||
// Readout column: big value, units, then delta + trend, age and IOB.
|
||||
const glucoseText = `${latest.sgv}`;
|
||||
const glucoseY = PAD + step;
|
||||
const glucoseWidth = large.measureText(glucoseText);
|
||||
image.drawText(large, PAD, glucoseY, glucoseText, 230);
|
||||
if (isNightscoutPointStale(latest, nowMs)) {
|
||||
// Same rule as the app: older than NIGHTSCOUT_STALE_MS (15 min) gets a
|
||||
// line through the digits at mid-height.
|
||||
drawNightscoutValueStrikeThrough(image, PAD, glucoseY + (large.lineHeight >> 1), glucoseWidth);
|
||||
}
|
||||
const unitsX = PAD + glucoseWidth + 4;
|
||||
if (unitsX + small.measureText(state.units) <= PAD + READOUT_WIDTH) {
|
||||
image.drawText(small, unitsX, glucoseY + large.lineHeight - small.lineHeight, state.units, 140);
|
||||
}
|
||||
let y = glucoseY + large.lineHeight + 2;
|
||||
const deltaText = `${formatDelta(state.delta)} `;
|
||||
image.drawText(small, PAD, y, deltaText, 180);
|
||||
drawDirectionIndicator(image, small, PAD + small.measureText(deltaText), y, state.direction, 180);
|
||||
y += step;
|
||||
image.drawText(small, PAD, y, `${formatAgeShortFromTimestamp(latest.timestampMs, nowMs)} ago`, 130);
|
||||
y += step;
|
||||
image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160);
|
||||
y += step;
|
||||
|
||||
// Breached limits only, one per line while they fit, inverted like the
|
||||
// app's warning fields. Nothing here means nothing is out of range.
|
||||
const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs);
|
||||
const warnings: string[] = [];
|
||||
if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`);
|
||||
if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`);
|
||||
if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`);
|
||||
if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`);
|
||||
for (const warning of warnings) {
|
||||
if (y + small.lineHeight > image.height - 2) break;
|
||||
const text = truncateText(small, warning, READOUT_WIDTH - 4);
|
||||
image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230);
|
||||
image.drawText(small, PAD, y, text, 1);
|
||||
y += step;
|
||||
}
|
||||
|
||||
const graphX = PAD + READOUT_WIDTH + PAD;
|
||||
drawNightscoutGraph(
|
||||
image,
|
||||
{ x: graphX, y: PAD, width: image.width - graphX - PAD, height: image.height - 2 * PAD },
|
||||
state,
|
||||
nowMs,
|
||||
small,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery";
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../../../native/phone-battery";
|
||||
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
|
||||
import {
|
||||
batteryDisplayModeSetting,
|
||||
batteryIndicatorVisible,
|
||||
glassesBatteryVisibilitySetting,
|
||||
onAnySettingChanged,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../../../ui/dashboard-settings";
|
||||
import { shell } from "../../../ui/shell/shell";
|
||||
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { timerEngine } from "../../timer/timer-engine";
|
||||
import { formatCountdown, sortTimers, timerDisplayName, timerPhase, timerRemainingMs } from "../../timer/timer-model";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const NOTIFICATION_ICON_SIZE = 24;
|
||||
const NOTIFICATION_ICON_GAP = 4;
|
||||
const BATTERY_ITEM_GAP = 12;
|
||||
const BATTERY_LABEL_VALUE = 150;
|
||||
|
||||
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||
|
||||
/**
|
||||
* Date and time, the phone's notification icons, the phone/watch/G2/R1
|
||||
* battery indicators (the Wear OS watch's only while one is reachable), and
|
||||
* the Timers app's countdowns: the top bar's contents plus timers, laid out
|
||||
* for a card. Battery style and per-device visibility
|
||||
* follow Settings > Display > Battery indicators, so the card agrees with
|
||||
* the bar. Countdowns tick once a second only while one is running.
|
||||
*/
|
||||
export class SystemCardWidget implements GlanceWidget {
|
||||
private requestRender: (() => void) | null = null;
|
||||
private minuteTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private secondTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribeNotifications: (() => void) | null = null;
|
||||
private unsubscribeSettings: (() => void) | null = null;
|
||||
private unsubscribeTimers: (() => void) | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.scheduleMinuteTick();
|
||||
this.unsubscribeNotifications = onAndroidNotificationPosted(() => this.requestRender?.());
|
||||
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
|
||||
this.unsubscribeTimers = timerEngine.onChange(() => {
|
||||
this.syncSecondTick();
|
||||
this.requestRender?.();
|
||||
});
|
||||
this.syncSecondTick();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.requestRender = null;
|
||||
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||
this.minuteTimer = null;
|
||||
if (this.secondTimer !== null) clearInterval(this.secondTimer);
|
||||
this.secondTimer = null;
|
||||
this.unsubscribeNotifications?.();
|
||||
this.unsubscribeNotifications = null;
|
||||
this.unsubscribeSettings?.();
|
||||
this.unsubscribeSettings = null;
|
||||
this.unsubscribeTimers?.();
|
||||
this.unsubscribeTimers = null;
|
||||
}
|
||||
|
||||
/** Countdowns change every second; run that clock only while one is running. */
|
||||
private syncSecondTick(): void {
|
||||
const running = timerEngine.timers().some((timer) => timerPhase(timer) === "running");
|
||||
if (running && this.secondTimer === null) {
|
||||
this.secondTimer = setInterval(() => this.requestRender?.(), 1_000);
|
||||
} else if (!running && this.secondTimer !== null) {
|
||||
clearInterval(this.secondTimer);
|
||||
this.secondTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Repaint on the minute boundary, so the clock never shows a stale minute. */
|
||||
private scheduleMinuteTick(): void {
|
||||
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||
const now = Date.now();
|
||||
const untilNextMinute = 60_000 - (now % 60_000) + 50;
|
||||
this.minuteTimer = setTimeout(() => {
|
||||
this.minuteTimer = null;
|
||||
if (!this.requestRender) return;
|
||||
this.requestRender();
|
||||
this.scheduleMinuteTick();
|
||||
}, untilNextMinute);
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const large = getDefaultLargeFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const now = new Date();
|
||||
|
||||
// Clock and date down the left; batteries take the right edge.
|
||||
const batteryLeft = drawBatteryBlock(image, small, image.width - PAD, PAD);
|
||||
const textWidth = Math.max(0, batteryLeft - BATTERY_ITEM_GAP - PAD);
|
||||
const timeText = formatClockTime(now);
|
||||
const timeFont = large.measureText(timeText) <= textWidth ? large : medium;
|
||||
let y = PAD;
|
||||
image.drawText(timeFont, PAD, y, timeText, 230);
|
||||
y += timeFont.lineHeight + 2;
|
||||
image.drawText(medium, PAD, y, formatClockDate(now), 170);
|
||||
y += medium.lineHeight + 4;
|
||||
|
||||
// Notification icons along the bottom, as many as fit; the Timers app's
|
||||
// countdowns take the band between the date and the icons.
|
||||
const iconY = image.height - PAD - NOTIFICATION_ICON_SIZE;
|
||||
drawTimers(image, small, PAD, y, textWidth, iconY - 4, now.getTime());
|
||||
const maxIcons = Math.max(0, ((image.width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
|
||||
if (maxIcons > 0) {
|
||||
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
|
||||
if (stale) noteStaleDataUsed();
|
||||
for (let index = 0; index < icons.length; index++) {
|
||||
image.drawImage(icons[index]!, PAD + index * (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP), iconY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function collectBatteryItems(): BatteryItem[] {
|
||||
const items: BatteryItem[] = [];
|
||||
const push = (visibility: BatteryIndicatorVisibility, label: string, percent: number | null, charging: boolean | null) => {
|
||||
if (percent === null || !Number.isFinite(percent)) return;
|
||||
const clamped = Math.max(0, Math.min(100, Math.round(percent)));
|
||||
if (!batteryIndicatorVisible(visibility, clamped)) return;
|
||||
items.push({ label, percent: clamped, charging: Boolean(charging) });
|
||||
};
|
||||
const phone = readPhoneBatteryState();
|
||||
push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging);
|
||||
const levels = shell.getBatteryLevels();
|
||||
push(watchBatteryVisibilitySetting.get(), "Watch", levels.watch, levels.watchCharging);
|
||||
push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging);
|
||||
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
|
||||
push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* The battery indicators right-aligned at `right`, in the configured style.
|
||||
* Side-by-side styles put the label beside the gauge (or percentage) on one
|
||||
* line; stacked styles centre the label above it. Returns the block's left
|
||||
* edge (or `right` when nothing is shown).
|
||||
*/
|
||||
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): number {
|
||||
const items = collectBatteryItems();
|
||||
if (!items.length) return right;
|
||||
const mode = batteryDisplayModeSetting.get();
|
||||
const percentage = mode === "percentage" || mode === "stacked-percentage";
|
||||
const stacked = mode === "stacked" || mode === "stacked-percentage";
|
||||
const gaugeHeight = drawBattery(0, false).height;
|
||||
let x = right;
|
||||
for (let index = items.length - 1; index >= 0; index--) {
|
||||
const item = items[index]!;
|
||||
const percentText = `${item.percent}%`;
|
||||
const labelWidth = font.measureText(item.label);
|
||||
const valueWidth = percentage ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
|
||||
if (stacked) {
|
||||
const itemWidth = Math.max(labelWidth, valueWidth);
|
||||
x -= itemWidth;
|
||||
image.drawText(font, x + (((itemWidth - labelWidth) / 2) | 0), top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + (((itemWidth - valueWidth) / 2) | 0), top + font.lineHeight + 2, item, percentage, gaugeHeight);
|
||||
} else {
|
||||
x -= labelWidth + 5 + valueWidth;
|
||||
image.drawText(font, x, top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + labelWidth + 5, top, item, percentage, gaugeHeight);
|
||||
}
|
||||
x -= BATTERY_ITEM_GAP;
|
||||
}
|
||||
return x + BATTERY_ITEM_GAP;
|
||||
}
|
||||
|
||||
function drawBatteryValue(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
lineTop: number,
|
||||
item: BatteryItem,
|
||||
percentage: boolean,
|
||||
gaugeHeight: number,
|
||||
): void {
|
||||
if (percentage) {
|
||||
const percentText = `${item.percent}%`;
|
||||
if (item.charging) {
|
||||
// Inverted text marks charging, as in the top bar.
|
||||
image.fillRect(x - 2, lineTop - 1, font.measureText(percentText) + 4, font.lineHeight + 2, 255);
|
||||
image.drawText(font, x, lineTop, percentText, 1);
|
||||
} else {
|
||||
image.drawText(font, x, lineTop, percentText, 200);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const icon = drawBattery(item.percent, item.charging);
|
||||
image.bitBlt(icon, x, lineTop + Math.max(0, ((font.lineHeight - gaugeHeight) / 2) | 0), { transparentZero: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* The Timers app's countdowns, soonest first: remaining time (or "Done" once
|
||||
* rung, inverted so it is noticed; "paused" for a paused one) and the
|
||||
* timer's name, one per line while they fit between `top` and `bottom`.
|
||||
*/
|
||||
function drawTimers(image: GrayImage, font: UiFont, x: number, top: number, width: number, bottom: number, nowMs: number): void {
|
||||
const timers = sortTimers(timerEngine.timers(), nowMs);
|
||||
const step = lineStep(font);
|
||||
let y = top;
|
||||
for (const timer of timers) {
|
||||
if (y + font.lineHeight > bottom) break;
|
||||
const phase = timerPhase(timer);
|
||||
const clock = phase === "rung" ? "Done" : formatCountdown(timerRemainingMs(timer, nowMs));
|
||||
const clockWidth = font.measureText(clock);
|
||||
if (phase === "rung") {
|
||||
image.fillRect(x - 2, y - 1, clockWidth + 4, font.lineHeight + 2, 230);
|
||||
image.drawText(font, x, y, clock, 1);
|
||||
} else {
|
||||
image.drawText(font, x, y, clock, phase === "paused" ? 120 : 200);
|
||||
}
|
||||
const nameX = x + clockWidth + 8;
|
||||
const name = phase === "paused" ? `${timerDisplayName(timer)} (paused)` : timerDisplayName(timer);
|
||||
image.drawText(font, nameX, y, truncateText(font, name, Math.max(0, x + width - nameX)), 160);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
|
||||
import {
|
||||
drawSessionRow,
|
||||
isSessionRowActive,
|
||||
SESSION_ACTIVITY_STEP_MS,
|
||||
TERMINAL_SESSIONS_STATE_KEY,
|
||||
type TerminalSessionsSnapshot,
|
||||
} from "../../terminal/session-list";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const GUTTER_X = PAD;
|
||||
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
||||
const BOTTOM_MARGIN = 2;
|
||||
|
||||
/**
|
||||
* The Terminal app's session list at card size: the same hosts, sessions,
|
||||
* activity indicators and open-window numbers as the hub window, painted
|
||||
* with the same row painter from the snapshot the Terminal worker publishes.
|
||||
* The indicator animates on the hub's cadence while any session is active;
|
||||
* each row carries its own activity deadline, so nothing needs the worker
|
||||
* once the snapshot is in.
|
||||
*/
|
||||
export class TerminalWidget implements GlanceWidget {
|
||||
private snapshot: TerminalSessionsSnapshot | undefined = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private requestRender: (() => void) | null = null;
|
||||
private phase = 0;
|
||||
private animationTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
|
||||
this.unsubscribe = onWorkerStateChanged<TerminalSessionsSnapshot>(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
|
||||
this.snapshot = snapshot;
|
||||
this.syncAnimation();
|
||||
requestRender();
|
||||
});
|
||||
this.syncAnimation();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.requestRender = null;
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.animationTimer !== null) clearInterval(this.animationTimer);
|
||||
this.animationTimer = null;
|
||||
}
|
||||
|
||||
private anyActive(nowMs = Date.now()): boolean {
|
||||
return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs))));
|
||||
}
|
||||
|
||||
/** Run the indicator clock only while something is active; the last tick clears expired ones. */
|
||||
private syncAnimation(): void {
|
||||
const shouldRun = this.anyActive();
|
||||
if (shouldRun && this.animationTimer === null) {
|
||||
this.phase = 0;
|
||||
this.animationTimer = setInterval(() => {
|
||||
this.phase = (this.phase + 1) % 2;
|
||||
this.requestRender?.();
|
||||
if (!this.anyActive()) this.syncAnimation();
|
||||
}, SESSION_ACTIVITY_STEP_MS);
|
||||
} else if (!shouldRun && this.animationTimer !== null) {
|
||||
clearInterval(this.animationTimer);
|
||||
this.animationTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const step = lineStep(font);
|
||||
const textWidth = image.width - 2 * PAD;
|
||||
const snapshot = this.snapshot;
|
||||
const nowMs = Date.now();
|
||||
let y = PAD;
|
||||
image.drawText(font, PAD, y, "Terminal", 150);
|
||||
y += step;
|
||||
|
||||
if (!snapshot) {
|
||||
image.drawText(font, PAD, y, "Open the Terminal app to connect", 120);
|
||||
return;
|
||||
}
|
||||
const hosts = snapshot.hosts;
|
||||
const sessionCount = hosts.reduce((sum, host) => sum + host.sessions.length, 0);
|
||||
if (!hosts.length || !sessionCount) {
|
||||
const text = !snapshot.configured
|
||||
? "No connections configured"
|
||||
: !hosts.length
|
||||
? snapshot.status
|
||||
: "No live sessions";
|
||||
image.drawText(font, PAD, y, truncateText(font, text, textWidth), 120);
|
||||
return;
|
||||
}
|
||||
|
||||
// Rows are placed a line step apart but only need their line height to
|
||||
// fit, so the last one may sit right against the bottom margin.
|
||||
const fits = (top: number) => top + font.lineHeight <= image.height - BOTTOM_MARGIN;
|
||||
const multiHost = hosts.length > 1;
|
||||
for (const host of hosts) {
|
||||
if (multiHost) {
|
||||
if (!fits(y)) return;
|
||||
image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110);
|
||||
y += step;
|
||||
}
|
||||
for (const row of host.sessions) {
|
||||
if (!fits(y)) return;
|
||||
drawSessionRow(image, font, {
|
||||
x: GUTTER_X,
|
||||
y,
|
||||
width: image.width - GUTTER_X - PAD,
|
||||
label: row.label,
|
||||
openGlyph: row.openGlyph,
|
||||
active: isSessionRowActive(row, nowMs),
|
||||
phase: this.phase,
|
||||
value: 200,
|
||||
});
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ const DEFAULT_ASSIGNMENTS: Record<string, string> = {
|
||||
freecell: "Games",
|
||||
minesweeper: "Games",
|
||||
pinball: "Games",
|
||||
flappy: "Games",
|
||||
calculator: "Prototypes",
|
||||
microphones: "Prototypes",
|
||||
nightscout: "Prototypes",
|
||||
|
||||
@@ -20,8 +20,9 @@ declare const com: any;
|
||||
* Host-side controller for the CFW mic_control array: sends the same config
|
||||
* to both temples (each temple is its own endpoint and its own 2-mic array),
|
||||
* tracks per-temple field-104 status, and keeps the 90 s streaming lease
|
||||
* renewed while capture is armed. Degrades cleanly on firmware without the
|
||||
* feature: micControlSupported() is false and apply() refuses to arm.
|
||||
* renewed while capture is armed. The feature is part of the required
|
||||
* firmware revision, so the only gate is the developer setting:
|
||||
* micControlSupported() is false and apply() refuses to arm when it is off.
|
||||
*/
|
||||
|
||||
const CONFIG_SETTING_KEY = "microphones.array-config";
|
||||
@@ -38,26 +39,13 @@ function activeCommunicator(): any {
|
||||
}
|
||||
}
|
||||
|
||||
/** True when the connected glasses advertise the CFW mic_control feature. */
|
||||
export function micControlAdvertised(): boolean {
|
||||
const communicator = activeCommunicator();
|
||||
if (!communicator) return false;
|
||||
try {
|
||||
const caps = String(communicator.getFirmwareCapabilities() ?? "");
|
||||
return caps.trim().split(/\s+/).includes("micctl");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the connected glasses advertise the CFW mic_control feature AND
|
||||
* the "Use microphone control" developer setting is on. With the setting off
|
||||
* (the default), Faceclaw behaves exactly as it would on firmware without the
|
||||
* micctl caps token.
|
||||
* True when the "Use microphone control" developer setting is on. With the
|
||||
* setting off, Faceclaw behaves exactly as it would on firmware without the
|
||||
* mic-control channel (one mixed stream).
|
||||
*/
|
||||
export function micControlSupported(): boolean {
|
||||
return useMicControlSetting.get() && micControlAdvertised();
|
||||
return useMicControlSetting.get();
|
||||
}
|
||||
|
||||
export function loadMicConfig(): MicConfig {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/**
|
||||
* Wire codec for the CFW mic_control feature (g2flash `microphone-configurations`
|
||||
* branch, EVENCFW/16, caps tokens micctl/micmc/micraw). Mirrors the contract
|
||||
* header in g2flash patches/mic_control.c:
|
||||
* Wire codec for the CFW mic_control feature. Mirrors the contract header in
|
||||
* g2flash patches/mic_control.c:
|
||||
*
|
||||
* field 103 (write) ['M','C', ver, op, <9 config bytes for CONFIGURE>]
|
||||
* field 104 (read) 21-byte ['M','C', ver, active, src, chanMask, codec,
|
||||
|
||||
@@ -730,7 +730,10 @@ class MicSession {
|
||||
},
|
||||
});
|
||||
engine.setListener(this.captionListenerProxy);
|
||||
engine.setAsrModelDir(isAsrModelReady() ? moonshineModelDir() : null);
|
||||
// Live captions are a separate sherpa-onnx pipeline (FaceclawCaptionEngine)
|
||||
// that only ever uses Moonshine; unaffected by the two-model on-device
|
||||
// Whisper addition in native/voice-control.ts's push-to-talk path.
|
||||
engine.setAsrModelDir(isAsrModelReady("moonshine") ? moonshineModelDir() : null);
|
||||
engine.setSpeakerModelPath(micModelPath("speaker-embedding"));
|
||||
engine.start();
|
||||
this.captionEngine = engine;
|
||||
|
||||
@@ -23,7 +23,7 @@ import { isAsrModelReady, startAsrModelDownload, asrModelState } from "../../nat
|
||||
import { CaptionsLayer } from "./captions-layer";
|
||||
import { LevelsLayer } from "./levels-layer";
|
||||
import { RadarLayer } from "./radar-layer";
|
||||
import { loadMicConfig, micArrayController, micControlAdvertised, micControlSupported, saveMicConfig } from "./mic-control";
|
||||
import { loadMicConfig, micArrayController, micControlSupported, saveMicConfig } from "./mic-control";
|
||||
import { isMicModelReady, micModelState, startMicModelDownload } from "./mic-models";
|
||||
import { micChannelLabel, type MicChannelKey, type MicConfig } from "./mic-protocol";
|
||||
import { micSession } from "./mic-session";
|
||||
@@ -166,7 +166,7 @@ function micSetupMenu(): MenuLayer {
|
||||
{
|
||||
label: "Array status",
|
||||
description:
|
||||
"Per-temple mic-control status. Requires the mic-control custom firmware (caps token micctl) and the Use microphone control developer setting.",
|
||||
"Per-temple mic-control status. Requires the Use microphone control developer setting.",
|
||||
onSelect: (ctx) => {
|
||||
micArrayController.query();
|
||||
const { left, right } = micArrayController.getStatuses();
|
||||
@@ -186,9 +186,7 @@ function micSetupMenu(): MenuLayer {
|
||||
"Array status",
|
||||
micControlSupported()
|
||||
? `${describe("Left", left)}\n${describe("Right", right)}\n${consistency}`
|
||||
: micControlAdvertised()
|
||||
? "Mic-control is available but disabled. Turn on Settings > Developer > Use microphone control to control the four mics individually."
|
||||
: "The connected firmware does not advertise mic-control. Flash the microphone-configurations CFW build to control the four mics individually; on stock firmware the glasses stream one mixed channel with a firmware-computed direction of arrival.",
|
||||
: "Mic-control is disabled. Turn on Settings > Developer > Use microphone control to control the four mics individually; while it is off the glasses stream one mixed channel with a firmware-computed direction of arrival.",
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -330,17 +328,20 @@ function voiceSpeakersMenu(): MenuLayer {
|
||||
() => startMicModelDownload("speaker-embedding"),
|
||||
),
|
||||
modelItem(
|
||||
// Live captions always use Moonshine (see the matching note in
|
||||
// mic-session.ts); the second on-device model (Whisper) is a
|
||||
// push-to-talk dictation option only, managed in Settings > Voice.
|
||||
"Caption ASR model",
|
||||
isAsrModelReady,
|
||||
() => isAsrModelReady("moonshine"),
|
||||
() => {
|
||||
const state = asrModelState();
|
||||
const state = asrModelState("moonshine");
|
||||
if (state.status === "ready") return "ready";
|
||||
if (state.status === "downloading") {
|
||||
return `${Math.round((state.bytesDownloaded / state.totalBytes) * 100)}%`;
|
||||
}
|
||||
return "download (141 MB)";
|
||||
},
|
||||
startAsrModelDownload,
|
||||
() => startAsrModelDownload("moonshine"),
|
||||
),
|
||||
modelItem(
|
||||
"Re-diarization model",
|
||||
|
||||
@@ -9,9 +9,13 @@
|
||||
* scroll moves along the row, click reveals (or chords a satisfied number),
|
||||
* long-press toggles a flag, double-click returns to row-select.
|
||||
* Watch swipes skip the two-layer scheme and move the cell cursor in four
|
||||
* directions; a watch double-click pauses directly.
|
||||
* directions; a watch double-click pauses directly. Tap-then-hold opens the
|
||||
* window menu in any phase, pausing a live game first.
|
||||
* Paused/won/lost: click resumes or starts a new game, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus. Losing input focus mid-game (a shell overlay such as the system
|
||||
* menu or a notification, focus to the sidebar) pauses, as does
|
||||
* backgrounding or screen-off. The difficulty setting persists across
|
||||
* launches.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
@@ -19,9 +23,11 @@ import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../gra
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
|
||||
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -55,6 +61,8 @@ const DIFFICULTIES = [
|
||||
{ name: "Medium", mines: 20 },
|
||||
{ name: "Hard", mines: 26 },
|
||||
] as const;
|
||||
/** Persisted difficulty, stored by name so a reordering can't misfile it. */
|
||||
const DIFFICULTY_KEY = "minesweeper.difficulty";
|
||||
|
||||
/** Number glyph shades by adjacent-mine count; higher counts read brighter. */
|
||||
const COUNT_SHADES = [0, 140, 170, 200, 220, 235, 245, 250, 250];
|
||||
@@ -155,7 +163,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
focused: false,
|
||||
menu: null,
|
||||
phase: "playing",
|
||||
difficultyIndex: 0,
|
||||
difficultyIndex: loadDifficultyIndex(),
|
||||
mines: new Uint8Array(COLS * ROWS),
|
||||
counts: new Uint8Array(COLS * ROWS),
|
||||
cellState: new Uint8Array(COLS * ROWS),
|
||||
@@ -169,7 +177,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
elapsedMs: 0,
|
||||
runningSinceMs: null,
|
||||
tickTimer: null,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("minesweeper"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -215,6 +223,19 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses, so the
|
||||
// clock stops and the board is hidden while the player can't act.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncClock(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -237,6 +258,24 @@ function inferForeground(window: MinesweeperWindow, focused: boolean): void {
|
||||
syncClock(window);
|
||||
}
|
||||
|
||||
function loadDifficultyIndex(): number {
|
||||
try {
|
||||
const name = getStringSetting(DIFFICULTY_KEY, "");
|
||||
const index = DIFFICULTIES.findIndex((difficulty) => difficulty.name === name);
|
||||
return index >= 0 ? index : 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function saveDifficultyIndex(window: MinesweeperWindow): void {
|
||||
try {
|
||||
setStringSetting(DIFFICULTY_KEY, DIFFICULTIES[window.difficultyIndex]!.name);
|
||||
} catch (error) {
|
||||
console.warn(`minesweeper difficulty save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
|
||||
* steps on its own timer, and the Java call is safe from the worker thread.
|
||||
@@ -252,7 +291,7 @@ function playSfx(window: MinesweeperWindow, steps: Step[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for flagging. */
|
||||
/** The window's context menu (game actions). Tap-then-hold reaches it in any phase; playing keeps long-press for flagging. */
|
||||
function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
const nextDifficulty = DIFFICULTIES[(window.difficultyIndex + 1) % DIFFICULTIES.length]!;
|
||||
return [
|
||||
@@ -269,6 +308,7 @@ function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.difficultyIndex = (window.difficultyIndex + 1) % DIFFICULTIES.length;
|
||||
saveDifficultyIndex(window);
|
||||
resetGame(window);
|
||||
playSfx(window, SFX_RESUME);
|
||||
},
|
||||
@@ -278,6 +318,7 @@ function windowMenuItems(window: MinesweeperWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("minesweeper", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -290,7 +331,7 @@ function windowMenu(window: MinesweeperWindow): WindowMenu {
|
||||
windowId: window.windowId,
|
||||
post,
|
||||
title: () => window.title,
|
||||
items: () => (window.phase === "playing" ? [] : windowMenuItems(window)),
|
||||
items: () => windowMenuItems(window),
|
||||
claimsLongPress: () => window.phase === "playing",
|
||||
size: { width: window.viewportWidth, height: window.viewportHeight },
|
||||
paintBase: () => paintContent(window),
|
||||
@@ -367,7 +408,11 @@ 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.
|
||||
// The menu's actions (new game, difficulty) all discard the board, and
|
||||
// the clock must not run under it, so pause first; the board is hidden
|
||||
// while paused, so the menu can't be used to study it off the clock.
|
||||
window.phase = "paused";
|
||||
syncClock(window);
|
||||
windowMenu(window).open();
|
||||
break;
|
||||
case "double-click":
|
||||
|
||||
@@ -68,7 +68,7 @@ class MusicAppLayer implements Layer {
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
"Notification access is required before Android exposes media sessions. Click to open settings.",
|
||||
"Notification access is required to control Android media sessions. Tap to open settings.",
|
||||
width - 48,
|
||||
);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
|
||||
@@ -28,12 +28,14 @@ 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 { openUrlOnPhone } from "../../native/open-url";
|
||||
import {
|
||||
navigateDestinationAddressDraftSetting,
|
||||
navigateDestinationNameDraftSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
mapboxApiKeySetting,
|
||||
enumSettingMenuItem,
|
||||
type ConfigSettingString,
|
||||
} from "../../ui/dashboard-settings";
|
||||
@@ -68,6 +70,7 @@ import {
|
||||
fetchStaticMapGray,
|
||||
geocodeForward,
|
||||
isMapboxConfigured,
|
||||
MAPBOX_TOKEN_SETTING_KEY,
|
||||
type GeocodeCandidate,
|
||||
type Route,
|
||||
type RouteProfile,
|
||||
@@ -190,10 +193,14 @@ 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. */
|
||||
/**
|
||||
* An entry on the idle page's list: a destination, or (while no Mapbox
|
||||
* token is set) one of the setup actions offered in its place.
|
||||
*/
|
||||
type IdleEntry =
|
||||
| { kind: "saved"; destination: SavedDestination }
|
||||
| { kind: "recent"; destination: RecentDestination };
|
||||
| { kind: "recent"; destination: RecentDestination }
|
||||
| { kind: "action"; label: string; detail: string; run: () => void };
|
||||
|
||||
/** Idle-page list selection (index into idleEntries()). */
|
||||
let idleSelection = 0;
|
||||
@@ -208,6 +215,8 @@ type DestinationEdit = {
|
||||
setting: ConfigSettingString;
|
||||
title: string;
|
||||
onDone: (value: string) => void;
|
||||
/** Runs on double-click (cancel); edits of a live setting restore it here. */
|
||||
onCancel?: () => void;
|
||||
};
|
||||
let editing: DestinationEdit | null = null;
|
||||
|
||||
@@ -251,7 +260,9 @@ function post(message: WorkerAppReply): void {
|
||||
// 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 token key repaints too: the phone editor writes it live during
|
||||
// Edit token, and the idle page changes shape once one is set.
|
||||
if (key.startsWith("navigate.") || key === MAPBOX_TOKEN_SETTING_KEY) render();
|
||||
});
|
||||
|
||||
// The host queues messages until this arrives: posts to a worker whose bundle
|
||||
@@ -374,7 +385,7 @@ function startNavigation(query: string, requestedProfile: RouteProfile): Promise
|
||||
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.";
|
||||
statusMessage = "Navigation needs a Mapbox token. This is free (up to a usage limit); open Mapbox in a browser on your phone or pick Edit token below.";
|
||||
phase = "idle";
|
||||
render();
|
||||
throw new Error(statusMessage);
|
||||
@@ -1036,8 +1047,13 @@ function closeMenusAnd(ctx: LayerContext, action: () => void): void {
|
||||
* 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 };
|
||||
function beginEdit(
|
||||
setting: ConfigSettingString,
|
||||
title: string,
|
||||
onDone: (value: string) => void,
|
||||
onCancel?: () => void,
|
||||
): void {
|
||||
editing = { setting, title, onDone, onCancel };
|
||||
post({ type: "start-text-setting-edit", settingId: setting.id });
|
||||
render();
|
||||
}
|
||||
@@ -1047,6 +1063,7 @@ function finishEdit(confirmed: boolean): void {
|
||||
editing = null;
|
||||
post({ type: "end-text-setting-edit" });
|
||||
if (current && confirmed) current.onDone(current.setting.get());
|
||||
else if (current) current.onCancel?.();
|
||||
// onDone may have started the next step; only fully leave when it didn't.
|
||||
if (!editing) {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
@@ -1075,10 +1092,52 @@ function beginAddDestination(): void {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit the Mapbox token in place: the phone editor writes the real setting
|
||||
* live, so cancelling puts back whatever was there before.
|
||||
*/
|
||||
function beginTokenEdit(): void {
|
||||
const previous = mapboxApiKeySetting.get();
|
||||
beginEdit(
|
||||
mapboxApiKeySetting,
|
||||
mapboxApiKeySetting.glassesEditTitle,
|
||||
() => {
|
||||
statusMessage = "";
|
||||
},
|
||||
() => mapboxApiKeySetting.set(previous),
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Idle page destination list
|
||||
|
||||
/** Where a user gets a token: Mapbox's access-tokens page (sign-up/login first when needed). */
|
||||
const MAPBOX_TOKENS_URL = "https://account.mapbox.com/access-tokens/";
|
||||
|
||||
/** Offered in place of the destination list until a Mapbox token is set. */
|
||||
function tokenSetupEntries(): IdleEntry[] {
|
||||
return [
|
||||
{
|
||||
kind: "action",
|
||||
label: "Open mapbox.com",
|
||||
detail: "Get a free token in the phone browser",
|
||||
run: () => {
|
||||
statusMessage = openUrlOnPhone(MAPBOX_TOKENS_URL)
|
||||
? "Opened mapbox.com on your phone. Copy your public token (pk...) and pick Edit token."
|
||||
: "Could not open a browser on the phone. Visit account.mapbox.com/access-tokens to get a token.";
|
||||
},
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Edit token",
|
||||
detail: "Type or paste it in the phone app",
|
||||
run: () => beginTokenEdit(),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function idleEntries(): IdleEntry[] {
|
||||
if (!isMapboxConfigured()) return tokenSetupEntries();
|
||||
const entries: IdleEntry[] = loadSavedDestinations()
|
||||
.filter((destination) => destination.address)
|
||||
.map((destination): IdleEntry => ({ kind: "saved", destination }));
|
||||
@@ -1094,6 +1153,10 @@ function clampIdleSelection(): void {
|
||||
}
|
||||
|
||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||
if (entry.kind === "action") {
|
||||
entry.run();
|
||||
return;
|
||||
}
|
||||
const target: NavTarget =
|
||||
entry.kind === "saved"
|
||||
? { kind: "query", query: entry.destination.address, label: entry.destination.name }
|
||||
@@ -1233,8 +1296,9 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
||||
image.drawText(mediumFont, 24, 16, "Navigate", 245);
|
||||
const busy = phase !== "idle";
|
||||
const entries = busy ? [] : idleEntries();
|
||||
const hint = !isMapboxConfigured()
|
||||
? "Set a Mapbox token in Settings > API Keys to enable navigation."
|
||||
const configured = isMapboxConfigured();
|
||||
const hint = !configured
|
||||
? "Navigation needs a Mapbox public token (free at mapbox.com). Get one, then enter it here or in Settings > API Keys."
|
||||
: 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.";
|
||||
@@ -1255,7 +1319,7 @@ function paintIdle(image: GrayImage, win: NavWindow): void {
|
||||
paintIdleList(image, win, entries, y);
|
||||
drawFooter(
|
||||
image,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} go ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} ${configured ? "go" : "choose"} ${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`);
|
||||
@@ -1281,7 +1345,18 @@ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], t
|
||||
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") {
|
||||
if (entry.kind === "action") {
|
||||
const label = truncateText(smallFont, entry.label, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else 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;
|
||||
|
||||
@@ -24,11 +24,11 @@ import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
|
||||
const nightscoutLargeFont = getDefaultLargeFont();
|
||||
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||
export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
|
||||
|
||||
function drawDirectionIndicator(
|
||||
export function drawDirectionIndicator(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
x: number,
|
||||
@@ -85,7 +85,7 @@ function truncateLine(text: string, maxChars: number): string {
|
||||
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
|
||||
}
|
||||
|
||||
function formatDelta(delta: number | null): string {
|
||||
export function formatDelta(delta: number | null): string {
|
||||
if (delta === null) return "--";
|
||||
return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ function formatBolusLabel(value: number): string {
|
||||
return Number.isInteger(value) ? `${value}` : value.toFixed(1);
|
||||
}
|
||||
|
||||
function drawNightscoutGraph(
|
||||
export function drawNightscoutGraph(
|
||||
image: GrayImage,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
nightscout: NightscoutState,
|
||||
@@ -431,10 +431,10 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
];
|
||||
}
|
||||
|
||||
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
||||
export function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
||||
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
|
||||
}
|
||||
|
||||
function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||
export function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||
image.drawLine(x, y, x + width, y, 180);
|
||||
}
|
||||
@@ -13,11 +13,12 @@
|
||||
*
|
||||
* Controls (ball ready): scroll sets launch power, click launches. In play:
|
||||
* click flips both flippers, scroll-up/down flips left/right individually,
|
||||
* long-press nudges the table (three quick nudges tilt), double-click pauses.
|
||||
* Watch swipes: left/right work the matching flipper, up nudges (or raises
|
||||
* launch power at the plunger), down lowers launch power.
|
||||
* double-click pauses. Watch swipes: left/right work the matching flipper,
|
||||
* up/down raise and lower launch power at the plunger.
|
||||
* Paused/game over: click resumes or starts a new game, double-click yields
|
||||
* focus, long-press opens the window menu.
|
||||
* focus, long-press opens the window menu. Losing input focus mid-game (a
|
||||
* shell overlay such as the system menu or a notification, focus to the
|
||||
* sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
@@ -29,6 +30,7 @@ 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 { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
@@ -36,7 +38,6 @@ import {
|
||||
directionalFallback,
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_LONG_PRESS,
|
||||
GESTURE_SCROLL,
|
||||
type InputEvent,
|
||||
} from "../../ui/gestures";
|
||||
@@ -77,11 +78,6 @@ const RENDER_TICK_MS = 50;
|
||||
const LAUNCH_SPEEDS = [320, 380, 440, 500, 560] as const;
|
||||
const BUMPER_KICK = 340;
|
||||
const SLING_KICK = 300;
|
||||
const NUDGE_KICK_UP = 70;
|
||||
const NUDGE_KICK_SIDE = 45;
|
||||
/** Nudges add 1 heat each and decay slowly; reaching 3 tilts the ball away. */
|
||||
const TILT_LIMIT = 3;
|
||||
const TILT_HEAT_DECAY_PER_S = 0.4;
|
||||
|
||||
const SCORE_BUMPER = 100;
|
||||
const SCORE_SLING = 25;
|
||||
@@ -208,7 +204,6 @@ const SFX_DRAIN: Step[] = [
|
||||
{ freq: 370, duty: 45, ms: 100 },
|
||||
{ freq: 247, duty: 50, ms: 200 },
|
||||
];
|
||||
const SFX_TILT: Step[] = [{ freq: 110, duty: 60, ms: 300 }];
|
||||
const SFX_GAME_OVER: Step[] = [
|
||||
{ freq: 494, duty: 45, ms: 150 },
|
||||
{ freq: 440, duty: 45, ms: 150 },
|
||||
@@ -269,10 +264,6 @@ type PinballWindow = {
|
||||
/** Debounce: a sensor re-arms only after the ball leaves its zone. */
|
||||
rolloverInside: boolean[];
|
||||
bumperFlashUntilMs: number[];
|
||||
tiltHeat: number;
|
||||
tilted: boolean;
|
||||
/** Alternates nudge direction so mashing doesn't push one way forever. */
|
||||
nudgeSign: 1 | -1;
|
||||
/** Transient center-table message ("BALL LOST", "+500", ...). */
|
||||
toast: string;
|
||||
toastUntilMs: number;
|
||||
@@ -331,15 +322,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
rolloverLit: ROLLOVERS.map(() => false),
|
||||
rolloverInside: ROLLOVERS.map(() => false),
|
||||
bumperFlashUntilMs: BUMPERS.map(() => 0),
|
||||
tiltHeat: 0,
|
||||
tilted: false,
|
||||
nudgeSign: 1,
|
||||
toast: "",
|
||||
toastUntilMs: 0,
|
||||
tickTimer: null,
|
||||
lastTickAtMs: 0,
|
||||
lastMinorSfxAtMs: 0,
|
||||
soundOn: true,
|
||||
soundOn: loadSoundEnabled("pinball"),
|
||||
lastSubmittedFingerprint: "",
|
||||
};
|
||||
windows.set(message.windowId, window);
|
||||
@@ -385,6 +373,19 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
// Anything that takes input away (the system menu, a notification
|
||||
// modal, the voice dialog, focus back to the sidebar) pauses: the
|
||||
// ball must not drain under a menu the player can't flip through.
|
||||
if (!message.focused && window.phase === "playing") {
|
||||
window.phase = "paused";
|
||||
syncTickTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
@@ -446,7 +447,7 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** The window's context menu (game actions), offered while paused or over; playing keeps long-press for the nudge. */
|
||||
/** The window's context menu (game actions), offered while paused or over. */
|
||||
function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
@@ -462,6 +463,7 @@ function windowMenuItems(window: PinballWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
window.soundOn = !window.soundOn;
|
||||
saveSoundEnabled("pinball", window.soundOn);
|
||||
if (window.soundOn) playSfx(window, SFX_RESUME);
|
||||
},
|
||||
},
|
||||
@@ -475,7 +477,6 @@ function windowMenu(window: PinballWindow): WindowMenu {
|
||||
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,
|
||||
@@ -519,8 +520,8 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
flip(window, window.flippers[1]!);
|
||||
}
|
||||
break;
|
||||
// Watch swipes: left/right work the matching flipper; up nudges the
|
||||
// table (or raises launch power at the plunger), down lowers it.
|
||||
// Watch swipes: left/right work the matching flipper; up/down set the
|
||||
// launch power at the plunger.
|
||||
case "swipe-left":
|
||||
if (!ready) flip(window, window.flippers[0]!);
|
||||
break;
|
||||
@@ -530,8 +531,6 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
case "swipe-up":
|
||||
if (ready) {
|
||||
window.launchPower = clamp(window.launchPower + 1, 1, LAUNCH_SPEEDS.length);
|
||||
} else {
|
||||
nudge(window);
|
||||
}
|
||||
break;
|
||||
case "swipe-down":
|
||||
@@ -547,13 +546,6 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
|
||||
flip(window, window.flippers[1]!);
|
||||
}
|
||||
break;
|
||||
case "long-press":
|
||||
if (ready) {
|
||||
frameTimings.finishFrame(frameId, "discarded: pinball ignored input");
|
||||
return;
|
||||
}
|
||||
nudge(window);
|
||||
break;
|
||||
case "short-then-long-press":
|
||||
// No context menu mid-game, so this opens the system menu instead.
|
||||
windowMenu(window).open();
|
||||
@@ -606,8 +598,6 @@ function resetGame(window: PinballWindow): void {
|
||||
window.score = 0;
|
||||
window.rolloverLit = ROLLOVERS.map(() => false);
|
||||
window.rolloverInside = ROLLOVERS.map(() => false);
|
||||
window.tiltHeat = 0;
|
||||
window.tilted = false;
|
||||
window.toast = "";
|
||||
window.toastUntilMs = 0;
|
||||
for (const flipper of window.flippers) {
|
||||
@@ -628,7 +618,6 @@ function launchBall(window: PinballWindow): void {
|
||||
}
|
||||
|
||||
function flip(window: PinballWindow, flipper: Flipper): void {
|
||||
if (window.tilted) return;
|
||||
if (flipper.state === "hold") {
|
||||
flipper.holdUntilMs = Date.now() + FLIPPER_HOLD_MS;
|
||||
return;
|
||||
@@ -637,25 +626,8 @@ function flip(window: PinballWindow, flipper: Flipper): void {
|
||||
playSfx(window, SFX_FLIPPER, true);
|
||||
}
|
||||
|
||||
function nudge(window: PinballWindow): void {
|
||||
if (window.tilted) return;
|
||||
window.ballVy -= NUDGE_KICK_UP;
|
||||
window.ballVx += window.nudgeSign * NUDGE_KICK_SIDE;
|
||||
window.nudgeSign = window.nudgeSign === 1 ? -1 : 1;
|
||||
window.tiltHeat += 1;
|
||||
if (window.tiltHeat >= TILT_LIMIT) {
|
||||
window.tilted = true;
|
||||
showToast(window, "TILT");
|
||||
// A tilt drops the flippers dead until the ball drains.
|
||||
for (const flipper of window.flippers) {
|
||||
if (flipper.state !== "rest") flipper.state = "falling";
|
||||
}
|
||||
playSfx(window, SFX_TILT);
|
||||
}
|
||||
}
|
||||
|
||||
function addScore(window: PinballWindow, points: number): void {
|
||||
if (!window.tilted) window.score += points;
|
||||
window.score += points;
|
||||
}
|
||||
|
||||
function showToast(window: PinballWindow, text: string, ms = 1100): void {
|
||||
@@ -710,7 +682,6 @@ function tick(window: PinballWindow): void {
|
||||
function stepPhysics(window: PinballWindow): void {
|
||||
const dt = PHYSICS_DT;
|
||||
stepFlippers(window, dt);
|
||||
window.tiltHeat = Math.max(0, window.tiltHeat - TILT_HEAT_DECAY_PER_S * dt);
|
||||
if (window.ballState !== "live") return;
|
||||
|
||||
window.ballVy += GRAVITY * dt;
|
||||
@@ -894,8 +865,6 @@ function ballDrained(window: PinballWindow): void {
|
||||
window.ballVx = 0;
|
||||
window.ballVy = 0;
|
||||
window.trail = [];
|
||||
window.tilted = false;
|
||||
window.tiltHeat = 0;
|
||||
window.ballsLeft--;
|
||||
if (window.ballsLeft <= 0) {
|
||||
window.phase = "game-over";
|
||||
@@ -1007,9 +976,6 @@ function paintContent(window: PinballWindow): GrayImage {
|
||||
if (window.toastUntilMs > now && window.toast) {
|
||||
drawCenteredIn(image, mediumFont, LEFT, LANE_X - LEFT, 150, window.toast, 245);
|
||||
}
|
||||
if (window.tilted) {
|
||||
drawCenteredIn(image, mediumFont, LEFT, LANE_X - LEFT, 60, "TILT", 245);
|
||||
}
|
||||
if (window.phase === "paused") {
|
||||
paintDialog(image, "PAUSED", [`${GESTURE_CLICK} resume`, `${GESTURE_DOUBLE_CLICK} leave`]);
|
||||
} else if (window.phase === "game-over") {
|
||||
@@ -1050,7 +1016,7 @@ function paintPanel(image: GrayImage, window: PinballWindow): void {
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
} else {
|
||||
image.drawText(smallFont, PANEL_X, 216, `${GESTURE_CLICK} flip ${GESTURE_SCROLL} L/R flip`, 115);
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_LONG_PRESS} nudge ${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* The terminal session list as it is drawn in two places: the Terminal app's
|
||||
* hub window (in the worker) and the Glanceboard's Terminal widget (on the
|
||||
* main thread). The worker publishes a snapshot of the list through the
|
||||
* host's worker-state channel; both painters use drawSessionRow so a row
|
||||
* looks the same in each.
|
||||
*
|
||||
* No NativeScript imports: this module is bundled into the worker as well.
|
||||
*/
|
||||
import { type GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
|
||||
/** Worker-state key the Terminal worker publishes its snapshot under. */
|
||||
export const TERMINAL_SESSIONS_STATE_KEY = "terminal:sessions";
|
||||
|
||||
/** Alternation period of the activity indicator, shared by hub rows, the widget and sidebar cursors. */
|
||||
export const SESSION_ACTIVITY_STEP_MS = 800;
|
||||
|
||||
export type TerminalSessionRow = {
|
||||
/** Connection-scoped identity (connection id + socket). */
|
||||
key: string;
|
||||
label: string;
|
||||
/** Local-clock time (Date.now() domain) until which the session counts as active; 0 = idle. */
|
||||
activeUntilMs: number;
|
||||
/** Sidebar glyph ("1", "2", ...) of the view window showing this session, or null when none is open. */
|
||||
openGlyph: string | null;
|
||||
};
|
||||
|
||||
export type TerminalHostGroup = {
|
||||
/** Host name, shown as a heading when more than one host is connected. */
|
||||
name: string;
|
||||
sessions: TerminalSessionRow[];
|
||||
};
|
||||
|
||||
export type TerminalSessionsSnapshot = {
|
||||
/** Connected hosts, each with its live sessions most-recently-updated first. */
|
||||
hosts: TerminalHostGroup[];
|
||||
/** The hub's status line ("Connecting...", "No connections configured.", ...). */
|
||||
status: string;
|
||||
/** Whether any g2mirror connection is configured at all. */
|
||||
configured: boolean;
|
||||
};
|
||||
|
||||
/** Whether a row's activity indicator should show right now. */
|
||||
export function isSessionRowActive(row: TerminalSessionRow, nowMs = Date.now()): boolean {
|
||||
return row.activeUntilMs > nowMs;
|
||||
}
|
||||
|
||||
/** Width of the window-number column (one glyph plus breathing room). */
|
||||
export function sessionGlyphColumnWidth(font: UiFont): number {
|
||||
return font.measureText("W") + 6;
|
||||
}
|
||||
|
||||
/** Left edge of the label in a row whose activity gutter starts at `x`. */
|
||||
export function sessionLabelX(font: UiFont, x: number): number {
|
||||
return x + SESSION_INDICATOR_SIZE + 4 + sessionGlyphColumnWidth(font);
|
||||
}
|
||||
|
||||
const SESSION_INDICATOR_SIZE = 6;
|
||||
|
||||
/**
|
||||
* One session row: the activity indicator in the gutter at `x` (a small
|
||||
* square, filled on even animation phases and outlined on odd ones, drawn as
|
||||
* shapes rather than a font glyph so both states read in every UI font),
|
||||
* then the open view window's glyph in its own column, then the label,
|
||||
* truncated (never wrapped) to fit `width`.
|
||||
*/
|
||||
export function drawSessionRow(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
options: {
|
||||
x: number;
|
||||
/** Top of the text line. */
|
||||
y: number;
|
||||
/** Horizontal room from `x`, label included. */
|
||||
width: number;
|
||||
label: string;
|
||||
openGlyph: string | null;
|
||||
active: boolean;
|
||||
/** Animation phase (0 = filled indicator). */
|
||||
phase: number;
|
||||
/** Text brightness for the label (the glyph draws a little dimmer). */
|
||||
value: number;
|
||||
},
|
||||
): void {
|
||||
const { x, y, width, label, openGlyph, active, phase, value } = options;
|
||||
if (active) {
|
||||
const iy = y + Math.max(0, ((font.lineHeight - SESSION_INDICATOR_SIZE) / 2) | 0);
|
||||
if (phase % 2 === 0) {
|
||||
image.fillRect(x, iy, SESSION_INDICATOR_SIZE, SESSION_INDICATOR_SIZE, value);
|
||||
} else {
|
||||
image.drawRect(x, iy, SESSION_INDICATOR_SIZE, SESSION_INDICATOR_SIZE, value);
|
||||
}
|
||||
}
|
||||
if (openGlyph) {
|
||||
image.drawText(font, x + SESSION_INDICATOR_SIZE + 4, y, openGlyph, Math.max(0, value - 60));
|
||||
}
|
||||
const labelX = sessionLabelX(font, x);
|
||||
image.drawText(font, labelX, y, truncateText(font, label, Math.max(0, x + width - labelX)), value);
|
||||
}
|
||||
@@ -38,6 +38,11 @@ import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fo
|
||||
import { layoutHubHeader } from "./hub-header";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
|
||||
import {
|
||||
drawSessionRow,
|
||||
TERMINAL_SESSIONS_STATE_KEY,
|
||||
type TerminalSessionsSnapshot,
|
||||
} from "./session-list";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -723,6 +728,48 @@ function renderHubWindows(): void {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "hub") scheduleRender(window);
|
||||
}
|
||||
// Every change the hub repaints for is one the published list may reflect.
|
||||
publishSessionsSnapshot();
|
||||
}
|
||||
|
||||
let lastPublishedSnapshot = "";
|
||||
|
||||
/**
|
||||
* Publish the session list for main-thread consumers (the Glanceboard's
|
||||
* Terminal widget): connected hosts with their sessions most-recently-updated
|
||||
* first, each with its activity deadline (local clock, so the consumer can
|
||||
* expire and animate it itself) and the glyph of its open view window. Posted
|
||||
* only when the JSON actually changed.
|
||||
*/
|
||||
function publishSessionsSnapshot(): void {
|
||||
const connected = connectedControls();
|
||||
const snapshot: TerminalSessionsSnapshot = {
|
||||
hosts: connected.map((control) => {
|
||||
const connectionId = control.config.id;
|
||||
const sessions = (control.state?.sessions ?? [])
|
||||
.slice()
|
||||
.sort(
|
||||
(a, b) =>
|
||||
(sessionRecency.get(recencyKey(connectionId, b.socket)) ?? 0) -
|
||||
(sessionRecency.get(recencyKey(connectionId, a.socket)) ?? 0),
|
||||
);
|
||||
return {
|
||||
name: connectionDisplayName(control.config),
|
||||
sessions: sessions.map((session) => ({
|
||||
key: recencyKey(connectionId, session.socket),
|
||||
label: sessionLabel(session),
|
||||
activeUntilMs: (sessionActivity.get(recencyKey(connectionId, session.socket)) ?? -ACTIVITY_ACTIVE_MS) + ACTIVITY_ACTIVE_MS,
|
||||
openGlyph: viewGlyphForSocket(connectionId, session.socket),
|
||||
})),
|
||||
};
|
||||
}),
|
||||
status: sessionsStatusLine(),
|
||||
configured: controls.size > 0,
|
||||
};
|
||||
const encoded = JSON.stringify(snapshot);
|
||||
if (encoded === lastPublishedSnapshot) return;
|
||||
lastPublishedSnapshot = encoded;
|
||||
post({ type: "publish-state", key: TERMINAL_SESSIONS_STATE_KEY, state: snapshot });
|
||||
}
|
||||
|
||||
// One animation clock for foreground hub rows and every terminal sidebar icon.
|
||||
@@ -1118,6 +1165,8 @@ type HubItem = {
|
||||
heading?: boolean;
|
||||
/** Session with recent output: an animated indicator marks the row. */
|
||||
active?: boolean;
|
||||
/** Glyph of the view window showing this session (the number column). */
|
||||
openGlyph?: string | null;
|
||||
onSelect?: () => void;
|
||||
};
|
||||
|
||||
@@ -1183,10 +1232,10 @@ function hubSessionItems(window: HubWindow): HubItem[] {
|
||||
}
|
||||
const sessions = orderedSessions(window, control);
|
||||
for (const session of sessions) {
|
||||
const openWindowId = viewWindowIdForSocket(control.config.id, session.socket);
|
||||
items.push({
|
||||
label: openWindowId ? `${sessionLabel(session)} [open]` : sessionLabel(session),
|
||||
label: sessionLabel(session),
|
||||
active: isSessionActive(control.config.id, session.socket),
|
||||
openGlyph: viewGlyphForSocket(control.config.id, session.socket),
|
||||
onSelect: () => {
|
||||
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
|
||||
if (windowId) {
|
||||
@@ -1421,6 +1470,19 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number):
|
||||
* Includes views that were requested but whose surface hasn't opened yet, so
|
||||
* a quick double-select can't spawn two windows for one session.
|
||||
*/
|
||||
/** Sidebar glyph of the (open or opening) view window on a session, or null. */
|
||||
function viewGlyphForSocket(connectionId: string, socket: string): string | null {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
||||
return window.glyph || null;
|
||||
}
|
||||
}
|
||||
for (const pending of pendingViews.values()) {
|
||||
if (pending.connectionId === connectionId && pending.socket === socket) return pending.glyph || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function viewWindowIdForSocket(connectionId: string, socket: string): string | null {
|
||||
for (const window of windows.values()) {
|
||||
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
||||
@@ -1539,20 +1601,18 @@ function paintHub(window: HubWindow): GrayImage {
|
||||
// an outline-only selection signals the sidebar owns input.
|
||||
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
|
||||
}
|
||||
image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200);
|
||||
if (item.active) {
|
||||
// Activity indicator in the gutter left of the label, alternating
|
||||
// filled/outline each animation step (drawn shapes, not a font glyph,
|
||||
// so the two states render distinctly in every UI font).
|
||||
const size = 6;
|
||||
const iy = y + 2 + Math.max(0, ((font.lineHeight - size) / 2) | 0);
|
||||
const value = selected ? 255 : 200;
|
||||
if (hubAnimationPhase === 0) {
|
||||
image.fillRect(22, iy, size, size, value);
|
||||
} else {
|
||||
image.drawRect(22, iy, size, size, value);
|
||||
}
|
||||
}
|
||||
// Activity gutter, open-window number column, then the label, truncated
|
||||
// to the row (shared with the Glanceboard's Terminal widget).
|
||||
drawSessionRow(image, chromeFont(), {
|
||||
x: 22,
|
||||
y: y + 2,
|
||||
width: window.viewportWidth - 22 - 26,
|
||||
label: item.label,
|
||||
openGlyph: item.openGlyph ?? null,
|
||||
active: Boolean(item.active),
|
||||
phase: hubAnimationPhase,
|
||||
value: selected ? 255 : 200,
|
||||
});
|
||||
}
|
||||
if (items.length > visibleRowCount) {
|
||||
drawListScrollbar(
|
||||
@@ -1595,6 +1655,11 @@ function hubStatusLine(window: HubWindow): string {
|
||||
if (window.mode === "connections") {
|
||||
return "Select a connection to connect, disconnect, or remove.";
|
||||
}
|
||||
return sessionsStatusLine();
|
||||
}
|
||||
|
||||
/** The sessions view's status line (also published with the session snapshot). */
|
||||
function sessionsStatusLine(): string {
|
||||
if (controls.size === 0) {
|
||||
return "No connections configured.";
|
||||
}
|
||||
@@ -1830,7 +1895,8 @@ function toolListSessions(): ToolResult {
|
||||
lines.push(`${connectionDisplayName(control.config)}:`);
|
||||
}
|
||||
for (const session of sessions) {
|
||||
const open = viewWindowIdForSocket(control.config.id, session.socket) ? " [open]" : "";
|
||||
const glyph = viewGlyphForSocket(control.config.id, session.socket);
|
||||
const open = glyph ? ` [open in window ${glyph}]` : viewWindowIdForSocket(control.config.id, session.socket) ? " [open]" : "";
|
||||
lines.push(`- ${sessionLabel(session)}${open}`);
|
||||
}
|
||||
if (!sessions.length && multiHost) {
|
||||
|
||||
@@ -266,6 +266,11 @@ export class TimerEngine {
|
||||
);
|
||||
}
|
||||
|
||||
/** Every countdown timer (running, paused or rung), unsorted. */
|
||||
timers(): readonly CountdownTimer[] {
|
||||
return this.state.timers;
|
||||
}
|
||||
|
||||
ringingItems(): RingingItem[] {
|
||||
const items: RingingItem[] = [];
|
||||
for (const timer of this.state.timers) {
|
||||
|
||||
@@ -13,7 +13,9 @@ import { TranscriptModel, TranscriptScroll, wrapTranscribeText } from "./transcr
|
||||
* it, and saves it to Downloads from the app menu.
|
||||
*/
|
||||
export class TranscribeLayer implements Layer {
|
||||
private status = "Listening...";
|
||||
private status = "Starting microphone...";
|
||||
private listening = false;
|
||||
private detail = "";
|
||||
private readonly transcript = new TranscriptModel();
|
||||
private readonly scroll = new TranscriptScroll();
|
||||
private saveNotice = "";
|
||||
@@ -29,6 +31,8 @@ export class TranscribeLayer implements Layer {
|
||||
this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause());
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.listening = state.listening;
|
||||
this.detail = state.detail;
|
||||
this.saveNotice = "";
|
||||
requestRender();
|
||||
});
|
||||
@@ -38,7 +42,7 @@ export class TranscribeLayer implements Layer {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const text = this.transcript.text || "Listening...";
|
||||
const text = this.transcript.text || (this.listening ? "Listening..." : this.detail);
|
||||
const wrapped = wrapTranscribeText((text) => font.measureText(text), text, width - 64);
|
||||
|
||||
image.drawText(font, 24, 20, "Transcribe", 200);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
+113
-47
@@ -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[] {
|
||||
|
||||
@@ -2,10 +2,10 @@ import { concat, rle4 } from './ble-protocol'
|
||||
|
||||
/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in
|
||||
* units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first.
|
||||
* null means identical, incomparable, or a full-screen box: use dedup/mode 6.
|
||||
* null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport.
|
||||
*/
|
||||
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array,
|
||||
width: number, height: number, frameId: number, deflate: (data: Uint8Array) => Uint8Array): Uint8Array | null {
|
||||
width: number, height: number, frameId: number): Uint8Array | null {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
|
||||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
|
||||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
|
||||
@@ -30,5 +30,20 @@ export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8
|
||||
region.set(next.subarray(start, start + regionStride), y * regionStride)
|
||||
}
|
||||
return concat(new Uint8Array([3, left / 4, top / 2, boxWidth / 4, boxHeight / 2,
|
||||
frameId & 255, (frameId >>> 8) & 255]), deflate(rle4(region)))
|
||||
frameId & 255, (frameId >>> 8) & 255]), rle4(region))
|
||||
}
|
||||
|
||||
/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */
|
||||
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
|
||||
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
|
||||
throw new Error('Invalid framebuffer dimensions')
|
||||
const bands: Uint8Array[] = []
|
||||
let id = firstId
|
||||
for (let top = 0; top < height; top += 64) {
|
||||
const rows = Math.min(64, height - top)
|
||||
bands.push(concat(new Uint8Array([3, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]),
|
||||
rle4(packed.subarray(top * width / 2, (top + rows) * width / 2))))
|
||||
id = id >= 0xfffe ? 1 : id + 1
|
||||
}
|
||||
return bands
|
||||
}
|
||||
@@ -3,7 +3,7 @@ export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
|
||||
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
|
||||
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9 } as const
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const
|
||||
export function concat(...parts: Uint8Array[]): Uint8Array {
|
||||
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
|
||||
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
|
||||
@@ -28,13 +28,7 @@ export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), inte
|
||||
export function createLayout(magic: number): Uint8Array {
|
||||
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
|
||||
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
|
||||
const image = concat(geometry, integer(5, 10), string(6, 'img00'))
|
||||
return wrap(0, magic, 3, concat(integer(1, 2), bytes(3, text), bytes(4, image), integer(5, 10000)))
|
||||
}
|
||||
export function imageFragment(magic: number, payload: Uint8Array, offset: number, size = 3800): Uint8Array {
|
||||
const fragment = payload.subarray(offset, offset + size)
|
||||
return wrap(3, magic, 5, concat(integer(1, 10), string(2, 'img00'), integer(3, 10), integer(4, payload.length),
|
||||
integer(5, 0), integer(6, Math.floor(offset / size)), integer(7, fragment.length), bytes(8, fragment)))
|
||||
return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000)))
|
||||
}
|
||||
export function crc16(data: Uint8Array): number {
|
||||
let crc = 0xffff
|
||||
@@ -55,7 +49,7 @@ export function frameMessage(payload: Uint8Array, sid: number, flag: number, seq
|
||||
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
|
||||
})
|
||||
}
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number }
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array }
|
||||
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
|
||||
export class MessageReceiver {
|
||||
private streams = new Map<string, Uint8Array>()
|
||||
@@ -70,7 +64,12 @@ export class MessageReceiver {
|
||||
const length = stream[3], count = stream[4], index = stream[5]
|
||||
if (stream.length < length + 8) break
|
||||
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
|
||||
const packet = stream.slice(0, length + 8)
|
||||
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
|
||||
if (sid === SID.cfw) {
|
||||
output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet })
|
||||
continue
|
||||
}
|
||||
if (!count || !index || index > count) { this.messages.delete(key); continue }
|
||||
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
|
||||
const message = this.messages.get(key)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { concat, crc16, SID } from './ble-protocol'
|
||||
|
||||
declare const require: (name: string) => any
|
||||
export const CFW_MAX_MESSAGE = 65535
|
||||
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
|
||||
const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8)
|
||||
|
||||
/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */
|
||||
export class CfwTransport {
|
||||
private compressor: any
|
||||
private resetPending = true
|
||||
private lenses = 0
|
||||
reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 }
|
||||
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
|
||||
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
|
||||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
|
||||
throw new Error('Invalid CFW stream parameters')
|
||||
if (this.lenses !== lenses) this.reset()
|
||||
if (!this.compressor) this.compressor = new (require('pako').Deflate)()
|
||||
const chunks: Uint8Array[] = []
|
||||
// SYNC_FLUSH emits this record while retaining history for the next one.
|
||||
this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice())
|
||||
if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed')
|
||||
let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0)
|
||||
if (!body.length) flags &= ~4
|
||||
if (body.length > CFW_MAX_MESSAGE) {
|
||||
this.reset(); body = message; flags = lenses | 8
|
||||
} else { this.resetPending = false; this.lenses = lenses }
|
||||
const checksum = crc16(message)
|
||||
const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body)
|
||||
const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = []
|
||||
for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) {
|
||||
const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) |
|
||||
(offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity))
|
||||
const crc = crc16(body)
|
||||
packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]),
|
||||
body, new Uint8Array([crc & 255, crc >>> 8])))
|
||||
}
|
||||
return packets
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate the entire primary + redundant-ACK packet before applying any ACK. */
|
||||
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
|
||||
const n = packet.length
|
||||
if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 ||
|
||||
packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 ||
|
||||
![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) ||
|
||||
crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null
|
||||
const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10),
|
||||
lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }]
|
||||
for (let offset = 17; offset < n - 2; offset += 7)
|
||||
acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12],
|
||||
size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) })
|
||||
return acks
|
||||
}
|
||||
+109
-38
@@ -16,7 +16,7 @@ import { ALL_NOTIFICATIONS, onAndroidNotificationPosted, readActiveNotifications
|
||||
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";
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
|
||||
import { hasExtractedEvenHubFonts } from "./firmware-builder";
|
||||
import { resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy";
|
||||
import { WearRemote, type WearRemoteInputKind } from "./wear-remote";
|
||||
@@ -43,6 +43,8 @@ const MIRROR_TOUCH_GESTURES: Record<Exclude<MirrorTouchKind, "tap">, WearRemoteI
|
||||
"swipe-right": "swipe-right",
|
||||
};
|
||||
import { findSoundEffect, playSoundEffect } from "../ui/sound-effects";
|
||||
import { GlanceHost } from "./glance-host";
|
||||
import { type GlanceEvent } from "./glance-state";
|
||||
import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state";
|
||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||
import { voiceControlBridge } from "../native/voice-control";
|
||||
@@ -52,6 +54,7 @@ import { prepareFrameDraws } from "../graphics/glyph-wire";
|
||||
import { getDefaultMediumFont } from "../graphics/ui-fonts";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
|
||||
import { type InputEvent } from "../ui/gestures";
|
||||
import { registerSystemTools } from "../assistant/system-tools";
|
||||
import { updateGlassesPresence } from "./glasses-presence";
|
||||
import { timerEngine } from "../apps/timer/timer-engine";
|
||||
@@ -252,10 +255,30 @@ class DashboardController {
|
||||
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private evenHubSuspendTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private evenHubSessionSuspended = false;
|
||||
/**
|
||||
* The Glanceboard: the alternate sleep-time display, on its own surface
|
||||
* above the shell. Fed sleep-time input from handleInputEvent; it uses the
|
||||
* same wake barrier as the shell and hands the compositor back to the
|
||||
* screen-off path when it hides.
|
||||
*/
|
||||
private readonly glance = new GlanceHost({
|
||||
getDisplay: () => this.display,
|
||||
getProvider: () => ALL_APPS.find((app) => app.glanceboard)?.glanceboard ?? null,
|
||||
canShow: () => (this.phase === "connected" || this.isPreviewDisplayActive()) && !this.glassesLocked,
|
||||
ensureSessionActive: (frameId) => this.ensureEvenHubSessionActive(frameId),
|
||||
onHiddenWhileAsleep: () => {
|
||||
if (!shell.isScreenOn()) this.handleScreenStateChanged(false);
|
||||
},
|
||||
onVisibilityChanged: () => {
|
||||
this.schedulePreviewUpdate();
|
||||
this.emit();
|
||||
},
|
||||
appendLog: (message) => this.appendLog(message),
|
||||
});
|
||||
private evenHubResumePromise: Promise<boolean> | null = null;
|
||||
private faceclawWakeLeaseSupported = false;
|
||||
/** Faceclaw's firmware at the required revision reported in; its extensions can be used. */
|
||||
private customFirmwareConfirmed = false;
|
||||
private faceclawWakeLeaseState: boolean | null = null;
|
||||
private wearNotifySupported = false;
|
||||
private glassesWorn: boolean | null = null;
|
||||
private phoneLocked = false;
|
||||
private glassesLocked = false;
|
||||
@@ -319,7 +342,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(),
|
||||
@@ -359,6 +382,8 @@ class DashboardController {
|
||||
this.emit();
|
||||
},
|
||||
onScreenStateChanged: (on) => {
|
||||
// Any wake of the regular UI replaces a showing Glanceboard.
|
||||
if (on) this.glance.dismiss();
|
||||
this.handleScreenStateChanged(on);
|
||||
if (on) this.requestShellRender();
|
||||
this.emit();
|
||||
@@ -631,7 +656,7 @@ class DashboardController {
|
||||
const communicator = this.communicator;
|
||||
if (
|
||||
!lockScreenEnabledSetting.get() ||
|
||||
!this.wearNotifySupported ||
|
||||
!this.customFirmwareConfirmed ||
|
||||
this.phase !== "connected" ||
|
||||
!communicator
|
||||
) {
|
||||
@@ -736,7 +761,7 @@ class DashboardController {
|
||||
// foreground app when Faceclaw handles the glasses wakeword. Without the
|
||||
// latter, Even AI displaces EvenHub before our first wake frame can ACK.
|
||||
return (
|
||||
this.faceclawWakeLeaseSupported &&
|
||||
this.customFirmwareConfirmed &&
|
||||
(suspendEvenHubWhenScreenOffSetting.get() || wakeWordActionSetting.get() !== "off")
|
||||
);
|
||||
}
|
||||
@@ -788,6 +813,7 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
!this.communicator ||
|
||||
this.evenHubSessionSuspended
|
||||
@@ -801,9 +827,11 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
this.communicator !== communicator
|
||||
) {
|
||||
// A showing Glanceboard re-arms this when it hides.
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -816,7 +844,7 @@ class DashboardController {
|
||||
}
|
||||
|
||||
void (async () => {
|
||||
if (this.faceclawWakeLeaseSupported) {
|
||||
if (this.customFirmwareConfirmed) {
|
||||
// Refresh immediately before teardown so the first suspended wake
|
||||
// cannot race an old lease's expiry.
|
||||
const leaseReady = await this.syncFaceclawWakeLease(communicator, true);
|
||||
@@ -829,6 +857,7 @@ class DashboardController {
|
||||
if (
|
||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||
shell.isScreenOn() ||
|
||||
this.glance.isVisible() ||
|
||||
this.phase !== "connected" ||
|
||||
this.communicator !== communicator
|
||||
) {
|
||||
@@ -1250,6 +1279,7 @@ class DashboardController {
|
||||
const target = this.previewTarget;
|
||||
if (!target) return;
|
||||
this.previewTarget = null;
|
||||
this.glance.reset();
|
||||
target.release();
|
||||
this.clearDashboardTimer();
|
||||
this.appendLog("Preview-only display released.");
|
||||
@@ -1291,10 +1321,10 @@ class DashboardController {
|
||||
);
|
||||
|
||||
let communicator: FaceclawCommunicatorBridge | null = null;
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.wearNotifySupported = false;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.glance.reset();
|
||||
this.evenHubResumePromise = null;
|
||||
this.connectRunning = true;
|
||||
|
||||
@@ -1390,9 +1420,14 @@ 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) {
|
||||
@@ -1436,26 +1471,18 @@ class DashboardController {
|
||||
this.offFirmwareInfo = communicator.onFirmwareInfo((info) => {
|
||||
this.appendLog(
|
||||
`firmware: L=${info.leftVersion || "?"} R=${info.rightVersion || "?"}` +
|
||||
(info.capabilities ? ` caps="${info.capabilities}"` : " (no CFW capability string)"),
|
||||
(info.extension ? ` ext="${info.extension}"` : " (no firmware extension string)"),
|
||||
);
|
||||
const warning = firmwareIncompatibilityMessage(info) ?? "";
|
||||
const wakeLeaseSupported = info.capabilities
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.includes("wakelease");
|
||||
const wearNotifySupported = info.capabilities
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.includes("wearnotify");
|
||||
if (wakeLeaseSupported !== this.faceclawWakeLeaseSupported) {
|
||||
this.faceclawWakeLeaseSupported = wakeLeaseSupported;
|
||||
// The firmware contract is all-or-nothing: a compatible revision has
|
||||
// every extension (wake lease, wear notifications, ...) we use.
|
||||
const customFirmwareConfirmed = !warning && hasCompatibleFirmware(info);
|
||||
if (customFirmwareConfirmed !== this.customFirmwareConfirmed) {
|
||||
this.customFirmwareConfirmed = customFirmwareConfirmed;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
void this.syncFaceclawWakeLease().catch((error) => {
|
||||
this.appendLog(`wake takeover lease sync failed: ${this.formatError(error)}`);
|
||||
});
|
||||
}
|
||||
if (wearNotifySupported !== this.wearNotifySupported) {
|
||||
this.wearNotifySupported = wearNotifySupported;
|
||||
this.ensureWearStateTracking();
|
||||
}
|
||||
if (warning !== this.firmwareWarningMessage) {
|
||||
@@ -1553,8 +1580,8 @@ class DashboardController {
|
||||
}
|
||||
this.communicator = null;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.wearNotifySupported = false;
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.glance.reset();
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.evenHubResumePromise = null;
|
||||
this.clearDashboardTimer();
|
||||
@@ -1688,6 +1715,7 @@ class DashboardController {
|
||||
const communicator = this.communicator;
|
||||
this.communicator = null;
|
||||
this.lockSurfaceConfigured = false;
|
||||
this.glance.reset();
|
||||
this.evenHubSessionSuspended = false;
|
||||
this.evenHubResumePromise = null;
|
||||
|
||||
@@ -1746,9 +1774,8 @@ class DashboardController {
|
||||
await communicator?.close().catch(() => {});
|
||||
} finally {
|
||||
stopForegroundNotification();
|
||||
this.faceclawWakeLeaseSupported = false;
|
||||
this.customFirmwareConfirmed = false;
|
||||
this.faceclawWakeLeaseState = null;
|
||||
this.wearNotifySupported = false;
|
||||
this.setPhase("disconnected");
|
||||
this.setStatus("Disconnected.");
|
||||
this.appendLog("Disconnected from the glasses.");
|
||||
@@ -1762,14 +1789,14 @@ class DashboardController {
|
||||
* finger-down/finger-up (the watch) hold for as long as the user does.
|
||||
*/
|
||||
async injectSyntheticRingInput(kind: WearRemoteInputKind, origin: SyntheticInputOrigin = "ring"): Promise<void> {
|
||||
// Match the ring while the display is dark: only a double-click wakes it,
|
||||
// and that wake is handled by Shell.receiveInput. This check also protects
|
||||
// against a watch acting on a stale state snapshot.
|
||||
// Match the ring while the display is dark: a double-click wakes it
|
||||
// (handled by Shell.receiveInput) and a tap or hold shows the
|
||||
// Glanceboard; scrolls and the menu gesture mean nothing. This check also
|
||||
// protects against a watch acting on a stale state snapshot.
|
||||
if (
|
||||
origin === "watch" &&
|
||||
!shell.isScreenOn() &&
|
||||
kind !== "double-click" &&
|
||||
kind !== "long-press-release"
|
||||
(kind === "scroll-up" || kind === "scroll-down" || kind === "short-then-long-press")
|
||||
) {
|
||||
this.appendLog(`${kind} (watch scheme) ignored while display is off`);
|
||||
return;
|
||||
@@ -1800,7 +1827,12 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
if (!shell.isScreenOn()) {
|
||||
// Asleep, the mirror offers the sleep gestures: double-tap wakes, a
|
||||
// tap shows the Glanceboard for its timeout. The mirror's hold has no
|
||||
// real release (the synthetic long-press releases at once, which would
|
||||
// only flash the board), so it counts as a tap here too.
|
||||
if (kind === "double-tap") await this.injectSyntheticRingInput("double-click", "watch");
|
||||
else if (kind === "tap" || kind === "long-press") await this.injectSyntheticRingInput("click", "watch");
|
||||
return;
|
||||
}
|
||||
if (kind !== "tap") {
|
||||
@@ -1874,12 +1906,15 @@ 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 {
|
||||
voiceControlBridge.stopPushToTalk();
|
||||
private stopVoiceCapture(): Promise<void> | void {
|
||||
++this.pttCaptureGeneration;
|
||||
return voiceControlBridge.stopPushToTalk();
|
||||
}
|
||||
|
||||
private startContinuousVoiceCapture(): void {
|
||||
@@ -1944,7 +1979,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();
|
||||
@@ -1952,8 +1988,12 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
const communicator = this.communicator;
|
||||
void ensureVoicePermissions()
|
||||
// The system prompt appears on the phone; tell the glasses dialog so it
|
||||
// sends the user there rather than claiming to listen.
|
||||
if (!hasMicrophonePermission()) voiceControlBridge.reportPermissionPrompt();
|
||||
await ensureVoicePermissions()
|
||||
.then(() => {
|
||||
if (kind === "ptt" && pttGeneration !== this.pttCaptureGeneration) return;
|
||||
if (previewCapture) {
|
||||
if (!this.isPreviewDisplayActive()) return;
|
||||
} else if (this.phase !== "connected" || this.communicator !== communicator) {
|
||||
@@ -1968,6 +2008,7 @@ class DashboardController {
|
||||
})
|
||||
.catch((error) => {
|
||||
this.appendLog(`voice permission failed: ${this.formatError(error)}`);
|
||||
if (!hasMicrophonePermission()) voiceControlBridge.reportPermissionDenied();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2057,6 +2098,20 @@ class DashboardController {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!shell.isScreenOn()) {
|
||||
// Sleep-time gestures belong to the Glanceboard: a tap or a hold
|
||||
// shows it (the shell would ignore them), and a double-tap while it
|
||||
// is up dismisses it and falls through to the normal wake below.
|
||||
const glanceEvent = this.glanceEventFor(inputEvent, event);
|
||||
if (glanceEvent?.type === "dismiss") {
|
||||
this.glance.dismiss();
|
||||
} else if (glanceEvent) {
|
||||
frameTimings.annotateFrame(frameId, `glanceboard ${glanceEvent.type}`);
|
||||
await this.glance.handleEvent(glanceEvent, frameId);
|
||||
frameOwned = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
const wakewordShouldWake =
|
||||
event.kind === "even-ai" &&
|
||||
event.eventType === EvenAIStatus.EVEN_AI_WAKE_UP &&
|
||||
@@ -2130,6 +2185,19 @@ class DashboardController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard event a sleep-time input means, or null when the shell
|
||||
* should see the input as usual. A display-wake carries the head-tilt
|
||||
* gesture (HEAD_UP_EVENT) as a press; the double-tap display-wake stays the
|
||||
* regular UI's wake.
|
||||
*/
|
||||
private glanceEventFor(inputEvent: InputEvent, event: RawInputEvent): GlanceEvent | null {
|
||||
if (inputEvent.type === "display-wake") {
|
||||
return event.eventType === OsEventTypeList.HEAD_UP_EVENT ? this.glance.eventForGesture("head-tilt") : null;
|
||||
}
|
||||
return this.glance.eventForGesture(inputEvent.type);
|
||||
}
|
||||
|
||||
/** Launch or focus an in-process singleton app (notifications, debug tests). */
|
||||
private async launchInProcessApp(
|
||||
windowId: string,
|
||||
@@ -2527,6 +2595,9 @@ 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",
|
||||
|
||||
@@ -71,6 +71,12 @@ export const OsEventTypeList = {
|
||||
RING_LONG_PRESS_RELEASE_EVENT: 10,
|
||||
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
|
||||
SHORT_THEN_LONG_PRESS_EVENT: 11,
|
||||
/**
|
||||
* CFW extension (head-tilt wake branch): the IMU head-up gesture, forwarded
|
||||
* while the display sleeps. Reaches the phone as a display-wake carrying
|
||||
* this eventType; the glance host treats it as a press.
|
||||
*/
|
||||
HEAD_UP_EVENT: 12,
|
||||
} as const;
|
||||
|
||||
export const OsEventTypeName: Record<number, string> = {
|
||||
@@ -86,4 +92,5 @@ export const OsEventTypeName: Record<number, string> = {
|
||||
9: "RING_LONG_PRESS_EVENT",
|
||||
10: "RING_LONG_PRESS_RELEASE_EVENT",
|
||||
11: "SHORT_THEN_LONG_PRESS_EVENT",
|
||||
12: "HEAD_UP_EVENT",
|
||||
};
|
||||
+146
-68
@@ -1,20 +1,79 @@
|
||||
/**
|
||||
* Compatibility check for the glasses firmware. Faceclaw requires the custom
|
||||
* firmware: version >= 2.2.4.34 with the required direct-framebuffer and wear
|
||||
* notification tokens in the CFW capability string. Stock firmware sends no
|
||||
* capability string at all.
|
||||
* Compatibility check for the glasses firmware. Faceclaw requires its own
|
||||
* custom firmware at a specific revision.
|
||||
*
|
||||
* The firmware identifies itself through one extra string in the sid-0x09
|
||||
* settings READ response (protobuf field 100, the "firmware extension" slot):
|
||||
*
|
||||
* "Faceclaw/<n>" -- Faceclaw's custom firmware, revision <n>. The revision
|
||||
* goes up whenever the firmware contract changes and is
|
||||
* not kept in sync with Faceclaw app versions.
|
||||
* "EVENCFW/..." -- older Faceclaw firmware, from before revisions existed
|
||||
* (it advertised feature tokens instead). Always outdated.
|
||||
* (empty) -- stock firmware; it never sends the field.
|
||||
* anything else -- custom firmware from a different source.
|
||||
*
|
||||
* Because an exact revision is required, nothing else in the app gates on the
|
||||
* extension string: once the firmware is compatible, every feature it provides
|
||||
* is assumed present.
|
||||
*/
|
||||
|
||||
import { type FirmwareInfo } from "../native/faceclaw-communicator";
|
||||
export type FirmwareInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
/** Raw contents of the firmware-extension slot ("" on stock firmware). */
|
||||
extension: string;
|
||||
};
|
||||
|
||||
const MIN_FIRMWARE_VERSION = [2, 2, 4, 34];
|
||||
const REQUIRED_FIRMWARE_EXTENSIONS = ["img640", "fbguard", "wearnotify"] as const;
|
||||
/** The Faceclaw firmware revision this build of the app needs. */
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 13;
|
||||
|
||||
const FACECLAW_PREFIX = "Faceclaw/";
|
||||
const LEGACY_PREFIX = "EVENCFW";
|
||||
|
||||
// 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(".");
|
||||
|
||||
export type FirmwareExtension =
|
||||
/** Nothing in the slot: stock firmware. */
|
||||
| { kind: "none" }
|
||||
/** Faceclaw's firmware at the given revision. */
|
||||
| { kind: "faceclaw"; version: number }
|
||||
/** Pre-revision Faceclaw firmware ("EVENCFW/<ver> <tokens>"). */
|
||||
| { kind: "legacy-faceclaw"; text: string }
|
||||
/** Custom firmware from some other project. */
|
||||
| { kind: "other"; text: string };
|
||||
|
||||
export function parseFirmwareExtension(extension: string): FirmwareExtension {
|
||||
const text = extension.trim();
|
||||
if (!text) return { kind: "none" };
|
||||
if (text.startsWith(FACECLAW_PREFIX)) {
|
||||
const version = parseInt(text.slice(FACECLAW_PREFIX.length), 10);
|
||||
if (Number.isFinite(version) && version >= 0) return { kind: "faceclaw", version };
|
||||
return { kind: "other", text };
|
||||
}
|
||||
if (text.startsWith(LEGACY_PREFIX)) return { kind: "legacy-faceclaw", text };
|
||||
return { kind: "other", text };
|
||||
}
|
||||
|
||||
/** Short user-facing description of what the extension slot reported. */
|
||||
export function describeFirmwareExtension(extension: FirmwareExtension): string {
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
return `Faceclaw firmware revision ${extension.version}`;
|
||||
case "legacy-faceclaw":
|
||||
return `an older Faceclaw firmware (reported "${extension.text}")`;
|
||||
case "other":
|
||||
return `custom firmware from a different source (reported "${extension.text}")`;
|
||||
default:
|
||||
return "stock firmware";
|
||||
}
|
||||
}
|
||||
|
||||
function parseDottedVersion(version: string): number[] {
|
||||
return version
|
||||
@@ -36,66 +95,53 @@ function compareVersions(a: number[], b: number[]): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the glasses run Faceclaw's firmware at the required revision or
|
||||
* newer. (A newer revision is accepted: revisions only add to the contract.)
|
||||
*/
|
||||
export function hasCompatibleFirmware(info: FirmwareInfo): boolean {
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
return extension.kind === "faceclaw" && extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Human-readable explanation of why this firmware cannot run Faceclaw, or
|
||||
* null if it is compatible. Returns null when no version was reported at all
|
||||
* null if it is compatible. Returns null when nothing was reported at all
|
||||
* (no data is not evidence of incompatibility).
|
||||
*/
|
||||
export function firmwareIncompatibilityMessage(info: FirmwareInfo): string | null {
|
||||
const reportedVersions = [info.leftVersion, info.rightVersion].filter((v) => v.trim().length > 0);
|
||||
if (reportedVersions.length === 0) return null;
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
if (reportedVersions.length === 0 && extension.kind === "none") return null;
|
||||
|
||||
const minVersionText = MIN_FIRMWARE_VERSION.join(".");
|
||||
const versionsText = `L=${info.leftVersion || "unknown"} R=${info.rightVersion || "unknown"}`;
|
||||
const required = `Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}`;
|
||||
|
||||
if (reportedVersions.some((v) => compareVersions(parseDottedVersion(v), MIN_FIRMWARE_VERSION) < 0)) {
|
||||
return (
|
||||
`The glasses report firmware ${versionsText}, but Faceclaw requires the modified firmware, ` +
|
||||
`version ${minVersionText} or newer. Displaying images will not work until the glasses firmware is updated.`
|
||||
);
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
if (extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION) return null;
|
||||
return (
|
||||
`The glasses run Faceclaw firmware revision ${extension.version} (base ${versionsText}), ` +
|
||||
`but this version of Faceclaw requires revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}. ` +
|
||||
"Install the updated custom firmware to continue."
|
||||
);
|
||||
case "legacy-faceclaw":
|
||||
return (
|
||||
`The glasses run an older version of Faceclaw's custom firmware (reported "${extension.text}", ` +
|
||||
`base ${versionsText}). This version of Faceclaw requires ${required}. ` +
|
||||
"Install the updated custom firmware to continue."
|
||||
);
|
||||
case "other":
|
||||
return (
|
||||
`The glasses run custom firmware from a different source (reported "${extension.text}", ` +
|
||||
`base ${versionsText}). Faceclaw requires ${required}; installing it will replace the current firmware.`
|
||||
);
|
||||
default:
|
||||
return (
|
||||
`The glasses report stock firmware ${versionsText}. Faceclaw requires ${required}. ` +
|
||||
"Displaying images will not work until the custom firmware is installed."
|
||||
);
|
||||
}
|
||||
|
||||
const tokens = info.capabilities.trim().split(/\s+/);
|
||||
const missingExtensions = REQUIRED_FIRMWARE_EXTENSIONS.filter(
|
||||
(extension) => !tokens.includes(extension),
|
||||
);
|
||||
if (missingExtensions.length) {
|
||||
return (
|
||||
`The glasses firmware (${versionsText}) does not advertise the required ` +
|
||||
`${missingExtensions.map((extension) => `"${extension}"`).join(" and ")} extension` +
|
||||
`${missingExtensions.length === 1 ? "" : "s"}` +
|
||||
`${info.capabilities.trim() ? ` (reported: ${info.capabilities.trim()})` : ", which suggests stock firmware"}. ` +
|
||||
`Faceclaw requires the modified firmware with the guarded 640x480 direct-framebuffer path and wear notifications.`
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** True when the glasses advertise every custom-firmware extension Faceclaw needs. */
|
||||
export function hasCustomFirmware(info: FirmwareInfo): boolean {
|
||||
const tokens = info.capabilities.trim().split(/\s+/);
|
||||
return REQUIRED_FIRMWARE_EXTENSIONS.every((extension) => tokens.includes(extension));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the firmware advertises the optional mic-control extension: the
|
||||
* private field-103/104 channel for per-temple multi-microphone capture that
|
||||
* the Microphones app's 4-mic array mode and beamforming need. Absent on the
|
||||
* stock single-mono-stream path, which still works (firmware-computed
|
||||
* direction of arrival, one mixed channel).
|
||||
*/
|
||||
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. */
|
||||
@@ -109,24 +155,56 @@ 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.
|
||||
* - "custom": Faceclaw's firmware at the required revision (or newer) is
|
||||
* already installed — nothing to flash.
|
||||
* - "older-faceclaw": Faceclaw's firmware at an older revision (including
|
||||
* the pre-revision "EVENCFW" builds) — flash to update it.
|
||||
* - "other-custom": custom firmware from a different source — flash to
|
||||
* replace it.
|
||||
* - "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"
|
||||
| "older-faceclaw"
|
||||
| "other-custom"
|
||||
| "flashable-stock"
|
||||
| "newer-stock-validated"
|
||||
| "newer-stock-unvalidated"
|
||||
| "unknown";
|
||||
|
||||
export function classifyOnboardingFirmware(info: FirmwareInfo): {
|
||||
kind: OnboardingFirmwareKind;
|
||||
version: string;
|
||||
extension: FirmwareExtension;
|
||||
} {
|
||||
if (hasCustomFirmware(info)) {
|
||||
return { kind: "custom", version: reportedFirmwareVersion(info) };
|
||||
}
|
||||
const extension = parseFirmwareExtension(info.extension);
|
||||
const version = reportedFirmwareVersion(info);
|
||||
switch (extension.kind) {
|
||||
case "faceclaw":
|
||||
return {
|
||||
kind: extension.version >= REQUIRED_FACECLAW_FIRMWARE_VERSION ? "custom" : "older-faceclaw",
|
||||
version,
|
||||
extension,
|
||||
};
|
||||
case "legacy-faceclaw":
|
||||
return { kind: "older-faceclaw", version, extension };
|
||||
case "other":
|
||||
return { kind: "other-custom", version, extension };
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!version) {
|
||||
return { kind: "unknown", version: "" };
|
||||
return { kind: "unknown", version: "", extension };
|
||||
}
|
||||
if (compareVersions(parseDottedVersion(version), BASE_STOCK_VERSION) <= 0) {
|
||||
return { kind: "flashable-stock", version, extension };
|
||||
} else if (compareVersions(parseDottedVersion(version), VALIDATED_STOCK_VERSION) <= 0) {
|
||||
return { kind: "newer-stock-validated", version, extension };
|
||||
} else {
|
||||
return { kind: "newer-stock-unvalidated", version, extension };
|
||||
}
|
||||
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
|
||||
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
|
||||
}
|
||||
@@ -19,168 +19,192 @@ export type FirmwarePatchSet = {
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.2.9.22.bin",
|
||||
"baseSha256": "a03fbea9f68a9de6bc271daabb9f3a41c59053d1086622c76a4e990f829cc561",
|
||||
"outputSha256": "920f5e45129453c155d7fb004b93049985594744be973e793cf41e8b6e07005a",
|
||||
"outputSha256": "32cea69474252a2b2f9c0fdc5f89bf0d5d6f745b4eec10aea8e9c34e3f106b3f",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 935280,
|
||||
"old": "fff717f8",
|
||||
"new": "64f395fb",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 935628,
|
||||
"old": "fef769ff",
|
||||
"new": "64f3e7fa",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 943818,
|
||||
"old": "fcf76aff",
|
||||
"new": "62f3e8fa",
|
||||
"desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 1415004,
|
||||
"old": "fcf745f8",
|
||||
"new": "eef23dfb",
|
||||
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
|
||||
},
|
||||
{
|
||||
"offset": 1363762,
|
||||
"old": "7c20",
|
||||
"new": "7d20",
|
||||
"desc": "Set Local Feature: enable LE 2M bit 8"
|
||||
},
|
||||
{
|
||||
"offset": 4410298,
|
||||
"old": "0c001800",
|
||||
"new": "06000600",
|
||||
"desc": "fast connection interval min=max=7.5 ms; latency remains 0"
|
||||
},
|
||||
{
|
||||
"offset": 1053266,
|
||||
"old": "0500",
|
||||
"new": "a325",
|
||||
"desc": "_connectParamReq_impl: force requested mode to fast (0xa3); disables idle slow requests"
|
||||
},
|
||||
{
|
||||
"offset": 1124178,
|
||||
"old": "5ff43432",
|
||||
"new": "5ff43332",
|
||||
"desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor"
|
||||
},
|
||||
{
|
||||
"offset": 1524180,
|
||||
"old": "bdf82c10",
|
||||
"new": "40f24120",
|
||||
"desc": "container width <= 576"
|
||||
},
|
||||
{
|
||||
"offset": 1524380,
|
||||
"old": "bdf82e00",
|
||||
"new": "40f22111",
|
||||
"desc": "container height movw #0x121"
|
||||
},
|
||||
{
|
||||
"offset": 1524384,
|
||||
"old": "9128",
|
||||
"new": "8842",
|
||||
"desc": "container height cmp r0,r1"
|
||||
},
|
||||
{
|
||||
"offset": 1516784,
|
||||
"old": "70f775ff",
|
||||
"new": "d6f295fa",
|
||||
"desc": "bl snapshot_side @ 0x4ec0ee (shared image-complete helper)"
|
||||
},
|
||||
{
|
||||
"offset": 1516816,
|
||||
"old": "30d0",
|
||||
"new": "1cd0",
|
||||
"desc": "image-complete success -> immediate ACK (pipelining-safe)"
|
||||
},
|
||||
{
|
||||
"offset": 1222660,
|
||||
"old": "49f0daff",
|
||||
"new": "1ef30ff9",
|
||||
"desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)"
|
||||
},
|
||||
{
|
||||
"offset": 1242342,
|
||||
"old": "d4f790fb",
|
||||
"new": "1af3faf9",
|
||||
"new": "1af33cfc",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1240038,
|
||||
"old": "f5f710f9",
|
||||
"new": "1af3bafb",
|
||||
"new": "1af3dafd",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 939336,
|
||||
"old": "0bf02af9",
|
||||
"new": "63f325fe",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open double-tap takeover)"
|
||||
"new": "64f351f8",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f146 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 939528,
|
||||
"old": "0bf0caf8",
|
||||
"new": "63f3c5fd",
|
||||
"desc": "bl faceclaw_display_start @ 0x45f206 (fail-open double-tap takeover)"
|
||||
"new": "64f3bff8",
|
||||
"desc": "bl faceclaw_display_start_headup @ 0x45f206 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 939016,
|
||||
"old": "10f096f8",
|
||||
"new": "65f397fd",
|
||||
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
|
||||
},
|
||||
{
|
||||
"offset": 939036,
|
||||
"old": "fff765fb",
|
||||
"new": "63f3ddff",
|
||||
"desc": "bl faceclaw_idle_input_gate (idle tap/long/release -> field-102 events 2/3/4)"
|
||||
},
|
||||
{
|
||||
"offset": 831036,
|
||||
"old": "1df057fc",
|
||||
"new": "7ff3b3fd",
|
||||
"new": "7ff373ff",
|
||||
"desc": "bl gesture_press (Faceclaw lease gates event 9 vs stock UI event 8)"
|
||||
},
|
||||
{
|
||||
"offset": 831042,
|
||||
"old": "24f023f9",
|
||||
"new": "7ff354fe",
|
||||
"new": "80f314f8",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 831800,
|
||||
"old": "1df0d9fa",
|
||||
"new": "7ff339fc",
|
||||
"new": "7ff3f9fd",
|
||||
"desc": "bl gesture_release (source-qualified event 10 vs stock UI event 0x4a)"
|
||||
},
|
||||
{
|
||||
"offset": 1553752,
|
||||
"old": "7fb50600",
|
||||
"new": "cef2afb9",
|
||||
"new": "cef2f1bb",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1047796,
|
||||
"old": "f6f7edff",
|
||||
"new": "45f37df9",
|
||||
"new": "45f32ffd",
|
||||
"desc": "bl display_copy_hook @ 0x4798f2 (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1048112,
|
||||
"old": "f6f74fff",
|
||||
"new": "45f3dff8",
|
||||
"new": "45f391fc",
|
||||
"desc": "bl display_copy_hook @ 0x479a2e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1255404,
|
||||
"old": "d9f758ff",
|
||||
"new": "16f3fbfb",
|
||||
"new": "16f3f9fd",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac3ea (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1255504,
|
||||
"old": "d9f726ff",
|
||||
"new": "16f3c9fb",
|
||||
"new": "16f3c7fd",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ac44e (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 831998,
|
||||
"old": "1df076fa",
|
||||
"new": "79f3b8fe",
|
||||
"desc": "bl compass_event_forward (global IMU heading -> stock nav BLE notifier)"
|
||||
"offset": 1297700,
|
||||
"old": "65f0affd",
|
||||
"new": "0cf351f8",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 1296176,
|
||||
"old": "fff759fc",
|
||||
"new": "0cf3c7fb",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 4476518,
|
||||
"old": "",
|
||||
"new": "02fb01f000e000bfb0b54ff2ac44c2f229040146206846f23f52c5f28802b2eb500f40f258304cf2e9124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304cf20732c2f20000c0f24802104770b500284ed001684ef66962ccf2ff02914218bf70bd90f8081190f8095146f6756c8d42c0f2510c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b142f64d42c0f244022146bde87040104770bd00bf70b542f2ed23c0f2460314460d4698474ff2a841c2f229010e6831466ff39601b1f1005f0ad131684ef66962ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04cf6dd71c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd4ff27d42c0f2590208469047284670bd00bf2de9f04f9db04ff2a84ac2f2290adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66967ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f6b400c2f20000d0f8008004a802f0aaffb8f1000f00f0f881032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f04f8196f8dc10002000298df8140000f0c78105a90a5c002a00f09a810a185378002b00f058819378002b00f05681d378002b00f054811379002b00f052815379002b00f050819379002b00f04e81d379002b00f04c81137a002b00f04a81537a002b00f04881937a002b00f04681d37a002b00f04481137b002b00f04281537b002b00f04081937b002b00f03e81d37b002b00f03c81137c002b00f03a81537c002b00f03881937c002b00f03681d37c002b00f03481137d002b00f03281537d002b00f03081937d002b00f02e81d37d002b00f02c81137e002b00f02a81537e002b00f02881937e002b00f02681d37e002b00f02481137f002b00f02281537f002b00f02081937f002b00f01e81d27f002a00f01c81203060287ff47baf3be19bf82a0270b1dbf82c0240b146f68051c2f207010968401a002800f3c98000208bf82a0240f6d100c0f247001db0bde8f04f00474ff4217004f071fd002800f09c804ff67c51cff6ff71002200bf43182e3183f8842283f8852283f8862283f8872283f8882283f8892283f88a2283f88b2283f88c2283f88d2283f88e2283f88f2283f8902283f8912283f8922283f8932283f8942283f8952283f8962283f8972283f8982283f8992283f89a2283f89b2283f89c2283f89d2283f89e2283f89f2283f8a02283f8a12283f8a22283f8a32283f8a42283f8a52283f8a62283f8a72283f8a82283f8a92283f8aa2283f8ab2283f8ac2283f8ad2283f8ae2283f8af2283f8b02283f8b122a0d1012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf8290240f6d100c0f24700804715e00020caf8000000208bf82802cbf824024ff416304cf60361069005a8c0f24701cdf81480884701208bf82a0204a802f08bfecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0178500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e01Line truncated
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff2a84416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2c731c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bb804ff4377005f039f8002800f0a1824ff62451cff6ff7142183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff436afbff35f8f44e8000121b154e8001f5145f8d02be7aa4645ea064100260127c0f8701206e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bc804ff4377004f05eff002800f0c6814ff62451cff6ff7100bf42183d3182f8dc6282f8dd6282f8de6282f8df6282f8e06282f8e16282f8e26282f8e36282f8e46282f8e56282f8e66282f8e76282f8e86282f8e96282f8ea6282f8eb6282f8ec6282f8ed6282f8ee6282f8ef6282f8f06282f8f16282f8f26282f8f36282f8f46282f8f56282f8f66282f8f76282f8f86282f8f96282f8fa6282f8fb6282f8fc6282f8fd6282f8fe6282f8ff6282f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382d1c0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff437afbff35f8f00bf44e8000121b154e8001fa942f8d02be7d0f8746242f6f767c0f24407002ebff35f8f00f0eb8007f19a0230464ff0ff314ff0ff379047002840f0418143f67750c0f24f00804700274ff001080397adf8107006e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e1804ff4377004f068fe002800f0d9804ff62451cff6ff7100bf42183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff436afbff35f8f44e8000149b354e8001fa942f8d02be705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f9e64ff0ff3742e007f5a9718847f2e60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f07df903ab28465146012205f0abfc9df8111007464846002918bf4ff0ff3702f0a3fabbf1000f044608d19df8100028b949f28340c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b149f28341c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e770b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f8455046f6756c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9fb04ff2a840c2f229000668bff35f8f30466ff39600b0f1005f42d130684ef66c65ccf2ff05a8423bd196f8640130b3d6f860411cb340f6b400c2f20000076804a802f0a5f8002f00f06a8140f2030a384621464ff416320296cff6807a4ff6ff760397fff752fb4ff6245b48f2c738cff6ff7b00270124c0f245081ce096f8660170b1d6f8680140b146f68051c2f207010968401a002802f37080002086f8660140f6d100c0f247001fb0bde8f04f0047bff32f8fc0474ff2a840c2f22900d0f80090bff35f8f0Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 779114,
|
||||
"old": "846a3800",
|
||||
"new": "0e073900",
|
||||
"new": "7c1b3900",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "046b3800",
|
||||
"new": "8e073900",
|
||||
"new": "fc1b3900",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 779234,
|
||||
"old": "846a3804",
|
||||
"new": "0e073904",
|
||||
"new": "7c1b3904",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 779238,
|
||||
"old": "85c355a2",
|
||||
"new": "21ab3673",
|
||||
"new": "a1afb7e4",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "ed292ee4",
|
||||
"new": "9e600c9c",
|
||||
"new": "b1adc0d0",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 779118,
|
||||
"old": "ed292ee4",
|
||||
"new": "9e600c9c",
|
||||
"new": "b1adc0d0",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* Shows the Glanceboard while the shell sleeps. The board is not a window:
|
||||
* it lives on its own full-screen opaque compositor surface above the shell
|
||||
* (below only the lock screen), so it never composites with the sidebar,
|
||||
* top bar, or the sleeping app's retained frame. The host owns that surface,
|
||||
* the visibility state machine (glance-state.ts), its auto-hide timer, and
|
||||
* the board's render loop; the controller feeds it sleep-time input and the
|
||||
* wake barrier, and takes the compositor back to blank when the board hides.
|
||||
*/
|
||||
import { GrayImage, G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||
import { prepareFrameDraws } from "../graphics/glyph-wire";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
|
||||
import * as frameTimings from "../native/frame-timings";
|
||||
import { type DisplayTarget } from "../native/preview-display";
|
||||
import { minWindowTop } from "../ui/shell/geometry";
|
||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||
import { type GlanceBoardInstance, type GlanceboardProvider } from "../apps/app-definition";
|
||||
import { GLANCE_HIDDEN, glanceEventForGesture, reduceGlance, type GlanceEvent, type GlanceState } from "./glance-state";
|
||||
|
||||
export const GLANCE_SURFACE_ID = "glance";
|
||||
/** Above the shell (1) and every window (0); below the lock screen (1000). */
|
||||
export const GLANCE_SURFACE_Z_ORDER = 900;
|
||||
|
||||
export type GlanceHostOptions = {
|
||||
getDisplay: () => DisplayTarget | null;
|
||||
getProvider: () => GlanceboardProvider | null;
|
||||
/** Whether a board may show right now (connected or preview, not locked). */
|
||||
canShow: () => boolean;
|
||||
/**
|
||||
* The controller's wake barrier: restore the EvenHub session if it was
|
||||
* suspended and unblank the compositor. Resolves once the frame is visible.
|
||||
*/
|
||||
ensureSessionActive: (frameId: number) => Promise<boolean>;
|
||||
/**
|
||||
* The board hid on its own (timer or release) with the shell still
|
||||
* asleep: the controller re-blanks and re-arms the screen-off power save.
|
||||
*/
|
||||
onHiddenWhileAsleep: () => void;
|
||||
/** Visibility changed either way (phone mirror refresh). */
|
||||
onVisibilityChanged: () => void;
|
||||
appendLog: (message: string) => void;
|
||||
};
|
||||
|
||||
export class GlanceHost {
|
||||
private state: GlanceState = GLANCE_HIDDEN;
|
||||
private board: GlanceBoardInstance | null = null;
|
||||
/** Whether the surface is currently visible on the compositor. */
|
||||
private shown = false;
|
||||
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private configuredFor: DisplayTarget | null = null;
|
||||
/** Serializes show/hide against each other and against in-flight renders. */
|
||||
private queue: Promise<void> = Promise.resolve();
|
||||
private rendering = false;
|
||||
private renderPending = false;
|
||||
private nextRenderWantsFreshData = false;
|
||||
|
||||
constructor(private readonly options: GlanceHostOptions) {}
|
||||
|
||||
isVisible(): boolean {
|
||||
return this.state.visible;
|
||||
}
|
||||
|
||||
/** Whether sleep-time gestures should reach the board at all. */
|
||||
isEnabled(): boolean {
|
||||
return this.options.getProvider()?.isEnabled() ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The glance event a sleep-time gesture means under the board's settings,
|
||||
* or null when the shell should see the input as usual: nothing while the
|
||||
* board is disabled, no press from a tap when "Show on tap" is disabled,
|
||||
* no hold when "Show on long press" is off, no press from a head-tilt when
|
||||
* "Show on head tilt" is off (it then wakes the regular UI). A release
|
||||
* always passes so a hold in progress can end.
|
||||
*/
|
||||
eventForGesture(gesture: "head-tilt" | string): GlanceEvent | null {
|
||||
const provider = this.options.getProvider();
|
||||
if (!provider?.isEnabled()) return null;
|
||||
if (gesture === "click" && !provider.showOnTap()) return null;
|
||||
if ((gesture === "long-press" || gesture === "short-then-long-press") && !provider.showOnLongPress()) return null;
|
||||
if (gesture === "head-tilt" && !provider.showOnHeadTilt()) return null;
|
||||
return glanceEventForGesture(gesture, this.isVisible());
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a glance event from sleep-time input. The frame is owned from here:
|
||||
* a show submits it with the board's first frame, anything else finishes it.
|
||||
*/
|
||||
async handleEvent(event: GlanceEvent, frameId: number): Promise<void> {
|
||||
const before = this.state;
|
||||
const timeoutMs = this.options.getProvider()?.tapTimeoutMs();
|
||||
this.state = reduceGlance(before, event, Date.now(), timeoutMs);
|
||||
this.armHideTimer();
|
||||
if (this.state.visible === before.visible) {
|
||||
frameTimings.finishFrame(frameId, this.state.visible ? "glanceboard stays up" : "glance event while hidden");
|
||||
return;
|
||||
}
|
||||
if (this.state.visible) {
|
||||
await this.enqueue(() => this.show(frameId));
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "glanceboard hidden");
|
||||
await this.enqueue(() => this.hide(true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide because the regular UI is waking (double-tap, a notification, ...):
|
||||
* the shell's own wake path handles the compositor from here, so no re-blank.
|
||||
*/
|
||||
dismiss(): void {
|
||||
if (!this.state.visible && !this.shown && !this.board) return;
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
void this.enqueue(() => this.hide(false));
|
||||
}
|
||||
|
||||
/** The display target went away (disconnect, preview teardown): forget everything. */
|
||||
reset(): void {
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
this.board?.stop();
|
||||
this.board = null;
|
||||
this.shown = false;
|
||||
this.configuredFor = null;
|
||||
this.renderPending = false;
|
||||
}
|
||||
|
||||
private enqueue(operation: () => Promise<void>): Promise<void> {
|
||||
const run = this.queue.then(operation, operation).catch((error) => {
|
||||
this.options.appendLog(`glanceboard operation failed: ${String(error)}`);
|
||||
});
|
||||
this.queue = run;
|
||||
return run;
|
||||
}
|
||||
|
||||
private armHideTimer(): void {
|
||||
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
||||
this.hideTimer = null;
|
||||
const hideAtMs = this.state.hideAtMs;
|
||||
if (!this.state.visible || hideAtMs === null) return;
|
||||
this.hideTimer = setTimeout(() => {
|
||||
this.hideTimer = null;
|
||||
void this.handleEvent({ type: "timeout" }, frameTimings.startFrame("glanceboard timeout"));
|
||||
}, Math.max(0, hideAtMs - Date.now()));
|
||||
}
|
||||
|
||||
private async configureSurface(display: DisplayTarget): Promise<void> {
|
||||
await display.configureSurface(GLANCE_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: G2_LENS_WIDTH,
|
||||
height: G2_LENS_HEIGHT,
|
||||
zOrder: GLANCE_SURFACE_Z_ORDER,
|
||||
transparency: "opaque",
|
||||
});
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||
this.configuredFor = display;
|
||||
}
|
||||
|
||||
private async show(frameId: number): Promise<void> {
|
||||
if (!this.state.visible) {
|
||||
// Hidden again before this ran (a quick release); nothing to do.
|
||||
frameTimings.finishFrame(frameId, "glanceboard show superseded");
|
||||
return;
|
||||
}
|
||||
const display = this.options.getDisplay();
|
||||
const provider = this.options.getProvider();
|
||||
if (!display || !provider || !this.options.canShow()) {
|
||||
this.state = GLANCE_HIDDEN;
|
||||
this.armHideTimer();
|
||||
frameTimings.finishFrame(frameId, "glanceboard unavailable");
|
||||
return;
|
||||
}
|
||||
if (this.configuredFor !== display) await this.configureSurface(display);
|
||||
if (!this.board) {
|
||||
this.board = provider.createBoard(() => this.requestRender());
|
||||
this.board.start();
|
||||
}
|
||||
// Submit the first frame and reveal the surface while the compositor is
|
||||
// still blanked, so the unblank below composites the board on top of the
|
||||
// sleeping UI in one step rather than flashing the app first.
|
||||
await this.render(frameId);
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, true);
|
||||
this.shown = true;
|
||||
const ready = await this.options.ensureSessionActive(frameId);
|
||||
if (!ready) this.options.appendLog("glanceboard: wake barrier did not complete");
|
||||
this.options.onVisibilityChanged();
|
||||
}
|
||||
|
||||
private async hide(stillAsleep: boolean): Promise<void> {
|
||||
const wasShown = this.shown;
|
||||
this.shown = false;
|
||||
const display = this.options.getDisplay();
|
||||
if (wasShown && display && this.configuredFor === display) {
|
||||
// Blank before revealing what is under the board: hiding the surface
|
||||
// first recomposites the sleeping UI's retained frames and transmits
|
||||
// them, a visible flash of the main UI before the screen goes dark.
|
||||
// Blanked, the surface change composites to the same black frame. (The
|
||||
// controller's screen-off path below blanks again, harmlessly.)
|
||||
if (stillAsleep) {
|
||||
await display.setScreenBlanked(true);
|
||||
}
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||
}
|
||||
this.board?.stop();
|
||||
this.board = null;
|
||||
this.renderPending = false;
|
||||
if (wasShown && stillAsleep) this.options.onHiddenWhileAsleep();
|
||||
if (wasShown) this.options.onVisibilityChanged();
|
||||
}
|
||||
|
||||
private requestRender(): void {
|
||||
if (!this.board) return;
|
||||
if (this.rendering) {
|
||||
this.renderPending = true;
|
||||
return;
|
||||
}
|
||||
void this.enqueue(() => this.render(frameTimings.startFrame("render:glanceboard")));
|
||||
}
|
||||
|
||||
/** Paint the board into a full-screen frame and submit it to the surface. */
|
||||
private async render(frameId: number): Promise<void> {
|
||||
const board = this.board;
|
||||
const display = this.options.getDisplay();
|
||||
if (!board || !display || this.configuredFor !== display) {
|
||||
frameTimings.finishFrame(frameId, "discarded: glanceboard not showing");
|
||||
return;
|
||||
}
|
||||
this.rendering = true;
|
||||
try {
|
||||
frameTimings.annotateFrame(frameId, "glanceboard");
|
||||
const wantFreshData = this.nextRenderWantsFreshData;
|
||||
this.nextRenderWantsFreshData = false;
|
||||
beginRenderPass(!wantFreshData);
|
||||
const paintStartedAtMs = Date.now();
|
||||
const frame = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => {
|
||||
const boardImage = board.paint();
|
||||
const full = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
// Centred horizontally in the panel, in the standard window band
|
||||
// vertically (Display > Vertical position).
|
||||
const x = Math.max(0, ((G2_LENS_WIDTH - boardImage.width) / 2) | 0);
|
||||
const y = Math.max(0, Math.min(G2_LENS_HEIGHT - boardImage.height, minWindowTop()));
|
||||
boardImage.composeInto(full, x, y);
|
||||
return full;
|
||||
}),
|
||||
);
|
||||
const paintUsedStaleData = endRenderPass();
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const planes: Plane[] = [{ image: frame, x: 0, y: 0 }];
|
||||
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());
|
||||
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
|
||||
await frameTimings.spanAsync(frameId, "submit", () =>
|
||||
display.submitSurfaceFrame(
|
||||
GLANCE_SURFACE_ID,
|
||||
buffer,
|
||||
{ x: 0, y: 0, width: image.width, height: image.height },
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
preparedDraws,
|
||||
),
|
||||
);
|
||||
if (paintUsedStaleData) {
|
||||
// Same contract as the windows: a paint that used cached data (the
|
||||
// notification icons) gets one follow-up with fresh data.
|
||||
this.nextRenderWantsFreshData = true;
|
||||
this.renderPending = true;
|
||||
}
|
||||
} finally {
|
||||
this.rendering = false;
|
||||
if (this.renderPending && this.board) {
|
||||
this.renderPending = false;
|
||||
this.requestRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* The Glanceboard's visibility rules, as a pure reducer so they can be tested
|
||||
* without the display plumbing. The board is an alternate sleep-time display:
|
||||
* while the shell is asleep a press (single tap, or a head-tilt wake) shows it
|
||||
* for a short while, a hold (long-press or short-then-long-press) shows it until release,
|
||||
* and a double-tap dismisses it in favour of the regular UI. GlanceHost feeds
|
||||
* these events in and applies the resulting visibility to the compositor.
|
||||
*
|
||||
* No NativeScript imports: this module is compiled and run under plain node
|
||||
* by tests/glance-state.test.cjs.
|
||||
*/
|
||||
|
||||
/** Fallback press timeout when the host supplies none; each further press restarts it. */
|
||||
export const GLANCE_TIMEOUT_MS = 3000;
|
||||
|
||||
export type GlanceState = {
|
||||
visible: boolean;
|
||||
/** A long-press is holding the board up; the timer does not run meanwhile. */
|
||||
holding: boolean;
|
||||
/** When the board hides on its own (epoch ms), or null while held / hidden. */
|
||||
hideAtMs: number | null;
|
||||
};
|
||||
|
||||
export type GlanceEvent =
|
||||
/** Single tap or head-tilt wake: show, and (re)start the auto-hide timer. */
|
||||
| { type: "press" }
|
||||
/** Long-press: show and keep showing until the release. */
|
||||
| { type: "hold" }
|
||||
/** The long-press ended. */
|
||||
| { type: "release" }
|
||||
/** The auto-hide timer fired. */
|
||||
| { type: "timeout" }
|
||||
/** Double-tap (or any other wake of the regular UI): hide now. */
|
||||
| { type: "dismiss" };
|
||||
|
||||
export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, hideAtMs: null });
|
||||
|
||||
export function reduceGlance(
|
||||
state: GlanceState,
|
||||
event: GlanceEvent,
|
||||
nowMs: number,
|
||||
timeoutMs: number = GLANCE_TIMEOUT_MS,
|
||||
): GlanceState {
|
||||
switch (event.type) {
|
||||
case "press":
|
||||
// A tap during a hold changes nothing: the hold decides when it ends.
|
||||
if (state.holding) return state;
|
||||
return { visible: true, holding: false, hideAtMs: nowMs + timeoutMs };
|
||||
case "hold":
|
||||
return { visible: true, holding: true, hideAtMs: null };
|
||||
case "release":
|
||||
// Releases delivered late or after a disabled hold gesture mean nothing here.
|
||||
return state.holding ? GLANCE_HIDDEN : state;
|
||||
case "timeout":
|
||||
if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state;
|
||||
return GLANCE_HIDDEN;
|
||||
case "dismiss":
|
||||
return GLANCE_HIDDEN;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Which glance event, if any, a shell input gesture means while the screen is
|
||||
* off. A double-tap only concerns the board while it is showing (it then
|
||||
* dismisses it before waking the regular UI); everything else the shell
|
||||
* already handles or ignores while asleep.
|
||||
*/
|
||||
export function glanceEventForGesture(
|
||||
gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | string,
|
||||
boardVisible: boolean,
|
||||
): GlanceEvent | null {
|
||||
switch (gesture) {
|
||||
case "click":
|
||||
case "head-tilt":
|
||||
return { type: "press" };
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
return { type: "hold" };
|
||||
case "long-press-release":
|
||||
return { type: "release" };
|
||||
case "double-click":
|
||||
return boardVisible ? { type: "dismiss" } : null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+115
-31
@@ -1,6 +1,8 @@
|
||||
import * as protocol from './ble-protocol'
|
||||
import { iosBleTraffic } from './ble-traffic-counters'
|
||||
import { buildBoundingBoxPayload } from './ble-image-optimizer'
|
||||
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
||||
import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
|
||||
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
|
||||
@@ -25,7 +27,9 @@ class AckTimeout extends Error {}
|
||||
// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image
|
||||
// messages queued for writing or awaiting ACK, not individual BLE packets.
|
||||
const DISPLAY_WINDOW_SIZE = 3
|
||||
type DisplayFrame = { packed: Uint8Array; payload: Uint8Array; offset: number; pending: number }
|
||||
type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number;
|
||||
retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void }
|
||||
type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number }
|
||||
|
||||
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
|
||||
export class GlassesSession {
|
||||
@@ -34,6 +38,9 @@ export class GlassesSession {
|
||||
private generation = 0
|
||||
private ids: Record<string, string> = {}
|
||||
private limits: Record<string, number> = {}
|
||||
private cfwPending: CfwPending[] = []
|
||||
private cfwTransports = new Map<string, CfwTransport>()
|
||||
private cfwTimer: number | null = null
|
||||
private pending = new Map<string, PendingAck>()
|
||||
private receiver = new protocol.MessageReceiver()
|
||||
private writes = new Map<string, Promise<void>>()
|
||||
@@ -68,7 +75,6 @@ export class GlassesSession {
|
||||
private microphoneWork: Promise<void> = Promise.resolve()
|
||||
private microphoneEnabled = false
|
||||
constructor(private readonly transport: SessionTransport,
|
||||
private readonly deflate: (data: Uint8Array) => Uint8Array,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {},
|
||||
@@ -114,9 +120,9 @@ export class GlassesSession {
|
||||
this.update('connecting', 'Checking glasses firmware…')
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
|
||||
this.applySettings(settings)
|
||||
const caps = this.state.capabilities.split(/\s+/)
|
||||
if (!['img640', 'fbguard', 'wearnotify'].every(cap => caps.includes(cap)))
|
||||
throw new Error('The glasses do not report compatible Faceclaw firmware (img640, fbguard, wearnotify). Bluetooth is working, but this display requires the modified firmware. Flashing remains available on Android.')
|
||||
const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities }
|
||||
if (!hasCompatibleFirmware(firmware))
|
||||
throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Flashing remains available on Android.')
|
||||
this.update('connecting', 'Starting glasses display…')
|
||||
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
|
||||
await this.lease(true)
|
||||
@@ -176,7 +182,7 @@ export class GlassesSession {
|
||||
private nextMagic(): number {
|
||||
for (let i = 0; i < 156; i++) {
|
||||
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
|
||||
if (magic !== 156 && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
}
|
||||
throw new Error('All message identifiers are in use')
|
||||
}
|
||||
@@ -200,15 +206,20 @@ export class GlassesSession {
|
||||
})
|
||||
}
|
||||
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
|
||||
return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]))
|
||||
}
|
||||
private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise<void> {
|
||||
const identifier = this.ids[role], generation = this.generation
|
||||
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
|
||||
const frames = protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role])
|
||||
const previous = this.writes.get(identifier) ?? Promise.resolve()
|
||||
// A failed write poisons this link's queue until reset: do not transmit
|
||||
// later fragments after a hole in the image stream.
|
||||
const work = previous.then(async () => {
|
||||
for (const frame of frames) {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
for (const frame of build()) {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
await this.transport.write(identifier, protocol.G2_WRITE, frame)
|
||||
iosBleTraffic.recordWrite(frame.length)
|
||||
}
|
||||
@@ -221,6 +232,75 @@ export class GlassesSession {
|
||||
private lease(acquire: boolean): Promise<void[]> {
|
||||
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
|
||||
}
|
||||
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
|
||||
if (payload.length > CFW_MAX_MESSAGE) return Promise.reject(new Error('CFW message too large'))
|
||||
return new Promise((resolve, reject) => {
|
||||
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
|
||||
retryPending: false, deadline: 0, resolve, reject }
|
||||
this.cfwPending.push(item)
|
||||
this.sendCfw(item)
|
||||
})
|
||||
}
|
||||
private sendCfw(item: CfwPending): void {
|
||||
const generation = this.generation, magic = item.magic
|
||||
const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item)
|
||||
void this.writePackets(item.role, () => {
|
||||
let transport = this.cfwTransports.get(item.role)
|
||||
if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) }
|
||||
if (item.retries) transport.reset()
|
||||
return transport.encode(item.payload, magic, 3, this.limits[item.role])
|
||||
}, current).then(() => {
|
||||
if (!current()) return
|
||||
item.deadline = Date.now() + 500
|
||||
this.scheduleCfwRecovery()
|
||||
}).catch(error => { if (current()) this.fail(error, true) })
|
||||
}
|
||||
private receiveCfw(identifier: string, packet: Uint8Array): void {
|
||||
const acks = parseCfwAcks(packet)
|
||||
if (!acks) return
|
||||
for (const ack of acks) {
|
||||
// The ingress temple relays the other lens's result on the SAME link.
|
||||
const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId)
|
||||
if (!item || ack.messageId !== 0) continue
|
||||
if (ack.nack) item.retryPending = true
|
||||
else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens
|
||||
this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`)
|
||||
}
|
||||
while (this.cfwPending.length) {
|
||||
const head = this.cfwPending[0]
|
||||
if (head.retryPending || head.ackLenses !== 3) break
|
||||
this.cfwPending.shift(); head.resolve()
|
||||
}
|
||||
this.scheduleCfwRecovery()
|
||||
}
|
||||
private scheduleCfwRecovery(): void {
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
let deadline = Infinity
|
||||
for (const item of this.cfwPending) {
|
||||
if (item.retryPending) deadline = 0
|
||||
else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline)
|
||||
}
|
||||
if (deadline === Infinity) return
|
||||
this.cfwTimer = setTimeout(() => {
|
||||
this.cfwTimer = null
|
||||
this.recoverCfw()
|
||||
}, Math.max(0, deadline - Date.now()))
|
||||
}
|
||||
private recoverCfw(): void {
|
||||
if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) {
|
||||
this.scheduleCfwRecovery(); return
|
||||
}
|
||||
if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return }
|
||||
this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`)
|
||||
// Go back to the oldest unresolved command, including any ACKed tail.
|
||||
// Fresh stream IDs reject stale ACKs; each replay resets compression history.
|
||||
for (const item of this.cfwPending) {
|
||||
item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0
|
||||
item.deadline = 0; item.retryPending = false
|
||||
}
|
||||
for (const item of this.cfwPending) this.sendCfw(item)
|
||||
}
|
||||
private receive(event: TransportEvent): void {
|
||||
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
|
||||
if (event.kind === 'disconnected') {
|
||||
@@ -252,6 +332,10 @@ export class GlassesSession {
|
||||
}
|
||||
for (const message of this.receiver.receive(event.identifier!, data)) {
|
||||
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
|
||||
if (message.sid === protocol.SID.cfw) {
|
||||
this.receiveCfw(event.identifier!, message.packet!)
|
||||
continue
|
||||
}
|
||||
if (![1, 6].includes(message.flag)) {
|
||||
// A master arm can relay the other arm's ACK; magic is allocated
|
||||
// across BOTH links, matching Android's communicator.
|
||||
@@ -343,32 +427,32 @@ export class GlassesSession {
|
||||
// Compare with the last enqueued image, not the last ACKed one. In an
|
||||
// A -> B -> A sequence, B may still be in flight when A is requested.
|
||||
if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId, this.deflate)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), this.deflate(protocol.rle4(packed)))
|
||||
if (payload.length > 165888) throw new Error('Compressed frame exceeds the glasses image buffer')
|
||||
if (delta) {
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
|
||||
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
const commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
|
||||
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
|
||||
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
this.log(`Display bbox ${delta[3] * 4}x${delta[4] * 2}+${delta[1] * 4}+${delta[2] * 2} fid=${delta[5] | (delta[6] << 8)} (${payload.length} bytes)`)
|
||||
} else this.log(`Display full 640x480 (${payload.length} bytes)`)
|
||||
this.displaySending = { packed, payload, offset: 0, pending: 0 }
|
||||
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
|
||||
this.displaySending = { packed, commands, offset: 0, pending: 0 }
|
||||
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
|
||||
}
|
||||
const frame = this.displaySending, offset = frame.offset
|
||||
frame.offset += 3800; frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.payload.length) this.displaySending = null
|
||||
// send() serializes whole messages on L, but request() waits for each
|
||||
// magic's ACK independently. Fill the other slots without awaiting it.
|
||||
void this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, frame.payload, offset), 'Display frame').then(() => {
|
||||
const frame = this.displaySending, payload = frame.commands[frame.offset++]
|
||||
frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.commands.length) this.displaySending = null
|
||||
// Writes serialize on L while up to three complete commands await their
|
||||
// two-lens ACKs. Retire commands in submission order.
|
||||
void this.requestCfw('left', payload).then(() => {
|
||||
if (generation !== this.generation) return
|
||||
frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now()
|
||||
// Retire in submission order even if relayed ACKs arrive out of order.
|
||||
// A frame counts only after every fragment has been acknowledged.
|
||||
// A frame counts only after every command has been acknowledged.
|
||||
while (this.displayFrames.length) {
|
||||
const completed = this.displayFrames[0]
|
||||
if (completed.offset < completed.payload.length || completed.pending) break
|
||||
if (completed.offset < completed.commands.length || completed.pending) break
|
||||
this.displayFrames.shift(); this.displayed = completed.packed
|
||||
this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state })
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${completed.payload.length} bytes)`)
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`)
|
||||
}
|
||||
this.wake()
|
||||
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
|
||||
@@ -404,6 +488,11 @@ export class GlassesSession {
|
||||
this.timer = this.retryTimer = null; this.pumping = false
|
||||
const pending = [...this.pending.values()]; this.pending.clear()
|
||||
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
const custom = this.cfwPending; this.cfwPending = []
|
||||
for (const item of custom) item.reject(new Error('Connection ended'))
|
||||
this.cfwTransports.clear()
|
||||
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
|
||||
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
|
||||
}
|
||||
@@ -436,12 +525,7 @@ export class GlassesSession {
|
||||
this.update('disconnecting', 'Disconnecting…'); this.reset()
|
||||
try {
|
||||
if (cleanup) {
|
||||
if (this.state.capabilities.split(/\s+/).includes('cleanup11'))
|
||||
await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, new Uint8Array([11]), 0), 'Display cleanup', 1200)
|
||||
else {
|
||||
await this.request('right', protocol.SID.hub, protocol.shutdown, 'Close display', 1200)
|
||||
await this.lease(false)
|
||||
}
|
||||
await this.requestCfw('left', new Uint8Array([11]))
|
||||
}
|
||||
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
|
||||
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
|
||||
|
||||
@@ -308,8 +308,7 @@ export class IosPreviewController {
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (!this.session) {
|
||||
const { deflate } = require('pako') as { deflate: (data: Uint8Array) => Uint8Array }
|
||||
this.session = new GlassesSession(iosBluetooth(), deflate, state => {
|
||||
this.session = new GlassesSession(iosBluetooth(), state => {
|
||||
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
|
||||
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
|
||||
this.syncRuntime()
|
||||
|
||||
+71
-2
@@ -14,6 +14,12 @@
|
||||
* stream to the watch as events so a query made from the wrist can be read
|
||||
* from the wrist.
|
||||
*
|
||||
* The watch's own battery flows the other way for the glasses' top-bar and
|
||||
* Glanceboard indicators: the phone polls it (the watch answers from a
|
||||
* listener service even while its app is closed) and the open watch app
|
||||
* pushes changes unprompted. The level lives in shell.getBatteryLevels()
|
||||
* beside the G2 and R1 ones and is cleared when no watch is reachable.
|
||||
*
|
||||
* Transport is app/native/wear-bridge.ts (Java FaceclawWearBridge); the
|
||||
* message shapes are documented in wear/PROTOCOL.md.
|
||||
*/
|
||||
@@ -88,6 +94,14 @@ const MAX_TEXT_LENGTH = 2000;
|
||||
const STATE_PUBLISH_DEBOUNCE_MS = 150;
|
||||
const STATE_REFRESH_INTERVAL_MS = 30_000;
|
||||
const ASSISTANT_STREAM_MIN_INTERVAL_MS = 250;
|
||||
// Each poll wakes the watch app's process for a moment; the level changes
|
||||
// slowly, and a charger being plugged in is pushed at once whenever the
|
||||
// watch app is open, so a leisurely cadence is enough.
|
||||
const WATCH_BATTERY_POLL_INTERVAL_MS = 5 * 60_000;
|
||||
// Floor between polls that state changes (a watch coming into range, the
|
||||
// glasses connecting) would otherwise trigger back to back.
|
||||
const WATCH_BATTERY_POLL_MIN_GAP_MS = 20_000;
|
||||
const WATCH_BATTERY_URGENT_POLL_LIMIT = 3;
|
||||
|
||||
/** What the dashboard controller lends the watch remote. */
|
||||
export type WearRemoteHost = {
|
||||
@@ -120,6 +134,11 @@ export class WearRemote {
|
||||
private assistantStreamTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private assistantPendingText: string | null = null;
|
||||
private lastAssistantSentAtMs = 0;
|
||||
private lastWatchBatteryPollAtMs = 0;
|
||||
private watchBatteryKnown = false;
|
||||
// Urgent polls that got no answer; a watch app too old to answer them
|
||||
// must not be poked every publish, so after a few the cadence drops back.
|
||||
private watchBatteryPollsUnanswered = 0;
|
||||
|
||||
constructor(private readonly host: WearRemoteHost) {
|
||||
this.active = wearBridge.isAvailable();
|
||||
@@ -141,8 +160,16 @@ export class WearRemote {
|
||||
connection.reachable ? `watch: ${connection.watchName || "watch"} reachable` : "watch: no watch reachable",
|
||||
);
|
||||
}
|
||||
// A watch that just came into range gets the current state at once.
|
||||
if (connection.reachable) this.publishNow(true);
|
||||
// A watch that just came into range gets the current state at once,
|
||||
// and is asked for its battery; one that left takes its indicator
|
||||
// with it (no placeholder).
|
||||
if (changed) this.watchBatteryPollsUnanswered = 0;
|
||||
if (connection.reachable) {
|
||||
this.publishNow(true);
|
||||
this.pollWatchBattery(true);
|
||||
} else if (changed) {
|
||||
this.setWatchBattery(null, null);
|
||||
}
|
||||
});
|
||||
shell.onAssistantActivity((event) => this.forwardAssistantActivity(event));
|
||||
shell.onAlertShown((text) => {
|
||||
@@ -155,9 +182,38 @@ export class WearRemote {
|
||||
// Catch-all refresh for state the controller does not announce (cheap:
|
||||
// identical states are dropped on the Java side).
|
||||
setInterval(() => this.schedulePublish(), STATE_REFRESH_INTERVAL_MS);
|
||||
setInterval(() => this.pollWatchBattery(false), WATCH_BATTERY_POLL_INTERVAL_MS);
|
||||
this.schedulePublish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the reachable watch for its battery. `urgent` skips the poll gap for
|
||||
* a level that is not known yet (the glasses just connected, the watch
|
||||
* just appeared); a level already on screen waits for the regular cadence.
|
||||
*/
|
||||
private pollWatchBattery(urgent: boolean): void {
|
||||
if (!this.active || !this.watchReachable) return;
|
||||
const now = Date.now();
|
||||
const hurry = urgent && !this.watchBatteryKnown && this.watchBatteryPollsUnanswered < WATCH_BATTERY_URGENT_POLL_LIMIT;
|
||||
const gap = hurry ? WATCH_BATTERY_POLL_MIN_GAP_MS : WATCH_BATTERY_POLL_INTERVAL_MS;
|
||||
if (now - this.lastWatchBatteryPollAtMs < gap) return;
|
||||
this.lastWatchBatteryPollAtMs = now;
|
||||
this.watchBatteryPollsUnanswered++;
|
||||
wearBridge.sendToWatch(WEAR_PATHS.batteryRequest, {});
|
||||
}
|
||||
|
||||
/** Record a watch battery report (or its absence) and repaint the indicators. */
|
||||
private setWatchBattery(battery: number | null, charging: boolean | null): void {
|
||||
const current = shell.getBatteryLevels();
|
||||
if (current.watch === battery && current.watchCharging === charging) return;
|
||||
this.watchBatteryKnown = battery !== null;
|
||||
shell.setBatteryLevels({ watch: battery, watchCharging: charging });
|
||||
// Repaint the top bar, as the controller does for G2/R1 reports.
|
||||
const phase = this.host.getState().phase;
|
||||
if (phase === "connected" || phase === "charging") this.host.requestShellRender();
|
||||
this.host.appendLog(battery === null ? "watch: battery unknown" : `watch: battery ${battery}%${charging ? " (charging)" : ""}`);
|
||||
}
|
||||
|
||||
/** Whether a watch running the Faceclaw watch app is reachable right now. */
|
||||
isWatchReachable(): boolean {
|
||||
return this.watchReachable;
|
||||
@@ -183,6 +239,10 @@ export class WearRemote {
|
||||
} catch (error) {
|
||||
this.host.appendLog(`watch: state publish failed: ${formatError(error)}`);
|
||||
}
|
||||
// The indicator is only ever drawn while the glasses are connected, so
|
||||
// a still-unknown level is worth asking for as soon as they are.
|
||||
const phase = this.host.getState().phase;
|
||||
if (phase === "connected" || phase === "charging") this.pollWatchBattery(true);
|
||||
}
|
||||
|
||||
private buildState(): Record<string, unknown> {
|
||||
@@ -240,6 +300,15 @@ export class WearRemote {
|
||||
ack(true);
|
||||
return;
|
||||
}
|
||||
if (message.path === WEAR_PATHS.battery) {
|
||||
// Status, not control: not gated by the remote setting and never
|
||||
// acked (the watch sends it fire-and-forget, without a seq).
|
||||
this.watchBatteryPollsUnanswered = 0;
|
||||
const battery = readNumber(message.payload.battery, -1);
|
||||
const valid = Number.isInteger(battery) && battery >= 0 && battery <= 100;
|
||||
this.setWatchBattery(valid ? battery : null, valid ? message.payload.charging === true : null);
|
||||
return;
|
||||
}
|
||||
if (!watchRemoteEnabledSetting.get()) {
|
||||
ack(false, "Watch control is turned off in Faceclaw's Settings > Watch.");
|
||||
return;
|
||||
|
||||
+62
-23
@@ -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++) {
|
||||
|
||||
@@ -16,6 +16,10 @@ 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>',
|
||||
eye:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/></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:
|
||||
@@ -30,6 +34,8 @@ export const ICON_SVGS = {
|
||||
// Not a Lucide icon: a ball above two angled flippers, for Pinball.
|
||||
pinball:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="6" r="3"/><path d="M4 14l7 5"/><path d="M20 14l-7 5"/><circle cx="4" cy="14" r="1"/><circle cx="20" cy="14" r="1"/></svg>',
|
||||
bird:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M16 7h.01"/><path d="M3.4 18H12a8 8 0 0 0 8-8V7a4 4 0 0 0-7.28-2.3L2 20"/><path d="m20 7 2 .5-2 .5"/><path d="M10 18v3"/><path d="M14 17.75V21"/><path d="M7 18a6 6 0 0 0 3.84-10.61"/></svg>',
|
||||
spade:
|
||||
'<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:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Ambient light sensor access through the custom firmware (CFW image-handler
|
||||
* mode 16, capability token "als16"; see g2flash/patches/als_sensor.c).
|
||||
* mode 16; 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
|
||||
|
||||
+172
-95
@@ -4,50 +4,107 @@ declare const com: any;
|
||||
declare const java: any;
|
||||
|
||||
/**
|
||||
* Download management for the on-device transcription model (Moonshine, via
|
||||
* sherpa-onnx). The model is no longer bundled in the APK; it is fetched on
|
||||
* demand into the same filesDir location that earlier releases copied the
|
||||
* bundled files to, so upgraded installs that already used on-device
|
||||
* transcription need no download. FaceclawVoiceController reads the files
|
||||
* from this directory.
|
||||
* Download management for the on-device transcription models (sherpa-onnx
|
||||
* offline recognizers). Models are fetched on demand into filesDir rather
|
||||
* than bundled in the APK; FaceclawVoiceController reads the files from the
|
||||
* directory each model's `dirName` names.
|
||||
*
|
||||
* Mirrors the on-phone assistant model flow in llama.ts, except the model is
|
||||
* three files rather than one; they download sequentially through
|
||||
* Two models, same shape:
|
||||
* - "moonshine": the original on-device option (three files). The model is
|
||||
* no longer bundled in the APK; it is fetched into the same filesDir
|
||||
* location that earlier releases copied the bundled files to, so
|
||||
* upgraded installs that already used on-device transcription need no
|
||||
* download.
|
||||
* - "whisper-base-en": sherpa-onnx's offline Whisper backend, base.en,
|
||||
* int8-quantized. Picked over tiny.en for materially better accuracy
|
||||
* (Whisper's own tiny/base WER gap is real and well documented) on the
|
||||
* reasoning that FaceclawVoiceController.java no longer re-decodes a
|
||||
* Whisper segment on every live-partial tick — see its ASR_WHISPER_*
|
||||
* comment — so base.en's extra compute is a one-time cost per utterance,
|
||||
* not a cost paid ~1.4x/second while the user is speaking. tiny.en is an
|
||||
* easy swap (this file's numbers only) if base.en proves too slow on a
|
||||
* real phone; nobody has measured that yet. Files are csukuangfj's own
|
||||
* (the sherpa-onnx maintainer's) HF mirror of the upstream sherpa-onnx
|
||||
* release asset, matching the mirror-not-the-GH-tarball approach already
|
||||
* used for Moonshine below. Every hash here was computed locally from a
|
||||
* freshly downloaded copy of the file, not copied from a doc page.
|
||||
*
|
||||
* Mirrors the on-phone assistant model flow in llama.ts, except each model
|
||||
* here is multiple files rather than one; they download sequentially through
|
||||
* FaceclawModelDownloader (resume + pinned sha256 per file).
|
||||
*/
|
||||
|
||||
export type AsrModelId = "moonshine" | "whisper-base-en";
|
||||
|
||||
type AsrModelFile = {
|
||||
name: string;
|
||||
sha256: string;
|
||||
sizeBytes: number;
|
||||
};
|
||||
|
||||
export const ASR_MODEL = {
|
||||
label: "Moonshine (English)",
|
||||
dirName: "sherpa-onnx-moonshine-base-en-quantized-2026-02-27",
|
||||
// Hugging Face mirror of the sherpa-onnx release asset of the same name.
|
||||
// The GitHub release only offers a tar.bz2, which the phone can't unpack;
|
||||
// this mirror serves the same files (sha256-verified) individually.
|
||||
baseUrl:
|
||||
"https://huggingface.co/csukuangfj2/sherpa-onnx-moonshine-base-en-quantized-2026-02-27/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "decoder_model_merged.ort",
|
||||
sha256: "d9d7b333af34bc552580576ddcf248a1c6c839e0d3b43b09afb9376ed009899d",
|
||||
sizeBytes: 109424400,
|
||||
},
|
||||
{
|
||||
name: "encoder_model.ort",
|
||||
sha256: "7c66495948d0d08ec1af454cd4b5514862ae6511e94712a60e6d83eaec8dc8cf",
|
||||
sizeBytes: 31326816,
|
||||
},
|
||||
{
|
||||
name: "tokens.txt",
|
||||
sha256: "2870d843e14c1e187bf1913a521562a63b53933814bd7f2145120468f494a049",
|
||||
sizeBytes: 549350,
|
||||
},
|
||||
] as AsrModelFile[],
|
||||
totalBytes: 141300566,
|
||||
type AsrModelDef = {
|
||||
label: string;
|
||||
dirName: string;
|
||||
baseUrl: string;
|
||||
files: AsrModelFile[];
|
||||
totalBytes: number;
|
||||
};
|
||||
|
||||
export const ASR_MODELS: Record<AsrModelId, AsrModelDef> = {
|
||||
moonshine: {
|
||||
label: "Moonshine (English)",
|
||||
dirName: "sherpa-onnx-moonshine-base-en-quantized-2026-02-27",
|
||||
// Hugging Face mirror of the sherpa-onnx release asset of the same name.
|
||||
// The GitHub release only offers a tar.bz2, which the phone can't unpack;
|
||||
// this mirror serves the same files (sha256-verified) individually.
|
||||
baseUrl:
|
||||
"https://huggingface.co/csukuangfj2/sherpa-onnx-moonshine-base-en-quantized-2026-02-27/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "decoder_model_merged.ort",
|
||||
sha256: "d9d7b333af34bc552580576ddcf248a1c6c839e0d3b43b09afb9376ed009899d",
|
||||
sizeBytes: 109424400,
|
||||
},
|
||||
{
|
||||
name: "encoder_model.ort",
|
||||
sha256: "7c66495948d0d08ec1af454cd4b5514862ae6511e94712a60e6d83eaec8dc8cf",
|
||||
sizeBytes: 31326816,
|
||||
},
|
||||
{
|
||||
name: "tokens.txt",
|
||||
sha256: "2870d843e14c1e187bf1913a521562a63b53933814bd7f2145120468f494a049",
|
||||
sizeBytes: 549350,
|
||||
},
|
||||
],
|
||||
totalBytes: 141300566,
|
||||
},
|
||||
"whisper-base-en": {
|
||||
label: "Whisper (English, base)",
|
||||
dirName: "sherpa-onnx-whisper-base-en-int8",
|
||||
// csukuangfj/sherpa-onnx-whisper-base.en on Hugging Face: the sherpa-onnx
|
||||
// maintainer's own mirror of the project's whisper export, individual
|
||||
// files (no tar.bz2-unpack problem to begin with, but kept as the same
|
||||
// per-file-with-pinned-hash shape as Moonshine above for consistency).
|
||||
baseUrl: "https://huggingface.co/csukuangfj/sherpa-onnx-whisper-base.en/resolve/main/",
|
||||
files: [
|
||||
{
|
||||
name: "base.en-encoder.int8.onnx",
|
||||
sha256: "ef6b936f4c9b1d90a3b68634b60c4ed8576b26172b33c2535ec0e933c9edb823",
|
||||
sizeBytes: 29120534,
|
||||
},
|
||||
{
|
||||
name: "base.en-decoder.int8.onnx",
|
||||
sha256: "f7162ad6db2dbef16cfaeaa7f945b9d7dd9c1b8d472f6aca82f2273d185e4d41",
|
||||
sizeBytes: 130669978,
|
||||
},
|
||||
{
|
||||
name: "base.en-tokens.txt",
|
||||
sha256: "306cd27f03c1a714eca7108e03d66b7dc042abe8c258b44c199a7ed9838dd930",
|
||||
sizeBytes: 835554,
|
||||
},
|
||||
],
|
||||
totalBytes: 160626066,
|
||||
},
|
||||
};
|
||||
|
||||
export type AsrModelState = {
|
||||
@@ -56,121 +113,141 @@ export type AsrModelState = {
|
||||
totalBytes: number;
|
||||
};
|
||||
|
||||
let downloader: any = null;
|
||||
// Bytes of files already fully downloaded in this run, plus progress within
|
||||
// the file currently downloading; drives the aggregate percentage.
|
||||
let completedBytes = 0;
|
||||
let currentFileBytes = 0;
|
||||
const stateListeners = new Set<(state: AsrModelState) => void>();
|
||||
type ModelRuntime = {
|
||||
downloader: any;
|
||||
// Bytes of files already fully downloaded in this run, plus progress within
|
||||
// the file currently downloading; drives the aggregate percentage.
|
||||
completedBytes: number;
|
||||
currentFileBytes: number;
|
||||
stateListeners: Set<(state: AsrModelState) => void>;
|
||||
};
|
||||
|
||||
function modelDirPath(): string {
|
||||
function freshRuntime(): ModelRuntime {
|
||||
return { downloader: null, completedBytes: 0, currentFileBytes: 0, stateListeners: new Set() };
|
||||
}
|
||||
|
||||
const runtimes: Record<AsrModelId, ModelRuntime> = {
|
||||
moonshine: freshRuntime(),
|
||||
"whisper-base-en": freshRuntime(),
|
||||
};
|
||||
|
||||
function modelDirPath(id: AsrModelId): string {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
return `${context.getFilesDir().getAbsolutePath()}/faceclaw-voice-asr/${ASR_MODEL.dirName}`;
|
||||
return `${context.getFilesDir().getAbsolutePath()}/faceclaw-voice-asr/${ASR_MODELS[id].dirName}`;
|
||||
}
|
||||
|
||||
function filePath(file: AsrModelFile): string {
|
||||
return `${modelDirPath()}/${file.name}`;
|
||||
function filePath(id: AsrModelId, file: AsrModelFile): string {
|
||||
return `${modelDirPath(id)}/${file.name}`;
|
||||
}
|
||||
|
||||
function isFilePresent(file: AsrModelFile): boolean {
|
||||
function isFilePresent(id: AsrModelId, file: AsrModelFile): boolean {
|
||||
try {
|
||||
const javaFile = new java.io.File(filePath(file));
|
||||
const javaFile = new java.io.File(filePath(id, file));
|
||||
return javaFile.exists() && javaFile.length() > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isAsrModelReady(): boolean {
|
||||
export function isAsrModelReady(id: AsrModelId): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
return ASR_MODEL.files.every(isFilePresent);
|
||||
return ASR_MODELS[id].files.every((file) => isFilePresent(id, file));
|
||||
}
|
||||
|
||||
export function asrModelState(): AsrModelState {
|
||||
if (downloader) {
|
||||
export function asrModelState(id: AsrModelId): AsrModelState {
|
||||
const runtime = runtimes[id];
|
||||
const totalBytes = ASR_MODELS[id].totalBytes;
|
||||
if (runtime.downloader) {
|
||||
return {
|
||||
status: "downloading",
|
||||
bytesDownloaded: completedBytes + currentFileBytes,
|
||||
totalBytes: ASR_MODEL.totalBytes,
|
||||
bytesDownloaded: runtime.completedBytes + runtime.currentFileBytes,
|
||||
totalBytes,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: isAsrModelReady() ? "ready" : "absent",
|
||||
status: isAsrModelReady(id) ? "ready" : "absent",
|
||||
bytesDownloaded: 0,
|
||||
totalBytes: ASR_MODEL.totalBytes,
|
||||
totalBytes,
|
||||
};
|
||||
}
|
||||
|
||||
export function onAsrModelStateChanged(listener: (state: AsrModelState) => void): () => void {
|
||||
stateListeners.add(listener);
|
||||
return () => stateListeners.delete(listener);
|
||||
export function onAsrModelStateChanged(id: AsrModelId, listener: (state: AsrModelState) => void): () => void {
|
||||
const runtime = runtimes[id];
|
||||
runtime.stateListeners.add(listener);
|
||||
return () => runtime.stateListeners.delete(listener);
|
||||
}
|
||||
|
||||
function notifyStateChanged(): void {
|
||||
const state = asrModelState();
|
||||
stateListeners.forEach((listener) => listener(state));
|
||||
function notifyStateChanged(id: AsrModelId): void {
|
||||
const state = asrModelState(id);
|
||||
runtimes[id].stateListeners.forEach((listener) => listener(state));
|
||||
}
|
||||
|
||||
export function startAsrModelDownload(): void {
|
||||
if (!global.isAndroid || downloader || isAsrModelReady()) return;
|
||||
completedBytes = ASR_MODEL.files.filter(isFilePresent).reduce((sum, f) => sum + f.sizeBytes, 0);
|
||||
downloadNextFile();
|
||||
notifyStateChanged();
|
||||
export function startAsrModelDownload(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
if (!global.isAndroid || runtime.downloader || isAsrModelReady(id)) return;
|
||||
runtime.completedBytes = ASR_MODELS[id].files
|
||||
.filter((file) => isFilePresent(id, file))
|
||||
.reduce((sum, f) => sum + f.sizeBytes, 0);
|
||||
downloadNextFile(id);
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
|
||||
function downloadNextFile(): void {
|
||||
const nextFile = ASR_MODEL.files.find((file) => !isFilePresent(file));
|
||||
function downloadNextFile(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
const def = ASR_MODELS[id];
|
||||
const nextFile = def.files.find((file) => !isFilePresent(id, file));
|
||||
if (!nextFile) {
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
return;
|
||||
}
|
||||
currentFileBytes = 0;
|
||||
runtime.currentFileBytes = 0;
|
||||
const listener = new com.faceclaw.app.FaceclawModelDownloaderListener({
|
||||
onProgress: (bytes: number, _total: number) => {
|
||||
currentFileBytes = Number(bytes);
|
||||
notifyStateChanged();
|
||||
runtime.currentFileBytes = Number(bytes);
|
||||
notifyStateChanged(id);
|
||||
},
|
||||
onDone: () => {
|
||||
completedBytes += nextFile.sizeBytes;
|
||||
currentFileBytes = 0;
|
||||
downloadNextFile();
|
||||
runtime.completedBytes += nextFile.sizeBytes;
|
||||
runtime.currentFileBytes = 0;
|
||||
downloadNextFile(id);
|
||||
},
|
||||
onError: (message: string) => {
|
||||
console.error(`Voice model download failed (${nextFile.name}): ${message}`);
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
console.error(`Voice model download failed (${id}/${nextFile.name}): ${message}`);
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
},
|
||||
});
|
||||
downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${ASR_MODEL.baseUrl}${nextFile.name}`,
|
||||
filePath(nextFile),
|
||||
runtime.downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${def.baseUrl}${nextFile.name}`,
|
||||
filePath(id, nextFile),
|
||||
nextFile.sha256,
|
||||
nextFile.sizeBytes,
|
||||
listener,
|
||||
);
|
||||
downloader.start();
|
||||
runtime.downloader.start();
|
||||
}
|
||||
|
||||
/** Stops the download; already-fetched bytes are kept and resumed next time. */
|
||||
export function cancelAsrModelDownload(): void {
|
||||
if (!downloader) return;
|
||||
downloader.cancel();
|
||||
downloader = null;
|
||||
notifyStateChanged();
|
||||
export function cancelAsrModelDownload(id: AsrModelId): void {
|
||||
const runtime = runtimes[id];
|
||||
if (!runtime.downloader) return;
|
||||
runtime.downloader.cancel();
|
||||
runtime.downloader = null;
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
|
||||
export function deleteAsrModel(): void {
|
||||
export function deleteAsrModel(id: AsrModelId): void {
|
||||
if (!global.isAndroid) return;
|
||||
cancelAsrModelDownload();
|
||||
cancelAsrModelDownload(id);
|
||||
try {
|
||||
for (const file of ASR_MODEL.files) {
|
||||
new java.io.File(filePath(file)).delete();
|
||||
new java.io.File(`${filePath(file)}.part`).delete();
|
||||
for (const file of ASR_MODELS[id].files) {
|
||||
new java.io.File(filePath(id, file)).delete();
|
||||
new java.io.File(`${filePath(id, file)}.part`).delete();
|
||||
}
|
||||
new java.io.File(modelDirPath()).delete();
|
||||
new java.io.File(modelDirPath(id)).delete();
|
||||
} catch (error) {
|
||||
console.error(`Voice model delete failed: ${String(error)}`);
|
||||
console.error(`Voice model delete failed (${id}): ${String(error)}`);
|
||||
}
|
||||
notifyStateChanged();
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark controls (Developer app). The Java communicator
|
||||
* streams no-op image payloads for a fixed duration with a selectable message
|
||||
* size and pipeline window, then reports throughput. Progress and results are
|
||||
* polled with getBandwidthBenchmarkStatus.
|
||||
*
|
||||
* Talks to the active communicator via the FaceclawBleCommunicator.getActive()
|
||||
* static, so it works from any isolate without threading the bridge through.
|
||||
*/
|
||||
|
||||
export type BandwidthBenchmarkStatus = {
|
||||
state: "idle" | "starting" | "running" | "done";
|
||||
messageSize: number;
|
||||
windowSize: number;
|
||||
linkMode: number;
|
||||
elapsedMs: number;
|
||||
messagesSent: number;
|
||||
messagesAcked: number;
|
||||
timeouts: number;
|
||||
payloadBytesAcked: number;
|
||||
wireBytesAcked: number;
|
||||
aborted: boolean;
|
||||
};
|
||||
|
||||
function activeCommunicator(): any {
|
||||
if (!global.isAndroid) return null;
|
||||
try {
|
||||
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Start a run. False if not connected or a run is already active. */
|
||||
export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number, linkMode = 0): boolean {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return false;
|
||||
try {
|
||||
return Boolean(
|
||||
active.startBandwidthBenchmarkWithLinkMode(
|
||||
Math.round(messageSize), Math.round(windowSize), Math.round(durationMs), Math.round(linkMode),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
console.warn(`startBandwidthBenchmark failed: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Cancel an in-progress run. Safe to call when idle. */
|
||||
export function cancelBandwidthBenchmark(): void {
|
||||
try {
|
||||
activeCommunicator()?.cancelBandwidthBenchmark();
|
||||
} catch (error) {
|
||||
console.warn(`cancelBandwidthBenchmark failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Status of the current or most recent run; null when not connected. */
|
||||
export function getBandwidthBenchmarkStatus(): BandwidthBenchmarkStatus | null {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return null;
|
||||
try {
|
||||
return JSON.parse(String(active.getBandwidthBenchmarkStatus())) as BandwidthBenchmarkStatus;
|
||||
} catch (error) {
|
||||
console.warn(`getBandwidthBenchmarkStatus failed: ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,17 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
import { type FirmwareInfo } from "../g2/firmware-compat";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
export type DeviceInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
capabilities: string;
|
||||
};
|
||||
export type DeviceInfo = FirmwareInfo;
|
||||
|
||||
export type DeviceInfoState = "connecting" | "authenticating" | "querying";
|
||||
|
||||
/**
|
||||
* TS wrapper around the native FaceclawDeviceInfoProbe — a one-shot,
|
||||
* stock-compatible connect + firmware/device-info read. `run()` resolves with
|
||||
* the firmware versions and CFW capability string, or rejects on failure.
|
||||
* the firmware versions and the firmware-extension string, or rejects on failure.
|
||||
*/
|
||||
export class DeviceInfoProbe {
|
||||
private readonly probe: any;
|
||||
@@ -34,11 +32,11 @@ export class DeviceInfoProbe {
|
||||
onLog: (line: string) => this.emit(this.logListeners, String(line)),
|
||||
onState: (state: string, detail: string) =>
|
||||
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? "")),
|
||||
onResult: (leftVersion: string, rightVersion: string, capabilities: string) =>
|
||||
onResult: (leftVersion: string, rightVersion: string, extension: string) =>
|
||||
this.settle(null, {
|
||||
leftVersion: String(leftVersion),
|
||||
rightVersion: String(rightVersion),
|
||||
capabilities: String(capabilities),
|
||||
extension: String(extension),
|
||||
}),
|
||||
onError: (message: string) => this.settle(new Error(String(message)), null),
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { Dialogs, Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -23,11 +23,19 @@ export function readEvenAppNotificationState(): EvenAppNotificationState {
|
||||
}
|
||||
|
||||
export function openEvenAppSettings(): void {
|
||||
if (!global.isAndroid) return;
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (context) {
|
||||
com.faceclaw.app.FaceclawEvenAppDetector.openEvenAppSettings(context);
|
||||
}
|
||||
const context = global.isAndroid ? Utils.android.getApplicationContext() : null;
|
||||
const result = context
|
||||
? com.faceclaw.app.FaceclawEvenAppDetector.openEvenAppSettings(context)
|
||||
: "failed";
|
||||
if (result === "opened") return;
|
||||
const notInstalled = result === "not-installed";
|
||||
void Dialogs.alert({
|
||||
title: notInstalled ? "Even app not installed" : "Unable to open Even app settings",
|
||||
message: notInstalled
|
||||
? "The official Even Realities app isn't installed on this phone. If your glasses are connected to another app or phone, disconnect them there. Otherwise, you can continue."
|
||||
: "Open your phone's settings manually to manage the Even app, or disconnect your glasses from within the app that's connected to them.",
|
||||
okButtonText: "OK",
|
||||
});
|
||||
}
|
||||
|
||||
export function openNotificationAccessSettings(): void {
|
||||
|
||||
@@ -22,6 +22,8 @@ export type CommunicatorState = {
|
||||
export type HeadsetBatteryState = {
|
||||
battery: number;
|
||||
chargingStatus: number;
|
||||
ringBattery?: number;
|
||||
ringChargingStatus?: number;
|
||||
};
|
||||
|
||||
export type FrameMetrics = {
|
||||
@@ -30,11 +32,9 @@ export type FrameMetrics = {
|
||||
tileCount: number;
|
||||
};
|
||||
|
||||
export type FirmwareInfo = {
|
||||
leftVersion: string;
|
||||
rightVersion: string;
|
||||
capabilities: string;
|
||||
};
|
||||
import { type FirmwareInfo } from "../g2/firmware-compat";
|
||||
|
||||
export type { FirmwareInfo };
|
||||
|
||||
/**
|
||||
* Compositor surface configuration. Position/size are in screen pixels;
|
||||
@@ -189,10 +189,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);
|
||||
},
|
||||
@@ -226,11 +228,11 @@ export class FaceclawCommunicatorBridge {
|
||||
onFrameFinished: (frameId: number, outcome: string) => {
|
||||
this.recordFrameFinished(Number(frameId), String(outcome));
|
||||
},
|
||||
onFirmwareInfo: (leftVersion: string, rightVersion: string, capabilities: string) => {
|
||||
const info = {
|
||||
onFirmwareInfo: (leftVersion: string, rightVersion: string, extension: string) => {
|
||||
const info: FirmwareInfo = {
|
||||
leftVersion: String(leftVersion),
|
||||
rightVersion: String(rightVersion),
|
||||
capabilities: String(capabilities),
|
||||
extension: String(extension),
|
||||
};
|
||||
this.emitAsync(this.firmwareInfoListeners, info);
|
||||
},
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Open a URL in the phone's browser. Uses the application context with
|
||||
* NEW_TASK so it works from worker threads (no Activity in hand). Returns
|
||||
* false when no browser is available or on a non-Android platform.
|
||||
*/
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const android: any;
|
||||
declare const global: any;
|
||||
|
||||
export function openUrlOnPhone(url: string): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
try {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return false;
|
||||
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url));
|
||||
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn(`open-url: could not open ${url}: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,14 @@ export class IosVoiceControlBridge {
|
||||
private log: (message: string) => void = () => {}
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statuses.add(listener); listener({ status: this.status }); return () => { this.statuses.delete(listener) }
|
||||
this.statuses.add(listener); listener({ status: this.status, listening: this.capturing, detail: '' }); return () => { this.statuses.delete(listener) }
|
||||
}
|
||||
onTranscript(listener: (event: VoiceTranscriptEvent) => void): () => void {
|
||||
this.transcripts.add(listener); return () => { this.transcripts.delete(listener) }
|
||||
}
|
||||
onSpeechEnd(listener: () => void): () => void { this.ends.add(listener); return () => { this.ends.delete(listener) } }
|
||||
onSpeechPause(_listener: () => void): () => void { return () => {} }
|
||||
private setStatus(status: string): void { this.status = status; for (const fn of [...this.statuses]) fn({ status }) }
|
||||
private setStatus(status: string): void { this.status = status; for (const fn of [...this.statuses]) fn({ status, listening: this.capturing, detail: '' }) }
|
||||
private ensureNative(): any {
|
||||
if (!this.native) {
|
||||
this.native = FaceclawSpeech.new()
|
||||
|
||||
@@ -11,10 +11,27 @@ import { toUint8Array } from "../util/array-util";
|
||||
declare const com: any;
|
||||
|
||||
export type VoiceControlState = {
|
||||
/** One-line state of the capture: progress, "Listening...", or an error. */
|
||||
status: string;
|
||||
/**
|
||||
* Whether audio is actually being captured and transcribed right now. False
|
||||
* while the mic is starting, while the phone is showing the permission
|
||||
* prompt, and after an error, so a dialog must not claim to be listening.
|
||||
*/
|
||||
listening: boolean;
|
||||
/**
|
||||
* Longer guidance to show in place of the (still empty) transcript, e.g.
|
||||
* what the user has to do on the phone. Empty when there is nothing to say.
|
||||
*/
|
||||
detail: string;
|
||||
};
|
||||
|
||||
export type VoiceProviderKind = "onboard" | "elevenlabs" | "whisper" | "soniox";
|
||||
// "whisper" (no "onboard-" prefix) is the OpenAI CLOUD provider (gpt-realtime-whisper,
|
||||
// see openai-stt.ts) -- an unfortunate pre-existing name collision with the model
|
||||
// family, not with "onboard-whisper" below, which is the on-device sherpa-onnx
|
||||
// Whisper backend added alongside "onboard" (Moonshine). Kept as-is rather than
|
||||
// renamed, since it's a persisted settings value on real installs already.
|
||||
export type VoiceProviderKind = "onboard" | "onboard-whisper" | "elevenlabs" | "whisper" | "soniox";
|
||||
|
||||
export type VoiceTranscriptEvent = CloudSttTranscriptEvent & {
|
||||
/**
|
||||
@@ -88,7 +105,12 @@ export class FaceclawVoiceControlBridge {
|
||||
private controller: any | null = null;
|
||||
private listenerProxy: any | null = null;
|
||||
private status = "Voice control stopped.";
|
||||
private listening = false;
|
||||
private detail = "";
|
||||
private started = false;
|
||||
private nativeCaptureFinished: Promise<void> = Promise.resolve();
|
||||
private nextNativeCaptureId = 1;
|
||||
private readonly nativeCaptureResolvers = new Map<number, () => void>();
|
||||
// The mic is a single shared stream; these are the reasons it is running.
|
||||
// The first holder starts capture (choosing the provider); the mic stops
|
||||
// when the last one releases. Transcript events broadcast to every
|
||||
@@ -118,7 +140,7 @@ export class FaceclawVoiceControlBridge {
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statusListeners.add(listener);
|
||||
listener({ status: this.status });
|
||||
listener(this.state());
|
||||
return () => this.statusListeners.delete(listener);
|
||||
}
|
||||
|
||||
@@ -142,14 +164,41 @@ export class FaceclawVoiceControlBridge {
|
||||
return () => this.speechPauseListeners.delete(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* The capture is blocked on the phone's system microphone-permission
|
||||
* prompt. Reported by the controller before it asks Android, so the dialog
|
||||
* on the glasses can send the user to the phone instead of claiming to
|
||||
* listen; the next status (from the capture start, or reportPermissionDenied)
|
||||
* replaces it.
|
||||
*/
|
||||
reportPermissionPrompt(): void {
|
||||
this.setStatus(
|
||||
"Waiting for microphone permission...",
|
||||
"Allow microphone access in the prompt on your phone.",
|
||||
);
|
||||
}
|
||||
|
||||
/** The user declined the microphone permission (or the prompt could not be shown). */
|
||||
reportPermissionDenied(): void {
|
||||
this.setStatus(
|
||||
"Microphone access denied.",
|
||||
"Enable the microphone for Faceclaw in the phone's app settings, then try again.",
|
||||
);
|
||||
}
|
||||
|
||||
/** Begin push-to-talk capture (momentary; released with stopPushToTalk). */
|
||||
startPushToTalk(options: PushToTalkOptions): void {
|
||||
this.acquireCapture("ptt", options);
|
||||
}
|
||||
|
||||
/** End push-to-talk: for cloud, commit for a final result if it was the last holder. */
|
||||
stopPushToTalk(): void {
|
||||
stopPushToTalk(): Promise<void> | void {
|
||||
// Shared continuous capture keeps running; cloud providers finalize via
|
||||
// their own callbacks. Only a stopped native recognizer must be awaited.
|
||||
const finished = !this.cloudClient && this.captureHolders.has("ptt") && this.captureHolders.size === 1
|
||||
? this.nativeCaptureFinished : undefined;
|
||||
this.releaseCapture("ptt", true);
|
||||
return finished;
|
||||
}
|
||||
|
||||
/** Begin continuous capture (Transcribe): the mic stays on until released. */
|
||||
@@ -262,6 +311,9 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
|
||||
this.speechPause.reset();
|
||||
// Replaces whatever the last capture left behind ("Listening...", an
|
||||
// error, the permission prompt) until the controller reports progress.
|
||||
this.setStatus("Starting microphone...");
|
||||
// A previous push-to-talk commit may still be awaiting its final result.
|
||||
this.cloudClient?.stop();
|
||||
const cloudClient = this.createCloudClient(options);
|
||||
@@ -275,7 +327,12 @@ export class FaceclawVoiceControlBridge {
|
||||
|
||||
this.cloudClient = null;
|
||||
this.started = true;
|
||||
this.controller?.start("onboard");
|
||||
// Which on-device model to load; a no-op setter for every provider except
|
||||
// "onboard-whisper" (FaceclawVoiceController defaults to Moonshine).
|
||||
this.controller?.setOnboardModelKind(options.provider === "onboard-whisper" ? "whisper" : "moonshine");
|
||||
const captureId = this.nextNativeCaptureId++;
|
||||
this.nativeCaptureFinished = new Promise((resolve) => { this.nativeCaptureResolvers.set(captureId, resolve); });
|
||||
this.controller?.start("onboard", captureId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,7 +341,7 @@ export class FaceclawVoiceControlBridge {
|
||||
* than failing the capture outright.
|
||||
*/
|
||||
private createCloudClient(options: PushToTalkOptions): CloudSttClient | null {
|
||||
if (options.provider === "onboard") return null;
|
||||
if (options.provider === "onboard" || options.provider === "onboard-whisper") return null;
|
||||
const sttOptions = {
|
||||
apiKey: "",
|
||||
onTranscript: (event: CloudSttTranscriptEvent) =>
|
||||
@@ -444,6 +501,10 @@ export class FaceclawVoiceControlBridge {
|
||||
onTranscript: (text: string, isFinal: boolean) => {
|
||||
this.emitTranscript(String(text), Boolean(isFinal));
|
||||
},
|
||||
onStopped: (captureId: number) => {
|
||||
this.nativeCaptureResolvers.get(captureId)?.();
|
||||
this.nativeCaptureResolvers.delete(captureId);
|
||||
},
|
||||
onPcm: (pcm: any) => {
|
||||
const bytes = toUint8Array(pcm);
|
||||
this.cloudClient?.acceptPcm(bytes);
|
||||
@@ -492,12 +553,21 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
}
|
||||
|
||||
private setStatus(status: string): void {
|
||||
private setStatus(status: string, detail = ""): void {
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
// Every "audio is flowing" report — from the Java controller and from the
|
||||
// cloud providers — is phrased "Listening..." / "Listening (X)...".
|
||||
this.listening = status.startsWith("Listening");
|
||||
const state = this.state();
|
||||
for (const listener of this.statusListeners) {
|
||||
listener({ status });
|
||||
listener(state);
|
||||
}
|
||||
}
|
||||
|
||||
private state(): VoiceControlState {
|
||||
return { status: this.status, listening: this.listening, detail: this.detail };
|
||||
}
|
||||
}
|
||||
|
||||
function toJavaFloats(values: Float32Array): any {
|
||||
|
||||
@@ -24,6 +24,10 @@ export const WEAR_PATHS = {
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
@@ -157,7 +161,12 @@ class WearBridge {
|
||||
|
||||
/** Send an event to every reachable watch. */
|
||||
sendEvent(payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(WEAR_PATHS.event, JSON.stringify(payload));
|
||||
this.sendToWatch(WEAR_PATHS.event, payload);
|
||||
}
|
||||
|
||||
/** Send a message on any phone -> watch path to every reachable watch. */
|
||||
sendToWatch(path: string, payload: Record<string, unknown>): void {
|
||||
this.getJava()?.sendToWatch(path, JSON.stringify(payload));
|
||||
}
|
||||
|
||||
/** Acknowledge a watch message; `seq` echoes the watch's sequence number. */
|
||||
|
||||
@@ -7,14 +7,13 @@
|
||||
<Label col="2" text="{{ connectionStatusLabel }}" tap="{{ onConnectionStatusTap }}" class="action-bar-status" />
|
||||
<Label col="3" text="⚠️" tap="{{ onWarningIconTap }}" visibility="{{ warningIconVisibility }}" class="action-bar-warning" />
|
||||
</GridLayout>
|
||||
<ActionItem text="{{ buttonLabel }}" tap="{{ onTap }}" isEnabled="{{ connectItemEnabled }}" android.position="popup" />
|
||||
<ActionItem text="{{ buttonLabel }}" tap="{{ onTap }}" isEnabled="{{ connectItemEnabled }}" visibility="{{ glassesMenuItemsVisibility }}" android.position="popup" />
|
||||
<ActionItem text="Pair glasses" tap="{{ onPairGlassesTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Configure devices" tap="{{ onConfigureTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Permissions" tap="{{ onPermissionsTap }}" android.position="popup" />
|
||||
<ActionItem text="Conversations" tap="{{ onConversationsTap }}" android.position="popup" />
|
||||
<ActionItem text="Take screenshot" tap="{{ onTakeScreenshotTap }}" android.position="popup" />
|
||||
<ActionItem text="Record screen" tap="{{ onRecordScreenTap }}" android.position="popup" />
|
||||
<ActionItem text="Uninstall custom firmware" tap="{{ onUninstallFirmwareTap }}" isEnabled="{{ canRun }}" android.position="popup" />
|
||||
<ActionItem text="Uninstall custom firmware" tap="{{ onUninstallFirmwareTap }}" isEnabled="{{ canRun }}" visibility="{{ glassesMenuItemsVisibility }}" android.position="popup" />
|
||||
</ActionBar>
|
||||
|
||||
<GridLayout>
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
import { sampleBleTraffic } from "../native/ble-traffic";
|
||||
import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses";
|
||||
import { isAutoReconnectSuppressed, resumeAutoReconnect } from "../g2/reconnect-policy";
|
||||
import { isPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||
|
||||
@@ -887,11 +888,6 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
onConfigureTap(): void {
|
||||
if (!this.canRun) return;
|
||||
Frame.topmost()?.navigate("phone-ui/config-page");
|
||||
}
|
||||
|
||||
onPermissionsTap(): void {
|
||||
Frame.topmost()?.navigate({
|
||||
moduleName: "phone-ui/permissions-page",
|
||||
@@ -904,6 +900,16 @@ export class MainViewModel extends Observable {
|
||||
Frame.topmost()?.navigate("phone-ui/conversations-page");
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview-only users have no glasses paired, so the Connect and Uninstall
|
||||
* menu items have nothing to act on; hide them until pairing completes.
|
||||
* The view model is rebuilt on every visit to the main page, so this picks
|
||||
* up the mode change when pairing/flashing returns here.
|
||||
*/
|
||||
get glassesMenuItemsVisibility(): "visible" | "collapse" {
|
||||
return isPreviewOnlyMode() ? "collapse" : "visible";
|
||||
}
|
||||
|
||||
/**
|
||||
* Live scan that names each nearby pair by model, colour, and serial and
|
||||
* checks both arms belong together. A connected arm stops advertising, so
|
||||
@@ -911,9 +917,18 @@ export class MainViewModel extends Observable {
|
||||
* state; pairing is a detour, not a Disconnect, so lift the suppression
|
||||
* right away — nothing dials the glasses until the main page's autoConnect
|
||||
* runs again on the way back.
|
||||
*
|
||||
* In preview-only mode there are no glasses yet, so pairing is really the
|
||||
* rest of onboarding: re-enter that chain at its "Disconnect Other Apps"
|
||||
* step, which leads to the scan, the firmware check, and flashing. The
|
||||
* chain's Back buttons pop history, so its first page returns here.
|
||||
*/
|
||||
async onPairGlassesTap(): Promise<void> {
|
||||
if (!this.canRun) return;
|
||||
if (isPreviewOnlyMode()) {
|
||||
Frame.topmost()?.navigate({ moduleName: "phone-ui/onboarding-unpair-page" });
|
||||
return;
|
||||
}
|
||||
if (this.phase === "connected" || this.phase === "charging" || this.phase === "connecting") {
|
||||
try {
|
||||
await dashboardController.disconnect();
|
||||
@@ -1002,8 +1017,15 @@ export class MainViewModel extends Observable {
|
||||
// Each pad therefore defers its double-click until the finger-up (the touch
|
||||
// handler) and converts the deferred pair into the G2 tap-then-hold gesture
|
||||
// when a longPress lands first.
|
||||
//
|
||||
// A hold is two events, like the hardware's: longPress sends the press
|
||||
// (long-press-start, which the controller delivers as a plain long-press)
|
||||
// and the finger-up sends the release, so a hold really holds (the
|
||||
// Glanceboard stays up until the finger lifts). The firmware sends the
|
||||
// same release after a tap-then-hold, so that pair gets one too.
|
||||
|
||||
private ringPadDoubleTapPending = false;
|
||||
private ringPadHeld = false;
|
||||
|
||||
async onRingPadTap(): Promise<void> {
|
||||
await dashboardController.injectSyntheticRingInput("click");
|
||||
@@ -1014,8 +1036,9 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
|
||||
async onRingPadLongPress(): Promise<void> {
|
||||
const kind = this.ringPadDoubleTapPending ? "short-then-long-press" : "long-press";
|
||||
const kind = this.ringPadDoubleTapPending ? "short-then-long-press" : "long-press-start";
|
||||
this.ringPadDoubleTapPending = false;
|
||||
this.ringPadHeld = true;
|
||||
await dashboardController.injectSyntheticRingInput(kind);
|
||||
}
|
||||
|
||||
@@ -1023,6 +1046,11 @@ export class MainViewModel extends Observable {
|
||||
if (args.action !== "up" && args.action !== "cancel") return;
|
||||
const pending = this.ringPadDoubleTapPending;
|
||||
this.ringPadDoubleTapPending = false;
|
||||
if (this.ringPadHeld) {
|
||||
this.ringPadHeld = false;
|
||||
await dashboardController.injectSyntheticRingInput("long-press-release");
|
||||
return;
|
||||
}
|
||||
if (args.action === "up" && pending) {
|
||||
await dashboardController.injectSyntheticRingInput("double-click");
|
||||
}
|
||||
@@ -1049,8 +1077,9 @@ export class MainViewModel extends Observable {
|
||||
|
||||
private padTwoFingerDown = false;
|
||||
// See the ring pad above: defers the double-click so a longPress can turn
|
||||
// the pair into tap-then-hold.
|
||||
// the pair into tap-then-hold, and pairs every hold with a release.
|
||||
private padDoubleTapPending = false;
|
||||
private padHeld = false;
|
||||
|
||||
/** What the next gesture lands on, as the watch pad shows it. */
|
||||
get padFocusLine(): string {
|
||||
@@ -1072,8 +1101,10 @@ export class MainViewModel extends Observable {
|
||||
}
|
||||
|
||||
async onPadLongPress(): Promise<void> {
|
||||
const kind = this.padDoubleTapPending ? "short-then-long-press" : "long-press";
|
||||
if (this.padTwoFingerDown) return;
|
||||
const kind = this.padDoubleTapPending ? "short-then-long-press" : "long-press-start";
|
||||
this.padDoubleTapPending = false;
|
||||
this.padHeld = true;
|
||||
await dashboardController.injectSyntheticRingInput(kind, "watch");
|
||||
this.refreshPadFocusLine();
|
||||
}
|
||||
@@ -1093,6 +1124,12 @@ export class MainViewModel extends Observable {
|
||||
if (args.action === "up" || args.action === "cancel") {
|
||||
const pendingDouble = this.padDoubleTapPending;
|
||||
this.padDoubleTapPending = false;
|
||||
if (this.padHeld) {
|
||||
this.padHeld = false;
|
||||
await dashboardController.injectSyntheticRingInput("long-press-release", "watch");
|
||||
this.refreshPadFocusLine();
|
||||
return;
|
||||
}
|
||||
const twoFinger = this.padTwoFingerDown;
|
||||
if (twoFinger) {
|
||||
// Let the single-tap recognizer's delayed tap see the flag first.
|
||||
|
||||
@@ -7,12 +7,19 @@ import {
|
||||
hasExtractedEvenHubFonts,
|
||||
type FirmwareProgress,
|
||||
} from "../g2/firmware-builder";
|
||||
import { classifyOnboardingFirmware, FLASHABLE_STOCK_VERSION_TEXT, type OnboardingFirmwareKind } from "../g2/firmware-compat";
|
||||
import {
|
||||
BASE_STOCK_VERSION_TEXT,
|
||||
REQUIRED_FACECLAW_FIRMWARE_VERSION,
|
||||
classifyOnboardingFirmware,
|
||||
describeFirmwareExtension,
|
||||
type FirmwareExtension,
|
||||
type OnboardingFirmwareKind,
|
||||
} from "../g2/firmware-compat";
|
||||
import { DeviceInfoProbe, DeviceInfoState } from "../native/device-info-probe";
|
||||
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
|
||||
type CheckPhase = "checking" | "fonts" | "custom" | "flashable" | "newer" | "error";
|
||||
type CheckPhase = "checking" | "fonts" | "custom" | "flashable" | "newer-validated" | "newer" | "error";
|
||||
|
||||
export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
private _phase: CheckPhase = "checking";
|
||||
@@ -72,6 +79,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
case "custom":
|
||||
return "Finish";
|
||||
case "flashable":
|
||||
case "newer-validated":
|
||||
return "Install Firmware";
|
||||
case "newer":
|
||||
return "Proceed Anyway";
|
||||
@@ -107,6 +115,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.finish();
|
||||
return;
|
||||
case "flashable":
|
||||
case "newer-validated":
|
||||
case "newer":
|
||||
this.goToFlashing();
|
||||
return;
|
||||
@@ -143,7 +152,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
if (this._phase !== "error") return;
|
||||
// Same path as a real "custom" classification, so the phone-side G2 fonts
|
||||
// still get extracted when they're missing.
|
||||
this.applyClassification("custom", "", "");
|
||||
this.applyClassification("custom", "", { kind: "none" });
|
||||
}
|
||||
|
||||
// --- probe flow ------------------------------------------------------------
|
||||
@@ -169,35 +178,39 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.disposeProbe();
|
||||
const probe = new DeviceInfoProbe(stored.right, stored.left);
|
||||
this.probeInstance = probe;
|
||||
probe.onStateChange((state) => this.reportProbeState(state));
|
||||
probe.onStateChange((state, detail) => this.reportProbeState(state, detail));
|
||||
|
||||
const info = await probe.run();
|
||||
this.probeInstance = null;
|
||||
|
||||
const { kind, version } = classifyOnboardingFirmware(info);
|
||||
this.applyClassification(kind, version, info.capabilities.trim());
|
||||
const { kind, version, extension } = classifyOnboardingFirmware(info);
|
||||
this.applyClassification(kind, version, extension);
|
||||
} catch (error) {
|
||||
this.probeInstance = null;
|
||||
this.toError(this.formatError(error));
|
||||
}
|
||||
}
|
||||
|
||||
private reportProbeState(state: DeviceInfoState): void {
|
||||
private reportProbeState(state: DeviceInfoState, detail: string): void {
|
||||
// The probe brings up the right lens, then the left, and pairs with each
|
||||
// in turn (they are separate Bluetooth devices with separate bonds).
|
||||
const lens = detail === "left" || detail === "right" ? `the ${detail} lens` : "your glasses";
|
||||
if (state === "connecting") {
|
||||
this.status = "Connecting to your glasses...";
|
||||
this.status = `Connecting to ${lens}...`;
|
||||
} else if (state === "authenticating") {
|
||||
// First-time connections pair here; the OS may show a Bluetooth dialog.
|
||||
this.status = "Authenticating with your glasses... If Android asks to pair, tap Pair.";
|
||||
this.status = `Pairing with ${lens}... Each lens pairs separately; if Android asks to pair, tap Pair.`;
|
||||
} else if (state === "querying") {
|
||||
this.status = "Reading the firmware version...";
|
||||
}
|
||||
}
|
||||
|
||||
private applyClassification(kind: OnboardingFirmwareKind, version: string, capabilities: string): void {
|
||||
private applyClassification(kind: OnboardingFirmwareKind, version: string, extension: FirmwareExtension): void {
|
||||
this.busy = false;
|
||||
// `kind` is classifyOnboardingFirmware's OnboardingFirmwareKind ("custom" |
|
||||
// "flashable-stock" | "newer-stock" | "unknown") -- NOT CheckPhase's shorter
|
||||
// "flashable"/"newer" (used below via setPhase for _phase/primaryLabel/etc).
|
||||
// "older-faceclaw" | "other-custom" | "flashable-stock" | "newer-stock-unvalidated" |
|
||||
// "newer-stock-validated" | "unknown") -- NOT CheckPhase's shorter
|
||||
// "flashable"/"newer-validated"/"newer" (used below via setPhase for _phase/primaryLabel/etc).
|
||||
// The two types share two of four names, so a typo'd case label here type-checks
|
||||
// fine as long as `kind` stays plain `string`, but silently falls through to
|
||||
// `default` (the hard "couldn't read a firmware version" error) for every real
|
||||
@@ -206,10 +219,26 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
switch (kind) {
|
||||
case "custom":
|
||||
if (!hasExtractedEvenHubFonts()) {
|
||||
void this.extractFontsForCustomFirmware(version, capabilities);
|
||||
void this.extractFontsForCustomFirmware(version, extension);
|
||||
break;
|
||||
}
|
||||
this.showCustomReady(version, capabilities, false);
|
||||
this.showCustomReady(version, extension, false);
|
||||
break;
|
||||
case "older-faceclaw":
|
||||
this.setPhase("flashable");
|
||||
this.headline = "Firmware Update Available";
|
||||
this.status =
|
||||
`Your glasses run ${describeFirmwareExtension(extension)}${version ? `, based on stock ${version}` : ""}. ` +
|
||||
`This version of Faceclaw needs revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}. ` +
|
||||
"Tap Install Firmware to update it.";
|
||||
break;
|
||||
case "other-custom":
|
||||
this.setPhase("flashable");
|
||||
this.headline = "Other Custom Firmware";
|
||||
this.status =
|
||||
`Your glasses run ${describeFirmwareExtension(extension)}${version ? `, based on stock ${version}` : ""}. ` +
|
||||
`Faceclaw needs its own custom firmware (revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}). ` +
|
||||
"Tap Install Firmware to replace the current firmware with it.";
|
||||
break;
|
||||
case "flashable-stock":
|
||||
this.setPhase("flashable");
|
||||
@@ -218,11 +247,21 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
`Your glasses run stock firmware ${version}. This is compatible — tap Install Firmware to flash ` +
|
||||
"Faceclaw's custom firmware.";
|
||||
break;
|
||||
case "newer-stock":
|
||||
case "newer-stock-validated":
|
||||
this.setPhase("newer-validated");
|
||||
this.headline = "Ready to Install";
|
||||
this.status =
|
||||
`Your glasses run stock firmware ${version}, which is compatible. This version is newer than the ` +
|
||||
`${BASE_STOCK_VERSION_TEXT} release Faceclaw's custom image is built from. You may not be able to ` +
|
||||
`use the official Even app without upgrading back to ${version} first (the official Even app will ` +
|
||||
`perform that upgrade for you if you re-pair it).`;
|
||||
break;
|
||||
|
||||
case "newer-stock-unvalidated":
|
||||
this.setPhase("newer");
|
||||
this.headline = "Unrecognized Firmware";
|
||||
this.status =
|
||||
`Your glasses run stock firmware ${version}, which is newer than the ${FLASHABLE_STOCK_VERSION_TEXT} ` +
|
||||
`Your glasses run stock firmware ${version}, which is newer than the ${BASE_STOCK_VERSION_TEXT} ` +
|
||||
"release Faceclaw's custom image is built from. Flashing may not work correctly and carries extra risk. " +
|
||||
"You can proceed anyway, or go back.";
|
||||
break;
|
||||
@@ -236,7 +275,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
private async extractFontsForCustomFirmware(version: string, capabilities: string): Promise<void> {
|
||||
private async extractFontsForCustomFirmware(version: string, extension: FirmwareExtension): Promise<void> {
|
||||
this.setPhase("fonts");
|
||||
this.headline = "Preparing G2 Fonts";
|
||||
this.busy = true;
|
||||
@@ -245,7 +284,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
"Downloading the official firmware to extract them; your glasses will not be reflashed.";
|
||||
try {
|
||||
await downloadAndExtractEvenHubFonts((progress) => this.reportFontProgress(progress));
|
||||
this.showCustomReady(version, capabilities, true);
|
||||
this.showCustomReady(version, extension, true);
|
||||
} catch (error) {
|
||||
this.toError(
|
||||
`Custom firmware is installed, but the G2 fonts could not be prepared: ${this.formatError(error)}`,
|
||||
@@ -270,13 +309,16 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
private showCustomReady(version: string, capabilities: string, extractedFonts: boolean): void {
|
||||
private showCustomReady(version: string, extension: FirmwareExtension, extractedFonts: boolean): void {
|
||||
this.busy = false;
|
||||
this.setPhase("custom");
|
||||
this.headline = extractedFonts ? "Fonts Ready" : "Custom Firmware Detected";
|
||||
const details = [
|
||||
extension.kind === "faceclaw" ? `revision ${extension.version}` : "",
|
||||
version ? `based on stock ${version}` : "",
|
||||
].filter(Boolean);
|
||||
this.status =
|
||||
`Your glasses already run Faceclaw's custom firmware${version ? ` (version ${version})` : ""}` +
|
||||
`${capabilities ? `, extensions: ${capabilities}` : ""}. ` +
|
||||
`Your glasses already run Faceclaw's custom firmware${details.length ? ` (${details.join(", ")})` : ""}. ` +
|
||||
(extractedFonts
|
||||
? "The phone-side G2 fonts were extracted successfully. No flashing was needed — you're all set."
|
||||
: "The phone-side G2 fonts are present. No flashing needed — you're all set.");
|
||||
@@ -288,7 +330,9 @@ export class OnboardingFirmwareCheckViewModel extends Observable {
|
||||
this.disposeProbe();
|
||||
Frame.topmost()?.navigate({
|
||||
moduleName: "phone-ui/onboarding-flash-page",
|
||||
context: { mode: "install", fromOnboarding: true },
|
||||
// This page already described the install; skip the flash page's
|
||||
// intro/"Connect & Confirm" step and connect immediately.
|
||||
context: { mode: "install", fromOnboarding: true, autoStart: true },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ import { FlashMode, OnboardingFlashViewModel } from "./onboarding-flash-view-mod
|
||||
export function navigatingTo(args: NavigatedData): void {
|
||||
const page = args.object as Page;
|
||||
if (!page.bindingContext) {
|
||||
const context = (args.context as { mode?: FlashMode; fromOnboarding?: boolean } | undefined) ?? undefined;
|
||||
const context =
|
||||
(args.context as { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean } | undefined) ?? undefined;
|
||||
page.bindingContext = new OnboardingFlashViewModel({
|
||||
mode: context?.mode ?? "install",
|
||||
fromOnboarding: context?.fromOnboarding ?? true,
|
||||
autoStart: context?.autoStart ?? false,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import { resumeAutoReconnect, suppressAutoReconnect } from "../g2/reconnect-poli
|
||||
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
||||
import { formatErrorMessage } from "../util/format-error";
|
||||
|
||||
type FlashPhase = "intro" | "prompt" | "building" | "ready" | "flashing" | "flashed" | "error";
|
||||
type FlashPhase = "intro" | "prompt" | "building" | "flashing" | "flashed" | "error";
|
||||
|
||||
export type FlashMode = "install" | "uninstall";
|
||||
|
||||
@@ -53,7 +53,7 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
private flasherUnsubscribers: Array<() => void> = [];
|
||||
private retryAction: () => void = () => this.beginPrompt();
|
||||
|
||||
constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean }) {
|
||||
constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean }) {
|
||||
super();
|
||||
// The flash flow needs the glasses to itself: from here on the main
|
||||
// page must not auto-reconnect, until an install succeeds (below) or
|
||||
@@ -68,13 +68,19 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
"and reflashes the official firmware — removing Faceclaw's custom features."
|
||||
: "This connects to your glasses, asks for confirmation on the lens, checks the battery, then downloads, " +
|
||||
"verifies, and flashes Faceclaw's custom firmware.";
|
||||
if (options?.autoStart) {
|
||||
// Entered from a page that already explained what's about to happen
|
||||
// (the onboarding firmware check), so the intro/"Connect & Confirm"
|
||||
// step would be redundant: go straight to connecting.
|
||||
void this.beginPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
// Kept short to fit the glasses' ~50-column text grid.
|
||||
private get glassesWarning(): string {
|
||||
return this.mode === "uninstall"
|
||||
? "Reinstalling the official firmware removes Faceclaw's custom features. Continue?"
|
||||
: "Flashing custom firmware will void your warranty and carries some risk of bricking the glasses. Continue?";
|
||||
: "Flashing custom firmware will void your warranty. Continue?";
|
||||
}
|
||||
|
||||
private get noun(): string {
|
||||
@@ -161,8 +167,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
switch (this._phase) {
|
||||
case "intro":
|
||||
return "Connect & Confirm";
|
||||
case "ready":
|
||||
return "Flash Now";
|
||||
case "flashed":
|
||||
return "Finish";
|
||||
case "error":
|
||||
@@ -176,8 +180,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
switch (this._phase) {
|
||||
case "prompt":
|
||||
return "Cancel";
|
||||
case "ready":
|
||||
return "Not Now";
|
||||
default:
|
||||
return "Back";
|
||||
}
|
||||
@@ -201,9 +203,6 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
case "intro":
|
||||
void this.beginPrompt();
|
||||
return;
|
||||
case "ready":
|
||||
this.startFlashing();
|
||||
return;
|
||||
case "flashed":
|
||||
this.finish();
|
||||
return;
|
||||
@@ -330,7 +329,7 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
this.disposePrompt();
|
||||
if (!approved) {
|
||||
this.busy = false;
|
||||
this.toError("You declined on the glasses. No firmware was written.", () => this.beginPrompt());
|
||||
this.toError("You declined on the glasses. Firmware was not installed.", () => this.beginPrompt());
|
||||
return;
|
||||
}
|
||||
const lowBattery = this.describeLowBattery(this.promptBattery);
|
||||
@@ -392,15 +391,9 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
const build = this.mode === "uninstall" ? buildStockFirmware : buildCustomFirmware;
|
||||
const result = await build((progress) => this.reportBuildProgress(progress));
|
||||
this.firmwarePath = result.path;
|
||||
this.busy = false;
|
||||
this.setPhase("ready");
|
||||
this.headline = "Firmware Ready";
|
||||
this.status =
|
||||
`The ${this.noun} is prepared and verified (${result.bytes.toLocaleString()} bytes).\n\n` +
|
||||
"Tap Flash Now to write it to your glasses. Keep both lenses powered on and nearby — " +
|
||||
"each lens takes a few minutes, and the glasses will reboot when each lens finishes. " +
|
||||
"Do not close the app during flashing.";
|
||||
this.appendLog(`saved to ${result.path}`);
|
||||
this.appendLog(`${this.noun} prepared and verified (${result.bytes.toLocaleString()} bytes), saved to ${result.path}`);
|
||||
// The user already said Yes on the lens; no second confirmation here.
|
||||
this.startFlashing();
|
||||
} catch (error) {
|
||||
this.busy = false;
|
||||
const message =
|
||||
@@ -445,7 +438,9 @@ export class OnboardingFlashViewModel extends Observable {
|
||||
this.headline = "Flashing Firmware";
|
||||
this.busy = true;
|
||||
this.progress = 0;
|
||||
this.status = "Starting. Do not close the app or power off the glasses.";
|
||||
this.status =
|
||||
"Starting. Keep both lenses powered on and nearby — each lens takes a few minutes, " +
|
||||
"and the glasses will reboot when each lens finishes. Do not close the app during flashing.";
|
||||
|
||||
this.disposeFlasher();
|
||||
const flasher = new FirmwareFlasher(this.addresses, this.firmwarePath);
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
<StackLayout>
|
||||
<Label text="Disconnect Other Apps" class="h1 text-center m-t-24" textWrap="true" />
|
||||
<Label
|
||||
text="Faceclaw and the official Even Realities app can't both be connected to the glasses at once. Before continuing, open the Even app, go to Home, select your glasses, open the Connection submenu, and press Disconnect (or turn off the Even app's Bluetooth). If any other glasses app is installed (such as Mentra), disconnect it too."
|
||||
text="Your glasses can only be connected to one app or phone at a time. If they're connected to another app (such as Even Realities or MentraOS) or another phone, disconnect them there before continuing. If nothing else is connected, you can continue."
|
||||
class="body-text m-t-24" textWrap="true" />
|
||||
<Button text="Open Even App Settings" tap="{{ onOpenEvenAppSettingsTap }}" width="{{ settingsButtonWidth }}" horizontalAlignment="center" class="-primary m-t-24" />
|
||||
<Label
|
||||
text="If you use the Even Realities app, go to Home, select your glasses, open the Connection submenu, and press Disconnect (or disable the Even app's "nearby devices" permission)."
|
||||
class="body-text m-t-16" textWrap="true" />
|
||||
<Button text="Open Even App Settings" tap="{{ onOpenEvenAppSettingsTap }}" visibility="{{ settingsButtonVisibility }}" width="{{ settingsButtonWidth }}" horizontalAlignment="center" class="-primary m-t-24" />
|
||||
</StackLayout>
|
||||
</ScrollView>
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ import { Frame, Observable, Screen } from "@nativescript/core";
|
||||
import { openEvenAppSettings } from "../native/even-app-conflict";
|
||||
|
||||
export class OnboardingUnpairViewModel extends Observable {
|
||||
get settingsButtonVisibility(): "visible" | "collapsed" {
|
||||
return global.isAndroid ? "visible" : "collapsed";
|
||||
}
|
||||
|
||||
onOpenEvenAppSettingsTap(): void {
|
||||
openEvenAppSettings();
|
||||
}
|
||||
|
||||
@@ -28,8 +28,7 @@ const STEP_CONTENT: Record<OnboardingStep, StepContent> = {
|
||||
2: {
|
||||
headline: "Before You Continue",
|
||||
tagline: "",
|
||||
body:
|
||||
"This unofficial software provides a custom user interface and functionality for the Even Realities G2 smart glasses. It is not created or supported by Even Realities. If this software somehow breaks my headset, this is not Even's fault and is not covered by the hardware's warranty. If this software doesn't break my headset, using this software may void the hardware's warranty anyways, at the sole discretion of Even Realities. This software is a development prototype and may not be relied on for anything important. This software may be broken at any time by software or firmware updates created by Even, and this will not be Even's fault.",
|
||||
body: "This software provides a custom user interface and functionality for the Even Realities G2 smart glasses. It is not created or supported by Even Realities. If this software somehow breaks your headset, this is not Even's fault and is not covered by the hardware's warranty. If this software doesn't break you headset, using this software may void the hardware's warranty anyways, at the sole discretion of Even Realities. This software is beta; don't rely on it for anything critically important.",
|
||||
primaryLabel: "Agree",
|
||||
secondaryLabel: "Back",
|
||||
splash: false,
|
||||
@@ -38,8 +37,7 @@ const STEP_CONTENT: Record<OnboardingStep, StepContent> = {
|
||||
3: {
|
||||
headline: "Custom Firmware Required",
|
||||
tagline: "",
|
||||
body:
|
||||
"Faceclaw only runs on Even Realities G2 glasses that have Faceclaw's custom firmware installed. You have two choices:\n\n• Preview Only — explore Faceclaw's interface on your phone's screen without pairing any glasses. Nothing is written to a headset.\n\n• Flash Firmware — install the custom firmware on your glasses now, then use Faceclaw for real. Faceclaw scans for nearby glasses so you can pick yours by model, colour, and serial, then connects, asks for confirmation on the lens, and downloads and prepares the firmware.\n\nFlashing replaces the official firmware. It may void your warranty and, like any firmware update, carries a risk of bricking the device. The glasses can only be connected to one app at a time, so the next step will walk you through disconnecting the official Even app (and any other glasses apps) first.",
|
||||
body: "Faceclaw only runs on Even Realities G2 glasses that have Faceclaw's custom firmware installed. You have two choices:\n\n• Preview Only — explore Faceclaw's interface on your phone's screen without pairing any glasses. Nothing is written to a headset.\n\n• Flash Firmware — install the custom firmware on your glasses now, then use Faceclaw for real.\n\nFlashing replaces the official firmware. You can uninstall and return to the official firmware later, by reconnecting the official Even Realities app. Installing custom firmware is not covered by warranty.",
|
||||
primaryLabel: "Flash Firmware",
|
||||
secondaryLabel: "Preview Only",
|
||||
splash: false,
|
||||
|
||||
@@ -171,9 +171,9 @@ export class PairingViewModel extends Observable {
|
||||
|
||||
get instructions(): string {
|
||||
return (
|
||||
"Take the glasses out of their case (or open the lid) and keep them next to the phone. " +
|
||||
"Each arm advertises its serial; a pair is ready when both arms report the same one. " +
|
||||
"An arm connected to the official Even app stops advertising — disconnect it there first."
|
||||
"Select your G2 glasses and R1 ring below. Wear the glasses or take them out of the case, " +
|
||||
"and keep the glasses and phone nearby. If your glasses are not detected, make sure no other " +
|
||||
"apps are connected."
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Clock/date text shared by the shell's top bar and the Glanceboard's System
|
||||
* Card, so the two never disagree on the time-format setting or the date
|
||||
* spelling.
|
||||
*/
|
||||
import { timeFormatSetting } from "./dashboard-settings";
|
||||
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
/** Format a clock time, honoring the 12/24-hour setting ("14:05" / "2:05 PM"). */
|
||||
export function formatClockTime(now: Date): string {
|
||||
const minutes = String(now.getMinutes()).padStart(2, "0");
|
||||
const hour24 = now.getHours();
|
||||
if (timeFormatSetting.get() === "12h") {
|
||||
const hour12 = ((hour24 + 11) % 12) + 1;
|
||||
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
||||
}
|
||||
return `${hour24}:${minutes}`;
|
||||
}
|
||||
|
||||
/** Short date as the top bar shows it: "Thu 11 Sep". */
|
||||
export function formatClockDate(now: Date): string {
|
||||
return `${WEEKDAYS[now.getDay()]} ${now.getDate()} ${MONTHS[now.getMonth()]}`;
|
||||
}
|
||||
@@ -25,7 +25,9 @@ export type NightscoutSettings = {
|
||||
siteUrl: string;
|
||||
apiToken: string;
|
||||
};
|
||||
export type BatteryDisplayMode = "icon" | "percentage";
|
||||
export type BatteryDisplayMode = "icon" | "percentage" | "stacked" | "stacked-percentage";
|
||||
/** When a top-bar battery indicator is shown: always, only below 50%, or never. */
|
||||
export type BatteryIndicatorVisibility = "always" | "low" | "never";
|
||||
export type TimeFormat = "24h" | "12h";
|
||||
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "3m" | "never";
|
||||
// "auto" lets the glasses' ambient-light sensor drive brightness; the numeric
|
||||
@@ -211,14 +213,68 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
|
||||
|
||||
export const batteryDisplayModeSetting = new ConfigSettingEnum<BatteryDisplayMode>({
|
||||
id: "batteryDisplayMode",
|
||||
label: "Battery display",
|
||||
label: "Style",
|
||||
storageKey: "dashboard.systemCard.batteryDisplayMode",
|
||||
defaultValue: "icon",
|
||||
values: ["icon", "percentage"],
|
||||
defaultValue: "stacked",
|
||||
values: ["icon", "percentage", "stacked", "stacked-percentage"],
|
||||
formatValue: batteryDisplayModeLabel,
|
||||
description: "How the top bar shows the phone and glasses battery levels: a small gauge icon or an exact percentage.",
|
||||
description: "How the top bar shows battery levels: a gauge icon or exact percentage beside the label, or a compact gauge or percentage with the label stacked above it.",
|
||||
});
|
||||
|
||||
/** Below this charge level a "Below 50%" indicator becomes visible. */
|
||||
export const BATTERY_LOW_VISIBILITY_THRESHOLD = 50;
|
||||
|
||||
function batteryVisibilitySetting(
|
||||
id: string,
|
||||
device: string,
|
||||
storageKey: string,
|
||||
): ConfigSettingEnum<BatteryIndicatorVisibility> {
|
||||
return new ConfigSettingEnum<BatteryIndicatorVisibility>({
|
||||
id,
|
||||
label: device,
|
||||
storageKey,
|
||||
defaultValue: "always",
|
||||
values: ["always", "low", "never"],
|
||||
formatValue: batteryIndicatorVisibilityLabel,
|
||||
description: `When the top bar shows the ${device} battery: always, only once it drops below ${BATTERY_LOW_VISIBILITY_THRESHOLD}%, or never.`,
|
||||
});
|
||||
}
|
||||
|
||||
export const phoneBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"phoneBatteryVisibility", "Phone", "display.battery.phoneVisibility",
|
||||
);
|
||||
export const glassesBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"glassesBatteryVisibility", "G2", "display.battery.glassesVisibility",
|
||||
);
|
||||
export const ringBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"ringBatteryVisibility", "R1", "display.battery.ringVisibility",
|
||||
);
|
||||
/** The Wear OS watch; the indicator only exists while a watch is reachable. */
|
||||
export const watchBatteryVisibilitySetting = batteryVisibilitySetting(
|
||||
"watchBatteryVisibility", "Watch", "display.battery.watchVisibility",
|
||||
);
|
||||
|
||||
/** Whether an indicator with this visibility setting shows at the given charge. */
|
||||
export function batteryIndicatorVisible(visibility: BatteryIndicatorVisibility, percent: number): boolean {
|
||||
if (visibility === "never") return false;
|
||||
if (visibility === "always") return true;
|
||||
return percent < BATTERY_LOW_VISIBILITY_THRESHOLD;
|
||||
}
|
||||
|
||||
/**
|
||||
* One string summarizing every setting the top-bar battery block reads, so
|
||||
* the shell can cheaply tell whether a settings change needs a repaint.
|
||||
*/
|
||||
export function batteryIndicatorSettingsKey(): string {
|
||||
return [
|
||||
batteryDisplayModeSetting.get(),
|
||||
phoneBatteryVisibilitySetting.get(),
|
||||
glassesBatteryVisibilitySetting.get(),
|
||||
ringBatteryVisibilitySetting.get(),
|
||||
watchBatteryVisibilitySetting.get(),
|
||||
].join("|");
|
||||
}
|
||||
|
||||
export const timeFormatSetting = new ConfigSettingEnum<TimeFormat>({
|
||||
id: "timeFormat",
|
||||
label: "Time format",
|
||||
@@ -447,7 +503,7 @@ export const useMicControlSetting = new ConfigSettingBoolean({
|
||||
storageKey: "developer.useMicControl",
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Use the custom firmware's per-temple mic-control channel (caps token micctl) for the Microphones app's array capture. When off, behave as if the firmware doesn't have the feature and use the standard single mixed stream.",
|
||||
"Use the custom firmware's per-temple mic-control channel for the Microphones app's array capture. When off, use the standard single mixed stream.",
|
||||
});
|
||||
|
||||
export const showBleBandwidthSetting = new ConfigSettingBoolean({
|
||||
@@ -472,12 +528,19 @@ export const ringConnectionModeSetting = new ConfigSettingEnum<RingConnectionMod
|
||||
"How R1 ring input reaches the phone. Only via glasses: the ring's own link to the glasses carries its gestures, and the phone never opens a Bluetooth connection to the ring. Direct: also connect to the ring from the phone (currently unreliable). Takes effect on the next connection to the glasses.",
|
||||
});
|
||||
|
||||
export type VoiceProvider = "onboard" | "elevenlabs" | "whisper" | "soniox";
|
||||
// "whisper" (no "onboard-" prefix) is OpenAI's CLOUD realtime model
|
||||
// (gpt-realtime-whisper); "onboard-whisper" is the on-device sherpa-onnx
|
||||
// Whisper backend. Same underlying model family, two different places it
|
||||
// runs -- see the same note in native/voice-control.ts. The "whisper" value
|
||||
// keeps its name (it's a persisted setting on real installs) but its label
|
||||
// below now says "OpenAI" to tell the two apart in the picker.
|
||||
export type VoiceProvider = "onboard" | "onboard-whisper" | "elevenlabs" | "whisper" | "soniox";
|
||||
|
||||
const voiceProviderLabels: Record<VoiceProvider, string> = {
|
||||
onboard: "On-device",
|
||||
onboard: "On-device (Moonshine)",
|
||||
"onboard-whisper": "On-device (Whisper)",
|
||||
elevenlabs: "ElevenLabs",
|
||||
whisper: "Whisper",
|
||||
whisper: "OpenAI (Whisper)",
|
||||
soniox: "Soniox",
|
||||
};
|
||||
|
||||
@@ -486,7 +549,7 @@ export const voiceProviderSetting = new ConfigSettingEnum<VoiceProvider>({
|
||||
label: "Transcription Provider",
|
||||
storageKey: "voice.provider",
|
||||
defaultValue: "onboard",
|
||||
values: ["onboard", "elevenlabs", "whisper", "soniox"],
|
||||
values: ["onboard", "onboard-whisper", "elevenlabs", "whisper", "soniox"],
|
||||
formatValue: (value) => voiceProviderLabels[value] ?? value,
|
||||
isDisabled: (value) => {
|
||||
if (value === "elevenlabs") return elevenLabsApiKeySetting.get().trim().length === 0;
|
||||
@@ -494,7 +557,7 @@ export const voiceProviderSetting = new ConfigSettingEnum<VoiceProvider>({
|
||||
if (value === "soniox") return sonioxApiKeySetting.get().trim().length === 0;
|
||||
return false;
|
||||
},
|
||||
description: "Speech-to-text engine for voice input. ElevenLabs, Whisper, and Soniox are cloud services that need an API key, with significantly better accuracy than on-device transcription. On-device transcription needs the voice model downloaded (below).",
|
||||
description: "Speech-to-text engine for voice input. ElevenLabs, OpenAI, and Soniox are cloud services that need an API key, with significantly better accuracy than on-device transcription. The two On-device options need their voice model downloaded (below) and never leave the phone.",
|
||||
});
|
||||
|
||||
const wakeWordActionLabels: Record<WakeWordAction, string> = {
|
||||
@@ -934,7 +997,16 @@ export function screenTimeoutLabel(value: ScreenTimeoutSetting): string {
|
||||
}
|
||||
|
||||
export function batteryDisplayModeLabel(value: BatteryDisplayMode): string {
|
||||
return value === "icon" ? "Icon" : "Percentage";
|
||||
if (value === "percentage") return "Percentage";
|
||||
if (value === "stacked") return "Stacked";
|
||||
if (value === "stacked-percentage") return "Stacked percentage";
|
||||
return "Icon";
|
||||
}
|
||||
|
||||
export function batteryIndicatorVisibilityLabel(value: BatteryIndicatorVisibility): string {
|
||||
if (value === "never") return "Never";
|
||||
if (value === "low") return `Below ${BATTERY_LOW_VISIBILITY_THRESHOLD}%`;
|
||||
return "Always";
|
||||
}
|
||||
|
||||
export function timeFormatLabel(value: TimeFormat): string {
|
||||
|
||||
@@ -13,12 +13,13 @@ import {
|
||||
startLocalModelDownload,
|
||||
} from "../../native/llama";
|
||||
import {
|
||||
ASR_MODEL,
|
||||
ASR_MODELS,
|
||||
asrModelState,
|
||||
cancelAsrModelDownload,
|
||||
deleteAsrModel,
|
||||
onAsrModelStateChanged,
|
||||
startAsrModelDownload,
|
||||
type AsrModelId,
|
||||
} from "../../native/asr-model";
|
||||
import { TextViewerLayer } from "../../apps/files/text-viewer";
|
||||
import type { LayerContext } from "../layers";
|
||||
@@ -35,6 +36,10 @@ import {
|
||||
assistantSkipConfirmationSetting,
|
||||
batteryDisplayModeSetting,
|
||||
brightnessSetting,
|
||||
glassesBatteryVisibilitySetting,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
displayModeSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
@@ -75,7 +80,7 @@ import {
|
||||
} from "../dashboard-settings";
|
||||
import { clearRecentDestinations } from "../../apps/navigate/destinations";
|
||||
import { wearBridge } from "../../native/wear-bridge";
|
||||
import { SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
import { terminalFontPickerMenuItem, uiFontPickerMenuItem } from "../font-picker";
|
||||
|
||||
/** The Settings app's master-detail panel (sections on the left, contents on the right). */
|
||||
@@ -102,8 +107,8 @@ function settingsSections(): SettingsSection[] {
|
||||
enumSettingMenuItem(verticalPositionSetting),
|
||||
// Band / tall / full-panel; the dashboard controller reflows windows.
|
||||
enumSettingMenuItem(displayModeSetting),
|
||||
// Controls the top-bar battery indicators (icon vs percentage).
|
||||
enumSettingMenuItem(batteryDisplayModeSetting),
|
||||
// Submenu: top-bar battery indicator style plus per-device visibility.
|
||||
batteryIndicatorsMenuItem(),
|
||||
// Controls the top-bar clock (24-hour vs 12-hour).
|
||||
enumSettingMenuItem(timeFormatSetting),
|
||||
// Opens the modal font picker (face, weight, size) for UI text.
|
||||
@@ -115,7 +120,8 @@ function settingsSections(): SettingsSection[] {
|
||||
items: [
|
||||
enumSettingMenuItem(wakeWordActionSetting),
|
||||
enumSettingMenuItem(voiceProviderSetting),
|
||||
asrModelMenuItem(),
|
||||
asrModelMenuItem("moonshine"),
|
||||
asrModelMenuItem("whisper-base-en"),
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -253,6 +259,39 @@ function settingsSections(): SettingsSection[] {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The Display section's "Battery indicators" row: opens a modal submenu with
|
||||
* the style (icon / percentage / stacked) and, per device, when its
|
||||
* indicator is visible. The row itself shows the current style.
|
||||
*/
|
||||
function batteryIndicatorsMenuItem(): MenuItem {
|
||||
return {
|
||||
label: "Battery indicators",
|
||||
description:
|
||||
"Top-bar battery indicators for the phone, Wear OS watch, glasses, and ring: their style, and whether each shows always, only when low, or never.",
|
||||
onSelect: (ctx) => {
|
||||
openSettingsSubMenu(ctx, "Battery indicators", [
|
||||
enumSettingMenuItem(batteryDisplayModeSetting),
|
||||
enumSettingMenuItem(phoneBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(watchBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(glassesBatteryVisibilitySetting),
|
||||
enumSettingMenuItem(ringBatteryVisibilitySetting),
|
||||
]);
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(
|
||||
image,
|
||||
getDefaultSmallFont(),
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
"Battery indicators",
|
||||
batteryDisplayModeSetting.displayValue(),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const LOCAL_MODEL_GB = `${(LOCAL_MODEL.sizeBytes / 1e9).toFixed(1)}GB`;
|
||||
|
||||
// While a download is running, re-render on progress updates so the row's
|
||||
@@ -324,48 +363,52 @@ function localModelMenuItem(): MenuItem {
|
||||
};
|
||||
}
|
||||
|
||||
const ASR_MODEL_MB = `${Math.round(ASR_MODEL.totalBytes / 1e6)}MB`;
|
||||
function asrModelMb(id: AsrModelId): string {
|
||||
return `${Math.round(ASR_MODELS[id].totalBytes / 1e6)}MB`;
|
||||
}
|
||||
|
||||
let asrModelRenderUnsub: (() => void) | null = null;
|
||||
const asrModelRenderUnsub: Partial<Record<AsrModelId, () => void>> = {};
|
||||
|
||||
function watchAsrModelDownload(ctx: LayerContext): void {
|
||||
asrModelRenderUnsub?.();
|
||||
asrModelRenderUnsub = onAsrModelStateChanged((state) => {
|
||||
function watchAsrModelDownload(id: AsrModelId, ctx: LayerContext): void {
|
||||
asrModelRenderUnsub[id]?.();
|
||||
asrModelRenderUnsub[id] = onAsrModelStateChanged(id, (state) => {
|
||||
ctx.actions.requestRender();
|
||||
if (state.status !== "downloading") {
|
||||
asrModelRenderUnsub?.();
|
||||
asrModelRenderUnsub = null;
|
||||
asrModelRenderUnsub[id]?.();
|
||||
asrModelRenderUnsub[id] = undefined;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function asrModelStatusText(): string {
|
||||
const state = asrModelState();
|
||||
function asrModelStatusText(id: AsrModelId): string {
|
||||
const state = asrModelState(id);
|
||||
if (state.status === "ready") return "downloaded";
|
||||
if (state.status === "downloading") {
|
||||
const pct = state.totalBytes > 0 ? Math.floor((state.bytesDownloaded / state.totalBytes) * 100) : 0;
|
||||
return `${pct}% of ${ASR_MODEL_MB}`;
|
||||
return `${pct}% of ${asrModelMb(id)}`;
|
||||
}
|
||||
return "not downloaded";
|
||||
}
|
||||
|
||||
/** Download/cancel/delete management for the on-device transcription model. */
|
||||
function asrModelMenuItem(): MenuItem {
|
||||
/** Download/cancel/delete management for one on-device transcription model. */
|
||||
function asrModelMenuItem(id: AsrModelId): MenuItem {
|
||||
const def = ASR_MODELS[id];
|
||||
const rowLabel = `On-device model: ${def.label}`;
|
||||
return {
|
||||
label: "On-device voice model",
|
||||
label: rowLabel,
|
||||
description:
|
||||
`${ASR_MODEL.label} (${ASR_MODEL_MB} download). ` +
|
||||
`${def.label} (${asrModelMb(id)} download). ` +
|
||||
"Transcribes voice input on the phone itself, with no API key or cloud service. " +
|
||||
"Required for the On-device transcription provider; the cloud providers work without it. " +
|
||||
"Required for its matching Transcription Provider option; the other providers work without it. " +
|
||||
"An interrupted download resumes where it left off.",
|
||||
onSelect: (ctx) => {
|
||||
const state = asrModelState();
|
||||
const state = asrModelState(id);
|
||||
const action: MenuItem =
|
||||
state.status === "downloading"
|
||||
? {
|
||||
label: "Cancel download",
|
||||
onSelect: (innerCtx) => {
|
||||
cancelAsrModelDownload();
|
||||
cancelAsrModelDownload(id);
|
||||
innerCtx.stack.pop();
|
||||
},
|
||||
}
|
||||
@@ -373,22 +416,22 @@ function asrModelMenuItem(): MenuItem {
|
||||
? {
|
||||
label: "Delete model",
|
||||
onSelect: (innerCtx) => {
|
||||
deleteAsrModel();
|
||||
deleteAsrModel(id);
|
||||
innerCtx.stack.pop();
|
||||
},
|
||||
}
|
||||
: {
|
||||
label: `Download (${ASR_MODEL_MB})`,
|
||||
label: `Download (${asrModelMb(id)})`,
|
||||
onSelect: (innerCtx) => {
|
||||
startAsrModelDownload();
|
||||
watchAsrModelDownload(innerCtx);
|
||||
startAsrModelDownload(id);
|
||||
watchAsrModelDownload(id, innerCtx);
|
||||
innerCtx.stack.pop();
|
||||
},
|
||||
};
|
||||
openModalMenu(ctx, "On-device voice model", [action], 0);
|
||||
openModalMenu(ctx, rowLabel, [action], 0);
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, "On-device voice model", asrModelStatusText());
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, rowLabel, asrModelStatusText(id));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ export type LayerActions = {
|
||||
* (hands-free); otherwise it runs until stopVoiceCapture.
|
||||
*/
|
||||
startVoiceCapture: (endpointing?: boolean) => Promise<void> | void;
|
||||
/** Stop push-to-talk; for a cloud provider this also commits for a final result. */
|
||||
/** Stop push-to-talk; awaits native recognition, or commits a cloud provider for a final result. */
|
||||
stopVoiceCapture: () => Promise<void> | void;
|
||||
/** Start continuous capture (Transcribe); shares the mic with push-to-talk. */
|
||||
startContinuousVoiceCapture: () => Promise<void> | void;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { brightnessSetting, brightnessSettingToLevel, type BrightnessSetting } from "../dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, gestureHints, type InputEvent } from "../gestures";
|
||||
import type { Layer, LayerContext, PaintBelow } from "../layers";
|
||||
import { appViewportRect, SHELL_OPAQUE_BLACK } from "./geometry";
|
||||
|
||||
const DIALOG_WIDTH = 272;
|
||||
const BAR_WIDTH = 12;
|
||||
const BAR_HEIGHT = 100;
|
||||
|
||||
/** Manual brightness changes apply immediately so the wearer can judge the level. */
|
||||
export class BrightnessPickerLayer implements Layer {
|
||||
constructor(private readonly onClosed: () => void) {}
|
||||
|
||||
onRemoved(): void {
|
||||
this.onClosed();
|
||||
}
|
||||
|
||||
paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const image = paintBelow();
|
||||
const font = getDefaultSmallFont();
|
||||
const viewport = appViewportRect("min");
|
||||
const height = BAR_HEIGHT + 3 * font.lineHeight + 56;
|
||||
const left = viewport.x + Math.round((viewport.width - DIALOG_WIDTH) / 2);
|
||||
const top = viewport.y + Math.round((viewport.height - height) / 2);
|
||||
image.fillRoundedRect(left, top, DIALOG_WIDTH, height, SHELL_OPAQUE_BLACK, 8);
|
||||
image.drawRoundedRect(left, top, DIALOG_WIDTH, height, 110, 8);
|
||||
|
||||
const centeredText = (text: string, y: number, value: number) => {
|
||||
image.drawText(font, left + Math.round((DIALOG_WIDTH - font.measureText(text)) / 2), y, text, value);
|
||||
};
|
||||
centeredText("Brightness", top + 12, 235);
|
||||
const barX = left + Math.round((DIALOG_WIDTH - BAR_WIDTH) / 2);
|
||||
const barY = top + font.lineHeight + 24;
|
||||
const level = brightnessSettingToLevel(brightnessSetting.get());
|
||||
image.drawRect(barX - 2, barY - 2, BAR_WIDTH + 4, BAR_HEIGHT + 4, 150);
|
||||
const filledHeight = Math.round(BAR_HEIGHT * (level ?? 0) / 100);
|
||||
image.fillRect(barX, barY + BAR_HEIGHT - filledHeight, BAR_WIDTH, filledHeight, 255);
|
||||
image.drawText(font, barX + BAR_WIDTH + 12, barY + Math.round((BAR_HEIGHT - font.lineHeight) / 2),
|
||||
level === null ? "Auto" : `${level}%`, 235);
|
||||
centeredText(gestureHints([[GESTURE_SCROLL, "adjust"]]), barY + BAR_HEIGHT + 12, 150);
|
||||
centeredText(gestureHints([[`${GESTURE_CLICK} / ${GESTURE_DOUBLE_CLICK}`, "confirm"]]),
|
||||
barY + BAR_HEIGHT + font.lineHeight + 16, 150);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down": {
|
||||
const level = brightnessSettingToLevel(brightnessSetting.get());
|
||||
// Respect Auto if it was enabled on the phone while this dialog was open.
|
||||
if (level === null) return;
|
||||
const next = clamp(level + (event.type === "scroll-up" ? 10 : -10), 0, 100);
|
||||
if (next !== level) brightnessSetting.set(String(next) as BrightnessSetting);
|
||||
return;
|
||||
}
|
||||
case "click":
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
+116
-28
@@ -7,7 +7,17 @@ import { readActiveNotificationIcons } from "../../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../../native/phone-battery";
|
||||
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../util/render-freshness";
|
||||
import { renderIcon, renderIconWithGlyph, type IconActivity, type IconName } from "../../graphics/icons";
|
||||
import { batteryDisplayModeSetting, timeFormatSetting } from "../dashboard-settings";
|
||||
import {
|
||||
batteryDisplayModeSetting,
|
||||
batteryIndicatorVisible,
|
||||
glassesBatteryVisibilitySetting,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../dashboard-settings";
|
||||
import { formatClockDate, formatClockTime } from "../clock-format";
|
||||
import { getFont } from "../../graphics/bdffont";
|
||||
import { Layer } from "../layers";
|
||||
import { scrollToKeepSelectionVisible } from "../menu";
|
||||
import { lineStep } from "../metrics";
|
||||
@@ -83,8 +93,6 @@ export function sidebarContentLeft(windowCount: number): number {
|
||||
}
|
||||
const NOTIFICATION_ICON_SIZE = 24;
|
||||
const BORDER_VALUE = 40;
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
export type ShellChromeWindow = {
|
||||
windowId: string;
|
||||
@@ -101,7 +109,15 @@ export type ShellChromeState = {
|
||||
/** Height mode of the foreground window; decides where its top bar sits. */
|
||||
foregroundHeightMode: WindowHeightMode;
|
||||
foregroundAppId?: string;
|
||||
battery: { headset: number | null; headsetCharging: boolean | null };
|
||||
battery: {
|
||||
headset: number | null;
|
||||
headsetCharging: boolean | null;
|
||||
ring: number | null;
|
||||
ringCharging: boolean | null;
|
||||
/** The Wear OS watch (app/g2/wear-remote.ts); null when no watch is reachable. */
|
||||
watch: number | null;
|
||||
watchCharging: boolean | null;
|
||||
};
|
||||
/** App-provided tray images, drawn between notification icons and batteries. */
|
||||
trayIcons: GrayImage[];
|
||||
};
|
||||
@@ -312,8 +328,7 @@ export class ShellChromeLayer implements Layer {
|
||||
image.drawLine(barLeft, barTop + TOP_BAR_HEIGHT - 1, G2_LENS_WIDTH - 1, barTop + TOP_BAR_HEIGHT - 1, BORDER_VALUE);
|
||||
|
||||
const now = new Date();
|
||||
const clock = `${WEEKDAYS[now.getDay()]} ${now.getDate()} ${MONTHS[now.getMonth()]} ` +
|
||||
formatClockTime(now);
|
||||
const clock = `${formatClockDate(now)} ${formatClockTime(now)}`;
|
||||
const clockX = barLeft + 10;
|
||||
const textY = barTop + Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
|
||||
image.drawText(font, clockX, textY, clock, 210);
|
||||
@@ -336,35 +351,51 @@ export class ShellChromeLayer implements Layer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Labelled battery indicators for the phone and the G2, right-aligned in
|
||||
* the top bar, following the dashboard card's icon/percentage setting.
|
||||
* Returns the left edge of the battery block.
|
||||
* Labelled battery indicators for the phone, Wear OS watch, G2, and R1,
|
||||
* right-aligned in the top bar. The watch one exists only while a watch
|
||||
* running the Faceclaw watch app is reachable (no placeholder otherwise).
|
||||
* The Settings > Display > Battery indicators submenu picks the style (label beside a gauge icon or percentage, or stacked above
|
||||
* either) and, per device, whether the indicator shows always, only below
|
||||
* 50%, or never. Returns the left edge of the battery block.
|
||||
*/
|
||||
private drawTopBarBatteries(image: GrayImage, state: ShellChromeState, barTop: number): number {
|
||||
const font = getDefaultSmallFont();
|
||||
const percentageMode = batteryDisplayModeSetting.get() === "percentage";
|
||||
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||
const mode = batteryDisplayModeSetting.get();
|
||||
const items: BatteryItem[] = [];
|
||||
const phone = readPhoneBatteryState();
|
||||
if (phone.battery !== null && Number.isFinite(phone.battery)) {
|
||||
items.push({ label: "Phone", percent: phone.battery, charging: Boolean(phone.charging) });
|
||||
pushBatteryItem(items, phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, Boolean(phone.charging));
|
||||
}
|
||||
if (state.battery.watch !== null && Number.isFinite(state.battery.watch)) {
|
||||
pushBatteryItem(items, watchBatteryVisibilitySetting.get(), "Watch", state.battery.watch,
|
||||
Boolean(state.battery.watchCharging));
|
||||
}
|
||||
if (state.battery.headset !== null && Number.isFinite(state.battery.headset)) {
|
||||
items.push({ label: "G2", percent: state.battery.headset, charging: Boolean(state.battery.headsetCharging) });
|
||||
pushBatteryItem(items, glassesBatteryVisibilitySetting.get(), "G2", state.battery.headset,
|
||||
Boolean(state.battery.headsetCharging));
|
||||
}
|
||||
if (state.battery.ring !== null && Number.isInteger(state.battery.ring)
|
||||
&& state.battery.ring >= 0 && state.battery.ring <= 100) {
|
||||
pushBatteryItem(items, ringBatteryVisibilitySetting.get(), "R1", state.battery.ring,
|
||||
Boolean(state.battery.ringCharging));
|
||||
}
|
||||
if (!items.length) return G2_LENS_WIDTH;
|
||||
if (mode === "stacked" || mode === "stacked-percentage") {
|
||||
return drawStackedBatteries(image, items, barTop, mode === "stacked-percentage");
|
||||
}
|
||||
|
||||
const font = getDefaultSmallFont();
|
||||
const percentageMode = mode === "percentage";
|
||||
const labelGap = 5;
|
||||
const itemGap = 12;
|
||||
const textY = barTop + Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
|
||||
let x = G2_LENS_WIDTH - 8;
|
||||
let x = G2_LENS_WIDTH - BATTERY_BLOCK_RIGHT_MARGIN;
|
||||
for (let index = items.length - 1; index >= 0; index--) {
|
||||
const item = items[index]!;
|
||||
const percentText = `${Math.max(0, Math.min(100, Math.round(item.percent)))}%`;
|
||||
const percentText = `${item.percent}%`;
|
||||
const valueWidth = percentageMode ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
|
||||
const labelWidth = font.measureText(item.label);
|
||||
x -= labelWidth + labelGap + valueWidth;
|
||||
image.drawText(font, x, textY, item.label, 150);
|
||||
image.drawText(font, x, textY, item.label, BATTERY_LABEL_VALUE);
|
||||
const valueX = x + labelWidth + labelGap;
|
||||
if (percentageMode) {
|
||||
if (item.charging) {
|
||||
@@ -386,6 +417,74 @@ export class ShellChromeLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||
|
||||
const BATTERY_BLOCK_RIGHT_MARGIN = 8;
|
||||
const BATTERY_LABEL_VALUE = 150;
|
||||
|
||||
/** Append an indicator if its visibility setting shows it at this charge. */
|
||||
function pushBatteryItem(
|
||||
items: BatteryItem[],
|
||||
visibility: BatteryIndicatorVisibility,
|
||||
label: string,
|
||||
percent: number,
|
||||
charging: boolean,
|
||||
): void {
|
||||
const clamped = Math.max(0, Math.min(100, Math.round(percent)));
|
||||
if (!batteryIndicatorVisible(visibility, clamped)) return;
|
||||
items.push({ label, percent: clamped, charging });
|
||||
}
|
||||
|
||||
// Stacked style geometry, in rows below the bar's top edge. The bar has 27
|
||||
// usable rows above its bottom border line, too few for the user-sized UI
|
||||
// font (line height up to 21px) over a 10px gauge, so both stacked lines use
|
||||
// a fixed 12px bitmap face instead: TerminusV, proportional, with 8px
|
||||
// capitals and digits on a 10px ascent. Ink then spans rows 3..22: label
|
||||
// capitals at 3..10, a 2px gap, and the gauge at 13..22 (or the percentage
|
||||
// digits at 13..20, whose charging highlight box covers 12..21), leaving 3
|
||||
// rows above and 4 below (before the border) so the pair reads as one
|
||||
// centered unit.
|
||||
const STACKED_LABEL_BASELINE = 11;
|
||||
const STACKED_ICON_TOP = 13;
|
||||
const STACKED_PERCENT_BASELINE = 21;
|
||||
const STACKED_ITEM_GAP = 10;
|
||||
|
||||
/**
|
||||
* Stacked styles: each label centered above its gauge icon (or percentage
|
||||
* text), right-aligned in the bar. Returns the left edge of the battery
|
||||
* block.
|
||||
*/
|
||||
function drawStackedBatteries(image: GrayImage, items: BatteryItem[], barTop: number, percentage: boolean): number {
|
||||
const font = getFont("terminusv12");
|
||||
const labelTop = barTop + STACKED_LABEL_BASELINE - font.ascent;
|
||||
const percentTop = barTop + STACKED_PERCENT_BASELINE - font.ascent;
|
||||
let x = G2_LENS_WIDTH - BATTERY_BLOCK_RIGHT_MARGIN;
|
||||
for (let index = items.length - 1; index >= 0; index--) {
|
||||
const item = items[index]!;
|
||||
const percentText = `${item.percent}%`;
|
||||
const labelWidth = font.measureText(item.label);
|
||||
const valueWidth = percentage ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
|
||||
const itemWidth = Math.max(labelWidth, valueWidth);
|
||||
x -= itemWidth;
|
||||
image.drawText(font, x + (((itemWidth - labelWidth) / 2) | 0), labelTop, item.label, BATTERY_LABEL_VALUE);
|
||||
const valueX = x + (((itemWidth - valueWidth) / 2) | 0);
|
||||
if (!percentage) {
|
||||
const icon = drawBattery(item.percent, item.charging);
|
||||
image.bitBlt(icon, valueX, barTop + STACKED_ICON_TOP, { transparentZero: true });
|
||||
} else if (item.charging) {
|
||||
// Inverted text marks charging, as in the side-by-side percentage style;
|
||||
// the box hugs the digits' 8px ink rather than the full 12px line.
|
||||
const inkTop = barTop + STACKED_PERCENT_BASELINE - 8;
|
||||
image.fillRect(valueX - 2, inkTop - 1, valueWidth + 4, 10, 255);
|
||||
image.drawText(font, valueX, percentTop, percentText, 1);
|
||||
} else {
|
||||
image.drawText(font, valueX, percentTop, percentText, 200);
|
||||
}
|
||||
x -= STACKED_ITEM_GAP;
|
||||
}
|
||||
return x + STACKED_ITEM_GAP;
|
||||
}
|
||||
|
||||
// Ambient cards (encounter popups): compact and deliberately unobtrusive,
|
||||
// anchored to the bottom-right of the window band and stacking upward, clear
|
||||
// of the caption area's left-aligned text. Never interactive.
|
||||
@@ -519,17 +618,6 @@ function tabLeftInset(yy: number, top: number, bottom: number, radius: number):
|
||||
return Math.max(0, Math.round(radius - Math.sqrt(Math.max(0, radius * radius - dy * dy))));
|
||||
}
|
||||
|
||||
/** Format the top-bar clock time, honoring the 12/24-hour setting. */
|
||||
function formatClockTime(now: Date): string {
|
||||
const minutes = String(now.getMinutes()).padStart(2, "0");
|
||||
const hour24 = now.getHours();
|
||||
if (timeFormatSetting.get() === "12h") {
|
||||
const hour12 = ((hour24 + 11) % 12) + 1;
|
||||
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
||||
}
|
||||
return `${hour24}:${minutes}`;
|
||||
}
|
||||
|
||||
/** Small triangle marker for sidebar overflow; direction -1 = up, 1 = down. */
|
||||
function drawChevron(image: GrayImage, centerX: number, y: number, direction: -1 | 1): void {
|
||||
const half = 5;
|
||||
|
||||
@@ -38,6 +38,12 @@ export type InProcessWindowOptions = {
|
||||
* has no menu of its own, and tap-then-hold opens the system menu instead.
|
||||
*/
|
||||
menuItems?: () => MenuItem[];
|
||||
/** Dedicated chat gestures: click menu, hold microphone, tap-hold system menu. */
|
||||
holdToTalk?: boolean;
|
||||
isVoiceCapturing?: () => boolean;
|
||||
onSystemMenuOpened?: () => void;
|
||||
onAppMenuOpened?: () => void;
|
||||
setScreenOn?: ShellWindow["setScreenOn"];
|
||||
/** Shared actions; requestRender is rebound to this window's render. */
|
||||
actions: LayerActions;
|
||||
/**
|
||||
@@ -141,7 +147,8 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
shell.openSystemMenu(options.windowId);
|
||||
return;
|
||||
}
|
||||
stack.push(new WindowMenuLayer(options.title, items));
|
||||
options.onAppMenuOpened?.();
|
||||
stack.push(new WindowMenuLayer(options.title, items, options.holdToTalk));
|
||||
};
|
||||
|
||||
const window: ShellWindow = {
|
||||
@@ -153,6 +160,9 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
// The window's own LayerStack decides per layer whether a swipe is
|
||||
// directional or falls back to click / double-click.
|
||||
acceptsDirectional: true,
|
||||
holdToTalk: options.holdToTalk,
|
||||
isVoiceCapturing: options.isVoiceCapturing,
|
||||
setScreenOn: options.setScreenOn,
|
||||
heightMode,
|
||||
hasAppMenu: () => appMenuItems().length > 0,
|
||||
close: () => {
|
||||
@@ -168,6 +178,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
// The shell opened its system menu over this window; close our own
|
||||
// context menu so the two never stack. Never forwarded to app layers.
|
||||
if (event.type === "system-menu-opened") {
|
||||
options.onSystemMenuOpened?.();
|
||||
if (stack.popIfTop((layer) => layer instanceof WindowMenuLayer)) {
|
||||
requestRender();
|
||||
}
|
||||
@@ -178,7 +189,8 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
// submenus and app content alike. A plain long-press never arrives:
|
||||
// the shell keeps it for the system menu (no in-process window claims
|
||||
// it).
|
||||
if (event.type === "short-then-long-press") {
|
||||
if ((!options.holdToTalk && event.type === "short-then-long-press") ||
|
||||
(options.holdToTalk && event.type === "click" && stack.isAtBase())) {
|
||||
openWindowMenu();
|
||||
await render(frameId);
|
||||
return;
|
||||
|
||||
+162
-49
@@ -21,7 +21,9 @@ import { KeyboardInputLayer, type KeyboardInputSession } from "./keyboard-input"
|
||||
import { voiceActivity } from "./voice-activity";
|
||||
import { AssistantLayer } from "./assistant";
|
||||
import { AssistantSession, type AssistantBackendConfig } from "../../assistant/session";
|
||||
import { resolveAssistantModel } from "../../assistant/models";
|
||||
import { resolveAssistantModel, type AssistantModel } from "../../assistant/models";
|
||||
import { AssistantConversations, type ReasoningLevel } from "../../assistant/conversations";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import type { AssistantContext } from "../../assistant/types";
|
||||
import { SingleNotificationLayer } from "../notifications";
|
||||
import {
|
||||
@@ -32,7 +34,8 @@ import {
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
batteryDisplayModeSetting,
|
||||
batteryIndicatorSettingsKey,
|
||||
brightnessSetting,
|
||||
onAnySettingChanged,
|
||||
openAiApiKeySetting,
|
||||
timeFormatSetting,
|
||||
@@ -42,6 +45,7 @@ import { onAmbientCardsChanged } from "./ambient-cards";
|
||||
import { ShellChromeLayer, sidebarContentLeft, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer";
|
||||
import { ShellModalLayer } from "./modal-layer";
|
||||
import { ToolDebugMenuLayer } from "./tool-debug-layer";
|
||||
import { BrightnessPickerLayer } from "./brightness-picker-layer";
|
||||
import { toolRegistry } from "../../assistant/tool-registry";
|
||||
import {
|
||||
minWindowTop,
|
||||
@@ -61,7 +65,7 @@ import {
|
||||
* Input flow: every event enters via receiveInput. The shell consumes
|
||||
* everything while the sidebar or a shell overlay has focus and forwards the
|
||||
* rest to the focused window. Long-press opens the shell-owned system menu
|
||||
* (Focus app switcher, Voice input, Close window, Debug) without reaching the
|
||||
* (Focus app switcher, Voice input, Brightness, Close window, Debug) without reaching the
|
||||
* app, so the shell keeps working when a window's handler hangs; a window
|
||||
* that claims long-press for a move of its own gets it forwarded instead, and
|
||||
* holding the press past the escape threshold still opens the system menu.
|
||||
@@ -91,6 +95,10 @@ export type ShellWindow = {
|
||||
* system menu; holding the press past the escape threshold still opens it.
|
||||
*/
|
||||
claimsLongPress?: () => boolean;
|
||||
/** Chat uses hold-to-talk and tap-then-hold for the system menu, without an escape timer. */
|
||||
holdToTalk?: boolean;
|
||||
/** A window owns microphone capture outside the shell voice dialog. */
|
||||
isVoiceCapturing?: () => boolean;
|
||||
/**
|
||||
* True when the window gives swipe-left / swipe-right (watch directional
|
||||
* input) a meaning; otherwise the shell forwards directionalFallback(event).
|
||||
@@ -141,6 +149,13 @@ export type ShellWindow = {
|
||||
onFocus?: (lastInput: InputEvent | null) => void;
|
||||
/** Screen turned on/off; hidden or screen-off windows should stop painting. */
|
||||
setScreenOn?: (on: boolean) => void;
|
||||
/**
|
||||
* Input focus arrived at or left this window: it is (no longer) where
|
||||
* ordinary input goes. Unlike setForeground / onFocus, this also tracks
|
||||
* shell overlays (the system menu, a notification modal, the voice dialog)
|
||||
* and screen-off, so a game can pause on any of them. Sent on change only.
|
||||
*/
|
||||
setInputFocus?: (focused: boolean) => void;
|
||||
};
|
||||
|
||||
export type ShellConfig = {
|
||||
@@ -283,24 +298,55 @@ class ShellAlertLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/** Every setting the top bar paints from, as one comparable string. */
|
||||
function topBarSettingsKey(): string {
|
||||
return `${batteryIndicatorSettingsKey()}|${timeFormatSetting.get()}`;
|
||||
}
|
||||
|
||||
class Shell {
|
||||
private windows: ShellWindow[] = [];
|
||||
private selectedIndex = 0;
|
||||
/** Window ids in most-recently-visible-first order; closing the visible window returns to the next entry. */
|
||||
private mruWindowIds: string[] = [];
|
||||
private focus: FocusKind = "sidebar";
|
||||
/** The window last told it holds input focus (see syncInputFocus). */
|
||||
private inputFocusedWindowId: string | null = null;
|
||||
private screenOn = true;
|
||||
private lastInputAtMs = Date.now();
|
||||
/** The most recent input event received, for windows gaining focus (see ShellWindow.onFocus). */
|
||||
private lastInput: InputEvent | null = null;
|
||||
private battery: ShellChromeState["battery"] = { headset: null, headsetCharging: null };
|
||||
private battery: ShellChromeState["battery"] = {
|
||||
headset: null, headsetCharging: null, ring: null, ringCharging: null, watch: null, watchCharging: null,
|
||||
};
|
||||
private attention = new Map<string, boolean>();
|
||||
// App-provided top-bar tray icons, keyed by owner id; drawn between the
|
||||
// notification icons and the battery indicators.
|
||||
private readonly trayIcons = new Map<string, GrayImage>();
|
||||
private activeVoiceLayer: VoiceInputLayer | null = null;
|
||||
private activeKeyboardLayer: KeyboardInputLayer | null = null;
|
||||
private assistantSession: AssistantSession | null = null;
|
||||
private conversations: AssistantConversations | null = null;
|
||||
private get assistantSession(): AssistantSession | null {
|
||||
return this.conversations?.current().session ?? null;
|
||||
}
|
||||
|
||||
getAssistantConversations(): AssistantConversations {
|
||||
if (!this.conversations) {
|
||||
this.conversations = new AssistantConversations(
|
||||
(model, reasoning) => this.resolveAssistantConfiguration(model, reasoning),
|
||||
() => assistantModelSetting.get(),
|
||||
(value) => setStringSetting("assistant.conversations", value),
|
||||
getStringSetting("assistant.conversations", ""),
|
||||
);
|
||||
}
|
||||
return this.conversations;
|
||||
}
|
||||
|
||||
async prepareChatVoiceCapture(): Promise<boolean> {
|
||||
if (this.activeVoiceLayer || this.activeKeyboardLayer || this.voiceDialogPending) return false;
|
||||
const ready = (await this.config.prepareVoiceCapture?.()) ?? true;
|
||||
return ready && !this.activeVoiceLayer && !this.activeKeyboardLayer &&
|
||||
this.stack.isAtBase() && this.isWindowFocused("ai-chat");
|
||||
}
|
||||
private assistantLayer: AssistantLayer | null = null;
|
||||
private readonly assistantActivityListeners = new Set<(event: AssistantActivityEvent) => void>();
|
||||
private readonly alertListeners = new Set<(text: string) => void>();
|
||||
@@ -315,14 +361,21 @@ class Shell {
|
||||
private readonly chrome = new ShellChromeLayer(() => this.chromeState());
|
||||
private readonly stack = new LayerStack(this.chrome, this.actions);
|
||||
|
||||
// Top-bar settings we mirror into the chrome; a change to either repaints
|
||||
// the shell surface so the top bar reflects it immediately.
|
||||
// Top-bar settings we mirror into the chrome; a change to any of them
|
||||
// repaints the shell surface so the top bar reflects it immediately.
|
||||
private topBarSettingsSubscribed = false;
|
||||
private lastBatteryDisplayMode: string | null = null;
|
||||
private lastTimeFormat: string | null = null;
|
||||
private lastTopBarSettingsKey: string | null = null;
|
||||
|
||||
configure(config: ShellConfig): void {
|
||||
this.config = config;
|
||||
// Nearly every state change ends in a shell render request, so it is
|
||||
// the natural point to tell windows about input-focus changes too.
|
||||
this.config = {
|
||||
...config,
|
||||
requestShellRender: () => {
|
||||
this.syncInputFocus();
|
||||
config.requestShellRender();
|
||||
},
|
||||
};
|
||||
this.stack.setActions(config.actions);
|
||||
this.subscribeToTopBarSettings();
|
||||
this.subscribeToAmbientCards();
|
||||
@@ -345,16 +398,11 @@ class Shell {
|
||||
private subscribeToTopBarSettings(): void {
|
||||
if (this.topBarSettingsSubscribed) return;
|
||||
this.topBarSettingsSubscribed = true;
|
||||
this.lastBatteryDisplayMode = batteryDisplayModeSetting.get();
|
||||
this.lastTimeFormat = timeFormatSetting.get();
|
||||
this.lastTopBarSettingsKey = topBarSettingsKey();
|
||||
onAnySettingChanged(() => {
|
||||
const batteryMode = batteryDisplayModeSetting.get();
|
||||
const timeFormat = timeFormatSetting.get();
|
||||
if (batteryMode === this.lastBatteryDisplayMode && timeFormat === this.lastTimeFormat) {
|
||||
return;
|
||||
}
|
||||
this.lastBatteryDisplayMode = batteryMode;
|
||||
this.lastTimeFormat = timeFormat;
|
||||
const key = topBarSettingsKey();
|
||||
if (key === this.lastTopBarSettingsKey) return;
|
||||
this.lastTopBarSettingsKey = key;
|
||||
this.config.requestShellRender();
|
||||
});
|
||||
}
|
||||
@@ -468,7 +516,7 @@ class Shell {
|
||||
return this.screenOn;
|
||||
}
|
||||
|
||||
/** Current headset battery levels (for the assistant's get_state tool). */
|
||||
/** Current G2, R1, and Wear OS watch battery levels (top bar, Glanceboard, assistant). */
|
||||
getBatteryLevels(): ShellChromeState["battery"] {
|
||||
return this.battery;
|
||||
}
|
||||
@@ -508,6 +556,34 @@ class Shell {
|
||||
return !!window && this.focus === "window" && this.foregroundWindow() === window;
|
||||
}
|
||||
|
||||
/**
|
||||
* The window ordinary input reaches right now: the foreground window with
|
||||
* focus in-window, the screen on, and no shell overlay (system menu,
|
||||
* notification modal, voice dialog, alert) above it.
|
||||
*/
|
||||
private inputTargetWindow(): ShellWindow | undefined {
|
||||
if (!this.screenOn || this.focus !== "window" || !this.stack.isAtBase() || this.activeVoiceLayer) {
|
||||
return undefined;
|
||||
}
|
||||
return this.foregroundWindow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell windows when they gain or lose input focus (ShellWindow.setInputFocus).
|
||||
* Called from every shell render request and the input/wake/sleep paths,
|
||||
* which between them follow every focus-affecting state change; it diffs
|
||||
* against the last notification, so calling it often is cheap.
|
||||
*/
|
||||
private syncInputFocus(): void {
|
||||
const target = this.inputTargetWindow();
|
||||
const targetId = target?.windowId ?? null;
|
||||
if (targetId === this.inputFocusedWindowId) return;
|
||||
const previous = this.windows.find((w) => w.windowId === this.inputFocusedWindowId);
|
||||
this.inputFocusedWindowId = targetId;
|
||||
previous?.setInputFocus?.(false);
|
||||
target?.setInputFocus?.(true);
|
||||
}
|
||||
|
||||
/** Turn the screen on (if off) and set focus. Returns whether it was off. */
|
||||
wake(focus: FocusKind, nowMs = Date.now()): boolean {
|
||||
this.lastInputAtMs = nowMs;
|
||||
@@ -515,7 +591,10 @@ class Shell {
|
||||
const alreadyFocused = this.isFocusTarget(gaining);
|
||||
this.focus = focus;
|
||||
if (gaining && !alreadyFocused) gaining.onFocus?.(this.lastInput);
|
||||
if (this.screenOn) return false;
|
||||
if (this.screenOn) {
|
||||
this.syncInputFocus();
|
||||
return false;
|
||||
}
|
||||
this.screenOn = true;
|
||||
this.config.onScreenStateChanged(true);
|
||||
for (const window of this.windows) {
|
||||
@@ -524,6 +603,7 @@ class Shell {
|
||||
// Refresh the foreground window; the compositor restored its retained
|
||||
// frame, but its content may be stale (e.g. a running stopwatch).
|
||||
this.foregroundWindow()?.requestRender();
|
||||
this.syncInputFocus();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -536,6 +616,7 @@ class Shell {
|
||||
for (const window of this.windows) {
|
||||
window.setScreenOn?.(false);
|
||||
}
|
||||
this.syncInputFocus();
|
||||
this.config.onScreenStateChanged(false);
|
||||
}
|
||||
|
||||
@@ -548,6 +629,7 @@ class Shell {
|
||||
this.setSelectedIndex(index);
|
||||
this.focus = "window";
|
||||
if (!alreadyFocused) target?.onFocus?.(this.lastInput);
|
||||
this.syncInputFocus();
|
||||
}
|
||||
|
||||
/** Idle timeout: sleep if the configured timeout elapsed. Returns whether it slept. */
|
||||
@@ -561,7 +643,7 @@ class Shell {
|
||||
// ring. An in-flight assistant turn suspends it for the same reason (a
|
||||
// tool loop can run for a while with no input); once the turn ends and
|
||||
// the Follow-up/Done menu is showing, the normal idle timeout resumes.
|
||||
if (this.activeVoiceLayer || this.activeKeyboardLayer || this.assistantSession?.isTurnActive()) {
|
||||
if (this.activeVoiceLayer || this.activeKeyboardLayer || this.assistantSession?.isTurnActive() || this.foregroundWindow()?.isVoiceCapturing?.()) {
|
||||
this.lastInputAtMs = nowMs;
|
||||
return false;
|
||||
}
|
||||
@@ -626,6 +708,16 @@ class Shell {
|
||||
}
|
||||
|
||||
async receiveInput(event: InputEvent, frameId = 0): Promise<ShellInputOutcome> {
|
||||
try {
|
||||
return await this.routeInput(event, frameId);
|
||||
} finally {
|
||||
// Whatever the input did (opened an overlay, moved focus, slept the
|
||||
// screen), windows learn about the resulting input-focus change.
|
||||
this.syncInputFocus();
|
||||
}
|
||||
}
|
||||
|
||||
private async routeInput(event: InputEvent, frameId: number): Promise<ShellInputOutcome> {
|
||||
const previous = this.lastInput;
|
||||
this.lastInput = event;
|
||||
// A visible window may paint a source-dependent indicator (see
|
||||
@@ -647,6 +739,7 @@ class Shell {
|
||||
// Even AI app never launches, so the firmware does not power the display
|
||||
// for us either -- actions that need it wake the screen themselves.
|
||||
if (event.type === "wakeword") {
|
||||
if (this.foregroundWindow()?.isVoiceCapturing?.()) return { shell: true, window: false };
|
||||
const action = wakeWordActionSetting.get();
|
||||
if (action === "off") {
|
||||
return { shell: false, window: false };
|
||||
@@ -698,11 +791,11 @@ class Shell {
|
||||
if (!window) {
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
if (!window.claimsLongPress?.()) {
|
||||
if (!window.holdToTalk && !window.claimsLongPress?.()) {
|
||||
this.openEscapeMenu();
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
this.startEscapeMenuTimer();
|
||||
if (!window.holdToTalk) this.startEscapeMenuTimer();
|
||||
if (this.focus === "sidebar") {
|
||||
this.focus = "window";
|
||||
window.onFocus?.(this.lastInput);
|
||||
@@ -718,6 +811,10 @@ class Shell {
|
||||
// when it has none.
|
||||
if (event.type === "short-then-long-press") {
|
||||
const window = this.foregroundWindow();
|
||||
if (window?.holdToTalk) {
|
||||
this.openEscapeMenu();
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
// Over the open system menu it switches to the app's context menu:
|
||||
// close the system menu and deliver the gesture to the window as
|
||||
// usual. A window without a menu of its own would only ask for the
|
||||
@@ -1027,7 +1124,7 @@ class Shell {
|
||||
*/
|
||||
startKeyboardInput(): KeyboardInputSession | null {
|
||||
if (this.activeKeyboardLayer) return this.activeKeyboardLayer;
|
||||
if (this.activeVoiceLayer) return null;
|
||||
if (this.activeVoiceLayer || this.foregroundWindow()?.isVoiceCapturing?.()) return null;
|
||||
if (!this.screenOn) this.wake("sidebar");
|
||||
const assistantLayer = this.assistantLayer;
|
||||
const assistantSession = this.assistantSession;
|
||||
@@ -1072,7 +1169,8 @@ class Shell {
|
||||
}
|
||||
|
||||
isAssistantAvailable(): boolean {
|
||||
return this.resolveAssistantConfiguration() !== null;
|
||||
const current = this.getAssistantConversations().current();
|
||||
return this.resolveAssistantConfiguration(current.model, current.reasoning) !== null;
|
||||
}
|
||||
|
||||
/** Observe assistant turns (see AssistantActivityEvent). */
|
||||
@@ -1110,20 +1208,13 @@ class Shell {
|
||||
}
|
||||
|
||||
private ensureAssistantSession(): AssistantSession | null {
|
||||
const config = this.resolveAssistantConfiguration();
|
||||
if (!config) return null;
|
||||
if (
|
||||
!this.assistantSession ||
|
||||
this.assistantSession.isExpired() ||
|
||||
!this.assistantSession.matchesConfiguration(config)
|
||||
) {
|
||||
this.assistantSession?.cancel();
|
||||
this.assistantSession = new AssistantSession(config);
|
||||
}
|
||||
return this.assistantSession;
|
||||
return this.getAssistantConversations().ensureSession();
|
||||
}
|
||||
|
||||
private resolveAssistantConfiguration(): AssistantBackendConfig | null {
|
||||
private resolveAssistantConfiguration(
|
||||
model: AssistantModel = this.conversations?.current().model ?? assistantModelSetting.get(),
|
||||
reasoning: ReasoningLevel = this.conversations?.current().reasoning ?? "default",
|
||||
): AssistantBackendConfig | null {
|
||||
if (assistantBackendSetting.get() === "external") {
|
||||
const host = assistantBridgeHostSetting.get().trim();
|
||||
const token = assistantBridgeTokenSetting.get();
|
||||
@@ -1131,10 +1222,11 @@ class Shell {
|
||||
const port = parseInt(assistantBridgePortSetting.get(), 10) || 8790;
|
||||
return { kind: "external", bridge: { host, port, token } };
|
||||
}
|
||||
const llm = resolveAssistantModel(assistantModelSetting.get(), {
|
||||
const llm = resolveAssistantModel(model, {
|
||||
anthropic: anthropicApiKeySetting.get(),
|
||||
openai: openAiApiKeySetting.get(),
|
||||
});
|
||||
if (llm && reasoning !== "default" && llm.effort) llm.effort = reasoning;
|
||||
return llm ? { kind: "direct", llm } : null;
|
||||
}
|
||||
|
||||
@@ -1154,7 +1246,9 @@ class Shell {
|
||||
* Opens the assistant overlay if it isn't already up; a follow-up reuses the
|
||||
* existing session and overlay.
|
||||
*/
|
||||
sendToAssistant(text: string): void {
|
||||
sendToAssistant(text: string, showOverlay = true): void {
|
||||
text = text.trim();
|
||||
if (!text) return;
|
||||
const session = this.ensureAssistantSession();
|
||||
if (!session) {
|
||||
this.showAlert(
|
||||
@@ -1164,7 +1258,15 @@ class Shell {
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (session.isTurnActive()) {
|
||||
this.showAlert("The assistant is still working on the previous request");
|
||||
return;
|
||||
}
|
||||
if (!this.screenOn) this.wake("sidebar");
|
||||
if (!showOverlay || this.foregroundWindow()?.appId === "ai-chat") {
|
||||
this.runAssistantTurn(session, null, text);
|
||||
return;
|
||||
}
|
||||
let layer = this.assistantLayer;
|
||||
if (!layer) {
|
||||
const created = new AssistantLayer(this.config.actions, {
|
||||
@@ -1173,7 +1275,7 @@ class Shell {
|
||||
onClose: () => this.closeAssistantLayer(),
|
||||
onRemoved: () => {
|
||||
// Removed by any path (Done, or the screen sleeping mid-conversation):
|
||||
// stop the turn and drop the reference so a later query starts clean.
|
||||
// stop the turn and drop the overlay reference; keep shared history.
|
||||
this.assistantSession?.cancel();
|
||||
if (this.assistantLayer === created) this.assistantLayer = null;
|
||||
this.emitAssistantActivity({ phase: "closed", text: "" });
|
||||
@@ -1187,23 +1289,24 @@ class Shell {
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
private runAssistantTurn(session: AssistantSession, layer: AssistantLayer, text: string): void {
|
||||
layer.startTurn();
|
||||
private runAssistantTurn(session: AssistantSession, layer: AssistantLayer | null, text: string): void {
|
||||
if (session.isTurnActive()) return;
|
||||
layer?.startTurn();
|
||||
let replySoFar = "";
|
||||
this.emitAssistantActivity({ phase: "thinking", text: "" });
|
||||
session.sendUtterance(text, this.buildAssistantContext(), {
|
||||
onTextDelta: (delta, textSoFar) => {
|
||||
replySoFar = textSoFar;
|
||||
layer.onTextDelta(delta, textSoFar);
|
||||
layer?.onTextDelta(delta, textSoFar);
|
||||
this.emitAssistantActivity({ phase: "streaming", text: textSoFar });
|
||||
},
|
||||
onToolActivity: (label) => layer.onToolActivity(label),
|
||||
onToolActivity: (label) => layer?.onToolActivity(label),
|
||||
onTurnDone: () => {
|
||||
layer.onTurnDone();
|
||||
layer?.onTurnDone();
|
||||
this.emitAssistantActivity({ phase: "done", text: replySoFar });
|
||||
},
|
||||
onError: (message) => {
|
||||
layer.onError(message);
|
||||
layer?.onError(message);
|
||||
this.emitAssistantActivity({ phase: "error", text: message });
|
||||
},
|
||||
});
|
||||
@@ -1301,7 +1404,7 @@ class Shell {
|
||||
|
||||
/**
|
||||
* The system/escape menu: the entries every window shares (Focus app
|
||||
* switcher, Voice input, Close window) plus Debug. Shell-owned and
|
||||
* switcher, Voice input, Brightness, Close window) plus Debug. Shell-owned and
|
||||
* shell-drawn (never the app's), so an unresponsive app can always be
|
||||
* closed. It opens for long-press (over the app's own menu too), after an
|
||||
* extended hold in a window that claims long-press, and on a window's
|
||||
@@ -1348,6 +1451,16 @@ class Shell {
|
||||
},
|
||||
}]),
|
||||
);
|
||||
if (brightnessSetting.get() !== "auto") {
|
||||
items.push({
|
||||
label: "Brightness",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new BrightnessPickerLayer(() => this.yieldFocusToSidebar()));
|
||||
this.config.requestShellRender();
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: "Debug",
|
||||
onSelect: (ctx) => {
|
||||
@@ -1359,7 +1472,7 @@ class Shell {
|
||||
// when the app has one (otherwise it opens this very menu, not worth a
|
||||
// hint).
|
||||
const footer = foreground.hasAppMenu?.()
|
||||
? gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "app menu"]])
|
||||
? (foreground.holdToTalk ? undefined : gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "app menu"]]))
|
||||
: undefined;
|
||||
layer = new ShellOverlayMenuLayer(items, footer, () => this.yieldFocusToSidebar());
|
||||
layer.selectItem(initialSelection);
|
||||
|
||||
+70
-20
@@ -5,8 +5,7 @@ import { anthropicApiKeySetting } from "../dashboard-settings";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, gestureHints, type InputEvent } from "../gestures";
|
||||
import { Layer, type LayerActions, type LayerContext } from "../layers";
|
||||
import { paintInputDialog } from "./input-dialog";
|
||||
// After the mic stops, the provider's final transcript can trail in (cloud
|
||||
// commit round-trip); wait this long for it before refining with what we have.
|
||||
// After native recognition finishes, a cloud final can still trail in.
|
||||
const FOLLOWUP_FINALIZE_TIMEOUT_MS = 1200;
|
||||
|
||||
/**
|
||||
@@ -64,7 +63,11 @@ export type VoiceInputLayerOptions = {
|
||||
*/
|
||||
export class VoiceInputLayer implements Layer {
|
||||
private phase: VoicePhase = "capturing";
|
||||
private status = "Listening...";
|
||||
private status = "Starting microphone...";
|
||||
/** Mirrors the bridge: only true while audio is actually being transcribed. */
|
||||
private listening = false;
|
||||
/** Bridge guidance shown in place of the empty transcript (e.g. "check your phone"). */
|
||||
private detail = "";
|
||||
// The active utterance. displayText() is what the dialog shows and what
|
||||
// Send delivers; the refine flow also writes the merged result here.
|
||||
private finalizedText = "";
|
||||
@@ -76,6 +79,10 @@ export class VoiceInputLayer implements Layer {
|
||||
private capturing = false;
|
||||
/** Continuation stopped; waiting for the trailing final transcript. */
|
||||
private followupFinalizeTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private pendingFollowup = false;
|
||||
private stoppingCapture = false;
|
||||
private stopGeneration = 0;
|
||||
private acceptingTranscript = true;
|
||||
private refineHandle: AnthropicStreamHandle | null = null;
|
||||
private menuIndex = 0;
|
||||
/** Auto-send (wakeword skip-confirmation) is waiting to fire. */
|
||||
@@ -114,6 +121,8 @@ export class VoiceInputLayer implements Layer {
|
||||
// The refine stage owns the status line ("Refining...", error text).
|
||||
if (this.phase === "refining") return;
|
||||
this.status = state.status;
|
||||
this.listening = state.listening;
|
||||
this.detail = state.detail;
|
||||
this.actions.requestRender();
|
||||
});
|
||||
if (this.handsFree || global.isIOS) {
|
||||
@@ -149,19 +158,48 @@ export class VoiceInputLayer implements Layer {
|
||||
}
|
||||
this.capturing = false;
|
||||
this.phase = "menu";
|
||||
void this.actions.stopVoiceCapture();
|
||||
if (this.autoSend && this.sendTargets.length) {
|
||||
// Skip-confirmation (wakeword): send to the default target as soon as the
|
||||
// transcript finalizes, or after a short wait for the trailing final.
|
||||
this.pendingAutoSend = true;
|
||||
this.status = "Sending...";
|
||||
this.autoSendTimer = setTimeout(() => this.performAutoSend(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
|
||||
} else if (this.status.startsWith("Listening")) {
|
||||
} else if (this.status.endsWith("...")) {
|
||||
// A progress status ("Starting microphone...", "Listening...") gives
|
||||
// way to the menu prompt; an error (ending in ".") stays visible.
|
||||
this.status = "Send, continue, or discard?";
|
||||
}
|
||||
this.stopCapture(() => {
|
||||
if (this.pendingAutoSend) {
|
||||
this.autoSendTimer = setTimeout(() => this.performAutoSend(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
|
||||
}
|
||||
});
|
||||
this.actions.requestRender();
|
||||
}
|
||||
|
||||
/** Native stop can outlast its synchronous join while Whisper is decoding. */
|
||||
private stopCapture(onStopped: () => void): void {
|
||||
const generation = ++this.stopGeneration;
|
||||
this.stoppingCapture = true;
|
||||
const afterStop = () => {
|
||||
if (generation !== this.stopGeneration) return;
|
||||
this.stoppingCapture = false;
|
||||
onStopped();
|
||||
this.actions.requestRender();
|
||||
};
|
||||
const stopped = this.actions.stopVoiceCapture();
|
||||
if (!stopped) {
|
||||
afterStop();
|
||||
return;
|
||||
}
|
||||
void stopped.then(afterStop, (error) => {
|
||||
if (generation !== this.stopGeneration) return;
|
||||
this.stoppingCapture = false;
|
||||
this.pendingAutoSend = false;
|
||||
this.acceptingTranscript = false;
|
||||
this.backToMenu(this.phase === "continuing" ? this.baseText : this.displayText(), String(error));
|
||||
});
|
||||
}
|
||||
|
||||
/** Fire the queued skip-confirmation send (or fall back to the menu). */
|
||||
private performAutoSend(): void {
|
||||
if (this.autoSendTimer !== null) {
|
||||
@@ -185,7 +223,7 @@ export class VoiceInputLayer implements Layer {
|
||||
/** The menu rows: one per send target, then Continue, then Discard. */
|
||||
private menuRows(): Array<{ label: string; dim: boolean; onSelect: () => void }> {
|
||||
const text = this.displayText().trim();
|
||||
const hasText = text.length > 0;
|
||||
const hasText = text.length > 0 && !this.stoppingCapture;
|
||||
const hasLlmKey = !global.isIOS && anthropicApiKeySetting.get().trim().length > 0;
|
||||
const rows: Array<{ label: string; dim: boolean; onSelect: () => void }> = [];
|
||||
for (const target of this.sendTargets) {
|
||||
@@ -193,6 +231,7 @@ export class VoiceInputLayer implements Layer {
|
||||
label: target.label,
|
||||
dim: !hasText,
|
||||
onSelect: () => {
|
||||
if (this.stoppingCapture) return;
|
||||
this.dismiss();
|
||||
if (hasText) target.onSend(text);
|
||||
},
|
||||
@@ -200,9 +239,9 @@ export class VoiceInputLayer implements Layer {
|
||||
}
|
||||
rows.push({
|
||||
label: global.isIOS ? "Continue (Not available on iOS)" : hasLlmKey ? "Continue" : "Continue (Needs LLM API key)",
|
||||
dim: !hasLlmKey,
|
||||
dim: !hasLlmKey || this.stoppingCapture,
|
||||
onSelect: () => {
|
||||
if (hasLlmKey) this.startContinuation();
|
||||
if (hasLlmKey && !this.stoppingCapture) this.startContinuation();
|
||||
},
|
||||
});
|
||||
rows.push({ label: "Discard", dim: false, onSelect: () => this.dismiss() });
|
||||
@@ -213,8 +252,8 @@ export class VoiceInputLayer implements Layer {
|
||||
const image = paintBelow();
|
||||
const inMenu = this.phase === "menu";
|
||||
paintInputDialog(image, {
|
||||
title: this.capturing ? "Voice ●" : "Voice",
|
||||
status: this.status,
|
||||
title: this.capturing && this.listening ? "Voice ●" : "Voice",
|
||||
status: this.stoppingCapture ? "Finishing transcription..." : this.status,
|
||||
text: this.displayText() || this.placeholderText(),
|
||||
rows: inMenu ? this.menuRows() : [],
|
||||
selectedRow: this.menuIndex,
|
||||
@@ -276,6 +315,7 @@ export class VoiceInputLayer implements Layer {
|
||||
|
||||
/** Continue selected: keep the message aside and record a follow-up. */
|
||||
private startContinuation(): void {
|
||||
this.acceptingTranscript = true;
|
||||
this.baseText = this.displayText().trim();
|
||||
this.finalizedText = "";
|
||||
this.liveText = "";
|
||||
@@ -290,15 +330,18 @@ export class VoiceInputLayer implements Layer {
|
||||
private endContinuationCapture(): void {
|
||||
if (!this.capturing) return;
|
||||
this.capturing = false;
|
||||
void this.actions.stopVoiceCapture();
|
||||
this.status = "Refining...";
|
||||
this.pendingFollowup = true;
|
||||
this.status = "Finishing transcription...";
|
||||
this.stopCapture(() => {
|
||||
if (this.pendingFollowup) {
|
||||
this.followupFinalizeTimer = setTimeout(() => this.beginRefine(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
|
||||
}
|
||||
});
|
||||
this.actions.requestRender();
|
||||
// The provider's committed transcript arrives shortly after stop; refine
|
||||
// when it does, or after a timeout with whatever partials we have.
|
||||
this.followupFinalizeTimer = setTimeout(() => this.beginRefine(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
private beginRefine(): void {
|
||||
this.pendingFollowup = false;
|
||||
if (this.followupFinalizeTimer !== null) {
|
||||
clearTimeout(this.followupFinalizeTimer);
|
||||
this.followupFinalizeTimer = null;
|
||||
@@ -335,9 +378,10 @@ export class VoiceInputLayer implements Layer {
|
||||
|
||||
/** Abort a continuation (mic or LLM stage) and restore the prior message. */
|
||||
private cancelContinuation(status: string): void {
|
||||
this.acceptingTranscript = false;
|
||||
if (this.capturing) {
|
||||
this.capturing = false;
|
||||
void this.actions.stopVoiceCapture();
|
||||
this.stopCapture(() => {});
|
||||
}
|
||||
this.refineHandle?.cancel();
|
||||
this.refineHandle = null;
|
||||
@@ -345,6 +389,7 @@ export class VoiceInputLayer implements Layer {
|
||||
}
|
||||
|
||||
private backToMenu(text: string, status: string): void {
|
||||
this.pendingFollowup = false;
|
||||
if (this.followupFinalizeTimer !== null) {
|
||||
clearTimeout(this.followupFinalizeTimer);
|
||||
this.followupFinalizeTimer = null;
|
||||
@@ -358,6 +403,9 @@ export class VoiceInputLayer implements Layer {
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
++this.stopGeneration;
|
||||
this.acceptingTranscript = false;
|
||||
this.pendingFollowup = false;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.unsubscribeStatus?.();
|
||||
@@ -392,7 +440,9 @@ export class VoiceInputLayer implements Layer {
|
||||
private placeholderText(): string {
|
||||
switch (this.phase) {
|
||||
case "capturing":
|
||||
return "Listening...";
|
||||
// Only claim to listen when the mic is actually running; otherwise
|
||||
// the bridge's guidance (permission prompt on the phone, etc.), if any.
|
||||
return this.listening ? "Listening..." : this.detail;
|
||||
case "continuing":
|
||||
return "Say more, or describe an edit...";
|
||||
case "refining":
|
||||
@@ -419,14 +469,14 @@ export class VoiceInputLayer implements Layer {
|
||||
private onTranscript(event: VoiceTranscriptEvent): void {
|
||||
// The refine stream owns the text buffers once it starts; a transcript
|
||||
// that trails in after that point is stale.
|
||||
if (this.phase === "refining") return;
|
||||
if (!this.acceptingTranscript || this.phase === "refining") return;
|
||||
if (event.isFinal) {
|
||||
const finalText = event.text.trim() || this.liveText.trim();
|
||||
if (finalText) {
|
||||
this.finalizedText = this.finalizedText ? `${this.finalizedText} ${finalText}` : finalText;
|
||||
}
|
||||
this.liveText = "";
|
||||
if (this.phase === "continuing" && this.followupFinalizeTimer !== null) {
|
||||
if (this.phase === "continuing" && this.pendingFollowup) {
|
||||
// The follow-up finalized; no need to keep waiting.
|
||||
this.beginRefine();
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Main-thread mirror of small state that worker apps publish (the
|
||||
* "publish-state" worker reply): the latest value per key, with change
|
||||
* notifications, so main-thread consumers that are not the app's own windows
|
||||
* (a Glanceboard widget, say) can show what a worker knows without a second
|
||||
* data connection. Values cross postMessage, so they are plain JSON.
|
||||
*/
|
||||
const values = new Map<string, unknown>();
|
||||
const listeners = new Map<string, Set<(value: unknown) => void>>();
|
||||
|
||||
export function publishWorkerState(key: string, value: unknown): void {
|
||||
values.set(key, value);
|
||||
for (const listener of Array.from(listeners.get(key) ?? [])) {
|
||||
try {
|
||||
listener(value);
|
||||
} catch (error) {
|
||||
console.warn(`worker-state listener for ${key} failed`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The latest published value for `key`, or undefined when nothing was published yet. */
|
||||
export function readWorkerState<T>(key: string): T | undefined {
|
||||
return values.get(key) as T | undefined;
|
||||
}
|
||||
|
||||
/** Subscribe to later publications of `key`; returns the unsubscribe. */
|
||||
export function onWorkerStateChanged<T>(key: string, listener: (value: T) => void): () => void {
|
||||
let set = listeners.get(key);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
listeners.set(key, set);
|
||||
}
|
||||
set.add(listener as (value: unknown) => void);
|
||||
return () => {
|
||||
set!.delete(listener as (value: unknown) => void);
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/to
|
||||
import { appViewportSize, type WindowHeightMode } from "./geometry";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { shell, type ShellWindow } from "./shell";
|
||||
import { publishWorkerState } from "./worker-state";
|
||||
|
||||
/**
|
||||
* Messages between the shell (main thread) and an app worker. One worker
|
||||
@@ -20,6 +21,12 @@ export type WorkerAppMessage =
|
||||
| { type: "text-input"; windowId: string; text: string }
|
||||
| { type: "render"; windowId: string; focused: boolean }
|
||||
| { type: "foreground"; windowId: string; foreground: boolean; focused: boolean }
|
||||
/**
|
||||
* Input focus arrived at or left the window, counting shell overlays and
|
||||
* screen-off (see ShellWindow.setInputFocus). Sent on change only; the
|
||||
* per-message `focused` flags above stay the source of truth for painting.
|
||||
*/
|
||||
| { type: "input-focus"; windowId: string; focused: boolean }
|
||||
| { type: "screen"; on: boolean }
|
||||
/** Assistant tool invocation aimed at a window; reply with tool-result. */
|
||||
| { type: "tool-call"; callId: string; windowId: string; name: string; args: unknown };
|
||||
@@ -135,6 +142,15 @@ export type WorkerAppReply =
|
||||
type: "tool-result";
|
||||
callId: string;
|
||||
result: ToolResult;
|
||||
}
|
||||
| {
|
||||
/**
|
||||
* Publish a small piece of app state for main-thread consumers outside
|
||||
* the app's windows (see app/ui/shell/worker-state.ts). JSON only.
|
||||
*/
|
||||
type: "publish-state";
|
||||
key: string;
|
||||
state: unknown;
|
||||
};
|
||||
|
||||
export type WorkerWindowSpec = {
|
||||
@@ -318,6 +334,9 @@ export class WorkerAppHost {
|
||||
case "set-title":
|
||||
// Titles are informational for now (sidebar shows icons only).
|
||||
break;
|
||||
case "publish-state":
|
||||
publishWorkerState(message.key, message.state);
|
||||
break;
|
||||
case "set-tools":
|
||||
// Only a window we actually have open may contribute tools.
|
||||
if (this.openWindows.has(message.windowId)) {
|
||||
@@ -420,6 +439,9 @@ export class WorkerAppHost {
|
||||
// uniform; the worker treats it globally.
|
||||
this.post({ type: "screen", on });
|
||||
},
|
||||
setInputFocus: (focused) => {
|
||||
this.post({ type: "input-focus", windowId: spec.windowId, focused });
|
||||
},
|
||||
};
|
||||
shell.registerWindow(window);
|
||||
// Configure the surface before any foregrounding so the worker's first
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* The "Sound: on/off" entry in a game's context menu, persisted per app under
|
||||
* `<appId>.soundOn` so each game remembers its own choice across launches.
|
||||
* Reads default to on; storage failures fall back to on and never throw.
|
||||
*/
|
||||
import { getBooleanSetting, setBooleanSetting } from "../native/settings-store";
|
||||
|
||||
function soundKey(appId: string): string {
|
||||
return `${appId}.soundOn`;
|
||||
}
|
||||
|
||||
export function loadSoundEnabled(appId: string): boolean {
|
||||
try {
|
||||
return getBooleanSetting(soundKey(appId), true);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function saveSoundEnabled(appId: string, enabled: boolean): void {
|
||||
try {
|
||||
setBooleanSetting(soundKey(appId), enabled);
|
||||
} catch (error) {
|
||||
console.warn(`${appId} sound setting save failed: ${error}`);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { singlePlane, type Plane } from "../graphics/plane";
|
||||
import { GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "./gestures";
|
||||
import { GESTURE_LONG_PRESS, GESTURE_SHORT_THEN_LONG_PRESS, gestureHints, type InputEvent } from "./gestures";
|
||||
import { type Layer, LayerStack, noopLayerActions } from "./layers";
|
||||
import { CONTEXT_MENU_DIM, MenuLayer, type MenuItem, type MenuLayout } from "./menu";
|
||||
import type { WorkerAppReply } from "./shell/worker-window";
|
||||
@@ -27,8 +27,10 @@ export const WINDOW_MENU_LAYOUT: MenuLayout = {
|
||||
};
|
||||
|
||||
export class WindowMenuLayer extends MenuLayer {
|
||||
constructor(title: string | null, items: MenuItem[]) {
|
||||
super(title, items, WINDOW_MENU_LAYOUT);
|
||||
constructor(title: string | null, items: MenuItem[], holdToTalk = false) {
|
||||
super(title, items, holdToTalk
|
||||
? { ...WINDOW_MENU_LAYOUT, footer: gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "system menu"]]) }
|
||||
: WINDOW_MENU_LAYOUT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
export const FACECLAW_VERSION = "0.6.5";
|
||||
export const FACECLAW_VERSION = "0.7.0";
|
||||
export const USER_AGENT = "Faceclaw/"+FACECLAW_VERSION;
|
||||
Reference in new issue
Block a user