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
@@ -3,8 +3,8 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const DIALOG_X = 8;
|
||||
const DIALOG_Y = 8;
|
||||
@@ -21,6 +21,8 @@ const SERVICE_LABELS: Record<string, string> = {
|
||||
|
||||
export type ApiKeyRequestItem = { id: string; configured: boolean };
|
||||
|
||||
type ConsentAction = "Allow" | "Deny";
|
||||
|
||||
/**
|
||||
* Consent dialog shown when an EvenHub app calls requestApiKeyAccess: lists the
|
||||
* requested API-key services (marking any the user hasn't configured) and asks
|
||||
@@ -28,8 +30,14 @@ export type ApiKeyRequestItem = { id: string; configured: boolean };
|
||||
* double-click) shares nothing.
|
||||
*/
|
||||
export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
/** 0 = Allow, 1 = Deny. */
|
||||
private selectedIndex = 0;
|
||||
private readonly menu = new Menu<ConsentAction>({
|
||||
items: ["Allow", "Deny"],
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => drawActionRow(args),
|
||||
});
|
||||
private resolved = false;
|
||||
|
||||
constructor(
|
||||
@@ -67,16 +75,11 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
}
|
||||
y += 4;
|
||||
|
||||
const focused = ctx.stack.isFocused();
|
||||
const actions = ["Allow", "Deny"];
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const rowY = y + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
|
||||
}
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -84,10 +87,10 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down":
|
||||
this.selectedIndex = (this.selectedIndex + 1) % 2;
|
||||
void this.menu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
this.resolve(ctx, this.selectedIndex === 0);
|
||||
this.resolve(ctx, this.menu.selectedItem === "Allow");
|
||||
return;
|
||||
case "double-click":
|
||||
this.resolve(ctx, false);
|
||||
@@ -113,3 +116,7 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
else this.onCancel();
|
||||
}
|
||||
}
|
||||
|
||||
function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs<ConsentAction>): void {
|
||||
image.drawText(getDefaultSmallFont(), x + 10, y + 3, item, selected ? 255 : 200);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { TextViewerLayer } from "../files/text-viewer";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
@@ -18,6 +18,9 @@ import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const X = 18;
|
||||
|
||||
type DetailAction = "about" | "whats-new" | "primary";
|
||||
const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"];
|
||||
|
||||
export type EvenHubStoreDetailOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
@@ -27,8 +30,22 @@ export type EvenHubStoreDetailOptions = {
|
||||
|
||||
/** Storefront metadata and the Install/Launch action for one public app. */
|
||||
export class EvenHubStoreDetailLayer implements Layer {
|
||||
/** About, What's New, Install/Launch. */
|
||||
private selectedIndex = 2;
|
||||
/** About, What's New, Install/Launch; starts on the primary action. */
|
||||
private readonly actions = new Menu<DetailAction>({
|
||||
items: ACTIONS,
|
||||
selectedIndex: 2,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()) + 4,
|
||||
draw: ({ image, item, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const label = item === "about" ? "About" : item === "whats-new" ? "What's New" : this.primaryLabel;
|
||||
const value = this.working && item === "primary" ? 145 : selected ? 245 : 190;
|
||||
image.drawText(font, x + 11, y + 5, truncateText(font, label, width - 18), value);
|
||||
},
|
||||
});
|
||||
/** The primary action's label for the paint in progress. */
|
||||
private primaryLabel = "";
|
||||
private working = false;
|
||||
private detailPromise: Promise<void> | null = null;
|
||||
private status = "";
|
||||
@@ -47,7 +64,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionRowH = listRowHeight(font) + 4;
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)];
|
||||
this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed);
|
||||
|
||||
// Detail lines as (text, shade, extra gap below); built per candidate
|
||||
// width so the layout choice below can measure before drawing.
|
||||
@@ -90,7 +107,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
// Stacked layout (details full-width, actions at the bottom) when it
|
||||
// fits; otherwise the actions become a narrow top-right menu and the
|
||||
// details flow down a left column beside it.
|
||||
const stackedActionTop = height - 6 - actions.length * actionRowH;
|
||||
const stackedActionTop = height - 6 - ACTIONS.length * actionRowH;
|
||||
const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop;
|
||||
const menuW = 150;
|
||||
const menuX = width - menuW - 12;
|
||||
@@ -105,15 +122,11 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionX = stacked ? X : menuX;
|
||||
const actionW = stacked ? textWidth : menuW;
|
||||
const actionTop = stacked ? stackedActionTop : 8;
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const actionY = actionTop + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, actionX - 5, actionY, actionW + 10, actionRowH - 1, ctx.stack.isFocused(), 8);
|
||||
}
|
||||
const value = this.working && index === 2 ? 145 : selected ? 245 : 190;
|
||||
image.drawText(font, actionX + 6, actionY + 5, truncateText(font, actions[index]!, actionW - 8), value);
|
||||
}
|
||||
this.actions.paint(
|
||||
image,
|
||||
{ x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -123,12 +136,8 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
if (this.working) return;
|
||||
if (event.type === "scroll-up") {
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
}
|
||||
if (event.type === "scroll-down") {
|
||||
this.selectedIndex = Math.min(2, this.selectedIndex + 1);
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
await this.actions.handleInput(event);
|
||||
return;
|
||||
}
|
||||
if (event.type !== "click") return;
|
||||
@@ -143,11 +152,12 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
try {
|
||||
await this.ensureDetail(ctx);
|
||||
|
||||
if (this.selectedIndex === 0) {
|
||||
const action = this.actions.selectedItem;
|
||||
if (action === "about") {
|
||||
ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About"));
|
||||
return;
|
||||
}
|
||||
if (this.selectedIndex === 1) {
|
||||
if (action === "whats-new") {
|
||||
ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { drawSelectionHighlight, type MenuItem } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ConfigSettingString } from "../../ui/dashboard-settings";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api";
|
||||
@@ -24,6 +24,11 @@ function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight + 6;
|
||||
}
|
||||
|
||||
/** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */
|
||||
function storeRowHeight(font: UiFont): number {
|
||||
return Math.max(30, 2 * lineStep(font) + 4);
|
||||
}
|
||||
|
||||
/** Last search typed on the phone; kept so reopening the store shows it again. */
|
||||
export const evenHubSearchQuerySetting = new ConfigSettingString({
|
||||
id: "evenhub-search-query",
|
||||
@@ -64,27 +69,46 @@ type TabState = {
|
||||
loading: boolean;
|
||||
/** Progress or error text for the subtitle line. */
|
||||
status: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Selection and scroll over `rows` (resynced before each paint and input). */
|
||||
menu: Menu<StoreRow>;
|
||||
/** Search only: the query the rows belong to. */
|
||||
query: string;
|
||||
};
|
||||
|
||||
function emptyTab(): TabState {
|
||||
const rows: StoreRow[] = [];
|
||||
return {
|
||||
rows: [],
|
||||
rows,
|
||||
total: 0,
|
||||
nextPage: 1,
|
||||
exhausted: false,
|
||||
loaded: false,
|
||||
loading: false,
|
||||
status: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
menu: new Menu<StoreRow>({
|
||||
items: rows,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 5 },
|
||||
getHeight: () => storeRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, width }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const { title, detail, titleValue } = rowText(item);
|
||||
const textX = x + 6;
|
||||
const textW = width - 12;
|
||||
image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue);
|
||||
image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115);
|
||||
},
|
||||
}),
|
||||
query: "",
|
||||
};
|
||||
}
|
||||
|
||||
/** The tab's menu, with its items brought up to date with `rows` (which loads replace or extend). */
|
||||
function tabMenu(tab: TabState): Menu<StoreRow> {
|
||||
if (tab.menu.items !== tab.rows) tab.menu.setItems(tab.rows);
|
||||
return tab.menu;
|
||||
}
|
||||
|
||||
export type EvenHubStoreLayerOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
@@ -170,25 +194,16 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const rowH = storeRowHeight(font);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length);
|
||||
const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows);
|
||||
const listFocused = ctx.stack.isFocused() && this.focus === "list";
|
||||
for (let index = tab.scrollRow; index < last; index++) {
|
||||
const row = tab.rows[index]!;
|
||||
const y = headerH + (index - tab.scrollRow) * rowH;
|
||||
if (index === tab.selectedIndex) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5);
|
||||
}
|
||||
const { title, detail, titleValue } = this.rowText(row);
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue);
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (tab.rows.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length);
|
||||
}
|
||||
const menu = tabMenu(tab);
|
||||
menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 },
|
||||
listFocused,
|
||||
);
|
||||
menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -250,41 +265,6 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.showingLogin) {
|
||||
if (event.type === "click" && !this.loginBusy) {
|
||||
@@ -299,16 +279,17 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
const tab = this.tabs[this.activeTab];
|
||||
const menu = tabMenu(tab);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
tab.selectedIndex = Math.max(0, tab.selectedIndex - 1);
|
||||
await menu.handleInput(event);
|
||||
return;
|
||||
case "scroll-down":
|
||||
tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1);
|
||||
if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
await menu.handleInput(event);
|
||||
if ((menu.selectedIndex ?? 0) >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
return;
|
||||
case "click":
|
||||
await this.activateRow(ctx, tab.rows[tab.selectedIndex]);
|
||||
await this.activateRow(ctx, menu.selectedItem ?? undefined);
|
||||
return;
|
||||
case "double-click":
|
||||
this.focus = "tabs";
|
||||
@@ -540,7 +521,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
state.rows = [{ kind: "search-query" }];
|
||||
state.loaded = !query;
|
||||
// Keep the cursor on the first result once there are some.
|
||||
state.selectedIndex = query ? 1 : 0;
|
||||
state.menu.setItems(state.rows, query ? 1 : 0);
|
||||
this.tabs.search = state;
|
||||
ctx.actions.requestRender();
|
||||
if (query) await this.loadNextPage(ctx, "search");
|
||||
@@ -566,8 +547,8 @@ export class EvenHubStoreLayer implements Layer {
|
||||
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
|
||||
);
|
||||
state.rows = rows;
|
||||
state.selectedIndex = 0;
|
||||
state.scrollRow = 0;
|
||||
state.menu.setItems(rows, 0);
|
||||
state.menu.scrollTop = 0;
|
||||
state.status = "";
|
||||
state.exhausted = true;
|
||||
} catch (error) {
|
||||
@@ -643,7 +624,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
state.rows = state.rows.filter((row) =>
|
||||
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
|
||||
);
|
||||
state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1));
|
||||
state.menu.setItems(state.rows);
|
||||
}
|
||||
|
||||
// ---- login and phone editors --------------------------------------------
|
||||
@@ -755,6 +736,42 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/** A list row's title (with its shade) and dim detail line. */
|
||||
function rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A registry entry dressed as a store record, for pages opened before the store record arrives. */
|
||||
function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user