Use animated menus in a bunch more places

This commit is contained in:
James Babcock committed 2026-09-23 23:57:50 -07:00
1 parent 123c1142d6
commit 71c1971992
37 files changed
+1457 -1005

No files matched your search

+22 -71
View File
@@ -1,26 +1,13 @@
import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { type GrayImage } from "../../graphics/image";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../gestures";
import { drawSelectionHighlight } from "../menu";
import { listRowHeight } from "../metrics";
import { Layer, type LayerActions, type LayerContext } from "../layers";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "./geometry";
import { createInputDialogMenu, paintInputDialog, type InputDialogRow } from "./input-dialog";
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 = 28;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
const MENU_ROWS = 2;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
return minWindowTop() + DIALOG_MARGIN_Y;
}
const FOLLOW_UP_ROW = 0;
const MENU_ROWS: readonly InputDialogRow[] = [
{ label: "Follow-up", dim: false },
{ label: "Done", dim: false },
];
/** thinking: turn running; done/error: finished, showing the Follow-up/Done menu. */
type AssistantPhase = "thinking" | "done" | "error";
@@ -46,7 +33,7 @@ export class AssistantLayer implements Layer {
private phase: AssistantPhase = "thinking";
private replyText = "";
private status = "Thinking...";
private menuIndex = 0;
private readonly menu = createInputDialogMenu(MENU_ROWS);
constructor(
private readonly actions: LayerActions,
@@ -58,7 +45,7 @@ export class AssistantLayer implements Layer {
this.phase = "thinking";
this.replyText = "";
this.status = "Thinking...";
this.menuIndex = 0;
this.menu.select(0);
this.actions.requestRender();
}
@@ -76,61 +63,28 @@ export class AssistantLayer implements Layer {
onTurnDone(): void {
this.phase = "done";
this.status = this.replyText.trim() ? "" : "(no reply)";
this.menuIndex = 0;
this.menu.select(0);
this.actions.requestRender();
}
onError(message: string): void {
this.phase = "error";
this.status = message;
this.menuIndex = 0;
this.menu.select(0);
this.actions.requestRender();
}
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const font = getDefaultSmallFont();
const menuRowH = listRowHeight(font);
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.fillRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1);
image.drawRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90);
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 * menuRowH - 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 * menuRowH - 2;
for (let i = 0; i < rows.length; i++) {
const rowY = menuTop + i * menuRowH;
const selected = i === this.menuIndex;
if (selected) {
drawSelectionHighlight(image, left - 4, rowY - 2, DIALOG_W - 24, menuRowH - 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 - font.lineHeight);
}
paintInputDialog(image, {
title: this.phase === "thinking" ? "Assistant ●" : "Assistant",
status: this.status,
statusValue: this.phase === "error" ? 200 : 130,
text: this.replyText,
menu: inMenu ? this.menu : null,
hint: inMenu ? undefined : `${GESTURE_DOUBLE_CLICK} cancel`,
});
return image;
}
@@ -140,7 +94,7 @@ export class AssistantLayer implements Layer {
this.callbacks.onCancel();
this.phase = "done";
this.status = "Cancelled";
this.menuIndex = 0;
this.menu.select(0);
this.actions.requestRender();
}
return;
@@ -148,15 +102,12 @@ export class AssistantLayer implements Layer {
// 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;
void this.menu.handleInput(event);
this.actions.requestRender();
return;
case "click":
if (this.menuIndex === 0) {
if (this.menu.selectedIndex === FOLLOW_UP_ROW) {
this.callbacks.onFollowUp();
} else {
this.callbacks.onClose();
+52 -24
View File
@@ -1,13 +1,13 @@
import { G2_LENS_WIDTH, type GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { drawSelectionHighlight } from "../menu";
import { Menu, type MenuDrawArgs } from "../menu-core";
import { listRowHeight } from "../metrics";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "./geometry";
/**
* The shared look of the shell's text-entry dialogs (voice input and phone
* keyboard input): a solid box over whatever is on screen with a title line,
* The shared look of the shell's text dialogs (voice input, phone keyboard
* input, and the assistant's reply): a solid box over whatever is on screen with a title line,
* a status line, the message so far, and either a menu of destinations at
* the bottom or a gesture hint.
*/
@@ -19,6 +19,14 @@ const DIALOG_W = G2_LENS_WIDTH - 80;
const DIALOG_MARGIN_Y = 28;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
const TEXT_LEFT = DIALOG_X + 16;
// Menu row boxes: 4px left of the text column, 2px shorter than the row
// pitch, with each row's label inset 8px / 4px into its box.
const MENU_X = TEXT_LEFT - 4;
const MENU_W = DIALOG_W - 24;
const MENU_ROW_GAP = 2;
const MENU_LABEL_INSET_X = 8;
const MENU_LABEL_INSET_Y = 4;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
@@ -31,20 +39,47 @@ export type InputDialogRow = {
dim: boolean;
};
export type InputDialogContent = {
export type InputDialogContent<T extends InputDialogRow = InputDialogRow> = {
title: string;
status: string;
/** Gray level of the status line. Default 130. */
statusValue?: number;
/** The message body (a placeholder when nothing has been captured yet). */
text: string;
/** Menu rows along the bottom edge; empty for no menu. */
rows: readonly InputDialogRow[];
selectedRow: number;
/**
* Menu along the bottom edge (see createInputDialogMenu), or null for none.
* Height is reserved for every item.
*/
menu: Menu<T> | null;
/** Gesture hint drawn along the bottom edge when there is no menu. */
hint?: string;
};
/**
* A wrapping menu of dialog rows, laid out and drawn the way paintInputDialog
* expects. The owning layer keeps it across paints (so the selection sticks),
* refreshes its rows with setItems, forwards scroll events to it, and hands
* it to paintInputDialog while the menu is showing.
*/
export function createInputDialogMenu<T extends InputDialogRow>(items: readonly T[] = [], selectedIndex = 0): Menu<T> {
return new Menu<T>({
items,
selectedIndex,
wrap: true,
rowGap: MENU_ROW_GAP,
highlight: { radius: 6 },
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: drawMenuRow,
});
}
function drawMenuRow({ image, item, x, y, selected }: MenuDrawArgs<InputDialogRow>): void {
const value = item.dim ? 90 : selected ? 255 : 200;
image.drawText(getDefaultSmallFont(), x + MENU_LABEL_INSET_X, y + MENU_LABEL_INSET_Y, item.label, value);
}
/** Paint the dialog onto `image` (an already-painted canvas of the layers below). */
export function paintInputDialog(image: GrayImage, content: InputDialogContent): void {
export function paintInputDialog<T extends InputDialogRow>(image: GrayImage, content: InputDialogContent<T>): void {
const font = getDefaultSmallFont();
const menuRowH = listRowHeight(font);
const top = dialogY();
@@ -54,14 +89,14 @@ export function paintInputDialog(image: GrayImage, content: InputDialogContent):
image.fillRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1);
image.drawRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90);
const left = DIALOG_X + 16;
const left = TEXT_LEFT;
image.drawText(font, left, top + 12, content.title, 220);
image.drawText(font, left, top + 30, truncateText(font, content.status, TEXT_MAX_WIDTH), 130);
image.drawText(font, left, top + 30, truncateText(font, content.status, TEXT_MAX_WIDTH), content.statusValue ?? 130);
const rows = content.rows;
const hasMenu = rows.length > 0;
const menu = content.menu;
const rowCount = menu?.items.length ?? 0;
// Reserve space for the actual number of rows this menu has.
const textBottom = hasMenu ? top + DIALOG_H - rows.length * menuRowH - 8 : top + DIALOG_H - 8;
const textBottom = rowCount > 0 ? top + DIALOG_H - rowCount * menuRowH - 8 : top + DIALOG_H - 8;
const textTop = top + 56;
const maxLines = Math.max(1, ((textBottom - textTop) / 16) | 0);
@@ -73,17 +108,10 @@ export function paintInputDialog(image: GrayImage, content: InputDialogContent):
image.drawText(font, left, textTop + (index - firstLine) * 16, wrapped[index]!, 235);
}
if (hasMenu) {
const menuTop = top + DIALOG_H - rows.length * menuRowH - 2;
for (let i = 0; i < rows.length; i++) {
const rowY = menuTop + i * menuRowH;
const selected = i === content.selectedRow;
if (selected) {
drawSelectionHighlight(image, left - 4, rowY - 2, DIALOG_W - 24, menuRowH - 2, true, 6);
}
const row = rows[i]!;
image.drawText(font, left + 4, rowY + 2, row.label, row.dim ? 90 : selected ? 255 : 200);
}
if (menu && rowCount > 0) {
// Row boxes are one pitch apart, the last ending 6px above the dialog's bottom edge.
const menuTop = top + DIALOG_H - rowCount * menuRowH - 4;
menu.paint(image, { x: MENU_X, y: menuTop, width: MENU_W, height: rowCount * menuRowH - MENU_ROW_GAP }, true);
} else if (content.hint) {
image.drawText(font, left, top + DIALOG_H - 14, content.hint, 110 - font.lineHeight);
}
+15 -15
View File
@@ -1,9 +1,13 @@
import { type GrayImage } from "../../graphics/image";
import { type InputEvent } from "../gestures";
import { Layer, type LayerActions, type LayerContext } from "../layers";
import { paintInputDialog } from "./input-dialog";
import { type Menu } from "../menu-core";
import { createInputDialogMenu, paintInputDialog, type InputDialogRow } from "./input-dialog";
import { type VoiceSendTarget } from "./voice-input";
/** A menu row: a send target, or Discard (no target). */
type KeyboardMenuRow = InputDialogRow & { target?: VoiceSendTarget };
/**
* The phone side's handle on an open keyboard-input dialog: the phone types
* into it and picks where the text goes. The layer itself implements this;
@@ -46,7 +50,7 @@ export type KeyboardInputLayerOptions = {
*/
export class KeyboardInputLayer implements Layer, KeyboardInputSession {
private text = "";
private menuIndex: number;
private readonly menu: Menu<KeyboardMenuRow>;
private closed = false;
private readonly actions: LayerActions;
@@ -62,7 +66,7 @@ export class KeyboardInputLayer implements Layer, KeyboardInputSession {
this.sendTargets = options.sendTargets;
const defaultIndex = options.defaultTargetIndex ?? 0;
this.defaultTargetIndex = Math.min(Math.max(0, defaultIndex), Math.max(0, this.sendTargets.length - 1));
this.menuIndex = this.defaultTargetIndex;
this.menu = createInputDialogMenu(this.menuRows(), this.defaultTargetIndex);
}
get targets(): ReadonlyArray<{ id: string; label: string }> {
@@ -76,8 +80,7 @@ export class KeyboardInputLayer implements Layer, KeyboardInputSession {
}
send(): void {
const row = this.menuRows()[this.menuIndex];
const target = row?.target ?? this.sendTargets[this.defaultTargetIndex];
const target = this.menu.selectedItem?.target ?? this.sendTargets[this.defaultTargetIndex];
if (target) this.sendToTarget(target);
}
@@ -101,9 +104,9 @@ export class KeyboardInputLayer implements Layer, KeyboardInputSession {
}
/** The menu rows: one per send target, then Discard. */
private menuRows(): Array<{ label: string; dim: boolean; target?: VoiceSendTarget }> {
private menuRows(): KeyboardMenuRow[] {
const hasText = this.text.trim().length > 0;
const rows: Array<{ label: string; dim: boolean; target?: VoiceSendTarget }> = [];
const rows: KeyboardMenuRow[] = [];
for (const target of this.sendTargets) {
rows.push({ label: target.label, dim: !hasText, target });
}
@@ -113,29 +116,26 @@ export class KeyboardInputLayer implements Layer, KeyboardInputSession {
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const image = paintBelow();
this.menu.setItems(this.menuRows());
paintInputDialog(image, {
title: "Keyboard",
status: "Type on the phone",
text: this.text || "(nothing typed yet)",
rows: this.menuRows(),
selectedRow: this.menuIndex,
menu: this.menu,
});
return image;
}
handleInput(event: InputEvent, _ctx: LayerContext): void {
const rowCount = this.menuRows().length;
this.menu.setItems(this.menuRows());
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;
void this.menu.handleInput(event);
this.actions.requestRender();
return;
case "click": {
const row = this.menuRows()[this.menuIndex];
const row = this.menu.selectedItem;
if (!row) return;
if (row.target) {
this.sendToTarget(row.target);
+17 -17
View File
@@ -4,7 +4,8 @@ import { refineDictation, type AnthropicStreamHandle } from "../../native/anthro
import { anthropicApiKeySetting } from "../dashboard-settings";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, gestureHints, type InputEvent } from "../gestures";
import { Layer, type LayerActions, type LayerContext } from "../layers";
import { paintInputDialog } from "./input-dialog";
import { type Menu } from "../menu-core";
import { createInputDialogMenu, paintInputDialog, type InputDialogRow } from "./input-dialog";
// After native recognition finishes, a cloud final can still trail in.
const FOLLOWUP_FINALIZE_TIMEOUT_MS = 1200;
@@ -16,6 +17,9 @@ const FOLLOWUP_FINALIZE_TIMEOUT_MS = 1200;
*/
type VoicePhase = "capturing" | "menu" | "continuing" | "refining";
/** A menu row; dim rows still take clicks, and onSelect decides what they do. */
type VoiceMenuRow = InputDialogRow & { onSelect: () => void };
/**
* 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
@@ -84,7 +88,8 @@ export class VoiceInputLayer implements Layer {
private stopGeneration = 0;
private acceptingTranscript = true;
private refineHandle: AnthropicStreamHandle | null = null;
private menuIndex = 0;
/** The send / Continue / Discard menu; its rows are rebuilt on every paint and input. */
private readonly menu: Menu<VoiceMenuRow>;
/** Auto-send (wakeword skip-confirmation) is waiting to fire. */
private pendingAutoSend = false;
private autoSendTimer: ReturnType<typeof setTimeout> | null = null;
@@ -111,7 +116,7 @@ export class VoiceInputLayer implements Layer {
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;
this.menu = createInputDialogMenu(this.menuRows(), this.defaultTargetIndex);
}
startCapture(): void {
@@ -221,11 +226,11 @@ export class VoiceInputLayer implements Layer {
}
/** The menu rows: one per send target, then Continue, then Discard. */
private menuRows(): Array<{ label: string; dim: boolean; onSelect: () => void }> {
private menuRows(): VoiceMenuRow[] {
const text = this.displayText().trim();
const hasText = text.length > 0 && !this.stoppingCapture;
const hasLlmKey = anthropicApiKeySetting.get().trim().length > 0;
const rows: Array<{ label: string; dim: boolean; onSelect: () => void }> = [];
const rows: VoiceMenuRow[] = [];
for (const target of this.sendTargets) {
rows.push({
label: target.label,
@@ -251,12 +256,12 @@ export class VoiceInputLayer implements Layer {
paint(_ctx: LayerContext, paintBelow: () => GrayImage): GrayImage {
const image = paintBelow();
const inMenu = this.phase === "menu";
if (inMenu) this.menu.setItems(this.menuRows());
paintInputDialog(image, {
title: this.capturing && this.listening ? "Voice ●" : "Voice",
status: this.stoppingCapture ? "Finishing transcription..." : this.status,
text: this.displayText() || this.placeholderText(),
rows: inMenu ? this.menuRows() : [],
selectedRow: this.menuIndex,
menu: inMenu ? this.menu : null,
hint: inMenu ? undefined : this.hintText(),
});
return image;
@@ -290,21 +295,16 @@ export class VoiceInputLayer implements Layer {
}
private handleMenuInput(event: InputEvent): void {
const rowCount = this.menuRows().length;
this.menu.setItems(this.menuRows());
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;
void this.menu.handleInput(event);
this.actions.requestRender();
return;
case "click": {
const row = this.menuRows()[this.menuIndex];
row?.onSelect();
case "click":
this.menu.selectedItem?.onSelect();
return;
}
case "double-click":
this.dismiss();
return;
@@ -397,7 +397,7 @@ export class VoiceInputLayer implements Layer {
this.finalizedText = text;
this.liveText = "";
this.phase = "menu";
this.menuIndex = 0;
this.menu.select(0);
this.status = status;
this.actions.requestRender();
}