Merge main into ios-port and update the iOS CFW transport

This commit is contained in:
James Babcock committed 2026-09-17 23:33:51 -07:00
commit e8e144f3f2
156 files changed
+10528 -1679

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
import { GrayImage, type UiFont } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models";
import type { AssistantTranscriptEntry } from "../../assistant/session";
import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations";
import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, gestureHints, type InputEvent } from "../../ui/gestures";
import type { Layer, LayerContext } from "../../ui/layers";
import type { MenuItem } from "../../ui/menu";
import { WindowMenuLayer } from "../../ui/window-menu";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { VoiceDraft } from "./voice-draft";
class AiChatLayer implements Layer {
readonly acceptsDirectional = true;
private firstLine: number | null = null; // null follows the streaming tail
private maxFirstLine = 0;
private selectedId = "";
private requestRender = () => {};
private readonly wrappedEntries = new WeakMap<AssistantTranscriptEntry, {
font: UiFont; width: number; text: string; lines: string[];
}>();
readonly draft: VoiceDraft;
constructor(private readonly conversations: AssistantConversations, options: InProcessAppOptions) {
this.draft = new VoiceDraft(options.actions, () => shell.prepareChatVoiceCapture(),
() => this.requestRender(), (text) => this.send(text));
}
start(render: () => void): () => void {
this.requestRender = render;
return this.conversations.onChanged(() => render());
}
send(text: string): void {
this.firstLine = null;
shell.sendToAssistant(text, false);
}
paint(ctx: LayerContext): GrayImage {
const size = ctx.stack.getBaseSize();
const image = new GrayImage(size.width, size.height);
const font = getDefaultSmallFont();
const step = font.lineHeight + 3;
const left = 12;
const width = size.width - 28;
const record = this.conversations.current();
if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; }
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
const title = this.conversations.title();
image.drawText(font, left, 6, truncateText(font, title, width), 180);
const label = assistantBackendSetting.get() === "external" ? "External agent" :
`${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`;
const footer = wrapText(font, `${label} ${gestureHints([
[GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"],
])}`, width);
const footerY = size.height - step * footer.length;
const footerTop = footerY - step * 3 - 12;
const historyTop = step + 16;
const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step));
const lines: { text: string; color: number }[] = [];
for (const entry of transcript) {
if (!entry.text) continue;
let wrapped = this.wrappedEntries.get(entry);
if (!wrapped || wrapped.font !== font || wrapped.width !== width || wrapped.text !== entry.text) {
wrapped = { font, width, text: entry.text,
lines: wrapText(font, `${entry.role === "user" ? "You" : "AI"}: ${entry.text}`, width - 8) };
this.wrappedEntries.set(entry, wrapped);
}
for (const text of wrapped.lines) {
lines.push({ text, color: entry.role === "user" ? 175 : 245 });
}
lines.push({ text: "", color: 0 });
}
if (!lines.length) lines.push({ text: "Hold to talk to your assistant.", color: 175 },
{ text: "Voice and chat share this conversation.", color: 150 });
this.maxFirstLine = Math.max(0, lines.length - visible);
const first = this.firstLine === null ? this.maxFirstLine : Math.min(this.firstLine, this.maxFirstLine);
for (let row = 0; row < visible; row++) {
const line = lines[first + row];
if (line) image.drawText(font, left, historyTop + row * step, line.text, line.color);
}
if (this.maxFirstLine > 0) {
const track = visible * step;
const thumb = Math.max(8, Math.floor(track * visible / lines.length));
image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110);
}
image.fillRect(left, footerTop, width, 1, 65);
const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status;
if (state) image.drawText(font, left, footerTop + 5, truncateText(font, state, width), 160);
const pending = wrapText(font, this.draft.text || (this.draft.active ? "Listening..." : "Hold to speak; release to send"), width);
pending.slice(-2).forEach((text, i) => image.drawText(font, left, footerTop + step * (i + 1) + 5, text, 220));
footer.forEach((text, i) => image.drawText(font, left + width - font.measureText(text), footerY + i * step, text, 120));
return image;
}
handleInput(event: InputEvent): void {
switch (event.type) {
case "scroll-up": case "swipe-up":
this.firstLine = Math.max(0, (this.firstLine ?? this.maxFirstLine) - 3); return;
case "scroll-down": case "swipe-down": {
const next = (this.firstLine ?? this.maxFirstLine) + 3;
this.firstLine = next >= this.maxFirstLine ? null : next; return;
}
case "long-press":
if (!this.conversations.current().session?.isTurnActive()) {
if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; }
void this.draft.start();
}
return;
case "long-press-release": this.draft.release(); return;
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
}
}
menuItems(): MenuItem[] {
const record = this.conversations.current();
const busy = () => this.draft.active || Boolean(record.session?.isTurnActive());
const external = assistantBackendSetting.get() === "external";
const submenu = (ctx: LayerContext, title: string, items: MenuItem[]) => ctx.stack.push(new WindowMenuLayer(title, items, true));
return [
...(external ? [{ label: "Sessions and model managed by bridge", disabled: true, onSelect: () => {} }] : []),
{ label: "New session", disabled: () => external || busy(), onSelect: (ctx) => {
if (this.conversations.create()) ctx.stack.clearToBase();
} },
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
[...this.conversations.list()].reverse().map((item) => ({
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
}))) },
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
ASSISTANT_MODEL_VALUES.map((model) => ({
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
disabled: () => !this.conversations.available(model, record.reasoning),
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
}))) },
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
return external || busy() || !llm?.effort;
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
}))) },
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
record.session?.cancel(); ctx.stack.clearToBase();
} }] : []),
];
}
}
export function createAiChatWindow(options: InProcessAppOptions): InProcessWindow {
const layer = new AiChatLayer(shell.getAssistantConversations(), options);
let unsubscribe = () => {};
const app = createInProcessWindow({
...options,
appId: "ai-chat", windowId: "ai-chat", title: "AI Chat", iconLetter: "AI", icon: "message-circle", closeable: true,
baseLayer: layer, holdToTalk: true,
isVoiceCapturing: () => layer.draft.active,
onSystemMenuOpened: () => layer.draft.cancel(),
onAppMenuOpened: () => layer.draft.cancel(),
setScreenOn: (on) => { if (!on) layer.draft.cancel(); },
setSurfaceVisible: (visible) => { if (!visible) layer.draft.cancel(); options.setSurfaceVisible(visible); },
menuItems: () => layer.menuItems(),
receiveTextInput: (text) => { if (!layer.draft.active) layer.send(text); },
onClosed: () => { unsubscribe(); layer.draft.cancel(); options.onClosed(); },
});
unsubscribe = layer.start(app.requestRender);
return app;
}
+10
View File
@@ -0,0 +1,10 @@
import type { AppDefinition } from "../app-definition";
import { createAiChatWindow } from "./ai-chat-app";
const aiChatApp: AppDefinition = {
appId: "ai-chat",
title: "AI Chat",
icon: "message-circle",
launch: (ctx) => ctx.launchInProcessApp("ai-chat", "window:ai-chat", createAiChatWindow),
};
export default aiChatApp;
+136
View File
@@ -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();
}
}
+6
View File
@@ -1,4 +1,5 @@
import { type AppDefinition } from "./app-definition";
import aiChatApp from "./ai-chat";
import launcherApp from "./launcher";
import timerApp from "./timer";
import calculatorApp from "./calculator";
@@ -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,
];
+33
View File
@@ -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 = {
+31 -8
View File
@@ -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. */
+2 -2
View File
@@ -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
View File
@@ -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);
}
+352
View File
@@ -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);
}
+212
View File
@@ -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;
}
}
}
+7
View File
@@ -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,
);
+1 -2
View File
@@ -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;
}
+777
View File
@@ -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}`);
}
}
+135
View File
@@ -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;
}
+18
View File
@@ -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;
+108 -22
View File
@@ -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,
);
}
}
+127
View File
@@ -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);
}
}
+358
View File
@@ -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;
}
+32
View File
@@ -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;
+122
View File
@@ -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;
}
+43
View File
@@ -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);
}
}
}
+26
View File
@@ -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,
);
}
}
+233
View File
@@ -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;
}
}
}
}
+1
View File
@@ -16,6 +16,7 @@ const DEFAULT_ASSIGNMENTS: Record<string, string> = {
freecell: "Games",
minesweeper: "Games",
pinball: "Games",
flappy: "Games",
calculator: "Prototypes",
microphones: "Prototypes",
nightscout: "Prototypes",
+7 -19
View File
@@ -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 {
+2 -3
View File
@@ -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,
+4 -1
View File
@@ -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;
+9 -8
View File
@@ -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",
+52 -7
View File
@@ -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":
+1 -1
View File
@@ -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++) {
+85 -10
View File
@@ -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;
+6 -6
View File
@@ -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);
}
+26 -60
View File
@@ -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);
}
}
+100
View File
@@ -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);
}
+83 -17
View File
@@ -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) {
+5
View File
@@ -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) {
+6 -2
View File
@@ -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);