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

+94 -95
View File
@@ -1,10 +1,10 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { InputEvent } from "../gestures";
import { Layer, LayerContext, PaintBelow } from "../layers";
import { drawSelectionHighlight, isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
import { isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
import { Menu } from "../menu-core";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../metrics";
import { shell } from "../shell/shell";
@@ -30,6 +30,10 @@ export type SettingsSection = {
const PAD = 8;
const LEFT_W = 150;
/** Gap between a row's selection box and the next row. */
const ROW_GAP = 2;
/** Horizontal inset of row text (and custom row content) from its selection box. */
const ROW_TEXT_X = 10;
const MAX_DESCRIPTION_LINES = 3;
// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the
// settings items never use it (they act via ctx only).
@@ -45,23 +49,62 @@ export class SettingsPanelLayer implements Layer {
// Watch swipes map onto the two columns: right goes into a section's
// items, left comes back out (and out to the sidebar from the left column).
readonly acceptsDirectional = true;
private leftIndex = 0;
private rightIndex = 0;
private focus: "left" | "right" = "left";
private leftScroll = 0;
private rightScroll = 0;
private readonly leftMenu: Menu<SettingsSection>;
/** The highlighted section's items; unselected while focus is in the left column (a preview). */
private readonly rightMenu: Menu<MenuItem>;
/** The context of the paint in progress, handed to item render callbacks. */
private paintCtx: LayerContext | null = null;
constructor(private readonly sections: SettingsSection[]) {}
constructor(private readonly sections: SettingsSection[]) {
this.leftMenu = new Menu<SettingsSection>({
items: sections,
wrap: true,
rowGap: ROW_GAP,
highlight: { radius: 6 },
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: ({ image, item, x, y, selected }) => {
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200);
},
});
this.rightMenu = new Menu<MenuItem>({
items: this.section().items,
selectedIndex: null,
wrap: true,
rowGap: ROW_GAP,
highlight: { radius: 6 },
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: ({ image, item, x, y, width, height, selected }) => {
const disabled = isMenuItemDisabled(item);
if (item.render) {
item.render({
image,
x: x + ROW_TEXT_X,
y,
width: width - ROW_TEXT_X - 8,
height: height - 1,
selected,
disabled,
text: item.label,
ctx: this.paintCtx!,
});
} else {
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label,
disabled ? 70 : selected ? 255 : 200);
}
},
});
}
private section(): SettingsSection {
return this.sections[clamp(this.leftIndex, 0, this.sections.length - 1)]!;
return this.leftMenu.selectedItem ?? this.sections[0]!;
}
/** Select a left-column section by label (deep link, e.g. from an app's menu). */
focusSection(label: string): void {
const index = this.sections.findIndex((section) => section.label === label);
if (index < 0) return;
this.leftIndex = index;
this.leftMenu.select(index);
this.focus = "left";
this.resetRight();
}
@@ -71,27 +114,16 @@ export class SettingsPanelLayer implements Layer {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const appFocused = ctx.stack.isFocused();
this.leftIndex = clamp(this.leftIndex, 0, this.sections.length - 1);
const section = this.section();
const rightItems = section.items;
this.rightIndex = clamp(this.rightIndex, 0, Math.max(0, rightItems.length - 1));
this.rightMenu.setItems(rightItems);
const rowH = listRowHeight(font);
const top = PAD;
const listBottom = height - PAD;
// Left column: section labels.
const leftRows = Math.max(1, ((listBottom - top) / rowH) | 0);
this.leftScroll = clampScroll(this.leftIndex, this.leftScroll, leftRows, this.sections.length);
for (let i = this.leftScroll; i < Math.min(this.sections.length, this.leftScroll + leftRows); i++) {
const rowY = top + (i - this.leftScroll) * rowH;
const selected = i === this.leftIndex;
if (selected) {
drawSelectionHighlight(image, PAD - 2, rowY, LEFT_W, rowH - 2, appFocused && this.focus === "left", 6);
}
image.drawText(font, PAD + 8, rowY + LIST_ROW_TEXT_INSET, this.sections[i]!.label, selected ? 255 : 200);
}
this.leftMenu.paint(image, { x: PAD - 2, y: top, width: LEFT_W, height: listBottom - top },
appFocused && this.focus === "left");
// Column divider (a light rule, not a full border).
const divX = PAD + LEFT_W + 6;
@@ -108,38 +140,15 @@ export class SettingsPanelLayer implements Layer {
if (rightItems.length && rightTop < listBottom) {
// Once focus is in the right column, the selected item's extended
// description (when it has one) is drawn over the bottom of the pane by
// the companion SettingsDescriptionOverlayLayer. The row list keeps the
// full column height — rows the description band covers are drawn and
// partially occluded by the overlay's plane — but the scroll clamp uses
// the shrunk height so the selected row always stays clear of it.
const descriptionH = this.descriptionHeight(font, rightW);
const clampRows = Math.max(1, ((listBottom - descriptionH - rightTop) / rowH) | 0);
const rightRows = Math.max(1, ((listBottom - rightTop) / rowH) | 0);
this.rightScroll = clampScroll(this.rightIndex, this.rightScroll, clampRows, rightItems.length);
for (let i = this.rightScroll; i < Math.min(rightItems.length, this.rightScroll + rightRows); i++) {
const item = rightItems[i]!;
const rowY = rightTop + (i - this.rightScroll) * rowH;
// A selection only appears once focus is in the right column; before
// that the pane is a preview of what tapping would open.
const selected = this.focus === "right" && i === this.rightIndex;
const disabled = isMenuItemDisabled(item);
if (selected) {
drawSelectionHighlight(image, rightX - 2, rowY, rightW + 2, rowH - 2, appFocused, 6);
}
if (item.render) {
item.render({
image,
x: rightX + 8,
y: rowY,
width: rightW - 16,
height: rowH - 3,
selected,
disabled,
text: item.label,
ctx,
});
} else {
image.drawText(font, rightX + 8, rowY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
// the companion SettingsDescriptionOverlayLayer. The list stops above
// that band, so the selected row always stays clear of it.
const listHeight = listBottom - this.descriptionHeight(font, rightW) - rightTop;
if (listHeight > 0) {
this.paintCtx = ctx;
try {
this.rightMenu.paint(image, { x: rightX - 2, y: rightTop, width: rightW + 2, height: listHeight }, appFocused);
} finally {
this.paintCtx = null;
}
}
}
@@ -150,8 +159,7 @@ export class SettingsPanelLayer implements Layer {
/** The selected item's wrapped help text, or [] when none applies. */
private descriptionLines(font: UiFont, rightW: number): string[] {
if (this.focus !== "right") return [];
const items = this.section().items;
const item = items[clamp(this.rightIndex, 0, Math.max(0, items.length - 1))];
const item = this.rightMenu.selectedItem;
if (!item?.description) return [];
return wrapText(font, item.description, rightW - 4).slice(0, MAX_DESCRIPTION_LINES);
}
@@ -164,9 +172,8 @@ export class SettingsPanelLayer implements Layer {
/**
* Draw the selected item's help text band (separator + text on an opaque
* background) at the bottom of the right column. Called by the companion
* overlay layer with the overlay's own canvas: as a separate plane its
* raster covers the row list's glyphs, so it can partially occlude a row —
* which raster drawn into the panel's own image cannot do.
* overlay layer with the overlay's own canvas, so the band stays above
* whatever the panel paints (including a section's detail content).
*/
paintDescriptionOverlay(image: GrayImage, ctx: LayerContext): void {
const font = getDefaultSmallFont();
@@ -186,18 +193,15 @@ export class SettingsPanelLayer implements Layer {
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const scroll = scrollEventFor(event);
if (this.focus === "left") {
if (scroll) {
const before = this.leftMenu.selectedIndex;
await this.leftMenu.handleInput(scroll);
if (this.leftMenu.selectedIndex !== before) this.resetRight();
return;
}
switch (event.type) {
case "scroll-up":
case "swipe-up":
this.leftIndex = (this.leftIndex + this.sections.length - 1) % this.sections.length;
this.resetRight();
return;
case "scroll-down":
case "swipe-down":
this.leftIndex = (this.leftIndex + 1) % this.sections.length;
this.resetRight();
return;
case "click":
case "swipe-right":
// Only enter sections that have something interactive on the right.
@@ -215,38 +219,42 @@ export class SettingsPanelLayer implements Layer {
}
}
const items = this.section().items;
if (scroll) {
await this.rightMenu.handleInput(scroll);
return;
}
switch (event.type) {
case "scroll-up":
case "swipe-up":
if (items.length) this.rightIndex = (this.rightIndex + items.length - 1) % items.length;
return;
case "scroll-down":
case "swipe-down":
if (items.length) this.rightIndex = (this.rightIndex + 1) % items.length;
return;
case "click":
case "swipe-right":
if (items.length) {
const item = items[clamp(this.rightIndex, 0, items.length - 1)]!;
if (!isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
}
case "swipe-right": {
const item = this.rightMenu.selectedItem;
if (item && !isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
return;
}
case "double-click":
case "swipe-left":
// Back to a preview: the right column keeps its scroll but shows no selection.
this.focus = "left";
this.rightMenu.select(null);
return;
default:
return;
}
}
/** Show the highlighted section's items from the top, selected on the first only when focus is there. */
private resetRight(): void {
this.rightIndex = 0;
this.rightScroll = 0;
this.rightMenu.setItems(this.section().items, this.focus === "right" ? 0 : null);
this.rightMenu.scrollTop = 0;
}
}
/** Scroll and watch swipe-up/down both move a column's selection. */
function scrollEventFor(event: InputEvent): InputEvent | null {
if (event.type === "scroll-up" || event.type === "swipe-up") return { ...event, type: "scroll-up" } as InputEvent;
if (event.type === "scroll-down" || event.type === "swipe-down") return { ...event, type: "scroll-down" } as InputEvent;
return null;
}
/**
* Companion layer for SettingsPanelLayer, sitting permanently above it in the
* settings app's stack: paints the selected item's help text as its own plane
@@ -276,12 +284,3 @@ export class SettingsDescriptionOverlayLayer implements Layer {
export function openSettingsSubMenu(ctx: LayerContext, title: string, items: MenuItem[]): void {
openModalMenu(ctx, title, items);
}
function clampScroll(selected: number, scroll: number, visible: number, count: number): number {
if (selected < scroll) {
scroll = selected;
} else if (selected >= scroll + visible) {
scroll = selected - visible + 1;
}
return clamp(scroll, 0, Math.max(0, count - visible));
}
+33 -27
View File
@@ -23,7 +23,8 @@ import {
type UiFontSelection,
} from "../graphics/ui-fonts";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "./gestures";
import { drawRightValueMenuItem, drawSelectionHighlight, openModalMenu, type MenuItem } from "./menu";
import { drawRightValueMenuItem, openModalMenu, type MenuItem } from "./menu";
import { Menu, type MenuDrawArgs } from "./menu-core";
import { LIST_ROW_TEXT_INSET, listRowHeight } from "./metrics";
import type { Layer, LayerContext } from "./layers";
@@ -62,7 +63,14 @@ type RowId = (typeof ROWS)[number];
export class FontPickerLayer implements Layer {
private readonly families: FontFamily[];
private draft: Draft;
private selectedRow = 0;
private readonly menu = new Menu<RowId>({
items: ROWS,
wrap: true,
rowGap: 2,
highlight: { radius: 8 },
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawRow(args),
});
constructor(private readonly options: FontPickerOptions) {
this.families = collectFamilies(options.monospaceOnly);
@@ -111,26 +119,7 @@ export class FontPickerLayer implements Layer {
const rowWidth = width - 2 * rowX;
const rowHeight = listRowHeight(font);
const rowsTop = 24 + font.lineHeight;
for (let index = 0; index < ROWS.length; index++) {
const row = ROWS[index]!;
const y = rowsTop + index * rowHeight;
const disabled = this.rowDisabled(row);
if (index === this.selectedRow) {
drawSelectionHighlight(image, rowX - 10, y, rowWidth + 20, rowHeight - 2, focused, 8);
}
if (row === "save") {
image.drawText(font, rowX, y + LIST_ROW_TEXT_INSET, "Save", index === this.selectedRow ? 255 : 200);
continue;
}
const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size";
const value = this.rowValue(row);
if (disabled) {
image.drawText(font, rowX, y + LIST_ROW_TEXT_INSET, label, 70);
image.drawText(font, rowX + rowWidth - font.measureText(value) - 2, y + LIST_ROW_TEXT_INSET, value, 70);
} else {
drawRightValueMenuItem(image, font, rowX, y, rowWidth, label, value);
}
}
this.menu.paint(image, { x: rowX - 10, y: rowsTop, width: rowWidth + 20, height: ROWS.length * rowHeight }, focused);
// Preview: a separator, then the sample line in the draft font.
const previewTop = rowsTop + ROWS.length * rowHeight + 10;
@@ -145,6 +134,25 @@ export class FontPickerLayer implements Layer {
return image;
}
/** Row box = highlight box; text sits 10px in from its left edge and 10px from its right. */
private drawRow({ image, item: row, x, y, width, selected }: MenuDrawArgs<RowId>): void {
const font = getDefaultSmallFont();
const textX = x + 10;
const textWidth = width - 20;
if (row === "save") {
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, "Save", selected ? 255 : 200);
return;
}
const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size";
const value = this.rowValue(row);
if (this.rowDisabled(row)) {
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, label, 70);
image.drawText(font, textX + textWidth - font.measureText(value) - 2, y + LIST_ROW_TEXT_INSET, value, 70);
} else {
drawRightValueMenuItem(image, font, textX, y, textWidth, label, value);
}
}
private rowDisabled(row: RowId): boolean {
return (row === "weight" || row === "size") && this.draft.kind === "bitmap";
}
@@ -165,14 +173,12 @@ export class FontPickerLayer implements Layer {
handleInput(event: InputEvent, ctx: LayerContext): void {
switch (event.type) {
case "scroll-up":
this.selectedRow = (this.selectedRow + ROWS.length - 1) % ROWS.length;
return;
case "scroll-down":
this.selectedRow = (this.selectedRow + 1) % ROWS.length;
void this.menu.handleInput(event);
return;
case "click": {
const row = ROWS[this.selectedRow]!;
if (this.rowDisabled(row)) return;
const row = this.menu.selectedItem;
if (!row || this.rowDisabled(row)) return;
if (row === "face") this.openFaceMenu(ctx);
else if (row === "weight") this.openWeightMenu(ctx);
else if (row === "size") this.openSizeMenu(ctx);
+44 -11
View File
@@ -80,15 +80,20 @@ type MenuLayout = { tops: number[]; heights: number[]; total: number };
export class Menu<T> {
private itemList: readonly T[];
private selected: number | null;
/**
* The caller asked for no selection. Otherwise a null selection only means
* nothing was selectable, and the first selectable item is chosen as soon
* as one exists (a menu created empty and filled later starts on it).
*/
private deselected: boolean;
private scrollY = 0;
private lastBox: MenuBox | null = null;
private readonly motion = new MenuHighlightMotion();
constructor(private readonly options: MenuOptions<T>) {
this.itemList = options.items ?? [];
this.selected = options.selectedIndex === undefined
? this.nextSelectable(null, 1)
: this.resolve(options.selectedIndex);
this.deselected = options.selectedIndex === null;
this.selected = this.settle(options.selectedIndex ?? null);
}
get items(): readonly T[] {
@@ -110,6 +115,11 @@ export class Menu<T> {
return this.scrollY;
}
/** Move the viewport's top edge; the next paint clamps it and scrolls the selection back into view. */
set scrollTop(value: number) {
this.scrollY = Math.max(0, value | 0);
}
/** True when the content did not fit the box given to the last paint. */
get overflows(): boolean {
if (!this.lastBox) return false;
@@ -119,8 +129,8 @@ export class Menu<T> {
/**
* Replace the items. `selectedIndex` names the new selection (null for
* none); left out, the selection stays at the same index, clamped into
* range. The viewport keeps the selected row at the same on-screen
* position when it can.
* range, or lands on the first selectable item if there was none. The
* viewport keeps the selected row at the same on-screen position when it can.
*/
setItems(items: readonly T[], selectedIndex?: number | null): void {
const width = this.lastBox?.width;
@@ -128,15 +138,21 @@ export class Menu<T> {
? this.layout(width).tops[this.selected]! - this.scrollY
: null;
this.itemList = items;
this.selected = this.resolve(selectedIndex === undefined ? this.selected : selectedIndex);
if (selectedIndex !== undefined) this.deselected = selectedIndex === null;
this.selected = this.settle(selectedIndex === undefined ? this.selected : selectedIndex);
if (before !== null && this.selected !== null && width !== undefined) {
this.scrollY = Math.max(0, this.layout(width).tops[this.selected]! - before);
}
}
/** Move the selection; a non-selectable or out-of-range index lands on the nearest selectable one. */
/**
* Move the selection; a non-selectable or out-of-range index lands on the
* nearest selectable one. null clears it until the user scrolls or a
* number is selected.
*/
select(index: number | null): void {
this.selected = this.resolve(index);
this.deselected = index === null;
this.selected = this.settle(index);
}
/** Step the selection by one selectable row; handles wrap and the exit callbacks. */
@@ -147,6 +163,7 @@ export class Menu<T> {
if (next !== null) {
if (from !== null) this.motion.navigate(from, false);
this.selected = next;
this.deselected = false;
return;
}
if (from === null && this.scrollContent(delta)) return;
@@ -155,6 +172,7 @@ export class Menu<T> {
if (wrapped !== null && wrapped !== from) {
if (from !== null) this.motion.navigate(from, true);
this.selected = wrapped;
this.deselected = false;
} else if (from === null && this.lastBox) {
this.scrollY = delta > 0 ? 0 : this.maxScroll(this.layout(this.lastBox.width), this.lastBox.height);
}
@@ -258,9 +276,15 @@ export class Menu<T> {
return null;
}
/** Resolve a stored selection: null stays null only when the caller deselected. */
private settle(index: number | null): number | null {
if (index === null) return this.deselected ? null : this.nextSelectable(null, 1);
return this.resolve(index);
}
/** Clamp an index into range and onto a selectable row (nearest below, else nearest above). */
private resolve(index: number | null): number | null {
if (index === null || !this.itemList.length) return null;
private resolve(index: number): number | null {
if (!this.itemList.length) return null;
const clamped = Math.max(0, Math.min(this.itemList.length - 1, index | 0));
if (this.isSelectable(clamped)) return clamped;
return this.nextSelectable(clamped, 1) ?? this.nextSelectable(clamped, -1);
@@ -268,7 +292,7 @@ export class Menu<T> {
/** Re-validate the selection against the current items (they may have been edited in place). */
private reconcile(): void {
this.selected = this.resolve(this.selected);
this.selected = this.settle(this.selected);
}
private layout(width: number): MenuLayout {
@@ -312,6 +336,15 @@ export class Menu<T> {
}
}
}
// Never scroll past the end (setItems can leave the viewport there when
// rows are removed or inserted above the selection): pull back to the
// first row top that still shows the last row, so no partial row starts
// the list. The selected row stays visible, since it ends by the list's end.
const maxScroll = this.maxScroll(layout, viewportHeight);
if (target > maxScroll) {
const aligned = layout.tops.find((rowTop) => rowTop >= maxScroll);
target = aligned !== undefined && aligned <= target ? aligned : maxScroll;
}
this.scrollY = Math.max(0, target);
}
+1 -1
View File
@@ -1,4 +1,4 @@
export const MENU_HIGHLIGHT_DURATION_MS = 300;
export const MENU_HIGHLIGHT_DURATION_MS = 240;
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number; durationMs: number };
let nextToken = 1;
+22 -3
View File
@@ -6,7 +6,7 @@ import { Layer, LayerContext, PaintBelow } from "./layers";
import { Menu, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "./menu-core";
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
import { LIST_ROW_TEXT_INSET, centeredTextY, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
const DEFAULT_MENU_X = 8;
const DEFAULT_MENU_Y = 8;
const DEFAULT_MENU_WIDTH = 272;
@@ -148,7 +148,7 @@ export function drawSubmenuIndicator(
): void {
const arrow = ">";
const x = highlightX + highlightWidth - font.measureText(arrow) - 4;
const y = highlightY + (((highlightHeight - font.lineHeight) / 2) | 0);
const y = centeredTextY(font, highlightY, highlightHeight);
image.drawText(font, x, y, arrow, value);
}
@@ -206,7 +206,7 @@ export class MenuLayer implements Layer {
constructor(
private readonly title: string | null,
private readonly items: MenuItem[],
private items: MenuItem[],
private readonly layout: MenuLayout = {
x: DEFAULT_MENU_X,
y: DEFAULT_MENU_Y,
@@ -248,6 +248,25 @@ export class MenuLayer implements Layer {
return this;
}
/** The selected row's index, or null when nothing is selectable. */
get selectedIndex(): number | null {
return this.menu.selectedIndex;
}
get selectedItem(): MenuItem | null {
return this.menu.selectedItem;
}
/**
* Replace the rows in place, keeping scroll and highlight state. The
* selection follows Menu.setItems: pass the new index of the previously
* selected row, or leave it out to keep the same index, clamped.
*/
setItems(items: MenuItem[], selectedIndex?: number | null): void {
this.items = items;
this.menu.setItems(items, selectedIndex);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const rowHeight = listRowHeight(font);
+65 -5
View File
@@ -8,6 +8,8 @@
*
* The formulas reproduce the pre-TTF constants exactly at the 12px default:
* listRowHeight 20, tightRowHeight 16, lineStep 14, menuTitleHeight 16.
* tightRowHeight follows the measured ink rather than the line box, which
* for Terminus is the same thing.
*/
import type { UiFont } from "../graphics/image";
@@ -20,13 +22,71 @@ export function listRowHeight(font: UiFont): number {
export const LIST_ROW_TEXT_INSET = 4;
/**
* Height of one row in dense lists (file browser, track lists). Grows at
* half the lineHeight's rate above the 12px anchor: TTF line heights carry
* internal leading that already reads as row spacing, so full-rate growth
* made large-font rows feel too tall.
* Characters whose ink bounds a font's text rows: accented capitals (the
* highest Latin marks), descenders, brackets, and digits.
*/
const INK_SAMPLE = "ÅÉÎÖÇÑbdfhklgjpqy()[]{}|/_0123456789";
export type TextInkBounds = {
/** First inked row, relative to the drawText y (the line top); may be negative. */
top: number;
/** One past the last inked row, relative to the line top; may exceed lineHeight. */
bottom: number;
};
const inkBoundsCache = new WeakMap<UiFont, TextInkBounds>();
/**
* The vertical extent of a font's ink over a representative sample, measured
* from the rendered glyphs. Bitmap faces fill their line box exactly; TTF
* faces may leave internal leading inside it or overshoot it with marks.
* Falls back to the line box when the font renders no sample ink.
*/
export function textInkBounds(font: UiFont): TextInkBounds {
const cached = inkBoundsCache.get(font);
if (cached) return cached;
let top = Infinity;
let bottom = -Infinity;
if (typeof font.getGlyph === "function") {
for (const char of INK_SAMPLE) {
const glyph = font.getGlyph(char.codePointAt(0)!);
if (!glyph || glyph.bbxHeight <= 0) continue;
// Same placement as rasterizeGlyph: baseline at y + ascent, bbxY up from it.
const glyphTop = font.ascent - (glyph.bbxHeight + glyph.bbxY);
top = Math.min(top, glyphTop);
bottom = Math.max(bottom, glyphTop + glyph.bbxHeight);
}
}
const bounds = top < bottom ? { top, bottom } : { top: 0, bottom: font.lineHeight };
inkBoundsCache.set(font, bounds);
return bounds;
}
/** Height of a font's ink extent (see textInkBounds). */
export function textInkHeight(font: UiFont): number {
const { top, bottom } = textInkBounds(font);
return bottom - top;
}
/**
* The drawText y that centers a font's ink vertically in a box, rounding the
* spare pixel to the bottom. Pair with row heights derived from
* textInkHeight so the ink fits whatever the face's internal leading.
*/
export function centeredTextY(font: UiFont, boxTop: number, boxHeight: number): number {
const { top } = textInkBounds(font);
return boxTop + Math.floor((boxHeight - textInkHeight(font)) / 2) - top;
}
/**
* Row pitch in dense lists (file browser, track lists): the font's ink height
* plus 4px, so a selection box one pixel shorter than the pitch keeps at
* least a pixel of clearance around the ink. Draw the text with
* centeredTextY; a fixed offset from the row top can overflow faces whose
* ink starts below their line top.
*/
export function tightRowHeight(font: UiFont): number {
return font.lineHeight + 4 - Math.floor(Math.max(0, font.lineHeight - 12) / 2);
return textInkHeight(font) + 4;
}
/** Step between consecutive lines of body/paragraph text. */
+25 -19
View File
@@ -7,35 +7,41 @@ import { type InputEvent } from "./gestures";
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
/** Rebuild rows when new sources arrive, keeping the current selection by package. */
/** Refresh rows as new sources arrive, keeping the current selection by package. */
export class NotificationFilterLayer implements Layer {
private menu: MenuLayer | null = null;
private packages = "";
private selectedPackage = "";
private menuWidth = 0;
/** The package behind each current row, parallel to the menu's items. */
private packages: string[] = [];
private currentMenu(ctx: LayerContext): MenuLayer {
readActiveNotifications(ALL_NOTIFICATIONS);
const sources = readNotificationSources().sort((a, b) =>
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
const signature = JSON.stringify([ctx.stack.getBaseSize().width, sources.map((source) => [source.packageName, source.appName])]);
if (!this.menu || signature !== this.packages) {
this.packages = signature;
const items: MenuItem[] = sources.map((source) => ({
label: source.appName,
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
render: ({ image, x, y, width, selected }) => {
if (selected) this.selectedPackage = source.packageName;
const font = getDefaultSmallFont();
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
shouldShowNotificationOnGlasses(source.packageName), selected);
},
}));
if (!items.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} });
const items: MenuItem[] = sources.map((source) => ({
label: source.appName,
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
render: ({ image, x, y, width, selected }) => {
const font = getDefaultSmallFont();
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
shouldShowNotificationOnGlasses(source.packageName), selected);
},
}));
if (!items.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} });
const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
const index = sources.findIndex((source) => source.packageName === selectedPackage);
this.packages = sources.map((source) => source.packageName);
const width = ctx.stack.getBaseSize().width;
if (!this.menu || width !== this.menuWidth) {
this.menuWidth = width;
this.menu = new MenuLayer("Notification filter", items, {
x: 8, y: 8, width: ctx.stack.getBaseSize().width - 16,
x: 8, y: 8, width: width - 16,
opaque: true, showBorder: false,
footer: "On = show popups on glasses",
}).selectItem(Math.max(0, sources.findIndex((source) => source.packageName === this.selectedPackage)));
}).selectItem(Math.max(0, index));
} else {
this.menu.setItems(items, index >= 0 ? index : undefined);
}
return this.menu;
}
+85 -58
View File
@@ -4,7 +4,7 @@ import { getDefaultSmallFont } from "../graphics/ui-fonts";
import { truncateText } from "../graphics/textwrap";
import { GrayImage, type UiFont } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { lineStep, listRowHeight } from "./metrics";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "./metrics";
import {
ALL_NOTIFICATIONS,
dismissNotification,
@@ -19,7 +19,7 @@ import { notificationEmptyMessage } from "../native/notification-access";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../util/render-freshness";
import { type InputEvent } from "./gestures";
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
import { MenuLayer } from "./menu";
import { Menu, type MenuBox } from "./menu-core";
const PAGE_X = 12;
const PAGE_Y = 12;
@@ -35,7 +35,19 @@ const CARD_GAP = 6;
const MAX_NOTIFICATIONS = 50;
// Right-hand action menu of the detail view.
const DETAIL_MENU_WIDTH = 148;
/** Top of the action menu's first row box, and the space kept below its last. */
const DETAIL_MENU_TOP = 22;
const DETAIL_MENU_BOTTOM_MARGIN = 17;
const DETAIL_MENU_ROW_GAP = 3;
/** Row text inset from the row box: horizontal, then vertical. */
const DETAIL_MENU_TEXT_X = 8;
const DETAIL_MENU_TEXT_Y = 4;
const DETAIL_CONTENT_X = 24;
// Disable-source confirmation: actions on top, the explanation below them.
const CONFIRM_X = 24;
const CONFIRM_Y = 8;
const CONFIRM_TEXT_X = 10;
const CONFIRM_PROMPT_GAP = 16;
/** Icon for a paint pass: allow-stale, reporting staleness to the render loop. */
function iconForNotification(key: string): GrayImage | null {
@@ -58,6 +70,8 @@ type DetailMenuItem =
| { kind: "dismiss"; label: string }
| { kind: "disable-source"; label: string };
type ConfirmationItem = { label: string; run: (ctx: LayerContext) => void };
export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal";
type SingleNotificationLayerOptions = {
@@ -168,8 +182,24 @@ export class NotificationsListLayer implements Layer {
* new-notification modal.
*/
export class SingleNotificationLayer implements Layer {
private selectedMenuIndex = 0;
private confirmation: { source: AndroidNotification; menu: MenuLayer } | null = null;
/** The action column; its items are rebuilt from the live notification on every paint and input. */
private readonly detailMenu = new Menu<DetailMenuItem>({
wrap: false,
rowGap: DETAIL_MENU_ROW_GAP,
highlight: { radius: 6 },
getHeight: (item, width) => {
const font = getDefaultSmallFont();
return detailMenuRowLines(font, item, width).length * lineStep(font) + 8;
},
draw: ({ image, item, x, y, width, selected }) => {
const font = getDefaultSmallFont();
const lines = detailMenuRowLines(font, item, width);
for (let line = 0; line < lines.length; line++) {
image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185);
}
},
});
private confirmation: { source: AndroidNotification; menu: Menu<ConfirmationItem> } | null = null;
constructor(
private readonly notificationKey: string,
@@ -187,17 +217,18 @@ export class SingleNotificationLayer implements Layer {
return this.closeUnavailableNotification(ctx, paintBelow);
}
const menu = buildDetailMenu(notification, this.options.origin);
this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1));
this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin));
drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height);
drawDetailMenu(image, font, menu, this.selectedMenuIndex, width);
this.detailMenu.paint(image, detailMenuBox(width, height), ctx.stack.isFocused());
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (this.confirmation) {
const { menu } = this.confirmation;
if (event.type === "double-click") this.close(ctx);
else await this.confirmation.menu.handleInput(event, ctx);
else if (event.type === "click") menu.selectedItem?.run(ctx);
else await menu.handleInput(event);
return;
}
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey);
@@ -205,24 +236,21 @@ export class SingleNotificationLayer implements Layer {
this.closeUnavailableNotification(ctx);
return;
}
const menu = buildDetailMenu(notification, this.options.origin);
this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, menu.length - 1);
this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin));
if (event.type === "double-click") {
this.close(ctx);
return;
}
if (event.type === "scroll-up") {
this.selectedMenuIndex = Math.max(0, this.selectedMenuIndex - 1);
return;
}
if (event.type === "scroll-down") {
this.selectedMenuIndex = Math.min(menu.length - 1, this.selectedMenuIndex + 1);
if (event.type === "scroll-up" || event.type === "scroll-down") {
await this.detailMenu.handleInput(event);
return;
}
if (event.type !== "click") return;
const item = menu[this.selectedMenuIndex]!;
// "(unavailable)" actions stay clickable: Android decides whether they do anything.
const item = this.detailMenu.selectedItem;
if (!item) return;
if (item.kind === "back") {
this.close(ctx);
} else if (item.kind === "action") {
@@ -237,13 +265,21 @@ export class SingleNotificationLayer implements Layer {
// Keep the modal alive for confirmation even after Android removes the notification.
this.confirmation = {
source: notification,
menu: new MenuLayer(null, [
{ label: "Cancel", onSelect: () => this.close(ctx) },
{ label: "Turn off popups", onSelect: () => {
setNotificationSourceEnabled(notification, false);
this.close(ctx);
} },
], { x: 12, y: 0, width: ctx.stack.getBaseSize().width - 24, minHeight: 0, showBorder: false }),
menu: new Menu<ConfirmationItem>({
items: [
{ label: "Cancel", run: (runCtx) => this.close(runCtx) },
{ label: "Turn off popups", run: (runCtx) => {
setNotificationSourceEnabled(notification, false);
this.close(runCtx);
} },
],
wrap: true,
rowGap: 1,
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: ({ image, item, x, y, selected }) => {
image.drawText(getDefaultSmallFont(), x + CONFIRM_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200);
},
}),
};
dismissNotification(this.notificationKey);
}
@@ -255,15 +291,21 @@ export class SingleNotificationLayer implements Layer {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// Put actions first so they remain reachable even with a large UI font.
menu.paint(ctx, () => image);
const top = 24 + 2 * listRowHeight(font);
const menuBox: MenuBox = {
x: CONFIRM_X,
y: CONFIRM_Y,
width: width - 2 * CONFIRM_X,
height: Math.min(menu.items.length * listRowHeight(font), height - CONFIRM_Y),
};
menu.paint(image, menuBox, ctx.stack.isFocused());
const top = menuBox.y + menuBox.height + CONFIRM_PROMPT_GAP;
const prompt = `Turn off notification popups from ${source.appName || source.packageName}? You can turn them back on in the Notifications app's Notification filter.`;
const lines = wrapText(font, prompt, width - 48);
const lines = wrapText(font, prompt, width - 2 * CONFIRM_X);
const maxLines = Math.max(1, Math.floor((height - top - 12) / lineStep(font)));
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
const text = index === maxLines - 1 && lines.length > maxLines
? truncateText(font, lines[index] + "...", width - 48) : lines[index]!;
image.drawText(font, 24, top + index * lineStep(font), text, 210);
? truncateText(font, lines[index] + "...", width - 2 * CONFIRM_X) : lines[index]!;
image.drawText(font, CONFIRM_X, top + index * lineStep(font), text, 210);
}
return image;
}
@@ -404,35 +446,20 @@ function drawDetailContent(
}
}
function drawDetailMenu(image: GrayImage, font: UiFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void {
const menuX = width - DETAIL_MENU_WIDTH - 24;
const menuY = 24;
const rows = menu.map((item) => {
const lines = item.kind === "disable-source"
? wrapText(font, item.label, DETAIL_MENU_WIDTH - 16)
: [truncateText(font, item.label, DETAIL_MENU_WIDTH - 16)];
return { lines, height: lines.length * lineStep(font) + 8 };
});
const selectedBottom = rows.slice(0, selectedIndex + 1).reduce((sum, row) => sum + row.height, 0);
const scrollY = Math.max(0, selectedBottom - (image.height - menuY - 12));
let y = menuY - scrollY;
for (let index = 0; index < menu.length; index++) {
const row = rows[index]!;
if (y < menuY) {
y += row.height;
continue;
}
if (y + row.height > image.height - 12) break;
const selected = index === selectedIndex;
if (selected) {
image.fillRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, row.height - 3, 18, 6);
image.drawRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, row.height - 3, 60, 6);
}
for (let line = 0; line < row.lines.length; line++) {
image.drawText(font, menuX, y + 2 + line * lineStep(font), row.lines[line]!, selected ? 255 : 185);
}
y += row.height;
}
/** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */
function detailMenuBox(width: number, height: number): MenuBox {
return {
x: width - DETAIL_MENU_WIDTH - 32,
y: DETAIL_MENU_TOP,
width: DETAIL_MENU_WIDTH,
height: height - DETAIL_MENU_TOP - DETAIL_MENU_BOTTOM_MARGIN,
};
}
/** An action row's text lines: the disable-source row wraps, the rest truncate to one line. */
function detailMenuRowLines(font: UiFont, item: DetailMenuItem, rowWidth: number): string[] {
const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X;
return item.kind === "disable-source" ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)];
}
function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] {
+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();
}