diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 40eb5600..0996ad82 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -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({ + wrap: true, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawAction(args), + }); + private readonly queueMenu = new Menu({ + 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): 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): 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 { 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); } } diff --git a/app/ui/menu-core.ts b/app/ui/menu-core.ts new file mode 100644 index 00000000..121ceb5d --- /dev/null +++ b/app/ui/menu-core.ts @@ -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 = { + /** + * 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 = { + 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) => void; + /** Called by activate() / a click with the selected item. */ + onSelect?: (item: T, index: number) => void | Promise; + /** + * 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 { + 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) { + 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 { + 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 { + 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; + } +} diff --git a/app/ui/menu.ts b/app/ui/menu.ts index ba108426..f3f4b8ad 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -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; + /** 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({ + 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 { - 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; } } diff --git a/tests/brightness-picker.test.cjs b/tests/brightness-picker.test.cjs index 037d45c5..4163c5ba 100644 --- a/tests/brightness-picker.test.cjs +++ b/tests/brightness-picker.test.cjs @@ -30,6 +30,9 @@ const menu = load('app/ui/menu.ts', { './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/image': graphics, '../graphics/ui-fonts': fonts, '../graphics/textwrap': textwrap, '../util/numeric-util': numeric, './gestures': gestures, './metrics': load('app/ui/metrics.ts'), + './menu-core': load('app/ui/menu-core.ts', { + '../graphics/image': graphics, './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), + }), }); function fixture(initial = '50', viewport = { x: 64, y: 124, width: 576, height: 260 }) { diff --git a/tests/menu-core.test.cjs b/tests/menu-core.test.cjs new file mode 100644 index 00000000..01b4cc24 --- /dev/null +++ b/tests/menu-core.test.cjs @@ -0,0 +1,240 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { Menu } = require('../.test-build/app/ui/menu-core.js'); +const { GrayImage } = require('../.test-build/app/graphics/image.js'); + +/** A menu of string items whose draw callback records every row it paints. */ +function stringMenu(overrides = {}) { + const drawn = []; + const exits = { top: 0, bottom: 0 }; + const selected = []; + const menu = new Menu({ + items: ['a', 'b', 'c', 'd', 'e'], + getHeight: () => 20, + draw: (args) => drawn.push({ item: args.item, x: args.x, y: args.y, width: args.width, height: args.height, + selected: args.selected, focused: args.focused, scratch: args.image !== drawn.image }), + onSelect: (item, index) => selected.push({ item, index }), + onExitTop: () => exits.top++, + onExitBottom: () => exits.bottom++, + ...overrides, + }); + const image = new GrayImage(100, 100); + drawn.image = image; + const paint = (focused = true, box = { x: 10, y: 5, width: 80, height: 60 }) => { + drawn.length = 0; + menu.paint(image, box, focused); + return drawn.slice(); + }; + return { menu, paint, drawn, exits, selected, image }; +} + +test('defaults to the first selectable item and lays rows out from the box top', () => { + const { menu, paint } = stringMenu(); + assert.equal(menu.selectedIndex, 0); + const rows = paint(); + assert.deepEqual(rows.map((r) => r.item), ['a', 'b', 'c'], 'only rows that fully fit are drawn'); + assert.deepEqual(rows.map((r) => r.y), [0, 25, 45], 'the selected row draws into a scratch image at y 0'); + assert.equal(rows[0].scratch, true); + assert.equal(rows[1].scratch, false); + assert.equal(rows[0].width, 80); + assert.equal(rows[0].height, 20); + assert.ok(rows.every((r) => r.focused)); + assert.equal(paint(false)[0].focused, false); +}); + +test('rowGap shrinks the drawable box without changing the pitch', () => { + const { paint } = stringMenu({ rowGap: 1 }); + const rows = paint(); + assert.deepEqual(rows.map((r) => r.height), [19, 19, 19]); + assert.deepEqual(rows.map((r) => r.y), [0, 25, 45]); +}); + +test('scrolling keeps the selection visible and aligns the viewport to a row top', async () => { + const { menu, paint } = stringMenu(); + await menu.handleInput({ type: 'scroll-down' }); + await menu.handleInput({ type: 'scroll-down' }); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 3); + assert.deepEqual(paint().map((r) => r.item), ['b', 'c', 'd']); + assert.equal(menu.scrollTop, 20); + await menu.handleInput({ type: 'scroll-up' }); + await menu.handleInput({ type: 'scroll-up' }); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(menu.selectedIndex, 0); + assert.deepEqual(paint().map((r) => r.item), ['a', 'b', 'c']); + assert.equal(menu.scrollTop, 0); +}); + +test('without wrap the ends call the exit callbacks and hold the selection', async () => { + const { menu, exits } = stringMenu(); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(menu.selectedIndex, 0); + assert.equal(exits.top, 1); + menu.select(4); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 4); + assert.equal(exits.bottom, 1); +}); + +test('with wrap the ends connect and the exit callbacks stay quiet', async () => { + const { menu, exits } = stringMenu({ wrap: true }); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(menu.selectedIndex, 4); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 0); + assert.deepEqual(exits, { top: 0, bottom: 0 }); +}); + +test('navigation skips unselectable items and never lands on them', async () => { + const { menu } = stringMenu({ + items: ['Group', 'a', 'b', 'Group 2', 'c'], + isSelectable: (item) => !item.startsWith('Group'), + }); + assert.equal(menu.selectedIndex, 1, 'initial selection skips the leading heading'); + await menu.handleInput({ type: 'scroll-down' }); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 4, 'the heading between b and c is skipped'); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 4); + menu.select(3); + assert.equal(menu.selectedIndex, 4, 'selecting a heading lands on the next selectable item'); + menu.select(0); + assert.equal(menu.selectedIndex, 1); +}); + +test('a null selection draws no highlight, and scrolling from it picks an end', async () => { + const { menu, paint } = stringMenu({ selectedIndex: null }); + assert.equal(menu.selectedIndex, null); + assert.equal(menu.selectedItem, null); + assert.ok(paint().every((r) => !r.selected && !r.scratch)); + assert.equal(await menu.handleInput({ type: 'click' }), false); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(menu.selectedIndex, 4); + menu.select(null); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 0); +}); + +test('with nothing selectable, up and down page the content and exit at the ends', async () => { + const { menu, paint, exits } = stringMenu({ isSelectable: () => false }); + paint(); + assert.equal(menu.selectedIndex, null); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.scrollTop, 20); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.scrollTop, 40, 'clamped to the content end'); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(exits.bottom, 1); + await menu.handleInput({ type: 'scroll-up' }); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(menu.scrollTop, 0); + await menu.handleInput({ type: 'scroll-up' }); + assert.equal(exits.top, 1); +}); + +test('click activates the selected item and reports other events as unhandled', async () => { + const { menu, selected } = stringMenu(); + menu.select(2); + assert.equal(await menu.handleInput({ type: 'click' }), true); + assert.deepEqual(selected, [{ item: 'c', index: 2 }]); + assert.equal(await menu.handleInput({ type: 'double-click' }), false); +}); + +test('setItems keeps the index by default, takes an explicit one, and holds the selected row in place', () => { + const { menu, paint } = stringMenu(); + menu.select(3); + paint(); + assert.equal(menu.scrollTop, 20, 'row d sits at the bottom of the viewport'); + menu.setItems(['x', 'y', 'z', 'd2', 'e2', 'f2', 'g2']); + assert.equal(menu.selectedIndex, 3); + assert.equal(menu.scrollTop, 20); + menu.setItems(['x', 'y', 'z', 'w', 'v', 'u', 'd3'], 6); + assert.equal(menu.selectedIndex, 6); + paint(); + assert.equal(menu.scrollTop, 80, 'row d3 (top 120) stays 40 px below the viewport top'); + menu.setItems(['only'], null); + assert.equal(menu.selectedIndex, null); + menu.setItems(['p', 'q'], 9); + assert.equal(menu.selectedIndex, 1, 'an out-of-range index clamps'); +}); + +test('variable heights: the layout follows getHeight and partial rows are hidden', () => { + const heights = { a: 10, b: 30, c: 15, d: 40, e: 5 }; + const { menu, paint } = stringMenu({ getHeight: (item) => heights[item] }); + let rows = paint(); + assert.deepEqual(rows.map((r) => [r.item, r.y, r.height]), [['a', 0, 10], ['b', 15, 30], ['c', 45, 15]]); + menu.select(3); + rows = paint(); + assert.equal(menu.scrollTop, 40, 'aligned to the top of row c'); + assert.deepEqual(rows.map((r) => [r.item, r.y]), [['c', 5], ['d', 0], ['e', 60]], 'e fits under d exactly'); + assert.equal(menu.overflows, true); +}); + +test('a selected row taller than the box is drawn from its top', () => { + const { menu, paint } = stringMenu({ getHeight: (item) => (item === 'c' ? 90 : 20) }); + menu.select(2); + const rows = paint(); + assert.deepEqual(rows.map((r) => r.item), ['c']); + assert.equal(menu.scrollTop, 40); +}); + +test('the selection re-validates against items edited in place', async () => { + const items = ['a', 'b', 'c']; + const { menu } = stringMenu({ items }); + menu.select(2); + items.length = 1; + assert.equal(menu.selectedIndex, 0); + items.length = 0; + assert.equal(menu.selectedIndex, null); + items.push('z'); + await menu.handleInput({ type: 'scroll-down' }); + assert.equal(menu.selectedIndex, 0); +}); + +test('the highlight is a replayed selection over the row scratch image', () => { + const { paint, image } = stringMenu(); + paint(); + const selection = image.draws.find((d) => d.presentation); + assert.ok(selection); + assert.equal(selection.x, 10); + assert.equal(selection.y, 5); + assert.equal(selection.presentation.radius, 8); + assert.equal(selection.presentation.background, 15); + assert.equal(selection.presentation.border, 45); + const unfocused = new GrayImage(100, 100); + stringMenu().menu.paint(unfocused, { x: 0, y: 0, width: 50, height: 50 }, false); + assert.equal(unfocused.draws.find((d) => d.presentation).presentation.background, 0); + const plain = new GrayImage(100, 100); + stringMenu({ highlight: false }).menu.paint(plain, { x: 0, y: 0, width: 50, height: 50 }, true); + assert.equal(plain.draws.some((d) => d.presentation), false); +}); + +test('indexAt maps a viewport coordinate to the selectable item under it', () => { + const { menu, paint } = stringMenu({ isSelectable: (item) => item !== 'b' }); + paint(); + assert.equal(menu.indexAt(4), null, 'above the box'); + assert.equal(menu.indexAt(5), 0); + assert.equal(menu.indexAt(30), null, 'row b is not selectable'); + assert.equal(menu.indexAt(50), 2); + assert.equal(menu.indexAt(65), null, 'below the box'); +}); + +test('the scrollbar tracks the pixel scroll fraction', () => { + const { menu, paint, image } = stringMenu(); + paint(); + image.clear(); + menu.drawScrollbar(image, 95, 5, 60); + assert.equal(image.getPixel(95, 5), 120, 'thumb at the top'); + assert.equal(image.getPixel(95, 60), 30, 'track below it'); + menu.select(4); + paint(); + image.clear(); + menu.drawScrollbar(image, 95, 5, 60); + assert.equal(image.getPixel(95, 5), 30); + assert.equal(image.getPixel(95, 64), 120, 'thumb at the bottom'); + const short = stringMenu({ items: ['a'] }); + short.paint(); + short.image.clear(); + short.menu.drawScrollbar(short.image, 95, 5, 60); + assert.equal(short.image.getPixel(95, 5), 0, 'no scrollbar when the content fits'); +}); diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index 2709219b..fc541512 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -15,11 +15,14 @@ function load(file,modules) { } const font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4, drawText:(image,x,y,text,value)=>{for(let i=0;ifont}, '../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{}, './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), - './metrics':require('../.test-build/app/ui/metrics.js'), + './metrics':require('../.test-build/app/ui/metrics.js'),'./menu-core':menuCore, }); const {prepareFrameDraws}=load('app/graphics/glyph-wire.ts',{'./presentation-wire':wire,'../native/texture-atlas':{textureAtlasAvailable:()=>false}}); function menu() { diff --git a/tests/music-settings.test.cjs b/tests/music-settings.test.cjs index 248b8caf..32d693f2 100644 --- a/tests/music-settings.test.cjs +++ b/tests/music-settings.test.cjs @@ -156,6 +156,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), './metrics': load('app/ui/metrics.ts'), './gestures': {}, }; + deps['./menu-core'] = load('app/ui/menu-core.ts', (name) => deps[name]); const menu = load('app/ui/menu.ts', (name) => deps[name]); const uiDeps = { '../../graphics/image': deps['../graphics/image'], diff --git a/tests/notification-filter.test.cjs b/tests/notification-filter.test.cjs index 07d2c57b..669675bb 100644 --- a/tests/notification-filter.test.cjs +++ b/tests/notification-filter.test.cjs @@ -92,6 +92,7 @@ function ui(fontSize = 12) { assert.ok(name in dependencies, `Unexpected dependency ${name}`); return dependencies[name]; }; + dependencies['./menu-core'] = load('app/ui/menu-core.ts', requireModule); dependencies['./menu'] = load('app/ui/menu.ts', requireModule); const { SingleNotificationLayer } = load('app/ui/notifications.ts', requireModule); const { NotificationFilterLayer } = load('app/ui/notification-filter.ts', requireModule); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 4a66e3c4..a4d6a71f 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -25,6 +25,7 @@ "../app/graphics/plane.ts", "../app/graphics/shell-scene.ts", "../app/ui/metrics.ts", + "../app/ui/menu-core.ts", "../app/phone-ui/phone-gestures.ts", "../app/g2/firmware-compat.ts", "../app/g2/glasses-hardware-identity.ts",