forked from Blendman974/faceclaw
Merge branch 'main' into ring-health-fixes
This commit is contained in:
commit
4f8c082b52
368 files changed
+26238
-3401
No files matched your search
@@ -39,7 +39,7 @@ export class VoiceDraft {
|
||||
this.phase = "listening";
|
||||
this.status = "Listening... release to send";
|
||||
this.ownsVoiceActivity = true;
|
||||
voiceActivity.setActive(true);
|
||||
voiceActivity.setActive(true, "voice-draft");
|
||||
this.unsubscribe.push(voiceControlBridge.onTranscript((event) => {
|
||||
if (!this.active || generation !== this.generation) return;
|
||||
if (event.isFinal) {
|
||||
@@ -129,7 +129,7 @@ export class VoiceDraft {
|
||||
}
|
||||
if (this.ownsVoiceActivity) {
|
||||
this.ownsVoiceActivity = false;
|
||||
voiceActivity.setActive(false);
|
||||
voiceActivity.setActive(false, "voice-draft");
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import musicApp from "./music";
|
||||
import nightscoutApp from "./nightscout";
|
||||
import healthApp from "./health";
|
||||
import transcribeApp from "./transcribe";
|
||||
import translateApp from "./translate";
|
||||
import teleprompterApp from "./teleprompter";
|
||||
import microphonesApp from "./microphones";
|
||||
import notificationsApp from "./notifications";
|
||||
@@ -27,6 +28,7 @@ import developerApp from "./developer";
|
||||
import evenhubApp from "./evenhub";
|
||||
import glanceboardApp from "./glanceboard";
|
||||
import settingsApp from "./settings";
|
||||
import onboardingApp from "./onboarding";
|
||||
|
||||
/**
|
||||
* Every app, in launcher-grid order (the launcher itself is first but hidden
|
||||
@@ -45,6 +47,7 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
nightscoutApp,
|
||||
healthApp,
|
||||
transcribeApp,
|
||||
translateApp,
|
||||
teleprompterApp,
|
||||
microphonesApp,
|
||||
notificationsApp,
|
||||
@@ -62,4 +65,5 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
evenhubApp,
|
||||
glanceboardApp,
|
||||
settingsApp,
|
||||
onboardingApp,
|
||||
];
|
||||
@@ -4,7 +4,7 @@ import { type IconName } from "../graphics/icons";
|
||||
import { type LayerActions } from "../ui/layers";
|
||||
import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window";
|
||||
import { WorkerAppHost } from "../ui/shell/worker-window";
|
||||
import { shell } from "../ui/shell/shell";
|
||||
import { shell, type TrayWidget } from "../ui/shell/shell";
|
||||
|
||||
/**
|
||||
* An app, as seen from outside while it isn't running. Each app directory's
|
||||
@@ -96,6 +96,17 @@ export type AppContext = {
|
||||
surfaceId: string,
|
||||
create: (options: InProcessAppOptions) => InProcessWindow,
|
||||
) => Promise<void>;
|
||||
/**
|
||||
* Open a main-thread singleton window in the background (neither
|
||||
* foregrounded nor focused) unless it is open already; `opened` says this
|
||||
* call opened it. For windows the shell brings forward itself, such as an
|
||||
* app widget's (see inProcessTrayWidget). Hosts that leave it out can't.
|
||||
*/
|
||||
openInProcessAppInBackground?: (
|
||||
windowId: string,
|
||||
surfaceId: string,
|
||||
create: (options: InProcessAppOptions) => InProcessWindow,
|
||||
) => { window: InProcessWindow; opened: boolean };
|
||||
/** Get this app's shared worker host, spawning the worker on first use. */
|
||||
ensureWorkerHost: (createWorker: () => Worker) => WorkerAppHost;
|
||||
/** Submit a window surface's painted frame, as planes (windows created outside launchInProcessApp). */
|
||||
@@ -120,6 +131,32 @@ export type TextEditorHost = {
|
||||
requestRender: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* A tray widget (see Shell.setTrayIcon) whose window is a main-thread
|
||||
* singleton app window, opened in the background while the switcher's
|
||||
* selection is on it; undefined on hosts that can't do that. `show`, if
|
||||
* given, runs each time the selection moves onto the widget, once the
|
||||
* window is open, to bring what the widget stands for on screen in it (the
|
||||
* shell repaints the window after).
|
||||
*/
|
||||
export function inProcessTrayWidget(
|
||||
ctx: AppContext,
|
||||
windowId: string,
|
||||
surfaceId: string,
|
||||
create: (options: InProcessAppOptions) => InProcessWindow,
|
||||
show?: () => void,
|
||||
): TrayWidget | undefined {
|
||||
const open = ctx.openInProcessAppInBackground;
|
||||
if (!open) return undefined;
|
||||
return {
|
||||
openWindow: () => {
|
||||
const { window, opened } = open(windowId, surfaceId, create);
|
||||
show?.();
|
||||
return { window: window.window, opened };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch-or-focus flow shared by the worker-hosted apps: focus the app's
|
||||
* existing window if one is open, otherwise open a window in the app's shared
|
||||
|
||||
@@ -49,7 +49,7 @@ const PANEL_X = 230;
|
||||
/**
|
||||
* Height the layout above is drawn for: a min-height window's viewport under
|
||||
* the standard top bar. A taller viewport (the status bar folded into the
|
||||
* switcher row, a full-panel display) centers it vertically.
|
||||
* switcher row, a Tall or Full Screen display) centers it vertically.
|
||||
*/
|
||||
const LAYOUT_HEIGHT = 260;
|
||||
|
||||
@@ -290,8 +290,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
updateTickTimer(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 });
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "resize-window":
|
||||
case "text-input":
|
||||
case "tool-call":
|
||||
break;
|
||||
|
||||
@@ -116,6 +116,9 @@ export function createCalculatorAppWindow(options: InProcessAppOptions): InProce
|
||||
},
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
// Closing keeps the base layer (clearToBase), so stop its continuous
|
||||
// listening explicitly; otherwise the mic capture outlives the window.
|
||||
layer.onRemoved();
|
||||
toolRegistry.removeAppTools(CALCULATOR_WINDOW_ID);
|
||||
options.onClosed();
|
||||
},
|
||||
|
||||
@@ -16,6 +16,7 @@ import { type Layer, type LayerActions, type LayerContext } from "../../ui/layer
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
|
||||
import { voiceActivity } from "../../ui/shell/voice-activity";
|
||||
import { type MathBranchOutcome, MathCoordinator } from "./math/coordinator";
|
||||
import { type LongRunOffer, type LongRunProgress, offerGlassesPrompt, progressText } from "./math/workload";
|
||||
import { plain } from "./math/text-renderer";
|
||||
@@ -56,6 +57,11 @@ export const calculatorDegreesSetting = new ConfigSettingBoolean({
|
||||
description: "Evaluate sin/cos/tan in degrees instead of radians.",
|
||||
});
|
||||
|
||||
// The calculator's id for shared continuous capture, and its claim on the mic
|
||||
// while a dictation is armed.
|
||||
const CONTINUOUS_CLIENT = "calculator";
|
||||
const DICTATION_HOLDER = "calculator-dictation";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Layer
|
||||
|
||||
@@ -131,22 +137,22 @@ export class CalculatorLayer implements Layer {
|
||||
if (this.listening === "continuous") {
|
||||
if (!this.continuousActive) {
|
||||
this.continuousActive = true;
|
||||
void this.actions.startContinuousVoiceCapture();
|
||||
void this.actions.startContinuousVoiceCapture(CONTINUOUS_CLIENT);
|
||||
}
|
||||
} else if (this.continuousActive) {
|
||||
this.continuousActive = false;
|
||||
void this.actions.stopContinuousVoiceCapture();
|
||||
void this.actions.stopContinuousVoiceCapture(CONTINUOUS_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
private stopListening(): void {
|
||||
if (this.dictating) {
|
||||
this.dictating = false;
|
||||
void this.actions.stopVoiceCapture();
|
||||
this.setDictating(false);
|
||||
}
|
||||
if (this.continuousActive) {
|
||||
this.continuousActive = false;
|
||||
void this.actions.stopContinuousVoiceCapture();
|
||||
void this.actions.stopContinuousVoiceCapture(CONTINUOUS_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,15 +254,25 @@ export class CalculatorLayer implements Layer {
|
||||
beginDictation(): void {
|
||||
if (this.textInput) { void this.textInput(); return; }
|
||||
if (this.dictating) return;
|
||||
this.dictating = true;
|
||||
this.setDictating(true);
|
||||
void this.actions.startVoiceCapture(true);
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
/**
|
||||
* Arm or disarm dictation, holding the mic claim for it as the voice dialog
|
||||
* does: the Microphones app and EvenHub mic apps stand down meanwhile (in
|
||||
* 4-mic mode their stream would otherwise reach the recognizer).
|
||||
*/
|
||||
private setDictating(dictating: boolean): void {
|
||||
this.dictating = dictating;
|
||||
voiceActivity.setActive(dictating, DICTATION_HOLDER);
|
||||
}
|
||||
|
||||
cancelDictation(): void {
|
||||
if (!this.dictating) return;
|
||||
this.dictating = false;
|
||||
void this.actions.stopVoiceCapture();
|
||||
this.setDictating(false);
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
@@ -277,8 +293,8 @@ export class CalculatorLayer implements Layer {
|
||||
const continuous = this.continuousActive && this.foreground;
|
||||
if (!armed && !continuous) return;
|
||||
if (armed) {
|
||||
this.dictating = false;
|
||||
void this.actions.stopVoiceCapture();
|
||||
this.setDictating(false);
|
||||
}
|
||||
// A standing offer or a run in flight owns the next yes/no said in the
|
||||
// room ("keep going", "never mind", "stop").
|
||||
|
||||
@@ -23,7 +23,14 @@ import {
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||
import {
|
||||
cardinalDirection,
|
||||
createCompassBackground,
|
||||
drawCompassRose,
|
||||
layoutCompassRose,
|
||||
magneticAccuracyDots,
|
||||
TICK_HEIGHT,
|
||||
} from "./compass-rose";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||
@@ -277,14 +284,7 @@ function frameStatusText(): string {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The firmware's magnetometer calibration readiness (0-3) as filled/empty
|
||||
* dots, e.g. "Calibration: ●●○" for 2/3. This is the glasses' own sensor
|
||||
* calibration, not the wearer-fit offset set on the calibration screen.
|
||||
*/
|
||||
/** The firmware's magnetometer calibration readiness, e.g. "Calibration: ●●○" for 2/3. */
|
||||
function magneticAccuracyText(diagnostics: CompassDiagnostics | null): string {
|
||||
const accuracy = diagnostics && (diagnostics.flags & 0x80) ? diagnostics.magneticAccuracy : -1;
|
||||
if (accuracy < 0) return "Calibration: --";
|
||||
const filled = Math.min(3, accuracy);
|
||||
return `Calibration: ${"●".repeat(filled)}${"○".repeat(3 - filled)}`;
|
||||
return `Calibration: ${magneticAccuracyDots(diagnostics) ?? "--"}`;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
* own the magnetometer lifecycle and the readout text around it.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { type CompassDiagnostics } from "../../native/compass-types";
|
||||
import { normalizeHeading } from "./calibration";
|
||||
|
||||
/**
|
||||
@@ -102,6 +103,19 @@ export function cardinalDirection(heading: number): string {
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* The firmware's magnetometer calibration readiness (0-3) as filled/empty
|
||||
* dots, e.g. "●●○" for 2/3, or null when the firmware doesn't report it. This
|
||||
* is the glasses' own sensor calibration, not the wearer-fit offset set on the
|
||||
* calibration screen.
|
||||
*/
|
||||
export function magneticAccuracyDots(diagnostics: CompassDiagnostics | null): string | null {
|
||||
const accuracy = diagnostics && (diagnostics.flags & 0x80) ? diagnostics.magneticAccuracy : -1;
|
||||
if (accuracy < 0) return null;
|
||||
const filled = Math.min(3, accuracy);
|
||||
return "●".repeat(filled) + "○".repeat(3 - filled);
|
||||
}
|
||||
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
|
||||
@@ -65,6 +65,14 @@ type ArmStats = {
|
||||
statusAtMs: number | null;
|
||||
};
|
||||
|
||||
/** Drop arrivals older than the rate window. */
|
||||
function trimRecent(stats: ArmStats, nowMs: number): void {
|
||||
const cutoff = nowMs - RATE_WINDOW_MS;
|
||||
let drop = 0;
|
||||
while (drop < stats.recent.length && stats.recent[drop]!.wallMs < cutoff) drop++;
|
||||
if (drop > 0) stats.recent.splice(0, drop);
|
||||
}
|
||||
|
||||
function emptyStats(): ArmStats {
|
||||
return {
|
||||
packets: 0,
|
||||
@@ -169,6 +177,8 @@ export class MicrophonesMonitorLayer implements Layer {
|
||||
this.advanceBuckets(nowMs);
|
||||
stats.packets++;
|
||||
stats.buckets[BUCKETS - 1]!++;
|
||||
// Trim here too: painting (the other trim) stops while the page is hidden.
|
||||
trimRecent(stats, nowMs);
|
||||
stats.recent.push({ wallMs: nowMs, arrivalMs, bytes: data.length });
|
||||
stats.lastArrivalWallMs = nowMs;
|
||||
|
||||
@@ -268,8 +278,7 @@ export class MicrophonesMonitorLayer implements Layer {
|
||||
const font = getDefaultSmallFont();
|
||||
const line = font.lineHeight;
|
||||
const stats = this.stats[arm];
|
||||
const cutoff = nowMs - RATE_WINDOW_MS;
|
||||
while (stats.recent.length > 0 && stats.recent[0]!.wallMs < cutoff) stats.recent.shift();
|
||||
trimRecent(stats, nowMs);
|
||||
const rate = (stats.recent.length * 1000) / RATE_WINDOW_MS;
|
||||
const byteRate = stats.recent.reduce((sum, arrival) => sum + arrival.bytes, 0) / RATE_WINDOW_MS;
|
||||
let maxGap = 0;
|
||||
|
||||
@@ -14,7 +14,7 @@ import { isCompassCalibrated } from "../compass/calibration";
|
||||
import { refreshDeclination } from "../compass/declination";
|
||||
import { resolveHeading } from "../compass/heading";
|
||||
|
||||
/** Wire shape of the `compass` extension event (faceclaw-extensions `CompassReading`). */
|
||||
/** Wire shape of the `compass` extension event (@faceclaw/evenhub-extensions `CompassReading`). */
|
||||
export type EvenHubCompassReading = {
|
||||
headingDegrees: number;
|
||||
magneticHeadingDegrees: number;
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
*
|
||||
* Text uses Even's extracted 20px firmware font (via EvenHubFont), measured
|
||||
* and wrapped with @evenrealities/pretext, so glyph widths and line breaks
|
||||
* match what apps expect.
|
||||
* match what apps expect. Extended layouts may pick other fonts per container
|
||||
* (fonts.ts), which wrap with Faceclaw's own text wrapping.
|
||||
*
|
||||
* Known deviations from stock, acceptable for now:
|
||||
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
|
||||
@@ -13,7 +14,7 @@
|
||||
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { EvenHubFont } from "../../graphics/evenhub-font";
|
||||
import { containerFace } from "./fonts";
|
||||
import {
|
||||
type EvenHubContainer,
|
||||
type EvenHubImageContainer,
|
||||
@@ -47,14 +48,11 @@ function paintTextContainer(image: GrayImage, container: EvenHubTextContainer):
|
||||
// the container's border at full brightness regardless.
|
||||
paintBorder(image, container);
|
||||
const inset = container.borderWidth + container.paddingLength;
|
||||
EvenHubFont.get().drawTextWrapped(
|
||||
image,
|
||||
container.x + inset,
|
||||
container.y + inset,
|
||||
Math.max(1, container.width - 2 * inset),
|
||||
container.content,
|
||||
textGrey(container.textColor),
|
||||
);
|
||||
const face = containerFace(container.font);
|
||||
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * inset));
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
face.drawText(image, container.x + inset, container.y + inset + i * face.lineHeight, lines[i]!, textGrey(container.textColor));
|
||||
}
|
||||
}
|
||||
|
||||
function paintBorder(
|
||||
@@ -94,7 +92,7 @@ function paintImageContainer(image: GrayImage, container: EvenHubImageContainer)
|
||||
|
||||
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
|
||||
paintBorder(image, container);
|
||||
paintListItems(image, container, EvenHubFont.get(), focused);
|
||||
paintListItems(image, container, containerFace(container.font), focused);
|
||||
}
|
||||
|
||||
/** Bottom-to-top paint order for a page's containers. */
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
* (Container_ID), and numbers sometimes arrive as strings, so all field
|
||||
* reads go through loose key matching (the SDK's pickLoose behavior).
|
||||
*/
|
||||
import { mergeFontSpecs, parseFontSpec, type EvenHubFontSpec } from "./font-spec";
|
||||
|
||||
export type EvenHubTextContainer = {
|
||||
kind: "text";
|
||||
@@ -24,6 +25,8 @@ export type EvenHubTextContainer = {
|
||||
content: string;
|
||||
/** SDK 0.0.14 textColor: brightness level 0..4; undefined means the device default (4). */
|
||||
textColor: number | undefined;
|
||||
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
|
||||
font: EvenHubFontSpec | undefined;
|
||||
};
|
||||
|
||||
export type EvenHubImageContainer = {
|
||||
@@ -63,6 +66,8 @@ export type EvenHubListContainer = {
|
||||
selectBorder: boolean;
|
||||
/** Selection is host-local: scroll moves it with no app round-trip. */
|
||||
selectedIndex: number;
|
||||
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
|
||||
font: EvenHubFontSpec | undefined;
|
||||
};
|
||||
|
||||
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
|
||||
@@ -130,6 +135,17 @@ export function readString(obj: Record<string, unknown>, key: string, fallback:
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* A 0/1 protobuf flag. Stock apps send numbers; extension layouts may send
|
||||
* booleans (the extension types allow both), and numeric strings show up in
|
||||
* the wild.
|
||||
*/
|
||||
function readFlag(obj: Record<string, unknown>, key: string): boolean {
|
||||
const value = pickLoose(obj, key);
|
||||
if (typeof value === "boolean") return value;
|
||||
return readNumber(obj, key, 0) !== 0;
|
||||
}
|
||||
|
||||
export function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
@@ -138,7 +154,12 @@ function readBorderWidth(json: Record<string, unknown>): number {
|
||||
return Math.max(0, Math.min(MAX_BORDER_WIDTH, readNumber(json, "borderWidth", 0)));
|
||||
}
|
||||
|
||||
function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer {
|
||||
/** A container's font: its own `font` over the layout-wide default. */
|
||||
function readFont(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubFontSpec | undefined {
|
||||
return mergeFontSpecs(layoutFont, parseFontSpec(pickLoose(json, "font")));
|
||||
}
|
||||
|
||||
function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubTextContainer {
|
||||
return {
|
||||
kind: "text",
|
||||
id: readNumber(json, "containerID", 0),
|
||||
@@ -150,11 +171,12 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
|
||||
borderWidth: readBorderWidth(json),
|
||||
borderRadius: readNumber(json, "borderRadius", 0),
|
||||
paddingLength: readNumber(json, "paddingLength", 0),
|
||||
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
|
||||
isEventCapture: readFlag(json, "isEventCapture"),
|
||||
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
|
||||
preserve: readNumber(json, "preserve", 0) !== 0,
|
||||
preserve: readFlag(json, "preserve"),
|
||||
content: readString(json, "content", ""),
|
||||
textColor: readTextBrightness(json),
|
||||
font: readFont(json, layoutFont),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -220,14 +242,14 @@ function parseImageContainer(json: Record<string, unknown>): EvenHubImageContain
|
||||
width: readNumber(json, "width", 0),
|
||||
height: readNumber(json, "height", 0),
|
||||
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
|
||||
preserve: readNumber(json, "preserve", 0) !== 0,
|
||||
preserve: readFlag(json, "preserve"),
|
||||
pixels: null,
|
||||
pixelsWidth: 0,
|
||||
pixelsHeight: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function parseListContainer(json: Record<string, unknown>): EvenHubListContainer {
|
||||
function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubListContainer {
|
||||
const itemContainer = asRecord(pickLoose(json, "itemContainer"));
|
||||
const itemNamesRaw = pickLoose(itemContainer, "itemName");
|
||||
const itemNames = Array.isArray(itemNamesRaw) ? itemNamesRaw.map(String) : [];
|
||||
@@ -242,13 +264,14 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
|
||||
borderWidth: readBorderWidth(json),
|
||||
borderRadius: readNumber(json, "borderRadius", 0),
|
||||
paddingLength: readNumber(json, "paddingLength", 0),
|
||||
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
|
||||
isEventCapture: readFlag(json, "isEventCapture"),
|
||||
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
|
||||
preserve: readNumber(json, "preserve", 0) !== 0,
|
||||
preserve: readFlag(json, "preserve"),
|
||||
itemNames,
|
||||
itemWidth: readNumber(itemContainer, "itemWidth", 0),
|
||||
selectBorder: readNumber(itemContainer, "isItemSelectBorderEn", 0) !== 0,
|
||||
selectBorder: readFlag(itemContainer, "isItemSelectBorderEn"),
|
||||
selectedIndex: 0,
|
||||
font: readFont(json, layoutFont),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -263,9 +286,11 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
|
||||
*/
|
||||
export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
const containers: EvenHubContainer[] = [];
|
||||
// Extended layouts may set a default font for every text and list container.
|
||||
const layoutFont = parseFontSpec(pickLoose(data, "font"));
|
||||
const lists = pickLoose(data, "listObject");
|
||||
if (Array.isArray(lists)) {
|
||||
for (const item of lists) containers.push(parseListContainer(asRecord(item)));
|
||||
for (const item of lists) containers.push(parseListContainer(asRecord(item), layoutFont));
|
||||
}
|
||||
const images = pickLoose(data, "imageObject");
|
||||
if (Array.isArray(images)) {
|
||||
@@ -273,7 +298,7 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
|
||||
}
|
||||
const texts = pickLoose(data, "textObject");
|
||||
if (Array.isArray(texts)) {
|
||||
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
|
||||
for (const item of texts) containers.push(parseTextContainer(asRecord(item), layoutFont));
|
||||
}
|
||||
return { containers, menuItems: parseMenuItems(data) };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* The live data behind an EvenHub app's device status: glasses presence (fed
|
||||
* by the connection controller), the headset battery (from the shell's top
|
||||
* bar state), and the paired glasses' serial.
|
||||
*/
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { getGlassesPresence, onGlassesPresenceChanged } from "../../g2/glasses-presence";
|
||||
import { loadPairedGlassesIdentity } from "../../g2/device-addresses";
|
||||
import { type EvenHubDeviceStatusSource } from "./device-status";
|
||||
|
||||
export const liveDeviceStatusSource: EvenHubDeviceStatusSource = {
|
||||
read: () => {
|
||||
const presence = getGlassesPresence();
|
||||
return {
|
||||
serial: loadPairedGlassesIdentity()?.serial ?? null,
|
||||
connected: presence.connected,
|
||||
worn: presence.worn,
|
||||
battery: shell.getBatteryLevels().headset,
|
||||
charging: presence.charging,
|
||||
};
|
||||
},
|
||||
subscribe: (listener) => {
|
||||
const offs = [onGlassesPresenceChanged(listener), shell.onBatteryLevelsChanged(listener)];
|
||||
return () => {
|
||||
for (const off of offs) off();
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* The stock SDK's DeviceStatus (getGlassesInfo().status and the
|
||||
* deviceStatusChanged push), built from what Faceclaw actually knows about
|
||||
* the glasses. Pure, so it can be tested without NativeScript.
|
||||
*/
|
||||
|
||||
/** Used when no glasses have been paired (preview-only mode). */
|
||||
export const PLACEHOLDER_SERIAL = "FACECLAW-G2";
|
||||
|
||||
/** Wire shape of the SDK's DeviceStatus. Unknown fields are omitted rather than guessed. */
|
||||
export type EvenHubDeviceStatus = {
|
||||
sn: string;
|
||||
connectType: "connected" | "disconnected";
|
||||
isWearing?: boolean;
|
||||
batteryLevel?: number;
|
||||
isCharging?: boolean;
|
||||
isInCase?: boolean;
|
||||
};
|
||||
|
||||
export type EvenHubDeviceStatusInput = {
|
||||
/** The paired glasses' serial number, or null when none are paired. */
|
||||
serial: string | null;
|
||||
connected: boolean;
|
||||
/** null until the firmware reports a wear state this session. */
|
||||
worn: boolean | null;
|
||||
/** Last known headset battery percentage, or null if never reported. */
|
||||
battery: number | null;
|
||||
charging: boolean;
|
||||
};
|
||||
|
||||
/** Where a session reads the device status from (live in the app; fixed in tests). */
|
||||
export type EvenHubDeviceStatusSource = {
|
||||
read(): EvenHubDeviceStatusInput;
|
||||
/** Call `listener` after anything read() reports may have changed. Returns an unsubscribe function. */
|
||||
subscribe(listener: () => void): () => void;
|
||||
};
|
||||
|
||||
/** No glasses: what a session reports when it isn't given a live source. */
|
||||
export const OFFLINE_DEVICE_STATUS_SOURCE: EvenHubDeviceStatusSource = {
|
||||
read: () => ({ serial: null, connected: false, worn: null, battery: null, charging: false }),
|
||||
subscribe: () => () => {},
|
||||
};
|
||||
|
||||
export function buildEvenHubDeviceStatus(input: EvenHubDeviceStatusInput): EvenHubDeviceStatus {
|
||||
const status: EvenHubDeviceStatus = {
|
||||
// The SDK only applies a status push whose sn matches getGlassesInfo's,
|
||||
// so both must come from here.
|
||||
sn: input.serial?.trim() || PLACEHOLDER_SERIAL,
|
||||
connectType: input.connected ? "connected" : "disconnected",
|
||||
isCharging: input.charging,
|
||||
// The G2 only charges in its case, and Faceclaw has no separate
|
||||
// in-case report.
|
||||
isInCase: input.charging,
|
||||
};
|
||||
if (input.worn !== null) status.isWearing = input.worn;
|
||||
if (input.battery !== null) status.batteryLevel = input.battery;
|
||||
return status;
|
||||
}
|
||||
|
||||
export function sameDeviceStatus(a: EvenHubDeviceStatus, b: EvenHubDeviceStatus): boolean {
|
||||
return a.sn === b.sn && a.connectType === b.connectType && a.isWearing === b.isWearing &&
|
||||
a.batteryLevel === b.batteryLevel && a.isCharging === b.isCharging && a.isInCase === b.isInCase;
|
||||
}
|
||||
@@ -235,7 +235,7 @@ export class EvenHubApiClient {
|
||||
const token = options.token ?? "";
|
||||
const headers: Record<string, string> = {
|
||||
common,
|
||||
sign: hmacSign(signingParts(method, path, common, query, token, body)),
|
||||
sign: hmacSign(signingParts(method, path, common, signedQuery(options.query ?? {}), token, body)),
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (token) headers.token = token;
|
||||
@@ -266,7 +266,7 @@ export function signingParts(
|
||||
body = "",
|
||||
): string {
|
||||
const parts = [method.toUpperCase(), path, common];
|
||||
if (query) parts.push(normalizeQuery(query));
|
||||
if (query) parts.push(query);
|
||||
if (token) parts.push(token);
|
||||
if (body) parts.push(body);
|
||||
return parts.sort().join("\n");
|
||||
@@ -320,19 +320,26 @@ function buildCommon(version: 1 | 3): string {
|
||||
return Object.entries(fields).map(([key, value]) => `${formEncode(key)}=${formEncode(String(value))}`).join("&");
|
||||
}
|
||||
|
||||
function encodePairs(values: Record<string, string | number>): string {
|
||||
/** Query entries sorted by key in code-unit order (Dart's default String sort). */
|
||||
function sortedPairs(values: Record<string, string | number>): [string, string][] {
|
||||
return Object.entries(values)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([key, value]) => `${formEncode(key)}=${formEncode(String(value))}`)
|
||||
.join("&");
|
||||
.map(([key, value]): [string, string] => [key, String(value)])
|
||||
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
}
|
||||
|
||||
function normalizeQuery(query: string): string {
|
||||
return query
|
||||
.split("&")
|
||||
.filter(Boolean)
|
||||
.sort((a, b) => a.split("=", 1)[0]!.localeCompare(b.split("=", 1)[0]!))
|
||||
.join("&");
|
||||
function encodePairs(values: Record<string, string | number>): string {
|
||||
return sortedPairs(values).map(([key, value]) => `${formEncode(key)}=${formEncode(value)}`).join("&");
|
||||
}
|
||||
|
||||
/**
|
||||
* The query as it participates in signing: sorted raw `key=value` pairs, NOT
|
||||
* URL-encoded (SignatureUtil.calculateSignature in the official 2.2.8 Dart
|
||||
* image). The server signs the decoded parameters, so signing the encoded
|
||||
* form only matched when encoding was a no-op (a search for "two words" was
|
||||
* rejected with HTTP 406).
|
||||
*/
|
||||
function signedQuery(values: Record<string, string | number>): string {
|
||||
return sortedPairs(values).map(([key, value]) => `${key}=${value}`).join("&");
|
||||
}
|
||||
|
||||
function formEncode(value: string): string {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { EVENHUB_SCREEN_WIDTH } from "./compositor";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { directionalFallback, isDirectionalInput, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
@@ -18,13 +18,20 @@ import { EvenHubSession } from "./session";
|
||||
import { renderInstalledEvenHubIcon } from "./installed-apps";
|
||||
|
||||
class EvenHubAppLayer implements Layer {
|
||||
/**
|
||||
* Watch swipes become the usual stock gestures here rather than in the
|
||||
* stack's fallback, which labels them as ring input: an app reading the
|
||||
* extension's source field should see that they came from the watch.
|
||||
*/
|
||||
readonly acceptsDirectional = true;
|
||||
|
||||
constructor(private readonly session: EvenHubSession) {}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const size = ctx.stack.getBaseSize();
|
||||
// EvenHub apps lay out for the stock 576-wide surface. In the full-panel
|
||||
// display mode the window is 640 wide; paint at stock width and centre
|
||||
// it rather than leaving all the slack on the right.
|
||||
// EvenHub apps lay out for the stock 576-wide surface. In the Full
|
||||
// Screen display mode the window is 640 wide; paint at stock width and
|
||||
// centre it rather than leaving all the slack on the right.
|
||||
if (size.width > EVENHUB_SCREEN_WIDTH) {
|
||||
const content = this.session.paint({ width: EVENHUB_SCREEN_WIDTH, height: size.height }, ctx.stack.isFocused());
|
||||
const image = new GrayImage(size.width, size.height, 0);
|
||||
@@ -40,7 +47,7 @@ class EvenHubAppLayer implements Layer {
|
||||
// menu intercepts tap-then-hold), which is the guaranteed way out since
|
||||
// EvenHub apps own double-click. Tap-then-hold is still reported to the
|
||||
// app as LONG_PRESS_EVENT — see the handleInput wrapper below.
|
||||
this.session.handleGesture(event);
|
||||
this.session.handleGesture(isDirectionalInput(event) ? ({ ...directionalFallback(event), source: "watch" } as InputEvent) : event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +57,7 @@ export function createEvenHubWindow(
|
||||
session: EvenHubSession,
|
||||
options: InProcessAppOptions,
|
||||
onShowPhone: () => void,
|
||||
requestShellRender: () => void,
|
||||
): InProcessWindow {
|
||||
const fallbackIcon = windowIcon("package", session.manifest.name.charAt(0).toUpperCase() || "E");
|
||||
const created = createInProcessWindow({
|
||||
@@ -58,11 +66,14 @@ export function createEvenHubWindow(
|
||||
title: session.manifest.name,
|
||||
iconLetter: session.manifest.name.charAt(0).toUpperCase() || "E",
|
||||
closeable: true,
|
||||
// An icon the app set at runtime (extension setWindowIcon) wins over the
|
||||
// package's.
|
||||
drawIcon: makeImageWindowIcon(
|
||||
(size) => renderInstalledEvenHubIcon(session.manifest.packageId, size),
|
||||
(size) => session.renderWindowIcon(size) ?? renderInstalledEvenHubIcon(session.manifest.packageId, size),
|
||||
fallbackIcon,
|
||||
),
|
||||
heightMode: "medium",
|
||||
keepsScreenOn: () => session.keepsScreenOn(),
|
||||
// The window's context menu doubles as the OS contextual menu EvenHub
|
||||
// apps register with `menuObject` (SDK 0.0.14): the app's own entries come
|
||||
// first, then the host entries. Evaluated at open time, so a rebuild that
|
||||
@@ -99,6 +110,14 @@ export function createEvenHubWindow(
|
||||
// The extended layout uses the full 576x452 app area ("max"); stock apps
|
||||
// stay in the 576x288 band ("medium").
|
||||
setTallCanvas: (tall) => created.setHeightMode(tall ? "max" : "medium"),
|
||||
setAttention: (attention) => shell.setWindowAttention(windowId, attention),
|
||||
// Only while the app listens for text: otherwise the shell's Type Into App
|
||||
// destinations (voice dialog, phone keyboard, watch) would send into a void.
|
||||
setAcceptsTextInput: (accept) => {
|
||||
created.window.receiveTextInput = accept ? (text, textOptions) => session.deliverTextInput(text, textOptions) : undefined;
|
||||
},
|
||||
startVoiceInput: () => shell.foregroundWindow()?.windowId === windowId && shell.startVoiceInput(),
|
||||
iconChanged: requestShellRender,
|
||||
windowId,
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* Font options for EvenHub text and list containers (the extension layout's
|
||||
* `font: { family, size, weight }`): parsing, inheritance from the layout's
|
||||
* default, and choosing a face from what is installed. Pure, so it can be
|
||||
* tested without NativeScript; app/apps/evenhub/fonts.ts loads the result.
|
||||
*
|
||||
* A container with no font anywhere keeps the stock firmware font, exactly as
|
||||
* on stock. Naming any font option switches to Faceclaw's fonts, with the
|
||||
* defaults below filling the options left out.
|
||||
*/
|
||||
|
||||
/** A container's font request, after parsing. Absent fields take the defaults. */
|
||||
export type EvenHubFontSpec = {
|
||||
family?: string;
|
||||
size?: number;
|
||||
weight?: number;
|
||||
};
|
||||
|
||||
export const DEFAULT_FONT_FAMILY = "Roboto";
|
||||
/** The stock firmware font's size, so switching family alone keeps text about the same size. */
|
||||
export const DEFAULT_FONT_SIZE = 20;
|
||||
export const DEFAULT_FONT_WEIGHT = 400;
|
||||
export const MIN_FONT_SIZE = 6;
|
||||
/** Larger glyphs leave the glyph-cache path (255px limit), so text stops being cheap to send. */
|
||||
export const MAX_FONT_SIZE = 128;
|
||||
|
||||
/**
|
||||
* Parse a `font` object from a layout or container. Undefined when there is
|
||||
* none; unusable fields are dropped, so `{}` means "Faceclaw's default font".
|
||||
*/
|
||||
export function parseFontSpec(raw: unknown): EvenHubFontSpec | undefined {
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
||||
const record = raw as Record<string, unknown>;
|
||||
const spec: EvenHubFontSpec = {};
|
||||
if (typeof record.family === "string" && record.family.trim()) spec.family = record.family.trim();
|
||||
const size = typeof record.size === "string" ? Number(record.size) : record.size;
|
||||
if (typeof size === "number" && Number.isFinite(size)) {
|
||||
spec.size = Math.min(MAX_FONT_SIZE, Math.max(MIN_FONT_SIZE, Math.round(size)));
|
||||
}
|
||||
const weight = parseWeight(record.weight);
|
||||
if (weight !== undefined) spec.weight = weight;
|
||||
return spec;
|
||||
}
|
||||
|
||||
function parseWeight(raw: unknown): number | undefined {
|
||||
if (typeof raw === "string") {
|
||||
const keyword = raw.trim().toLowerCase();
|
||||
if (keyword === "normal") return 400;
|
||||
if (keyword === "bold") return 700;
|
||||
raw = Number(keyword);
|
||||
}
|
||||
if (typeof raw !== "number" || !Number.isFinite(raw)) return undefined;
|
||||
return Math.min(1000, Math.max(1, Math.round(raw)));
|
||||
}
|
||||
|
||||
/** A container's font: its own options over the layout default's, field by field. */
|
||||
export function mergeFontSpecs(
|
||||
layoutDefault: EvenHubFontSpec | undefined,
|
||||
own: EvenHubFontSpec | undefined,
|
||||
): EvenHubFontSpec | undefined {
|
||||
if (!layoutDefault) return own;
|
||||
if (!own) return layoutDefault;
|
||||
return { ...layoutDefault, ...own };
|
||||
}
|
||||
|
||||
/** One loadable face: a TTF file, a Terminus weight, or the stock font. */
|
||||
export type FontFaceCandidate = {
|
||||
/** Family as the font names itself (e.g. "Inter 18pt"). */
|
||||
family: string;
|
||||
/** 100..900. */
|
||||
weight: number;
|
||||
/** The sizes a bitmap face comes in; null for a scalable face. */
|
||||
sizes: readonly number[] | null;
|
||||
monospace: boolean;
|
||||
/** Identifies the face to the loader. */
|
||||
key: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* The family name apps use: an optical-size suffix ("Inter 18pt") is dropped,
|
||||
* since the bundled Inter is its 18pt cut and apps would ask for "Inter".
|
||||
*/
|
||||
export function displayFamily(family: string): string {
|
||||
return family.replace(/\s+\d+\s*pt$/i, "").trim() || family;
|
||||
}
|
||||
|
||||
function normalizeFamily(name: string): string {
|
||||
return name.toLowerCase().replace(/[^a-z0-9]/g, "");
|
||||
}
|
||||
|
||||
export function familyMatches(candidate: string, requested: string): boolean {
|
||||
const wanted = normalizeFamily(requested);
|
||||
return normalizeFamily(candidate) === wanted || normalizeFamily(displayFamily(candidate)) === wanted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the face and pixel size for a spec. An unknown family falls back to
|
||||
* DEFAULT_FONT_FAMILY; null when even that is missing (the caller then uses
|
||||
* the stock font). Weight follows CSS's font matching: 400–500 look up to
|
||||
* 500 first, lighter requests look lighter first, heavier ones heavier
|
||||
* first. A bitmap face takes its nearest size (the smaller on a tie).
|
||||
*/
|
||||
export function selectFontFace(
|
||||
spec: EvenHubFontSpec,
|
||||
candidates: readonly FontFaceCandidate[],
|
||||
): { face: FontFaceCandidate; size: number } | null {
|
||||
const family = spec.family ?? DEFAULT_FONT_FAMILY;
|
||||
let faces = candidates.filter((candidate) => familyMatches(candidate.family, family));
|
||||
if (faces.length === 0) faces = candidates.filter((candidate) => familyMatches(candidate.family, DEFAULT_FONT_FAMILY));
|
||||
if (faces.length === 0) return null;
|
||||
const desired = spec.weight ?? DEFAULT_FONT_WEIGHT;
|
||||
const face = [...faces].sort((a, b) => weightRank(a.weight, desired) - weightRank(b.weight, desired))[0]!;
|
||||
const requestedSize = spec.size ?? DEFAULT_FONT_SIZE;
|
||||
return { face, size: face.sizes ? nearestSize(face.sizes, requestedSize) : requestedSize };
|
||||
}
|
||||
|
||||
/** Lower is better: CSS font-weight matching as a sort key. */
|
||||
function weightRank(weight: number, desired: number): number {
|
||||
const distance = Math.abs(weight - desired);
|
||||
if (weight === desired) return 0;
|
||||
if (desired >= 400 && desired <= 500) {
|
||||
if (weight > desired && weight <= 500) return distance;
|
||||
if (weight < desired) return 1000 + distance;
|
||||
return 2000 + distance;
|
||||
}
|
||||
const preferLighter = desired < 400;
|
||||
return (weight < desired) === preferLighter ? distance : 1000 + distance;
|
||||
}
|
||||
|
||||
function nearestSize(sizes: readonly number[], requested: number): number {
|
||||
let best = sizes[0]!;
|
||||
for (const size of sizes) {
|
||||
const better = Math.abs(size - requested) < Math.abs(best - requested) ||
|
||||
(Math.abs(size - requested) === Math.abs(best - requested) && size < best);
|
||||
if (better) best = size;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** A family as the extension's getFonts() reports it. */
|
||||
export type FontFamilyInfo = {
|
||||
family: string;
|
||||
weights: number[];
|
||||
/** Bitmap families' sizes; absent for scalable families. */
|
||||
sizes?: number[];
|
||||
monospace: boolean;
|
||||
};
|
||||
|
||||
/** Group faces into families for getFonts(), sorted by name. */
|
||||
export function describeFontFamilies(candidates: readonly FontFaceCandidate[]): FontFamilyInfo[] {
|
||||
const families = new Map<string, FontFamilyInfo>();
|
||||
for (const candidate of candidates) {
|
||||
const name = displayFamily(candidate.family);
|
||||
const key = normalizeFamily(name);
|
||||
let info = families.get(key);
|
||||
if (!info) {
|
||||
info = { family: name, weights: [], monospace: candidate.monospace };
|
||||
families.set(key, info);
|
||||
}
|
||||
if (!info.weights.includes(candidate.weight)) info.weights.push(candidate.weight);
|
||||
if (candidate.sizes) info.sizes = Array.from(new Set([...(info.sizes ?? []), ...candidate.sizes]));
|
||||
info.monospace = info.monospace && candidate.monospace;
|
||||
}
|
||||
return Array.from(families.values())
|
||||
.map((info) => ({
|
||||
...info,
|
||||
weights: [...info.weights].sort((a, b) => a - b),
|
||||
...(info.sizes ? { sizes: [...info.sizes].sort((a, b) => a - b) } : {}),
|
||||
}))
|
||||
.sort((a, b) => a.family.localeCompare(b.family));
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Loads the fonts EvenHub text and list containers ask for (see font-spec.ts
|
||||
* for the matching rules): the stock firmware font, Terminus at its bitmap
|
||||
* sizes, and every installed TTF/OTF — the bundled Roboto, Inter, Montserrat
|
||||
* and Roboto Mono, plus any the user installed from the Files app.
|
||||
*/
|
||||
import { EvenHubFont } from "../../graphics/evenhub-font";
|
||||
import { getTerminusFont, TERMINUS_SIZES } from "../../graphics/bdffont";
|
||||
import { TtfFont } from "../../graphics/ttf-font";
|
||||
import { listInstalledFonts } from "../../graphics/installed-fonts";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import type { GrayImage, UiFont } from "../../graphics/image";
|
||||
import {
|
||||
describeFontFamilies,
|
||||
familyMatches,
|
||||
selectFontFace,
|
||||
type EvenHubFontSpec,
|
||||
type FontFaceCandidate,
|
||||
type FontFamilyInfo,
|
||||
} from "./font-spec";
|
||||
|
||||
/** What containers draw text with: the stock font's interface, which UiFonts are adapted to. */
|
||||
export type EvenHubTextFace = {
|
||||
readonly lineHeight: number;
|
||||
measureLine(text: string): number;
|
||||
wrap(text: string, maxWidth: number): string[];
|
||||
drawText(image: GrayImage, x: number, y: number, text: string, value?: number): void;
|
||||
};
|
||||
|
||||
class UiFontFace implements EvenHubTextFace {
|
||||
constructor(private readonly font: UiFont) {}
|
||||
|
||||
get lineHeight(): number {
|
||||
return this.font.lineHeight;
|
||||
}
|
||||
|
||||
measureLine(text: string): number {
|
||||
return this.font.measureText(text);
|
||||
}
|
||||
|
||||
wrap(text: string, maxWidth: number): string[] {
|
||||
return wrapText(this.font, text, maxWidth);
|
||||
}
|
||||
|
||||
drawText(image: GrayImage, x: number, y: number, text: string, value = 255): void {
|
||||
this.font.drawText(image, x, y, text, value);
|
||||
}
|
||||
}
|
||||
|
||||
const STOCK_KEY = "stock";
|
||||
const TERMINUS_KEY_PREFIX = "terminus:";
|
||||
const TTF_KEY_PREFIX = "ttf:";
|
||||
|
||||
const BUILT_IN_FACES: readonly FontFaceCandidate[] = [
|
||||
// The firmware font: one size, one weight.
|
||||
{ family: "EvenHub", weight: 400, sizes: [20], monospace: false, key: STOCK_KEY },
|
||||
{ family: "Terminus", weight: 400, sizes: TERMINUS_SIZES, monospace: true, key: `${TERMINUS_KEY_PREFIX}400` },
|
||||
{ family: "Terminus", weight: 700, sizes: TERMINUS_SIZES, monospace: true, key: `${TERMINUS_KEY_PREFIX}700` },
|
||||
];
|
||||
|
||||
/** A family the listing lacks is re-listed at most this often (the user may have just installed it). */
|
||||
const RELIST_INTERVAL_MS = 30_000;
|
||||
|
||||
let faces: FontFaceCandidate[] | null = null;
|
||||
let listedAtMs = 0;
|
||||
|
||||
function listFaces(): FontFaceCandidate[] {
|
||||
const installed = listInstalledFonts()
|
||||
// Upright faces only: there is no italic option yet.
|
||||
.filter((font) => font.weightOrder < 1000)
|
||||
.map((font): FontFaceCandidate => ({
|
||||
family: font.family,
|
||||
weight: font.weightOrder,
|
||||
sizes: null,
|
||||
monospace: font.monospace,
|
||||
key: `${TTF_KEY_PREFIX}${font.path}`,
|
||||
}));
|
||||
faces = [...BUILT_IN_FACES, ...installed];
|
||||
listedAtMs = Date.now();
|
||||
return faces;
|
||||
}
|
||||
|
||||
/** Whether some listed face has the family (no family asks for the default, which is bundled). */
|
||||
function hasFamily(available: readonly FontFaceCandidate[], family: string | undefined): boolean {
|
||||
return !family || available.some((face) => familyMatches(face.family, family));
|
||||
}
|
||||
|
||||
/** Resolved faces by spec; null means the stock font (asked for, or nothing loadable). */
|
||||
const resolved = new Map<string, EvenHubTextFace | null>();
|
||||
|
||||
/** The face for a spec, and whether it is in the family asked for (rather than the fallback). */
|
||||
function loadFace(spec: EvenHubFontSpec): { face: EvenHubTextFace | null; exact: boolean } {
|
||||
let available = faces ?? listFaces();
|
||||
if (!hasFamily(available, spec.family) && Date.now() - listedAtMs > RELIST_INTERVAL_MS) available = listFaces();
|
||||
const exact = hasFamily(available, spec.family);
|
||||
return { face: openFace(selectFontFace(spec, available)), exact };
|
||||
}
|
||||
|
||||
function openFace(choice: { face: FontFaceCandidate; size: number } | null): EvenHubTextFace | null {
|
||||
if (!choice || choice.face.key === STOCK_KEY) return null;
|
||||
const { face, size } = choice;
|
||||
if (face.key.startsWith(TERMINUS_KEY_PREFIX)) {
|
||||
const font = getTerminusFont(size, face.weight >= 600);
|
||||
return font ? new UiFontFace(font) : null;
|
||||
}
|
||||
const font = TtfFont.load(face.key.slice(TTF_KEY_PREFIX.length), size);
|
||||
return font ? new UiFontFace(font) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The face a container draws with: the stock font when it names no font,
|
||||
* otherwise the closest match to its spec.
|
||||
*/
|
||||
export function containerFace(spec: EvenHubFontSpec | undefined): EvenHubTextFace {
|
||||
if (!spec) return EvenHubFont.get();
|
||||
const key = `${spec.family ?? ""}|${spec.size ?? ""}|${spec.weight ?? ""}`;
|
||||
let face = resolved.get(key);
|
||||
if (face === undefined) {
|
||||
const loaded = loadFace(spec);
|
||||
face = loaded.face;
|
||||
// A fallback isn't kept, so a family the user installs later gets picked up.
|
||||
if (loaded.exact) resolved.set(key, face);
|
||||
}
|
||||
// Not cached as an object: EvenHubFont.get() changes once firmware fonts are extracted.
|
||||
return face ?? EvenHubFont.get();
|
||||
}
|
||||
|
||||
/** The font families apps can ask for (extension getFonts). Re-lists installed fonts. */
|
||||
export function availableFontFamilies(): FontFamilyInfo[] {
|
||||
return describeFontFamilies(listFaces());
|
||||
}
|
||||
|
||||
/** Width of the widest line of `text` and the line height, in the font a container would use. */
|
||||
export function measureContainerText(text: string, spec: EvenHubFontSpec | undefined): { width: number; lineHeight: number } {
|
||||
const face = containerFace(spec);
|
||||
let width = 0;
|
||||
for (const line of text.split("\n")) width = Math.max(width, face.measureLine(line));
|
||||
return { width: Math.ceil(width), lineHeight: face.lineHeight };
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { unpackRuntime } from "./unpack-runtime";
|
||||
import { type EvenHubPermission } from "./permissions";
|
||||
import { EvenHubSession } from "./session";
|
||||
import { createEvenHubWindow } from "./evenhub-window";
|
||||
import { liveDeviceStatusSource } from "./device-status-source";
|
||||
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
@@ -126,7 +127,7 @@ async function startApp(
|
||||
): Promise<void> {
|
||||
const windowId = `evenhub:app:${nextSerial++}`;
|
||||
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl,
|
||||
payload => ctx.actions.playBuzzerSequence(payload));
|
||||
payload => ctx.actions.playBuzzerSequence(payload), liveDeviceStatusSource);
|
||||
const webView = createEvenHubWebView(session);
|
||||
session.attachWebView({
|
||||
evaluateJs: webView.evaluateJs,
|
||||
@@ -147,7 +148,7 @@ async function startApp(
|
||||
|
||||
try {
|
||||
await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) =>
|
||||
createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)),
|
||||
createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId), () => ctx.requestShellRender()),
|
||||
);
|
||||
} catch (error) { session.close(); throw error; }
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ import { voiceActivity } from "../../ui/shell/voice-activity";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
// The router's claim on the shared raw-PCM tap (the Microphones app holds its own).
|
||||
const RAW_CAPTURE_OWNER = "evenhub-mic-router";
|
||||
|
||||
export type EvenHubMicClient = {
|
||||
readonly windowId: string;
|
||||
isForeground(): boolean;
|
||||
@@ -83,9 +86,9 @@ class EvenHubMicRouter {
|
||||
// eligibility change so it self-heals after an STT preemption. Returns
|
||||
// false if STT currently owns the mic; a later trigger (assistant modal
|
||||
// close, foreground/screen change) re-tries.
|
||||
voiceControlBridge.startRawCapture({ communicator: activeCommunicator() });
|
||||
voiceControlBridge.startRawCapture({ communicator: activeCommunicator(), owner: RAW_CAPTURE_OWNER });
|
||||
} else {
|
||||
voiceControlBridge.stopRawCapture();
|
||||
voiceControlBridge.stopRawCapture(RAW_CAPTURE_OWNER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+283
-67
@@ -63,6 +63,16 @@ import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/to
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
||||
import { ensureFineLocationPermission } from "../../native/location-permissions";
|
||||
import {
|
||||
buildEvenHubDeviceStatus,
|
||||
OFFLINE_DEVICE_STATUS_SOURCE,
|
||||
sameDeviceStatus,
|
||||
type EvenHubDeviceStatus,
|
||||
type EvenHubDeviceStatusSource,
|
||||
} from "./device-status";
|
||||
import { parseWindowIcon, WindowIconRenderer } from "./window-icon";
|
||||
import { availableFontFamilies, containerFace, measureContainerText } from "./fonts";
|
||||
import { parseFontSpec } from "./font-spec";
|
||||
|
||||
const UPNG = require("upng-js");
|
||||
|
||||
@@ -226,9 +236,15 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
|
||||
/**
|
||||
* Injected at document-start (after the stock bridge shim): defines
|
||||
* `window.getFaceclawExtensions`, the single entry point for Faceclaw-only
|
||||
* capabilities (see the faceclaw-extensions package). It never touches the
|
||||
* standard SDK or any prototype — everything lives behind this one global, so
|
||||
* apps stay compatible with the stock Even app (where the global is absent).
|
||||
* capabilities. Its public contract (the `FaceclawExtensions` interface) is
|
||||
* the @faceclaw/evenhub-extensions package in evenhub-extensions/. Keep the
|
||||
* two in step (tests/evenhub-extensions-shim.test.cjs checks the member list).
|
||||
* When adding members, bump API_VERSION and tag them `@since apiVersion N` in
|
||||
* the package.
|
||||
*
|
||||
* It never touches the standard SDK or any prototype — everything lives behind
|
||||
* this one global, so apps stay compatible with the stock Even app (where the
|
||||
* global is absent).
|
||||
*
|
||||
* RPC uses its own id-keyed promise map and `__fcExtResolve`, kept separate
|
||||
* from the stock `__fcResolve` channel. Events arrive via `__fcExtEvent`.
|
||||
@@ -239,13 +255,15 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
(function () {
|
||||
if (window.getFaceclawExtensions) return;
|
||||
var VERSION = ${JSON.stringify(versionString)};
|
||||
var API_VERSION = 1;
|
||||
var pending = {};
|
||||
var nextId = 1;
|
||||
window.__fcExtResolve = function (id, ok, value) {
|
||||
var entry = pending[id];
|
||||
if (!entry) return;
|
||||
delete pending[id];
|
||||
(ok ? entry[0] : entry[1])(value);
|
||||
if (ok) entry[0](value);
|
||||
else entry[1](new Error(String(value)));
|
||||
};
|
||||
function call(method, params) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
@@ -260,9 +278,10 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
}
|
||||
var listeners = {};
|
||||
window.__fcExtEvent = function (name, data) {
|
||||
var arr = listeners[name];
|
||||
if (!arr) return;
|
||||
for (var i = 0; i < arr.slice().length; i++) {
|
||||
// Iterate a snapshot, so a listener that unsubscribes mid-dispatch
|
||||
// doesn't make the next one get skipped.
|
||||
var arr = (listeners[name] || []).slice();
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
try { arr[i](data); } catch (e) { if (window.console) console.error(e); }
|
||||
}
|
||||
};
|
||||
@@ -275,19 +294,29 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
if (i >= 0) arr.splice(i, 1);
|
||||
};
|
||||
}
|
||||
// Compass: enable on first listener, disable when the last one leaves.
|
||||
var compassListeners = [];
|
||||
function addCompass(cb) {
|
||||
compassListeners.push(cb);
|
||||
if (compassListeners.length === 1) send("setCompass", [true]);
|
||||
var off = on("compass", cb);
|
||||
return function () {
|
||||
off();
|
||||
var i = compassListeners.indexOf(cb);
|
||||
if (i >= 0) compassListeners.splice(i, 1);
|
||||
if (compassListeners.length === 0) send("setCompass", [false]);
|
||||
// Events the host only produces on request (compass, touch-down, text
|
||||
// input): it is told to start on the first listener and to stop when the
|
||||
// last one leaves. Each unsubscribe function works once.
|
||||
function counted(name, setMethod) {
|
||||
var count = 0;
|
||||
return function (cb) {
|
||||
var off = on(name, cb);
|
||||
if (++count === 1) send(setMethod, [true]);
|
||||
var active = true;
|
||||
return function () {
|
||||
if (!active) return;
|
||||
active = false;
|
||||
off();
|
||||
if (--count === 0) send(setMethod, [false]);
|
||||
};
|
||||
};
|
||||
}
|
||||
function setWindowIcon(icon) {
|
||||
if (!icon) return call("setWindowIcon", [null]);
|
||||
// Typed arrays (ImageData.data) don't survive JSON; send a plain array.
|
||||
var data = Array.prototype.slice.call(icon.data || []);
|
||||
return call("setWindowIcon", [{ width: icon.width, height: icon.height, data: data }]);
|
||||
}
|
||||
// Voice-assistant tools: specs go to the host; handlers stay here and run when
|
||||
// the host invokes a tool via __fcExtInvokeTool.
|
||||
var toolHandlers = {};
|
||||
@@ -318,6 +347,7 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
send("setAssistantTools", [specs]);
|
||||
}
|
||||
var extensions = {
|
||||
apiVersion: API_VERSION,
|
||||
getVersion: function () { return VERSION; },
|
||||
returnToAppSwitcher: function () { send("returnToAppSwitcher"); },
|
||||
quit: function () { send("quit"); },
|
||||
@@ -325,10 +355,18 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
getConfiguredApiKeys: function () { return call("getConfiguredApiKeys"); },
|
||||
requestApiKeyAccess: function (services) { return call("requestApiKeyAccess", [services || []]); },
|
||||
playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); },
|
||||
addCompassListener: addCompass,
|
||||
addCompassListener: counted("compass", "setCompass"),
|
||||
createLayout: function (layout) { return call("createLayout", [layout || {}]); },
|
||||
replaceLayout: function (layout) { return call("replaceLayout", [layout || {}]); },
|
||||
setAssistantTools: setAssistantTools
|
||||
setAssistantTools: setAssistantTools,
|
||||
addTouchDownListener: counted("touchDown", "setTouchDown"),
|
||||
addTextInputListener: counted("textInput", "setTextInput"),
|
||||
startVoiceInput: function () { return call("startVoiceInput"); },
|
||||
setWindowIcon: setWindowIcon,
|
||||
setAttention: function (attention) { send("setAttention", [!!attention]); },
|
||||
setKeepScreenOn: function (keepOn) { send("setKeepScreenOn", [!!keepOn]); },
|
||||
getFonts: function () { return call("getFonts"); },
|
||||
measureText: function (text, font) { return call("measureText", [String(text), font || null]); }
|
||||
};
|
||||
window.getFaceclawExtensions = function () { return extensions; };
|
||||
})();
|
||||
@@ -354,6 +392,14 @@ export type EvenHubWindowHooks = {
|
||||
pushOverlay: (layer: Layer) => void;
|
||||
/** Switch the window between the stock 576x288 band and the full 576x452 canvas. */
|
||||
setTallCanvas: (tall: boolean) => void;
|
||||
/** Show or clear the attention dot on the window's switcher icon. */
|
||||
setAttention: (attention: boolean) => void;
|
||||
/** Offer (or stop offering) the window as the shell's "Type Into App" destination. */
|
||||
setAcceptsTextInput: (accept: boolean) => void;
|
||||
/** Open the shell's voice dialog aimed at the window; false if it can't open now. */
|
||||
startVoiceInput: () => boolean;
|
||||
/** The app changed its window icon: repaint the switcher. */
|
||||
iconChanged: () => void;
|
||||
/** The shell window id (unique per running instance; used for tool registration). */
|
||||
windowId: string;
|
||||
};
|
||||
@@ -404,6 +450,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private shellFocused = true;
|
||||
/** API-key services the user has granted this app this session. */
|
||||
private readonly grantedApiKeys = new Set<string>();
|
||||
/** The app listens for touch-down (extension addTouchDownListener). */
|
||||
private touchDownRequested = false;
|
||||
/** The app listens for text input, so the shell offers it as a Type Into App destination. */
|
||||
private textInputRequested = false;
|
||||
/** The app asked for the attention dot on its switcher icon. */
|
||||
private attention = false;
|
||||
/** The app asked the idle screen timeout to wait while it is in the foreground. */
|
||||
private keepScreenOnRequested = false;
|
||||
/** The app's own switcher icon; null draws the package icon. */
|
||||
private windowIcon: WindowIconRenderer | null = null;
|
||||
/** The paired glasses' serial, read once so getGlassesInfo and every status push agree. */
|
||||
private glassesSerial: string | null | undefined = undefined;
|
||||
/** The last deviceStatusChanged sent, and the subscriptions that refresh it. */
|
||||
private lastDeviceStatus: EvenHubDeviceStatus | null = null;
|
||||
private readonly deviceStatusOffs: (() => void)[] = [];
|
||||
private log: (message: string) => void;
|
||||
|
||||
constructor(
|
||||
@@ -412,6 +473,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
log: (message: string) => void,
|
||||
remoteUrl = "",
|
||||
private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise<void> | void = () => {},
|
||||
private readonly deviceStatusSource: EvenHubDeviceStatusSource = OFFLINE_DEVICE_STATUS_SOURCE,
|
||||
) {
|
||||
this.manifest = manifest;
|
||||
this.distDir = distDir;
|
||||
@@ -425,26 +487,28 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.webViewHandle = handle;
|
||||
}
|
||||
|
||||
webViewFailed(message: string): void {
|
||||
/**
|
||||
* A WebView load failed. Once the app has loaded, a failed or blocked
|
||||
* navigation leaves its page running, so only a failure before then (or one
|
||||
* the host marks `fatal`) replaces the glasses window with the error.
|
||||
*/
|
||||
webViewFailed(message: string, fatal = false): void {
|
||||
if (this.closed) return;
|
||||
this.loadError = message; this.log(`evenhub: ${message}`); this.windowHooks?.requestRender();
|
||||
this.log(`evenhub: ${message}`);
|
||||
if (this.launchContextPushed && !fatal) return;
|
||||
this.loadError = message; this.windowHooks?.requestRender();
|
||||
}
|
||||
|
||||
/** The WebView finished loading the app: push the one-shot launch context. */
|
||||
webViewLoaded(): void {
|
||||
if (this.loadError) { this.loadError = ''; this.windowHooks?.requestRender(); }
|
||||
if (this.launchContextPushed) return;
|
||||
this.launchContextPushed = true;
|
||||
// Launched to drive the glasses (from the on-glasses file browser), so
|
||||
// apps that branch on this should take their glasses path.
|
||||
this.pushMessage("evenAppLaunchSource", { launchSource: "glassesMenu" });
|
||||
this.pushMessage("deviceStatusChanged", {
|
||||
sn: "FACECLAW-G2",
|
||||
connectType: "connected",
|
||||
isWearing: true,
|
||||
batteryLevel: 100,
|
||||
isCharging: false,
|
||||
isInCase: false,
|
||||
});
|
||||
this.pushDeviceStatus();
|
||||
this.deviceStatusOffs.push(this.deviceStatusSource.subscribe(() => this.pushDeviceStatus()));
|
||||
}
|
||||
|
||||
// ----- glasses window wiring -----
|
||||
@@ -452,6 +516,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
attachWindow(hooks: EvenHubWindowHooks): void {
|
||||
this.windowHooks = hooks;
|
||||
this.shellWindowId = hooks.windowId;
|
||||
// Requests the app made before the window existed.
|
||||
if (this.textInputRequested) hooks.setAcceptsTextInput(true);
|
||||
if (this.attention) hooks.setAttention(true);
|
||||
if (this.windowIcon) hooks.iconChanged();
|
||||
}
|
||||
|
||||
windowClosed(): void {
|
||||
@@ -481,18 +549,29 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
* locally with events only for clicks and boundary scrolls.
|
||||
*/
|
||||
handleGesture(event: InputEvent): void {
|
||||
// Touch-down has no stock event (an unknown eventType would parse as a
|
||||
// click), so it goes only to apps that subscribed through the extension.
|
||||
if (event.type === "ring-press") {
|
||||
if (this.touchDownRequested) {
|
||||
this.pushExtEvent("touchDown", { source: extensionInputSource(event.source), timestampMs: event.timestampMs });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!this.page) return;
|
||||
const capture = this.page ? eventCaptureContainer(this.page) : undefined;
|
||||
const capture = eventCaptureContainer(this.page);
|
||||
// The stock eventSource can't tell the watch from the ring, and scrolls
|
||||
// carry none; the extension field names the device when it is known.
|
||||
const inputSource = "source" in event && event.source ? extensionInputSource(event.source) : undefined;
|
||||
switch (event.type) {
|
||||
case "click":
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, CLICK_EVENT);
|
||||
this.emitListEvent(capture, CLICK_EVENT, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source));
|
||||
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source), inputSource);
|
||||
}
|
||||
break;
|
||||
case "double-click":
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source));
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source), inputSource);
|
||||
break;
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
@@ -506,9 +585,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
// given exclusive use of the gesture.
|
||||
const eventType = event.type === "long-press-release" ? LONG_PRESS_RELEASE_EVENT : LONG_PRESS_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, eventType);
|
||||
this.emitListEvent(capture, eventType, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, gestureSource(event.source));
|
||||
this.emitSysEvent(eventType, gestureSource(event.source), inputSource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -516,9 +595,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "scroll-down": {
|
||||
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.scrollList(capture, eventType);
|
||||
this.scrollList(capture, eventType, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, 0);
|
||||
this.emitSysEvent(eventType, 0, inputSource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -541,11 +620,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
|
||||
}
|
||||
|
||||
private scrollList(list: EvenHubListContainer, eventType: number): void {
|
||||
private scrollList(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
|
||||
// Swipe up selects the previous item, swipe down the next (stock direction).
|
||||
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
|
||||
// At a boundary the selection stays put (the list bounces) and the app hears about it.
|
||||
if (!stepListSelection(list, EvenHubFont.get(), delta)) this.emitListEvent(list, eventType);
|
||||
if (!stepListSelection(list, containerFace(list.font), delta)) this.emitListEvent(list, eventType, inputSource);
|
||||
this.windowHooks?.requestRender();
|
||||
}
|
||||
|
||||
@@ -810,19 +889,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return ApplicationSettings.getString(this.storageKey(readString(data, "key", "")), "");
|
||||
case "getUserInfo":
|
||||
return { uid: 0, name: "Faceclaw", avatar: "", country: "" };
|
||||
case "getGlassesInfo":
|
||||
return {
|
||||
model: "g2",
|
||||
sn: "FACECLAW-G2",
|
||||
status: {
|
||||
sn: "FACECLAW-G2",
|
||||
connectType: "connected",
|
||||
isWearing: true,
|
||||
batteryLevel: 100,
|
||||
isCharging: false,
|
||||
isInCase: false,
|
||||
},
|
||||
};
|
||||
case "getGlassesInfo": {
|
||||
const status = this.deviceStatus();
|
||||
return { model: "g2", sn: status.sn, status };
|
||||
}
|
||||
case "audioControl":
|
||||
return this.audioControl(data);
|
||||
case "getAppLocation":
|
||||
@@ -956,7 +1026,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
}
|
||||
|
||||
private storageKey(key: string): string {
|
||||
return `evenhub:${this.manifest.packageId}:ls:${key}`;
|
||||
// The package ID must end at the first ':' so that no other package can
|
||||
// reach this one's keys (manifest IDs are unvalidated: package "a" asking
|
||||
// for key "b:ls:c" must not read package "a:ls:b"'s "c"). IDs containing
|
||||
// ':' or '%' are encoded, so they can't collide with the plain IDs either.
|
||||
const id = this.manifest.packageId;
|
||||
return `evenhub:${/[:%]/.test(id) ? encodeURIComponent(id) : id}:ls:${key}`;
|
||||
}
|
||||
|
||||
// ----- pushes (host -> web) -----
|
||||
@@ -980,6 +1055,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
);
|
||||
}
|
||||
|
||||
/** Log a problem with the app's own calls where its developer will see it: the app's console. */
|
||||
private warnApp(message: string): void {
|
||||
this.log(`evenhub: ${message}`);
|
||||
this.webViewHandle?.evaluateJs(`window.console && console.warn(${JSON.stringify(`Faceclaw: ${message}`)})`);
|
||||
}
|
||||
|
||||
/** Deliver an extension event to the app (e.g. windowLifecycle). */
|
||||
private pushExtEvent(name: string, data: unknown): void {
|
||||
this.webViewHandle?.evaluateJs(
|
||||
@@ -991,17 +1072,18 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pushMessage("evenHubEvent", { type: payloadType, jsonData: elideZeroFields(jsonData) });
|
||||
}
|
||||
|
||||
private emitSysEvent(eventType: number, eventSource: number): void {
|
||||
this.pushEvenHubEvent("sysEvent", { eventType, eventSource });
|
||||
private emitSysEvent(eventType: number, eventSource: number, inputSource?: ExtensionInputSource): void {
|
||||
this.pushEvenHubEvent("sysEvent", { eventType, eventSource, [INPUT_SOURCE_FIELD]: inputSource });
|
||||
}
|
||||
|
||||
private emitListEvent(list: EvenHubListContainer, eventType: number): void {
|
||||
private emitListEvent(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
|
||||
this.pushEvenHubEvent("listEvent", {
|
||||
containerID: list.id,
|
||||
containerName: list.name,
|
||||
currentSelectItemIndex: list.selectedIndex,
|
||||
currentSelectItemName: list.itemNames[list.selectedIndex] ?? "",
|
||||
eventType,
|
||||
[INPUT_SOURCE_FIELD]: inputSource,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1017,7 +1099,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
result = await this.dispatchExtension(method, args.slice(1));
|
||||
} catch (error) {
|
||||
ok = false;
|
||||
result = String(error);
|
||||
result = error instanceof Error ? error.message : String(error);
|
||||
this.log(`evenhub: extension call failed: ${error}`);
|
||||
}
|
||||
// id 0 is a fire-and-forget send; nothing is awaiting it.
|
||||
@@ -1050,12 +1132,96 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "setAssistantTools":
|
||||
this.setAssistantTools(Array.isArray(params[0]) ? params[0] : []);
|
||||
return null;
|
||||
case "setTouchDown":
|
||||
this.touchDownRequested = params[0] === true;
|
||||
return null;
|
||||
case "setTextInput":
|
||||
this.setTextInputRequested(params[0] === true);
|
||||
return null;
|
||||
case "startVoiceInput":
|
||||
// Without a text listener the dialog would have nowhere in the app to send to.
|
||||
return this.textInputRequested && (this.windowHooks?.startVoiceInput() ?? false);
|
||||
case "setWindowIcon":
|
||||
this.setWindowIcon(params[0]);
|
||||
return null;
|
||||
case "setAttention":
|
||||
this.attention = params[0] === true;
|
||||
this.windowHooks?.setAttention(this.attention);
|
||||
return null;
|
||||
case "setKeepScreenOn":
|
||||
this.keepScreenOnRequested = params[0] === true;
|
||||
return null;
|
||||
case "getFonts":
|
||||
return availableFontFamilies();
|
||||
case "measureText":
|
||||
// No font measures in the stock font, as a container without one draws.
|
||||
return measureContainerText(String(params[0] ?? ""), parseFontSpec(params[1]));
|
||||
default:
|
||||
this.log(`evenhub: unknown extension method ${method}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ----- device status (stock getGlassesInfo / deviceStatusChanged) -----
|
||||
|
||||
private deviceStatus(): EvenHubDeviceStatus {
|
||||
const input = this.deviceStatusSource.read();
|
||||
if (this.glassesSerial === undefined) this.glassesSerial = input.serial;
|
||||
return buildEvenHubDeviceStatus({ ...input, serial: this.glassesSerial });
|
||||
}
|
||||
|
||||
/** Push deviceStatusChanged if the status differs from the last one sent. */
|
||||
private pushDeviceStatus(): void {
|
||||
if (this.closed) return;
|
||||
const status = this.deviceStatus();
|
||||
if (this.lastDeviceStatus && sameDeviceStatus(status, this.lastDeviceStatus)) return;
|
||||
this.lastDeviceStatus = status;
|
||||
this.pushMessage("deviceStatusChanged", status);
|
||||
}
|
||||
|
||||
// ----- window controls and text input (extensions) -----
|
||||
|
||||
/** Whether the idle screen timeout should wait while this window is in the foreground. */
|
||||
keepsScreenOn(): boolean {
|
||||
return this.keepScreenOnRequested;
|
||||
}
|
||||
|
||||
/** The app's own switcher icon at `size`, or null to draw the package icon. */
|
||||
renderWindowIcon(size: number): GrayImage | null {
|
||||
return this.windowIcon?.render(size) ?? null;
|
||||
}
|
||||
|
||||
private setWindowIcon(raw: unknown): void {
|
||||
if (raw == null) {
|
||||
this.windowIcon = null;
|
||||
} else {
|
||||
const image = parseWindowIcon(raw);
|
||||
if (!image) {
|
||||
throw new Error(
|
||||
"setWindowIcon: expected {width, height, data} of at most 256x256, with data holding " +
|
||||
"width*height grayscale or width*height*4 RGBA bytes",
|
||||
);
|
||||
}
|
||||
this.windowIcon = new WindowIconRenderer(image);
|
||||
}
|
||||
this.windowHooks?.iconChanged();
|
||||
}
|
||||
|
||||
private setTextInputRequested(accept: boolean): void {
|
||||
if (this.textInputRequested === accept) return;
|
||||
this.textInputRequested = accept;
|
||||
this.windowHooks?.setAcceptsTextInput(accept);
|
||||
}
|
||||
|
||||
/**
|
||||
* Text the shell routed to this window: the voice dialog's Type Into App,
|
||||
* the phone keyboard, the watch, or remote input. Only arrives while the
|
||||
* app listens (setAcceptsTextInput).
|
||||
*/
|
||||
deliverTextInput(text: string, options?: { submit?: boolean }): void {
|
||||
this.pushExtEvent("textInput", options?.submit === undefined ? { text } : { text, submit: options.submit });
|
||||
}
|
||||
|
||||
// ----- compass (EvenHubCompassClient) -----
|
||||
|
||||
deliverCompass(reading: EvenHubCompassReading): void {
|
||||
@@ -1106,12 +1272,16 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
for (const raw of rawSpecs) {
|
||||
const record = asRecord(raw);
|
||||
const name = readString(record, "name", "");
|
||||
if (!name) continue;
|
||||
const problem = appToolProblem(name, record.parameters);
|
||||
if (problem) {
|
||||
this.warnApp(`setAssistantTools: skipped tool ${JSON.stringify(name)}: ${problem}`);
|
||||
continue;
|
||||
}
|
||||
const availability = record.availability === "open" ? "open" : "foreground";
|
||||
specs.push({
|
||||
name,
|
||||
description: readString(record, "description", ""),
|
||||
inputSchema: asRecord(record.parameters),
|
||||
inputSchema: record.parameters == null ? { type: "object", properties: {} } : asRecord(record.parameters),
|
||||
availability,
|
||||
});
|
||||
}
|
||||
@@ -1202,13 +1372,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
if (steps.length === 0) return;
|
||||
for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) {
|
||||
const chunk = steps.slice(i, i + CFW_SEQ_MAX);
|
||||
const payload = buildSoundSequencePayload(chunk);
|
||||
try {
|
||||
await this.sendBuzzerSequence(payload);
|
||||
} catch (error) {
|
||||
this.log(`evenhub: playBuzzer failed: ${error}`);
|
||||
return;
|
||||
}
|
||||
// A send failure rejects the app's promise (handleExtensionCall logs it).
|
||||
await this.sendBuzzerSequence(buildSoundSequencePayload(chunk));
|
||||
if (i + CFW_SEQ_MAX < steps.length) await delay(phraseDurationMs(chunk));
|
||||
}
|
||||
}
|
||||
@@ -1239,6 +1404,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pendingToolCalls.clear();
|
||||
this.locationTracker?.stop();
|
||||
this.locationTracker = null;
|
||||
for (const off of this.deviceStatusOffs.splice(0)) off();
|
||||
if (!this.systemExitSent) {
|
||||
// Best effort: lets the app's teardown handlers run before the webview dies.
|
||||
this.emitSysEvent(SYSTEM_EXIT_EVENT, 0);
|
||||
@@ -1350,6 +1516,35 @@ function configuredApiKeyServices(): string[] {
|
||||
return API_KEY_SERVICES.filter((service) => service.setting.get().trim()).map((service) => service.id);
|
||||
}
|
||||
|
||||
/** App assistant-tool names: what every model provider accepts in a tool name. */
|
||||
const APP_TOOL_NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
||||
|
||||
/**
|
||||
* Why an app's assistant tool can't be offered, or null if it can. Provider
|
||||
* APIs reject a whole request over one bad tool definition, so offering it
|
||||
* would break every assistant turn while the app runs.
|
||||
*/
|
||||
function appToolProblem(name: string, parameters: unknown): string | null {
|
||||
if (!APP_TOOL_NAME.test(name)) return "name must be 1-64 letters, digits, underscores or hyphens";
|
||||
if (parameters == null) return null;
|
||||
const schema = parameters as Record<string, unknown>;
|
||||
if (typeof parameters !== "object" || Array.isArray(parameters) || schema.type !== "object") {
|
||||
return 'parameters must be a JSON Schema with type "object"';
|
||||
}
|
||||
if ("oneOf" in schema || "anyOf" in schema || "allOf" in schema) {
|
||||
return "parameters can't use oneOf, anyOf or allOf at the top level";
|
||||
}
|
||||
const properties = schema.properties;
|
||||
if (properties !== undefined && (!properties || typeof properties !== "object" || Array.isArray(properties))) {
|
||||
return "parameters.properties must be an object";
|
||||
}
|
||||
const required = schema.required;
|
||||
if (required !== undefined && !(Array.isArray(required) && required.every((key) => typeof key === "string"))) {
|
||||
return "parameters.required must be an array of property names";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Coerce an app-supplied buzzer step list into validated {freq, ms, duty?}. */
|
||||
function normalizeBuzzerSteps(raw: unknown[]): Step[] {
|
||||
const out: Step[] = [];
|
||||
@@ -1441,6 +1636,27 @@ function luminance(r: number, g: number, b: number): number {
|
||||
return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Input-device names in the extension API: the touchDown event's source, and
|
||||
* the `faceclawInputSource` field added to stock gesture events.
|
||||
*/
|
||||
type ExtensionInputSource = "ring" | "left-touchpad" | "right-touchpad" | "watch";
|
||||
|
||||
const INPUT_SOURCE_FIELD = "faceclawInputSource";
|
||||
|
||||
function extensionInputSource(source: InputSource): ExtensionInputSource {
|
||||
switch (source) {
|
||||
case "ring":
|
||||
return "ring";
|
||||
case "left-arm":
|
||||
return "left-touchpad";
|
||||
case "right-arm":
|
||||
return "right-touchpad";
|
||||
case "watch":
|
||||
return "watch";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ring/left-arm/right-arm -> PB EventSourceType. The watch is reported to
|
||||
* EvenHub apps as the ring: they only know stock hardware, and the watch's
|
||||
|
||||
@@ -23,14 +23,19 @@ window.__faceclawEvenHub = { postMessage: function(name, args, id) {
|
||||
export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
const host = FaceclawEvenHubWebView.new()
|
||||
host.packageIdentifier = session.manifest.packageId
|
||||
// The manager's close callback for the shown phone UI; the native back
|
||||
// button hides the host itself and reports it as a 'hidden' event.
|
||||
let onClose: (() => void) | null = null
|
||||
host.eventHandler = (json: string) => {
|
||||
try {
|
||||
const event = JSON.parse(json)
|
||||
if (event.kind === 'call' && typeof event.name === 'string' && typeof event.args === 'string' && Number.isSafeInteger(event.id))
|
||||
session.handleBridgeCall(event.name, event.args, event.id)
|
||||
else if (event.kind === 'loaded') session.webViewLoaded()
|
||||
else if (event.kind === 'error') session.webViewFailed(String(event.message))
|
||||
else if (event.kind === 'console') console.log(`[evenhub:${session.manifest.name}] ${event.message}`)
|
||||
else if (event.kind === 'error') session.webViewFailed(String(event.message), event.fatal === true)
|
||||
else if (event.kind === 'hidden') { const close = onClose; onClose = null; close?.() }
|
||||
// 'notice': host-side events such as a blocked navigation, logged with the app's console.
|
||||
else if (event.kind === 'console' || event.kind === 'notice') console.log(`[evenhub:${session.manifest.name}] ${event.message}`)
|
||||
} catch (error) { console.warn(`EvenHub bridge: ${error}`) }
|
||||
}
|
||||
// Defer loading until manager.ts has attached the session/window handles.
|
||||
@@ -41,5 +46,6 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
}, 0)
|
||||
return { native: host, evaluateJs: js => host.evaluate(js),
|
||||
destroy: () => { clearTimeout(loading); host.destroy() },
|
||||
showOnPhone: () => host.showOnPhone(), hideOnPhone: () => host.hideOnPhone() }
|
||||
showOnPhone: close => { onClose = close; host.showOnPhone() },
|
||||
hideOnPhone: () => { onClose = null; host.hideOnPhone() } }
|
||||
}
|
||||
+68
-18
@@ -15,6 +15,11 @@
|
||||
* the offline serving entirely and loads that URL from the network; the shim
|
||||
* is registered as a document-start script instead of being spliced into the
|
||||
* served HTML.
|
||||
*
|
||||
* The app can navigate its WebView to other sites, so host access is tied to
|
||||
* the app's origin: the bridge port is injected only into the app's own frames
|
||||
* (FaceclawEvenHubJsBridge), and every script the host runs in the WebView is
|
||||
* guarded by an origin check.
|
||||
*/
|
||||
import { Application, Utils } from "@nativescript/core";
|
||||
import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from "./session";
|
||||
@@ -34,19 +39,40 @@ export type EvenHubWebView = {
|
||||
native: unknown;
|
||||
};
|
||||
|
||||
/** A distinct (fake, intercepted) https origin per app isolates web storage. */
|
||||
function originHost(session: EvenHubSession): string {
|
||||
const safe = session.manifest.packageId.toLowerCase().replace(/[^a-z0-9.-]/g, "-");
|
||||
return `${safe}.evenhub.invalid`;
|
||||
/**
|
||||
* Wraps the bridge port (window.__faceclawEvenHubPort, which WebView injects
|
||||
* only into the app's own frames) as the window.__faceclawEvenHub interface
|
||||
* the bridge shim and the extensions call.
|
||||
*/
|
||||
export const ANDROID_EVENHUB_TRANSPORT_SCRIPT = `
|
||||
(function () {
|
||||
var port = window.__faceclawEvenHubPort;
|
||||
if (!port || window.__faceclawEvenHub) return;
|
||||
window.__faceclawEvenHub = {
|
||||
postMessage: function (name, args, id) { port.postMessage(JSON.stringify(['call', String(name), String(args), id])); },
|
||||
wakeTimers: function () { port.postMessage('["wake"]'); }
|
||||
};
|
||||
})();
|
||||
`;
|
||||
|
||||
/** A JS expression that is true only in a document from `startUrl`'s origin. */
|
||||
export function appOriginCheck(startUrl: string): string {
|
||||
return `(function (app) { return app.protocol === location.protocol && app.host === location.host; })(new URL(${JSON.stringify(startUrl)}))`;
|
||||
}
|
||||
|
||||
/** `js`, run only when the WebView's document is from `startUrl`'s origin. */
|
||||
export function guardToAppOrigin(startUrl: string, js: string): string {
|
||||
return `if (${appOriginCheck(startUrl)}) {\n${js}\n}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The allowed-origin rule for a remote app's document-start script: the
|
||||
* loaded URL's own origin, so the shim isn't handed to third-party frames.
|
||||
* A distinct (fake, intercepted) https origin per app. The per-app profile is
|
||||
* what keeps storage apart; without one, this still separates origin-scoped
|
||||
* storage, though not cookies scoped to the shared evenhub.invalid domain.
|
||||
*/
|
||||
function originRule(url: string): string {
|
||||
const match = /^(https?:\/\/[^/?#]+)/i.exec(url);
|
||||
return match ? match[1]! : "*";
|
||||
function originHost(session: EvenHubSession): string {
|
||||
const safe = session.manifest.packageId.toLowerCase().replace(/[^a-z0-9.-]/g, "-");
|
||||
return `${safe}.evenhub.invalid`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,12 +85,20 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
// A mutable wrapper, so the host can re-point the WebView at a recreated
|
||||
// activity (see FaceclawEvenHubWebViewHost).
|
||||
const webView = new com.faceclaw.app.FaceclawEvenHubWebView(new android.content.MutableContextWrapper(context));
|
||||
// The app's own WebView profile, so its browser storage (and that of sites it
|
||||
// navigates to) is separate from every other app's. This must come before
|
||||
// any other use of the WebView.
|
||||
if (!com.faceclaw.app.FaceclawEvenHubProfile.apply(webView, session.manifest.packageId)) {
|
||||
console.warn(`evenhub: this WebView can't separate profiles; ${session.manifest.packageId} shares browser storage with other apps`);
|
||||
}
|
||||
const settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
settings.setDomStorageEnabled(true);
|
||||
settings.setAllowFileAccess(false);
|
||||
settings.setMediaPlaybackRequiresUserGesture(false);
|
||||
|
||||
// Set once the renderer is gone: the WebView is destroyed and must not be used.
|
||||
let gone = false;
|
||||
const listener = new com.faceclaw.app.FaceclawEvenHubListener({
|
||||
onEvenAppMessage: (handlerName: string, argsJson: string, callId: number) => {
|
||||
session.handleBridgeCall(String(handlerName), String(argsJson), Number(callId));
|
||||
@@ -72,17 +106,31 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
onPageFinished: () => {
|
||||
session.webViewLoaded();
|
||||
},
|
||||
onRenderProcessGone: (crashed: boolean) => {
|
||||
gone = true;
|
||||
session.webViewFailed(crashed
|
||||
? "The app's web page crashed. Close and reopen the app."
|
||||
: "Android stopped the app's web page to free memory. Close and reopen the app.", true);
|
||||
},
|
||||
});
|
||||
|
||||
const remoteUrl = session.remoteUrl;
|
||||
// Remote apps load from their own server, so nothing is intercepted and the
|
||||
// fake per-app origin is unused.
|
||||
const host = remoteUrl ? "" : originHost(session);
|
||||
// Stock bridge shim first, then the Faceclaw extensions global (both at
|
||||
// document-start, before any app JS).
|
||||
const injectScript = EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`);
|
||||
const documentStartInstalled = remoteUrl
|
||||
? com.faceclaw.app.FaceclawEvenHubDocumentStart.install(webView, injectScript, originRule(remoteUrl)) === true
|
||||
const startUrl = remoteUrl || `https://${host}/${session.manifest.entrypoint}`;
|
||||
const originRule: string | null = com.faceclaw.app.FaceclawEvenHubJsBridge.originRule(startUrl);
|
||||
const appCheck = appOriginCheck(startUrl);
|
||||
const guarded = (js: string) => guardToAppOrigin(startUrl, js);
|
||||
// The transport, the stock bridge shim, then the Faceclaw extensions global
|
||||
// (all at document-start, before any app JS).
|
||||
const injectScript = guarded(ANDROID_EVENHUB_TRANSPORT_SCRIPT + EVENHUB_BRIDGE_INJECT_SCRIPT +
|
||||
buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`));
|
||||
const bridgeError: string | null = originRule
|
||||
? com.faceclaw.app.FaceclawEvenHubJsBridge.install(webView, originRule, listener)
|
||||
: `Not a web address: ${startUrl}`;
|
||||
const documentStartInstalled = remoteUrl && originRule
|
||||
? com.faceclaw.app.FaceclawEvenHubDocumentStart.install(webView, injectScript, originRule) === true
|
||||
: false;
|
||||
webView.setWebViewClient(
|
||||
new com.faceclaw.app.FaceclawEvenHubWebViewClient(
|
||||
@@ -95,20 +143,22 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
listener,
|
||||
),
|
||||
);
|
||||
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
|
||||
|
||||
// Surface the app's console in logcat (tag FaceclawEvenHubConsole).
|
||||
webView.setWebChromeClient(new com.faceclaw.app.FaceclawEvenHubChromeClient());
|
||||
|
||||
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
|
||||
nativeHost.attach(activity, webView);
|
||||
webView.loadUrl(remoteUrl || `https://${host}/${session.manifest.entrypoint}`);
|
||||
nativeHost.attach(activity, webView, appCheck);
|
||||
// Without the bridge the app could only hang waiting for it.
|
||||
if (bridgeError) session.webViewFailed(String(bridgeError), true);
|
||||
else webView.loadUrl(startUrl);
|
||||
|
||||
return {
|
||||
native: webView,
|
||||
evaluateJs: (js: string) => {
|
||||
if (gone) return;
|
||||
try {
|
||||
webView.evaluateJavascript(js, null);
|
||||
webView.evaluateJavascript(guarded(js), null);
|
||||
} catch (error) {
|
||||
console.warn(`evenhub evaluateJs failed: ${error}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* App-supplied window icons (the extension setWindowIcon): parse the pixel
|
||||
* buffer an app sends and scale it to the sizes the app switcher draws at.
|
||||
* Pure, so it can be tested without NativeScript.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
|
||||
/** Larger icons are refused: they only cost bridge traffic, since switcher icons are ~32px. */
|
||||
export const MAX_WINDOW_ICON_DIMENSION = 256;
|
||||
|
||||
/**
|
||||
* Parse `{width, height, data}` from the bridge into a grayscale image.
|
||||
* `data` is width*height brightness bytes, or width*height*4 RGBA bytes (an
|
||||
* ImageData), which are reduced to luminance times alpha. Arrays arrive as
|
||||
* plain arrays or as JSON-ified typed arrays ({"0": n, ...}). Returns null for
|
||||
* anything malformed.
|
||||
*/
|
||||
export function parseWindowIcon(raw: unknown): GrayImage | null {
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const record = raw as Record<string, unknown>;
|
||||
const width = Number(record.width);
|
||||
const height = Number(record.height);
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height)) return null;
|
||||
if (width < 1 || height < 1 || width > MAX_WINDOW_ICON_DIMENSION || height > MAX_WINDOW_ICON_DIMENSION) return null;
|
||||
const data = toNumberArray(record.data);
|
||||
if (!data) return null;
|
||||
const area = width * height;
|
||||
const image = new GrayImage(width, height, 0);
|
||||
if (data.length === area) {
|
||||
for (let i = 0; i < area; i++) image.pixels[i] = clampByte(data[i]!);
|
||||
} else if (data.length === area * 4) {
|
||||
for (let i = 0; i < area; i++) {
|
||||
const o = i * 4;
|
||||
const luminance = 0.2126 * data[o]! + 0.7152 * data[o + 1]! + 0.0722 * data[o + 2]!;
|
||||
image.pixels[i] = clampByte((luminance * data[o + 3]!) / 255);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale an icon to fit a size x size box, keeping its aspect ratio, with an
|
||||
* area-averaging filter. Results are cached per size, since the switcher
|
||||
* repaints often.
|
||||
*/
|
||||
export class WindowIconRenderer {
|
||||
private readonly scaled = new Map<number, GrayImage>();
|
||||
|
||||
constructor(private readonly source: GrayImage) {}
|
||||
|
||||
render(size: number): GrayImage {
|
||||
const box = Math.max(1, Math.round(size));
|
||||
let image = this.scaled.get(box);
|
||||
if (!image) {
|
||||
image = scaleToFit(this.source, box);
|
||||
this.scaled.set(box, image);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
function scaleToFit(source: GrayImage, box: number): GrayImage {
|
||||
const scale = Math.min(box / source.width, box / source.height);
|
||||
const width = Math.max(1, Math.round(source.width * scale));
|
||||
const height = Math.max(1, Math.round(source.height * scale));
|
||||
if (width === source.width && height === source.height) return source;
|
||||
const out = new GrayImage(width, height, 0);
|
||||
for (let y = 0; y < height; y++) {
|
||||
const y0 = (y * source.height) / height;
|
||||
const y1 = ((y + 1) * source.height) / height;
|
||||
for (let x = 0; x < width; x++) {
|
||||
const x0 = (x * source.width) / width;
|
||||
const x1 = ((x + 1) * source.width) / width;
|
||||
out.pixels[y * width + x] = clampByte(areaAverage(source, x0, y0, x1, y1));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Mean of the source over the rectangle [x0,x1) x [y0,y1), weighting partly covered pixels. */
|
||||
function areaAverage(source: GrayImage, x0: number, y0: number, x1: number, y1: number): number {
|
||||
let sum = 0;
|
||||
let weight = 0;
|
||||
for (let sy = Math.floor(y0); sy < Math.min(source.height, Math.ceil(y1)); sy++) {
|
||||
const wy = Math.min(y1, sy + 1) - Math.max(y0, sy);
|
||||
for (let sx = Math.floor(x0); sx < Math.min(source.width, Math.ceil(x1)); sx++) {
|
||||
const w = wy * (Math.min(x1, sx + 1) - Math.max(x0, sx));
|
||||
sum += source.pixels[sy * source.width + sx]! * w;
|
||||
weight += w;
|
||||
}
|
||||
}
|
||||
return weight > 0 ? sum / weight : 0;
|
||||
}
|
||||
|
||||
function toNumberArray(raw: unknown): number[] | null {
|
||||
let values: unknown[];
|
||||
if (Array.isArray(raw)) {
|
||||
values = raw;
|
||||
} else if (raw && typeof raw === "object") {
|
||||
const record = raw as Record<string, unknown>;
|
||||
values = [];
|
||||
for (let i = 0; String(i) in record; i++) values.push(record[String(i)]);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
const out: number[] = [];
|
||||
for (const entry of values) {
|
||||
const value = Number(entry);
|
||||
if (!Number.isFinite(value)) return null;
|
||||
out.push(value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function clampByte(value: number): number {
|
||||
return Math.max(0, Math.min(255, Math.round(value)));
|
||||
}
|
||||
@@ -205,9 +205,18 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
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 });
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "input-focus":
|
||||
case "navigation-sensors":
|
||||
case "resize-window":
|
||||
case "screen":
|
||||
case "text-input":
|
||||
case "tool-call":
|
||||
|
||||
@@ -15,6 +15,11 @@ export function isTerminalWidgetConfigured(): boolean {
|
||||
return isGlanceWidgetConfigured("terminal");
|
||||
}
|
||||
|
||||
/** Whether the system card can show: it is in a slot and the Glanceboard is enabled. */
|
||||
export function isSystemCardWidgetInUse(): boolean {
|
||||
return getBooleanSetting("glanceboard.enabled", false) && isGlanceWidgetConfigured("system-card");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the T3 Code widget can show: it is in a slot and the Glanceboard is
|
||||
* enabled (the T3 worker holds its server connections open only then).
|
||||
|
||||
@@ -5,7 +5,7 @@ import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from
|
||||
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
@@ -27,7 +27,8 @@ import {
|
||||
type GlanceSlotChoice,
|
||||
} from "./glanceboard-settings";
|
||||
import { resolveGlanceRegions, slotDividers } from "./layout";
|
||||
import { glanceWidgetSpans } from "./widgets";
|
||||
import { type GlanceWidgetDefinition } from "./widget";
|
||||
import { findGlanceWidget, glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
|
||||
export const GLANCEBOARD_SURFACE_ID = "window:glanceboard";
|
||||
@@ -300,10 +301,14 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
|
||||
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
}
|
||||
|
||||
/** Settings: layout, sleep gestures, duration, slot lines, widget font, and stereo depth. */
|
||||
/**
|
||||
* Settings: layout, sleep gestures, duration, slot lines, widget font, and
|
||||
* stereo depth, then a row per widget on the board for its own settings.
|
||||
*/
|
||||
function openGlanceSettings(ctx: LayerContext): void {
|
||||
const items: MenuItem[] = [
|
||||
enumSettingMenuItem(glanceLayoutSetting),
|
||||
const items = (): MenuItem[] => [
|
||||
// The layout decides which slots (and so which widgets) are on the board.
|
||||
enumSettingMenuItem(glanceLayoutSetting, { onChange: () => layer.setItems(items()) }),
|
||||
enumSettingMenuItem(glanceTapDurationSetting),
|
||||
toggleSettingMenuItem(glanceShowOnLongPressSetting),
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
@@ -314,8 +319,31 @@ function openGlanceSettings(ctx: LayerContext): void {
|
||||
"Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.",
|
||||
}),
|
||||
enumSettingMenuItem(glanceDepthSetting),
|
||||
...widgetSettingsMenuItems(),
|
||||
];
|
||||
ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width)));
|
||||
const layer = new MenuLayer("Glanceboard settings", items(), pageMenuLayout(ctx.stack.getBaseSize().width));
|
||||
ctx.stack.push(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* One row per widget on the board, in slot order: a submenu of the widget's
|
||||
* settings, or a disabled row for a widget that has none.
|
||||
*/
|
||||
function widgetSettingsMenuItems(): MenuItem[] {
|
||||
const widgets = new Set<GlanceWidgetDefinition>();
|
||||
for (const setting of glanceSlotSettings()) {
|
||||
const widget = findGlanceWidget(setting.get());
|
||||
if (widget) widgets.add(widget);
|
||||
}
|
||||
return Array.from(widgets, (widget): MenuItem => {
|
||||
const settingsMenuItems = widget.settingsMenuItems;
|
||||
if (!settingsMenuItems) {
|
||||
return { label: widget.label, description: `${widget.label} has no settings.`, disabled: true, onSelect: () => {} };
|
||||
}
|
||||
return submenuItem(widget.label, (ctx) => {
|
||||
ctx.stack.push(new MenuLayer(`${widget.label} settings`, settingsMenuItems(), pageMenuLayout(ctx.stack.getBaseSize().width)));
|
||||
}, { description: `Settings for the ${widget.label} widget.` });
|
||||
});
|
||||
}
|
||||
|
||||
/** The live board in the window; any click or double-click returns to the list. */
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type GrayImage } from "../../graphics/image";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
|
||||
/**
|
||||
* A Glanceboard widget: a small self-contained view that paints into a slot
|
||||
@@ -39,5 +40,11 @@ export type GlanceWidgetDefinition = {
|
||||
* gets the taller canvas; the divider between the slots is not drawn.
|
||||
*/
|
||||
tall?: boolean;
|
||||
/**
|
||||
* The rows of this widget's submenu in the Glanceboard app's Settings. A
|
||||
* widget without settings still gets a row there (disabled) while it is
|
||||
* on the board.
|
||||
*/
|
||||
settingsMenuItems?: () => MenuItem[];
|
||||
create: () => GlanceWidget;
|
||||
};
|
||||
@@ -1,8 +1,15 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { hasLocationPermission } from "../../../native/location-permissions";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassDiagnostics } from "../../../native/compass";
|
||||
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
||||
import {
|
||||
cardinalDirection,
|
||||
createCompassBackground,
|
||||
drawCompassRose,
|
||||
layoutCompassRose,
|
||||
magneticAccuracyDots,
|
||||
TICK_HEIGHT,
|
||||
} from "../../compass/compass-rose";
|
||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
|
||||
import { getNorthReference, resolveHeading } from "../../compass/heading";
|
||||
import { glanceFont } from "../glance-font";
|
||||
@@ -14,16 +21,21 @@ const TOP_PAD = 4;
|
||||
const BOTTOM_PAD = 4;
|
||||
const EDGE_PAD = 8;
|
||||
const MAX_ROSE_RADIUS = 60;
|
||||
/** Inset of the top-right magnetometer calibration dots from the card edges. */
|
||||
const ACCURACY_TOP_PAD = TOP_PAD;
|
||||
const ACCURACY_RIGHT_PAD = EDGE_PAD;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* already known, otherwise the readout says it is magnetic. The firmware's
|
||||
* magnetometer calibration shows as the Compass app's dots, without a label.
|
||||
*/
|
||||
export class CompassWidget implements GlanceWidget {
|
||||
private rawHeading: number | null = null;
|
||||
private diagnostics: CompassDiagnostics | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private unsubscribeDeclination: (() => void) | null = null;
|
||||
private enabled = false;
|
||||
@@ -33,6 +45,7 @@ export class CompassWidget implements GlanceWidget {
|
||||
this.unsubscribe = addCompassListener((event) => {
|
||||
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||
this.diagnostics = event.diagnostics ?? null;
|
||||
requestRender();
|
||||
}
|
||||
});
|
||||
@@ -96,5 +109,9 @@ export class CompassWidget implements GlanceWidget {
|
||||
if (status) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(status) / 2), y + medium.lineHeight + 2, status, 125);
|
||||
}
|
||||
const accuracyDots = magneticAccuracyDots(this.diagnostics);
|
||||
if (accuracyDots) {
|
||||
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyDots), ACCURACY_TOP_PAD, accuracyDots, 175);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
import { type GlanceWidgetDefinition, type GlanceWidgetId } from "../widget";
|
||||
import { CalendarWidget } from "./calendar-widget";
|
||||
import { CompassWidget } from "./compass-widget";
|
||||
import { MusicWidget } from "./music-widget";
|
||||
import { MusicWidget, musicSettingsMenuItems } from "./music-widget";
|
||||
import { NightscoutWidget } from "./nightscout-widget";
|
||||
import { SystemCardWidget } from "./system-card";
|
||||
import { SystemCardWidget, systemCardSettingsMenuItems } from "./system-card";
|
||||
import { T3CodeWidget } from "./t3code-widget";
|
||||
import { TerminalWidget } from "./terminal-widget";
|
||||
import { HealthWidget } from "./health-widget";
|
||||
|
||||
/** Every widget a board slot can hold. */
|
||||
export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
{ id: "system-card", label: "System card", create: () => new SystemCardWidget() },
|
||||
{ id: "system-card", label: "System card", settingsMenuItems: systemCardSettingsMenuItems, create: () => new SystemCardWidget() },
|
||||
{ id: "calendar", label: "Calendar", tall: true, create: () => new CalendarWidget() },
|
||||
{ id: "terminal", label: "Terminal", tall: true, create: () => new TerminalWidget() },
|
||||
{ id: "t3code", label: "T3 Code", tall: true, create: () => new T3CodeWidget() },
|
||||
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||
{ id: "music", label: "Music", settingsMenuItems: musicSettingsMenuItems, create: () => new MusicWidget() },
|
||||
{ id: "health", label: "Health", create: () => new HealthWidget() },
|
||||
];
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import { GrayImage } from "../../../graphics/image";
|
||||
import { renderIcon } from "../../../graphics/icons";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
|
||||
import { ConfigSettingBoolean, onAnySettingChanged, toggleSettingMenuItem } from "../../../ui/dashboard-settings";
|
||||
import { type MenuItem } from "../../../ui/menu";
|
||||
import { clamp } from "../../../util/numeric-util";
|
||||
import { glanceFont } from "../glance-font";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
@@ -36,20 +38,37 @@ function idleNoteImage(): GrayImage | null {
|
||||
return note;
|
||||
}
|
||||
const ART_X = PAD;
|
||||
const ART_TEXT_GAP = 12;
|
||||
const PROGRESS_BAR_HEIGHT = 5;
|
||||
|
||||
export const musicShowAlbumArtSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-music-show-album-art",
|
||||
label: "Show album art",
|
||||
storageKey: "glanceboard.music.showAlbumArt",
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Show the album art beside the track details. When off, the title, artist, album and progress bar use the full width of the card.",
|
||||
});
|
||||
|
||||
/** The music card's submenu in Glanceboard > Settings. */
|
||||
export function musicSettingsMenuItems(): MenuItem[] {
|
||||
return [toggleSettingMenuItem(musicShowAlbumArtSetting)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Now playing: album art (unless turned off), title/artist/album and a
|
||||
* progress bar for the active media session (Apple Music on iOS), 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 unsubscribeSettings: (() => void) | null = null;
|
||||
private progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
/** The artVersion `art` was fetched for; -1 = not fetched yet. */
|
||||
private artVersion = -1;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||
@@ -61,11 +80,14 @@ export class MusicWidget implements GlanceWidget {
|
||||
}
|
||||
requestRender();
|
||||
});
|
||||
this.unsubscribeSettings = onAnySettingChanged(requestRender);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.unsubscribeSettings?.();
|
||||
this.unsubscribeSettings = null;
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
|
||||
@@ -83,7 +105,7 @@ export class MusicWidget implements GlanceWidget {
|
||||
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);
|
||||
image.drawText(small, PAD, PAD + small.lineHeight + 6, mediaControllerBridge.accessLabel, 140);
|
||||
return;
|
||||
}
|
||||
// Nothing playing: a dim note, centred, and nothing else.
|
||||
@@ -92,10 +114,12 @@ export class MusicWidget implements GlanceWidget {
|
||||
return;
|
||||
}
|
||||
|
||||
// Without the art, the text and progress bar keep its rows and take its columns.
|
||||
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
|
||||
this.drawArt(image, media, artY);
|
||||
const showArt = musicShowAlbumArtSetting.get();
|
||||
if (showArt) this.drawArt(image, media, artY);
|
||||
|
||||
const textX = ART_X + ART_SIZE + 12;
|
||||
const textX = showArt ? ART_X + ART_SIZE + ART_TEXT_GAP : PAD;
|
||||
const textWidth = image.width - textX - PAD;
|
||||
let y = artY;
|
||||
image.drawText(medium, textX, y, truncateText(medium, media.title || "Unknown title", textWidth), 230);
|
||||
@@ -129,10 +153,8 @@ export class MusicWidget implements GlanceWidget {
|
||||
}
|
||||
|
||||
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;
|
||||
if (media.artVersion !== this.artVersion) {
|
||||
this.artVersion = media.artVersion;
|
||||
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||
}
|
||||
if (this.art) {
|
||||
|
||||
@@ -1,25 +1,30 @@
|
||||
import {
|
||||
BATTERY_ICON_WIDTH,
|
||||
DENSE_BATTERY_BLOCK_HEIGHT,
|
||||
denseBatteriesWidth,
|
||||
drawBattery,
|
||||
drawDenseBatteries,
|
||||
type BatteryDevice,
|
||||
} from "../../../graphics/battery";
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../../native/location-permissions";
|
||||
import { onAndroidNotificationsChanged, readActiveNotificationIcons } from "../../../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../../../native/phone-battery";
|
||||
import { weatherBridge } from "../../../native/weather";
|
||||
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
|
||||
import {
|
||||
batteryDisplayModeSetting,
|
||||
batteryIndicatorVisible,
|
||||
ConfigSettingBoolean,
|
||||
glassesBatteryVisibilitySetting,
|
||||
onAnySettingChanged,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
toggleSettingMenuItem,
|
||||
watchBatteryVisibilitySetting,
|
||||
type BatteryIndicatorVisibility,
|
||||
} from "../../../ui/dashboard-settings";
|
||||
import { type MenuItem } from "../../../ui/menu";
|
||||
import { shell } from "../../../ui/shell/shell";
|
||||
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
@@ -36,20 +41,113 @@ const NOTIFICATION_ICON_SIZE = 24;
|
||||
const NOTIFICATION_ICON_GAP = 4;
|
||||
const BATTERY_ITEM_GAP = 12;
|
||||
const BATTERY_LABEL_VALUE = 150;
|
||||
/** Gap between the battery block and the weather reading below it. */
|
||||
const WEATHER_GAP = 6;
|
||||
/** Gap between the battery block and the weather reading stacked after it. */
|
||||
const SIDE_BLOCK_GAP = 6;
|
||||
/** Rows between the time and the date. */
|
||||
const CLOCK_LINE_GAP = 2;
|
||||
/** Rows between the clock and the timers, and between the timers and the notification icons. */
|
||||
const TIMERS_GAP = 4;
|
||||
const TIMER_NAME_GAP = 8;
|
||||
|
||||
type BatteryItem = { label: string; device: BatteryDevice; percent: number; charging: boolean };
|
||||
/** A block drawn against the card's right edge: its left edge and its rows. */
|
||||
type RightBlock = { left: number; top: number; bottom: number };
|
||||
type Rect = { x: number; y: number; width: number; height: number };
|
||||
/** A side-column block, measured so it can be placed against either edge before it is drawn. */
|
||||
type SideBlock = { width: number; height: number; draw: (image: GrayImage, left: number, top: number) => void };
|
||||
/** The main column's horizontal extent on some rows. */
|
||||
type Span = { left: number; right: number };
|
||||
|
||||
export const systemCardShowTimeSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-show-time",
|
||||
label: "Time",
|
||||
storageKey: "glanceboard.systemCard.showTime",
|
||||
defaultValue: true,
|
||||
description: "Show the clock on the system card.",
|
||||
});
|
||||
|
||||
export const systemCardShowDateSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-show-date",
|
||||
label: "Date",
|
||||
storageKey: "glanceboard.systemCard.showDate",
|
||||
defaultValue: true,
|
||||
description: "Show the date on the system card, under the time.",
|
||||
});
|
||||
|
||||
export const systemCardShowBatterySetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-show-battery",
|
||||
label: "Battery",
|
||||
storageKey: "glanceboard.systemCard.showBattery",
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Show battery indicators on the system card. Their style and which devices appear follow Settings > Customization > Battery indicators.",
|
||||
});
|
||||
|
||||
export const systemCardShowNotificationsSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-show-notifications",
|
||||
label: "Notifications",
|
||||
storageKey: "glanceboard.systemCard.showNotifications",
|
||||
defaultValue: true,
|
||||
description: "Show the icons of the phone's notifications along the edge of the system card.",
|
||||
});
|
||||
|
||||
export const systemCardFlipHorizontalSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-flip-horizontal",
|
||||
label: "Flip layout horizontally",
|
||||
storageKey: "glanceboard.systemCard.flipHorizontal",
|
||||
defaultValue: false,
|
||||
description:
|
||||
"Mirror the system card left to right: time, date, timers and notifications on the right, batteries and weather on the left.",
|
||||
});
|
||||
|
||||
export const systemCardFlipVerticalSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-system-card-flip-vertical",
|
||||
label: "Flip layout vertically",
|
||||
storageKey: "glanceboard.systemCard.flipVertical",
|
||||
defaultValue: false,
|
||||
description:
|
||||
"Mirror the system card top to bottom: notifications along the top; time, date, batteries and weather along the bottom.",
|
||||
});
|
||||
|
||||
/**
|
||||
* The system card's submenu in Glanceboard > Settings. Weather is the
|
||||
* Weather app's "Show on system card" setting under a shorter label.
|
||||
*/
|
||||
export function systemCardSettingsMenuItems(): MenuItem[] {
|
||||
return [
|
||||
toggleSettingMenuItem(systemCardShowTimeSetting),
|
||||
toggleSettingMenuItem(systemCardShowDateSetting),
|
||||
toggleSettingMenuItem(systemCardShowBatterySetting),
|
||||
toggleSettingMenuItem(weatherShowOnSystemCardSetting, {
|
||||
label: "Weather",
|
||||
onChange: (_ctx, enabled) => {
|
||||
if (enabled) requestWeatherLocationAccess();
|
||||
},
|
||||
}),
|
||||
toggleSettingMenuItem(systemCardShowNotificationsSetting),
|
||||
toggleSettingMenuItem(systemCardFlipHorizontalSetting),
|
||||
toggleSettingMenuItem(systemCardFlipVerticalSetting),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Weather switched on without location access: ask for it now, as the
|
||||
* Weather app's own toggle does, since the background refresh has no screen
|
||||
* to prompt from.
|
||||
*/
|
||||
function requestWeatherLocationAccess(): void {
|
||||
if (hasLocationPermission()) return;
|
||||
void ensureLocationPermission().then((granted) => {
|
||||
if (granted) void weatherBridge.refreshNow();
|
||||
}).catch((error) => console.warn(`Weather permission: ${error}`));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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), the
|
||||
* current weather (when the Weather app's "Show on system card" is on), 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 > Customization > Battery indicators, so the card agrees with
|
||||
* current weather, and the Timers app's countdowns: the top bar's contents
|
||||
* plus timers, laid out for a card. Every part but the timers can be turned
|
||||
* off, and the layout mirrored either way, in the card's submenu of
|
||||
* Glanceboard > Settings. Battery style and per-device visibility follow
|
||||
* Settings > Customization > Battery indicators, so the card agrees with
|
||||
* the bar. Countdowns tick once a second only while one is running.
|
||||
*/
|
||||
export class SystemCardWidget implements GlanceWidget {
|
||||
@@ -64,7 +162,7 @@ export class SystemCardWidget implements GlanceWidget {
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.scheduleMinuteTick();
|
||||
this.unsubscribeNotifications = onAndroidNotificationPosted(() => this.requestRender?.());
|
||||
this.unsubscribeNotifications = onAndroidNotificationsChanged(() => this.requestRender?.());
|
||||
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
|
||||
this.unsubscribeTimers = timerEngine.onChange(() => {
|
||||
this.syncSecondTick();
|
||||
@@ -120,35 +218,93 @@ export class SystemCardWidget implements GlanceWidget {
|
||||
const medium = glanceFont.medium();
|
||||
const small = glanceFont.small();
|
||||
const now = new Date();
|
||||
const { width, height } = image;
|
||||
|
||||
// Clock and date down the left; batteries take the right edge, with the
|
||||
// weather right-aligned beneath them. Text on the left stops short of
|
||||
// whichever of those shares its rows.
|
||||
const battery = drawBatteryBlock(image, small, image.width - PAD, PAD);
|
||||
const weatherTop = battery.bottom > battery.top ? battery.bottom + WEATHER_GAP : PAD;
|
||||
const weather = drawWeatherBlock(image, medium, image.width - PAD, weatherTop, now.getTime());
|
||||
const textRight = (top: number, bottom: number): number =>
|
||||
weather && weather.top < bottom && weather.bottom > top ? Math.min(battery.left, weather.left) : battery.left;
|
||||
const textWidth = Math.max(0, textRight(PAD, PAD + large.lineHeight) - BATTERY_ITEM_GAP - PAD);
|
||||
// Unflipped, the time and date run down the left from the top, the
|
||||
// batteries and then the weather stack down the right edge, and the
|
||||
// notification icons take the bottom row, with the timers in the band
|
||||
// between the date and the icons. The flips mirror where each block sits
|
||||
// (positions below are given for the unflipped card), not what is inside
|
||||
// it: the time stays above the date, and timers read soonest first.
|
||||
const flipX = systemCardFlipHorizontalSetting.get();
|
||||
const flipY = systemCardFlipVerticalSetting.get();
|
||||
const placeX = (x: number, w: number): number => (flipX ? width - x - w : x);
|
||||
const placeY = (y: number, h: number): number => (flipY ? height - y - h : y);
|
||||
|
||||
const sideBlocks: SideBlock[] = [];
|
||||
const batteryItems = systemCardShowBatterySetting.get() ? collectBatteryItems() : [];
|
||||
if (batteryItems.length) sideBlocks.push(batteryBlock(small, batteryItems));
|
||||
const weather = weatherShowOnSystemCardSetting.get() ? weatherBlock(medium, now.getTime()) : null;
|
||||
if (weather) sideBlocks.push(weather);
|
||||
const side: Rect[] = [];
|
||||
let sideOffset = PAD;
|
||||
for (const block of sideBlocks) {
|
||||
const rect = {
|
||||
x: placeX(width - PAD - block.width, block.width),
|
||||
y: placeY(sideOffset, block.height),
|
||||
width: block.width,
|
||||
height: block.height,
|
||||
};
|
||||
block.draw(image, rect.x, rect.y);
|
||||
side.push(rect);
|
||||
sideOffset += block.height + SIDE_BLOCK_GAP;
|
||||
}
|
||||
// Text in the main column stops short of whichever side blocks share its
|
||||
// rows, and hugs the card's outer edge.
|
||||
const span = (top: number, bottom: number): Span => {
|
||||
let left = PAD;
|
||||
let right = width - PAD;
|
||||
for (const rect of side) {
|
||||
if (rect.y >= bottom || rect.y + rect.height <= top) continue;
|
||||
if (flipX) left = Math.max(left, rect.x + rect.width + BATTERY_ITEM_GAP);
|
||||
else right = Math.min(right, rect.x - BATTERY_ITEM_GAP);
|
||||
}
|
||||
return { left, right };
|
||||
};
|
||||
const lineX = ({ left, right }: Span, lineWidth: number): number => (flipX ? right - lineWidth : left);
|
||||
|
||||
const showTime = systemCardShowTimeSetting.get();
|
||||
const showDate = systemCardShowDateSetting.get();
|
||||
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;
|
||||
const dateText = formatClockDate(now);
|
||||
const clockHeight = (timeFont: UiFont): number =>
|
||||
(showTime ? timeFont.lineHeight : 0) + (showTime && showDate ? CLOCK_LINE_GAP : 0) + (showDate ? medium.lineHeight : 0);
|
||||
// The time drops to the medium font when the large one would run into a side block.
|
||||
let timeFont = large;
|
||||
if (showTime) {
|
||||
const top = placeY(PAD, clockHeight(large));
|
||||
const { left, right } = span(top, top + large.lineHeight);
|
||||
if (large.measureText(timeText) > right - left) timeFont = medium;
|
||||
}
|
||||
const clockSize = clockHeight(timeFont);
|
||||
let y = placeY(PAD, clockSize);
|
||||
if (showTime) {
|
||||
image.drawText(timeFont, lineX(span(y, y + timeFont.lineHeight), timeFont.measureText(timeText)), y, timeText, 230);
|
||||
y += timeFont.lineHeight + CLOCK_LINE_GAP;
|
||||
}
|
||||
if (showDate) {
|
||||
image.drawText(medium, lineX(span(y, y + medium.lineHeight), medium.measureText(dateText)), y, dateText, 170);
|
||||
}
|
||||
|
||||
// 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;
|
||||
const timersWidth = Math.max(0, textRight(y, iconY - 4) - BATTERY_ITEM_GAP - PAD);
|
||||
drawTimers(image, small, PAD, y, timersWidth, 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 showNotifications = systemCardShowNotificationsSetting.get();
|
||||
const bandTop = clockSize > 0 ? PAD + clockSize + TIMERS_GAP : PAD;
|
||||
const bandBottom = height - PAD - (showNotifications ? NOTIFICATION_ICON_SIZE + TIMERS_GAP : 0);
|
||||
drawTimers(image, small, {
|
||||
top: placeY(bandTop, bandBottom - bandTop),
|
||||
height: bandBottom - bandTop,
|
||||
fromBottom: flipY,
|
||||
alignRight: flipX,
|
||||
span,
|
||||
}, now.getTime());
|
||||
|
||||
// Notification icons, as many as fit, the first nearest the main column's edge.
|
||||
const maxIcons = Math.max(0, ((width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
|
||||
if (showNotifications && maxIcons > 0) {
|
||||
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
|
||||
if (stale) noteStaleDataUsed();
|
||||
const iconY = placeY(height - PAD - NOTIFICATION_ICON_SIZE, NOTIFICATION_ICON_SIZE);
|
||||
for (let index = 0; index < icons.length; index++) {
|
||||
image.drawImage(icons[index]!, PAD + index * (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP), iconY);
|
||||
image.drawImage(icons[index]!, placeX(PAD + index * (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP), NOTIFICATION_ICON_SIZE), iconY);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -180,59 +336,62 @@ function collectBatteryItems(): BatteryItem[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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; the dense style swaps
|
||||
* labels for device icons and stacks two indicators per column, as in the
|
||||
* top bar. Returns the block's extent (zero-height at `right` when nothing
|
||||
* is shown).
|
||||
* The battery indicators 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; the dense style swaps labels for device icons
|
||||
* and stacks two indicators per column, as in the top bar.
|
||||
*/
|
||||
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): RightBlock {
|
||||
const items = collectBatteryItems();
|
||||
if (!items.length) return { left: right, top, bottom: top };
|
||||
function batteryBlock(font: UiFont, items: readonly BatteryItem[]): SideBlock {
|
||||
const mode = batteryDisplayModeSetting.get();
|
||||
if (mode === "dense") {
|
||||
return { left: drawDenseBatteries(image, items, right, top), top, bottom: top + DENSE_BATTERY_BLOCK_HEIGHT };
|
||||
const width = denseBatteriesWidth(items);
|
||||
return {
|
||||
width,
|
||||
height: DENSE_BATTERY_BLOCK_HEIGHT,
|
||||
draw: (image, left, top) => {
|
||||
drawDenseBatteries(image, items, left + width, top);
|
||||
},
|
||||
};
|
||||
}
|
||||
const percentage = mode === "percentage" || mode === "stacked-percentage";
|
||||
const stacked = mode === "stacked" || mode === "stacked-percentage";
|
||||
const gaugeHeight = drawBattery(0, false).height;
|
||||
const valueHeight = percentage ? font.lineHeight : Math.max(font.lineHeight, gaugeHeight);
|
||||
let x = right;
|
||||
for (let index = items.length - 1; index >= 0; index--) {
|
||||
const item = items[index]!;
|
||||
const percentText = `${item.percent}%`;
|
||||
const cells = items.map((item) => {
|
||||
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 { left: x + BATTERY_ITEM_GAP, top, bottom: top + (stacked ? font.lineHeight + 2 : 0) + valueHeight };
|
||||
const valueWidth = percentage ? font.measureText(`${item.percent}%`) : BATTERY_ICON_WIDTH;
|
||||
return { item, labelWidth, valueWidth, width: stacked ? Math.max(labelWidth, valueWidth) : labelWidth + 5 + valueWidth };
|
||||
});
|
||||
return {
|
||||
width: cells.reduce((total, cell) => total + cell.width, 0) + BATTERY_ITEM_GAP * (cells.length - 1),
|
||||
height: (stacked ? font.lineHeight + 2 : 0) + valueHeight,
|
||||
draw: (image, left, top) => {
|
||||
let x = left;
|
||||
for (const { item, labelWidth, valueWidth, width } of cells) {
|
||||
if (stacked) {
|
||||
image.drawText(font, x + (((width - labelWidth) / 2) | 0), top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + (((width - valueWidth) / 2) | 0), top + font.lineHeight + 2, item, percentage, gaugeHeight);
|
||||
} else {
|
||||
image.drawText(font, x, top, item.label, BATTERY_LABEL_VALUE);
|
||||
drawBatteryValue(image, font, x + labelWidth + 5, top, item, percentage, gaugeHeight);
|
||||
}
|
||||
x += width + BATTERY_ITEM_GAP;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The current weather's condition icon and temperature, right-aligned at
|
||||
* `right` from `top`, while the system-card weather setting is on and a
|
||||
* fresh reading exists. Returns the block's extent, or null when not drawn.
|
||||
*/
|
||||
function drawWeatherBlock(image: GrayImage, font: UiFont, right: number, top: number, nowMs: number): RightBlock | null {
|
||||
if (!weatherShowOnSystemCardSetting.get()) return null;
|
||||
/** The current weather's condition icon and temperature, or null without a fresh reading. */
|
||||
function weatherBlock(font: UiFont, nowMs: number): SideBlock | null {
|
||||
const reading = weatherReading(weatherBridge.snapshot(), nowMs);
|
||||
if (!reading) return null;
|
||||
const iconSize = font.lineHeight + 4;
|
||||
const height = Math.max(iconSize, font.lineHeight);
|
||||
const left = right - Math.ceil(measureWeatherReading(reading, font, iconSize));
|
||||
drawWeatherReading(image, reading, font, iconSize, left, top, height, 200);
|
||||
return { left, top, bottom: top + height };
|
||||
return {
|
||||
width: Math.ceil(measureWeatherReading(reading, font, iconSize)),
|
||||
height,
|
||||
draw: (image, left, top) => drawWeatherReading(image, reading, font, iconSize, left, top, height, 200),
|
||||
};
|
||||
}
|
||||
|
||||
function drawBatteryValue(
|
||||
@@ -262,26 +421,36 @@ function drawBatteryValue(
|
||||
/**
|
||||
* 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`.
|
||||
* timer's name, one per line, as many as fit in the band. The lines sit
|
||||
* against the band's top (or its bottom), each as wide as the main column
|
||||
* allows on its rows and hugging its left (or right) edge.
|
||||
*/
|
||||
function drawTimers(image: GrayImage, font: UiFont, x: number, top: number, width: number, bottom: number, nowMs: number): void {
|
||||
const timers = sortTimers(timerEngine.timers(), nowMs);
|
||||
function drawTimers(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
band: { top: number; height: number; fromBottom: boolean; alignRight: boolean; span: (top: number, bottom: number) => Span },
|
||||
nowMs: number,
|
||||
): void {
|
||||
const step = lineStep(font);
|
||||
let y = top;
|
||||
const fit = band.height < font.lineHeight ? 0 : 1 + Math.floor((band.height - font.lineHeight) / step);
|
||||
const timers = sortTimers(timerEngine.timers(), nowMs).slice(0, fit);
|
||||
if (!timers.length) return;
|
||||
let y = band.fromBottom ? band.top + band.height - (timers.length - 1) * step - font.lineHeight : band.top;
|
||||
for (const timer of timers) {
|
||||
if (y + font.lineHeight > bottom) break;
|
||||
const { left, right } = band.span(y, y + font.lineHeight);
|
||||
const phase = timerPhase(timer);
|
||||
const clock = phase === "rung" ? "Done" : formatCountdown(timerRemainingMs(timer, nowMs));
|
||||
const clockWidth = font.measureText(clock);
|
||||
const nameOffset = font.measureText(clock) + TIMER_NAME_GAP;
|
||||
const fullName = phase === "paused" ? `${timerDisplayName(timer)} (paused)` : timerDisplayName(timer);
|
||||
const name = truncateText(font, fullName, Math.max(0, right - left - nameOffset));
|
||||
const x = band.alignRight ? right - nameOffset - font.measureText(name) : left;
|
||||
if (phase === "rung") {
|
||||
image.fillRect(x - 2, y - 1, clockWidth + 4, font.lineHeight + 2, 230);
|
||||
image.fillRect(x - 2, y - 1, nameOffset - TIMER_NAME_GAP + 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);
|
||||
image.drawText(font, x + nameOffset, y, name, 160);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,10 @@ const BOTTOM_MARGIN = 2;
|
||||
* 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.
|
||||
*
|
||||
* The worker sorts sessions most-recently-updated first; the widget keeps the
|
||||
* order it first sees after the board opens so rows don't jump around while
|
||||
* it is showing. Sessions that appear later go to the end of their host.
|
||||
*/
|
||||
export class TerminalWidget implements GlanceWidget {
|
||||
private snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
|
||||
@@ -31,12 +35,17 @@ export class TerminalWidget implements GlanceWidget {
|
||||
private requestRender: (() => void) | null = null;
|
||||
private phase = 0;
|
||||
private animationTimer: ReturnType<typeof setInterval> | null = null;
|
||||
/** Row key -> display rank, fixed from the first snapshot seen since start. */
|
||||
private readonly rowOrder = new Map<string, number>();
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.rowOrder.clear();
|
||||
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
|
||||
this.rankNewRows();
|
||||
this.unsubscribe = onWorkerStateChanged(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
|
||||
this.snapshot = snapshot as TerminalSessionsSnapshot;
|
||||
this.rankNewRows();
|
||||
this.syncAnimation();
|
||||
requestRender();
|
||||
});
|
||||
@@ -51,6 +60,15 @@ export class TerminalWidget implements GlanceWidget {
|
||||
this.animationTimer = null;
|
||||
}
|
||||
|
||||
/** Give rows not seen yet the next ranks, in the snapshot's (recency) order. */
|
||||
private rankNewRows(): void {
|
||||
for (const host of this.snapshot?.hosts ?? []) {
|
||||
for (const row of host.sessions) {
|
||||
if (!this.rowOrder.has(row.key)) this.rowOrder.set(row.key, this.rowOrder.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private anyActive(nowMs = Date.now()): boolean {
|
||||
return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs))));
|
||||
}
|
||||
@@ -107,7 +125,10 @@ export class TerminalWidget implements GlanceWidget {
|
||||
image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110);
|
||||
y += step;
|
||||
}
|
||||
for (const row of host.sessions) {
|
||||
const rows = host.sessions
|
||||
.slice()
|
||||
.sort((a, b) => (this.rowOrder.get(a.key) ?? 0) - (this.rowOrder.get(b.key) ?? 0));
|
||||
for (const row of rows) {
|
||||
if (!fits(y)) return;
|
||||
drawSessionRow(image, font, {
|
||||
x: GUTTER_X,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/** Native services still outside the current iOS app-screen milestone. */
|
||||
const unavailable: Record<string, string> = {
|
||||
music: 'Media-player integration is not available on iOS yet.',
|
||||
transcribe: 'Voice capture and transcription are not available on iOS yet.',
|
||||
microphones: 'Glasses microphone capture is not available on iOS yet.',
|
||||
}
|
||||
export function iosAppUnavailableReason(appId: string): string | null {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// Utterance times come from the caption engine's sample clock, which only
|
||||
// advances with audio fed to it. Audio that never reaches the engine (beam
|
||||
// filtered, paused for the voice dialog, lost to a stall) would otherwise
|
||||
// shift every later line earlier, so the clock is re-anchored to wall time
|
||||
// (arrival time, so ordinary BLE jitter is tolerated) whenever the two drift
|
||||
// this far apart, in either direction: missing audio puts wall time ahead,
|
||||
// and a backlog delivered late in a burst puts it behind once the stream
|
||||
// catches up.
|
||||
export const CAPTION_CLOCK_DRIFT_MS = 300;
|
||||
const MAX_ANCHORS = 1000;
|
||||
|
||||
/** Maps a caption engine's clock (ms of audio fed to it) to wall time. */
|
||||
export class CaptionClock {
|
||||
private fedSamples = 0;
|
||||
// Engine-clock times with the wall time each corresponds to, in order.
|
||||
private readonly anchors: Array<{ engineMs: number; wallMs: number }> = [];
|
||||
|
||||
constructor(
|
||||
private readonly sampleRate: number,
|
||||
private readonly startWallMs: number,
|
||||
) {}
|
||||
|
||||
/** `bytes` of 16-bit mono PCM were fed to the engine, arriving at `nowMs`. */
|
||||
noteFed(bytes: number, nowMs: number): void {
|
||||
const samples = Math.floor(bytes / 2);
|
||||
const engineMs = (this.fedSamples * 1000) / this.sampleRate;
|
||||
this.fedSamples += samples;
|
||||
// The chunk arrived as it ended; its first sample is that much older.
|
||||
const wallMs = nowMs - (samples * 1000) / this.sampleRate;
|
||||
const last = this.anchors[this.anchors.length - 1];
|
||||
if (last && Math.abs(wallMs - (last.wallMs + engineMs - last.engineMs)) <= CAPTION_CLOCK_DRIFT_MS) return;
|
||||
this.anchors.push({ engineMs, wallMs });
|
||||
if (this.anchors.length > MAX_ANCHORS) this.anchors.shift();
|
||||
}
|
||||
|
||||
/** Wall time of a moment on the engine's clock. */
|
||||
wallMs(engineMs: number): number {
|
||||
for (let i = this.anchors.length - 1; i >= 0; i--) {
|
||||
const anchor = this.anchors[i]!;
|
||||
if (anchor.engineMs <= engineMs) return anchor.wallMs + engineMs - anchor.engineMs;
|
||||
}
|
||||
return this.startWallMs + engineMs;
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,15 @@ export class CaptionsLayer implements Layer {
|
||||
private state: MicSessionState = micSession.getState();
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private scrollback = 0;
|
||||
private lastLineCount = 0;
|
||||
private lastLine: CaptionLine | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribe = micSession.onState((state) => {
|
||||
if (state.captionLines.length !== this.lastLineCount) {
|
||||
this.lastLineCount = state.captionLines.length;
|
||||
// Watch the newest line itself: once the session's line cap is reached
|
||||
// the count stops changing even though new speech keeps arriving.
|
||||
const last = state.captionLines[state.captionLines.length - 1] ?? null;
|
||||
if (last !== this.lastLine) {
|
||||
this.lastLine = last;
|
||||
this.scrollback = 0;
|
||||
}
|
||||
this.state = state;
|
||||
@@ -36,7 +39,7 @@ export class CaptionsLayer implements Layer {
|
||||
|
||||
image.drawText(font, 12, 4, "Captions", 220);
|
||||
if (!state.captionsActive) {
|
||||
image.drawText(font, 12, 26, "Captions are off (enable in the Microphones menu).", 130);
|
||||
image.drawText(font, 12, 26, state.captionsNote, 130);
|
||||
}
|
||||
|
||||
const rows = this.buildRows(font, width - 36, state.captionLines);
|
||||
|
||||
@@ -16,6 +16,10 @@ declare const com: any;
|
||||
|
||||
// Qwen runs with a 4096-token context; leave room for the answer.
|
||||
const MAX_CONTEXT_CHARS = 7000;
|
||||
// The speaker roster grows with every voice ever heard; cap it and the
|
||||
// conversation list so they can't crowd the transcript excerpts out.
|
||||
const MAX_HEADER_CHARS = MAX_CONTEXT_CHARS / 2;
|
||||
const EXCERPTS_HEADING = "# Transcript excerpts\n";
|
||||
const MAX_KEYWORDS = 6;
|
||||
const SEGMENTS_PER_KEYWORD = 30;
|
||||
const RECENT_SEGMENT_LIMIT = 120;
|
||||
@@ -156,31 +160,57 @@ function buildContext(question: string): { context: string; note: string } {
|
||||
}
|
||||
}
|
||||
|
||||
const segments = [...segmentMap.values()].sort((a, b) => a.startedAt - b.startedAt);
|
||||
let header =
|
||||
`# Known speakers\n${speakerLines.join("\n") || "(none yet)"}\n\n` +
|
||||
`# Conversations${filters.sinceMs !== null || filters.emotion !== null ? " (filtered by the question)" : ""}\n` +
|
||||
`${sessionLines.join("\n") || "(none in range)"}\n\n`;
|
||||
let truncated = false;
|
||||
if (header.length > MAX_HEADER_CHARS) {
|
||||
header = `${header.slice(0, MAX_HEADER_CHARS)}\n(...)\n\n`;
|
||||
truncated = true;
|
||||
}
|
||||
|
||||
const sessionHeading = (sessionId: number) =>
|
||||
`\n## ${sessionTitles.get(sessionId) ?? `Conversation #${sessionId}`}`;
|
||||
const segmentText = (segment: SegmentRow) => {
|
||||
const speaker =
|
||||
segment.speakerId !== null ? (speakerNames.get(segment.speakerId) ?? "Speaker") : "Speaker";
|
||||
const emotion = segment.emotion && segment.emotion !== "neutral" ? ` [${segment.emotion}]` : "";
|
||||
const line = `[${formatWhen(segment.startedAt)}] ${speaker}${emotion}: ${segment.text}`;
|
||||
return segment.translation ? `${line}\n (translated: ${segment.translation})` : line;
|
||||
};
|
||||
|
||||
// Fill the remaining budget newest-first, so an overflowing history drops
|
||||
// its oldest excerpts rather than its most recent ones.
|
||||
const sorted = [...segmentMap.values()].sort((a, b) => a.startedAt - b.startedAt);
|
||||
let budget = MAX_CONTEXT_CHARS - header.length - EXCERPTS_HEADING.length;
|
||||
const segments: SegmentRow[] = [];
|
||||
let runSession = -1;
|
||||
for (let index = sorted.length - 1; index >= 0; index--) {
|
||||
const segment = sorted[index]!;
|
||||
let cost = segmentText(segment).length + 1;
|
||||
if (segment.sessionId !== runSession) cost += sessionHeading(segment.sessionId).length + 1;
|
||||
if (cost > budget) {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
budget -= cost;
|
||||
runSession = segment.sessionId;
|
||||
segments.push(segment);
|
||||
}
|
||||
segments.reverse();
|
||||
|
||||
const excerptLines: string[] = [];
|
||||
let currentSession = -1;
|
||||
for (const segment of segments) {
|
||||
if (segment.sessionId !== currentSession) {
|
||||
currentSession = segment.sessionId;
|
||||
excerptLines.push(`\n## ${sessionTitles.get(currentSession) ?? `Conversation #${currentSession}`}`);
|
||||
excerptLines.push(sessionHeading(currentSession));
|
||||
}
|
||||
const speaker =
|
||||
segment.speakerId !== null ? (speakerNames.get(segment.speakerId) ?? "Speaker") : "Speaker";
|
||||
const emotion = segment.emotion && segment.emotion !== "neutral" ? ` [${segment.emotion}]` : "";
|
||||
excerptLines.push(`[${formatWhen(segment.startedAt)}] ${speaker}${emotion}: ${segment.text}`);
|
||||
if (segment.translation) excerptLines.push(` (translated: ${segment.translation})`);
|
||||
excerptLines.push(segmentText(segment));
|
||||
}
|
||||
|
||||
let context =
|
||||
`# Known speakers\n${speakerLines.join("\n") || "(none yet)"}\n\n` +
|
||||
`# Conversations${filters.sinceMs !== null || filters.emotion !== null ? " (filtered by the question)" : ""}\n` +
|
||||
`${sessionLines.join("\n") || "(none in range)"}\n\n` +
|
||||
`# Transcript excerpts\n${excerptLines.join("\n") || "(no matching lines)"}`;
|
||||
let truncated = false;
|
||||
if (context.length > MAX_CONTEXT_CHARS) {
|
||||
context = context.slice(0, MAX_CONTEXT_CHARS);
|
||||
truncated = true;
|
||||
}
|
||||
const context = `${header}${EXCERPTS_HEADING}${excerptLines.join("\n") || "(no matching lines)"}`;
|
||||
const note =
|
||||
`${sessions.length} conversation${sessions.length === 1 ? "" : "s"}, ` +
|
||||
`${segments.length} transcript line${segments.length === 1 ? "" : "s"} consulted` +
|
||||
|
||||
@@ -286,6 +286,12 @@ export function angleInArc(azimuth: number, arcCenter: number, arcHalfWidth: num
|
||||
export class TempleAudioPipeline {
|
||||
private readonly beamformer: DelaySumBeamformer;
|
||||
private readonly anc = new InterMicNoiseCanceller();
|
||||
// Steering delays reach back before the start of each frame. Carrying the
|
||||
// previous frame's last samples keeps those real; zero-filling them (as a
|
||||
// standalone beam() does) dips the output at every frame boundary.
|
||||
private readonly carry: number;
|
||||
private frontTail: Float32Array;
|
||||
private rearTail: Float32Array;
|
||||
enableANC = true;
|
||||
|
||||
constructor(
|
||||
@@ -294,19 +300,38 @@ export class TempleAudioPipeline {
|
||||
geometry: MicGeometry = G2_DEFAULT_GEOMETRY,
|
||||
) {
|
||||
this.beamformer = new DelaySumBeamformer(sampleRate, geometry);
|
||||
const { front, rear } = geometry.positions[side];
|
||||
const spacing = Math.hypot(front.x - rear.x, front.y - rear.y, front.z - rear.z);
|
||||
this.carry = Math.ceil((spacing / geometry.speedOfSound) * sampleRate) + 1;
|
||||
this.frontTail = new Float32Array(this.carry);
|
||||
this.rearTail = new Float32Array(this.carry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process one frame's deinterleaved front/rear channels toward a
|
||||
* device-frame target, returning the enhanced mono beam for this temple.
|
||||
* Frames must be consecutive: each one continues the previous.
|
||||
*/
|
||||
process(front: Float32Array, rear: Float32Array, target: Bearing): Float32Array {
|
||||
const beam = this.beamformer.beam(this.side, front, rear, target);
|
||||
const n = Math.min(front.length, rear.length);
|
||||
const extendedFront = this.withTail(this.frontTail, front, n);
|
||||
const extendedRear = this.withTail(this.rearTail, rear, n);
|
||||
this.frontTail = extendedFront.slice(n);
|
||||
this.rearTail = extendedRear.slice(n);
|
||||
const beam = this.beamformer
|
||||
.beam(this.side, extendedFront, extendedRear, target)
|
||||
.slice(this.carry);
|
||||
if (!this.enableANC) return beam;
|
||||
// Null-steer reference: the difference channel carries off-axis noise.
|
||||
const n = Math.min(front.length, rear.length);
|
||||
const reference = new Float32Array(n);
|
||||
for (let i = 0; i < n; i++) reference[i] = front[i]! - rear[i]!;
|
||||
return this.anc.process(beam, reference);
|
||||
}
|
||||
|
||||
private withTail(tail: Float32Array, frame: Float32Array, n: number): Float32Array {
|
||||
const out = new Float32Array(tail.length + n);
|
||||
out.set(tail);
|
||||
out.set(frame.subarray(0, n), tail.length);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,9 @@ const microphonesApp: AppDefinition = {
|
||||
title: "Microphones",
|
||||
icon: "mic",
|
||||
boot: () => {
|
||||
// Prune expired captions/recordings per the retention settings.
|
||||
// Close out conversations and recordings a dead process left open, then
|
||||
// prune expired captions/recordings per the retention settings.
|
||||
micSession.recoverInterruptedConversations();
|
||||
void micSession.applyRetentionSweep();
|
||||
},
|
||||
launch: async (ctx) => {
|
||||
|
||||
@@ -110,6 +110,7 @@ class MicArrayController {
|
||||
* doesn't spend BLE bandwidth.
|
||||
*/
|
||||
apply(config: MicConfig): boolean {
|
||||
if (config.armHardware && !micControlSupported()) return false;
|
||||
const leftActive = templeActive(config, "left");
|
||||
const rightActive = templeActive(config, "right");
|
||||
if (leftActive || rightActive) {
|
||||
@@ -124,6 +125,17 @@ class MicArrayController {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-send CONFIGURE to one temple whose stream went quiet (a lost write, or
|
||||
* a temple that restarted on its own), leaving the other temple's stream
|
||||
* untouched.
|
||||
*/
|
||||
reconfigureTemple(config: MicConfig, side: MicSide): boolean {
|
||||
if (!templeActive(config, side)) return false;
|
||||
if (config.armHardware && !micControlSupported()) return false;
|
||||
return this.send("configure", config, `configure-${side}`, side === "right", side === "left");
|
||||
}
|
||||
|
||||
query(): boolean {
|
||||
return this.send("query", undefined, "query");
|
||||
}
|
||||
@@ -148,11 +160,11 @@ class MicArrayController {
|
||||
): boolean {
|
||||
const communicator = activeCommunicator();
|
||||
if (!communicator) return false;
|
||||
this.ensureStatusProxy(communicator);
|
||||
try {
|
||||
this.ensureStatusProxy(communicator);
|
||||
const record = encodeMicControl(op, config);
|
||||
communicator.sendFaceclawMicControl(toJavaBytes(record), label, rightTemple, leftTemple);
|
||||
return true;
|
||||
// False when the session wasn't ready and nothing was queued.
|
||||
return communicator.sendFaceclawMicControl(toJavaBytes(record), label, rightTemple, leftTemple) !== false;
|
||||
} catch (error) {
|
||||
console.warn(`mic control ${label} failed: ${error}`);
|
||||
return false;
|
||||
|
||||
@@ -75,6 +75,9 @@ export type MicModelState = {
|
||||
};
|
||||
|
||||
const downloaders = new Map<MicModelId, any>();
|
||||
// Each download's listener is referenced only from Java while it runs; hold
|
||||
// it here so it isn't GC'd before its callbacks.
|
||||
const downloadListeners = new Map<MicModelId, any>();
|
||||
const progress = new Map<MicModelId, number>();
|
||||
const stateListeners = new Set<(id: MicModelId, state: MicModelState) => void>();
|
||||
|
||||
@@ -142,6 +145,7 @@ function notifyStateChanged(id: MicModelId): void {
|
||||
|
||||
export function startMicModelDownload(id: MicModelId): void {
|
||||
if (!global.isAndroid || downloaders.has(id) || isMicModelReady(id)) return;
|
||||
progress.delete(id);
|
||||
downloadNextFile(id);
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
@@ -151,6 +155,7 @@ function downloadNextFile(id: MicModelId): void {
|
||||
const nextFile = definition.files.find((file) => !isFilePresent(id, file));
|
||||
if (!nextFile) {
|
||||
downloaders.delete(id);
|
||||
downloadListeners.delete(id);
|
||||
notifyStateChanged(id);
|
||||
return;
|
||||
}
|
||||
@@ -168,18 +173,28 @@ function downloadNextFile(id: MicModelId): void {
|
||||
onError: (message: string) => {
|
||||
console.error(`Mic model download failed (${id}/${nextFile.name}): ${message}`);
|
||||
downloaders.delete(id);
|
||||
downloadListeners.delete(id);
|
||||
notifyStateChanged(id);
|
||||
},
|
||||
});
|
||||
const downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${definition.baseUrl}${nextFile.remoteName}`,
|
||||
filePath(id, nextFile),
|
||||
nextFile.sha256,
|
||||
nextFile.sizeBytes,
|
||||
listener,
|
||||
);
|
||||
downloaders.set(id, downloader);
|
||||
downloader.start();
|
||||
downloadListeners.set(id, listener);
|
||||
try {
|
||||
const downloader = new com.faceclaw.app.FaceclawModelDownloader(
|
||||
`${definition.baseUrl}${nextFile.remoteName}`,
|
||||
filePath(id, nextFile),
|
||||
nextFile.sha256,
|
||||
nextFile.sizeBytes,
|
||||
listener,
|
||||
);
|
||||
downloaders.set(id, downloader);
|
||||
downloader.start();
|
||||
} catch (error) {
|
||||
// Without this, a dead entry would pin the model at "downloading".
|
||||
console.error(`Mic model download failed to start (${id}/${nextFile.name}): ${String(error)}`);
|
||||
downloaders.delete(id);
|
||||
downloadListeners.delete(id);
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
}
|
||||
|
||||
export function cancelMicModelDownload(id: MicModelId): void {
|
||||
@@ -187,6 +202,7 @@ export function cancelMicModelDownload(id: MicModelId): void {
|
||||
if (!downloader) return;
|
||||
downloader.cancel();
|
||||
downloaders.delete(id);
|
||||
downloadListeners.delete(id);
|
||||
notifyStateChanged(id);
|
||||
}
|
||||
|
||||
|
||||
+844
-236
File diff suppressed because it is too large.
Load diff
@@ -22,7 +22,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, micControlSupported, saveMicConfig } from "./mic-control";
|
||||
import { loadMicConfig, micArrayController, micControlSupported } from "./mic-control";
|
||||
import { isMicModelReady, micModelState, startMicModelDownload } from "./mic-models";
|
||||
import { micChannelLabel, type MicChannelKey, type MicConfig } from "./mic-protocol";
|
||||
import { micSession } from "./mic-session";
|
||||
@@ -72,13 +72,9 @@ const MENU_LAYOUT = {
|
||||
opaque: true,
|
||||
};
|
||||
|
||||
/** Persist a config change and re-apply it live when the array is streaming. */
|
||||
/** Persist a config change; the running session applies it live. */
|
||||
function updateMicConfig(mutate: (config: MicConfig) => MicConfig): void {
|
||||
const updated = mutate(loadMicConfig());
|
||||
saveMicConfig(updated);
|
||||
if (micSession.getState().mode === "extended") {
|
||||
micArrayController.apply(updated);
|
||||
}
|
||||
micSession.updateConfig(mutate);
|
||||
}
|
||||
|
||||
function micChannelToggleItem(
|
||||
@@ -457,8 +453,9 @@ export function createMicrophonesAppWindow(options: InProcessAppOptions): InProc
|
||||
}),
|
||||
configToggleItem(
|
||||
"Captions",
|
||||
() => captionsEnabledSetting.get(),
|
||||
() => micSession.setCaptionsEnabled(!captionsEnabledSetting.get()),
|
||||
// Shows off after repeated engine failures, so turning it on retries.
|
||||
() => micSession.captionsEnabled(),
|
||||
() => micSession.setCaptionsEnabled(!micSession.captionsEnabled()),
|
||||
undefined,
|
||||
captionsEnabledSetting.description,
|
||||
),
|
||||
|
||||
@@ -55,7 +55,9 @@ const SELF_PATTERNS = [
|
||||
new RegExp(`\\b[Mm]y name is ${NAME}`),
|
||||
new RegExp(`\\b[Ii](?:'| a)m ${NAME}`),
|
||||
new RegExp(`\\b[Cc]all me ${NAME}`),
|
||||
new RegExp(`\\b[Tt]his is ${NAME} speaking\\b`),
|
||||
// Phone-style: "this is Dana calling about..." is the speaker, not a
|
||||
// third party being introduced.
|
||||
new RegExp(`\\b[Tt]his is ${NAME} (?:[Ss]peaking|[Cc]alling)\\b`),
|
||||
];
|
||||
|
||||
const THIRD_PARTY_PATTERNS = [
|
||||
|
||||
@@ -35,6 +35,10 @@ function store(): any {
|
||||
return com.faceclaw.app.FaceclawConversationStore.getInstance(context);
|
||||
}
|
||||
|
||||
// Each run's listener is referenced only from Java while the diarizer works;
|
||||
// hold it here so it isn't GC'd before its callbacks.
|
||||
const activeListeners = new Set<any>();
|
||||
|
||||
export function rediarizeSession(
|
||||
sessionId: number,
|
||||
options: { numSpeakers?: number; threshold?: number },
|
||||
@@ -54,6 +58,7 @@ export function rediarizeSession(
|
||||
const listener = new com.faceclaw.app.FaceclawDiarizer.Listener({
|
||||
onProgress: (progress: number) => callbacks.onProgress(Number(progress)),
|
||||
onDone: (turnsJson: string) => {
|
||||
activeListeners.delete(listener);
|
||||
try {
|
||||
const turns = JSON.parse(String(turnsJson)) as Turn[];
|
||||
callbacks.onDone(applyTurns(sessionId, turns));
|
||||
@@ -61,17 +66,26 @@ export function rediarizeSession(
|
||||
callbacks.onError(`Applying diarization failed: ${String(error)}`);
|
||||
}
|
||||
},
|
||||
onError: (message: string) => callbacks.onError(String(message)),
|
||||
onError: (message: string) => {
|
||||
activeListeners.delete(listener);
|
||||
callbacks.onError(String(message));
|
||||
},
|
||||
});
|
||||
const diarizer = new com.faceclaw.app.FaceclawDiarizer(
|
||||
segmentationModel,
|
||||
embeddingModel,
|
||||
audioPath,
|
||||
options.numSpeakers ?? -1,
|
||||
options.threshold ?? 0.5,
|
||||
listener,
|
||||
);
|
||||
diarizer.start();
|
||||
activeListeners.add(listener);
|
||||
try {
|
||||
const diarizer = new com.faceclaw.app.FaceclawDiarizer(
|
||||
segmentationModel,
|
||||
embeddingModel,
|
||||
audioPath,
|
||||
options.numSpeakers ?? -1,
|
||||
options.threshold ?? 0.5,
|
||||
listener,
|
||||
);
|
||||
diarizer.start();
|
||||
} catch (error) {
|
||||
activeListeners.delete(listener);
|
||||
callbacks.onError(`Re-diarization failed to start: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
type StoredSegment = {
|
||||
@@ -140,6 +154,8 @@ function applyTurns(sessionId: number, turns: Turn[]): RediarizeResult {
|
||||
} else {
|
||||
const id = Number(store().createSpeaker(nextAutoName(), "#90A4AE", false, encodeEmbedding(centroid)));
|
||||
speakerRegistry.reload();
|
||||
// A failed insert returns -1; leave that cluster's segments as they were.
|
||||
if (id < 0) continue;
|
||||
clusterSpeaker.set(cluster, id);
|
||||
newSpeakers += 1;
|
||||
}
|
||||
|
||||
@@ -139,8 +139,9 @@ export class SpeakerRegistry {
|
||||
}));
|
||||
this.loaded = true;
|
||||
} catch (error) {
|
||||
// Keep the last good cache: emptying it would make the next utterance
|
||||
// enroll an already-known voice as a duplicate "Speaker N".
|
||||
console.error(`speaker reload failed: ${String(error)}`);
|
||||
this.profiles = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +195,8 @@ export class SpeakerRegistry {
|
||||
const id = Number(store().createSpeaker(name, color, false, encodeEmbedding(embedding)));
|
||||
this.reload();
|
||||
this.notifyChanged();
|
||||
const profile = this.byId(id)!;
|
||||
const profile = this.byId(id);
|
||||
if (!profile) throw new Error(`speaker "${name}" was not created (id ${id})`);
|
||||
return { profile, similarity: best?.similarity ?? 0, classification: "new", previousLastHeardAt: 0 };
|
||||
}
|
||||
|
||||
@@ -213,12 +215,14 @@ export class SpeakerRegistry {
|
||||
store().updateSpeakerEmbedding(existing.id, encodeEmbedding(embedding), CENTROID_UPDATE_CAP);
|
||||
this.reload();
|
||||
this.notifyChanged();
|
||||
return this.byId(existing.id)!;
|
||||
return this.byId(existing.id) ?? existing;
|
||||
}
|
||||
const id = Number(store().createSpeaker("Me", "#64B5F6", true, encodeEmbedding(embedding)));
|
||||
this.reload();
|
||||
this.notifyChanged();
|
||||
return this.byId(id)!;
|
||||
const profile = this.byId(id);
|
||||
if (!profile) throw new Error(`wearer profile was not created (id ${id})`);
|
||||
return profile;
|
||||
}
|
||||
|
||||
/** Is this utterance the wearer's own voice? Used to gate command recognition. */
|
||||
|
||||
@@ -18,7 +18,10 @@ const downloadedPairs = new Set<string>();
|
||||
export function deviceLanguage(): string {
|
||||
if (!global.isAndroid) return "en";
|
||||
try {
|
||||
return String(java.util.Locale.getDefault().getLanguage() || "en");
|
||||
// toLanguageTag() gives the modern code ("he", "id", "yi") that ML Kit
|
||||
// uses; getLanguage() returns the legacy "iw", "in", "ji".
|
||||
const tag = String(java.util.Locale.getDefault().toLanguageTag() || "");
|
||||
return tag.split("-")[0] || "en";
|
||||
} catch {
|
||||
return "en";
|
||||
}
|
||||
@@ -95,11 +98,18 @@ export function translateText(text: string, sourceLang: string, targetLang: stri
|
||||
if (!translator) return Promise.resolve("");
|
||||
const key = `${sourceLang}>${targetLang}`;
|
||||
return new Promise((resolve) => {
|
||||
// Also runs from the download listener, where a throw would escape into
|
||||
// the Play Services callback and leave this promise unsettled.
|
||||
const runTranslate = () => {
|
||||
translator
|
||||
.translate(text)
|
||||
.addOnSuccessListener(successListener((result: any) => resolve(String(result))))
|
||||
.addOnFailureListener(failureListener(() => resolve("")));
|
||||
try {
|
||||
translator
|
||||
.translate(text)
|
||||
.addOnSuccessListener(successListener((result: any) => resolve(String(result))))
|
||||
.addOnFailureListener(failureListener(() => resolve("")));
|
||||
} catch (error) {
|
||||
console.warn(`translation failed (${key}): ${error}`);
|
||||
resolve("");
|
||||
}
|
||||
};
|
||||
if (downloadedPairs.has(key)) {
|
||||
runTranslate();
|
||||
|
||||
@@ -250,8 +250,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
syncClock(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 });
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "resize-window":
|
||||
case "text-input":
|
||||
case "tool-call":
|
||||
break;
|
||||
|
||||
+26
-25
@@ -46,10 +46,11 @@ type FocusColumn = "actions" | "playlist";
|
||||
|
||||
/**
|
||||
* Music controller app: metadata + album art + transport controls for the
|
||||
* active Android media session (any player that publishes one), plus the
|
||||
* player's queue when it exposes one (scroll to a track, click to jump).
|
||||
* With no session, the front page lists the apps whose library can be
|
||||
* browsed (click one to open its MediaBrowseLayer).
|
||||
* active Android media session (any player that publishes one) or, on iOS,
|
||||
* the Apple Music system player, plus the player's queue when it exposes one
|
||||
* (scroll to a track, click to jump). With no session, the front page lists
|
||||
* the apps whose library can be browsed (click one to open its
|
||||
* MediaBrowseLayer).
|
||||
*/
|
||||
class MusicAppLayer implements Layer {
|
||||
// Watch swipes skip tracks: right = next, left = previous.
|
||||
@@ -83,7 +84,8 @@ class MusicAppLayer implements Layer {
|
||||
*/
|
||||
private libraryAppsFresh = false;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
/** The artVersion `art` was fetched for; -1 = not fetched yet. */
|
||||
private artVersion = -1;
|
||||
|
||||
isPlaylistFocused(): boolean {
|
||||
return this.focusColumn === "playlist";
|
||||
@@ -96,15 +98,11 @@ class MusicAppLayer implements Layer {
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
"Notification access is required to control Android media sessions. Tap to open settings.",
|
||||
width - 48,
|
||||
);
|
||||
const lines = wrapText(font, mediaControllerBridge.accessPrompt, width - 48);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(font, 24, 16 + index * lineStep(font), lines[index]!, 180);
|
||||
}
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings`, 110);
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} ${mediaControllerBridge.accessAction}`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -112,10 +110,12 @@ class MusicAppLayer implements Layer {
|
||||
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
|
||||
this.libraryAppsFresh = true;
|
||||
this.libraryMenu.setItems(apps);
|
||||
image.drawText(font, 24, 16, "No active media session.", 180);
|
||||
const prompt = apps.length
|
||||
? "Browse a music app's library, or start playback on the phone."
|
||||
: "Start playback in another app on the phone.";
|
||||
image.drawText(font, 24, 16, global.isIOS ? "Nothing is playing in Apple Music." : "No active media session.", 180);
|
||||
const prompt = global.isIOS
|
||||
? "Browse your library, or start playback on the phone."
|
||||
: apps.length
|
||||
? "Browse a music app's library, or start playback on the phone."
|
||||
: "Start playback in another app on the phone.";
|
||||
let y = 22 + lineStep(font);
|
||||
for (const line of wrapText(font, prompt, width - 48)) {
|
||||
image.drawText(font, 24, y, line, 150);
|
||||
@@ -228,7 +228,7 @@ class MusicAppLayer implements Layer {
|
||||
}
|
||||
if (!media.accessEnabled) {
|
||||
if (event.type === "click") {
|
||||
mediaControllerBridge.openNotificationAccessSettings();
|
||||
void mediaControllerBridge.requestAccess();
|
||||
} else if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
@@ -267,8 +267,8 @@ class MusicAppLayer implements Layer {
|
||||
const item = this.queueMenu.selectedItem;
|
||||
if (item) {
|
||||
await mediaControllerBridge.skipToQueueItem(item.id);
|
||||
// The player rebuilds the queue with the chosen track at index 0.
|
||||
this.queueMenu.select(0);
|
||||
// Android players rebuild the queue with the chosen track at index 0.
|
||||
if (mediaControllerBridge.skipRebuildsQueue) this.queueMenu.select(0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -371,12 +371,12 @@ class MusicAppLayer implements Layer {
|
||||
}
|
||||
|
||||
private drawArt(image: GrayImage, media: MediaControllerState): void {
|
||||
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||
// Retry while null: players (e.g. Spotify) often publish metadata before
|
||||
// the art bitmap is downloaded, then re-emit the same strings with the
|
||||
// bitmap attached. The no-art fetch path is a cheap near-no-op.
|
||||
if (key !== this.artKey || this.art === null) {
|
||||
this.artKey = key;
|
||||
// Keyed on artVersion, not the strings: players (e.g. Spotify) publish
|
||||
// metadata before the art bitmap is downloaded, or with the previous
|
||||
// track's bitmap after a quick double skip, then re-emit the same
|
||||
// strings with the right bitmap attached.
|
||||
if (media.artVersion !== this.artVersion) {
|
||||
this.artVersion = media.artVersion;
|
||||
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||
}
|
||||
if (this.art) {
|
||||
@@ -496,7 +496,8 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
iconLetter: "M",
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
// Music settings choose among Android media apps; iOS has only Apple Music.
|
||||
menuItems: () => global.isIOS ? [] : [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
|
||||
@@ -33,8 +33,7 @@ import {
|
||||
navigateDestinationAddressDraftSetting,
|
||||
navigateDestinationNameDraftSetting,
|
||||
navigateRememberRecentSetting,
|
||||
navigateDisplayModeSetting,
|
||||
navigateVerticalPositionSetting,
|
||||
navigateDisplayLayoutSetting,
|
||||
mapboxApiKeySetting,
|
||||
enumSettingMenuItem,
|
||||
type ConfigSettingString,
|
||||
@@ -935,10 +934,7 @@ function windowMenuItems(): MenuItem[] {
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push(
|
||||
enumSettingMenuItem(navigateDisplayModeSetting),
|
||||
enumSettingMenuItem(navigateVerticalPositionSetting),
|
||||
);
|
||||
items.push(enumSettingMenuItem(navigateDisplayLayoutSetting));
|
||||
items.push(submenuItem("Saved destinations", (ctx) => {
|
||||
ctx.stack.push(new WindowMenuLayer("Saved destinations", savedDestinationMenuItems()));
|
||||
}));
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { inProcessTrayWidget, type AppDefinition } from "../app-definition";
|
||||
import { createNightscoutAppWindow, startNightscoutTrayIcon, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app";
|
||||
import { startMissedBolusMonitor } from "./missed-bolus-monitor";
|
||||
|
||||
const nightscoutApp: AppDefinition = {
|
||||
appId: "nightscout",
|
||||
title: "Nightscout",
|
||||
icon: "nightscout",
|
||||
boot: () => startNightscoutTrayIcon(),
|
||||
boot: (ctx) => {
|
||||
// Selected in the switcher, the tray readout shows the app's window.
|
||||
startNightscoutTrayIcon(
|
||||
inProcessTrayWidget(ctx, NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow),
|
||||
);
|
||||
startMissedBolusMonitor(ctx);
|
||||
},
|
||||
launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
/**
|
||||
* Missed-bolus detector: given a recent window of CGM readings and
|
||||
* treatments, decide whether glucose is rising the way an unannounced meal
|
||||
* does, with no meal bolus, manual bolus or carb entry to account for it.
|
||||
*
|
||||
* Deliberately self-contained (no imports; plain data in, plain data out) so
|
||||
* it can move to the server side of Nightscout unchanged, and so it can be
|
||||
* iterated on against recorded history with
|
||||
* scripts/nightscout-missed-bolus-replay.cjs. The Faceclaw side (when to run
|
||||
* it, cooldown, the glasses notification) is in missed-bolus-monitor.ts.
|
||||
*
|
||||
* Glucose is mg/dL throughout: Nightscout stores sgv in mg/dL whatever the
|
||||
* site's display units.
|
||||
*/
|
||||
|
||||
export type GlucoseReading = { timeMs: number; mgdl: number };
|
||||
|
||||
/**
|
||||
* - meal: a meal/snack/wizard bolus, or any bolus entered together with carbs.
|
||||
* - manual: a bolus explicitly marked user-initiated (e.g. Loop's `automatic: false`).
|
||||
* - automatic: a bolus the loop issued (SMB, automatic bolus).
|
||||
* - ambiguous: not marked either way. OpenAPS on a Medtronic pump uploads its
|
||||
* SMBs as plain "Correction Bolus" records, indistinguishable from a manual
|
||||
* correction except by size; `ambiguousBolusMinUnits` decides.
|
||||
*/
|
||||
export type BolusKind = "meal" | "manual" | "automatic" | "ambiguous";
|
||||
export type BolusEvent = { timeMs: number; units: number; kind: BolusKind };
|
||||
export type CarbEntry = { timeMs: number; grams: number };
|
||||
|
||||
export type MissedBolusInput = {
|
||||
/** The time to classify as of. Readings and treatments after it are ignored. */
|
||||
nowMs: number;
|
||||
glucose: readonly GlucoseReading[];
|
||||
boluses: readonly BolusEvent[];
|
||||
carbs: readonly CarbEntry[];
|
||||
};
|
||||
|
||||
export type MissedBolusOptions = {
|
||||
/** The latest reading must be at most this old, or there is nothing current to judge. */
|
||||
maxReadingAgeMs: number;
|
||||
/** How far back from the latest reading to look for where the rise started. */
|
||||
lookbackMs: number;
|
||||
/** A gap between readings longer than this ends the window (the CGM dropped out). */
|
||||
maxGapMs: number;
|
||||
/** Recent span the rate of rise is fitted over (least squares). */
|
||||
slopeWindowMs: number;
|
||||
/** Fewest readings the rate fit needs. */
|
||||
minSlopeReadings: number;
|
||||
/** Rise from the window's lowest reading to the latest. */
|
||||
minRiseMgdl: number;
|
||||
/** The latest reading must be at least this high. */
|
||||
minGlucoseMgdl: number;
|
||||
/** Rises from below this are taken as recovering from a low (rescue carbs go unbolused). */
|
||||
hypoNadirMgdl: number;
|
||||
/** The rise must have lasted at least this long, to rule out sensor noise. */
|
||||
minRiseDurationMs: number;
|
||||
/** Rate of rise over the slope window, mg/dL per minute. */
|
||||
minRateMgdlPerMin: number;
|
||||
/** A bolus or carb entry this long before the rise began still accounts for it (pre-bolusing). */
|
||||
preBolusWindowMs: number;
|
||||
/** Smallest meal or manual bolus that accounts for a rise. */
|
||||
minManualBolusUnits: number;
|
||||
/** Smallest unmarked bolus taken as manual rather than an SMB. */
|
||||
ambiguousBolusMinUnits: number;
|
||||
/** Whether a carb entry with no bolus accounts for a rise (the user evidently knows they ate). */
|
||||
carbsCountAsAnnouncement: boolean;
|
||||
};
|
||||
|
||||
export const DEFAULT_MISSED_BOLUS_OPTIONS: Readonly<MissedBolusOptions> = {
|
||||
maxReadingAgeMs: 12 * 60_000,
|
||||
lookbackMs: 90 * 60_000,
|
||||
maxGapMs: 16 * 60_000,
|
||||
slopeWindowMs: 20 * 60_000,
|
||||
minSlopeReadings: 3,
|
||||
minRiseMgdl: 40,
|
||||
minGlucoseMgdl: 140,
|
||||
hypoNadirMgdl: 70,
|
||||
minRiseDurationMs: 20 * 60_000,
|
||||
minRateMgdlPerMin: 1.5,
|
||||
preBolusWindowMs: 60 * 60_000,
|
||||
minManualBolusUnits: 0.5,
|
||||
ambiguousBolusMinUnits: 1.5,
|
||||
carbsCountAsAnnouncement: true,
|
||||
};
|
||||
|
||||
export type GlucoseRise = {
|
||||
/** When the rise started: the lowest reading in the window (the latest, if tied). */
|
||||
onsetMs: number;
|
||||
nadirMgdl: number;
|
||||
latestMs: number;
|
||||
latestMgdl: number;
|
||||
riseMgdl: number;
|
||||
/** Least-squares rate over the slope window, mg/dL per minute. */
|
||||
rateMgdlPerMin: number;
|
||||
};
|
||||
|
||||
export type MissedBolusClassification = "missed-bolus" | "no-alert" | "insufficient-data";
|
||||
|
||||
export type MissedBolusResult = {
|
||||
classification: MissedBolusClassification;
|
||||
/** One-line explanation, for logs and the replay tool. */
|
||||
reason: string;
|
||||
/** The rise that was judged, when there was enough data to measure one. */
|
||||
rise: GlucoseRise | null;
|
||||
};
|
||||
|
||||
export function detectMissedBolus(
|
||||
input: MissedBolusInput,
|
||||
overrides: Partial<MissedBolusOptions> = {},
|
||||
): MissedBolusResult {
|
||||
const options: MissedBolusOptions = { ...DEFAULT_MISSED_BOLUS_OPTIONS, ...overrides };
|
||||
const { nowMs } = input;
|
||||
const readings = usableReadings(input.glucose, nowMs);
|
||||
const latest = readings[readings.length - 1];
|
||||
if (!latest) return insufficient("no glucose readings");
|
||||
if (nowMs - latest.timeMs > options.maxReadingAgeMs) {
|
||||
return insufficient(`latest reading is ${formatMinutes(nowMs - latest.timeMs)} old`);
|
||||
}
|
||||
|
||||
// The contiguous run of readings ending at the latest, within the lookback.
|
||||
const windowStartMs = latest.timeMs - options.lookbackMs;
|
||||
let first = readings.length - 1;
|
||||
while (first > 0) {
|
||||
const previous = readings[first - 1]!;
|
||||
if (previous.timeMs < windowStartMs || readings[first]!.timeMs - previous.timeMs > options.maxGapMs) break;
|
||||
first--;
|
||||
}
|
||||
const window = readings.slice(first);
|
||||
let nadir = window[0]!;
|
||||
for (const reading of window) {
|
||||
if (reading.mgdl <= nadir.mgdl) nadir = reading;
|
||||
}
|
||||
|
||||
const slopeStartMs = Math.max(nadir.timeMs, latest.timeMs - options.slopeWindowMs);
|
||||
const slopeReadings = window.filter((reading) => reading.timeMs >= slopeStartMs);
|
||||
const rise: GlucoseRise = {
|
||||
onsetMs: nadir.timeMs,
|
||||
nadirMgdl: nadir.mgdl,
|
||||
latestMs: latest.timeMs,
|
||||
latestMgdl: latest.mgdl,
|
||||
riseMgdl: latest.mgdl - nadir.mgdl,
|
||||
rateMgdlPerMin: slopeReadings.length >= 2 ? slopePerMinute(slopeReadings) : 0,
|
||||
};
|
||||
const noAlert = (reason: string): MissedBolusResult => ({ classification: "no-alert", reason, rise });
|
||||
|
||||
if (rise.riseMgdl < options.minRiseMgdl) {
|
||||
return noAlert(`rise of ${rise.riseMgdl} mg/dL is under ${options.minRiseMgdl}`);
|
||||
}
|
||||
if (latest.mgdl < options.minGlucoseMgdl) {
|
||||
return noAlert(`glucose ${latest.mgdl} mg/dL is under ${options.minGlucoseMgdl}`);
|
||||
}
|
||||
if (nadir.mgdl < options.hypoNadirMgdl) {
|
||||
return noAlert(`rising from a low of ${nadir.mgdl} mg/dL`);
|
||||
}
|
||||
if (latest.timeMs - nadir.timeMs < options.minRiseDurationMs) {
|
||||
return noAlert(`rise has lasted only ${formatMinutes(latest.timeMs - nadir.timeMs)}`);
|
||||
}
|
||||
if (slopeReadings.length < options.minSlopeReadings) {
|
||||
return { classification: "insufficient-data", reason: `only ${slopeReadings.length} readings to fit a rate`, rise };
|
||||
}
|
||||
if (rise.rateMgdlPerMin < options.minRateMgdlPerMin) {
|
||||
return noAlert(`rising ${formatRate(rise.rateMgdlPerMin)}, under ${formatRate(options.minRateMgdlPerMin)}`);
|
||||
}
|
||||
|
||||
const coverStartMs = nadir.timeMs - options.preBolusWindowMs;
|
||||
const inCoverWindow = (timeMs: number) => timeMs >= coverStartMs && timeMs <= nowMs;
|
||||
const bolus = latestMatching(input.boluses, (event) => inCoverWindow(event.timeMs) && accountsForRise(event, options));
|
||||
if (bolus) {
|
||||
return noAlert(`${formatUnits(bolus.units)} U ${bolus.kind} bolus ${formatMinutes(nowMs - bolus.timeMs)} ago`);
|
||||
}
|
||||
if (options.carbsCountAsAnnouncement) {
|
||||
const carbs = latestMatching(input.carbs, (entry) => inCoverWindow(entry.timeMs) && entry.grams > 0);
|
||||
if (carbs) return noAlert(`${carbs.grams} g carbs entered ${formatMinutes(nowMs - carbs.timeMs)} ago`);
|
||||
}
|
||||
return {
|
||||
classification: "missed-bolus",
|
||||
reason: `up ${rise.riseMgdl} mg/dL in ${formatMinutes(latest.timeMs - nadir.timeMs)}, rising ${formatRate(rise.rateMgdlPerMin)}, no bolus`,
|
||||
rise,
|
||||
};
|
||||
}
|
||||
|
||||
function insufficient(reason: string): MissedBolusResult {
|
||||
return { classification: "insufficient-data", reason, rise: null };
|
||||
}
|
||||
|
||||
function accountsForRise(bolus: BolusEvent, options: MissedBolusOptions): boolean {
|
||||
switch (bolus.kind) {
|
||||
case "meal":
|
||||
case "manual":
|
||||
return bolus.units >= options.minManualBolusUnits;
|
||||
case "ambiguous":
|
||||
return bolus.units >= Math.max(options.minManualBolusUnits, options.ambiguousBolusMinUnits);
|
||||
case "automatic":
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finite, in-range readings at or before now, oldest first, one per
|
||||
* timestamp. Dexcom reports 39 for "LOW" and smaller values as error codes.
|
||||
*/
|
||||
function usableReadings(glucose: readonly GlucoseReading[], nowMs: number): GlucoseReading[] {
|
||||
const sorted = glucose
|
||||
.filter((reading) => Number.isFinite(reading.timeMs) && Number.isFinite(reading.mgdl)
|
||||
&& reading.mgdl >= 39 && reading.mgdl <= 600 && reading.timeMs <= nowMs)
|
||||
.slice()
|
||||
.sort((a, b) => a.timeMs - b.timeMs);
|
||||
return sorted.filter((reading, index) => index === 0 || reading.timeMs !== sorted[index - 1]!.timeMs);
|
||||
}
|
||||
|
||||
/** Least-squares slope through the readings, in mg/dL per minute. */
|
||||
function slopePerMinute(readings: readonly GlucoseReading[]): number {
|
||||
const t0 = readings[0]!.timeMs;
|
||||
const n = readings.length;
|
||||
let sumX = 0, sumY = 0, sumXY = 0, sumXX = 0;
|
||||
for (const reading of readings) {
|
||||
const x = (reading.timeMs - t0) / 60_000;
|
||||
sumX += x;
|
||||
sumY += reading.mgdl;
|
||||
sumXY += x * reading.mgdl;
|
||||
sumXX += x * x;
|
||||
}
|
||||
const denominator = n * sumXX - sumX * sumX;
|
||||
return denominator > 0 ? (n * sumXY - sumX * sumY) / denominator : 0;
|
||||
}
|
||||
|
||||
function latestMatching<T extends { timeMs: number }>(events: readonly T[], predicate: (event: T) => boolean): T | null {
|
||||
let found: T | null = null;
|
||||
for (const event of events) {
|
||||
if (predicate(event) && (!found || event.timeMs > found.timeMs)) found = event;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function formatMinutes(ms: number): string {
|
||||
return `${Math.round(ms / 60_000)} min`;
|
||||
}
|
||||
|
||||
function formatRate(mgdlPerMin: number): string {
|
||||
return `${mgdlPerMin.toFixed(1)} mg/dL/min`;
|
||||
}
|
||||
|
||||
function formatUnits(units: number): string {
|
||||
return `${Math.round(units * 100) / 100}`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Nightscout record normalization, shared by the Faceclaw bridge and a
|
||||
// server-side port.
|
||||
|
||||
/** The fields of a Nightscout treatment record the detector reads. */
|
||||
export type NightscoutTreatmentRecord = {
|
||||
eventType?: unknown;
|
||||
created_at?: unknown;
|
||||
timestamp?: unknown;
|
||||
insulin?: unknown;
|
||||
carbs?: unknown;
|
||||
automatic?: unknown;
|
||||
isSMB?: unknown;
|
||||
type?: unknown;
|
||||
wizard?: unknown;
|
||||
};
|
||||
|
||||
/** The fields of a Nightscout entry record the detector reads. */
|
||||
export type NightscoutEntryRecord = { date?: unknown; sgv?: unknown };
|
||||
|
||||
const MEAL_BOLUS_EVENT_TYPES = new Set(["Meal Bolus", "Snack Bolus", "Bolus Wizard", "Combo Bolus"]);
|
||||
|
||||
/**
|
||||
* How a treatment's insulin was given. Uploaders mark automatic boluses
|
||||
* differently: Loop sets `automatic`, AndroidAPS `isSMB`, Trio/OpenAPS
|
||||
* `type: "SMB"` or eventType "SMB". Meal boluses carry carbs or a wizard
|
||||
* record, or a meal-type eventType.
|
||||
*/
|
||||
export function bolusKindFromTreatment(treatment: NightscoutTreatmentRecord): BolusKind {
|
||||
const eventType = typeof treatment.eventType === "string" ? treatment.eventType : "";
|
||||
if (treatment.automatic === true || treatment.isSMB === true || treatment.type === "SMB" || eventType === "SMB") {
|
||||
return "automatic";
|
||||
}
|
||||
if (MEAL_BOLUS_EVENT_TYPES.has(eventType) || positiveNumber(treatment.carbs) !== null
|
||||
|| (typeof treatment.wizard === "object" && treatment.wizard !== null)) {
|
||||
return "meal";
|
||||
}
|
||||
if (treatment.automatic === false || treatment.isSMB === false) return "manual";
|
||||
return "ambiguous";
|
||||
}
|
||||
|
||||
/** Boluses and carb entries from raw Nightscout treatment records, oldest first. */
|
||||
export function normalizeNightscoutTreatments(treatments: readonly NightscoutTreatmentRecord[]): {
|
||||
boluses: BolusEvent[];
|
||||
carbs: CarbEntry[];
|
||||
} {
|
||||
const boluses: BolusEvent[] = [];
|
||||
const carbs: CarbEntry[] = [];
|
||||
for (const treatment of treatments) {
|
||||
const timeMs = treatmentTimeMs(treatment);
|
||||
if (timeMs === null) continue;
|
||||
const units = positiveNumber(treatment.insulin);
|
||||
if (units !== null) boluses.push({ timeMs, units, kind: bolusKindFromTreatment(treatment) });
|
||||
const grams = positiveNumber(treatment.carbs);
|
||||
if (grams !== null) carbs.push({ timeMs, grams });
|
||||
}
|
||||
boluses.sort((a, b) => a.timeMs - b.timeMs);
|
||||
carbs.sort((a, b) => a.timeMs - b.timeMs);
|
||||
return { boluses, carbs };
|
||||
}
|
||||
|
||||
/** Glucose readings from raw Nightscout sgv entries, oldest first. */
|
||||
export function normalizeNightscoutEntries(entries: readonly NightscoutEntryRecord[]): GlucoseReading[] {
|
||||
const readings: GlucoseReading[] = [];
|
||||
for (const entry of entries) {
|
||||
const timeMs = Number(entry.date);
|
||||
const mgdl = Number(entry.sgv);
|
||||
if (Number.isFinite(timeMs) && timeMs > 0 && Number.isFinite(mgdl)) readings.push({ timeMs, mgdl });
|
||||
}
|
||||
return readings.sort((a, b) => a.timeMs - b.timeMs);
|
||||
}
|
||||
|
||||
function treatmentTimeMs(treatment: NightscoutTreatmentRecord): number | null {
|
||||
for (const value of [treatment.created_at, treatment.timestamp]) {
|
||||
if (typeof value === "number" && Number.isFinite(value) && value > 0) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const numeric = Number(value);
|
||||
const parsed = Number.isFinite(numeric) ? numeric : Date.parse(value);
|
||||
if (Number.isFinite(parsed) && parsed > 0) return parsed;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function positiveNumber(value: unknown): number | null {
|
||||
if (typeof value !== "number" && typeof value !== "string") return null;
|
||||
if (typeof value === "string" && !value.trim()) return null;
|
||||
const numeric = Number(value);
|
||||
return Number.isFinite(numeric) && numeric > 0 ? numeric : null;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { type AppContext } from "../app-definition";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { nightscoutMissedBolusAlertsSetting, nightscoutMissedBolusCooldownSetting } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { detectMissedBolus, type MissedBolusInput } from "./missed-bolus-detector";
|
||||
import { MissedBolusNotificationLayer } from "./missed-bolus-notification";
|
||||
|
||||
/** When the last alert was shown, persisted so a restart doesn't reset the cooldown. */
|
||||
const LAST_ALERT_STORAGE_KEY = "integrations.nightscout.missedBolusLastAlertMs";
|
||||
|
||||
let started = false;
|
||||
let lastInputKey = "";
|
||||
let lastSummary = "";
|
||||
|
||||
/**
|
||||
* Runs the missed-bolus detector each time a Nightscout sync brings new
|
||||
* readings or treatments, and shows a glasses notification when it fires
|
||||
* outside the cooldown. Shell-lifetime, started from the app's boot hook.
|
||||
*/
|
||||
export function startMissedBolusMonitor(ctx: AppContext): void {
|
||||
if (started) return;
|
||||
started = true;
|
||||
nightscoutBridge.onStateChange((state) => checkForMissedBolus(state, ctx));
|
||||
}
|
||||
|
||||
function checkForMissedBolus(state: NightscoutState, ctx: AppContext): void {
|
||||
if (!nightscoutMissedBolusAlertsSetting.get() || !state.available) return;
|
||||
const key = inputKey(state);
|
||||
if (key === lastInputKey) return;
|
||||
lastInputKey = key;
|
||||
|
||||
const nowMs = Date.now();
|
||||
const result = detectMissedBolus(detectorInput(state, nowMs));
|
||||
const summary = `${result.classification}: ${result.reason}`;
|
||||
if (summary !== lastSummary) {
|
||||
lastSummary = summary;
|
||||
console.log(`missed-bolus detector: ${summary}`);
|
||||
}
|
||||
if (result.classification !== "missed-bolus" || !result.rise) return;
|
||||
|
||||
const lastAlertMs = Number(getStringSetting(LAST_ALERT_STORAGE_KEY, "0")) || 0;
|
||||
const cooldownMs = Number(nightscoutMissedBolusCooldownSetting.get()) * 60_000;
|
||||
// A last-alert time in the future means the clock moved back; don't let it block alerts.
|
||||
if (lastAlertMs <= nowMs && nowMs - lastAlertMs < cooldownMs) return;
|
||||
setStringSetting(LAST_ALERT_STORAGE_KEY, String(nowMs));
|
||||
ctx.appendLog(`missed-bolus alert: ${result.reason}`);
|
||||
const rise = result.rise;
|
||||
shell.postAppNotification({
|
||||
key: `nightscout-missed-bolus:${nowMs}`,
|
||||
createLayer: (close) => new MissedBolusNotificationLayer(rise, nowMs, close, () => {
|
||||
void ctx.launchApp("nightscout");
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/** Changes whenever a sync brings a new reading or a treatment the detector reads. */
|
||||
function inputKey(state: NightscoutState): string {
|
||||
return [
|
||||
state.latest?.timestampMs ?? 0,
|
||||
state.boluses.map((bolus) => bolus.timestampMs).join(","),
|
||||
state.carbs.map((entry) => entry.timestampMs).join(","),
|
||||
].join("|");
|
||||
}
|
||||
|
||||
function detectorInput(state: NightscoutState, nowMs: number): MissedBolusInput {
|
||||
return {
|
||||
nowMs,
|
||||
glucose: state.history.map((point) => ({ timeMs: point.timestampMs, mgdl: point.sgv })),
|
||||
boluses: state.boluses.map((bolus) => ({ timeMs: bolus.timestampMs, units: bolus.insulin, kind: bolus.kind })),
|
||||
carbs: state.carbs.map((entry) => ({ timeMs: entry.timestampMs, grams: entry.carbs })),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { renderIcon } from "../../graphics/icons";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { formatRelativeTime } from "../../util/date-util";
|
||||
import { nightscoutBridge } from "../../native/nightscout-bridge";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import {
|
||||
NOTIFICATION_CONTENT_X,
|
||||
createNotificationActionMenu,
|
||||
drawNotificationHeader,
|
||||
notificationActionMenuBox,
|
||||
notificationContentWidth,
|
||||
} from "../../ui/notifications";
|
||||
import { type GlucoseRise } from "./missed-bolus-detector";
|
||||
import {
|
||||
drawDirectionIndicator,
|
||||
drawNightscoutGraph,
|
||||
drawNightscoutValueStrikeThrough,
|
||||
formatDelta,
|
||||
isNightscoutPointStale,
|
||||
} from "./nightscout";
|
||||
|
||||
const ICON_SIZE = 24;
|
||||
const GRAPH_GAP = 6;
|
||||
const GRAPH_BOTTOM_MARGIN = 12;
|
||||
const MIN_GRAPH_HEIGHT = 32;
|
||||
|
||||
type ActionItem = { label: string; run: () => void };
|
||||
|
||||
/**
|
||||
* The missed-bolus alert, laid out like an Android notification's detail view
|
||||
* (header, text, right-hand action column), with the live glucose readout and
|
||||
* 2-hour graph below the text.
|
||||
*/
|
||||
export class MissedBolusNotificationLayer implements Layer {
|
||||
private readonly menu = createNotificationActionMenu<ActionItem>();
|
||||
|
||||
constructor(
|
||||
private readonly rise: GlucoseRise,
|
||||
private readonly postedAtMs: number,
|
||||
private readonly close: () => void,
|
||||
openNightscout: () => void,
|
||||
) {
|
||||
this.menu.setItems([
|
||||
{ label: "Dismiss", run: close },
|
||||
{ label: "Open Nightscout", run: () => {
|
||||
close();
|
||||
openNightscout();
|
||||
} },
|
||||
]);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const step = lineStep(font);
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const contentX = NOTIFICATION_CONTENT_X;
|
||||
const contentWidth = notificationContentWidth(width);
|
||||
const nowMs = Date.now();
|
||||
|
||||
let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`);
|
||||
image.drawText(font, contentX, y, "Missed bolus?", 230);
|
||||
y += step;
|
||||
const minutes = Math.round((this.rise.latestMs - this.rise.onsetMs) / 60_000);
|
||||
const body = `Glucose rose ${this.rise.riseMgdl} mg/dL in ${minutes} min with no bolus or carbs entered.`;
|
||||
for (const line of wrapText(font, body, contentWidth)) {
|
||||
image.drawText(font, contentX, y, line, 190);
|
||||
y += step;
|
||||
}
|
||||
|
||||
// Live readout and graph: the modal repaints with each shell render, so
|
||||
// these follow new readings (and a bolus, once entered) while it is up.
|
||||
const nightscout = nightscoutBridge.snapshot();
|
||||
const latest = nightscout.latest;
|
||||
if (latest) {
|
||||
const value = `${latest.sgv}`;
|
||||
image.drawText(font, contentX, y, value, 230);
|
||||
if (isNightscoutPointStale(latest, nowMs)) {
|
||||
drawNightscoutValueStrikeThrough(image, contentX, y + (font.lineHeight >> 1), font.measureText(value));
|
||||
}
|
||||
let x = contentX + font.measureText(`${value} `);
|
||||
x += drawDirectionIndicator(image, font, x, y, nightscout.direction, 200);
|
||||
x += font.measureText(" ");
|
||||
const iob = nightscout.iob === null ? "" : ` IOB ${nightscout.iob.toFixed(2)}`;
|
||||
image.drawText(font, x, y, `${formatDelta(nightscout.delta)}${iob}`, 180);
|
||||
y += step;
|
||||
}
|
||||
const graphTop = y + GRAPH_GAP;
|
||||
const graphHeight = height - GRAPH_BOTTOM_MARGIN - graphTop;
|
||||
if (graphHeight >= MIN_GRAPH_HEIGHT) {
|
||||
drawNightscoutGraph(image, { x: contentX, y: graphTop, width: contentWidth, height: graphHeight }, nightscout, nowMs, font);
|
||||
}
|
||||
|
||||
this.menu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused());
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
this.close();
|
||||
} else if (event.type === "click") {
|
||||
this.menu.selectedItem?.run();
|
||||
} else if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
await this.menu.handleInput(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { evaluateNightscoutAlerts } from "./nightscout-alerts";
|
||||
import { loadNightscoutThresholds, nightscoutAlwaysShowInTopBarSetting, onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { shell, type TrayWidget } from "../../ui/shell/shell";
|
||||
|
||||
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
|
||||
export const NIGHTSCOUT_SURFACE_ID = "window:nightscout";
|
||||
@@ -23,11 +23,12 @@ const TRAY_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||
|
||||
const windowRenders = new Set<() => void>();
|
||||
let trayStarted = false;
|
||||
let trayWidget: TrayWidget | undefined;
|
||||
let trayTick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
function syncNightscout(): void {
|
||||
const visible = windowRenders.size > 0 || nightscoutAlwaysShowInTopBarSetting.get();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, visible ? buildNightscoutTrayIcon(nightscoutBridge.snapshot()) : null);
|
||||
shell.setTrayIcon(TRAY_ICON_ID, visible ? buildNightscoutTrayIcon(nightscoutBridge.snapshot()) : null, trayWidget);
|
||||
for (const render of windowRenders) render();
|
||||
// Age warnings and stale glucose must advance even during a stalled fetch.
|
||||
if (visible && trayTick === null) trayTick = setInterval(syncNightscout, 60_000);
|
||||
@@ -37,8 +38,12 @@ function syncNightscout(): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** Shell-lifetime subscription so the tray can be enabled before any window opens. */
|
||||
export function startNightscoutTrayIcon(): void {
|
||||
/**
|
||||
* Shell-lifetime subscription so the tray can be enabled before any window
|
||||
* opens; `widget` makes it selectable in the switcher.
|
||||
*/
|
||||
export function startNightscoutTrayIcon(widget?: TrayWidget): void {
|
||||
trayWidget ??= widget;
|
||||
if (trayStarted) return;
|
||||
trayStarted = true;
|
||||
nightscoutBridge.onStateChange(syncNightscout);
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
nightscoutBatteryLowSetting,
|
||||
nightscoutMaxLoopAgeSetting,
|
||||
nightscoutAlwaysShowInTopBarSetting,
|
||||
nightscoutMissedBolusAlertsSetting,
|
||||
nightscoutMissedBolusCooldownSetting,
|
||||
toggleSettingMenuItem,
|
||||
nightscoutApiTokenSetting,
|
||||
nightscoutSiteUrlSetting,
|
||||
@@ -28,6 +30,7 @@ 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;
|
||||
|
||||
/** Draws the trend indicator; returns its width. */
|
||||
export function drawDirectionIndicator(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
@@ -35,16 +38,10 @@ export function drawDirectionIndicator(
|
||||
y: number,
|
||||
direction: string,
|
||||
shade: number,
|
||||
): void {
|
||||
const label = directionGlyphLabel(font, direction);
|
||||
if (label) {
|
||||
image.drawText(font, x, y, label, shade);
|
||||
return;
|
||||
}
|
||||
|
||||
if (direction) {
|
||||
image.drawText(font, x, y, truncateLine(direction, 10), shade);
|
||||
}
|
||||
): number {
|
||||
const label = directionGlyphLabel(font, direction) || (direction ? truncateLine(direction, 10) : "");
|
||||
if (label) image.drawText(font, x, y, label, shade);
|
||||
return font.measureText(label);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -425,6 +422,8 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
textSettingMenuItem(nightscoutCartridgeLowSetting),
|
||||
textSettingMenuItem(nightscoutBatteryLowSetting),
|
||||
textSettingMenuItem(nightscoutMaxLoopAgeSetting),
|
||||
toggleSettingMenuItem(nightscoutMissedBolusAlertsSetting),
|
||||
textSettingMenuItem(nightscoutMissedBolusCooldownSetting),
|
||||
]);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { isNotificationListenerEnabled, requestNotificationListenerAccess } from "../../native/notification-access";
|
||||
import { notificationListenerState, requestNotificationListenerAccess } from "../../native/notification-access";
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from "./notifications-app";
|
||||
|
||||
@@ -7,10 +7,11 @@ const notificationsApp: AppDefinition = {
|
||||
title: "Notifications",
|
||||
icon: "bell",
|
||||
launch: (ctx) => {
|
||||
// Without notification-listener access the tray reads as empty; prompt
|
||||
// the user on the phone so the on-glasses "grant permission" message is
|
||||
// actionable. The app still opens to show that message.
|
||||
if (!isNotificationListenerEnabled()) {
|
||||
// Without notification-listener access (or with a listener Android won't
|
||||
// rebind) the tray reads as empty; open the access screen on the phone so
|
||||
// the on-glasses message is actionable. The app still opens to show it.
|
||||
const listenerState = notificationListenerState();
|
||||
if (listenerState === "no-access" || listenerState === "stuck") {
|
||||
requestNotificationListenerAccess();
|
||||
}
|
||||
return ctx.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NotificationsListLayer } from "../../ui/notifications";
|
||||
import { NotificationFilterLayer } from "../../ui/notification-filter";
|
||||
import { onAndroidNotificationPosted } from "../../native/notification-icons";
|
||||
import { onAndroidNotificationsChanged } from "../../native/notification-icons";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
@@ -18,7 +18,7 @@ export const NOTIFICATIONS_SURFACE_ID = "window:notifications";
|
||||
*/
|
||||
export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
// Newly posted notifications repaint the list while the window is open.
|
||||
let offNotificationPosted: (() => void) | null = null;
|
||||
let offNotificationsChanged: (() => void) | null = null;
|
||||
const created = createInProcessWindow({
|
||||
appId: "notifications",
|
||||
windowId: NOTIFICATIONS_WINDOW_ID,
|
||||
@@ -39,10 +39,10 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
offNotificationPosted?.();
|
||||
offNotificationsChanged?.();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
offNotificationPosted = onAndroidNotificationPosted(() => created.requestRender());
|
||||
offNotificationsChanged = onAndroidNotificationsChanged(() => created.requestRender());
|
||||
return created;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { dataCollectionSetting, dataCollectionSummary, type DataCollectionLevel } from "../../ui/dashboard-settings";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const CONTENT_GAP = 10;
|
||||
const BOTTOM_MARGIN = 8;
|
||||
const LABEL_GAP = 24;
|
||||
|
||||
/** None / Minimal / Full, each with a one-line summary; a tap saves the selected level. */
|
||||
export class DataCollectionPage implements OnboardingPage {
|
||||
readonly immersive = true;
|
||||
/** Width of the level-name column, measured each paint. */
|
||||
private labelColumnWidth = 0;
|
||||
private readonly menu = new Menu<DataCollectionLevel>({
|
||||
items: dataCollectionSetting.values,
|
||||
wrap: false,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(onboardingFont("body")),
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
|
||||
enter(): void {
|
||||
this.menu.select(dataCollectionSetting.values.indexOf(dataCollectionSetting.get()));
|
||||
}
|
||||
|
||||
paint(image: GrayImage, focused: boolean): void {
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Data Sharing",
|
||||
(global.isIOS ? "If using TestFlight, crash/usage reports cannot be disabled here. " : "") +
|
||||
"Choose how much data you want to share. Change later in Settings > Privacy.",
|
||||
3,
|
||||
) + CONTENT_GAP;
|
||||
const body = onboardingFont("body");
|
||||
this.labelColumnWidth =
|
||||
Math.max(...dataCollectionSetting.values.map((level) => body.measureText(dataCollectionSetting.displayValue(level)))) +
|
||||
LABEL_GAP;
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: PAGE_X - 10, y: top, width: image.width - 2 * (PAGE_X - 10), height: image.height - top - BOTTOM_MARGIN },
|
||||
focused,
|
||||
);
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down":
|
||||
void this.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const level = this.menu.selectedItem;
|
||||
if (level !== null) dataCollectionSetting.set(level);
|
||||
nav.next();
|
||||
return;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private drawRow({ image, item: level, x, y, width, height, selected }: MenuDrawArgs<DataCollectionLevel>): void {
|
||||
const body = onboardingFont("body");
|
||||
const detail = onboardingFont("detail");
|
||||
image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, dataCollectionSetting.displayValue(level), selected ? 255 : 200);
|
||||
const summaryX = x + 10 + this.labelColumnWidth;
|
||||
const summary = truncateText(detail, dataCollectionSummary(level), x + width - 10 - summaryX);
|
||||
image.drawText(detail, summaryX, y + Math.round((height - detail.lineHeight) / 2), summary, selected ? 210 : 150);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { appSwitcherPositionSetting, uiDepthSetting, type UiDepth } from "../../ui/dashboard-settings";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { layoutHasDepth } from "./layout-page";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const CONTENT_GAP = 10;
|
||||
const BOTTOM_MARGIN = 8;
|
||||
const COLUMN_WIDTH = 220;
|
||||
|
||||
/**
|
||||
* The whole display's stereo depth, after a layout that has one (Bottom or
|
||||
* Popup). Not immersive, so the page sits in the chosen layout, and the
|
||||
* depth is saved as the selection moves: the shell shifts everything with
|
||||
* it, which is the only way to see the difference.
|
||||
*/
|
||||
export class DepthPage implements OnboardingPage {
|
||||
private readonly menu = new Menu<UiDepth>({
|
||||
items: uiDepthSetting.values,
|
||||
wrap: false,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(onboardingFont("body")),
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
|
||||
applies(): boolean {
|
||||
return layoutHasDepth(appSwitcherPositionSetting.get());
|
||||
}
|
||||
|
||||
enter(): void {
|
||||
this.menu.select(uiDepthSetting.values.indexOf(uiDepthSetting.get()));
|
||||
}
|
||||
|
||||
paint(image: GrayImage, focused: boolean): void {
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Depth",
|
||||
"Swipe to move the display nearer or farther away, then tap to continue. Change later in Settings > Customization.",
|
||||
) + CONTENT_GAP;
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: PAGE_X - 10, y: top, width: COLUMN_WIDTH, height: image.height - top - BOTTOM_MARGIN },
|
||||
focused,
|
||||
);
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down": {
|
||||
void this.menu.handleInput(event);
|
||||
const depth = this.menu.selectedItem;
|
||||
if (depth !== null) uiDepthSetting.set(depth);
|
||||
return;
|
||||
}
|
||||
case "click":
|
||||
nav.next();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private drawRow({ image, item: depth, x, y, selected }: MenuDrawArgs<UiDepth>): void {
|
||||
image.drawText(onboardingFont("body"), x + 10, y + LIST_ROW_TEXT_INSET, uiDepthSetting.displayValue(depth), selected ? 255 : 190);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { displayLayoutSetting, type DisplayLayout } from "../../ui/dashboard-settings";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const CONTENT_GAP = 10;
|
||||
const BOTTOM_MARGIN = 8;
|
||||
const COLUMN_GAP = 16;
|
||||
|
||||
const DESCRIPTIONS = {
|
||||
band: "The stock 288px band, at this height in your view, leaving the rest of it clear.",
|
||||
tall: "Windows use the display's full height.",
|
||||
fullscreen: "Full height, and the display's whole width. Depth is turned off, since windows reach its edges.",
|
||||
};
|
||||
|
||||
/**
|
||||
* The display mode: how much of the display windows use (a band at one of
|
||||
* five heights, Tall, or Full Screen where the layout allows it), with a
|
||||
* line about the selected one. Not immersive, and the mode is saved as the
|
||||
* selection moves, so this window itself shows each mode as it really is
|
||||
* (the dashboard controller moves or resizes it, and every other window).
|
||||
*/
|
||||
export class DisplayModePage implements OnboardingPage {
|
||||
private readonly menu = new Menu<DisplayLayout>({
|
||||
wrap: false,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(onboardingFont("body")),
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
|
||||
enter(): void {
|
||||
// Full Screen comes and goes with the layout; where the layout doesn't
|
||||
// allow it, a saved Full Screen shows as Tall, as the shell does.
|
||||
const values = displayLayoutSetting.values.filter((value) => !displayLayoutSetting.isDisabled(value));
|
||||
const index = values.indexOf(displayLayoutSetting.get());
|
||||
this.menu.setItems(values, index >= 0 ? index : values.indexOf("tall"));
|
||||
}
|
||||
|
||||
paint(image: GrayImage, focused: boolean): void {
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Display mode",
|
||||
"Swipe to choose how much of the display windows use; it changes as you go. Tap to continue.",
|
||||
) + CONTENT_GAP;
|
||||
const body = onboardingFont("body");
|
||||
// TTF widths are fractional; the menu's row images need whole pixels.
|
||||
const columnWidth = Math.ceil(20 + Math.max(...this.menu.items.map((value) => body.measureText(displayLayoutSetting.displayValue(value)))));
|
||||
const menuBox = { x: PAGE_X - 10, y: top, width: columnWidth, height: image.height - top - BOTTOM_MARGIN };
|
||||
this.menu.paint(image, menuBox, focused);
|
||||
const layout = this.menu.selectedItem;
|
||||
if (layout === null) return;
|
||||
const detail = onboardingFont("detail");
|
||||
const left = menuBox.x + columnWidth + COLUMN_GAP;
|
||||
let y = top + LIST_ROW_TEXT_INSET;
|
||||
for (const line of wrapText(detail, description(layout), image.width - PAGE_X - left)) {
|
||||
if (y + detail.lineHeight > image.height - BOTTOM_MARGIN) break;
|
||||
image.drawText(detail, left, y, line, 170);
|
||||
y += detail.lineHeight;
|
||||
}
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down":
|
||||
void this.menu.handleInput(event);
|
||||
this.apply();
|
||||
return;
|
||||
case "click":
|
||||
this.apply();
|
||||
nav.next();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private apply(): void {
|
||||
const layout = this.menu.selectedItem;
|
||||
if (layout !== null && layout !== displayLayoutSetting.get()) displayLayoutSetting.set(layout);
|
||||
}
|
||||
|
||||
private drawRow({ image, item: layout, x, y, selected }: MenuDrawArgs<DisplayLayout>): void {
|
||||
image.drawText(onboardingFont("body"), x + 10, y + LIST_ROW_TEXT_INSET, displayLayoutSetting.displayValue(layout), selected ? 255 : 190);
|
||||
}
|
||||
}
|
||||
|
||||
function description(layout: DisplayLayout): string {
|
||||
return layout === "tall" || layout === "fullscreen" ? DESCRIPTIONS[layout] : DESCRIPTIONS.band;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createOnboardingAppWindow, ONBOARDING_SURFACE_ID, ONBOARDING_WINDOW_ID } from "./onboarding-app";
|
||||
import { ONBOARDING_APP_ID } from "./onboarding-progress";
|
||||
|
||||
/**
|
||||
* First-run setup on the glasses: welcome, text size, data-collection
|
||||
* preference, layout (app switcher position), depth for layouts that have
|
||||
* one, and display mode. The connection controllers open it on their own when it is
|
||||
* due (claimGlassesOnboardingAutoLaunch); the launcher entry reruns it.
|
||||
*/
|
||||
const onboardingApp: AppDefinition = {
|
||||
appId: ONBOARDING_APP_ID,
|
||||
title: "Onboarding",
|
||||
icon: "sparkles",
|
||||
launch: (ctx) => ctx.launchInProcessApp(ONBOARDING_WINDOW_ID, ONBOARDING_SURFACE_ID, createOnboardingAppWindow),
|
||||
};
|
||||
|
||||
export default onboardingApp;
|
||||
@@ -0,0 +1,43 @@
|
||||
import { IOS_PRIVACY_NOTICE } from "../../analytics/privacy-disclosure";
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
/** Paginate the notice so no disclosure is silently clipped on a short glasses viewport. */
|
||||
export class IosPrivacyPage implements OnboardingPage {
|
||||
readonly immersive = true;
|
||||
private page = 0;
|
||||
private pageCount = 1;
|
||||
|
||||
enter(): void { this.page = 0; }
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const top = drawPageHeading(image, "iOS / TestFlight privacy", "") + 8;
|
||||
const font = onboardingFont("detail");
|
||||
const lines = IOS_PRIVACY_NOTICE.split("\n\n").flatMap((paragraph, index) => [
|
||||
...(index ? [""] : []), ...wrapText(font, paragraph, image.width - 2 * PAGE_X),
|
||||
]);
|
||||
const perPage = Math.max(1, Math.floor((image.height - top - font.lineHeight - 20) / font.lineHeight));
|
||||
this.pageCount = Math.ceil(lines.length / perPage);
|
||||
this.page = Math.min(this.page, this.pageCount - 1);
|
||||
lines.slice(this.page * perPage, (this.page + 1) * perPage).forEach((line, index) => {
|
||||
image.drawText(font, PAGE_X, top + index * font.lineHeight, line, 200);
|
||||
});
|
||||
const action = this.page + 1 < this.pageCount ? "Tap for more" : "Tap to choose statistics";
|
||||
image.drawText(font, PAGE_X, image.height - font.lineHeight - 8,
|
||||
`${this.page + 1}/${this.pageCount} - ${action}`, 230);
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
if (event.type === "click") {
|
||||
if (this.page + 1 < this.pageCount) this.page++;
|
||||
else nav.next();
|
||||
} else if (event.type === "scroll-down" || event.type === "swipe-up") {
|
||||
this.page = Math.min(this.page + 1, this.pageCount - 1);
|
||||
} else if (event.type === "scroll-up" || event.type === "swipe-down") {
|
||||
this.page = Math.max(0, this.page - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* Schematic diagrams of the shell's layouts for the onboarding's Layout
|
||||
* page: to scale in real-display pixels, simplified, and drawn brighter
|
||||
* than the real chrome, which they shrink to a few pixels.
|
||||
*/
|
||||
import { G2_LENS_WIDTH, type GrayImage } from "../../graphics/image";
|
||||
import { statusBarPositionSetting, type AppSwitcherPosition } from "../../ui/dashboard-settings";
|
||||
import { SIDEBAR_WIDTH, SWITCHER_ROW_HEIGHT, MIN_WINDOW_HEIGHT, TOP_BAR_HEIGHT } from "../../ui/shell/geometry";
|
||||
|
||||
export const AREA_OUTLINE = 30;
|
||||
const CHROME_LINE = 100;
|
||||
const WINDOW_FRAME = 140;
|
||||
const ICON = 160;
|
||||
const SELECTED_ICON = 230;
|
||||
const STATUS = 140;
|
||||
const CONTENT = 110;
|
||||
const CONTENT_TITLE = 170;
|
||||
/** Window content under a popup switcher, which dims it. */
|
||||
const DIM = 0.5;
|
||||
|
||||
/** Draws in real-display pixels, scaled into an image at (x, y). */
|
||||
export class Diagram {
|
||||
constructor(
|
||||
private readonly image: GrayImage,
|
||||
private readonly x: number,
|
||||
private readonly y: number,
|
||||
private readonly scale: number,
|
||||
) {}
|
||||
|
||||
private box(x: number, y: number, width: number, height: number) {
|
||||
const left = this.x + Math.round(x * this.scale), top = this.y + Math.round(y * this.scale);
|
||||
return {
|
||||
x: left,
|
||||
y: top,
|
||||
width: Math.max(1, this.x + Math.round((x + width) * this.scale) - left),
|
||||
height: Math.max(1, this.y + Math.round((y + height) * this.scale) - top),
|
||||
};
|
||||
}
|
||||
|
||||
fill(x: number, y: number, width: number, height: number, value: number, radius = 0): void {
|
||||
const box = this.box(x, y, width, height);
|
||||
if (radius) this.image.fillRoundedRect(box.x, box.y, box.width, box.height, value, Math.max(1, Math.round(radius * this.scale)));
|
||||
else this.image.fillRect(box.x, box.y, box.width, box.height, value);
|
||||
}
|
||||
|
||||
outline(x: number, y: number, width: number, height: number, value: number, radius = 0): void {
|
||||
const box = this.box(x, y, width, height);
|
||||
this.image.drawRoundedRect(box.x, box.y, box.width, box.height, value, Math.max(0, Math.round(radius * this.scale)));
|
||||
}
|
||||
|
||||
/** A one-pixel line along a real-pixel row (horizontal) or column. */
|
||||
hline(x: number, y: number, width: number, value: number): void {
|
||||
const box = this.box(x, y, width, 1);
|
||||
this.image.fillRect(box.x, box.y, box.width, 1, value);
|
||||
}
|
||||
|
||||
vline(x: number, y: number, height: number, value: number): void {
|
||||
const box = this.box(x, y, 1, height);
|
||||
this.image.fillRect(box.x, box.y, 1, box.height, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The foreground window's band, in real-display rows: its top and height
|
||||
* (header and content; a bottom switcher row hangs under it), and whether
|
||||
* it spans the panel's whole width (Full Screen; never beside a side strip).
|
||||
*/
|
||||
export type DiagramBand = { top: number; height: number; fullWidth: boolean };
|
||||
|
||||
/**
|
||||
* A layout as the real chrome lays it out (geometry.ts, chrome-layer.ts):
|
||||
* the window band with its status bar and frame, and the switcher with a
|
||||
* few icons, the first selected. A popup switcher shows only with
|
||||
* `popupOpen`, dimming the window as it does. Status bar placement follows
|
||||
* the current setting.
|
||||
*/
|
||||
export function drawLayout(d: Diagram, position: AppSwitcherPosition, band: DiagramBand, popupOpen: boolean): void {
|
||||
const statusAtBottom = statusBarPositionSetting.get() === "bottom";
|
||||
const { top, height } = band;
|
||||
switch (position) {
|
||||
case "left":
|
||||
case "right": {
|
||||
const stripLeft = position === "left" ? 0 : G2_LENS_WIDTH - SIDEBAR_WIDTH;
|
||||
const contentLeft = position === "left" ? SIDEBAR_WIDTH : 0;
|
||||
const windowWidth = G2_LENS_WIDTH - SIDEBAR_WIDTH;
|
||||
drawStatusBar(d, contentLeft, top, windowWidth, TOP_BAR_HEIGHT - 1);
|
||||
drawContent(d, contentLeft, top + TOP_BAR_HEIGHT, windowWidth, height - TOP_BAR_HEIGHT, 1);
|
||||
// The strip keeps to the 288px band's height, even beside taller windows.
|
||||
const separator = position === "left" ? SIDEBAR_WIDTH - 1 : stripLeft;
|
||||
d.vline(separator, top + TOP_BAR_HEIGHT, MIN_WINDOW_HEIGHT - TOP_BAR_HEIGHT, CHROME_LINE);
|
||||
// One column of icons against the separator.
|
||||
const column = position === "left" ? SIDEBAR_WIDTH - 36 : stripLeft;
|
||||
for (let slot = 0; slot < 4; slot++) drawIcon(d, column + 2, top + TOP_BAR_HEIGHT + 10 + slot * 40, slot === 0);
|
||||
return;
|
||||
}
|
||||
case "bottom": {
|
||||
const { left, width, framed } = windowSpan(band);
|
||||
// With the status bar in the row, the window's header is just the frame's top.
|
||||
const header = !statusAtBottom ? TOP_BAR_HEIGHT : framed ? 1 : 0;
|
||||
if (!statusAtBottom) drawStatusBar(d, left, top, width, framed ? null : TOP_BAR_HEIGHT - 1);
|
||||
drawContent(d, left, top + header, width, height - header, 1);
|
||||
if (framed) d.outline(left - 1, top + header - 1, width + 2, height - header + 2, WINDOW_FRAME, 8);
|
||||
else d.hline(left, top + height, width, CHROME_LINE);
|
||||
// The first cell starts at the frame's side, or inset like a scrolled row's (chrome-layer switcherLayout).
|
||||
const cellsLeft = framed ? left - 1 : left + 8;
|
||||
for (let slot = 0; slot < 4; slot++) drawIcon(d, cellsLeft + 2 + slot * 40, top + height + 2, slot === 0);
|
||||
if (statusAtBottom) drawStatusItems(d, left + width, top + height + 4);
|
||||
return;
|
||||
}
|
||||
case "popup": {
|
||||
const { left, width, framed } = windowSpan(band);
|
||||
const edge = framed ? 1 : 0;
|
||||
const statusTop = statusAtBottom ? top + height - TOP_BAR_HEIGHT : top;
|
||||
const contentTop = statusAtBottom ? top + edge : top + TOP_BAR_HEIGHT;
|
||||
const contentHeight = height - TOP_BAR_HEIGHT - edge;
|
||||
drawStatusBar(d, left, statusTop, width, framed ? null : statusAtBottom ? 0 : TOP_BAR_HEIGHT - 1);
|
||||
drawContent(d, left, contentTop, width, contentHeight, popupOpen ? DIM : 1);
|
||||
if (framed) d.outline(left - 1, contentTop - 1, width + 2, contentHeight + 2, WINDOW_FRAME, 8);
|
||||
if (!popupOpen) return;
|
||||
// The popup: a row of icons over the selected window's title, centred on the content.
|
||||
const box = { width: 272, height: 98 };
|
||||
const boxLeft = left + (width - box.width) / 2;
|
||||
const boxTop = contentTop + (contentHeight - box.height) / 2;
|
||||
d.fill(boxLeft, boxTop, box.width, box.height, 0);
|
||||
d.outline(boxLeft, boxTop, box.width, box.height, WINDOW_FRAME);
|
||||
const iconsLeft = boxLeft + (box.width - (3 * 48 - 4)) / 2;
|
||||
for (let slot = 0; slot < 3; slot++) drawIcon(d, iconsLeft + slot * 48 + 6, boxTop + 18, slot === 0);
|
||||
d.fill(boxLeft + box.width / 2 - 50, boxTop + 70, 100, 12, STATUS, 6);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A bottom row or popup layout's window: centred at 576 wide and framed, or the panel's width without a frame. */
|
||||
function windowSpan(band: DiagramBand): { left: number; width: number; framed: boolean } {
|
||||
return band.fullWidth
|
||||
? { left: 0, width: G2_LENS_WIDTH, framed: false }
|
||||
: { left: SIDEBAR_WIDTH / 2, width: G2_LENS_WIDTH - SIDEBAR_WIDTH, framed: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* A status bar's 28 rows from `y`; `divider` is the row (within them) of its
|
||||
* line toward the window, if it draws one rather than meeting a frame.
|
||||
*/
|
||||
function drawStatusBar(d: Diagram, x: number, y: number, width: number, divider: number | null): void {
|
||||
if (divider !== null) d.hline(x, y + divider, width, CHROME_LINE);
|
||||
drawStatusItems(d, x + width, y);
|
||||
}
|
||||
|
||||
/** Notification icons, a battery and the clock, ending at `right`, in a status bar's 28 rows from `top`. */
|
||||
function drawStatusItems(d: Diagram, right: number, top: number): void {
|
||||
d.fill(right - 64, top + 7, 56, 14, STATUS, 4);
|
||||
d.outline(right - 108, top + 8, 32, 12, STATUS, 2);
|
||||
d.outline(right - 148, top + 4, 20, 20, CHROME_LINE, 4);
|
||||
d.outline(right - 176, top + 4, 20, 20, CHROME_LINE, 4);
|
||||
}
|
||||
|
||||
/** A switcher icon (32px); the selected one filled, as the switcher's selection tab is. */
|
||||
function drawIcon(d: Diagram, x: number, y: number, selected: boolean): void {
|
||||
if (selected) d.fill(x, y, 32, 32, SELECTED_ICON, 6);
|
||||
else d.outline(x, y, 32, 32, ICON, 6);
|
||||
}
|
||||
|
||||
/** Stand-in window content: a title and as many list rows as fit, the first selected. */
|
||||
function drawContent(d: Diagram, x: number, y: number, width: number, height: number, brightness: number): void {
|
||||
const rowWidths = [width - 60, 300, 360, 240];
|
||||
d.fill(x + 18, y + 14, 150, 18, Math.round(CONTENT_TITLE * brightness), 6);
|
||||
d.outline(x + 8, y + 44, width - 16, 34, Math.round(CONTENT * brightness), 8);
|
||||
for (let row = 0; ; row++) {
|
||||
const rowTop = y + 52 + row * 44;
|
||||
if (rowTop + 18 > y + height - 8) break;
|
||||
d.fill(x + 18, rowTop, Math.min(rowWidths[row % rowWidths.length]!, width - 36), 18, Math.round(CONTENT * brightness), 6);
|
||||
}
|
||||
}
|
||||
|
||||
/** Real-display rows the Layout page's diagrams cover: a 288px band and a bottom switcher row under it. */
|
||||
export const LAYOUT_AREA_HEIGHT = MIN_WINDOW_HEIGHT + SWITCHER_ROW_HEIGHT;
|
||||
@@ -0,0 +1,114 @@
|
||||
import { G2_LENS_WIDTH, type GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { appSwitcherPositionSetting, type AppSwitcherPosition } from "../../ui/dashboard-settings";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "../../ui/menu-core";
|
||||
import { MIN_WINDOW_HEIGHT } from "../../ui/shell/geometry";
|
||||
import { AREA_OUTLINE, Diagram, drawLayout, LAYOUT_AREA_HEIGHT } from "./layout-diagram";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const CONTENT_GAP = 10;
|
||||
const BOTTOM_MARGIN = 8;
|
||||
/** Diagram width as a fraction of the display's: the most a diagram is drawn at. */
|
||||
const DIAGRAM_SCALE = 0.4;
|
||||
/** Padding inside a cell's selection box, round the diagram and its caption. */
|
||||
const CELL_PADDING = 8;
|
||||
const CAPTION_GAP = 4;
|
||||
const COLUMN_GAP = 16;
|
||||
const ROW_GAP = 6;
|
||||
const DEPTH_SUBTITLE = "Customizable Depth";
|
||||
/** Grid order, in rows of two: the layouts with depth on top. */
|
||||
const LAYOUT_ORDER: readonly AppSwitcherPosition[] = ["bottom", "popup", "left", "right"];
|
||||
/** Selected on the first visit, and marked. */
|
||||
const RECOMMENDED_LAYOUT: AppSwitcherPosition = "bottom";
|
||||
|
||||
/** Whether a layout leaves the side margins the display's stereo depth shifts into (geometry.ts uiDepth). */
|
||||
export function layoutHasDepth(position: AppSwitcherPosition): boolean {
|
||||
return position === "bottom" || position === "popup";
|
||||
}
|
||||
|
||||
/**
|
||||
* The app switcher position, as a 2×2 grid of captioned diagrams of each
|
||||
* layout, Bottom (recommended, and selected at first) and Popup on top;
|
||||
* swipes move through them in reading order, a tap saves the selected one.
|
||||
* The page is immersive, so the diagrams get 40% of the display's width
|
||||
* each. Choosing a layout reflows open windows (the dashboard controller
|
||||
* watches the setting).
|
||||
*/
|
||||
export class LayoutPage implements OnboardingPage {
|
||||
readonly immersive = true;
|
||||
/** Kept across visits, so going back returns to the last choice. */
|
||||
private selected = LAYOUT_ORDER.indexOf(RECOMMENDED_LAYOUT);
|
||||
|
||||
paint(image: GrayImage, focused: boolean): void {
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Layout",
|
||||
"Swipe to choose where the app switcher goes, then tap to continue.",
|
||||
) + CONTENT_GAP;
|
||||
const title = onboardingFont("body");
|
||||
const subtitle = onboardingFont("detail");
|
||||
const captionHeight = CAPTION_GAP + title.lineHeight + subtitle.lineHeight;
|
||||
const available = image.height - top - BOTTOM_MARGIN;
|
||||
// Short of DIAGRAM_SCALE only where two rows of cells wouldn't fit.
|
||||
const scale = Math.min(
|
||||
DIAGRAM_SCALE,
|
||||
((available - ROW_GAP) / 2 - 2 * CELL_PADDING - captionHeight) / LAYOUT_AREA_HEIGHT,
|
||||
((image.width - COLUMN_GAP) / 2 - 2 * CELL_PADDING) / G2_LENS_WIDTH,
|
||||
);
|
||||
const diagramWidth = Math.round(G2_LENS_WIDTH * scale);
|
||||
const diagramHeight = Math.round(LAYOUT_AREA_HEIGHT * scale);
|
||||
const cellWidth = diagramWidth + 2 * CELL_PADDING;
|
||||
const cellHeight = diagramHeight + 2 * CELL_PADDING + captionHeight;
|
||||
const left = Math.round((image.width - 2 * cellWidth - COLUMN_GAP) / 2);
|
||||
const gridTop = top + Math.max(0, Math.round((available - 2 * cellHeight - ROW_GAP) / 2));
|
||||
LAYOUT_ORDER.forEach((position, index) => {
|
||||
const x = left + (index % 2) * (cellWidth + COLUMN_GAP);
|
||||
const y = gridTop + Math.floor(index / 2) * (cellHeight + ROW_GAP);
|
||||
const selected = index === this.selected;
|
||||
if (selected) {
|
||||
if (focused) image.fillRoundedRect(x, y, cellWidth, cellHeight, MENU_HIGHLIGHT_FILL, 8);
|
||||
image.drawRoundedRect(x, y, cellWidth, cellHeight, MENU_HIGHLIGHT_STROKE, 8);
|
||||
}
|
||||
drawLayoutDiagram(new Diagram(image, x + CELL_PADDING, y + CELL_PADDING, scale), position);
|
||||
const centre = (text: string, font: UiFont, textY: number, value: number) =>
|
||||
image.drawText(font, x + Math.round((cellWidth - font.measureText(text)) / 2), textY, text, value);
|
||||
const captionTop = y + CELL_PADDING + diagramHeight + CAPTION_GAP;
|
||||
const name = appSwitcherPositionSetting.displayValue(position);
|
||||
const caption = position === RECOMMENDED_LAYOUT ? `${name} (Recommended)` : name;
|
||||
centre(caption, title, captionTop, selected ? 255 : 190);
|
||||
if (layoutHasDepth(position)) {
|
||||
centre(DEPTH_SUBTITLE, subtitle, captionTop + title.lineHeight, selected ? 210 : 150);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
const last = LAYOUT_ORDER.length - 1;
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selected = Math.max(0, this.selected - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selected = Math.min(last, this.selected + 1);
|
||||
return;
|
||||
case "click":
|
||||
appSwitcherPositionSetting.set(LAYOUT_ORDER[this.selected]!);
|
||||
nav.next();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A layout's diagram: the 288px band, with the popup switcher open. Only a
|
||||
* bottom switcher row takes rows under the band; without one, the band sits
|
||||
* centred in their place, as the band and row share the screen's slack.
|
||||
*/
|
||||
function drawLayoutDiagram(d: Diagram, position: AppSwitcherPosition): void {
|
||||
d.outline(0, 0, G2_LENS_WIDTH, LAYOUT_AREA_HEIGHT, AREA_OUTLINE, 12);
|
||||
const top = position === "bottom" ? 0 : (LAYOUT_AREA_HEIGHT - MIN_WINDOW_HEIGHT) / 2;
|
||||
drawLayout(d, position, { top, height: MIN_WINDOW_HEIGHT, fullWidth: false }, true);
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import type { Layer, LayerActions, LayerContext } from "../../ui/layers";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { findSoundEffect, playSoundEffect } from "../../ui/sound-effects";
|
||||
import { DataCollectionPage } from "./data-collection-page";
|
||||
import { DepthPage } from "./depth-page";
|
||||
import { DisplayModePage } from "./display-mode-page";
|
||||
import { IosPrivacyPage } from "./ios-privacy-page";
|
||||
import { LayoutPage } from "./layout-page";
|
||||
import { markGlassesOnboardingFinished, ONBOARDING_APP_ID } from "./onboarding-progress";
|
||||
import type { OnboardingPage } from "./onboarding-page";
|
||||
import { TextSizePage } from "./text-size-page";
|
||||
import { WelcomePage } from "./welcome-page";
|
||||
|
||||
export const ONBOARDING_WINDOW_ID = "onboarding";
|
||||
export const ONBOARDING_SURFACE_ID = "window:onboarding";
|
||||
|
||||
/**
|
||||
* The on-glasses onboarding: a fixed sequence of pages, opened automatically
|
||||
* on the first connection after phone-side setup and after each upgrade
|
||||
* (see onboarding-progress.ts), and from the launcher any time. Finishing
|
||||
* the last page records this version as onboarded and closes the window;
|
||||
* closing it early leaves onboarding due.
|
||||
*/
|
||||
export function createOnboardingAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const pages: OnboardingPage[] = [
|
||||
new WelcomePage(), new TextSizePage(),
|
||||
...(global.isIOS ? [new IosPrivacyPage()] : []),
|
||||
new DataCollectionPage(), new LayoutPage(), new DepthPage(), new DisplayModePage(),
|
||||
];
|
||||
const app = createInProcessWindow({
|
||||
appId: ONBOARDING_APP_ID,
|
||||
windowId: ONBOARDING_WINDOW_ID,
|
||||
title: "Onboarding",
|
||||
iconLetter: "On",
|
||||
icon: "sparkles",
|
||||
closeable: true,
|
||||
immersive: pages[0]!.immersive,
|
||||
actions: options.actions,
|
||||
baseLayer: new OnboardingLayer(
|
||||
pages,
|
||||
// Only called on page changes, by when `app` is set.
|
||||
(immersive) => app.setImmersive(immersive),
|
||||
() => {
|
||||
markGlassesOnboardingFinished();
|
||||
shell.closeWindow(ONBOARDING_WINDOW_ID);
|
||||
},
|
||||
),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
reconfigureSurface: options.reconfigureSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
playWelcomeSound(options.actions);
|
||||
return app;
|
||||
}
|
||||
|
||||
/** The celebratory jingle that greets the welcome page (formerly played on the first connection). */
|
||||
function playWelcomeSound(actions: LayerActions): void {
|
||||
const effect = findSoundEffect("questcomplete");
|
||||
if (!effect) return;
|
||||
playSoundEffect(
|
||||
effect,
|
||||
(payload) => actions.playBuzzerSequence(payload),
|
||||
(ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
||||
).catch((error) => console.warn(`onboarding welcome sound failed: ${error}`));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hosts the current page, immersive or not as it asks. Double-click steps
|
||||
* back a page, or leaves the app from the first one (the standard yield to
|
||||
* the app switcher); every other gesture goes to the page. Pages that don't
|
||||
* apply (Depth beside a side-strip app switcher) are skipped either way.
|
||||
*/
|
||||
export class OnboardingLayer implements Layer {
|
||||
private index = 0;
|
||||
|
||||
constructor(
|
||||
private readonly pages: readonly OnboardingPage[],
|
||||
private readonly setImmersive: (immersive: boolean) => void,
|
||||
private readonly finish: () => void,
|
||||
) {
|
||||
pages[0]!.enter?.();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
this.pages[this.index]!.paint(image, ctx.stack.isFocused());
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
const previous = this.applicableIndex(this.index - 1, -1);
|
||||
if (previous === null) shell.yieldFocusToSidebar();
|
||||
else this.show(previous);
|
||||
return;
|
||||
}
|
||||
await this.pages[this.index]!.handleInput(event, { next: () => this.next() });
|
||||
}
|
||||
|
||||
private next(): void {
|
||||
const next = this.applicableIndex(this.index + 1, 1);
|
||||
if (next === null) this.finish();
|
||||
else this.show(next);
|
||||
}
|
||||
|
||||
/** The first page from `index` on in direction `step` that applies, if any. */
|
||||
private applicableIndex(index: number, step: 1 | -1): number | null {
|
||||
for (; index >= 0 && index < this.pages.length; index += step) {
|
||||
if (this.pages[index]!.applies?.() ?? true) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private show(index: number): void {
|
||||
this.index = index;
|
||||
const page = this.pages[index]!;
|
||||
page.enter?.();
|
||||
this.setImmersive(page.immersive ?? false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* The onboarding's own fonts: bundled Roboto at fixed sizes, deliberately
|
||||
* not the user's UI font, so the pages are readable before (and while) the
|
||||
* text size is being chosen. Bitmap Terminus stands in where TTF rendering
|
||||
* is unavailable.
|
||||
*/
|
||||
import { getFont, getTerminusFont } from "../../graphics/bdffont";
|
||||
import type { UiFont } from "../../graphics/image";
|
||||
import { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts";
|
||||
import { TtfFont } from "../../graphics/ttf-font";
|
||||
|
||||
export type OnboardingFontRole = "hero" | "title" | "body" | "detail";
|
||||
|
||||
const ROLE_FONTS: Record<OnboardingFontRole, { file: string; size: number; fallback: () => UiFont }> = {
|
||||
/** The welcome page's "Faceclaw". */
|
||||
hero: { file: "Roboto-Regular.ttf", size: 44, fallback: () => getFont("terminus32") },
|
||||
/** Page titles. */
|
||||
title: { file: "Roboto-Regular.ttf", size: 26, fallback: () => getFont("terminus24") },
|
||||
/** Prompts and choices. */
|
||||
body: { file: "Roboto-Regular.ttf", size: 20, fallback: () => getTerminusFont(20, false) ?? getFont("terminus16") },
|
||||
/** Explanations under titles and beside choices. */
|
||||
detail: { file: "Roboto-Regular.ttf", size: 16, fallback: () => getFont("terminus16") },
|
||||
};
|
||||
|
||||
let fontsEnsured = false;
|
||||
|
||||
export function onboardingFont(role: OnboardingFontRole): UiFont {
|
||||
if (!fontsEnsured) {
|
||||
fontsEnsured = true;
|
||||
try {
|
||||
ensurePreinstalledFonts(); // onboarding may be the first thing to draw TTF text
|
||||
} catch (error) {
|
||||
console.warn(`onboarding font preinstall failed: ${error}`);
|
||||
}
|
||||
}
|
||||
const spec = ROLE_FONTS[role];
|
||||
return TtfFont.load(installedFontPath(spec.file), spec.size) ?? spec.fallback();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
|
||||
/** Page-to-flow navigation, handed to a page's input handler. */
|
||||
export type OnboardingNav = {
|
||||
/** Go to the next page, or finish onboarding from the last one. */
|
||||
next: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* One onboarding step. The flow owns the page list, back navigation
|
||||
* (double-click) and finishing; a page paints itself into the window's
|
||||
* viewport and handles every other gesture.
|
||||
*/
|
||||
export interface OnboardingPage {
|
||||
/**
|
||||
* Show the page immersive (full-screen): the whole 640×480 panel, with
|
||||
* the app switcher and status bar hidden. Otherwise it is an ordinary
|
||||
* window in the chosen layout.
|
||||
*/
|
||||
readonly immersive?: boolean;
|
||||
/** Whether the page belongs in the flow right now; skipped both ways when false. */
|
||||
applies?(): boolean;
|
||||
/** The page became current, going forward or back. */
|
||||
enter?(): void;
|
||||
paint(image: GrayImage, focused: boolean): void;
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void | Promise<void>;
|
||||
}
|
||||
|
||||
/** Left margin and title position shared by the pages (the UI-wide (18, 10) title spot). */
|
||||
export const PAGE_X = 18;
|
||||
export const TITLE_Y = 10;
|
||||
|
||||
/**
|
||||
* Draw a page's title and its wrapped explanation (at most `maxLines`
|
||||
* lines) in the onboarding fonts. Returns the y just below the explanation.
|
||||
*/
|
||||
export function drawPageHeading(
|
||||
image: GrayImage,
|
||||
title: string,
|
||||
explanation: string,
|
||||
maxLines = 2,
|
||||
): number {
|
||||
const titleFont = onboardingFont("title");
|
||||
const detailFont = onboardingFont("detail");
|
||||
image.drawText(titleFont, PAGE_X, TITLE_Y, title, 230);
|
||||
let y = TITLE_Y + titleFont.lineHeight + 2;
|
||||
const lines = wrapText(detailFont, explanation, image.width - 2 * PAGE_X).slice(0, maxLines);
|
||||
for (const line of lines) {
|
||||
image.drawText(detailFont, PAGE_X, y, line, 160);
|
||||
y += detailFont.lineHeight;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Whether the on-glasses onboarding is due: it is recorded as finished per
|
||||
* Faceclaw version, so it runs on the first connection after phone-side
|
||||
* setup and again after any upgrade. The key shares the phone onboarding's
|
||||
* "onboarding." prefix so scripts/resetOnboarding.sh clears it too.
|
||||
*/
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { FACECLAW_VERSION } from "../../version";
|
||||
|
||||
export const ONBOARDING_APP_ID = "onboarding";
|
||||
|
||||
const FINISHED_VERSION_KEY = "onboarding.glassesFinishedVersion";
|
||||
|
||||
export function isGlassesOnboardingDue(): boolean {
|
||||
return getStringSetting(FINISHED_VERSION_KEY, "") !== FACECLAW_VERSION;
|
||||
}
|
||||
|
||||
export function markGlassesOnboardingFinished(): void {
|
||||
setStringSetting(FINISHED_VERSION_KEY, FACECLAW_VERSION);
|
||||
}
|
||||
|
||||
let autoLaunchChecked = false;
|
||||
|
||||
/**
|
||||
* Asked by the connection controllers on each rendered frame (the session
|
||||
* is warm by then, so the onboarding's welcome jingle won't be dropped).
|
||||
* True at most once per run, and only when onboarding is due: closing it
|
||||
* unfinished brings it back on the next app start, not on every reconnect.
|
||||
*/
|
||||
export function claimGlassesOnboardingAutoLaunch(): boolean {
|
||||
if (autoLaunchChecked) return false;
|
||||
autoLaunchChecked = true;
|
||||
return isGlassesOnboardingDue();
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
import type { GrayImage, UiFont } from "../../graphics/image";
|
||||
import { installedFontPath } from "../../graphics/installed-fonts";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { TtfFont } from "../../graphics/ttf-font";
|
||||
import {
|
||||
fontSelectionLabel,
|
||||
getDefaultSmallFont,
|
||||
getUiFontSelection,
|
||||
setUiFontSelection,
|
||||
uiFontSizeAllowed,
|
||||
} from "../../graphics/ui-fonts";
|
||||
import { FONT_SIZE_CHOICES } from "../../ui/font-picker";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { Menu, type MenuBox, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const CONTENT_GAP = 10;
|
||||
const BOTTOM_MARGIN = 8;
|
||||
const COLUMN_GAP = 12;
|
||||
const PREVIEW_PADDING = 10;
|
||||
const ROW_GAP = 2;
|
||||
/** Between a size and its "Recommended" note. */
|
||||
const NOTE_GAP = 8;
|
||||
/** The sizes offered here; Settings has the full range. */
|
||||
const SIZE_CHOICES = FONT_SIZE_CHOICES.filter((size) => size >= 12 && size <= 18);
|
||||
/** Selected on the first visit, and marked. */
|
||||
const RECOMMENDED_SIZE = 16;
|
||||
const RECOMMENDED_NOTE = "Recommended";
|
||||
/** Drawn while they fit, so smaller sizes visibly show more words. */
|
||||
const PREVIEW_PARAGRAPHS = [
|
||||
"This is how text will look on your glasses.",
|
||||
"Smaller sizes fit more words on the display at once, while larger sizes are easier to read at a " +
|
||||
"glance or on the move. Most people find the recommended size a good balance between the two.",
|
||||
];
|
||||
|
||||
/** The UI font's face, as far as this page is concerned: resizable (TTF) or not (bitmap). */
|
||||
type Face =
|
||||
| { kind: "sized"; file: string; path: string; sizes: number[] }
|
||||
| { kind: "fixed"; label: string };
|
||||
|
||||
/**
|
||||
* The UI font's size, 12 to 18px (16 recommended, and selected at first),
|
||||
* beside a preview of text in the selected size. The face and weight stay
|
||||
* as they are (they live in the Settings font picker); a bitmap face has
|
||||
* only one size, so the page just says so. The size is saved on tap, not
|
||||
* while scrolling.
|
||||
*/
|
||||
export class TextSizePage implements OnboardingPage {
|
||||
readonly immersive = true;
|
||||
private face: Face = { kind: "fixed", label: "Terminus" };
|
||||
private readonly sizeMenu = new Menu<number>({
|
||||
wrap: false,
|
||||
rowGap: ROW_GAP,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(onboardingFont("body")),
|
||||
draw: (args) => this.drawSizeRow(args),
|
||||
});
|
||||
|
||||
enter(): void {
|
||||
// Re-read on every visit: the face may have changed in Settings meanwhile.
|
||||
const selection = getUiFontSelection();
|
||||
if (selection.kind === "ttf") {
|
||||
const path = installedFontPath(selection.file);
|
||||
const sizes = SIZE_CHOICES.filter((size) => uiFontSizeAllowed(path, size));
|
||||
if (sizes.length) {
|
||||
this.face = { kind: "sized", file: selection.file, path, sizes };
|
||||
// The recommended size the first time; on a return, what was left selected.
|
||||
this.sizeMenu.setItems(sizes, nearestIndex(sizes, this.sizeMenu.selectedItem ?? RECOMMENDED_SIZE));
|
||||
return;
|
||||
}
|
||||
}
|
||||
// An unloadable TTF selection renders as Terminus, so call it that.
|
||||
this.face = { kind: "fixed", label: selection.kind === "bitmap" ? fontSelectionLabel(selection) : "Terminus" };
|
||||
}
|
||||
|
||||
paint(image: GrayImage, focused: boolean): void {
|
||||
if (this.face.kind === "fixed") {
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Text size",
|
||||
`Your UI font, ${this.face.label}, comes in one size. To choose a size, pick a different font in ` +
|
||||
"Settings > Customization. Tap to continue.",
|
||||
3,
|
||||
) + CONTENT_GAP;
|
||||
drawPreview(image, getDefaultSmallFont(), {
|
||||
x: PAGE_X - 10,
|
||||
y: top,
|
||||
width: image.width - 2 * (PAGE_X - 10),
|
||||
height: image.height - top - BOTTOM_MARGIN,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const top = drawPageHeading(
|
||||
image,
|
||||
"Text size",
|
||||
"Swipe to choose a size, then tap to continue. The typeface and weight can be changed in Settings.",
|
||||
) + CONTENT_GAP;
|
||||
const height = image.height - top - BOTTOM_MARGIN;
|
||||
const body = onboardingFont("body");
|
||||
// TTF widths are fractional; the menu's row images need whole pixels.
|
||||
const columnWidth = Math.ceil(20 + Math.max(...this.face.sizes.map((size) => body.measureText(sizeLabel(size)))) +
|
||||
NOTE_GAP + onboardingFont("detail").measureText(RECOMMENDED_NOTE));
|
||||
const menuBox = { x: PAGE_X - 10, y: top, width: columnWidth, height };
|
||||
this.sizeMenu.paint(image, menuBox, focused);
|
||||
const previewX = menuBox.x + menuBox.width + COLUMN_GAP;
|
||||
const size = this.sizeMenu.selectedItem;
|
||||
const font = (size !== null ? TtfFont.load(this.face.path, size) : null) ?? getDefaultSmallFont();
|
||||
drawPreview(image, font, { x: previewX, y: top, width: image.width - (PAGE_X - 10) - previewX, height });
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down":
|
||||
if (this.face.kind === "sized") void this.sizeMenu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
this.save();
|
||||
nav.next();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private save(): void {
|
||||
const size = this.sizeMenu.selectedItem;
|
||||
if (this.face.kind !== "sized" || size === null) return;
|
||||
const current = getUiFontSelection();
|
||||
if (current.kind === "ttf" && current.file === this.face.file && current.size === size) return;
|
||||
setUiFontSelection({ kind: "ttf", file: this.face.file, size });
|
||||
}
|
||||
|
||||
private drawSizeRow({ image, item: size, x, y, height, selected }: MenuDrawArgs<number>): void {
|
||||
const body = onboardingFont("body");
|
||||
const label = sizeLabel(size);
|
||||
image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 190);
|
||||
if (size !== RECOMMENDED_SIZE) return;
|
||||
const detail = onboardingFont("detail");
|
||||
const noteY = y + Math.round((height - detail.lineHeight) / 2);
|
||||
image.drawText(detail, x + 10 + body.measureText(label) + NOTE_GAP, noteY, RECOMMENDED_NOTE, selected ? 210 : 150);
|
||||
}
|
||||
}
|
||||
|
||||
function sizeLabel(size: number): string {
|
||||
return `${size} px`;
|
||||
}
|
||||
|
||||
/** A framed sample in `font`: paragraphs of text, as many whole lines as fit. */
|
||||
function drawPreview(image: GrayImage, font: UiFont, box: MenuBox): void {
|
||||
image.drawRoundedRect(box.x, box.y, box.width, box.height, 60, 8);
|
||||
const left = box.x + PREVIEW_PADDING;
|
||||
const width = box.width - 2 * PREVIEW_PADDING;
|
||||
const bottom = box.y + box.height - PREVIEW_PADDING;
|
||||
let y = box.y + PREVIEW_PADDING;
|
||||
for (const [index, paragraph] of PREVIEW_PARAGRAPHS.entries()) {
|
||||
if (index > 0) y += font.lineHeight >> 1;
|
||||
for (const line of wrapText(font, paragraph, width)) {
|
||||
if (y + font.lineHeight > bottom) return;
|
||||
image.drawText(font, left, y, line, 220);
|
||||
y += font.lineHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function nearestIndex(values: readonly number[], target: number): number {
|
||||
let best = 0;
|
||||
for (let index = 1; index < values.length; index++) {
|
||||
if (Math.abs(values[index]! - target) < Math.abs(values[best]! - target)) best = index;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { GrayImage, UiFont } from "../../graphics/image";
|
||||
import { getFullLogo } from "../../graphics/logo";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import type { InputEvent } from "../../ui/gestures";
|
||||
import { FACECLAW_VERSION } from "../../version";
|
||||
import { onboardingFont } from "./onboarding-fonts";
|
||||
import { PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
||||
|
||||
const LOGO_MAX_HEIGHT = 180;
|
||||
const LOGO_MARGIN = 12;
|
||||
const LOGO_TEXT_GAP = 28;
|
||||
const PROMPT_GAP = 22;
|
||||
const PROMPT = "Tap the ring or touchpad to continue";
|
||||
|
||||
type TextLine = { font: UiFont; text: string; value: number; spaceBefore: number };
|
||||
|
||||
/** The splash: logo beside the name, version and how to go on, centred as one group. */
|
||||
export class WelcomePage implements OnboardingPage {
|
||||
readonly immersive = true;
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const { width, height } = image;
|
||||
const logo = getFullLogo(Math.min(LOGO_MAX_HEIGHT, height - 2 * LOGO_MARGIN));
|
||||
const logoWidth = logo ? logo.width + LOGO_TEXT_GAP : 0;
|
||||
const body = onboardingFont("body");
|
||||
const lines: TextLine[] = [
|
||||
{ font: onboardingFont("hero"), text: "Faceclaw", value: 240, spaceBefore: 0 },
|
||||
{ font: body, text: `Version ${FACECLAW_VERSION}`, value: 150, spaceBefore: 0 },
|
||||
...wrapText(body, PROMPT, width - 2 * PAGE_X - logoWidth).map((text, index) => ({
|
||||
font: body,
|
||||
text,
|
||||
value: 215,
|
||||
spaceBefore: index === 0 ? PROMPT_GAP : 0,
|
||||
})),
|
||||
];
|
||||
const textWidth = Math.max(...lines.map((line) => line.font.measureText(line.text)));
|
||||
const textHeight = lines.reduce((total, line) => total + line.spaceBefore + line.font.lineHeight, 0);
|
||||
const left = Math.max(PAGE_X, Math.round((width - logoWidth - textWidth) / 2));
|
||||
if (logo) {
|
||||
image.bitBlt(logo, left, Math.round((height - logo.height) / 2), { transparentZero: true });
|
||||
}
|
||||
let y = Math.round((height - textHeight) / 2);
|
||||
for (const line of lines) {
|
||||
y += line.spaceBefore;
|
||||
image.drawText(line.font, left + logoWidth, y, line.text, line.value);
|
||||
y += line.font.lineHeight;
|
||||
}
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
||||
if (event.type === "click") nav.next();
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ const PANEL_X = 310;
|
||||
/**
|
||||
* Height the table and panel are drawn for: a min-height window's viewport
|
||||
* under the standard top bar. A taller viewport (the status bar folded into
|
||||
* the switcher row, a full-panel display) centers the layout vertically.
|
||||
* the switcher row, a Tall or Full Screen display) centers the layout vertically.
|
||||
*/
|
||||
const LAYOUT_HEIGHT = 260;
|
||||
|
||||
@@ -411,8 +411,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
syncTickTimer(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 });
|
||||
window.lastSubmittedFingerprint = "";
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "resize-window":
|
||||
case "text-input":
|
||||
case "tool-call":
|
||||
break;
|
||||
|
||||
@@ -37,6 +37,15 @@ export function dailyPageUid(date: Date): string {
|
||||
return `${month}-${day}-${date.getFullYear()}`;
|
||||
}
|
||||
|
||||
/** The date a daily-note page uid names, or null for any other uid. */
|
||||
export function dailyPageDate(uid: string): Date | null {
|
||||
const match = /^(\d{2})-(\d{2})-(\d{4})$/.exec(uid);
|
||||
if (!match) return null;
|
||||
const date = new Date(Number(match[3]), Number(match[1]) - 1, Number(match[2]));
|
||||
// Rejects out-of-range fields such as 02-31, which Date would roll over.
|
||||
return dailyPageUid(date) === uid ? date : null;
|
||||
}
|
||||
|
||||
/** Daily-note page title for a date, e.g. "August 3rd, 2026". */
|
||||
export function dailyPageTitle(date: Date): string {
|
||||
const months = [
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
* Roam app worker. Shows a Roam Research page (today's daily note by default)
|
||||
* as a selectable outline: ring scroll moves the selection, click toggles a
|
||||
* todo or follows a [[page]] link, double-click walks back through visited
|
||||
* pages then yields focus. Edits (check/uncheck, add todo, edit block) go
|
||||
* pages then yields focus. The context menu jumps to today's or a picked
|
||||
* date's daily note. Edits (check/uncheck, add todo, edit block) go
|
||||
* through the Roam backend API and are also exposed as assistant tools.
|
||||
* The graph name and API token are configured here too, from the context
|
||||
* menu, via the phone app's text editor.
|
||||
@@ -19,6 +20,7 @@ import { getActiveDisplay } from "../../native/active-display";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import type { MenuItem } from "../../ui/menu";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import { DatePickerLayer } from "../../ui/date-picker";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
@@ -32,6 +34,7 @@ import { docNodePageLink, type DocNode } from "../../ui/document/document-model"
|
||||
import {
|
||||
createBlock,
|
||||
createPage,
|
||||
dailyPageDate,
|
||||
dailyPageTitle,
|
||||
dailyPageUid,
|
||||
fetchPageByUid,
|
||||
@@ -251,9 +254,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "resize-window":
|
||||
if (!window || window.windowId !== message.windowId) break;
|
||||
window.viewportWidth = message.viewport.width;
|
||||
window.viewportHeight = message.viewport.height;
|
||||
window.view.resize(message.viewport.width - DOC_MARGIN * 2, message.viewport.height - HEADER_HEIGHT - BOTTOM_MARGIN);
|
||||
window.menu?.resize(message.viewport);
|
||||
window.lastSubmittedFingerprint = "";
|
||||
render();
|
||||
break;
|
||||
case "input-focus":
|
||||
case "navigation-sensors":
|
||||
case "resize-window":
|
||||
break;
|
||||
}
|
||||
};
|
||||
@@ -272,9 +283,12 @@ function inferForeground(focused: boolean): void {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Page loading
|
||||
|
||||
function dailyRef(date: Date): PageRef {
|
||||
return { uid: dailyPageUid(date), title: dailyPageTitle(date) };
|
||||
}
|
||||
|
||||
function todayRef(): PageRef {
|
||||
const now = new Date();
|
||||
return { uid: dailyPageUid(now), title: dailyPageTitle(now) };
|
||||
return dailyRef(new Date());
|
||||
}
|
||||
|
||||
async function loadPage(ref: PageRef, options: { resetStack?: boolean; pushCurrent?: boolean } = {}): Promise<void> {
|
||||
@@ -303,8 +317,8 @@ async function loadPage(ref: PageRef, options: { resetStack?: boolean; pushCurre
|
||||
if (page) {
|
||||
currentPage = page;
|
||||
pageMissing = false;
|
||||
} else if (uid === dailyPageUid(new Date())) {
|
||||
// Today's daily note isn't created until something is written to it.
|
||||
} else if (dailyPageDate(uid)) {
|
||||
// A daily note isn't created until something is written to it.
|
||||
currentPage = { uid, title: ref.title, children: [] };
|
||||
pageMissing = true;
|
||||
} else {
|
||||
@@ -363,6 +377,22 @@ function menuItems(): MenuItem[] {
|
||||
},
|
||||
});
|
||||
}
|
||||
items.push({
|
||||
label: "Go to date",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new DatePickerLayer({
|
||||
title: "Go to date",
|
||||
initial: (currentRef?.uid ? dailyPageDate(currentRef.uid) : null) ?? new Date(),
|
||||
confirmLabel: "open",
|
||||
onPick: (date) => {
|
||||
const ref = dailyRef(date);
|
||||
if (ref.uid === currentRef?.uid) void reloadCurrentPage();
|
||||
else void loadPage(ref, { pushCurrent: true });
|
||||
},
|
||||
}));
|
||||
},
|
||||
});
|
||||
items.push({
|
||||
label: "Refresh",
|
||||
onSelect: (ctx) => {
|
||||
@@ -507,7 +537,7 @@ async function toggleTodo(node: DocNode): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Create today's daily page if it only exists locally. */
|
||||
/** Create the shown daily page if it only exists locally. */
|
||||
async function ensureCurrentPageExists(): Promise<void> {
|
||||
if (!currentPage || !pageMissing) return;
|
||||
await createPage(currentPage.title, currentPage.uid);
|
||||
|
||||
@@ -15,7 +15,8 @@ import {
|
||||
webSocketUrl,
|
||||
type T3Fetch,
|
||||
} from "./t3-auth";
|
||||
import { ShellModel, ThreadDetailModel, type T3ServerProvider } from "./t3-model";
|
||||
import type { T3ServerProvider } from "./t3-launch";
|
||||
import { ShellModel, ThreadDetailModel } from "./t3-model";
|
||||
import { T3RpcConnection, T3RpcError, type RpcSocketFactory, type T3Subscription } from "./t3-rpc";
|
||||
|
||||
/** A paired environment as stored on the phone. */
|
||||
@@ -79,6 +80,8 @@ export class T3EnvironmentClient {
|
||||
readonly shell = new ShellModel();
|
||||
providers: T3ServerProvider[] = [];
|
||||
settings: Record<string, any> | null = null;
|
||||
/** The folder of threads without a project (its project is the "Scratch" one). */
|
||||
scratchWorkspaceRoot: string | null = null;
|
||||
private phaseValue: EnvironmentPhase = "idle";
|
||||
private statusValue = "";
|
||||
private connection: T3RpcConnection | null = null;
|
||||
@@ -200,6 +203,11 @@ export class T3EnvironmentClient {
|
||||
return String(result?.threadId ?? payload.threadId);
|
||||
}
|
||||
|
||||
/** Any other unary RPC (vcs.listRefs, projects.readFile, ...). */
|
||||
call<T>(method: string, payload: Record<string, unknown>): Promise<T> {
|
||||
return this.requireConnection().call<T>(method, payload);
|
||||
}
|
||||
|
||||
private requireConnection(): T3RpcConnection {
|
||||
if (this.phaseValue !== "connected" || !this.connection) {
|
||||
throw new T3RpcError("disconnected", `Not connected to ${this.config.label || "the server"}.`);
|
||||
@@ -265,6 +273,7 @@ export class T3EnvironmentClient {
|
||||
}
|
||||
this.providers = Array.isArray(config.providers) ? config.providers : [];
|
||||
this.settings = config.settings ?? null;
|
||||
this.scratchWorkspaceRoot = typeof config.scratchWorkspaceRoot === "string" ? config.scratchWorkspaceRoot : null;
|
||||
if (typeof config.environment?.label === "string" && config.environment.label && config.environment.label !== this.config.label) {
|
||||
this.config = { ...this.config, label: config.environment.label };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,516 @@
|
||||
/**
|
||||
* New-thread options for orchestration.launchThread, resolved the way T3
|
||||
* Code's own new-task flow does it (apps/mobile/src/features/threads/
|
||||
* new-task-flow-provider.tsx, lib/modelOptions.ts, thread-settings-options.ts,
|
||||
* new-task-context-presentation.ts; packages/shared/src/projectSettings.ts
|
||||
* and model.ts): which models, model options, access modes, workspaces and
|
||||
* branches to offer, their defaults, and the launch payload.
|
||||
*
|
||||
* Defaults follow T3's project-settings chain: the project's override, then
|
||||
* the environment's setting, then (for the workspace mode) the repository's
|
||||
* checked-in t3.json, then the built-in value. The user's explicit picks live
|
||||
* in a LaunchDraft; resolveLaunch() layers them over those defaults.
|
||||
*
|
||||
* Pure (no I/O), so it runs under the Node tests; the worker owns the draft,
|
||||
* the branch list and t3.json reads.
|
||||
*/
|
||||
import type { T3ModelOptionSelection, T3ModelSelection, T3ProjectShell } from "./t3-model";
|
||||
import { uuidv4 } from "./t3-model";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Wire shapes (subset)
|
||||
|
||||
export type T3OptionChoice = { id: string; label: string; description?: string; isDefault?: boolean };
|
||||
|
||||
/** One per-model option a provider offers (reasoning effort, fast mode, ...). */
|
||||
export type T3OptionDescriptor =
|
||||
| {
|
||||
id: string;
|
||||
label: string;
|
||||
description?: string;
|
||||
type: "select";
|
||||
options: T3OptionChoice[];
|
||||
currentValue?: string;
|
||||
/** Values applied by rewriting the prompt (Claude's "ultrathink"), not offered as picks. */
|
||||
promptInjectedValues?: string[];
|
||||
}
|
||||
| { id: string; label: string; description?: string; type: "boolean"; currentValue?: boolean };
|
||||
|
||||
export type T3ProviderModel = {
|
||||
slug: string;
|
||||
name?: string;
|
||||
isDefault?: boolean;
|
||||
isLegacy?: boolean;
|
||||
capabilities?: { optionDescriptors?: T3OptionDescriptor[] } | null;
|
||||
};
|
||||
|
||||
export type T3ServerProvider = {
|
||||
instanceId: string;
|
||||
displayName?: string;
|
||||
driver?: string;
|
||||
enabled?: boolean;
|
||||
installed?: boolean;
|
||||
availability?: string;
|
||||
auth?: { status?: string };
|
||||
supportedRuntimeModes?: string[];
|
||||
models?: T3ProviderModel[];
|
||||
};
|
||||
|
||||
/** A branch from vcs.listRefs. */
|
||||
export type T3VcsRef = {
|
||||
name: string;
|
||||
isRemote?: boolean;
|
||||
remoteName?: string;
|
||||
current: boolean;
|
||||
isDefault: boolean;
|
||||
/** Set when the branch is checked out in a worktree (the project root counts). */
|
||||
worktreePath: string | null;
|
||||
};
|
||||
|
||||
export type T3RefList = {
|
||||
refs: T3VcsRef[];
|
||||
isRepo: boolean;
|
||||
hasPrimaryRemote: boolean;
|
||||
nextCursor?: number | null;
|
||||
totalCount?: number;
|
||||
};
|
||||
|
||||
/** The settings a repository's t3.json can supply. */
|
||||
export type T3ProjectFileDefaults = { defaultThreadEnvMode?: WorkspaceMode };
|
||||
|
||||
export const T3_PROJECT_FILE_NAME = "t3.json";
|
||||
|
||||
export function parseProjectFile(contents: string): T3ProjectFileDefaults | null {
|
||||
try {
|
||||
const parsed = JSON.parse(contents);
|
||||
if (!parsed || typeof parsed !== "object") return null;
|
||||
const mode = parsed.defaultThreadEnvMode;
|
||||
return mode === "local" || mode === "worktree" ? { defaultThreadEnvMode: mode } : {};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Project-scoped settings
|
||||
|
||||
type Settings = Record<string, any> | null | undefined;
|
||||
|
||||
/**
|
||||
* The project's overrides. Until the server has folded them into
|
||||
* settings.projectSettingsOverrides, the project's own legacy model and
|
||||
* workspace fields count as overrides too.
|
||||
*/
|
||||
function projectOverrides(settings: Settings, project: T3ProjectShell): Record<string, unknown> {
|
||||
const stored = (settings?.projectSettingsOverrides?.[project.id] ?? {}) as Record<string, unknown>;
|
||||
if (settings?.projectSettingsFolded) return stored;
|
||||
return {
|
||||
...(project.defaultModelSelection != null ? { defaultModelSelection: project.defaultModelSelection } : {}),
|
||||
...(project.defaultThreadEnvMode != null ? { defaultThreadEnvMode: project.defaultThreadEnvMode } : {}),
|
||||
...stored,
|
||||
};
|
||||
}
|
||||
|
||||
/** A project-scoped setting: the project's override (even null), else the environment's. */
|
||||
function projectSetting(settings: Settings, project: T3ProjectShell, key: string): unknown {
|
||||
const override = projectOverrides(settings, project)[key];
|
||||
return override !== undefined ? override : settings?.[key];
|
||||
}
|
||||
|
||||
/** The project that is the environment's Scratch folder (threads without a project). */
|
||||
export function isScratchProject(project: T3ProjectShell, scratchWorkspaceRoot: string | null | undefined): boolean {
|
||||
if (!scratchWorkspaceRoot || !project.workspaceRoot) return false;
|
||||
const normalize = (path: string) => path.replace(/[\\/]+$/, "");
|
||||
return normalize(project.workspaceRoot) === normalize(scratchWorkspaceRoot);
|
||||
}
|
||||
|
||||
/**
|
||||
* The same repository among another environment's projects (moving a new
|
||||
* thread to another computer): repository identity, else the checkout's
|
||||
* folder name, else the title.
|
||||
*/
|
||||
export function matchingProject(projects: Iterable<T3ProjectShell>, target: T3ProjectShell): T3ProjectShell | null {
|
||||
const list = [...projects];
|
||||
const key = target.repositoryIdentity?.canonicalKey ?? null;
|
||||
if (key) {
|
||||
const match = list.find((project) => project.repositoryIdentity?.canonicalKey === key);
|
||||
if (match) return match;
|
||||
}
|
||||
const folder = baseName(target.workspaceRoot);
|
||||
return (
|
||||
(folder ? list.find((project) => baseName(project.workspaceRoot) === folder) : undefined) ??
|
||||
list.find((project) => project.title === target.title) ??
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
function baseName(path: string | undefined): string {
|
||||
return (path ?? "").replace(/[\\/]+$/, "").split(/[\\/]/).pop() ?? "";
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Providers and models
|
||||
|
||||
function providerUsable(provider: T3ServerProvider): boolean {
|
||||
return (
|
||||
provider.enabled !== false &&
|
||||
provider.installed !== false &&
|
||||
provider.auth?.status !== "unauthenticated" &&
|
||||
provider.availability !== "unavailable"
|
||||
);
|
||||
}
|
||||
|
||||
function findProvider(providers: readonly T3ServerProvider[], instanceId: string | null | undefined): T3ServerProvider | undefined {
|
||||
return instanceId ? providers.find((provider) => provider.instanceId === instanceId) : undefined;
|
||||
}
|
||||
|
||||
function findModel(providers: readonly T3ServerProvider[], selection: T3ModelSelection | null | undefined): T3ProviderModel | undefined {
|
||||
return findProvider(providers, selection?.instanceId)?.models?.find((model) => model.slug === selection?.model);
|
||||
}
|
||||
|
||||
const DRIVER_NAMES: Record<string, string> = { codex: "Codex", claudeAgent: "Claude", pi: "Pi" };
|
||||
|
||||
/** "Claude", "Codex", or the provider's configured name. */
|
||||
export function providerDisplayName(providers: readonly T3ServerProvider[], instanceId: string | null | undefined): string {
|
||||
if (!instanceId) return "";
|
||||
const provider = findProvider(providers, instanceId);
|
||||
const driver = provider?.driver ?? "";
|
||||
return provider?.displayName || DRIVER_NAMES[driver] || driver || instanceId;
|
||||
}
|
||||
|
||||
/** The model's display name ("Claude Opus 5"), else its slug. */
|
||||
export function modelDisplayName(providers: readonly T3ServerProvider[], selection: T3ModelSelection | null | undefined): string {
|
||||
if (!selection) return "";
|
||||
return findModel(providers, selection)?.name || selection.model;
|
||||
}
|
||||
|
||||
export type ModelChoiceGroup = {
|
||||
instanceId: string;
|
||||
label: string;
|
||||
/** Current models first, then legacy ones. */
|
||||
models: Array<{ selection: T3ModelSelection; label: string; isLegacy: boolean }>;
|
||||
};
|
||||
|
||||
/** Every model on a usable provider, grouped by provider. */
|
||||
export function modelChoices(providers: readonly T3ServerProvider[]): ModelChoiceGroup[] {
|
||||
const groups: ModelChoiceGroup[] = [];
|
||||
for (const provider of providers) {
|
||||
if (!providerUsable(provider)) continue;
|
||||
const models = (provider.models ?? [])
|
||||
.map((model) => ({
|
||||
selection: { instanceId: provider.instanceId, model: model.slug },
|
||||
label: model.name || model.slug,
|
||||
isLegacy: model.isLegacy === true,
|
||||
}))
|
||||
.sort((a, b) => Number(a.isLegacy) - Number(b.isLegacy));
|
||||
if (models.length) groups.push({ instanceId: provider.instanceId, label: providerDisplayName(providers, provider.instanceId), models });
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model for a new thread, the way T3's mobile app defaults it: the project's
|
||||
* default (project override, then environment), then the first usable
|
||||
* provider's default model, then any current model. Legacy models are never
|
||||
* picked implicitly.
|
||||
*/
|
||||
export function defaultModelSelection(
|
||||
settings: Settings,
|
||||
providers: readonly T3ServerProvider[],
|
||||
project: T3ProjectShell,
|
||||
): T3ModelSelection | null {
|
||||
const usable = (selection: unknown): selection is T3ModelSelection => {
|
||||
const candidate = selection as T3ModelSelection | null | undefined;
|
||||
if (!candidate?.instanceId || !candidate.model) return false;
|
||||
const provider = findProvider(providers, candidate.instanceId);
|
||||
if (!provider) return providers.length === 0;
|
||||
return providerUsable(provider) && findModel(providers, candidate)?.isLegacy !== true;
|
||||
};
|
||||
const override = projectOverrides(settings, project).defaultModelSelection;
|
||||
for (const candidate of [override, settings?.defaultModelSelection]) if (usable(candidate)) return candidate;
|
||||
for (const pickDefault of [true, false]) {
|
||||
for (const provider of providers) {
|
||||
if (!providerUsable(provider)) continue;
|
||||
const model = (provider.models ?? []).find((entry) => !entry.isLegacy && (!pickDefault || entry.isDefault));
|
||||
if (model) return { instanceId: provider.instanceId, model: model.slug };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-model options (effort, fast mode, ...)
|
||||
|
||||
/** Effort values T3 mobile leaves out of its picker (a workflow trigger, not a reasoning level). */
|
||||
const HIDDEN_CHOICE_IDS = new Set(["ultracode"]);
|
||||
|
||||
export function modelOptionDescriptors(providers: readonly T3ServerProvider[], selection: T3ModelSelection | null | undefined): T3OptionDescriptor[] {
|
||||
return findModel(providers, selection)?.capabilities?.optionDescriptors ?? [];
|
||||
}
|
||||
|
||||
function selectionOptions(selection: T3ModelSelection | null | undefined): T3ModelOptionSelection[] {
|
||||
return Array.isArray(selection?.options) ? selection!.options! : [];
|
||||
}
|
||||
|
||||
function validChoice(descriptor: Extract<T3OptionDescriptor, { type: "select" }>, value: unknown): string | undefined {
|
||||
if (typeof value !== "string" || !value) return undefined;
|
||||
if (!descriptor.options.length) return value;
|
||||
if (descriptor.promptInjectedValues?.includes(value)) return undefined;
|
||||
return descriptor.options.some((option) => option.id === value) ? value : undefined;
|
||||
}
|
||||
|
||||
/** The option's effective value: the selection's pick, else the provider's current value, else its default. */
|
||||
export function optionValue(descriptor: T3OptionDescriptor, selection: T3ModelSelection | null | undefined): string | boolean | undefined {
|
||||
const picked = selectionOptions(selection).find((option) => option.id === descriptor.id)?.value;
|
||||
if (descriptor.type === "boolean") {
|
||||
if (typeof picked === "boolean") return picked;
|
||||
return descriptor.currentValue;
|
||||
}
|
||||
return (
|
||||
validChoice(descriptor, picked) ??
|
||||
validChoice(descriptor, descriptor.currentValue) ??
|
||||
descriptor.options.find((option) => option.isDefault)?.id
|
||||
);
|
||||
}
|
||||
|
||||
export function optionValueLabel(descriptor: T3OptionDescriptor, value: string | boolean | undefined): string {
|
||||
if (descriptor.type === "boolean") return value ? "On" : "Off";
|
||||
return descriptor.options.find((option) => option.id === value)?.label ?? (typeof value === "string" ? value : "Default");
|
||||
}
|
||||
|
||||
/** The choices to offer for a select option. */
|
||||
export function selectableChoices(descriptor: Extract<T3OptionDescriptor, { type: "select" }>): T3OptionChoice[] {
|
||||
const injected = new Set(descriptor.promptInjectedValues ?? []);
|
||||
return descriptor.options.filter((option) => !injected.has(option.id) && !HIDDEN_CHOICE_IDS.has(option.id));
|
||||
}
|
||||
|
||||
/** The selection with one option picked. */
|
||||
export function withModelOption(selection: T3ModelSelection, id: string, value: string | boolean): T3ModelSelection {
|
||||
return { ...selection, options: [...selectionOptions(selection).filter((option) => option.id !== id), { id, value }] };
|
||||
}
|
||||
|
||||
/**
|
||||
* The selection as sent: only explicitly picked options whose values the
|
||||
* model still accepts (T3's normalizeSelectionOptions); none at all leaves
|
||||
* the provider's defaults in charge.
|
||||
*/
|
||||
export function dispatchModelSelection(providers: readonly T3ServerProvider[], selection: T3ModelSelection): T3ModelSelection {
|
||||
const bare = { instanceId: selection.instanceId, model: selection.model };
|
||||
const picked = selectionOptions(selection);
|
||||
if (!picked.length) return bare;
|
||||
const model = findModel(providers, selection);
|
||||
if (!model?.capabilities) return { ...bare, options: picked };
|
||||
const options: T3ModelOptionSelection[] = [];
|
||||
for (const descriptor of model.capabilities.optionDescriptors ?? []) {
|
||||
if (!picked.some((option) => option.id === descriptor.id)) continue;
|
||||
const value = optionValue(descriptor, selection);
|
||||
if (typeof value === "string" || typeof value === "boolean") options.push({ id: descriptor.id, value });
|
||||
}
|
||||
return options.length ? { ...bare, options } : bare;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Access (runtime mode)
|
||||
|
||||
export const RUNTIME_MODE_CHOICES: ReadonlyArray<{ mode: string; label: string; description: string }> = [
|
||||
{ mode: "approval-required", label: "Supervised", description: "Ask before commands and file changes." },
|
||||
{ mode: "auto-accept-edits", label: "Auto-accept edits", description: "Auto-approve edits, ask before other actions." },
|
||||
{ mode: "auto", label: "Auto", description: "Supported providers approve routine actions; others still ask." },
|
||||
{ mode: "full-access", label: "Full access", description: "Allow commands and edits without prompts." },
|
||||
];
|
||||
|
||||
const DEFAULT_RUNTIME_MODE = "full-access";
|
||||
|
||||
/** The access modes the provider supports (all of them when it doesn't say). */
|
||||
export function runtimeModeChoices(provider: T3ServerProvider | null | undefined): typeof RUNTIME_MODE_CHOICES {
|
||||
const supported = provider?.supportedRuntimeModes;
|
||||
if (!supported?.length) return RUNTIME_MODE_CHOICES;
|
||||
const filtered = RUNTIME_MODE_CHOICES.filter((choice) => supported.includes(choice.mode));
|
||||
return filtered.length ? filtered : RUNTIME_MODE_CHOICES;
|
||||
}
|
||||
|
||||
export function runtimeModeLabel(mode: string): string {
|
||||
return RUNTIME_MODE_CHOICES.find((choice) => choice.mode === mode)?.label ?? mode;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Workspace and branch
|
||||
|
||||
export type WorkspaceMode = "local" | "worktree";
|
||||
|
||||
/** Project override, then environment setting, then the repo's t3.json, then "local". */
|
||||
export function defaultWorkspaceMode(settings: Settings, project: T3ProjectShell, projectFile: T3ProjectFileDefaults | null | undefined): WorkspaceMode {
|
||||
const value = projectSetting(settings, project, "defaultThreadEnvMode");
|
||||
if (value === "local" || value === "worktree") return value;
|
||||
return projectFile?.defaultThreadEnvMode ?? "local";
|
||||
}
|
||||
|
||||
function defaultStartFromOrigin(settings: Settings, project: T3ProjectShell): boolean {
|
||||
const value = projectSetting(settings, project, "newWorktreesStartFromOrigin");
|
||||
return typeof value === "boolean" ? value : true;
|
||||
}
|
||||
|
||||
/** The checked-out branch's worktree, when it isn't the project checkout itself. */
|
||||
function separateWorktree(ref: T3VcsRef, project: T3ProjectShell): string | null {
|
||||
return ref.worktreePath && ref.worktreePath !== project.workspaceRoot ? ref.worktreePath : null;
|
||||
}
|
||||
|
||||
/** A short tag for a branch in the picker. */
|
||||
export function refBadge(ref: T3VcsRef, project: T3ProjectShell): string {
|
||||
if (ref.current) return "current";
|
||||
if (separateWorktree(ref, project)) return "worktree";
|
||||
if (ref.isRemote) return "remote";
|
||||
if (ref.isDefault) return "default";
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Picking this branch for a thread in the project checkout means switching
|
||||
* that checkout to it first (vcs.switchRef), as T3 mobile does. Branches
|
||||
* already checked out somewhere run where they are.
|
||||
*/
|
||||
export function needsCheckout(ref: T3VcsRef, mode: WorkspaceMode): boolean {
|
||||
return mode === "local" && !ref.current && !ref.worktreePath;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Draft → launch
|
||||
|
||||
/** The user's explicit picks; null fields take the resolved default. */
|
||||
export type LaunchDraft = {
|
||||
model: T3ModelSelection | null;
|
||||
runtimeMode: string | null;
|
||||
workspaceMode: WorkspaceMode | null;
|
||||
/** Local: the branch the thread runs on; worktree: the base branch. */
|
||||
branch: string | null;
|
||||
/** Local: run in this existing worktree (the picked branch is checked out there). */
|
||||
worktreePath: string | null;
|
||||
startFromOrigin: boolean | null;
|
||||
};
|
||||
|
||||
export function emptyLaunchDraft(): LaunchDraft {
|
||||
return { model: null, runtimeMode: null, workspaceMode: null, branch: null, worktreePath: null, startFromOrigin: null };
|
||||
}
|
||||
|
||||
export type LaunchContext = {
|
||||
settings: Settings;
|
||||
providers: readonly T3ServerProvider[];
|
||||
project: T3ProjectShell;
|
||||
scratchWorkspaceRoot?: string | null;
|
||||
/** The repo's t3.json defaults: undefined while unread, null when absent. */
|
||||
projectFile?: T3ProjectFileDefaults | null;
|
||||
/** vcs.listRefs for the project, or null until loaded. */
|
||||
refs: T3RefList | null;
|
||||
};
|
||||
|
||||
export type ResolvedLaunch = {
|
||||
model: T3ModelSelection | null;
|
||||
runtimeMode: string;
|
||||
/** False for the Scratch project and for folders that aren't repositories. */
|
||||
canChooseWorkspace: boolean;
|
||||
workspaceMode: WorkspaceMode;
|
||||
branch: string | null;
|
||||
worktreePath: string | null;
|
||||
startFromOrigin: boolean;
|
||||
/** Why the thread can't start yet (nothing to run it with, no base branch). */
|
||||
blocker: string | null;
|
||||
};
|
||||
|
||||
export function resolveLaunch(draft: LaunchDraft, context: LaunchContext): ResolvedLaunch {
|
||||
const { settings, providers, project, refs } = context;
|
||||
const model = draft.model ?? defaultModelSelection(settings, providers, project);
|
||||
const choices = runtimeModeChoices(findProvider(providers, model?.instanceId));
|
||||
const wantedMode = draft.runtimeMode ?? (projectSetting(settings, project, "defaultRuntimeMode") as string | null | undefined) ?? DEFAULT_RUNTIME_MODE;
|
||||
const runtimeMode = choices.some((choice) => choice.mode === wantedMode) ? wantedMode : choices[0]!.mode;
|
||||
const canChooseWorkspace = !isScratchProject(project, context.scratchWorkspaceRoot) && refs?.isRepo !== false;
|
||||
const workspaceMode: WorkspaceMode = canChooseWorkspace ? (draft.workspaceMode ?? defaultWorkspaceMode(settings, project, context.projectFile)) : "local";
|
||||
const startFromOrigin = draft.startFromOrigin ?? defaultStartFromOrigin(settings, project);
|
||||
const localRefs = (refs?.refs ?? []).filter((ref) => !ref.isRemote);
|
||||
let branch: string | null;
|
||||
let worktreePath: string | null = null;
|
||||
if (workspaceMode === "worktree") {
|
||||
// The default branch may only exist as origin/<default>, so search every ref for it.
|
||||
branch = draft.branch ?? refs?.refs.find((ref) => ref.isDefault)?.name ?? localRefs.find((ref) => ref.current)?.name ?? null;
|
||||
} else if (draft.branch) {
|
||||
branch = draft.branch;
|
||||
worktreePath = draft.worktreePath;
|
||||
} else {
|
||||
const current = localRefs.find((ref) => ref.current);
|
||||
branch = current?.name ?? null;
|
||||
worktreePath = current ? separateWorktree(current, project) : null;
|
||||
}
|
||||
let blocker: string | null = null;
|
||||
if (!model) blocker = "No usable agent on that computer: sign in to Claude Code, Codex or another provider in T3 Code first.";
|
||||
else if (workspaceMode === "worktree" && !branch) blocker = refs ? "Choose a base branch for the worktree." : "Still loading branches.";
|
||||
return { model, runtimeMode, canChooseWorkspace, workspaceMode, branch, worktreePath, startFromOrigin, blocker };
|
||||
}
|
||||
|
||||
/** "Current checkout", "Existing worktree" or "New worktree". */
|
||||
export function workspaceLabel(resolved: Pick<ResolvedLaunch, "workspaceMode" | "worktreePath">): string {
|
||||
if (resolved.workspaceMode === "worktree") return "New worktree";
|
||||
return resolved.worktreePath ? "Existing worktree" : "Current checkout";
|
||||
}
|
||||
|
||||
/** The branch row: "main", or a worktree's base "From origin/main". */
|
||||
export function branchLabel(resolved: Pick<ResolvedLaunch, "workspaceMode" | "branch" | "startFromOrigin">, hasPrimaryRemote: boolean): string {
|
||||
if (!resolved.branch) return resolved.workspaceMode === "worktree" ? "Choose branch" : "Unknown";
|
||||
if (resolved.workspaceMode === "local") return resolved.branch;
|
||||
const fromOrigin = resolved.startFromOrigin && hasPrimaryRemote && !resolved.branch.startsWith("origin/");
|
||||
return `From ${fromOrigin ? `origin/${resolved.branch}` : resolved.branch}`;
|
||||
}
|
||||
|
||||
/** The draft after picking a branch (or worktree base) in the given mode. */
|
||||
export function pickBranch(draft: LaunchDraft, ref: T3VcsRef, mode: WorkspaceMode, project: T3ProjectShell): LaunchDraft {
|
||||
return { ...draft, branch: ref.name, worktreePath: mode === "local" ? separateWorktree(ref, project) : null };
|
||||
}
|
||||
|
||||
/**
|
||||
* The draft after switching workspace mode. A new worktree keeps an explicitly
|
||||
* picked base; the project checkout goes back to whatever is checked out.
|
||||
*/
|
||||
export function pickWorkspaceMode(draft: LaunchDraft, mode: WorkspaceMode): LaunchDraft {
|
||||
return mode === "local"
|
||||
? { ...draft, workspaceMode: mode, branch: null, worktreePath: null }
|
||||
: { ...draft, workspaceMode: mode, worktreePath: null };
|
||||
}
|
||||
|
||||
/** Where the server runs the thread (OrchestrationV2 workspaceStrategy). */
|
||||
export function workspaceStrategy(resolved: ResolvedLaunch): Record<string, unknown> {
|
||||
if (resolved.workspaceMode === "worktree") {
|
||||
return { type: "worktree", baseRef: resolved.branch, ...(resolved.startFromOrigin ? { startFromOrigin: true } : {}) };
|
||||
}
|
||||
if (resolved.worktreePath) {
|
||||
return { type: "existing_worktree", worktreePath: resolved.worktreePath, ...(resolved.branch ? { branch: resolved.branch } : {}) };
|
||||
}
|
||||
return { type: "root", ...(resolved.branch ? { branch: resolved.branch } : {}) };
|
||||
}
|
||||
|
||||
/** First line of a prompt as a provisional title (the server generates a better one). */
|
||||
export function provisionalTitle(text: string): string {
|
||||
const collapsed = text.replace(/\s+/g, " ").trim();
|
||||
if (!collapsed) return "New thread";
|
||||
return collapsed.length > 72 ? `${collapsed.slice(0, 71).trimEnd()}…` : collapsed;
|
||||
}
|
||||
|
||||
/** The orchestration.launchThread payload. Throws when resolveLaunch reported a blocker. */
|
||||
export function launchThreadPayload(
|
||||
project: T3ProjectShell,
|
||||
text: string,
|
||||
resolved: ResolvedLaunch,
|
||||
providers: readonly T3ServerProvider[],
|
||||
): Record<string, unknown> {
|
||||
if (resolved.blocker || !resolved.model) throw new Error(resolved.blocker ?? "No model selected.");
|
||||
return {
|
||||
commandId: uuidv4(),
|
||||
creationSource: "mobile",
|
||||
threadId: uuidv4(),
|
||||
projectId: project.id,
|
||||
title: provisionalTitle(text),
|
||||
generateTitle: true,
|
||||
modelSelection: dispatchModelSelection(providers, resolved.model),
|
||||
runtimeMode: resolved.runtimeMode,
|
||||
interactionMode: "default",
|
||||
workspaceStrategy: workspaceStrategy(resolved),
|
||||
initialMessage: { messageId: uuidv4(), text, attachments: [] },
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
* transport and rendering: the shell (projects + thread list) reducer, the
|
||||
* per-thread detail reducer, thread status / ordering for the glasses list,
|
||||
* one-line summaries of timeline items, and the dispatch-command builders.
|
||||
* (New-thread options and the launch payload are in t3-launch.ts.)
|
||||
*
|
||||
* Mirrors T3's own client runtime (packages/client-runtime/src/state/
|
||||
* shellReducer.ts, orchestrationV2Projection.ts, models.ts; apps/mobile's
|
||||
@@ -18,10 +19,16 @@ export type T3ProjectShell = {
|
||||
id: string;
|
||||
title: string;
|
||||
workspaceRoot?: string;
|
||||
repositoryIdentity?: { canonicalKey?: string | null } | null;
|
||||
/** Legacy per-project defaults (superseded by settings.projectSettingsOverrides once folded). */
|
||||
defaultModelSelection?: T3ModelSelection | null;
|
||||
defaultThreadEnvMode?: string | null;
|
||||
};
|
||||
|
||||
export type T3ModelSelection = { instanceId: string; model: string; options?: unknown[] };
|
||||
/** A provider model option picked for a run ("effort" = "high", "fastMode" = true). */
|
||||
export type T3ModelOptionSelection = { id: string; value: string | boolean };
|
||||
|
||||
export type T3ModelSelection = { instanceId: string; model: string; options?: T3ModelOptionSelection[] };
|
||||
|
||||
export type T3ThreadShell = {
|
||||
id: string;
|
||||
@@ -839,86 +846,3 @@ export function approvalOptions(item: T3TurnItem): T3ApprovalOption[] {
|
||||
export function optionAnswer(option: { label: string; value?: string }): string {
|
||||
return option.value ?? option.label;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// New threads
|
||||
|
||||
export type T3ServerProvider = {
|
||||
instanceId: string;
|
||||
displayName?: string;
|
||||
driver?: string;
|
||||
enabled?: boolean;
|
||||
installed?: boolean;
|
||||
auth?: { status?: string };
|
||||
models?: Array<{ slug: string; name?: string; isDefault?: boolean; isLegacy?: boolean }>;
|
||||
};
|
||||
|
||||
function providerUsable(provider: T3ServerProvider): boolean {
|
||||
return provider.enabled !== false && provider.installed !== false && provider.auth?.status !== "unauthenticated";
|
||||
}
|
||||
|
||||
export function providerDisplayName(providers: readonly T3ServerProvider[], instanceId: string | null | undefined): string {
|
||||
if (!instanceId) return "";
|
||||
const provider = providers.find((candidate) => candidate.instanceId === instanceId);
|
||||
return provider?.displayName || provider?.driver || instanceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model for a new thread, the way T3's mobile app defaults it: project
|
||||
* override, environment default, the project's legacy default, then the
|
||||
* first usable provider's default model, then any usable model.
|
||||
*/
|
||||
export function defaultModelSelection(
|
||||
settings: Record<string, any> | null,
|
||||
providers: readonly T3ServerProvider[],
|
||||
project: T3ProjectShell,
|
||||
): T3ModelSelection | null {
|
||||
const usable = (selection: T3ModelSelection | null | undefined): selection is T3ModelSelection => {
|
||||
if (!selection?.instanceId || !selection.model) return false;
|
||||
const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
|
||||
return provider ? providerUsable(provider) : providers.length === 0;
|
||||
};
|
||||
const candidates = [
|
||||
settings?.projectSettingsOverrides?.[project.id]?.defaultModelSelection,
|
||||
settings?.defaultModelSelection,
|
||||
project.defaultModelSelection,
|
||||
];
|
||||
for (const candidate of candidates) if (usable(candidate)) return candidate;
|
||||
for (const pickDefault of [true, false]) {
|
||||
for (const provider of providers) {
|
||||
if (!providerUsable(provider)) continue;
|
||||
const model = (provider.models ?? []).find((entry) => !entry.isLegacy && (!pickDefault || entry.isDefault));
|
||||
if (model) return { instanceId: provider.instanceId, model: model.slug };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** First line of a prompt as a provisional title (the server generates a better one). */
|
||||
export function provisionalTitle(text: string): string {
|
||||
const collapsed = oneLine(text);
|
||||
if (!collapsed) return "New thread";
|
||||
return collapsed.length > 72 ? `${collapsed.slice(0, 71).trimEnd()}…` : collapsed;
|
||||
}
|
||||
|
||||
export function launchThreadPayload(
|
||||
project: T3ProjectShell,
|
||||
text: string,
|
||||
modelSelection: T3ModelSelection,
|
||||
settings: Record<string, any> | null,
|
||||
): Record<string, unknown> {
|
||||
const runtimeMode = settings?.projectSettingsOverrides?.[project.id]?.defaultRuntimeMode ?? settings?.defaultRuntimeMode ?? "full-access";
|
||||
return {
|
||||
commandId: uuidv4(),
|
||||
creationSource: "mobile",
|
||||
threadId: uuidv4(),
|
||||
projectId: project.id,
|
||||
title: provisionalTitle(text),
|
||||
generateTitle: true,
|
||||
modelSelection,
|
||||
runtimeMode,
|
||||
interactionMode: "default",
|
||||
workspaceStrategy: { type: "root" },
|
||||
initialMessage: { messageId: uuidv4(), text, attachments: [] },
|
||||
};
|
||||
}
|
||||
@@ -15,7 +15,9 @@
|
||||
* chosen from the menu). Tap-then-hold has Stop, Settle, etc.
|
||||
* - environments / pair: manage paired servers; pairing takes the link from
|
||||
* `t3 pair`, typed into the phone app's text editor.
|
||||
* - compose: the first message of a new thread in a chosen project.
|
||||
* - compose: a new thread's options (host, project, model and its options,
|
||||
* access mode, workspace/worktree, branch), then its first message by
|
||||
* voice or phone keyboard.
|
||||
* Double-click backs out a level, and from the list yields focus.
|
||||
*
|
||||
* Connections (one T3EnvironmentClient per paired environment) live while the
|
||||
@@ -39,7 +41,7 @@ import { openSocket } from "../../native/socket";
|
||||
import { fetchWithUserAgent } from "../../util/http";
|
||||
import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
||||
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
||||
@@ -57,17 +59,14 @@ import { buildGlanceSnapshot, glanceSummary, T3_GLANCE_STATE_KEY } from "./t3-gl
|
||||
import {
|
||||
approvalOptions,
|
||||
approvalResponseCommand,
|
||||
defaultModelSelection,
|
||||
describeRequest,
|
||||
formatAge,
|
||||
interruptCommand,
|
||||
isThreadSettled,
|
||||
isThreadUnread,
|
||||
launchThreadPayload,
|
||||
messageDispatchCommand,
|
||||
optionAnswer,
|
||||
plainMarkdown,
|
||||
providerDisplayName,
|
||||
questionAnswerCommand,
|
||||
sectionThreads,
|
||||
settleCommand,
|
||||
@@ -78,10 +77,43 @@ import {
|
||||
timelineEntries,
|
||||
visitCommand,
|
||||
type PendingRequest,
|
||||
type T3ModelSelection,
|
||||
type T3ProjectShell,
|
||||
type T3ThreadShell,
|
||||
type T3ThreadStatus,
|
||||
} from "./t3-model";
|
||||
import {
|
||||
branchLabel,
|
||||
emptyLaunchDraft,
|
||||
isScratchProject,
|
||||
launchThreadPayload,
|
||||
matchingProject,
|
||||
modelChoices,
|
||||
modelDisplayName,
|
||||
modelOptionDescriptors,
|
||||
needsCheckout,
|
||||
optionValue,
|
||||
optionValueLabel,
|
||||
parseProjectFile,
|
||||
pickBranch,
|
||||
pickWorkspaceMode,
|
||||
providerDisplayName,
|
||||
refBadge,
|
||||
resolveLaunch,
|
||||
runtimeModeChoices,
|
||||
runtimeModeLabel,
|
||||
selectableChoices,
|
||||
T3_PROJECT_FILE_NAME,
|
||||
withModelOption,
|
||||
workspaceLabel,
|
||||
type LaunchDraft,
|
||||
type ResolvedLaunch,
|
||||
type T3OptionDescriptor,
|
||||
type T3ProjectFileDefaults,
|
||||
type T3RefList,
|
||||
type T3VcsRef,
|
||||
type WorkspaceMode,
|
||||
} from "./t3-launch";
|
||||
import { parsePairingInput, baseUrlLabel } from "./t3-pairing";
|
||||
import type { RpcSocketFactory } from "./t3-rpc";
|
||||
import { TranscriptLayout } from "./t3-transcript";
|
||||
@@ -118,7 +150,7 @@ type Screen =
|
||||
| { kind: "thread"; envId: string; threadId: string }
|
||||
| { kind: "environments" }
|
||||
| { kind: "pair"; repairId: string | null }
|
||||
| { kind: "compose"; envId: string; projectId: string };
|
||||
| { kind: "compose" };
|
||||
|
||||
type AppWindow = {
|
||||
windowId: string;
|
||||
@@ -157,9 +189,10 @@ type ThreadView = {
|
||||
|
||||
type ListItem = {
|
||||
key: string;
|
||||
kind: "heading" | "thread" | "action";
|
||||
/** "field": a new-thread option, its name in a column and `right` as its value. */
|
||||
kind: "heading" | "thread" | "action" | "field";
|
||||
label: string;
|
||||
/** Dim text at the right edge (project, age, status). */
|
||||
/** Dim text at the right edge (project, age, status); a field's value. */
|
||||
right?: string;
|
||||
/** Status marker drawn left of a thread title. */
|
||||
marker?: string;
|
||||
@@ -184,6 +217,25 @@ let noticeTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let busyText = "";
|
||||
let screenError = "";
|
||||
|
||||
/** The new thread being set up on the compose screen. */
|
||||
type ComposeState = {
|
||||
envId: string;
|
||||
projectId: string;
|
||||
draft: LaunchDraft;
|
||||
/** vcs.listRefs for the project: null while loading, or when it failed (refsError). */
|
||||
refs: T3RefList | null;
|
||||
refsError: string;
|
||||
/** The repo's t3.json defaults: undefined while reading, null when it has none. */
|
||||
projectFile: T3ProjectFileDefaults | null | undefined;
|
||||
/** Settles when the branch list and t3.json reads in flight finish. */
|
||||
loading: Promise<void>;
|
||||
};
|
||||
|
||||
let compose: ComposeState | null = null;
|
||||
let composeSelectionKey: string | null = null;
|
||||
/** Where field values start on the compose screen (set per paint from the field names). */
|
||||
let composeValueColumn = 0;
|
||||
|
||||
const clients = new Map<string, T3EnvironmentClient>();
|
||||
const clientUnsubscribers = new Map<string, () => void>();
|
||||
/** The label last saved for each environment. */
|
||||
@@ -276,7 +328,8 @@ const T3_TOOLS: ToolSpec[] = [
|
||||
},
|
||||
{
|
||||
name: "start_thread",
|
||||
description: "Start a new T3 Code thread in a project (found by matching its name) with a first message for the agent.",
|
||||
description:
|
||||
"Start a new T3 Code thread in a project (found by matching its name) with a first message for the agent. Uses the project's defaults for model, access mode and workspace.",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
@@ -339,6 +392,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
if (window.renderTimer) clearTimeout(window.renderTimer);
|
||||
window = null;
|
||||
closeThreadView();
|
||||
compose = null;
|
||||
screen = { kind: "list" };
|
||||
// The Glanceboard widget may still want the connections.
|
||||
if (!hasT3BackgroundWork()) stopAllClients();
|
||||
@@ -582,10 +636,11 @@ function noteAttention(client: T3EnvironmentClient): void {
|
||||
function setScreen(next: Screen): void {
|
||||
leaveScreenSideEffects();
|
||||
if (next.kind !== "thread") closeThreadView();
|
||||
if (next.kind !== "compose") compose = null;
|
||||
screen = next;
|
||||
busyText = "";
|
||||
screenError = "";
|
||||
if (next.kind === "list" || next.kind === "environments") listMenu?.select(0);
|
||||
if (next.kind === "list" || next.kind === "environments" || next.kind === "compose") listMenu?.select(0);
|
||||
if (window) post({ type: "set-title", windowId: window.windowId, title: next.kind === "thread" ? threadTitle() : APP_TITLE });
|
||||
updateListRefreshTimer();
|
||||
}
|
||||
@@ -689,10 +744,10 @@ function showNotice(text: string): void {
|
||||
* From inside another menu's item the old menu is still closing (WindowMenu
|
||||
* ignores open() until its input handling finishes), so wait a tick.
|
||||
*/
|
||||
function openWindowMenu(items: MenuItem[], title: string): void {
|
||||
function openWindowMenu(items: MenuItem[], title: string, selectedIndex?: number): void {
|
||||
const open = () => {
|
||||
if (!window) return;
|
||||
windowMenu(window).open(items, title);
|
||||
windowMenu(window).open(items, title, selectedIndex);
|
||||
renderNow();
|
||||
};
|
||||
if (window?.menu?.isOpen()) setTimeout(open, 0);
|
||||
@@ -732,6 +787,7 @@ function handleInput(win: AppWindow, event: InputEvent, frameId: number): void {
|
||||
switch (screen.kind) {
|
||||
case "list":
|
||||
case "environments":
|
||||
case "compose":
|
||||
handleListInput(event, frameId);
|
||||
return;
|
||||
case "thread":
|
||||
@@ -744,13 +800,6 @@ function handleInput(win: AppWindow, event: InputEvent, frameId: number): void {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case "compose":
|
||||
if (event.type === "click" && !busyText) {
|
||||
post({ type: "start-voice-input", windowId: win.windowId });
|
||||
frameTimings.finishFrame(frameId, "discarded: t3code opened voice input");
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
frameTimings.finishFrame(frameId, "discarded: t3code ignored input");
|
||||
}
|
||||
@@ -761,14 +810,26 @@ function handleListInput(event: InputEvent, frameId: number): void {
|
||||
return;
|
||||
}
|
||||
const menu = list();
|
||||
const items = screen.kind === "environments" ? environmentItems() : listItems();
|
||||
const items = currentListItems();
|
||||
menu.setItems(items, selectionIndex(items));
|
||||
// Item actions are synchronous, so the move or action has applied when this returns.
|
||||
menu.handleInput(event).catch((error) => console.error("t3code list action failed", error));
|
||||
if (event.type !== "click") listSelectionKey = menu.selectedItem?.key ?? null;
|
||||
if (event.type !== "click") setSelectedKey(menu.selectedItem?.key ?? null);
|
||||
renderNow(frameId);
|
||||
}
|
||||
|
||||
/** The rows of whichever list-shaped screen is showing. */
|
||||
function currentListItems(): ListItem[] {
|
||||
switch (screen.kind) {
|
||||
case "environments":
|
||||
return environmentItems();
|
||||
case "compose":
|
||||
return composeItems();
|
||||
default:
|
||||
return listItems();
|
||||
}
|
||||
}
|
||||
|
||||
function handleThreadInput(event: InputEvent, frameId: number): void {
|
||||
const view = threadView;
|
||||
if (!view) {
|
||||
@@ -825,7 +886,7 @@ function handleTextInput(text: string): void {
|
||||
render();
|
||||
return;
|
||||
case "compose":
|
||||
void launchNewThread(screen.envId, screen.projectId, text);
|
||||
void launchNewThread(text);
|
||||
return;
|
||||
case "thread": {
|
||||
const view = threadView;
|
||||
@@ -1081,8 +1142,7 @@ function projectMenuItems(): MenuItem[] {
|
||||
label: multiEnv ? `${project.title} · ${environmentName(client)}` : project.title,
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.clearToBase();
|
||||
setScreen({ kind: "compose", envId: client.config.id, projectId: project.id });
|
||||
if (window) post({ type: "start-voice-input", windowId: window.windowId });
|
||||
beginCompose(client, project);
|
||||
},
|
||||
}));
|
||||
}
|
||||
@@ -1156,37 +1216,432 @@ async function pairFromDraft(): Promise<void> {
|
||||
// ---------------------------------------------------------------------------
|
||||
// New threads
|
||||
|
||||
function composeTarget(): { client: T3EnvironmentClient; project: T3ProjectShell } | null {
|
||||
if (screen.kind !== "compose") return null;
|
||||
const client = clients.get(screen.envId);
|
||||
const project = client?.shell.projects.get(screen.projectId);
|
||||
return client && project ? { client, project } : null;
|
||||
/** Most branches a picker lists (vcs.listRefs pages past this aren't fetched). */
|
||||
const BRANCH_LIST_LIMIT = 100;
|
||||
|
||||
type ComposeTarget = { client: T3EnvironmentClient; project: T3ProjectShell; state: ComposeState };
|
||||
|
||||
function composeTarget(): ComposeTarget | null {
|
||||
if (screen.kind !== "compose" || !compose) return null;
|
||||
const client = clients.get(compose.envId);
|
||||
const project = client?.shell.projects.get(compose.projectId);
|
||||
return client && project ? { client, project, state: compose } : null;
|
||||
}
|
||||
|
||||
async function launchNewThread(envId: string, projectId: string, text: string): Promise<void> {
|
||||
const client = clients.get(envId);
|
||||
const project = client?.shell.projects.get(projectId);
|
||||
if (!client || !project) return;
|
||||
const selection = defaultModelSelection(client.settings, client.providers, project);
|
||||
if (!selection) {
|
||||
screenError = "No usable agent on that computer: sign in to Claude Code, Codex or another provider in T3 Code first.";
|
||||
/** Show the new-thread options for a project, keeping whatever `draft` picks still apply there. */
|
||||
function beginCompose(client: T3EnvironmentClient, project: T3ProjectShell, draft: LaunchDraft = emptyLaunchDraft()): void {
|
||||
const state: ComposeState = {
|
||||
envId: client.config.id,
|
||||
projectId: project.id,
|
||||
draft,
|
||||
refs: null,
|
||||
refsError: "",
|
||||
projectFile: undefined,
|
||||
loading: Promise.resolve(),
|
||||
};
|
||||
if (screen.kind === "compose") {
|
||||
// Another host or project: keep the selection on the row that changed it.
|
||||
compose = state;
|
||||
screenError = "";
|
||||
} else {
|
||||
compose = state;
|
||||
composeSelectionKey = null;
|
||||
setScreen({ kind: "compose" });
|
||||
}
|
||||
loadComposeContext(state);
|
||||
render();
|
||||
}
|
||||
|
||||
/** Read the project's branches and t3.json (defaults and the branch picker). */
|
||||
function loadComposeContext(state: ComposeState): void {
|
||||
const client = clients.get(state.envId);
|
||||
const project = client?.shell.projects.get(state.projectId);
|
||||
if (!client || !project?.workspaceRoot || isScratchProject(project, client.scratchWorkspaceRoot)) {
|
||||
state.projectFile = null;
|
||||
return;
|
||||
}
|
||||
state.refsError = "";
|
||||
const refs = fetchRefs(client, project, true).then(
|
||||
(refList) => {
|
||||
if (compose !== state) return;
|
||||
state.refs = refList;
|
||||
render();
|
||||
},
|
||||
(error) => {
|
||||
if (compose !== state) return;
|
||||
state.refsError = errorMessage(error);
|
||||
render();
|
||||
},
|
||||
);
|
||||
const projectFile =
|
||||
state.projectFile !== undefined
|
||||
? Promise.resolve()
|
||||
: readProjectFile(client, project).then((file) => {
|
||||
if (compose !== state) return;
|
||||
state.projectFile = file;
|
||||
render();
|
||||
});
|
||||
state.loading = Promise.all([refs, projectFile]).then(() => {});
|
||||
}
|
||||
|
||||
function fetchRefs(client: T3EnvironmentClient, project: T3ProjectShell, refresh: boolean): Promise<T3RefList> {
|
||||
// refresh: the cached list's "current" flag can lag a checkout made outside T3 by minutes.
|
||||
return client.call<T3RefList>("vcs.listRefs", { cwd: project.workspaceRoot, limit: BRANCH_LIST_LIMIT, ...(refresh ? { refresh: true } : {}) });
|
||||
}
|
||||
|
||||
/** The repo's t3.json defaults; null when it has none or it can't be read. */
|
||||
async function readProjectFile(client: T3EnvironmentClient, project: T3ProjectShell): Promise<T3ProjectFileDefaults | null> {
|
||||
try {
|
||||
const result = await client.call<{ contents?: unknown; truncated?: boolean }>("projects.readFile", {
|
||||
cwd: project.workspaceRoot,
|
||||
relativePath: T3_PROJECT_FILE_NAME,
|
||||
});
|
||||
return typeof result?.contents === "string" && !result.truncated ? parseProjectFile(result.contents) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function composeResolved({ client, project, state }: ComposeTarget): ResolvedLaunch {
|
||||
return resolveLaunch(state.draft, {
|
||||
settings: client.settings,
|
||||
providers: client.providers,
|
||||
project,
|
||||
scratchWorkspaceRoot: client.scratchWorkspaceRoot,
|
||||
projectFile: state.projectFile,
|
||||
refs: state.refs,
|
||||
});
|
||||
}
|
||||
|
||||
/** A new-thread option row; `action` opens its picker (or toggles it). */
|
||||
function field(key: string, label: string, value: string, action?: () => void, submenu = true): ListItem {
|
||||
return {
|
||||
key: `field:${key}`,
|
||||
kind: "field",
|
||||
label,
|
||||
right: value,
|
||||
submenu: Boolean(action) && submenu,
|
||||
onSelect: action
|
||||
? () => {
|
||||
if (!busyText) action();
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function composeItems(): ListItem[] {
|
||||
const target = composeTarget();
|
||||
if (!target) return [];
|
||||
const { client, project, state } = target;
|
||||
const resolved = composeResolved(target);
|
||||
const model = resolved.model;
|
||||
const items: ListItem[] = [
|
||||
{
|
||||
key: "start",
|
||||
kind: "action",
|
||||
label: "Say what to do…",
|
||||
right: "or type it on the phone",
|
||||
onSelect: () => {
|
||||
if (!busyText && window) post({ type: "start-voice-input", windowId: window.windowId });
|
||||
},
|
||||
},
|
||||
field("host", "Host", environmentName(client), readyClients().length > 1 ? () => openHostPicker(target) : undefined),
|
||||
field("project", "Project", project.title, () => openProjectPicker(target)),
|
||||
field(
|
||||
"model",
|
||||
"Model",
|
||||
model ? modelRowValue(client, model) : "None available",
|
||||
modelChoices(client.providers).length ? () => openModelPicker(target, resolved) : undefined,
|
||||
),
|
||||
];
|
||||
if (model) {
|
||||
for (const descriptor of modelOptionDescriptors(client.providers, model)) {
|
||||
const value = optionValue(descriptor, model);
|
||||
const shown = optionValueLabel(descriptor, value);
|
||||
if (descriptor.type === "boolean") {
|
||||
items.push(field(`option:${descriptor.id}`, descriptor.label, shown, () => setModelOption(target, resolved, descriptor.id, !value), false));
|
||||
} else if (selectableChoices(descriptor).length) {
|
||||
items.push(field(`option:${descriptor.id}`, descriptor.label, shown, () => openOptionPicker(target, resolved, descriptor)));
|
||||
}
|
||||
}
|
||||
}
|
||||
const accessChoices = runtimeModeChoices(client.providers.find((provider) => provider.instanceId === model?.instanceId));
|
||||
items.push(field("access", "Access", runtimeModeLabel(resolved.runtimeMode), accessChoices.length > 1 ? () => openAccessPicker(target, resolved) : undefined));
|
||||
if (!resolved.canChooseWorkspace) {
|
||||
const why = isScratchProject(project, client.scratchWorkspaceRoot) ? "Own folder (no project)" : "Project folder (not a git repo)";
|
||||
items.push(field("workspace", "Workspace", why));
|
||||
return items;
|
||||
}
|
||||
items.push(field("workspace", "Workspace", workspaceLabel(resolved), () => openWorkspacePicker(target, resolved)));
|
||||
const branchName = resolved.workspaceMode === "worktree" ? "Base" : "Branch";
|
||||
if (state.refsError) {
|
||||
items.push(field("branch", branchName, "Couldn't load; retry", () => {
|
||||
loadComposeContext(state);
|
||||
render();
|
||||
}, false));
|
||||
} else if (!state.refs && !resolved.branch) {
|
||||
items.push(field("branch", branchName, "Loading…"));
|
||||
} else {
|
||||
items.push(field("branch", branchName, branchLabel(resolved, Boolean(state.refs?.hasPrimaryRemote)), state.refs ? () => openBranchPicker(target, resolved) : undefined));
|
||||
}
|
||||
if (resolved.workspaceMode === "worktree" && state.refs?.hasPrimaryRemote) {
|
||||
items.push(field("origin", "From origin", resolved.startFromOrigin ? "On" : "Off", () => {
|
||||
state.draft = { ...state.draft, startFromOrigin: !resolved.startFromOrigin };
|
||||
}, false));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** "Claude Opus 5", or "Codex · GPT-5.4" when the model's name doesn't say whose it is. */
|
||||
function modelRowValue(client: T3EnvironmentClient, model: T3ModelSelection): string {
|
||||
const provider = providerDisplayName(client.providers, model.instanceId);
|
||||
const name = modelDisplayName(client.providers, model);
|
||||
return name.toLowerCase().startsWith(provider.toLowerCase()) ? name : `${provider} · ${name}`;
|
||||
}
|
||||
|
||||
/** A picker row, " *" marking the current value, with an optional dim tag at the right. */
|
||||
function choiceItem(label: string, onSelect: MenuItem["onSelect"], options: { current?: boolean; right?: string } = {}): MenuItem {
|
||||
const text = options.current ? `${label} *` : label;
|
||||
const right = options.right;
|
||||
return {
|
||||
label: text,
|
||||
onSelect,
|
||||
render: right
|
||||
? ({ image, x, y, width, selected, disabled }) => {
|
||||
const f = font();
|
||||
const rightWidth = f.measureText(right);
|
||||
image.drawText(f, x, y + LIST_ROW_TEXT_INSET, truncateText(f, text, width - rightWidth - 12), disabled ? 70 : selected ? 255 : 200);
|
||||
image.drawText(f, x + width - rightWidth - 2, y + LIST_ROW_TEXT_INSET, right, selected ? 200 : 120);
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/** Open a picker on its current row. */
|
||||
function openChoices(items: MenuItem[], title: string, currentIndex: number): void {
|
||||
openWindowMenu(items, title, currentIndex >= 0 ? currentIndex : undefined);
|
||||
}
|
||||
|
||||
function openHostPicker({ client, project }: ComposeTarget): void {
|
||||
const hosts = readyClients();
|
||||
const items = hosts.map((host) =>
|
||||
choiceItem(
|
||||
environmentName(host),
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
if (host === client) return;
|
||||
// The same repository there if it has it, else its most recently used project.
|
||||
const target = matchingProject(host.shell.projects.values(), project) ?? projectChoices().find((choice) => choice.client === host)?.project;
|
||||
if (!target) {
|
||||
showNotice(`${environmentName(host)} has no projects.`);
|
||||
return;
|
||||
}
|
||||
beginCompose(host, target);
|
||||
},
|
||||
{ current: host === client },
|
||||
),
|
||||
);
|
||||
openChoices(items, "Host", hosts.indexOf(client));
|
||||
}
|
||||
|
||||
function openProjectPicker({ client, project, state }: ComposeTarget): void {
|
||||
const projects = projectChoices()
|
||||
.filter((choice) => choice.client === client)
|
||||
.map((choice) => choice.project);
|
||||
const items = projects.map((candidate) =>
|
||||
choiceItem(
|
||||
candidate.title,
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
if (candidate.id === project.id) return;
|
||||
// The model and access picks carry over; workspace and branch belong to the old repo.
|
||||
beginCompose(client, candidate, { ...emptyLaunchDraft(), model: state.draft.model, runtimeMode: state.draft.runtimeMode });
|
||||
},
|
||||
{ current: candidate.id === project.id },
|
||||
),
|
||||
);
|
||||
openChoices(items, "Project", projects.findIndex((candidate) => candidate.id === project.id));
|
||||
}
|
||||
|
||||
function openModelPicker({ client, state }: ComposeTarget, resolved: ResolvedLaunch): void {
|
||||
const current = resolved.model;
|
||||
const isCurrent = (instanceId: string, model: string) => current?.instanceId === instanceId && current.model === model;
|
||||
const groups = modelChoices(client.providers);
|
||||
const modelItems = (group: (typeof groups)[number]) =>
|
||||
group.models.map((choice) =>
|
||||
choiceItem(
|
||||
choice.label,
|
||||
(ctx) => {
|
||||
ctx.stack.clearToBase();
|
||||
// A different model starts from its own option defaults.
|
||||
if (!isCurrent(choice.selection.instanceId, choice.selection.model)) state.draft = { ...state.draft, model: choice.selection };
|
||||
},
|
||||
{ current: isCurrent(choice.selection.instanceId, choice.selection.model), right: choice.isLegacy ? "legacy" : undefined },
|
||||
),
|
||||
);
|
||||
const currentModelIndex = (group: (typeof groups)[number]) => group.models.findIndex((choice) => isCurrent(choice.selection.instanceId, choice.selection.model));
|
||||
if (groups.length === 1) {
|
||||
openChoices(modelItems(groups[0]!), `${groups[0]!.label} model`, currentModelIndex(groups[0]!));
|
||||
return;
|
||||
}
|
||||
const items = groups.map((group) =>
|
||||
submenuItem(group.instanceId === current?.instanceId ? `${group.label} *` : group.label, (ctx) =>
|
||||
ctx.stack.push(new WindowMenuLayer(`${group.label} model`, modelItems(group)).selectItem(Math.max(0, currentModelIndex(group)))),
|
||||
),
|
||||
);
|
||||
openChoices(items, "Model", groups.findIndex((group) => group.instanceId === current?.instanceId));
|
||||
}
|
||||
|
||||
function setModelOption({ state }: ComposeTarget, resolved: ResolvedLaunch, id: string, value: string | boolean): void {
|
||||
if (!resolved.model) return;
|
||||
state.draft = { ...state.draft, model: withModelOption(resolved.model, id, value) };
|
||||
}
|
||||
|
||||
function openOptionPicker(target: ComposeTarget, resolved: ResolvedLaunch, descriptor: Extract<T3OptionDescriptor, { type: "select" }>): void {
|
||||
const value = optionValue(descriptor, resolved.model);
|
||||
const choices = selectableChoices(descriptor);
|
||||
const items = choices.map((choice) =>
|
||||
choiceItem(
|
||||
choice.label,
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
setModelOption(target, resolved, descriptor.id, choice.id);
|
||||
},
|
||||
{ current: choice.id === value },
|
||||
),
|
||||
);
|
||||
openChoices(items, descriptor.label, choices.findIndex((choice) => choice.id === value));
|
||||
}
|
||||
|
||||
function openAccessPicker({ client, state }: ComposeTarget, resolved: ResolvedLaunch): void {
|
||||
const choices = runtimeModeChoices(client.providers.find((provider) => provider.instanceId === resolved.model?.instanceId));
|
||||
const items = choices.map((choice) =>
|
||||
choiceItem(
|
||||
choice.label,
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
state.draft = { ...state.draft, runtimeMode: choice.mode };
|
||||
},
|
||||
{ current: choice.mode === resolved.runtimeMode },
|
||||
),
|
||||
);
|
||||
openChoices(items, "Access", choices.findIndex((choice) => choice.mode === resolved.runtimeMode));
|
||||
}
|
||||
|
||||
function openWorkspacePicker({ state }: ComposeTarget, resolved: ResolvedLaunch): void {
|
||||
const modes: Array<{ mode: WorkspaceMode; label: string }> = [
|
||||
{ mode: "local", label: "Current checkout" },
|
||||
{ mode: "worktree", label: "New worktree" },
|
||||
];
|
||||
const items = modes.map(({ mode, label }) =>
|
||||
choiceItem(
|
||||
label,
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
if (mode !== resolved.workspaceMode || state.draft.worktreePath) state.draft = pickWorkspaceMode(state.draft, mode);
|
||||
},
|
||||
{ current: mode === resolved.workspaceMode && !resolved.worktreePath },
|
||||
),
|
||||
);
|
||||
openChoices(items, "Workspace", modes.findIndex(({ mode }) => mode === resolved.workspaceMode));
|
||||
}
|
||||
|
||||
function openBranchPicker(target: ComposeTarget, resolved: ResolvedLaunch): void {
|
||||
const { project, state } = target;
|
||||
const mode = resolved.workspaceMode;
|
||||
// A thread in the checkout runs on a local branch; a worktree can start from a remote one too.
|
||||
const refs = (state.refs?.refs ?? []).filter((ref) => mode === "worktree" || !ref.isRemote);
|
||||
const items: MenuItem[] = refs.map((ref) =>
|
||||
choiceItem(
|
||||
ref.name,
|
||||
(ctx) => {
|
||||
ctx.stack.pop();
|
||||
void selectBranch(target, ref, mode);
|
||||
},
|
||||
{ current: ref.name === resolved.branch, right: needsCheckout(ref, mode) ? "check out" : refBadge(ref, project) || undefined },
|
||||
),
|
||||
);
|
||||
const hidden = (state.refs?.totalCount ?? 0) - (state.refs?.refs.length ?? 0);
|
||||
if (state.refs?.nextCursor != null && hidden > 0) items.push({ label: `(${hidden} more not listed)`, disabled: true, onSelect: () => {} });
|
||||
if (!items.length) items.push({ label: "No branches", disabled: true, onSelect: () => {} });
|
||||
openChoices(items, mode === "worktree" ? "New worktree from" : "Run on branch", refs.findIndex((ref) => ref.name === resolved.branch));
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a branch. In the project checkout, a branch that isn't checked out
|
||||
* anywhere is checked out first (vcs.switchRef), as T3's own app does;
|
||||
* one checked out in another worktree runs there.
|
||||
*/
|
||||
async function selectBranch({ client, project, state }: ComposeTarget, ref: T3VcsRef, mode: WorkspaceMode): Promise<void> {
|
||||
if (!needsCheckout(ref, mode)) {
|
||||
state.draft = pickBranch(state.draft, ref, mode, project);
|
||||
render();
|
||||
return;
|
||||
}
|
||||
busyText = `Checking out ${ref.name}…`;
|
||||
screenError = "";
|
||||
render();
|
||||
try {
|
||||
const result = await client.call<{ refName?: string | null }>("vcs.switchRef", { cwd: project.workspaceRoot, refName: ref.name });
|
||||
if (compose !== state) return;
|
||||
state.draft = pickBranch(state.draft, { ...ref, name: result?.refName || ref.name, current: true }, mode, project);
|
||||
busyText = "";
|
||||
showNotice(`Checked out ${result?.refName || ref.name}`);
|
||||
loadComposeContext(state);
|
||||
} catch (error) {
|
||||
if (compose !== state) return;
|
||||
busyText = "";
|
||||
screenError = `Couldn't check out ${ref.name}: ${errorMessage(error)}`;
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
async function launchNewThread(text: string): Promise<void> {
|
||||
const target = composeTarget();
|
||||
if (!target) return;
|
||||
if (busyText) {
|
||||
showNotice(`Not started: still ${busyText.replace(/…$/, "").toLowerCase()}.`);
|
||||
return;
|
||||
}
|
||||
const { client, project, state } = target;
|
||||
busyText = "Starting thread…";
|
||||
screenError = "";
|
||||
render();
|
||||
try {
|
||||
const threadId = await client.launchThread(launchThreadPayload(project, text, selection, client.settings));
|
||||
if (screen.kind === "compose") openThread(envId, threadId);
|
||||
// Defaults can depend on the branch list and t3.json.
|
||||
await state.loading;
|
||||
if (compose !== state) return;
|
||||
const resolved = composeResolved(target);
|
||||
if (resolved.blocker) throw new Error(resolved.blocker);
|
||||
const threadId = await client.launchThread(launchThreadPayload(project, text, resolved, client.providers));
|
||||
if (compose === state) openThread(client.config.id, threadId);
|
||||
render();
|
||||
} catch (error) {
|
||||
if (compose !== state) return;
|
||||
busyText = "";
|
||||
screenError = errorMessage(error);
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
/** The project's own defaults for a new thread (the assistant tool has no screen to pick on). */
|
||||
async function defaultLaunch(client: T3EnvironmentClient, project: T3ProjectShell): Promise<ResolvedLaunch> {
|
||||
const scratch = isScratchProject(project, client.scratchWorkspaceRoot);
|
||||
const usesRepo = Boolean(project.workspaceRoot) && !scratch;
|
||||
const [projectFile, refs] = await Promise.all([
|
||||
usesRepo ? readProjectFile(client, project) : Promise.resolve(null),
|
||||
usesRepo ? fetchRefs(client, project, false).catch(() => null) : Promise.resolve(null),
|
||||
]);
|
||||
return resolveLaunch(emptyLaunchDraft(), {
|
||||
settings: client.settings,
|
||||
providers: client.providers,
|
||||
project,
|
||||
scratchWorkspaceRoot: client.scratchWorkspaceRoot,
|
||||
projectFile,
|
||||
refs,
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// List items
|
||||
|
||||
@@ -1208,11 +1663,17 @@ function list(): Menu<ListItem> {
|
||||
|
||||
/** The selection index for the remembered key (selection follows its thread as the list reorders). */
|
||||
function selectionIndex(items: readonly ListItem[]): number | undefined {
|
||||
if (!listSelectionKey) return undefined;
|
||||
const index = items.findIndex((item) => item.key === listSelectionKey);
|
||||
const key = screen.kind === "compose" ? composeSelectionKey : listSelectionKey;
|
||||
if (!key) return undefined;
|
||||
const index = items.findIndex((item) => item.key === key);
|
||||
return index >= 0 ? index : undefined;
|
||||
}
|
||||
|
||||
function setSelectedKey(key: string | null): void {
|
||||
if (screen.kind === "compose") composeSelectionKey = key;
|
||||
else listSelectionKey = key;
|
||||
}
|
||||
|
||||
function threadItem(client: T3EnvironmentClient, thread: T3ThreadShell, multiEnv: boolean, nowMs: number): ListItem {
|
||||
const { marker, value } = threadMarker(threadStatus(thread), { settled: isThreadSettled(thread), unread: isThreadUnread(thread) });
|
||||
const project = client.shell.projects.get(thread.projectId)?.title ?? "";
|
||||
@@ -1365,6 +1826,15 @@ function drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArg
|
||||
image.drawText(f, x + 4, textY, truncateText(f, item.label, width - 8), selected ? 255 : 140);
|
||||
return;
|
||||
}
|
||||
if (item.kind === "field") {
|
||||
const submenuWidth = item.submenu ? 16 : 0;
|
||||
const valueX = x + 6 + composeValueColumn;
|
||||
image.drawText(f, x + 6, textY, truncateText(f, item.label, composeValueColumn - 8), selected ? 230 : 140);
|
||||
const valueValue = selected ? 255 : item.onSelect ? 210 : 150;
|
||||
image.drawText(f, valueX, textY, truncateText(f, item.right ?? "", Math.max(10, x + width - valueX - submenuWidth - 6)), valueValue);
|
||||
if (item.submenu) drawSubmenuIndicator(image, f, x, y, width, height, selected ? 255 : 200);
|
||||
return;
|
||||
}
|
||||
const value = selected ? 255 : 200;
|
||||
const markerWidth = item.kind === "thread" ? 18 : 0;
|
||||
if (item.marker) image.drawText(f, x + 6, textY, item.marker, selected ? 255 : (item.markerValue ?? value));
|
||||
@@ -1580,20 +2050,33 @@ function paintPair(win: AppWindow): GrayImage {
|
||||
function paintCompose(win: AppWindow): GrayImage {
|
||||
const target = composeTarget();
|
||||
if (!target) return paintMessageScreen(win, "New thread", [{ text: "That project is no longer available.", value: 170 }], "");
|
||||
const { client, project } = target;
|
||||
const selection = defaultModelSelection(client.settings, client.providers, project);
|
||||
const agent = selection ? `${providerDisplayName(client.providers, selection.instanceId)} · ${selection.model}` : "No usable agent";
|
||||
return paintMessageScreen(
|
||||
win,
|
||||
`New thread: ${project.title}`,
|
||||
[
|
||||
{ text: `Agent: ${agent}`, value: 150 },
|
||||
{ text: "Say or type what the agent should do.", value: 200 },
|
||||
{ text: busyText, value: 200 },
|
||||
{ text: screenError, value: 230 },
|
||||
],
|
||||
busyText ? "" : `${GESTURE_CLICK} speak ${GESTURE_DOUBLE_CLICK} cancel`,
|
||||
);
|
||||
const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0);
|
||||
const f = font();
|
||||
const step = lineStep(f);
|
||||
const title = "New thread";
|
||||
image.drawText(f, TITLE_X, TITLE_Y, title, 220);
|
||||
const status = busyText || notice?.text || "";
|
||||
if (status) {
|
||||
const statusX = TITLE_X + f.measureText(title) + 16;
|
||||
image.drawText(f, statusX, TITLE_Y, truncateText(f, status, win.viewportWidth - statusX - 14), 150);
|
||||
}
|
||||
// Below the list: an error, or why nothing can start here.
|
||||
const resolved = composeResolved(target);
|
||||
const problem = screenError || (resolved.model ? "" : (resolved.blocker ?? ""));
|
||||
const problemLines = problem ? wrapText(f, problem, win.viewportWidth - TITLE_X * 2).slice(0, 3) : [];
|
||||
const listTop = TITLE_Y + step + 8;
|
||||
const listWidth = win.viewportWidth - LIST_LEFT - LIST_RIGHT;
|
||||
const listHeight = win.viewportHeight - 6 - listTop - (problemLines.length ? problemLines.length * step + 6 : 0);
|
||||
const items = composeItems();
|
||||
const widest = Math.max(0, ...items.filter((item) => item.kind === "field").map((item) => f.measureText(item.label)));
|
||||
composeValueColumn = Math.min(widest + 16, Math.floor(listWidth * 0.4));
|
||||
const menu = list();
|
||||
menu.setItems(items, selectionIndex(items));
|
||||
composeSelectionKey = menu.selectedItem?.key ?? composeSelectionKey;
|
||||
menu.paint(image, { x: LIST_LEFT, y: listTop - 2, width: listWidth, height: listHeight }, win.focused);
|
||||
menu.drawScrollbar(image, win.viewportWidth - 10, listTop, listHeight - 4);
|
||||
problemLines.forEach((line, index) => image.drawText(f, TITLE_X, listTop + listHeight + 4 + index * step, line, 230));
|
||||
return image;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1735,10 +2218,16 @@ async function handleTool(name: string, args: any): Promise<ToolResult> {
|
||||
if (!choice) {
|
||||
return { ok: false, error: `No project matches "${args?.project}". Projects: ${projectChoices().map(({ project }) => project.title).join(", ")}` };
|
||||
}
|
||||
const selection = defaultModelSelection(choice.client.settings, choice.client.providers, choice.project);
|
||||
if (!selection) return { ok: false, error: "No usable agent provider on that computer." };
|
||||
const threadId = await choice.client.launchThread(launchThreadPayload(choice.project, text, selection, choice.client.settings));
|
||||
return { ok: true, content: `Started a thread in ${choice.project.title} (id ${threadId}).` };
|
||||
const { client, project } = choice;
|
||||
const resolved = await defaultLaunch(client, project);
|
||||
if (resolved.blocker) return { ok: false, error: resolved.blocker };
|
||||
const threadId = await client.launchThread(launchThreadPayload(project, text, resolved, client.providers));
|
||||
const how = [
|
||||
modelDisplayName(client.providers, resolved.model),
|
||||
runtimeModeLabel(resolved.runtimeMode).toLowerCase(),
|
||||
resolved.workspaceMode === "worktree" ? `new worktree from ${resolved.branch}` : resolved.branch ? `on ${resolved.branch}` : "",
|
||||
].filter(Boolean);
|
||||
return { ok: true, content: `Started a thread in ${project.title} (${how.join(", ")}; id ${threadId}).` };
|
||||
}
|
||||
default:
|
||||
return { ok: false, error: `Unknown T3 Code tool: ${name}` };
|
||||
|
||||
@@ -9,8 +9,8 @@ const teleprompterApp: AppDefinition = {
|
||||
ctx.launchInProcessApp(TELEPROMPTER_WINDOW_ID, TELEPROMPTER_SURFACE_ID, (options) =>
|
||||
createTeleprompterAppWindow({
|
||||
...options,
|
||||
startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture(),
|
||||
startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture("teleprompter"),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture("teleprompter"),
|
||||
appendLog: (message) => ctx.appendLog(message),
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -55,10 +55,9 @@ import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
enumSettingMenuItem,
|
||||
terminalAutoReconnectSetting,
|
||||
terminalDisplayModeSetting,
|
||||
terminalDisplayLayoutSetting,
|
||||
terminalLaunchPresetsSetting,
|
||||
terminalNewConnectionSetting,
|
||||
terminalVerticalPositionSetting,
|
||||
terminalWakeOnBellSetting,
|
||||
textSettingMenuItem,
|
||||
toggleSettingMenuItem,
|
||||
@@ -1118,8 +1117,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
|
||||
*/
|
||||
function settingsMenuItems(): MenuItem[] {
|
||||
return [
|
||||
enumSettingMenuItem(terminalDisplayModeSetting),
|
||||
enumSettingMenuItem(terminalVerticalPositionSetting),
|
||||
enumSettingMenuItem(terminalDisplayLayoutSetting),
|
||||
terminalFontPickerMenuItem(),
|
||||
// Opens the phone text editor; the settings listener repaints its page.
|
||||
textSettingMenuItem(terminalLaunchPresetsSetting),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { inProcessTrayWidget, type AppDefinition } from "../app-definition";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { registerTimerTools } from "../../assistant/timer-tools";
|
||||
import { timerEngine, timersWakeSetting } from "./timer-engine";
|
||||
import { createTimerAppWindow, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
|
||||
import { createTimerAppWindow, showTrayTimer, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
|
||||
|
||||
/**
|
||||
* Timers, stopwatch and alarms. The clock engine boots with the shell so
|
||||
@@ -33,7 +33,8 @@ const timerApp: AppDefinition = {
|
||||
.catch((error) => console.error(`timers ring launch failed: ${error}`));
|
||||
},
|
||||
});
|
||||
startTimersTrayIcon();
|
||||
// Selected in the switcher, the tray readout shows its timer in the app's window.
|
||||
startTimersTrayIcon(inProcessTrayWidget(ctx, TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow, showTrayTimer));
|
||||
registerTimerTools((appId) => ctx.launchApp(appId));
|
||||
},
|
||||
launch: (ctx) => ctx.launchInProcessApp(TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow),
|
||||
|
||||
@@ -39,7 +39,7 @@ import {
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { shell, type TrayWidget } from "../../ui/shell/shell";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
@@ -68,6 +68,7 @@ import {
|
||||
formatTimeOfDay,
|
||||
nearestLadderIndex,
|
||||
sortAlarms,
|
||||
soonestRunningTimer,
|
||||
sortTimers,
|
||||
stopwatchElapsedMs,
|
||||
stopwatchIsRunning,
|
||||
@@ -271,6 +272,19 @@ export class TimersLayer implements Layer {
|
||||
if (index !== null) this.selectionKey = rows[index]!.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a timer on the stage, out of any picker: the tray widget's view of
|
||||
* the timer it counts down. Lands the selection first, if this is before
|
||||
* the first paint, so that doesn't move it again.
|
||||
*/
|
||||
showTimer(timer: CountdownTimer): void {
|
||||
this.reconcileSelection(this.buildRows(Date.now()));
|
||||
this.selectionKey = timerKey(timer);
|
||||
this.mode = { kind: "list" };
|
||||
this.ringButton = 0;
|
||||
this.notice = "";
|
||||
}
|
||||
|
||||
/** Called on every engine change: jump to a newly ringing item. */
|
||||
onEngineChanged(): void {
|
||||
const ringing = timerEngine.ringingItems();
|
||||
@@ -1291,8 +1305,12 @@ function pad2(value: number): string {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Window
|
||||
|
||||
/** The open Timers window's layer, for the tray widget (see showTrayTimer). */
|
||||
let windowLayer: TimersLayer | null = null;
|
||||
|
||||
export function createTimerAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const layer = new TimersLayer();
|
||||
windowLayer = layer;
|
||||
let tick: ReturnType<typeof setInterval> | null = null;
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
const stopTick = () => {
|
||||
@@ -1324,6 +1342,7 @@ export function createTimerAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
stopTick();
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
if (windowLayer === layer) windowLayer = null;
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
@@ -1348,9 +1367,10 @@ let trayTick: ReturnType<typeof setInterval> | null = null;
|
||||
/**
|
||||
* Keep a small "soonest timer" readout in the top bar while a timer runs,
|
||||
* so any app can glance at it. Minute granularity until the last minute,
|
||||
* so the shell isn't repainted every second for an hour.
|
||||
* so the shell isn't repainted every second for an hour. `widget` makes it
|
||||
* selectable in the switcher (see showTrayTimer).
|
||||
*/
|
||||
export function startTimersTrayIcon(): void {
|
||||
export function startTimersTrayIcon(widget?: TrayWidget): void {
|
||||
const sync = () => {
|
||||
const remaining = soonestRunningTimerRemainingMs(timerEngine);
|
||||
let text: string | null = null;
|
||||
@@ -1359,7 +1379,7 @@ export function startTimersTrayIcon(): void {
|
||||
}
|
||||
if (text === trayText) return;
|
||||
trayText = text;
|
||||
shell.setTrayIcon("timer", text === null ? null : renderTrayIcon(text));
|
||||
shell.setTrayIcon("timer", text === null ? null : renderTrayIcon(text), widget);
|
||||
if (text !== null && trayTick === null) {
|
||||
trayTick = setInterval(sync, 1000);
|
||||
} else if (text === null && trayTick !== null) {
|
||||
@@ -1371,6 +1391,16 @@ export function startTimersTrayIcon(): void {
|
||||
sync();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tray widget's view: the open Timers window with the timer the tray
|
||||
* icon counts down on its stage. Called each time the switcher's selection
|
||||
* moves onto the widget, once the window is open.
|
||||
*/
|
||||
export function showTrayTimer(): void {
|
||||
const timer = soonestRunningTimer(timerEngine.state.timers, Date.now());
|
||||
if (timer) windowLayer?.showTimer(timer);
|
||||
}
|
||||
|
||||
function renderTrayIcon(text: string): GrayImage {
|
||||
const font = getFont("terminus12");
|
||||
const dial = 12;
|
||||
|
||||
@@ -44,6 +44,7 @@ import {
|
||||
parseTimersState,
|
||||
pushRecentDuration,
|
||||
serializeTimersState,
|
||||
soonestRunningTimer,
|
||||
stopwatchElapsedMs,
|
||||
timerRemainingMs,
|
||||
type Alarm,
|
||||
@@ -685,13 +686,8 @@ function alarmNotificationText(alarm: Alarm): string {
|
||||
|
||||
/** Remaining time of the timer that ends soonest, for the tray icon; null when none runs. */
|
||||
export function soonestRunningTimerRemainingMs(engine: TimerEngine, now = Date.now()): number | null {
|
||||
let soonest: number | null = null;
|
||||
for (const timer of engine.state.timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
|
||||
const remaining = timerRemainingMs(timer, now);
|
||||
if (soonest === null || remaining < soonest) soonest = remaining;
|
||||
}
|
||||
return soonest;
|
||||
const timer = soonestRunningTimer(engine.state.timers, now);
|
||||
return timer ? timerRemainingMs(timer, now) : null;
|
||||
}
|
||||
|
||||
export const timerEngine = new TimerEngine();
|
||||
@@ -185,6 +185,16 @@ export function sortTimers(timers: readonly CountdownTimer[], nowMs: number): Co
|
||||
});
|
||||
}
|
||||
|
||||
/** The running timer that ends soonest (the one the tray icon counts down); null when none runs. */
|
||||
export function soonestRunningTimer(timers: readonly CountdownTimer[], nowMs: number): CountdownTimer | null {
|
||||
let soonest: CountdownTimer | null = null;
|
||||
for (const timer of timers) {
|
||||
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
|
||||
if (!soonest || timerRemainingMs(timer, nowMs) < timerRemainingMs(soonest, nowMs)) soonest = timer;
|
||||
}
|
||||
return soonest;
|
||||
}
|
||||
|
||||
export function pushRecentDuration(recent: readonly number[], durationMs: number): number[] {
|
||||
return [durationMs, ...recent.filter((value) => value !== durationMs)].slice(0, RECENT_DURATION_COUNT);
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ const transcribeApp: AppDefinition = {
|
||||
ctx.launchInProcessApp(TRANSCRIBE_WINDOW_ID, TRANSCRIBE_SURFACE_ID, (options) =>
|
||||
createTranscribeAppWindow({
|
||||
...options,
|
||||
startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture(),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture(),
|
||||
startContinuousVoiceCapture: (capture) => void options.actions.startContinuousVoiceCapture("transcribe", capture),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture("transcribe"),
|
||||
}),
|
||||
),
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { imageFromAsciiArt } from "../../graphics/image";
|
||||
import { TranscribeLayer } from "./transcribe";
|
||||
import { TranscribeLayer, transcriptTimestamp } from "./transcribe";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
@@ -13,8 +13,8 @@ export const TRANSCRIBE_SURFACE_ID = "window:transcribe";
|
||||
|
||||
const TRAY_ICON_ID = "transcribe";
|
||||
|
||||
// 16x18 microphone glyph for the top-bar tray.
|
||||
const MIC_ICON = imageFromAsciiArt(
|
||||
// 16x18 microphone glyph for the top-bar tray (also Translate's).
|
||||
export const MIC_ICON = imageFromAsciiArt(
|
||||
[
|
||||
" #### ",
|
||||
" ###### ",
|
||||
@@ -37,7 +37,7 @@ const MIC_ICON = imageFromAsciiArt(
|
||||
|
||||
export type TranscribeAppOptions = InProcessAppOptions & {
|
||||
/** Begin continuous mic capture (kept on while the window is open). */
|
||||
startContinuousVoiceCapture: () => void;
|
||||
startContinuousVoiceCapture: (options?: { recordAs?: string }) => void;
|
||||
stopContinuousVoiceCapture: () => void;
|
||||
};
|
||||
|
||||
@@ -47,7 +47,9 @@ export type TranscribeAppOptions = InProcessAppOptions & {
|
||||
* shows a microphone tray icon in the top bar.
|
||||
*/
|
||||
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
|
||||
const layer = new TranscribeLayer();
|
||||
// One timestamp per session names both the audio recording and a saved transcript.
|
||||
const stamp = transcriptTimestamp();
|
||||
const layer = new TranscribeLayer(stamp);
|
||||
const app = createInProcessWindow({
|
||||
appId: "transcribe",
|
||||
windowId: TRANSCRIBE_WINDOW_ID,
|
||||
@@ -77,7 +79,7 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
||||
// Wire async state changes to this window's renderer, rather than the
|
||||
// controller action from which the window-specific actions were derived.
|
||||
layer.start(app.requestRender);
|
||||
options.startContinuousVoiceCapture();
|
||||
options.startContinuousVoiceCapture({ recordAs: `transcribe-${stamp}` });
|
||||
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
|
||||
return app;
|
||||
}
|
||||
@@ -23,6 +23,9 @@ export class TranscribeLayer implements Layer {
|
||||
private unsubscribePause: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
/** `stamp` names the saved transcript, matching the session's audio recording. */
|
||||
constructor(private readonly stamp: string = transcriptTimestamp()) {}
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||
this.transcript.accept(event);
|
||||
@@ -93,13 +96,13 @@ export class TranscribeLayer implements Layer {
|
||||
this.saveNotice = "Nothing to save yet.";
|
||||
return;
|
||||
}
|
||||
const filename = `transcript-${transcriptTimestamp()}.txt`;
|
||||
const filename = `transcript-${this.stamp}.txt`;
|
||||
const path = writeTextToDownloads(filename, `${text}\n`);
|
||||
this.saveNotice = path ? `Saved ${filename}` : "Save failed (check file access).";
|
||||
}
|
||||
}
|
||||
|
||||
function transcriptTimestamp(): string {
|
||||
export function transcriptTimestamp(): string {
|
||||
const now = new Date();
|
||||
const pad = (value: number) => String(value).padStart(2, "0");
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createTranslateAppWindow, TRANSLATE_SURFACE_ID, TRANSLATE_WINDOW_ID } from "./translate-app";
|
||||
|
||||
const translateApp: AppDefinition = {
|
||||
appId: "translate",
|
||||
title: "Translate",
|
||||
icon: "languages",
|
||||
launch: (ctx) =>
|
||||
ctx.launchInProcessApp(TRANSLATE_WINDOW_ID, TRANSLATE_SURFACE_ID, (options) =>
|
||||
createTranslateAppWindow({
|
||||
...options,
|
||||
startContinuousVoiceCapture: (capture) => void options.actions.startContinuousVoiceCapture("translate", capture),
|
||||
stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture("translate"),
|
||||
}),
|
||||
),
|
||||
};
|
||||
|
||||
export default translateApp;
|
||||
@@ -0,0 +1,99 @@
|
||||
import { TranslateLayer } from "./translate";
|
||||
import { resolveTranslationModel } from "./translate-llm";
|
||||
import { resolveTranslateProvider, translateProviderSetting, translateShowOriginalSetting } from "./translate-settings";
|
||||
import { MIC_ICON } from "../transcribe/transcribe-app";
|
||||
import { transcriptTimestamp } from "../transcribe/transcribe";
|
||||
import { type ContinuousCaptureOptions } from "../../ui/layers";
|
||||
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
||||
import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
export const TRANSLATE_WINDOW_ID = "translate";
|
||||
export const TRANSLATE_SURFACE_ID = "window:translate";
|
||||
|
||||
const TRAY_ICON_ID = "translate";
|
||||
|
||||
const NO_PROVIDER_PROBLEM =
|
||||
"Translate needs a transcription provider that understands any language: add an ElevenLabs, OpenAI, or Soniox API key (Settings > API Keys), or a Whisper server URL (Settings > Voice).";
|
||||
const NO_MODEL_PROBLEM = global.isIOS
|
||||
? "Translate needs a language model: add an Anthropic or OpenAI API key (Settings > API Keys)."
|
||||
: "Translate needs a language model: add an Anthropic or OpenAI API key (Settings > API Keys), or download the on-phone model (Settings > Assistant).";
|
||||
|
||||
export type TranslateAppOptions = InProcessAppOptions & {
|
||||
/** Begin (or retarget) continuous mic capture, kept on while the window is open. */
|
||||
startContinuousVoiceCapture: (options: ContinuousCaptureOptions) => void;
|
||||
stopContinuousVoiceCapture: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The Translate app: live speech in any language, translated into English by
|
||||
* the assistant's model, in its own window. Like Transcribe it holds
|
||||
* continuous mic capture while open, but always with a multilingual cloud
|
||||
* provider (Translate settings), which a shared capture switches to.
|
||||
*/
|
||||
export function createTranslateAppWindow(options: TranslateAppOptions): InProcessWindow {
|
||||
// Checks the setup and (re)starts capture with the chosen provider: at
|
||||
// launch, when the provider setting changes, and on a tap after a problem.
|
||||
const startCapture = () => {
|
||||
const provider = resolveTranslateProvider();
|
||||
const problem = !provider ? NO_PROVIDER_PROBLEM : !resolveTranslationModel() ? NO_MODEL_PROBLEM : null;
|
||||
layer.setProblem(problem);
|
||||
if (!provider || problem) {
|
||||
options.stopContinuousVoiceCapture();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
return;
|
||||
}
|
||||
options.startContinuousVoiceCapture({ multilingualProvider: provider });
|
||||
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
|
||||
};
|
||||
const layer = new TranslateLayer(transcriptTimestamp(), startCapture);
|
||||
const app = createInProcessWindow({
|
||||
appId: "translate",
|
||||
windowId: TRANSLATE_WINDOW_ID,
|
||||
title: "Translate",
|
||||
iconLetter: "Tl",
|
||||
icon: "languages",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
menuItems: () => [
|
||||
{
|
||||
label: "Save",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
layer.saveTranscript();
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Translate settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
openSettingsSubMenu(ctx, "Translate settings", [
|
||||
enumSettingMenuItem(translateProviderSetting, { onChange: startCapture }),
|
||||
toggleSettingMenuItem(translateShowOriginalSetting),
|
||||
]);
|
||||
},
|
||||
},
|
||||
],
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
layer.onRemoved();
|
||||
options.stopContinuousVoiceCapture();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, null);
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
// Wire async state changes to this window's renderer, rather than the
|
||||
// controller action from which the window-specific actions were derived.
|
||||
layer.start(app.requestRender);
|
||||
startCapture();
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Streamed translation against the assistant's configured model (Settings >
|
||||
* Assistant). Each call translates one stretch of speech into English; see
|
||||
* TranslationModel for how stretches are batched and given context.
|
||||
*/
|
||||
import { streamAnthropicMessage } from "../../native/anthropic";
|
||||
import { streamLocalQwen } from "../../native/llama";
|
||||
import { streamOpenAiResponse } from "../../native/openai";
|
||||
import { resolveAssistantModel, type ResolvedAssistantModel } from "../../assistant/models";
|
||||
import { type LlmStreamHandle, type LlmStreamOptions } from "../../assistant/llm-protocol";
|
||||
import { anthropicApiKeySetting, assistantModelSetting, openAiApiKeySetting } from "../../ui/dashboard-settings";
|
||||
import { TRANSLATE_SYSTEM_PROMPT, buildTranslateUserMessage } from "../../prompts";
|
||||
import { type TranslationTurn } from "./translation-model";
|
||||
|
||||
/**
|
||||
* The model Translate uses, or null when none is configured. Hermes is an
|
||||
* agent with its own tools and memory rather than a translator (every
|
||||
* utterance would land in its session), so it falls back to Auto: the model
|
||||
* for whichever API key is set, else the on-phone model.
|
||||
*/
|
||||
export function resolveTranslationModel(): ResolvedAssistantModel | null {
|
||||
const selection = assistantModelSetting.get();
|
||||
return resolveAssistantModel(selection === "hermes" ? "auto" : selection, {
|
||||
anthropic: anthropicApiKeySetting.get(),
|
||||
openai: openAiApiKeySetting.get(),
|
||||
});
|
||||
}
|
||||
|
||||
export type TranslationRequest = {
|
||||
speech: string;
|
||||
context: TranslationTurn[];
|
||||
onProgress: (textSoFar: string) => void;
|
||||
onDone: (translation: string) => void;
|
||||
onError: (message: string) => void;
|
||||
};
|
||||
|
||||
export function streamTranslation(model: ResolvedAssistantModel, request: TranslationRequest): LlmStreamHandle {
|
||||
const options: LlmStreamOptions = {
|
||||
apiKey: model.apiKey,
|
||||
model: model.model,
|
||||
system: TRANSLATE_SYSTEM_PROMPT,
|
||||
effort: model.effort,
|
||||
maxTokens: 1024,
|
||||
messages: [{ role: "user", content: buildTranslateUserMessage(request.speech, request.context) }],
|
||||
onTextDelta: (_delta, textSoFar) => request.onProgress(cleanTranslation(textSoFar)),
|
||||
onDone: (result) => {
|
||||
if (result.stopReason === "refusal") request.onError("The model declined to translate this.");
|
||||
else request.onDone(cleanTranslation(result.text));
|
||||
},
|
||||
onError: request.onError,
|
||||
};
|
||||
switch (model.provider) {
|
||||
case "openai":
|
||||
return streamOpenAiResponse(options);
|
||||
case "local":
|
||||
return streamLocalQwen(options);
|
||||
default:
|
||||
return streamAnthropicMessage(options);
|
||||
}
|
||||
}
|
||||
|
||||
/** The context turns are tagged <english>; a model may echo the tag around its reply. */
|
||||
function cleanTranslation(text: string): string {
|
||||
return text.replace(/^\s*<english>/i, "").replace(/<\/english>\s*$/i, "").trim();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { ConfigSettingBoolean, ConfigSettingEnum, voiceProviderSetting, type VoiceProvider } from "../../ui/dashboard-settings";
|
||||
|
||||
/**
|
||||
* The transcription providers that recognize any spoken language, in picker
|
||||
* order. The on-device engines can't: Moonshine and Whisper base.en are
|
||||
* English-only, and Apple's recognizer hears only the iPhone's speech language.
|
||||
*/
|
||||
const MULTILINGUAL_PROVIDERS = ["elevenlabs", "whisper", "soniox", "whisper-server"] as const satisfies readonly VoiceProvider[];
|
||||
|
||||
export type MultilingualProvider = (typeof MULTILINGUAL_PROVIDERS)[number];
|
||||
export type TranslateProvider = "auto" | MultilingualProvider;
|
||||
|
||||
function isMultilingual(provider: VoiceProvider): provider is MultilingualProvider {
|
||||
return (MULTILINGUAL_PROVIDERS as readonly VoiceProvider[]).includes(provider);
|
||||
}
|
||||
|
||||
/** Whether the provider has its API key (or, for Whisper server, its URL). */
|
||||
function isSetUp(provider: MultilingualProvider): boolean {
|
||||
return !voiceProviderSetting.isDisabled(provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider Translate transcribes with, or null when none is set up. Auto
|
||||
* uses the main Transcription Provider when it is multilingual, else the
|
||||
* first multilingual provider that is set up.
|
||||
*/
|
||||
export function resolveTranslateProvider(choice: TranslateProvider = translateProviderSetting.get()): MultilingualProvider | null {
|
||||
if (choice !== "auto") return isSetUp(choice) ? choice : null;
|
||||
const main = voiceProviderSetting.get();
|
||||
if (isMultilingual(main) && isSetUp(main)) return main;
|
||||
return MULTILINGUAL_PROVIDERS.find(isSetUp) ?? null;
|
||||
}
|
||||
|
||||
export const translateProviderSetting = new ConfigSettingEnum<TranslateProvider>({
|
||||
id: "translate-provider",
|
||||
label: "Transcription provider",
|
||||
storageKey: "translate.provider",
|
||||
defaultValue: "auto",
|
||||
values: ["auto", ...MULTILINGUAL_PROVIDERS],
|
||||
formatValue: (value) => {
|
||||
if (value !== "auto") return voiceProviderSetting.displayValue(value);
|
||||
const resolved = resolveTranslateProvider("auto");
|
||||
return `Auto (${resolved ? voiceProviderSetting.displayValue(resolved) : "none set up"})`;
|
||||
},
|
||||
isDisabled: (value) => value !== "auto" && !isSetUp(value),
|
||||
description:
|
||||
"Speech-to-text for Translate. Only cloud providers recognize any language (the on-device engines don't), so each needs its API key in Settings > API Keys, or a Whisper server URL in Settings > Voice. Auto uses the main Transcription Provider when it is one of these.",
|
||||
});
|
||||
|
||||
export const translateShowOriginalSetting = new ConfigSettingBoolean({
|
||||
id: "translate-show-original",
|
||||
label: "Show original",
|
||||
storageKey: "translate.showOriginal",
|
||||
defaultValue: false,
|
||||
description: "Show what was said, dimmed, above each translation.",
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { writeTextToDownloads } from "../../native/file-access";
|
||||
import { voiceControlBridge } from "../../native/voice-control";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type LlmStreamHandle } from "../../assistant/llm-protocol";
|
||||
import { TranscriptScroll, wrapTranscribeText } from "../transcribe/transcript-model";
|
||||
import { TranslationModel, type TranslationJob, type TranslationLine } from "./translation-model";
|
||||
import { resolveTranslationModel, streamTranslation } from "./translate-llm";
|
||||
import { translateShowOriginalSetting } from "./translate-settings";
|
||||
|
||||
const NO_MODEL_NOTICE = "No language model is set up.";
|
||||
|
||||
/**
|
||||
* Live translation view. Continuous mic capture is owned by the app wrapper
|
||||
* (createTranslateAppWindow); this layer feeds finalized speech to the
|
||||
* assistant's model one stretch at a time, shows the English as it streams
|
||||
* in, and saves the session to Downloads from the app menu.
|
||||
*/
|
||||
export class TranslateLayer implements Layer {
|
||||
private status = "Starting microphone...";
|
||||
private listening = false;
|
||||
private detail = "";
|
||||
private saveNotice = "";
|
||||
private translationError = "";
|
||||
// Why the app can't run (no multilingual provider or no model); shown in
|
||||
// place of the transcript, and a tap asks the app wrapper to check again.
|
||||
private problem: string | null = null;
|
||||
private readonly model = new TranslationModel();
|
||||
private readonly scroll = new TranscriptScroll();
|
||||
private activeJob: TranslationJob | null = null;
|
||||
private activeHandle: LlmStreamHandle | null = null;
|
||||
private requestRender: () => void = () => {};
|
||||
private removed = false;
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
/** `stamp` names the saved transcript; `retry` re-runs the app's setup check. */
|
||||
constructor(private readonly stamp: string, private readonly retry: () => void) {}
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.requestRender = requestRender;
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||
this.model.accept(event);
|
||||
this.pump();
|
||||
requestRender();
|
||||
});
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
this.listening = state.listening;
|
||||
this.detail = state.detail;
|
||||
this.saveNotice = "";
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
setProblem(problem: string | null): void {
|
||||
this.problem = problem;
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const measure = (line: string) => font.measureText(line);
|
||||
const maxWidth = width - 64;
|
||||
|
||||
image.drawText(font, 24, 20, "Translate", 200);
|
||||
const status = this.problem ? "Setup needed" : this.translationError || this.status;
|
||||
image.drawText(font, 24, 40, this.saveNotice || status, 110);
|
||||
|
||||
let lines: TranslationLine[];
|
||||
if (this.problem) {
|
||||
lines = wrapTranscribeText(measure, `${this.problem}\n\nTap to check again.`, maxWidth).map((text) => ({ text, gray: 230 }));
|
||||
} else {
|
||||
lines = this.model.lines(measure, maxWidth, translateShowOriginalSetting.get());
|
||||
if (!lines.length) {
|
||||
const placeholder = this.listening ? "Listening..." : this.detail;
|
||||
lines = wrapTranscribeText(measure, placeholder, maxWidth).map((text) => ({ text, gray: 230 }));
|
||||
}
|
||||
}
|
||||
|
||||
const step = lineStep(font) + 2;
|
||||
const bodyTop = 62;
|
||||
const bodyLines = Math.max(1, Math.floor((height - 6 - bodyTop - font.lineHeight) / step) + 1);
|
||||
const firstLine = this.scroll.layout(lines.length, bodyLines);
|
||||
for (let index = firstLine; index < Math.min(lines.length, firstLine + bodyLines); index++) {
|
||||
const y = bodyTop + (index - firstLine) * step;
|
||||
image.drawText(font, 32, y, lines[index]!.text, lines[index]!.gray);
|
||||
}
|
||||
|
||||
if (lines.length > bodyLines) {
|
||||
const trackHeight = height - bodyTop - 6;
|
||||
const thumbHeight = Math.max(8, Math.round(trackHeight * bodyLines / lines.length));
|
||||
const thumbY = bodyTop + Math.round((trackHeight - thumbHeight) * firstLine / (lines.length - bodyLines));
|
||||
image.fillRect(width - 14, bodyTop, 2, trackHeight, 45);
|
||||
image.fillRect(width - 14, thumbY, 2, thumbHeight, 140);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
this.scroll.scroll(event.type === "scroll-up" ? -3 : 3);
|
||||
ctx.actions.requestRender();
|
||||
return;
|
||||
}
|
||||
if (event.type === "click" && this.problem) {
|
||||
this.retry();
|
||||
return;
|
||||
}
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
this.activeJob = null;
|
||||
this.activeHandle?.cancel();
|
||||
this.activeHandle = null;
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
}
|
||||
|
||||
saveTranscript(): void {
|
||||
const text = this.model.savedText().trim();
|
||||
if (!text) {
|
||||
this.saveNotice = "Nothing to save yet.";
|
||||
return;
|
||||
}
|
||||
const filename = `translation-${this.stamp}.txt`;
|
||||
const path = writeTextToDownloads(filename, `${text}\n`);
|
||||
this.saveNotice = path ? `Saved ${filename}` : "Save failed (check file access).";
|
||||
}
|
||||
|
||||
/** Start translating the next waiting stretch of speech, unless one is in flight. */
|
||||
private pump(): void {
|
||||
if (this.removed) return;
|
||||
const job = this.model.nextJob();
|
||||
if (!job) return;
|
||||
const llm = resolveTranslationModel();
|
||||
if (!llm) {
|
||||
// The model went away mid-session (a key was cleared): keep the original.
|
||||
this.model.fail(job.segment);
|
||||
this.translationError = NO_MODEL_NOTICE;
|
||||
this.pump();
|
||||
return;
|
||||
}
|
||||
this.activeJob = job;
|
||||
const handle = streamTranslation(llm, {
|
||||
speech: job.segment.source,
|
||||
context: job.context,
|
||||
onProgress: (textSoFar) => {
|
||||
if (this.activeJob !== job) return;
|
||||
this.model.progress(job.segment, textSoFar);
|
||||
this.requestRender();
|
||||
},
|
||||
onDone: (translation) => this.settle(job, () => {
|
||||
this.model.complete(job.segment, translation);
|
||||
this.translationError = "";
|
||||
}),
|
||||
onError: (message) => this.settle(job, () => {
|
||||
this.model.fail(job.segment);
|
||||
this.translationError = `Translation failed: ${message}`;
|
||||
}),
|
||||
});
|
||||
// A provider that fails synchronously has already settled (and moved on).
|
||||
if (this.activeJob === job) this.activeHandle = handle;
|
||||
}
|
||||
|
||||
private settle(job: TranslationJob, apply: () => void): void {
|
||||
if (this.activeJob !== job) return;
|
||||
this.activeJob = null;
|
||||
this.activeHandle = null;
|
||||
apply();
|
||||
this.pump();
|
||||
this.requestRender();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { wrapTranscribeText } from "../transcribe/transcript-model";
|
||||
|
||||
type TranscriptEvent = { text: string; isFinal: boolean };
|
||||
|
||||
export type TranslationState = "pending" | "translating" | "done" | "failed";
|
||||
|
||||
/** One stretch of finalized speech and its English translation. */
|
||||
export type TranslationSegment = {
|
||||
source: string;
|
||||
/** The translation so far (streamed while translating). */
|
||||
translation: string;
|
||||
state: TranslationState;
|
||||
};
|
||||
|
||||
export type TranslationTurn = { source: string; translation: string };
|
||||
|
||||
/** A segment handed to the model, with the finished turns before it. */
|
||||
export type TranslationJob = {
|
||||
segment: TranslationSegment;
|
||||
/** Earlier translated turns, oldest first. */
|
||||
context: TranslationTurn[];
|
||||
};
|
||||
|
||||
export type TranslationLine = { text: string; gray: number };
|
||||
|
||||
// Earlier turns sent with each request, so pronouns, names, and topic carry
|
||||
// over between requests; bounded so a long session's requests stay small.
|
||||
const CONTEXT_TURNS = 4;
|
||||
const CONTEXT_CHARS = 800;
|
||||
|
||||
const TRANSLATION_GRAY = 230;
|
||||
const ORIGINAL_GRAY = 120;
|
||||
const LIVE_GRAY = 90;
|
||||
|
||||
/**
|
||||
* The Translate app's conversation: finalized speech segments with their
|
||||
* translations, plus the live partial of the utterance in progress.
|
||||
* Translation is serial (one request at a time, in order, so each can carry
|
||||
* the turns before it as context); finals that arrive while a request is in
|
||||
* flight merge into one pending segment, so a slow model batches the backlog
|
||||
* instead of falling further behind one request per pause.
|
||||
*/
|
||||
export class TranslationModel {
|
||||
readonly segments: TranslationSegment[] = [];
|
||||
/** The utterance in progress (replace semantics), not yet final. */
|
||||
live = "";
|
||||
|
||||
accept(event: TranscriptEvent): void {
|
||||
const text = event.text.trim();
|
||||
if (!event.isFinal) {
|
||||
this.live = text;
|
||||
return;
|
||||
}
|
||||
const finalText = text || this.live;
|
||||
this.live = "";
|
||||
if (!finalText) return;
|
||||
const last = this.segments[this.segments.length - 1];
|
||||
if (last?.state === "pending") last.source = `${last.source} ${finalText}`;
|
||||
else this.segments.push({ source: finalText, translation: "", state: "pending" });
|
||||
}
|
||||
|
||||
/** Take the next segment to translate, or null while one is in flight or none waits. */
|
||||
nextJob(): TranslationJob | null {
|
||||
if (this.segments.some((segment) => segment.state === "translating")) return null;
|
||||
const segment = this.segments.find((candidate) => candidate.state === "pending");
|
||||
if (!segment) return null;
|
||||
segment.state = "translating";
|
||||
return { segment, context: this.contextBefore(segment) };
|
||||
}
|
||||
|
||||
progress(segment: TranslationSegment, textSoFar: string): void {
|
||||
if (segment.state === "translating") segment.translation = textSoFar;
|
||||
}
|
||||
|
||||
complete(segment: TranslationSegment, translation: string): void {
|
||||
segment.translation = translation;
|
||||
segment.state = "done";
|
||||
}
|
||||
|
||||
fail(segment: TranslationSegment): void {
|
||||
segment.translation = "";
|
||||
segment.state = "failed";
|
||||
}
|
||||
|
||||
private contextBefore(segment: TranslationSegment): TranslationTurn[] {
|
||||
const turns: TranslationTurn[] = [];
|
||||
let chars = 0;
|
||||
for (let index = this.segments.indexOf(segment) - 1; index >= 0 && turns.length < CONTEXT_TURNS; index--) {
|
||||
const earlier = this.segments[index]!;
|
||||
if (earlier.state !== "done" || !earlier.translation) continue;
|
||||
chars += earlier.source.length + earlier.translation.length;
|
||||
if (turns.length && chars > CONTEXT_CHARS) break;
|
||||
turns.unshift({ source: earlier.source, translation: earlier.translation });
|
||||
}
|
||||
return turns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display lines: each segment's translation, bright; its original, dim,
|
||||
* above it when `showOriginal` is on and in its place until a translation
|
||||
* arrives (or when it failed); then the live partial, dimmer.
|
||||
*/
|
||||
lines(measure: (text: string) => number, maxWidth: number, showOriginal: boolean): TranslationLine[] {
|
||||
const lines: TranslationLine[] = [];
|
||||
const push = (text: string, gray: number) => {
|
||||
for (const line of wrapTranscribeText(measure, text, maxWidth)) lines.push({ text: line, gray });
|
||||
};
|
||||
for (const segment of this.segments) {
|
||||
const translation = segment.translation.trim();
|
||||
// A finished, empty translation is noise the model chose to drop.
|
||||
if (showOriginal || (!translation && segment.state !== "done")) push(segment.source, ORIGINAL_GRAY);
|
||||
if (translation) push(translation, TRANSLATION_GRAY);
|
||||
}
|
||||
if (this.live) push(this.live, LIVE_GRAY);
|
||||
return lines;
|
||||
}
|
||||
|
||||
/** Plain-text transcript for saving: each original, then its translation. */
|
||||
savedText(): string {
|
||||
return this.segments
|
||||
.map((segment) => {
|
||||
const translation = segment.translation.trim();
|
||||
if (segment.state === "failed") return `${segment.source}\n→ (not translated)`;
|
||||
if (!translation || translation === segment.source.trim()) return segment.source;
|
||||
return `${segment.source}\n→ ${translation}`;
|
||||
})
|
||||
.join("\n\n");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { inProcessTrayWidget, type AppDefinition } from "../app-definition";
|
||||
import { createWeatherAppWindow, WEATHER_SURFACE_ID, WEATHER_WINDOW_ID } from "./weather-app";
|
||||
import { startWeatherIndicators } from "./weather-indicators";
|
||||
|
||||
@@ -6,7 +6,10 @@ const weatherApp: AppDefinition = {
|
||||
appId: "weather",
|
||||
title: "Weather",
|
||||
icon: "cloud-sun",
|
||||
boot: () => startWeatherIndicators(),
|
||||
// Selected in the switcher, the status-bar indicator shows the app's window.
|
||||
boot: (ctx) => startWeatherIndicators(
|
||||
inProcessTrayWidget(ctx, WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow),
|
||||
),
|
||||
launch: (ctx) => ctx.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { temperatureUnitSetting } from "../../ui/dashboard-settings";
|
||||
|
||||
/**
|
||||
* A temperature (weather data is kept in Fahrenheit) in the unit chosen in
|
||||
* Settings > Customization > Temperature unit, rounded: "72°F" / "22°C", or
|
||||
* "72°" without the unit letter.
|
||||
*/
|
||||
export function formatTemperature(fahrenheit: number, withUnit = true): string {
|
||||
const celsius = temperatureUnitSetting.get() === "celsius";
|
||||
const value = Math.round(celsius ? (fahrenheit - 32) * 5 / 9 : fahrenheit);
|
||||
return withUnit ? `${value}°${celsius ? "C" : "F"}` : `${value}°`;
|
||||
}
|
||||
|
||||
/** The widest temperature the forecast's column is sized for. */
|
||||
export function widestTemperatureSample(): string {
|
||||
return temperatureUnitSetting.get() === "celsius" ? "-20°C" : "100°F";
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
|
||||
import { weatherBridge } from "../../native/weather";
|
||||
import { toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
||||
import { WeatherLayer } from "./weather";
|
||||
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
|
||||
@@ -14,11 +14,12 @@ import {
|
||||
export const WEATHER_WINDOW_ID = "weather";
|
||||
export const WEATHER_SURFACE_ID = "window:weather";
|
||||
|
||||
/** Local current conditions and forecast from the National Weather Service. */
|
||||
/** Local current conditions and forecast from the National Weather Service, or Open-Meteo outside the US. */
|
||||
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let closed = false;
|
||||
let requestingPermission = false;
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
let unsubscribeSettings: (() => void) | null = null;
|
||||
|
||||
const requestUpdate = () => {
|
||||
if (closed || requestingPermission) return;
|
||||
@@ -81,11 +82,15 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
|
||||
closed = true;
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
unsubscribeSettings?.();
|
||||
unsubscribeSettings = null;
|
||||
weatherBridge.stop();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
|
||||
// Repaint for a temperature-unit change made in the Settings app.
|
||||
unsubscribeSettings = onAnySettingChanged(() => app.requestRender());
|
||||
requestUpdate();
|
||||
return app;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { renderSvgIcon } from "../../graphics/icons";
|
||||
import { type GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { type WeatherCondition, type WeatherState } from "../../native/weather";
|
||||
import { formatTemperature } from "./temperature-format";
|
||||
|
||||
// Lucide weather icons (ISC licensed), kept verbatim so they can be diffed
|
||||
// against upstream.
|
||||
@@ -90,7 +91,7 @@ export function weatherReading(state: WeatherState, nowMs: number): WeatherReadi
|
||||
return {
|
||||
condition: current.condition,
|
||||
isDaytime: current.isDaytime,
|
||||
temperature: current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°`,
|
||||
temperature: current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF, false),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { weatherBridge } from "../../native/weather";
|
||||
import { onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { shell, type TrayWidget } from "../../ui/shell/shell";
|
||||
import { isSystemCardWidgetInUse } from "../glanceboard/configuration";
|
||||
import { drawWeatherReading, measureWeatherReading, weatherReading, type WeatherReading } from "./weather-icons";
|
||||
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
|
||||
|
||||
@@ -11,31 +12,35 @@ const TRAY_HEIGHT = 24;
|
||||
const TRAY_ICON_SIZE = 20;
|
||||
|
||||
let started = false;
|
||||
let trayWidget: TrayWidget | undefined;
|
||||
/** What the tray currently shows, to skip rebuilding an unchanged icon. */
|
||||
let trayShown: { key: string; font: UiFont } | null = null;
|
||||
|
||||
/**
|
||||
* Shell-lifetime subscription: keeps the hourly background refresh running
|
||||
* while the status-bar or system-card indicator is enabled, and the tray
|
||||
* icon in sync. The system card reads the bridge itself while it shows.
|
||||
* while the status-bar indicator is enabled, or the system-card one is and
|
||||
* the system card is on an enabled Glanceboard, and the tray icon in sync.
|
||||
* The system card reads the bridge itself while it shows. `widget` makes
|
||||
* the tray icon selectable in the switcher.
|
||||
*/
|
||||
export function startWeatherIndicators(): void {
|
||||
export function startWeatherIndicators(widget?: TrayWidget): void {
|
||||
if (started) return;
|
||||
started = true;
|
||||
trayWidget = widget;
|
||||
weatherBridge.onStateChange(syncWeatherIndicators);
|
||||
onAnySettingChanged(syncWeatherIndicators);
|
||||
}
|
||||
|
||||
function syncWeatherIndicators(): void {
|
||||
const inStatusBar = weatherShowInStatusBarSetting.get();
|
||||
weatherBridge.setBackgroundRefresh(inStatusBar || weatherShowOnSystemCardSetting.get());
|
||||
weatherBridge.setBackgroundRefresh(inStatusBar || (weatherShowOnSystemCardSetting.get() && isSystemCardWidgetInUse()));
|
||||
|
||||
const reading = inStatusBar ? weatherReading(weatherBridge.snapshot(), Date.now()) : null;
|
||||
const font = getDefaultSmallFont();
|
||||
const key = reading ? `${reading.condition}|${reading.isDaytime}|${reading.temperature}` : null;
|
||||
if (key === (trayShown?.key ?? null) && (!trayShown || trayShown.font === font)) return;
|
||||
trayShown = key === null ? null : { key, font };
|
||||
shell.setTrayIcon(TRAY_ICON_ID, reading ? buildWeatherTrayIcon(reading, font) : null);
|
||||
shell.setTrayIcon(TRAY_ICON_ID, reading ? buildWeatherTrayIcon(reading, font) : null, trayWidget);
|
||||
}
|
||||
|
||||
/** Condition icon and temperature, baked to a raster so the tray keeps one stable image. */
|
||||
|
||||
@@ -14,11 +14,15 @@ export const weatherShowInStatusBarSetting = new ConfigSettingBoolean({
|
||||
"Show the current conditions and temperature in the top bar. Weather for your current location is fetched once an hour while the glasses are connected.",
|
||||
});
|
||||
|
||||
/**
|
||||
* Also the Weather toggle in the system card's Glanceboard settings. On by
|
||||
* default: it only fetches while the system card is on an enabled board.
|
||||
*/
|
||||
export const weatherShowOnSystemCardSetting = new ConfigSettingBoolean({
|
||||
id: "weather-show-on-system-card",
|
||||
label: "Show on system card",
|
||||
storageKey: "weather.showOnSystemCard",
|
||||
defaultValue: false,
|
||||
defaultValue: true,
|
||||
description:
|
||||
"Show the current conditions and temperature on the Glanceboard's system card. Weather for your current location is fetched once an hour while the glasses are connected.",
|
||||
"Show the current conditions and temperature on the Glanceboard's system card. Weather for your current location is fetched once an hour while the glasses are connected and the system card is on the Glanceboard.",
|
||||
});
|
||||
@@ -1,12 +1,13 @@
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { wrapText, truncateText } from "../../graphics/textwrap";
|
||||
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
|
||||
import { WEATHER_SOURCE_NAMES, type ForecastPeriod, type WeatherState } from "../../native/weather";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { renderWeatherIcon } from "./weather-icons";
|
||||
import { formatTemperature, widestTemperatureSample } from "./temperature-format";
|
||||
|
||||
const PAGE_X = 18;
|
||||
const HEADER_Y = 8;
|
||||
@@ -115,7 +116,7 @@ export class WeatherLayer implements Layer {
|
||||
const large = getDefaultLargeFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
|
||||
const temperature = current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF);
|
||||
const temperatureY = CURRENT_TOP + 8;
|
||||
let temperatureX = PAGE_X;
|
||||
if (current.condition) {
|
||||
@@ -145,7 +146,9 @@ export class WeatherLayer implements Layer {
|
||||
}
|
||||
image.drawText(small, textX, cy, details.join(" ") || "Current forecast", 170);
|
||||
cy += lineStep(small);
|
||||
const source = current.observed ? "Observed" : "Forecast";
|
||||
// Naming the source also credits Open-Meteo, as its CC BY licence asks.
|
||||
const sourceName = weather.source ? `${WEATHER_SOURCE_NAMES[weather.source]} ` : "";
|
||||
const source = `${sourceName}${current.observed ? "observed" : "forecast"}`;
|
||||
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
|
||||
const updating = weather.phase === "locating" || weather.phase === "loading" ? " · updating" : "";
|
||||
image.drawText(small, textX, cy, `${source}${age ? ` ${age}` : ""}${updating}`, 105);
|
||||
@@ -180,7 +183,7 @@ export class WeatherLayer implements Layer {
|
||||
...forecast.map((period) => font.measureText(period.name)),
|
||||
);
|
||||
const tempX = PAGE_X + nameWidth + FORECAST_COL_GAP;
|
||||
const precipX = tempX + Math.max(font.measureText("Temp"), font.measureText("100°F")) + FORECAST_COL_GAP;
|
||||
const precipX = tempX + Math.max(font.measureText("Temp"), font.measureText(widestTemperatureSample())) + FORECAST_COL_GAP;
|
||||
const summaryX = precipX + Math.max(font.measureText("Rain"), font.measureText("100%")) + FORECAST_COL_GAP;
|
||||
|
||||
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
|
||||
@@ -207,7 +210,7 @@ export class WeatherLayer implements Layer {
|
||||
const shade = selected ? 245 : 190;
|
||||
const textY = centeredTextY(font, y, height);
|
||||
image.drawText(font, x + columns.name, textY, period.name, shade);
|
||||
image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
|
||||
image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : formatTemperature(period.temperatureF), shade);
|
||||
image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
|
||||
// The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left.
|
||||
const summaryWidth = width - columns.summary - columns.name;
|
||||
|
||||
Reference in new issue
Block a user