Better menu class; use it in the Music app

This commit is contained in:
James Babcock committed 2026-09-23 22:05:44 -07:00
1 parent 440674b261
commit 123c1142d6
9 files changed
+694 -163

No files matched your search

+56 -86
View File
@@ -3,7 +3,8 @@ import { GrayImage } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures";
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu";
import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { lineStep, tightRowHeight } from "../../ui/metrics";
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
@@ -28,6 +29,8 @@ const ACTION_X = 22;
const ACTION_WIDTH = 154;
const COLUMN_DIVIDER_X = 190;
const QUEUE_X = 204;
/** Horizontal inset of row text from its selection box. */
const LIST_TEXT_INSET = 6;
const PLAYLIST_ACTION_INDEX = 1;
type MusicAction =
@@ -49,9 +52,20 @@ class MusicAppLayer implements Layer {
// Watch swipes skip tracks: right = next, left = previous.
readonly acceptsDirectional = true;
private focusColumn: FocusColumn = "actions";
private selectedActionIndex = 0;
private selectedQueueIndex = 0;
private queueScrollRow = 0;
private readonly actionsMenu = new Menu<MusicAction>({
wrap: true,
rowGap: 1,
highlight: { radius: 4 },
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawAction(args),
});
private readonly queueMenu = new Menu<MediaQueueItem>({
wrap: false,
rowGap: 1,
highlight: { radius: 4 },
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawQueueItem(args),
});
private art: GrayImage | null = null;
private artKey = "";
@@ -124,83 +138,49 @@ class MusicAppLayer implements Layer {
const actions = this.buildActions(media, queue);
this.reconcileSelection(actions, queue);
const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN;
const rowH = tightRowHeight(font);
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
if (this.selectedQueueIndex < this.queueScrollRow) {
this.queueScrollRow = this.selectedQueueIndex;
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
}
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
for (let index = 0; index < actions.length; index++) {
const action = actions[index]!;
const y = LIST_TOP + index * rowH;
const selected = index === this.selectedActionIndex;
const highlightX = ACTION_X - 6;
const highlightY = y - 1;
const highlightWidth = ACTION_WIDTH + 8;
const highlightHeight = rowH - 1;
if (selected) {
drawSelectionHighlight(
image,
highlightX,
highlightY,
highlightWidth,
highlightHeight,
ctx.stack.isFocused() && this.focusColumn === "actions",
4,
);
}
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value);
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
}
}
const focused = ctx.stack.isFocused();
// Row boxes start one pixel above LIST_TOP so the text lands at LIST_TOP + 1.
this.actionsMenu.paint(
image,
{ x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight },
focused && this.focusColumn === "actions",
);
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - LIST_BOTTOM_MARGIN - 3, 45);
if (!queue.length) {
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
} else {
const queueWidth = width - QUEUE_X - 20;
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
for (let index = this.queueScrollRow; index < lastVisible; index++) {
const item = queue[index]!;
const y = LIST_TOP + (index - this.queueScrollRow) * rowH;
const selected = index === this.selectedQueueIndex;
if (selected) {
drawSelectionHighlight(
image,
QUEUE_X - 6,
y - 1,
queueWidth + 8,
rowH - 1,
ctx.stack.isFocused() && this.focusColumn === "playlist",
4,
);
}
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
}
if (queue.length > visibleRows) {
const trackHeight = visibleRows * rowH - 4;
const trackX = width - 12;
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
const maxScrollRow = queue.length - visibleRows;
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
}
this.queueMenu.paint(
image,
{ x: QUEUE_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: queueWidth + 2 * LIST_TEXT_INSET - 4, height: listHeight },
focused && this.focusColumn === "playlist",
);
this.queueMenu.drawScrollbar(image, width - 12, LIST_TOP, listHeight - 4);
}
return image;
}
private drawAction({ image, item: action, x, y, width, height, selected }: MenuDrawArgs<MusicAction>): void {
const font = getDefaultSmallFont();
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, x + LIST_TEXT_INSET, y + 2, truncateText(font, action.label, ACTION_WIDTH - 16), value);
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, x, y, width, height, value);
}
}
private drawQueueItem({ image, item, x, y, width, selected }: MenuDrawArgs<MediaQueueItem>): void {
const font = getDefaultSmallFont();
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, x + LIST_TEXT_INSET, y + 2, truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200);
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click" && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
this.actionsMenu.select(PLAYLIST_ACTION_INDEX);
return;
}
const media = mediaControllerBridge.snapshot();
@@ -237,31 +217,20 @@ class MusicAppLayer implements Layer {
case "swipe-down":
return this.handleInput({ type: "scroll-down", timestampMs: event.timestampMs }, ctx);
case "scroll-up":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length;
}
return;
case "scroll-down":
if (this.focusColumn === "playlist") {
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
} else {
this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length;
}
await (this.focusColumn === "playlist" ? this.queueMenu : this.actionsMenu).handleInput(event);
return;
case "click": {
if (this.focusColumn === "playlist") {
const item = queue[this.selectedQueueIndex];
const item = this.queueMenu.selectedItem;
if (item) {
await mediaControllerBridge.skipToQueueItem(item.id);
// The player rebuilds the queue with the chosen track at index 0.
this.selectedQueueIndex = 0;
this.queueScrollRow = 0;
this.queueMenu.select(0);
}
return;
}
const action = actions[this.selectedActionIndex];
const action = this.actionsMenu.selectedItem;
if (!action || !action.enabled) return;
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
@@ -271,7 +240,7 @@ class MusicAppLayer implements Layer {
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
} else if (action.kind === "playlist") {
const activeIndex = queue.findIndex((item) => item.active);
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
if (activeIndex >= 0) this.queueMenu.select(activeIndex);
this.focusColumn = "playlist";
}
return;
@@ -332,12 +301,13 @@ class MusicAppLayer implements Layer {
];
}
/** Hand the menus the freshly built rows; each keeps its selection index, clamped. */
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
this.actionsMenu.setItems(actions);
this.queueMenu.setItems(queue);
if (!queue.length && this.focusColumn === "playlist") {
this.focusColumn = "actions";
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
this.actionsMenu.select(PLAYLIST_ACTION_INDEX);
}
}
+335
View File
@@ -0,0 +1,335 @@
import { GrayImage } from "../graphics/image";
import type { InputEvent } from "./gestures";
import { MenuHighlightMotion } from "./menu-highlight-motion";
/** Selected-row fill while the menu owns input; the outline alone marks an unfocused menu's selection. */
export const MENU_HIGHLIGHT_FILL = 15;
export const MENU_HIGHLIGHT_STROKE = 45;
const SCROLLBAR_TRACK_VALUE = 30;
const SCROLLBAR_THUMB_VALUE = 120;
const SCROLLBAR_WIDTH = 3;
const SCROLLBAR_MIN_THUMB = 8;
/** A rectangle in image coordinates. */
export type MenuBox = { x: number; y: number; width: number; height: number };
export type MenuDrawArgs<T> = {
/**
* Draw into this image, inside the (x, y, width, height) rect. For the
* selected row this is a scratch image exactly the rect's size (so x and y
* are 0), replayed over the highlight. Never draw relative to image.width
* or image.height, and never outside the rect.
*/
image: GrayImage;
item: T;
index: number;
x: number;
y: number;
width: number;
height: number;
selected: boolean;
/** Whether the menu owns input right now: the `focused` flag the host passed to paint. */
focused: boolean;
};
export type MenuHighlightStyle = {
/** Corner radius of the selection box. Default 8. */
radius?: number;
/** Stereo depth of the selection box. Default 0. */
depth?: number;
};
export type MenuOptions<T> = {
items?: readonly T[];
/** Initial selection. Default: the first selectable item. */
selectedIndex?: number | null;
/** Row pitch in pixels (including rowGap) for an item laid out at this width. */
getHeight: (item: T, width: number) => number;
/** Default: every item is selectable. Non-selectable items (group labels) are skipped by navigation. */
isSelectable?: (item: T) => boolean;
draw: (args: MenuDrawArgs<T>) => void;
/** Called by activate() / a click with the selected item. */
onSelect?: (item: T, index: number) => void | Promise<void>;
/**
* Whether scrolling past either end wraps to the other. When false the
* selection stays put and onExitTop / onExitBottom is called instead.
*/
wrap?: boolean;
onExitTop?: () => void;
onExitBottom?: () => void;
/** Pixels between consecutive rows, excluded from each row's drawable box and highlight. Default 0. */
rowGap?: number;
/** Selection box style, or false to draw no highlight (the draw callback shows the selection itself). */
highlight?: MenuHighlightStyle | false;
};
type MenuLayout = { tops: number[]; heights: number[]; total: number };
/**
* A vertical list with one selected row: the menu proper, independent of the
* Layer stack. A host paints it into a rect of its own image and forwards
* scroll / click events while the menu has focus; the host keeps the rest
* (title, box, focus between several menus, back navigation).
*
* Rows may vary in height and some may be unselectable; navigation skips
* those. Scrolling is pixel-based: the viewport moves the minimum needed to
* keep the selection fully visible, then aligns to a row top so the list
* never starts with a partially visible row. Rows that don't fully fit at
* the bottom are not drawn.
*/
export class Menu<T> {
private itemList: readonly T[];
private selected: number | null;
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);
}
get items(): readonly T[] {
return this.itemList;
}
get selectedIndex(): number | null {
this.reconcile();
return this.selected;
}
get selectedItem(): T | null {
const index = this.selectedIndex;
return index === null ? null : this.itemList[index]!;
}
/** Content offset of the viewport's top edge, in pixels. */
get scrollTop(): number {
return this.scrollY;
}
/** True when the content did not fit the box given to the last paint. */
get overflows(): boolean {
if (!this.lastBox) return false;
return this.layout(this.lastBox.width).total > this.lastBox.height;
}
/**
* 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.
*/
setItems(items: readonly T[], selectedIndex?: number | null): void {
const width = this.lastBox?.width;
const before = this.selected !== null && width !== undefined
? this.layout(width).tops[this.selected]! - this.scrollY
: null;
this.itemList = items;
this.selected = this.resolve(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. */
select(index: number | null): void {
this.selected = this.resolve(index);
}
/** Step the selection by one selectable row; handles wrap and the exit callbacks. */
moveSelection(delta: -1 | 1): void {
this.reconcile();
const from = this.selected;
const next = this.nextSelectable(from, delta);
if (next !== null) {
if (from !== null) this.motion.navigate(from, false);
this.selected = next;
return;
}
if (from === null && this.scrollContent(delta)) return;
if (this.options.wrap) {
const wrapped = this.nextSelectable(null, delta);
if (wrapped !== null && wrapped !== from) {
if (from !== null) this.motion.navigate(from, true);
this.selected = wrapped;
} else if (from === null && this.lastBox) {
this.scrollY = delta > 0 ? 0 : this.maxScroll(this.layout(this.lastBox.width), this.lastBox.height);
}
return;
}
if (delta > 0) this.options.onExitBottom?.();
else this.options.onExitTop?.();
}
/** Invoke onSelect for the selected item. Returns false when nothing is selected. */
async activate(): Promise<boolean> {
this.reconcile();
if (this.selected === null) return false;
await this.options.onSelect?.(this.itemList[this.selected]!, this.selected);
return true;
}
/** Handle scroll-up, scroll-down and click. Returns false for any other event, or a click with nothing selected. */
async handleInput(event: InputEvent): Promise<boolean> {
switch (event.type) {
case "scroll-up":
this.moveSelection(-1);
return true;
case "scroll-down":
this.moveSelection(1);
return true;
case "click":
return this.activate();
default:
return false;
}
}
/** The selectable item under image-coordinate y in the last painted box, for touch/mirror input. */
indexAt(y: number): number | null {
const box = this.lastBox;
if (!box || y < box.y || y >= box.y + box.height) return null;
const layout = this.layout(box.width);
const contentY = y - box.y + this.scrollY;
for (let index = 0; index < this.itemList.length; index++) {
if (contentY >= layout.tops[index]! && contentY < layout.tops[index]! + layout.heights[index]!) {
return this.isSelectable(index) ? index : null;
}
}
return null;
}
paint(image: GrayImage, box: MenuBox, focused: boolean): void {
this.lastBox = box;
this.reconcile();
const layout = this.layout(box.width);
this.ensureVisible(layout, box.height);
const gap = this.options.rowGap ?? 0;
const highlight = this.options.highlight;
for (let index = 0; index < this.itemList.length; index++) {
const top = layout.tops[index]! - this.scrollY;
const height = layout.heights[index]! - gap;
const selected = index === this.selected;
if (top < 0 || top >= box.height) continue;
// Rows cut off by the bottom edge are not drawn, except a selected row
// taller than the box, which has nowhere better to be.
if (top + height > box.height && !selected) continue;
const rowY = box.y + top;
const item = this.itemList[index]!;
if (selected && highlight !== false) {
const row = new GrayImage(box.width, height, 0);
this.options.draw({ image: row, item, index, x: 0, y: 0, width: box.width, height, selected, focused });
const animation = this.motion.paint(index, this.scrollY, box.x, rowY, box.width, height, Date.now());
image.drawMenuSelection(row, box.x, rowY, focused ? MENU_HIGHLIGHT_FILL : 0, MENU_HIGHLIGHT_STROKE,
highlight?.radius ?? 8, highlight?.depth ?? 0, animation);
} else {
this.options.draw({ image, item, index, x: box.x, y: rowY, width: box.width, height, selected, focused });
}
}
}
/** Draw a vertical scrollbar for the last painted box (no-op unless the content overflows it). */
drawScrollbar(image: GrayImage, x: number, y: number, height: number): void {
const box = this.lastBox;
if (!box) return;
const layout = this.layout(box.width);
if (layout.total <= box.height) return;
const maxScroll = this.maxScroll(layout, box.height);
const thumbHeight = Math.max(SCROLLBAR_MIN_THUMB, (height * box.height / layout.total) | 0);
const fraction = Math.min(1, Math.max(0, this.scrollY) / maxScroll);
image.fillRect(x, y, SCROLLBAR_WIDTH, height, SCROLLBAR_TRACK_VALUE);
image.fillRect(x, y + (((height - thumbHeight) * fraction) | 0), SCROLLBAR_WIDTH, thumbHeight, SCROLLBAR_THUMB_VALUE);
}
private isSelectable(index: number): boolean {
return this.options.isSelectable?.(this.itemList[index]!) ?? true;
}
/** The next selectable index after `from` in direction `delta`; from null, the first (or last) selectable. */
private nextSelectable(from: number | null, delta: -1 | 1): number | null {
const count = this.itemList.length;
let index = from === null ? (delta > 0 ? 0 : count - 1) : from + delta;
for (; index >= 0 && index < count; index += delta) {
if (this.isSelectable(index)) return index;
}
return null;
}
/** 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;
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);
}
/** Re-validate the selection against the current items (they may have been edited in place). */
private reconcile(): void {
this.selected = this.resolve(this.selected);
}
private layout(width: number): MenuLayout {
const tops: number[] = [];
const heights: number[] = [];
let total = 0;
for (const item of this.itemList) {
const height = Math.max(0, this.options.getHeight(item, width) | 0);
tops.push(total);
heights.push(height);
total += height;
}
if (this.itemList.length) total -= this.options.rowGap ?? 0;
return { tops, heights, total };
}
private maxScroll(layout: MenuLayout, viewportHeight: number): number {
return Math.max(0, layout.total - viewportHeight);
}
/** Scroll the viewport so the selected row is fully inside it, aligning the top to a row edge when it moves. */
private ensureVisible(layout: MenuLayout, viewportHeight: number): void {
if (this.selected === null) {
this.scrollY = Math.max(0, Math.min(this.maxScroll(layout, viewportHeight), this.scrollY));
return;
}
const top = layout.tops[this.selected]!;
const bottom = top + layout.heights[this.selected]! - (this.options.rowGap ?? 0);
let target = this.scrollY;
if (top < target) {
target = top;
} else if (bottom > target + viewportHeight) {
// A row taller than the viewport shows from its top.
target = Math.min(top, bottom - viewportHeight);
// Snap to the first row that starts inside the viewport: the selected
// row still fits (its bottom is within viewportHeight of that top).
for (let index = 0; index <= this.selected; index++) {
if (layout.tops[index]! >= target) {
target = layout.tops[index]!;
break;
}
}
}
this.scrollY = Math.max(0, target);
}
/** With nothing selectable, up/down page the content by rows. Returns false at the end being scrolled past. */
private scrollContent(delta: -1 | 1): boolean {
if (!this.lastBox) return false;
const layout = this.layout(this.lastBox.width);
const maxScroll = this.maxScroll(layout, this.lastBox.height);
if (delta > 0) {
if (this.scrollY >= maxScroll) return false;
const next = layout.tops.find((top) => top > this.scrollY);
this.scrollY = Math.min(maxScroll, next ?? maxScroll);
} else {
if (this.scrollY <= 0) return false;
let previous = 0;
for (const top of layout.tops) if (top < this.scrollY) previous = top;
this.scrollY = Math.max(0, previous);
}
return true;
}
}
+53 -76
View File
@@ -1,9 +1,9 @@
import { MenuHighlightMotion } from "./menu-highlight-motion";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type UiFont } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { getDefaultSmallFont } from "../graphics/ui-fonts";
import { clamp } from "../util/numeric-util";
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";
@@ -16,8 +16,10 @@ const DEFAULT_MENU_MIN_HEIGHT = G2_LENS_HEIGHT / 2 - 2 * DEFAULT_MENU_Y;
const MENU_BODY_PADDING = 8;
/** Gap between the last item row and a footer hint line. */
const MENU_FOOTER_GAP = 8;
const MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL = 15;
const MENU_HIGHLIGHT_SELECTED_BORDER_STROKE = 45;
/** Horizontal inset of the row (highlight) boxes from the menu box's edges. */
const MENU_ROW_INSET = 12;
/** Horizontal inset of row text from the row box. */
const MENU_ROW_TEXT_INSET = 10;
export type MenuLayout = {
/** Stereo depth of the menu surface, including the selected row. */
@@ -85,9 +87,9 @@ export function drawSelectionHighlight(
radius = 6,
): void {
if (focused) {
image.fillRoundedRect(x, y, width, height, MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL, radius);
image.fillRoundedRect(x, y, width, height, MENU_HIGHLIGHT_FILL, radius);
}
image.drawRoundedRect(x, y, width, height, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, radius);
image.drawRoundedRect(x, y, width, height, MENU_HIGHLIGHT_STROKE, radius);
}
/**
@@ -192,9 +194,9 @@ export function drawRightValueMenuItem(
export const CONTEXT_MENU_DIM = 0.25;
export class MenuLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
private readonly highlightMotion = new MenuHighlightMotion();
private readonly menu: Menu<MenuItem>;
/** The context of the paint in progress, handed to item render callbacks. */
private paintCtx: LayerContext | null = null;
get depth(): number { return this.layout.depth ?? 0; }
@@ -211,11 +213,38 @@ export class MenuLayer implements Layer {
width: DEFAULT_MENU_WIDTH,
},
public readonly paintOverBase = false,
) {}
) {
this.menu = new Menu<MenuItem>({
items,
wrap: true,
rowGap: 1,
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: ({ image, item, x, y, width, height, selected }) => {
const font = getDefaultSmallFont();
const disabled = isMenuItemDisabled(item);
if (item.render) {
item.render({
image,
x: x + MENU_ROW_TEXT_INSET,
y,
width: width - 2 * MENU_ROW_TEXT_INSET,
height: height - 2,
selected,
disabled,
text: item.label,
ctx: this.paintCtx!,
});
} else {
image.drawText(font, x + MENU_ROW_TEXT_INSET, y + LIST_ROW_TEXT_INSET, item.label,
disabled ? 70 : selected ? 255 : 200);
}
},
});
}
/** Start a newly opened picker on its current value. */
selectItem(index: number): this {
this.selectedIndex = clamp(index, 0, Math.max(0, this.items.length - 1));
this.menu.select(index);
return this;
}
@@ -242,12 +271,6 @@ export class MenuLayer implements Layer {
1,
((height - chromeTop - footerHeight - MENU_BODY_PADDING) / rowHeight) | 0,
);
this.scrollRow = scrollToKeepSelectionVisible(
this.scrollRow,
this.selectedIndex,
visibleRowCount,
this.items.length,
);
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
// transparent color key when a menu paints on the shell surface.
@@ -265,78 +288,32 @@ export class MenuLayer implements Layer {
}
const bodyY = y + chromeTop;
const focused = ctx.stack.isFocused();
const lastVisibleRow = Math.min(this.items.length, this.scrollRow + visibleRowCount);
for (let index = this.scrollRow; index < lastVisibleRow; index++) {
const item = this.items[index]!;
const rowY = bodyY + (index - this.scrollRow) * rowHeight;
const selected = index === this.selectedIndex;
const disabled = isMenuItemDisabled(item);
const row = selected ? new GrayImage(width - 24, rowHeight - 1, 0) : image;
const textX = selected ? 10 : x + 22;
const textY = selected ? 0 : rowY;
if (item.render) {
item.render({
image: row,
x: textX,
y: textY,
width: width - 44,
height: rowHeight - 3,
selected,
disabled,
text: item.label,
ctx,
});
} else {
row.drawText(font, textX, textY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
}
if (selected) {
const animation = this.highlightMotion.paint(index, this.scrollRow, x + 12, rowY, width - 24, rowHeight - 1, Date.now());
image.drawMenuSelection(row, x + 12, rowY, focused ? MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL : 0,
MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, 8, 0, animation);
}
const listHeight = visibleRowCount * rowHeight;
this.paintCtx = ctx;
try {
this.menu.paint(image, { x: x + MENU_ROW_INSET, y: bodyY, width: width - 2 * MENU_ROW_INSET, height: listHeight },
ctx.stack.isFocused());
} finally {
this.paintCtx = null;
}
if (this.items.length > visibleRowCount) {
drawListScrollbar(
image,
x + width - 7,
bodyY,
visibleRowCount * rowHeight - 4,
this.scrollRow,
visibleRowCount,
this.items.length,
);
}
this.menu.drawScrollbar(image, x + width - 7, bodyY, listHeight - 4);
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (!this.items.length) {
if (event.type === "double-click") {
ctx.stack.pop();
}
return;
}
switch (event.type) {
case "scroll-up":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === 0);
this.selectedIndex = (this.selectedIndex + this.items.length - 1) % this.items.length;
return;
case "scroll-down":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === this.items.length - 1);
this.selectedIndex = (this.selectedIndex + 1) % this.items.length;
return;
case "double-click":
ctx.stack.pop();
return;
case "click":
if (!isMenuItemDisabled(this.items[this.selectedIndex]!)) {
await this.items[this.selectedIndex]!.onSelect(ctx, this);
case "click": {
const item = this.menu.selectedItem;
if (item && !isMenuItemDisabled(item)) {
await item.onSelect(ctx, this);
}
return;
}
default:
await this.menu.handleInput(event);
return;
}
}