forked from Blendman974/faceclaw
[verified] merge upstream 25cb255b into all-features
This commit is contained in:
commit
ffcdae660d
169 files changed
+16201
-597
No files matched your search
+58
-6
@@ -39,6 +39,8 @@ const PAGE_TITLE_OFFSET_Y = 2;
|
||||
const PAGE_TITLE_GAP = 4;
|
||||
/** Room a page menu keeps right of its rows for the scrollbar. */
|
||||
const PAGE_SCROLLBAR_GUTTER = 6;
|
||||
/** A heading row's text: dimmer than a row's, brighter than a disabled row's. */
|
||||
const MENU_HEADING_VALUE = 90;
|
||||
|
||||
export type MenuLayout = {
|
||||
/** Stereo depth of the menu surface, including the selected row. */
|
||||
@@ -57,7 +59,7 @@ export type MenuLayout = {
|
||||
* Dim hint line pinned to the bottom edge inside the box, below the items
|
||||
* (gesture help such as "●— system menu"). Takes a line off the item area.
|
||||
*/
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
/** Brightness factor for everything beneath the menu (see Layer.dimUnderneath). Default: none. */
|
||||
dimUnderneath?: number;
|
||||
/**
|
||||
@@ -83,9 +85,16 @@ export type MenuPageLayout = {
|
||||
/** Width of the rows. Default (and at most): the window's, inside its side padding. */
|
||||
width?: number;
|
||||
/** Dim hint line pinned above the window's bottom padding, below the rows. */
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
};
|
||||
|
||||
/** A menu's footer line: fixed text, or read on every paint (a live value). */
|
||||
export type MenuFooter = string | (() => string);
|
||||
|
||||
function footerText(footer: MenuFooter): string {
|
||||
return typeof footer === "function" ? footer() : footer;
|
||||
}
|
||||
|
||||
export type MenuItemRenderArgs = {
|
||||
image: GrayImage;
|
||||
x: number;
|
||||
@@ -104,6 +113,12 @@ export type MenuItem = {
|
||||
description?: string;
|
||||
/** Disabled rows stay visible but render dimly and ignore clicks. */
|
||||
disabled?: boolean | (() => boolean);
|
||||
/**
|
||||
* A label for the group of rows after it (see menuHeadingItem). MenuLayer's
|
||||
* highlight skips it; a list that doesn't know headings shows it as a
|
||||
* disabled row.
|
||||
*/
|
||||
heading?: boolean;
|
||||
onSelect: (ctx: LayerContext, menu: MenuLayer) => Promise<void> | void;
|
||||
render?: (args: MenuItemRenderArgs) => void;
|
||||
};
|
||||
@@ -207,6 +222,22 @@ export function submenuItem(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A heading for the rows that follow it, splitting a long menu into sections:
|
||||
* dim capitals in the rows' text column, never selected.
|
||||
*/
|
||||
export function menuHeadingItem(label: string): MenuItem {
|
||||
return {
|
||||
label,
|
||||
heading: true,
|
||||
disabled: true,
|
||||
onSelect: () => {},
|
||||
render: ({ image, x, y, text }) => {
|
||||
image.drawText(getDefaultSmallFont(), x, y + LIST_ROW_TEXT_INSET, text.toUpperCase(), MENU_HEADING_VALUE);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function drawToggleMenuItem(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
@@ -274,6 +305,7 @@ export class MenuLayer implements Layer {
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
isSelectable: (item) => !item.heading,
|
||||
draw: ({ image, item, x, y, width, height, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
@@ -361,7 +393,7 @@ export class MenuLayer implements Layer {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
}
|
||||
if (layout.footer) {
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, layout.footer, 110);
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, footerText(layout.footer), 110);
|
||||
}
|
||||
|
||||
const bodyY = y + chromeTop;
|
||||
@@ -398,7 +430,7 @@ export class MenuLayer implements Layer {
|
||||
let fixedBottom = height - padding.bottom;
|
||||
if (layout.footer) {
|
||||
const footerY = height - padding.bottom - font.lineHeight;
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, layout.footer, 110);
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, footerText(layout.footer), 110);
|
||||
bottom = fixedBottom = footerY - MENU_FOOTER_GAP;
|
||||
}
|
||||
if (bottom <= top) return image;
|
||||
@@ -432,17 +464,26 @@ export class MenuLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
export type ModalMenuOptions = {
|
||||
/** A dim line under the items (see MenuLayout.footer). */
|
||||
footer?: MenuFooter;
|
||||
/** Runs when the menu leaves the stack, however it closes. */
|
||||
onClosed?: () => void;
|
||||
};
|
||||
|
||||
/** Open a centered, bordered menu over the current layer. */
|
||||
export function openModalMenu(
|
||||
ctx: LayerContext,
|
||||
title: string,
|
||||
items: MenuItem[],
|
||||
initialSelectedIndex = 0,
|
||||
options: ModalMenuOptions = {},
|
||||
): void {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const font = getDefaultSmallFont();
|
||||
const menuWidth = Math.min(320, width - 40);
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font);
|
||||
const footerHeight = options.footer ? MENU_FOOTER_GAP + lineStep(font) : 0;
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font) + footerHeight;
|
||||
// 20px clear above and below, or 8 in a short window (the RayNeo iO's),
|
||||
// where those rows are worth more than the margin.
|
||||
const marginY = height < SHORT_WINDOW_HEIGHT ? 8 : 20;
|
||||
@@ -455,8 +496,19 @@ export function openModalMenu(
|
||||
maxHeight: menuHeight,
|
||||
// Floats nearer than the content it opens over, like the window and system menus.
|
||||
depth: 4,
|
||||
footer: options.footer,
|
||||
};
|
||||
ctx.stack.push(new MenuLayer(title, items, layout).selectItem(initialSelectedIndex));
|
||||
ctx.stack.push(new ModalMenuLayer(title, items, layout, options.onClosed).selectItem(initialSelectedIndex));
|
||||
}
|
||||
|
||||
class ModalMenuLayer extends MenuLayer {
|
||||
constructor(title: string, items: MenuItem[], layout: MenuLayout, private readonly onClosed?: () => void) {
|
||||
super(title, items, layout);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.onClosed?.();
|
||||
}
|
||||
}
|
||||
|
||||
export function isMenuItemDisabled(item: MenuItem): boolean {
|
||||
|
||||
Reference in new issue
Block a user