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));
}