forked from Blendman974/faceclaw
Use animated menus in a bunch more places
This commit is contained in:
1 parent
123c1142d6
commit
71c1971992
37 files changed
+1457
-1005
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user