Refactor: Organize files by app

This commit is contained in:
James Babcock committed 2026-08-03 00:08:52 -07:00
1 parent 1ec6b3ff83
commit 9b3832d7a7
40 files changed
+175 -175

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GESTURE_DOUBLE_CLICK } from "../gestures";
import { drawSelectionHighlight } from "../menu";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "./geometry";
const DIALOG_X = 40;
const DIALOG_W = G2_LENS_WIDTH - 80;
// The dialog fits inside the min-height window band (like the other shell
// overlays), wherever the vertical position setting puts it.
const DIALOG_MARGIN_Y = 24;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
const MENU_ROW_H = 20;
const MENU_ROWS = 2;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
return minWindowTop() + DIALOG_MARGIN_Y;
}
/** thinking: turn running; done/error: finished, showing the Follow-up/Done menu. */
type AssistantPhase = "thinking" | "done" | "error";
export type AssistantLayerCallbacks = {
/** Record another utterance and continue this conversation. */
onFollowUp: () => void;
/** Abort the in-flight turn (double-click while thinking). */
onCancel: () => void;
/** The user closed the overlay (Done). */
onClose: () => void;
/** The layer left the stack by any path (Done, or the screen sleeping). */
onRemoved?: () => void;
};
/**
* The assistant overlay: streamed reply text with a status line for tool
* activity, and a Follow-up / Done menu once the turn ends. Double-click
* cancels an in-flight turn. The shell owns the AssistantSession and drives
* this layer's state through the on* methods as the turn streams.
*/
export class AssistantLayer implements Layer {
private phase: AssistantPhase = "thinking";
private replyText = "";
private status = "Thinking...";
private menuIndex = 0;
constructor(
private readonly actions: LayerActions,
private readonly callbacks: AssistantLayerCallbacks,
) {}
/** Shell: a new turn (initial utterance or follow-up) is starting. */
startTurn(): void {
this.phase = "thinking";
this.replyText = "";
this.status = "Thinking...";
this.menuIndex = 0;
this.actions.requestRender();
}
onTextDelta(_delta: string, textSoFar: string): void {
this.replyText = textSoFar;
if (this.phase === "thinking") this.status = "Thinking...";
this.actions.requestRender();
}
onToolActivity(label: string): void {
if (this.phase === "thinking") this.status = `→ ${label}`;
this.actions.requestRender();
}
onTurnDone(): void {
this.phase = "done";
this.status = this.replyText.trim() ? "" : "(no reply)";
this.menuIndex = 0;
this.actions.requestRender();
}
onError(message: string): void {
this.phase = "error";
this.status = message;
this.menuIndex = 0;
this.actions.requestRender();
}
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const font = getDefaultSmallFont();
const image = paintBelow();
const top = dialogY();
// Solid dialog box (fill 1, matching the voice dialog: opaque after 4bpp
// quantization, but not the color-key transparent 0).
image.fillRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1, 10);
image.drawRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90, 10);
const left = DIALOG_X + 16;
image.drawText(font, left, top + 12, this.phase === "thinking" ? "Assistant ●" : "Assistant", 220);
if (this.status) {
const statusValue = this.phase === "error" ? 200 : 130;
image.drawText(font, left, top + 30, truncateText(font, this.status, TEXT_MAX_WIDTH), statusValue);
}
const inMenu = this.phase !== "thinking";
const textBottom = inMenu ? top + DIALOG_H - MENU_ROWS * MENU_ROW_H - 8 : top + DIALOG_H - 8;
const textTop = top + 56;
const maxLines = Math.max(1, ((textBottom - textTop) / 16) | 0);
const wrapped = wrapText(font, this.replyText, TEXT_MAX_WIDTH);
// Keep the tail visible as text streams past the bottom.
const firstLine = Math.max(0, wrapped.length - maxLines);
for (let index = firstLine; index < wrapped.length; index++) {
image.drawText(font, left, textTop + (index - firstLine) * 16, wrapped[index]!, 235);
}
if (inMenu) {
const rows = ["Follow-up", "Done"];
const menuTop = top + DIALOG_H - MENU_ROWS * MENU_ROW_H - 2;
for (let i = 0; i < rows.length; i++) {
const rowY = menuTop + i * MENU_ROW_H;
const selected = i === this.menuIndex;
if (selected) {
drawSelectionHighlight(image, left - 4, rowY - 2, DIALOG_W - 24, MENU_ROW_H - 2, true, 6);
}
image.drawText(font, left + 4, rowY + 2, rows[i]!, selected ? 255 : 200);
}
} else {
image.drawText(font, left, top + DIALOG_H - 14, `${GESTURE_DOUBLE_CLICK} cancel`, 110);
}
return image;
}
handleInput(event: DashboardInputEvent, _ctx: LayerContext): void {
if (this.phase === "thinking") {
if (event.type === "double-click") {
this.callbacks.onCancel();
this.phase = "done";
this.status = "Cancelled";
this.menuIndex = 0;
this.actions.requestRender();
}
return;
}
// done / error: the Follow-up / Done menu.
switch (event.type) {
case "scroll-up":
this.menuIndex = (this.menuIndex + MENU_ROWS - 1) % MENU_ROWS;
this.actions.requestRender();
return;
case "scroll-down":
this.menuIndex = (this.menuIndex + 1) % MENU_ROWS;
this.actions.requestRender();
return;
case "click":
if (this.menuIndex === 0) {
this.callbacks.onFollowUp();
} else {
this.callbacks.onClose();
}
return;
case "double-click":
this.callbacks.onClose();
return;
default:
return;
}
}
onRemoved(): void {
this.callbacks.onRemoved?.();
}
}
-54
View File
@@ -1,54 +0,0 @@
import { ensureCalendarPermission, hasCalendarPermission } from "../../g2/android-permissions";
import { invalidateCalendarCache } from "../../native/calendar";
import { CalendarLayer } from "../apps/calendar";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const CALENDAR_WINDOW_ID = "calendar";
export const CALENDAR_SURFACE_ID = "window:calendar";
/**
* The Calendar app: a single screen listing upcoming events from the Android
* Calendar provider. If calendar permission is missing it shows a prompt and
* fires the system permission dialog (on launch and on any tap); once granted
* it re-renders with the event list.
*/
export function createCalendarAppWindow(options: InProcessAppOptions): InProcessWindow {
let requesting = false;
let app: InProcessWindow;
const requestPermission = () => {
if (requesting || hasCalendarPermission()) return;
requesting = true;
void ensureCalendarPermission().then((granted) => {
requesting = false;
if (granted) {
invalidateCalendarCache();
app.requestRender();
}
});
};
app = createInProcessWindow({
appId: "calendar",
windowId: CALENDAR_WINDOW_ID,
title: "Calendar",
iconLetter: "Ca",
icon: "calendar",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new CalendarLayer(requestPermission)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
// Prompt immediately on launch so the user doesn't have to discover the tap.
requestPermission();
return app;
}
-62
View File
@@ -1,62 +0,0 @@
import { MenuLayer } from "../menu";
import { ScreenTestLayer } from "../apps/screen-test";
import { BuzzerDemoLayer } from "../apps/buzzer-demo";
import { AccelerometerDemoLayer } from "../apps/accelerometer-demo";
import { appViewportSize } from "./geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const DEBUG_TESTS_WINDOW_ID = "debug-tests";
export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests";
/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */
export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow {
const menu = new MenuLayer(
"Debug tests",
[
{
label: "Screen test",
onSelect: (ctx) => {
ctx.stack.push(new ScreenTestLayer());
},
},
{
label: "Buzzer demo",
onSelect: (ctx) => {
ctx.stack.push(new BuzzerDemoLayer());
},
},
{
label: "Accelerometer demo",
onSelect: (ctx) => {
ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
},
},
],
{
x: 8,
y: 8,
width: 272,
minHeight: 0,
maxHeight: appViewportSize("min").height - 16,
},
);
return createInProcessWindow({
appId: "debug-tests",
windowId: DEBUG_TESTS_WINDOW_ID,
title: "Debug tests",
iconLetter: "Db",
icon: "flask-conical",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(menu),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
-153
View File
@@ -1,153 +0,0 @@
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
import { isDecodableImageFile } from "../../native/image-files";
import { FileBrowserLayer } from "../apps/file-browser";
import { FileInfoDialogLayer, type FileInfoAction } from "../apps/file-info-dialog";
import { ImageViewerLayer } from "../apps/image-viewer";
import { TextViewerLayer } from "../apps/text-viewer";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
import { shell } from "./shell";
export const FILES_WINDOW_ID = "files";
export const FILES_SURFACE_ID = "window:files";
const TEXT_FILE = /\.(txt|md|log)$/i;
export type FilesAppOptions = InProcessAppOptions & {
/** Open a text document as its own shell window (also used by the share intent). */
openDocumentWindow: (title: string, text: string) => void;
/** Open an image file as its own shell window. */
openImageWindow: (title: string, path: string) => void;
};
/**
* The Files app's launcher-opened window: a file browser over Places
* (bookmarks and storage roots). Picking any file opens an info dialog with
* metadata plus, for viewable types (text, images), the open actions.
*/
export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow {
let created: InProcessWindow | null = null;
const browser = new FileBrowserLayer({
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name),
// The browser handles double-click itself (up a level), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
onFilePicked: (entry, ctx) => {
ctx.stack.push(new FileInfoDialogLayer(entry, fileOpenActions(entry, options)));
},
});
created = createInProcessWindow({
appId: "files",
windowId: FILES_WINDOW_ID,
title: "Files",
iconLetter: "F",
icon: "folder",
closeable: true,
// Browser actions (view switch, bookmarks) only apply while the browser
// itself is on top; a pushed viewer or dialog gets just the defaults.
menuItems: () => (created && !created.stack.isAtBase() ? [] : browser.buildMenuItems()),
actions: options.actions,
baseLayer: browser,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
return created;
}
/** One opened text document as its own window (from the browser or a share intent). */
export function createTextDocumentWindow(
windowId: string,
title: string,
text: string,
options: InProcessAppOptions,
): InProcessWindow {
return createInProcessWindow({
appId: "files",
windowId,
title,
iconLetter: "F",
icon: "file-text",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new TextViewerLayer(text, title)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
/** One opened image file as its own window. */
export function createImageDocumentWindow(
windowId: string,
title: string,
path: string,
options: InProcessAppOptions,
): InProcessWindow {
return createInProcessWindow({
appId: "files",
windowId,
title,
iconLetter: "F",
icon: "image",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new ImageViewerLayer(path, title)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
/**
* The open actions for the picked-file dialog: View here / Open in new
* window for viewable types, empty for everything else (metadata only).
*/
function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileInfoAction[] {
if (TEXT_FILE.test(entry.name)) {
return [
{
label: "View here",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
ctx.stack.push(new TextViewerLayer(text ?? "(could not read file)", entry.name));
},
},
{
label: "Open in new window",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
options.openDocumentWindow(entry.name, text ?? "(could not read file)");
},
},
];
}
if (isDecodableImageFile(entry.name)) {
return [
{
label: "View here",
onSelect: (ctx) => {
ctx.stack.pop();
ctx.stack.push(new ImageViewerLayer(entry.path, entry.name));
},
},
{
label: "Open in new window",
onSelect: (ctx) => {
ctx.stack.pop();
options.openImageWindow(entry.name, entry.path);
},
},
];
}
return [];
}
-300
View File
@@ -1,300 +0,0 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
} from "../gestures";
import { DashboardInputEvent, Layer, LayerActions, LayerContext } from "../layers";
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu";
import { onAnySettingChanged } from "../dashboard-settings";
import { createInProcessWindow } from "./in-process-window";
import { getFolderAssignments, getFolders, getFolderStateFingerprint } from "./launcher-folders";
import { shell, type ShellWindow } from "./shell";
export type LauncherAppEntry = {
appId: string;
label: string;
icon: IconName;
};
export type LauncherOptions = {
actions: LayerActions;
apps: LauncherAppEntry[];
launchApp: (appId: string) => Promise<void> | void;
/** Submit a painted viewport-sized frame to this window's surface. */
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
/** Flip the launcher's compositor surface visibility on foreground changes. */
setSurfaceVisible: (visible: boolean) => void;
};
export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
const COLS = 5;
// Row height is sized so 3.5 rows fit the viewport: three full rows are
// always visible, and a fourth (half-clipped) row peeks in to signal that
// there are more apps to scroll to.
const VISIBLE_ROWS = 3.5;
const FULL_ROWS = Math.floor(VISIBLE_ROWS);
const GRID_TOP = 6;
const FOOTER_HEIGHT = 16;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
/** Extra top inset for the folder-name header while inside a folder. */
const FOLDER_HEADER_HEIGHT = 16;
type LauncherMode = "row" | "item";
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
| { kind: "app"; label: string; icon: IconName; appId: string }
| { kind: "folder"; label: string; name: string };
/**
* The launcher grid: app and folder icons with labels, arranged in a grid.
* Navigation has two levels so the max number of swipes to any app is halved:
* entering from the sidebar starts in "row" mode (scroll picks a row); a tap
* drops into "item" mode on that row, defaulting to the middle column (scroll
* picks the item); a tap launches an app or opens a folder (the same grid,
* restricted to that folder's apps). Double-click backs out one level (item →
* row, folder → top grid), and from the top level yields to the sidebar.
*
* The folder grouping lives in the settings store (see launcher-folders.ts)
* and is re-read every paint, so assistant folder tools take effect without
* the layer holding any copy of the state.
*/
class LauncherGridLayer implements Layer {
private mode: LauncherMode = "row";
private selectedRow = 0;
private selectedCol = 1;
private scrollRow = 0;
/** Folder whose contents the grid is showing, or null for the top grid. */
private currentFolder: string | null = null;
constructor(private readonly options: LauncherOptions) {}
/**
* The cells to show, computed fresh from the folder state. Self-heals
* currentFolder: if the open folder no longer exists (the assistant
* disbanded it), the view falls back to the top grid.
*/
private entries(): LauncherGridEntry[] {
const byAppId = new Map(this.options.apps.map((app) => [app.appId, app]));
if (this.currentFolder !== null) {
const members = (getFolders().get(this.currentFolder) ?? [])
.map((appId) => byAppId.get(appId))
.filter(Boolean) as LauncherAppEntry[];
if (members.length > 0) {
return members
.map((app): LauncherGridEntry => ({ kind: "app", label: app.label, icon: app.icon, appId: app.appId }))
.sort((a, b) => a.label.localeCompare(b.label));
}
this.currentFolder = null;
}
const assignments = getFolderAssignments();
const entries: LauncherGridEntry[] = [];
// A folder cell appears only when it holds at least one known app; stale
// assignments to removed apps are ignored.
for (const [name, members] of getFolders()) {
if (members.some((appId) => byAppId.has(appId))) {
entries.push({ kind: "folder", label: name, name });
}
}
for (const app of this.options.apps) {
if (!assignments[app.appId]) {
entries.push({ kind: "app", label: app.label, icon: app.icon, appId: app.appId });
}
}
return entries.sort((a, b) => a.label.localeCompare(b.label));
}
private rowCount(entryCount: number): number {
return Math.max(1, Math.ceil(entryCount / COLS));
}
private itemsInRow(entryCount: number, row: number): number {
return Math.max(0, Math.min(COLS, entryCount - row * COLS));
}
/** Leave the current folder, putting the selection back on its cell. */
private exitFolder(): void {
const folder = this.currentFolder;
this.currentFolder = null;
this.mode = "row";
const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder);
this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0;
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const focused = ctx.stack.isFocused();
const entries = this.entries();
const rows = this.rowCount(entries.length);
this.selectedRow = clamp(this.selectedRow, 0, rows - 1);
this.selectedCol = clamp(
this.selectedCol,
0,
Math.max(0, this.itemsInRow(entries.length, this.selectedRow) - 1),
);
const gridTop = this.currentFolder !== null ? GRID_TOP + FOLDER_HEADER_HEIGHT : GRID_TOP;
if (this.currentFolder !== null) {
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
const gridBottom = height - FOOTER_HEIGHT;
const rowH = (gridBottom - gridTop) / VISIBLE_ROWS;
const colW = width / COLS;
// Scroll to keep the selected row among the fully-visible rows.
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, FULL_ROWS, rows);
const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH;
// Selection highlight (row band, or a single cell in item mode).
const selY = rowY(this.selectedRow);
if (this.mode === "row") {
drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6);
} else {
drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6);
}
for (let index = 0; index < entries.length; index++) {
const entry = entries[index]!;
const row = Math.floor(index / COLS);
if (row < this.scrollRow) continue;
const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
if (blockTop >= gridBottom) break; // fully below the grid
const centerX = (index % COLS) * colW + colW / 2;
const icon = renderIcon(entry.kind === "app" ? entry.icon : "folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - blockTop));
image.bitBlt(icon, Math.round(centerX - icon.width / 2), Math.round(blockTop), {
height: clipHeight,
transparentZero: true,
});
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
const label = truncateText(font, entry.label, colW - 8);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210);
}
}
const selectedEntry = entries[this.selectedRow * COLS + this.selectedCol];
const pickVerb = selectedEntry?.kind === "folder" ? "open" : "launch";
const hint =
this.mode === "row"
? `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick ${GESTURE_DOUBLE_CLICK} back`
: `${GESTURE_SCROLL} item ${GESTURE_CLICK} ${pickVerb} ${GESTURE_DOUBLE_CLICK} row`;
image.drawText(font, 8, height - 14, hint, 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
const entries = this.entries();
const rows = this.rowCount(entries.length);
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.mode === "row") {
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
} else {
// Item selection traverses the grid linearly: past a row's edge it
// continues onto the adjacent row (stopping at the grid's ends).
const itemCount = this.itemsInRow(entries.length, this.selectedRow);
const next = clamp(this.selectedCol, 0, itemCount - 1) + delta;
if (next >= 0 && next < itemCount) {
this.selectedCol = next;
} else if (next < 0 && this.selectedRow > 0) {
this.selectedRow--;
this.selectedCol = this.itemsInRow(entries.length, this.selectedRow) - 1;
} else if (next >= itemCount && this.selectedRow < rows - 1) {
this.selectedRow++;
this.selectedCol = 0;
}
}
return;
}
case "click": {
if (this.mode === "row") {
this.mode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(
Math.floor(COLS / 2),
this.itemsInRow(entries.length, this.selectedRow) - 1,
);
} else {
const entry = entries[this.selectedRow * COLS + this.selectedCol];
if (!entry) return;
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.mode = "row";
this.selectedRow = 0;
this.scrollRow = 0;
} else {
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.mode = "row";
await this.options.launchApp(entry.appId);
}
}
return;
}
case "double-click":
if (this.mode === "item") {
this.mode = "row";
} else if (this.currentFolder !== null) {
this.exitFolder();
} else {
shell.yieldFocusToSidebar();
}
return;
default:
return;
}
}
}
/**
* The launcher: a pinned, uncloseable in-process window presenting the app
* grid. Selecting an app asks the controller to launch it and foregrounds the
* new window.
*/
export function createLauncherWindow(options: LauncherOptions): ShellWindow {
const created = createInProcessWindow({
appId: "launcher",
windowId: LAUNCHER_WINDOW_ID,
title: "Apps",
iconLetter: "A",
icon: "layout-grid",
closeable: false,
actions: options.actions,
// Not wrapped in YieldAtRootLayer: the grid handles double-click itself to
// back out of item selection before yielding to the sidebar.
baseLayer: new LauncherGridLayer(options),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
});
// The assistant's folder tools change the grouping from outside the window;
// the settings broadcast is the change signal, and the fingerprint check
// keeps every unrelated setting change from repainting the launcher. The
// launcher is pinned for the app's lifetime, so the subscription never needs
// tearing down.
let lastFolderState = getFolderStateFingerprint();
onAnySettingChanged(() => {
const state = getFolderStateFingerprint();
if (state === lastFolderState) return;
lastFolderState = state;
created.requestRender();
});
return created.window;
}
-105
View File
@@ -1,105 +0,0 @@
/**
* Launcher folder state: which apps are grouped into which named folder.
* Stored as one JSON string (appId -> folder name) in the settings store, so
* it persists and its changes broadcast to every isolate. An unset value means
* the default grouping; any edit persists the whole map, so clearing out the
* defaults sticks. Folders exist only while at least one app is assigned to
* them — there is no separate create/delete step.
*/
import { getStringSetting, setStringSetting } from "../../native/settings-store";
const STORAGE_KEY = "launcher.folders";
/** The out-of-the-box grouping, applied only while the setting is unset. */
const DEFAULT_ASSIGNMENTS: Record<string, string> = {
blocks: "Games",
freecell: "Games",
minesweeper: "Games",
pinball: "Games",
};
export const FOLDER_NAME_MAX_LENGTH = 24;
/** appId -> folder name. Malformed stored JSON reads as "no folders". */
export function getFolderAssignments(): Record<string, string> {
const raw = getStringSetting(STORAGE_KEY, "");
if (!raw) return { ...DEFAULT_ASSIGNMENTS };
try {
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
const assignments: Record<string, string> = {};
for (const appId of Object.keys(parsed)) {
const folder = parsed[appId];
if (typeof folder === "string" && folder.trim()) {
assignments[appId] = folder.trim();
}
}
return assignments;
} catch {
return {};
}
}
/** folder name -> appIds (assignment order), folder names sorted. */
export function getFolders(): Map<string, string[]> {
const assignments = getFolderAssignments();
const unsorted = new Map<string, string[]>();
for (const appId of Object.keys(assignments)) {
const folder = assignments[appId]!;
const members = unsorted.get(folder);
if (members) {
members.push(appId);
} else {
unsorted.set(folder, [appId]);
}
}
const folders = new Map<string, string[]>();
for (const name of Array.from(unsorted.keys()).sort((a, b) => a.localeCompare(b))) {
folders.set(name, unsorted.get(name)!);
}
return folders;
}
/** Assign an app to a folder, or (with null) return it to the top level. */
export function setAppFolder(appId: string, folder: string | null): void {
const assignments = getFolderAssignments();
if (folder) {
assignments[appId] = folder;
} else {
delete assignments[appId];
}
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
}
/** Move every app out of a folder. Returns how many apps were moved. */
export function disbandFolder(folder: string): number {
const assignments = getFolderAssignments();
let moved = 0;
for (const appId of Object.keys(assignments)) {
if (assignments[appId] === folder) {
delete assignments[appId];
moved++;
}
}
if (moved > 0) {
setStringSetting(STORAGE_KEY, JSON.stringify(assignments));
}
return moved;
}
/**
* Match an existing folder name case-insensitively (so "games" reuses
* "Games" instead of creating a near-duplicate), else the trimmed input.
*/
export function resolveFolderName(input: string): string {
const trimmed = input.trim();
for (const name of getFolders().keys()) {
if (name.toLowerCase() === trimmed.toLowerCase()) return name;
}
return trimmed;
}
/** Raw stored value, for cheap change detection by the launcher window. */
export function getFolderStateFingerprint(): string {
return getStringSetting(STORAGE_KEY, "");
}
-494
View File
@@ -1,494 +0,0 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
GESTURE_SCROLL_DOWN,
GESTURE_SCROLL_UP,
} from "../gestures";
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../menu";
import {
mediaControllerBridge,
type MediaControllerState,
type MediaQueueItem,
} from "../../native/media-controller";
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
import { MediaBrowseLayer } from "../apps/media-browse";
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../layers";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const MUSIC_WINDOW_ID = "music";
export const MUSIC_SURFACE_ID = "window:music";
const ART_SIZE = 96;
const ART_X = 22;
const ART_Y = 8;
const META_X = ART_X + ART_SIZE + 14;
const LIST_TOP = 120;
const ROW_HEIGHT = 16;
const ACTION_X = 22;
const ACTION_WIDTH = 154;
const COLUMN_DIVIDER_X = 190;
const QUEUE_X = 204;
const FOOTER_HEIGHT = 20;
const PLAYLIST_ACTION_INDEX = 1;
type MusicAction =
| { kind: "play-pause"; label: string; enabled: boolean }
| { kind: "previous"; label: string; enabled: boolean }
| { kind: "next"; label: string; enabled: boolean }
| { kind: "volume"; label: string; enabled: boolean }
| { kind: "playlist"; label: string; enabled: boolean }
| { kind: "browse"; label: string; enabled: boolean };
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).
*/
class MusicAppLayer implements Layer {
private focusColumn: FocusColumn = "actions";
private selectedActionIndex = 0;
private selectedQueueIndex = 0;
private queueScrollRow = 0;
private art: GrayImage | null = null;
private artKey = "";
isPlaylistFocused(): boolean {
return this.focusColumn === "playlist";
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const media = mediaControllerBridge.snapshot();
if (!media.accessEnabled) {
const lines = wrapText(
font,
"Notification access is required before Android exposes media sessions. Click to open settings.",
width - 48,
);
for (let index = 0; index < lines.length; index++) {
image.drawText(font, 24, 16 + index * 14, lines[index]!, 180);
}
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (!media.available) {
image.drawText(font, 24, 16, "No active media session.", 180);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 34, "Click to browse a music app's library,", 150);
image.drawText(font, 24, 48, "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse ${GESTURE_DOUBLE_CLICK} back`, 110);
} else {
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
}
return image;
}
this.drawArt(image, media);
const metaWidth = width - META_X - 24;
const titleLines = wrapText(font, media.title || "Unknown title", metaWidth).slice(0, 2);
for (let index = 0; index < titleLines.length; index++) {
image.drawText(font, META_X, ART_Y + 2 + index * 15, titleLines[index]!, 230);
}
image.drawText(font, META_X, ART_Y + 36, media.artist || "Unknown artist", 180);
if (media.album) {
image.drawText(font, META_X, ART_Y + 52, truncateText(font, media.album, metaWidth), 150);
}
image.drawText(font, META_X, ART_Y + 68, truncateText(font, media.appName || media.packageName, metaWidth), 110);
this.drawProgress(image, media, metaWidth);
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
if (this.selectedQueueIndex < this.queueScrollRow) {
this.queueScrollRow = this.selectedQueueIndex;
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
}
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
for (let index = 0; index < actions.length; index++) {
const action = actions[index]!;
const y = LIST_TOP + index * ROW_HEIGHT;
const selected = index === this.selectedActionIndex;
const highlightX = ACTION_X - 6;
const highlightY = y - 1;
const highlightWidth = ACTION_WIDTH + 8;
const highlightHeight = ROW_HEIGHT - 1;
if (selected) {
drawSelectionHighlight(
image,
highlightX,
highlightY,
highlightWidth,
highlightHeight,
ctx.stack.isFocused() && this.focusColumn === "actions",
4,
);
}
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value);
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
}
}
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - FOOTER_HEIGHT - 3, 45);
if (!queue.length) {
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
} else {
const queueWidth = width - QUEUE_X - 20;
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
for (let index = this.queueScrollRow; index < lastVisible; index++) {
const item = queue[index]!;
const y = LIST_TOP + (index - this.queueScrollRow) * ROW_HEIGHT;
const selected = index === this.selectedQueueIndex;
if (selected) {
drawSelectionHighlight(
image,
QUEUE_X - 6,
y - 1,
queueWidth + 8,
ROW_HEIGHT - 1,
ctx.stack.isFocused() && this.focusColumn === "playlist",
4,
);
}
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
}
if (queue.length > visibleRows) {
const trackHeight = visibleRows * ROW_HEIGHT - 4;
const trackX = width - 12;
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
const maxScrollRow = queue.length - visibleRows;
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
}
}
const backTarget = this.focusColumn === "playlist" ? "actions" : "back";
image.drawText(
font,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} ${backTarget}`,
110,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click" && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
return;
}
const media = mediaControllerBridge.snapshot();
if (!media.accessEnabled) {
if (event.type === "click") {
mediaControllerBridge.openNotificationAccessSettings();
} else if (event.type === "double-click") {
ctx.stack.pop();
}
return;
}
if (!media.available) {
if (event.type === "click") {
this.openBrowse(ctx);
}
return;
}
const queue = mediaControllerBridge.getQueue();
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
switch (event.type) {
case "scroll-up":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length;
}
return;
case "scroll-down":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length;
}
return;
case "click": {
if (this.focusColumn === "playlist") {
const item = queue[this.selectedQueueIndex];
if (item) {
await mediaControllerBridge.skipToQueueItem(item.id);
// The player rebuilds the queue with the chosen track at index 0.
this.selectedQueueIndex = 0;
this.queueScrollRow = 0;
}
return;
}
const action = actions[this.selectedActionIndex];
if (!action || !action.enabled) return;
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
else if (action.kind === "next") await mediaControllerBridge.skipNext();
else if (action.kind === "browse") this.openBrowse(ctx);
else if (action.kind === "volume") {
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
} else if (action.kind === "playlist") {
const activeIndex = queue.findIndex((item) => item.active);
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
this.focusColumn = "playlist";
}
return;
}
default:
return;
}
}
/**
* Open the media-library browser (the Android Auto MediaBrowserService
* path): with one browsable player installed go straight to its library,
* otherwise offer a picker first.
*/
private openBrowse(ctx: LayerContext): void {
const apps = mediaBrowserBridge.listBrowsableApps(true);
if (!apps.length) return;
const pushBrowser = (target: LayerContext, app: MediaBrowserApp) => {
target.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
};
if (apps.length === 1) {
pushBrowser(ctx, apps[0]!);
return;
}
ctx.stack.push(
new MenuLayer(
"Browse library",
apps.map((app) => ({
label: app.appName,
onSelect: (menuCtx: LayerContext) => {
menuCtx.stack.pop();
pushBrowser(menuCtx, app);
},
})),
),
);
}
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
const volume = mediaControllerBridge.getMediaVolumePercent();
return [
{
kind: "play-pause",
label: media.playbackState === "playing" ? "Pause" : "Play",
enabled: media.canPlayPause,
},
{ kind: "playlist", label: "Playlist", enabled: queue.length > 0 },
{ kind: "browse", label: "Browse library", enabled: mediaBrowserBridge.listBrowsableApps().length > 0 },
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
];
}
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
if (!queue.length && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
}
}
private drawProgress(image: GrayImage, media: MediaControllerState, width: number): void {
if (media.durationMs <= 0 || media.positionMs < 0) return;
const font = getDefaultSmallFont();
const y = ART_Y + 84;
const elapsed = formatMediaTime(media.positionMs);
const duration = formatMediaTime(media.durationMs);
image.drawText(font, META_X, y, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), y, duration, 140);
const barY = ART_Y + 100;
image.drawRect(META_X, barY, width, 5, 55);
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
image.fillRect(META_X + 1, barY + 1, Math.round((width - 2) * progress), 3, 170);
}
private drawArt(image: GrayImage, media: MediaControllerState): void {
const key = `${media.packageName}|${media.title}|${media.album}`;
if (key !== this.artKey) {
this.artKey = key;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
}
if (this.art) {
// Center within the art box.
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
const dy = ART_Y + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
image.bitBlt(this.art, dx, dy);
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
} else {
image.drawRect(ART_X, ART_Y, ART_SIZE, ART_SIZE, 60);
const font = getDefaultSmallFont();
image.drawText(font, ART_X + 22, ART_Y + ART_SIZE / 2 - 7, "no art", 90);
}
}
}
/** Centered volume control overlay; scroll gestures adjust a 0..100 target by two. */
class VolumeModalLayer implements Layer {
private volume: number;
constructor(initialVolume: number) {
this.volume = clamp(initialVolume, 0, 100);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const image = paintBelow();
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const { width, height } = ctx.stack.getBaseSize();
const boxWidth = 340;
const boxHeight = 150;
const x = ((width - boxWidth) / 2) | 0;
const y = ((height - boxHeight) / 2) | 0;
image.fillRoundedRect(x, y, boxWidth, boxHeight, 1, 8);
image.drawRoundedRect(x, y, boxWidth, boxHeight, 95, 8);
image.drawText(small, x + 18, y + 14, "Media volume", 180);
const value = `${this.volume} / 100`;
const valueX = x + (((boxWidth - medium.measureText(value)) / 2) | 0);
image.drawText(medium, valueX, y + 45, value, 245);
const barX = x + 28;
const barY = y + 83;
const barWidth = boxWidth - 56;
image.drawRect(barX, barY, barWidth, 9, 65);
image.fillRect(barX + 1, barY + 1, Math.round((barWidth - 2) * this.volume / 100), 7, 175);
const hint = `${GESTURE_SCROLL_UP} +2 ${GESTURE_SCROLL_DOWN} -2 ${GESTURE_DOUBLE_CLICK} done`;
const hintX = x + (((boxWidth - small.measureText(hint)) / 2) | 0);
image.drawText(small, hintX, y + 116, hint, 120);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "scroll-up") {
this.adjust(2);
} else if (event.type === "scroll-down") {
this.adjust(-2);
} else if (event.type === "double-click") {
ctx.stack.pop();
}
}
private adjust(delta: number): void {
this.volume = clamp(this.volume + delta, 0, 100);
mediaControllerBridge.setMediaVolumePercent(this.volume);
}
}
/** Let back leave the playlist column before the root wrapper yields to the shell. */
class MusicRootLayer implements Layer {
private readonly yieldAtRoot: YieldAtRootLayer;
constructor(private readonly music: MusicAppLayer) {
this.yieldAtRoot = new YieldAtRootLayer(music);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
return this.yieldAtRoot.paint(ctx, paintBelow);
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click" && this.music.isPlaylistFocused()) {
await this.music.handleInput(event, ctx);
return;
}
await this.yieldAtRoot.handleInput(event, ctx);
}
onRemoved(): void {
this.yieldAtRoot.onRemoved();
}
}
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
let unsubscribe: (() => void) | null = null;
let progressTimer: ReturnType<typeof setInterval> | null = null;
const musicLayer = new MusicAppLayer();
const stopProgressTimer = () => {
if (progressTimer !== null) {
clearInterval(progressTimer);
progressTimer = null;
}
};
const app = createInProcessWindow({
appId: "music",
windowId: MUSIC_WINDOW_ID,
title: "Music",
iconLetter: "M",
icon: "music",
closeable: true,
actions: options.actions,
baseLayer: new MusicRootLayer(musicLayer),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
stopProgressTimer();
unsubscribe?.();
unsubscribe = null;
options.onClosed();
},
});
unsubscribe = mediaControllerBridge.onStateChange((state) => {
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
progressTimer ??= setInterval(() => app.requestRender(), 1_000);
} else {
stopProgressTimer();
}
app.requestRender();
});
return app;
}
function formatMediaTime(milliseconds: number): string {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
return hours > 0
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
: `${minutes}:${String(seconds).padStart(2, "0")}`;
}
-103
View File
@@ -1,103 +0,0 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { NightscoutLayer } from "../apps/nightscout";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
import { shell } from "./shell";
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
export const NIGHTSCOUT_SURFACE_ID = "window:nightscout";
const TRAY_ICON_ID = "nightscout";
const TRAY_HEIGHT = 24;
const TRAY_GRAPH_WIDTH = 48;
const TRAY_STALE_MS = 15 * 60 * 1000;
const TRAY_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
/**
* The Nightscout app: the full-screen glucose view (previously reached via
* the dashboard card) in its own in-process window. While the app is running
* it also publishes a top-bar tray icon: the latest blood-glucose value
* (struck through when stale) plus a minimal 48px history graph.
*/
export function createNightscoutAppWindow(options: InProcessAppOptions): InProcessWindow {
let unsubscribe: (() => void) | null = null;
const app = createInProcessWindow({
appId: "nightscout",
windowId: NIGHTSCOUT_WINDOW_ID,
title: "Nightscout",
iconLetter: "Ns",
icon: "nightscout",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new NightscoutLayer()),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
shell.setTrayIcon(TRAY_ICON_ID, null);
options.onClosed();
},
});
// onStateChange fires immediately with the current snapshot, so this also
// publishes the initial tray icon.
unsubscribe = nightscoutBridge.onStateChange((state) => {
shell.setTrayIcon(TRAY_ICON_ID, buildNightscoutTrayIcon(state));
app.requestRender();
});
return app;
}
/** BG value (struck through when stale) + minimal 2-hour line graph. */
function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
const font = getDefaultSmallFont();
const nowMs = Date.now();
const label = state.latest ? `${state.latest.sgv}` : "--";
const labelWidth = font.measureText(label);
const image = new GrayImage(labelWidth + 4 + TRAY_GRAPH_WIDTH, TRAY_HEIGHT, 0);
const textY = Math.max(0, ((TRAY_HEIGHT - font.lineHeight) / 2) | 0);
image.drawText(font, 0, textY, label, 220);
const stale = state.latest !== null && nowMs - state.latest.timestampMs > TRAY_STALE_MS;
if (stale) {
image.drawLine(0, TRAY_HEIGHT / 2, labelWidth, TRAY_HEIGHT / 2, 180);
}
drawTrayGraph(image, labelWidth + 4, state, nowMs);
return image;
}
function drawTrayGraph(image: GrayImage, x: number, state: NightscoutState, nowMs: number): void {
const windowStartMs = nowMs - TRAY_GRAPH_WINDOW_MS;
const points = state.history.filter(
(point) => point.timestampMs >= windowStartMs && point.timestampMs <= nowMs,
);
if (points.length === 0) return;
const values = points.map((point) => point.sgv);
const min = Math.min(...values, 60);
const max = Math.max(...values, 200);
const span = Math.max(20, max - min);
const plotted = points.map((point) => ({
timestampMs: point.timestampMs,
x: x + Math.round(((point.timestampMs - windowStartMs) / TRAY_GRAPH_WINDOW_MS) * (TRAY_GRAPH_WIDTH - 1)),
y: TRAY_HEIGHT - 1 - Math.round(((point.sgv - min) / span) * (TRAY_HEIGHT - 1)),
}));
for (let index = 1; index < plotted.length; index++) {
const previous = plotted[index - 1]!;
const current = plotted[index]!;
if (current.timestampMs - previous.timestampMs <= TRAY_STALE_MS) {
image.drawLine(previous.x, previous.y, current.x, current.y, 190);
} else {
image.fillRect(current.x, current.y, 1, 1, 190);
}
}
}
-32
View File
@@ -1,32 +0,0 @@
import { NotificationsListLayer } from "../notifications";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const NOTIFICATIONS_WINDOW_ID = "notifications";
export const NOTIFICATIONS_SURFACE_ID = "window:notifications";
/**
* The Notifications app: the Android-notification list (previously a
* dashboard page) in its own in-process window; selecting a notification
* opens the detail view with its quick actions.
*/
export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow {
return createInProcessWindow({
appId: "notifications",
windowId: NOTIFICATIONS_WINDOW_ID,
title: "Notifications",
iconLetter: "N",
icon: "bell",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new NotificationsListLayer()),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
-64
View File
@@ -1,64 +0,0 @@
import { GrayImage } from "../../graphics/image";
import { LayerActions } from "../layers";
import { EditTextSettingLayer } from "../dashboard-settings";
import { createSettingsPanelLayer } from "../dashboard/settings-menus";
import { createInProcessWindow } from "./in-process-window";
import { type ShellWindow } from "./shell";
export const SETTINGS_WINDOW_ID = "settings";
export const SETTINGS_SURFACE_ID = "window:settings";
export type SettingsAppOptions = {
actions: LayerActions;
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
setSurfaceVisible: (visible: boolean) => void;
removeSurface: () => void;
onClosed: () => void;
};
export type SettingsAppWindow = {
window: ShellWindow;
requestRender: () => void;
/** Select a section in the left column by label (e.g. "Terminal"). */
focusSection: (label: string) => void;
/** Whether the glasses-side text-setting editor is the top layer. */
isTextEditorOnTop: () => boolean;
/** Pop the text-setting editor if it is on top; returns whether it was. */
closeTextEditor: () => boolean;
};
/**
* The Settings app: the settings menu tree (previously a dashboard submenu)
* hosted in its own in-process window. In-process because the text-setting
* editor is synchronized with the phone-side TextField through the
* controller, which lives on the main thread.
*/
export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAppWindow {
const panel = createSettingsPanelLayer();
const { window, stack, requestRender } = createInProcessWindow({
appId: "settings",
windowId: SETTINGS_WINDOW_ID,
title: "Settings",
iconLetter: "Se",
icon: "settings",
closeable: true,
actions: options.actions,
// Not wrapped in YieldAtRootLayer: the panel routes double-click itself
// (right column -> left column, then left column -> sidebar).
baseLayer: panel,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
return {
window,
requestRender,
focusSection: (label) => {
panel.focusSection(label);
requestRender();
},
isTextEditorOnTop: () => stack.topMatches((layer) => layer instanceof EditTextSettingLayer),
closeTextEditor: () => stack.popIfTop((layer) => layer instanceof EditTextSettingLayer),
};
}
+2 -2
View File
@@ -4,8 +4,8 @@ import { EvenAIStatus, EventSourceType, OsEventTypeList } from "../../g2/events"
import type { RawInputEvent } from "../../native/faceclaw-communicator";
import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, noopLayerActions } from "../layers";
import { MenuLayer, type MenuItem } from "../menu";
import { VoiceInputLayer, type VoiceSendTarget } from "../apps/voice-input";
import { AssistantLayer } from "../apps/assistant";
import { VoiceInputLayer, type VoiceSendTarget } from "./voice-input";
import { AssistantLayer } from "./assistant";
import { AssistantSession, type AssistantBackendConfig } from "../../assistant/session";
import { resolveAssistantModel } from "../../assistant/models";
import type { AssistantContext } from "../../assistant/types";
-75
View File
@@ -1,75 +0,0 @@
import { imageFromAsciiArt } from "../../graphics/image";
import { TranscribeLayer } from "../apps/transcribe";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
import { shell } from "./shell";
export const TRANSCRIBE_WINDOW_ID = "transcribe";
export const TRANSCRIBE_SURFACE_ID = "window:transcribe";
const TRAY_ICON_ID = "transcribe";
// 16x18 microphone glyph for the top-bar tray.
const MIC_ICON = imageFromAsciiArt(
[
" #### ",
" ###### ",
" # # ",
" # # ",
" # # ",
" # # ",
" # # ",
" # # # # ",
" # # # # ",
" # #### # ",
" # # ",
" ###### ",
" # ",
" # ",
" ###### ",
],
220,
);
export type TranscribeAppOptions = InProcessAppOptions & {
/** Begin continuous mic capture (kept on while the window is open). */
startContinuousVoiceCapture: () => void;
stopContinuousVoiceCapture: () => void;
};
/**
* The Transcribe app: live speech-to-text in its own window. While open it
* holds continuous mic capture (so push-to-talk can share the stream) and
* shows a microphone tray icon in the top bar.
*/
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
const layer = new TranscribeLayer();
const app = createInProcessWindow({
appId: "transcribe",
windowId: TRANSCRIBE_WINDOW_ID,
title: "Transcribe",
iconLetter: "Tr",
icon: "mic",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(layer),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
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);
options.startContinuousVoiceCapture();
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
return app;
}
+492
View File
@@ -0,0 +1,492 @@
import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { refineDictation, type AnthropicStreamHandle } from "../../native/anthropic";
import { anthropicApiKeySetting } from "../dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, gestureHints } from "../gestures";
import { drawSelectionHighlight } from "../menu";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "./geometry";
const DIALOG_X = 40;
const DIALOG_W = G2_LENS_WIDTH - 80;
// The dialog fits inside the min-height window band (like the other shell
// overlays), wherever the vertical position setting puts it.
const DIALOG_MARGIN_Y = 24;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
return minWindowTop() + DIALOG_MARGIN_Y;
}
const MENU_ROW_H = 20;
// After the mic stops, the provider's final transcript can trail in (cloud
// commit round-trip); wait this long for it before refining with what we have.
const FOLLOWUP_FINALIZE_TIMEOUT_MS = 1200;
/**
* The dialog's lifecycle. "capturing" is the first utterance (push-to-talk or
* click-to-finish); "menu" is the send / Continue / Discard menu; "continuing"
* records a follow-up utterance (always click-to-finish); "refining" streams
* the LLM-merged text, then returns to "menu".
*/
type VoicePhase = "capturing" | "menu" | "continuing" | "refining";
/**
* A destination the captured text can be sent to. The shell supplies these per
* entry point: "Send to Assistant" (hand off to the voice assistant) and/or
* "Type Into App" (deliver to the foreground window's text input).
*/
export type VoiceSendTarget = {
id: string;
label: string;
onSend: (text: string) => void;
};
export type VoiceInputLayerOptions = {
actions: LayerActions;
/** Post-removal cleanup (also fires when the screen turns off). */
onClosed: () => void;
/** Pop this layer off the shell stack. */
dismiss: () => void;
/** Ordered send destinations shown as the first menu rows. */
sendTargets: VoiceSendTarget[];
/** Which send target is highlighted by default (entry-point dependent). */
defaultTargetIndex?: number;
/** A single click ends the utterance (opened from a menu, no held button). */
finishOnClick?: boolean;
/** Wakeword flow: mic starts immediately and ends on detected silence. */
handsFree?: boolean;
/** Skip the menu: on capture end, send straight to the default target. */
autoSend?: boolean;
};
/**
* Push-to-talk voice dialog, drawn on top of whatever is already on screen.
* The mic runs while the button is held (long-press); releasing it stops the
* mic and shows a Send / Continue / Discard menu. Send delivers the transcript
* to the foreground window (e.g. the terminal); Discard (or double-click)
* closes. Continue records another utterance — extra content or a spoken edit
* ("change X to Y", "insert ... after ...") — and merges it into the message
* with an LLM, landing back on the same menu.
*
* When opened from a menu instead of a held button (finishOnClick), there is
* no release event to end the capture, so a single click stops the mic.
*
* When opened by the "Hey Even" wakeword (handsFree), there is likewise no
* button involved: the mic starts immediately and stops when the speaker does,
* via endpoint detection on the decoded PCM. A click still ends it early.
*/
export class VoiceInputLayer implements Layer {
private phase: VoicePhase = "capturing";
private status = "Listening...";
// The active utterance. displayText() is what the dialog shows and what
// Send delivers; the refine flow also writes the merged result here.
private finalizedText = "";
private liveText = "";
// Snapshot of the message when Continue starts, so a failed or cancelled
// continuation can fall back to it.
private baseText = "";
/** Whether the mic is running (capture bookkeeping, not UI state). */
private capturing = false;
/** Continuation stopped; waiting for the trailing final transcript. */
private followupFinalizeTimer: ReturnType<typeof setTimeout> | null = null;
private refineHandle: AnthropicStreamHandle | null = null;
private menuIndex = 0;
/** Auto-send (wakeword skip-confirmation) is waiting to fire. */
private pendingAutoSend = false;
private autoSendTimer: ReturnType<typeof setTimeout> | null = null;
private unsubscribeTranscript: (() => void) | null = null;
private unsubscribeStatus: (() => void) | null = null;
private unsubscribeSpeechEnd: (() => void) | null = null;
private readonly actions: LayerActions;
private readonly onClosed: () => void;
private readonly dismiss: () => void;
private readonly sendTargets: VoiceSendTarget[];
private readonly defaultTargetIndex: number;
private readonly finishOnClick: boolean;
private readonly handsFree: boolean;
private readonly autoSend: boolean;
constructor(options: VoiceInputLayerOptions) {
this.actions = options.actions;
this.onClosed = options.onClosed;
this.dismiss = options.dismiss;
this.sendTargets = options.sendTargets;
this.finishOnClick = options.finishOnClick ?? false;
this.handsFree = options.handsFree ?? false;
this.autoSend = options.autoSend ?? false;
const defaultIndex = options.defaultTargetIndex ?? 0;
this.defaultTargetIndex = Math.min(Math.max(0, defaultIndex), Math.max(0, this.sendTargets.length - 1));
this.menuIndex = this.defaultTargetIndex;
}
startCapture(): void {
if (this.capturing) return;
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event));
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
// The refine stage owns the status line ("Refining...", error text).
if (this.phase === "refining") return;
this.status = state.status;
this.actions.requestRender();
});
if (this.handsFree) {
// No button is held, so the mic has to stop itself. endCapture() is
// idempotent, and a click still ends the utterance early.
this.unsubscribeSpeechEnd = voiceControlBridge.onSpeechEnd(() => {
if (this.phase === "capturing") {
this.endCapture();
}
});
}
this.capturing = true;
void this.actions.startVoiceCapture(this.handsFree);
this.actions.requestRender();
}
/**
* Whether a single click ends the utterance. True when there is no held
* button to release: opened from the overlay menu (finishOnClick), or
* hands-free from the wakeword — where it also lets the user cut the
* silence-detection wait short.
*/
private get clickEndsCapture(): boolean {
return this.finishOnClick || this.handsFree;
}
/** Button released (or click when clickEndsCapture): stop the mic. */
endCapture(): void {
if (!this.capturing) return;
if (this.phase === "continuing") {
this.endContinuationCapture();
return;
}
this.capturing = false;
this.phase = "menu";
void this.actions.stopVoiceCapture();
if (this.autoSend && this.sendTargets.length) {
// Skip-confirmation (wakeword): send to the default target as soon as the
// transcript finalizes, or after a short wait for the trailing final.
this.pendingAutoSend = true;
this.status = "Sending...";
this.autoSendTimer = setTimeout(() => this.performAutoSend(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
} else if (this.status.startsWith("Listening")) {
this.status = "Send, continue, or discard?";
}
this.actions.requestRender();
}
/** Fire the queued skip-confirmation send (or fall back to the menu). */
private performAutoSend(): void {
if (this.autoSendTimer !== null) {
clearTimeout(this.autoSendTimer);
this.autoSendTimer = null;
}
if (!this.pendingAutoSend) return;
this.pendingAutoSend = false;
const text = this.displayText().trim();
const target = this.sendTargets[this.defaultTargetIndex];
if (text && target) {
this.dismiss();
target.onSend(text);
} else {
// Nothing heard; fall back to the menu so the user can retry or discard.
this.status = "Send, continue, or discard?";
this.actions.requestRender();
}
}
/** The menu rows: one per send target, then Continue, then Discard. */
private menuRows(): Array<{ label: string; dim: boolean; onSelect: () => void }> {
const text = this.displayText().trim();
const hasText = text.length > 0;
const hasLlmKey = anthropicApiKeySetting.get().trim().length > 0;
const rows: Array<{ label: string; dim: boolean; onSelect: () => void }> = [];
for (const target of this.sendTargets) {
rows.push({
label: target.label,
dim: !hasText,
onSelect: () => {
this.dismiss();
if (hasText) target.onSend(text);
},
});
}
rows.push({
label: hasLlmKey ? "Continue" : "Continue (Needs LLM API key)",
dim: !hasLlmKey,
onSelect: () => {
if (hasLlmKey) this.startContinuation();
},
});
rows.push({ label: "Discard", dim: false, onSelect: () => this.dismiss() });
return rows;
}
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const font = getDefaultSmallFont();
const image = paintBelow();
const top = dialogY();
// Solid dialog box over the underlying UI. Fill 1, not 0: identical after
// 4bpp quantization, but 0 is transparent on the color-key shell surface.
image.fillRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1, 10);
image.drawRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90, 10);
const left = DIALOG_X + 16;
image.drawText(font, left, top + 12, this.capturing ? "Voice ●" : "Voice", 220);
image.drawText(font, left, top + 30, truncateText(font, this.status, TEXT_MAX_WIDTH), 130);
const inMenu = this.phase === "menu";
const rows = inMenu ? this.menuRows() : [];
// Reserve space for the actual number of rows this menu has.
const textBottom = inMenu ? top + DIALOG_H - rows.length * MENU_ROW_H - 8 : top + DIALOG_H - 8;
const textTop = top + 56;
const maxLines = Math.max(1, ((textBottom - textTop) / 16) | 0);
const text = this.displayText() || this.placeholderText();
const wrapped = wrapText(font, text, TEXT_MAX_WIDTH);
const firstLine = Math.max(0, wrapped.length - maxLines);
for (let index = firstLine; index < wrapped.length; index++) {
image.drawText(font, left, textTop + (index - firstLine) * 16, wrapped[index]!, 235);
}
if (inMenu) {
const menuTop = top + DIALOG_H - rows.length * MENU_ROW_H - 2;
for (let i = 0; i < rows.length; i++) {
const rowY = menuTop + i * MENU_ROW_H;
const selected = i === this.menuIndex;
if (selected) {
drawSelectionHighlight(image, left - 4, rowY - 2, DIALOG_W - 24, MENU_ROW_H - 2, true, 6);
}
const row = rows[i]!;
image.drawText(font, left + 4, rowY + 2, row.label, row.dim ? 90 : selected ? 255 : 200);
}
} else {
image.drawText(font, left, top + DIALOG_H - 14, this.hintText(), 110);
}
return image;
}
handleInput(event: DashboardInputEvent, _ctx: LayerContext): void {
switch (this.phase) {
case "capturing":
if (event.type === "double-click") {
this.dismiss();
} else if (this.clickEndsCapture && event.type === "click") {
this.endCapture();
}
return;
case "continuing":
if (event.type === "click") {
this.endContinuationCapture();
} else if (event.type === "double-click") {
this.cancelContinuation("Continuation cancelled");
}
return;
case "refining":
if (event.type === "double-click") {
this.cancelContinuation("Refinement cancelled");
}
return;
case "menu":
this.handleMenuInput(event);
return;
}
}
private handleMenuInput(event: DashboardInputEvent): void {
const rowCount = this.menuRows().length;
switch (event.type) {
case "scroll-up":
this.menuIndex = (this.menuIndex + rowCount - 1) % rowCount;
this.actions.requestRender();
return;
case "scroll-down":
this.menuIndex = (this.menuIndex + 1) % rowCount;
this.actions.requestRender();
return;
case "click": {
const row = this.menuRows()[this.menuIndex];
row?.onSelect();
return;
}
case "double-click":
this.dismiss();
return;
default:
return;
}
}
/** Continue selected: keep the message aside and record a follow-up. */
private startContinuation(): void {
this.baseText = this.displayText().trim();
this.finalizedText = "";
this.liveText = "";
this.phase = "continuing";
this.status = "Listening...";
this.capturing = true;
void this.actions.startVoiceCapture();
this.actions.requestRender();
}
/** Follow-up tap: stop the mic, then refine once the transcript finalizes. */
private endContinuationCapture(): void {
if (!this.capturing) return;
this.capturing = false;
void this.actions.stopVoiceCapture();
this.status = "Refining...";
this.actions.requestRender();
// The provider's committed transcript arrives shortly after stop; refine
// when it does, or after a timeout with whatever partials we have.
this.followupFinalizeTimer = setTimeout(() => this.beginRefine(), FOLLOWUP_FINALIZE_TIMEOUT_MS);
}
private beginRefine(): void {
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
if (this.phase !== "continuing") return;
const followup = this.displayText().trim();
if (!followup) {
this.backToMenu(this.baseText, "No follow-up heard");
return;
}
this.phase = "refining";
this.finalizedText = "";
this.liveText = "";
this.status = "Refining...";
this.actions.requestRender();
this.refineHandle = refineDictation({
apiKey: anthropicApiKeySetting.get(),
original: this.baseText,
followup,
onTextDelta: (_delta, textSoFar) => {
this.finalizedText = textSoFar;
this.actions.requestRender();
},
onDone: (text) => {
this.refineHandle = null;
this.backToMenu(text, "Send, continue, or discard?");
},
onError: (message) => {
this.refineHandle = null;
this.backToMenu(this.baseText, message);
},
});
}
/** Abort a continuation (mic or LLM stage) and restore the prior message. */
private cancelContinuation(status: string): void {
if (this.capturing) {
this.capturing = false;
void this.actions.stopVoiceCapture();
}
this.refineHandle?.cancel();
this.refineHandle = null;
this.backToMenu(this.baseText, status);
}
private backToMenu(text: string, status: string): void {
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
this.finalizedText = text;
this.liveText = "";
this.phase = "menu";
this.menuIndex = 0;
this.status = status;
this.actions.requestRender();
}
onRemoved(): void {
this.unsubscribeTranscript?.();
this.unsubscribeTranscript = null;
this.unsubscribeStatus?.();
this.unsubscribeStatus = null;
this.unsubscribeSpeechEnd?.();
this.unsubscribeSpeechEnd = null;
if (this.followupFinalizeTimer !== null) {
clearTimeout(this.followupFinalizeTimer);
this.followupFinalizeTimer = null;
}
if (this.autoSendTimer !== null) {
clearTimeout(this.autoSendTimer);
this.autoSendTimer = null;
}
this.pendingAutoSend = false;
this.refineHandle?.cancel();
this.refineHandle = null;
if (this.capturing) {
this.capturing = false;
void this.actions.stopVoiceCapture();
}
this.onClosed();
}
private displayText(): string {
if (!this.finalizedText) return this.liveText;
if (!this.liveText) return this.finalizedText;
return `${this.finalizedText} ${this.liveText}`;
}
private placeholderText(): string {
switch (this.phase) {
case "capturing":
return "Listening...";
case "continuing":
return "Say more, or describe an edit...";
case "refining":
return "Refining...";
case "menu":
return "(no speech detected)";
}
}
private hintText(): string {
switch (this.phase) {
case "capturing":
return this.clickEndsCapture
? gestureHints([[GESTURE_CLICK, "done"], [GESTURE_DOUBLE_CLICK, "close"]])
: `${GESTURE_DOUBLE_CLICK} close`;
case "continuing":
return gestureHints([[GESTURE_CLICK, "done"], [GESTURE_DOUBLE_CLICK, "cancel"]]);
case "refining":
default:
return `${GESTURE_DOUBLE_CLICK} cancel`;
}
}
private onTranscript(event: VoiceTranscriptEvent): void {
// The refine stream owns the text buffers once it starts; a transcript
// that trails in after that point is stale.
if (this.phase === "refining") return;
if (event.isFinal) {
const finalText = event.text.trim() || this.liveText.trim();
if (finalText) {
this.finalizedText = this.finalizedText ? `${this.finalizedText} ${finalText}` : finalText;
}
this.liveText = "";
if (this.phase === "continuing" && this.followupFinalizeTimer !== null) {
// The follow-up finalized; no need to keep waiting.
this.beginRefine();
return;
}
if (this.pendingAutoSend) {
// The utterance finalized; skip-confirmation can fire without waiting
// out the fallback timer.
this.performAutoSend();
return;
}
} else {
this.liveText = event.text.trim();
}
this.actions.requestRender();
}
}
-56
View File
@@ -1,56 +0,0 @@
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
import { weatherBridge } from "../../native/weather";
import { WeatherLayer } from "../apps/weather";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const WEATHER_WINDOW_ID = "weather";
export const WEATHER_SURFACE_ID = "window:weather";
/** Local current conditions and forecast from the National Weather Service. */
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
let requestingPermission = false;
let unsubscribe: (() => void) | null = null;
let app: InProcessWindow;
const requestUpdate = () => {
if (requestingPermission) return;
if (hasLocationPermission()) {
weatherBridge.start();
return;
}
requestingPermission = true;
void ensureLocationPermission().then((granted) => {
requestingPermission = false;
if (granted) weatherBridge.start();
app.requestRender();
});
};
app = createInProcessWindow({
appId: "weather",
windowId: WEATHER_WINDOW_ID,
title: "Weather",
iconLetter: "W",
icon: "cloud-sun",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
weatherBridge.stop();
options.onClosed();
},
});
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
requestUpdate();
return app;
}