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,15 +1,9 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import {
|
||||
MenuLayer,
|
||||
drawListScrollbar,
|
||||
drawSelectionHighlight,
|
||||
drawSubmenuIndicator,
|
||||
scrollToKeepSelectionVisible,
|
||||
} from "../../ui/menu";
|
||||
import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
mediaBrowserBridge,
|
||||
type MediaBrowseItem,
|
||||
@@ -22,6 +16,8 @@ function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight;
|
||||
}
|
||||
const LIST_X = 20;
|
||||
/** The selection box extends this far left and right of the row text. */
|
||||
const LIST_HIGHLIGHT_PAD = 6;
|
||||
|
||||
export type MediaBrowseOptions = {
|
||||
app: MediaBrowserApp;
|
||||
@@ -37,8 +33,8 @@ type BrowseLevel = {
|
||||
/** null while the listing is loading. */
|
||||
items: MediaBrowseItem[] | null;
|
||||
error: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Holds the loaded items; keeps this level's selection and scroll while a child level is open. */
|
||||
menu: Menu<MediaBrowseItem>;
|
||||
};
|
||||
|
||||
type BrowsePhase = "idle" | "connecting" | "ready" | "failed";
|
||||
@@ -97,50 +93,16 @@ export class MediaBrowseLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
|
||||
const listHeight = height - headerHeight(font) - font.lineHeight - 10;
|
||||
const rowH = tightRowHeight(font);
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
|
||||
|
||||
const rowWidth = width - 2 * LIST_X;
|
||||
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
|
||||
for (let index = level.scrollRow; index < lastVisible; index++) {
|
||||
const item = items[index]!;
|
||||
const y = headerHeight(font) + (index - level.scrollRow) * rowH;
|
||||
const selected = index === level.selectedIndex;
|
||||
const highlightX = LIST_X - 6;
|
||||
const highlightY = y - 1;
|
||||
const highlightWidth = rowWidth + 12;
|
||||
const highlightHeight = rowH - 1;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
const value = selected ? 255 : 200;
|
||||
const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16);
|
||||
image.drawText(font, LIST_X, y + 1, title, value);
|
||||
if (item.subtitle) {
|
||||
const titleWidth = font.measureText(title);
|
||||
const subtitleWidth = font.measureText(item.subtitle);
|
||||
if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) {
|
||||
image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110);
|
||||
}
|
||||
}
|
||||
if (item.browsable) {
|
||||
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110);
|
||||
}
|
||||
}
|
||||
if (items.length > visibleRows) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
width - 12,
|
||||
headerHeight(font),
|
||||
visibleRows * rowH - 4,
|
||||
level.scrollRow,
|
||||
visibleRows,
|
||||
items.length,
|
||||
);
|
||||
}
|
||||
const listTop = headerHeight(font);
|
||||
const listHeight = height - listTop - font.lineHeight - 10;
|
||||
const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0);
|
||||
// Row boxes start one pixel above the header's bottom edge.
|
||||
level.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4);
|
||||
|
||||
image.drawText(
|
||||
font,
|
||||
@@ -175,13 +137,11 @@ export class MediaBrowseLayer implements Layer {
|
||||
if (!level || !items || !items.length) return;
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
level.selectedIndex = Math.max(0, level.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1);
|
||||
await level.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const item = items[clamp(level.selectedIndex, 0, items.length - 1)];
|
||||
const item = level.menu.selectedItem;
|
||||
if (!item) return;
|
||||
if (item.browsable && item.playable) {
|
||||
ctx.stack.push(
|
||||
@@ -233,18 +193,46 @@ export class MediaBrowseLayer implements Layer {
|
||||
}
|
||||
|
||||
private descendInto(ctx: LayerContext, item: MediaBrowseItem): void {
|
||||
const level: BrowseLevel = {
|
||||
parentId: item.mediaId,
|
||||
title: item.title || "(untitled)",
|
||||
items: null,
|
||||
error: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
};
|
||||
const level = this.createLevel(item.mediaId, item.title || "(untitled)");
|
||||
this.levels.push(level);
|
||||
this.loadLevel(ctx, level);
|
||||
}
|
||||
|
||||
private createLevel(parentId: string, title: string): BrowseLevel {
|
||||
return {
|
||||
parentId,
|
||||
title,
|
||||
items: null,
|
||||
error: "",
|
||||
menu: new Menu<MediaBrowseItem>({
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawItem(args),
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
private drawItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaBrowseItem>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const value = selected ? 255 : 200;
|
||||
const textWidth = width - 2 * LIST_HIGHLIGHT_PAD - 16;
|
||||
const textY = centeredTextY(font, y, height);
|
||||
const title = truncateRight(font, item.title || "(untitled)", textWidth);
|
||||
image.drawText(font, x + LIST_HIGHLIGHT_PAD, textY, title, value);
|
||||
if (item.subtitle) {
|
||||
const titleWidth = font.measureText(title);
|
||||
const subtitleWidth = font.measureText(item.subtitle);
|
||||
if (titleWidth + subtitleWidth + 24 <= textWidth) {
|
||||
image.drawText(font, x + LIST_HIGHLIGHT_PAD + textWidth - subtitleWidth, textY, item.subtitle, 110);
|
||||
}
|
||||
}
|
||||
if (item.browsable) {
|
||||
drawSubmenuIndicator(image, font, x, y, width, height, selected ? value : 110);
|
||||
}
|
||||
}
|
||||
|
||||
private startConnect(ctx: LayerContext): void {
|
||||
this.phase = "connecting";
|
||||
mediaBrowserBridge
|
||||
@@ -252,14 +240,7 @@ export class MediaBrowseLayer implements Layer {
|
||||
.then((rootId) => {
|
||||
if (this.removed) return;
|
||||
this.phase = "ready";
|
||||
const root: BrowseLevel = {
|
||||
parentId: rootId,
|
||||
title: "Library",
|
||||
items: null,
|
||||
error: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
};
|
||||
const root = this.createLevel(rootId, "Library");
|
||||
this.levels = [root];
|
||||
this.loadLevel(ctx, root);
|
||||
ctx.actions.requestRender();
|
||||
@@ -278,6 +259,7 @@ export class MediaBrowseLayer implements Layer {
|
||||
.then((items) => {
|
||||
if (this.removed) return;
|
||||
level.items = items;
|
||||
level.menu.setItems(items, 0);
|
||||
ctx.actions.requestRender();
|
||||
})
|
||||
.catch((error) => {
|
||||
|
||||
@@ -5,7 +5,7 @@ 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, drawSubmenuIndicator } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
@@ -139,7 +139,7 @@ class MusicAppLayer implements Layer {
|
||||
this.reconcileSelection(actions, queue);
|
||||
const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN;
|
||||
const focused = ctx.stack.isFocused();
|
||||
// Row boxes start one pixel above LIST_TOP so the text lands at LIST_TOP + 1.
|
||||
// Row boxes start one pixel above LIST_TOP, level with the column divider.
|
||||
this.actionsMenu.paint(
|
||||
image,
|
||||
{ x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight },
|
||||
@@ -165,16 +165,16 @@ class MusicAppLayer implements Layer {
|
||||
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);
|
||||
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), 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 {
|
||||
private drawQueueItem({ image, item, x, y, width, height, 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);
|
||||
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
|
||||
@@ -10,39 +10,45 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
/** Discovered apps stay visible even when ignored or no longer publishing a session. */
|
||||
export class MusicSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private signature = "";
|
||||
private selectedPackage = "";
|
||||
private menuWidth = 0;
|
||||
/** The package behind each current row, parallel to the menu's items. */
|
||||
private packages: string[] = [];
|
||||
|
||||
constructor() {
|
||||
// Refresh installed browser services, remembering ignored services too.
|
||||
mediaBrowserBridge.listBrowsableApps(true);
|
||||
}
|
||||
|
||||
/** Refresh the rows from the remembered apps, keeping the selection on the same package. */
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
const apps = readMediaApps().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
|
||||
const index = apps.findIndex((app) => app.packageName === selectedPackage);
|
||||
this.packages = apps.map((app) => app.packageName);
|
||||
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]);
|
||||
if (!this.menu || signature !== this.signature) {
|
||||
this.signature = signature;
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = app.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
if (!this.menu || width !== this.menuWidth) {
|
||||
this.menuWidth = width;
|
||||
this.menu = new MenuLayer("Music settings", items, {
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = enabled Off = ignored",
|
||||
}).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage)));
|
||||
}).selectItem(Math.max(0, index));
|
||||
} else {
|
||||
this.menu.setItems(items, index >= 0 ? index : undefined);
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user