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