forked from Blendman974/faceclaw
Merge notification focus and selected menu-label scrolling
This commit is contained in:
commit
309f05547b
14 files changed
+577
-26
No files matched your search
@@ -4,7 +4,7 @@ import { wrapText } from "../../graphics/textwrap";
|
||||
import { InputEvent } from "../gestures";
|
||||
import { Layer, LayerContext, PaintBelow } from "../layers";
|
||||
import { isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
|
||||
import { Menu } from "../menu-core";
|
||||
import { Menu, drawMenuLabel } from "../menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../metrics";
|
||||
import { shell } from "../shell/shell";
|
||||
|
||||
@@ -66,8 +66,9 @@ export class SettingsPanelLayer implements Layer {
|
||||
rowGap: ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, selected }) => {
|
||||
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200);
|
||||
draw: ({ image, item, x, y, width, selected }) => {
|
||||
drawMenuLabel(image, getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET,
|
||||
width - ROW_TEXT_X - 8, item.label, selected ? 255 : 200);
|
||||
},
|
||||
});
|
||||
this.rightMenu = new Menu<MenuItem>({
|
||||
@@ -92,7 +93,8 @@ export class SettingsPanelLayer implements Layer {
|
||||
ctx: this.paintCtx!,
|
||||
});
|
||||
} else {
|
||||
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label,
|
||||
drawMenuLabel(image, getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET,
|
||||
width - ROW_TEXT_X - 8, item.label,
|
||||
disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
},
|
||||
|
||||
+46
-4
@@ -2,6 +2,8 @@ import { DrawExpression as E } from "../graphics/draw-expression";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { menuScrollList, slidingHighlightY, type MenuScrollHighlight } from "../graphics/menu-scroll-list";
|
||||
import type { InputEvent } from "./gestures";
|
||||
import { MenuLabelMotion, type MenuLabelPainter } from "./menu-label";
|
||||
export { drawMenuLabel } from "./menu-label";
|
||||
import { MenuHighlightMotion } from "./menu-highlight-motion";
|
||||
import { MenuScrollMotion, scrollOffsetExpression, type MenuScrollAnimation } from "./menu-scroll-motion";
|
||||
|
||||
@@ -39,6 +41,8 @@ export type MenuDrawArgs<T> = {
|
||||
selected: boolean;
|
||||
/** Whether the menu owns input right now: the `focused` flag the host passed to paint. */
|
||||
focused: boolean;
|
||||
/** Clipped label; overflowing selected/focused labels get a retained reading pass. */
|
||||
drawLabel: MenuLabelPainter;
|
||||
};
|
||||
|
||||
export type MenuHighlightStyle = {
|
||||
@@ -110,6 +114,7 @@ export class Menu<T> {
|
||||
private lastBox: MenuBox | null = null;
|
||||
private readonly motion = new MenuHighlightMotion();
|
||||
private readonly scrollMotion = new MenuScrollMotion();
|
||||
private readonly labelMotion = new MenuLabelMotion();
|
||||
|
||||
constructor(private readonly options: MenuOptions<T>) {
|
||||
this.itemList = options.items ?? [];
|
||||
@@ -160,7 +165,9 @@ export class Menu<T> {
|
||||
: null;
|
||||
this.itemList = items;
|
||||
if (selectedIndex !== undefined) this.deselected = selectedIndex === null;
|
||||
const previousSelection = this.selected;
|
||||
this.selected = this.settle(selectedIndex === undefined ? this.selected : selectedIndex);
|
||||
if (this.selected !== previousSelection) this.labelMotion.reset();
|
||||
if (before !== null && this.selected !== null && width !== undefined) {
|
||||
this.scrollY = Math.max(0, this.layout(width).tops[this.selected]! - before);
|
||||
}
|
||||
@@ -173,7 +180,9 @@ export class Menu<T> {
|
||||
*/
|
||||
select(index: number | null): void {
|
||||
this.deselected = index === null;
|
||||
this.selected = this.settle(index);
|
||||
const next = this.settle(index);
|
||||
if (next !== this.selected) this.labelMotion.reset();
|
||||
this.selected = next;
|
||||
}
|
||||
|
||||
/** Step the selection by one selectable row; handles wrap and the exit callbacks. */
|
||||
@@ -184,6 +193,7 @@ export class Menu<T> {
|
||||
if (next !== null) {
|
||||
if (from !== null) this.motion.navigate(from, false);
|
||||
this.scrollMotion.navigate(false);
|
||||
this.labelMotion.reset();
|
||||
this.selected = next;
|
||||
this.deselected = false;
|
||||
return;
|
||||
@@ -194,6 +204,7 @@ export class Menu<T> {
|
||||
if (wrapped !== null && wrapped !== from) {
|
||||
if (from !== null) this.motion.navigate(from, true);
|
||||
this.scrollMotion.navigate(true);
|
||||
this.labelMotion.reset();
|
||||
this.selected = wrapped;
|
||||
this.deselected = false;
|
||||
} else if (from === null && this.lastBox) {
|
||||
@@ -247,6 +258,7 @@ export class Menu<T> {
|
||||
paint(image: GrayImage, box: MenuBox, focused: boolean): void {
|
||||
this.lastBox = box;
|
||||
this.reconcile();
|
||||
if (!focused || this.selected === null) this.labelMotion.reset();
|
||||
const layout = this.layout(box.width);
|
||||
this.ensureVisible(layout, box.height);
|
||||
const now = Date.now();
|
||||
@@ -277,9 +289,10 @@ export class Menu<T> {
|
||||
}
|
||||
// A selected row taller than the box is drawn whole, from its top.
|
||||
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 });
|
||||
this.draw({ image: row, item, index, x: 0, y: 0, width: box.width, height, selected, focused });
|
||||
image.drawMenuSelection(row, box.x, rowY, background, MENU_HIGHLIGHT_STROKE,
|
||||
highlight?.radius ?? 8, highlight?.depth ?? 0, animation);
|
||||
this.copyRowPresentations(row, image, box.x, rowY, box.y, box.y + box.height, highlight?.depth ?? 0);
|
||||
} else {
|
||||
this.drawRow(image, index, box.x, rowY, box.width, height, focused, box.y, box.y + box.height);
|
||||
}
|
||||
@@ -293,6 +306,31 @@ export class Menu<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Retain row-local animations, intersecting their clips with the row and viewport. */
|
||||
private copyRowPresentations(row: GrayImage, target: GrayImage, x: number, y: number,
|
||||
clipTop: number, clipBottom: number, depth: number): void {
|
||||
for (const draw of row.draws) {
|
||||
if (draw.kind !== "image" || !draw.presentation?.displayList) continue;
|
||||
const left = Math.max(0, -draw.x), right = Math.min(draw.source.width, row.width - draw.x);
|
||||
const top = Math.max(0, -draw.y, clipTop - y - draw.y);
|
||||
const bottom = Math.min(draw.source.height, row.height - draw.y, clipBottom - y - draw.y);
|
||||
if (right <= left || bottom <= top) continue;
|
||||
const list = draw.presentation.displayList;
|
||||
target.drawDisplayList({ ...list, calls: list.calls.map(call => {
|
||||
const c = call.clip ?? { x: 0, y: 0, width: draw.source.width, height: draw.source.height };
|
||||
const cx = Math.max(left, c.x), cy = Math.max(top, c.y);
|
||||
return { ...call, clip: { x: cx, y: cy,
|
||||
width: Math.max(0, Math.min(right, c.x + c.width) - cx),
|
||||
height: Math.max(0, Math.min(bottom, c.y + c.height) - cy) } };
|
||||
}) }, x + draw.x, y + draw.y, draw.source.width, draw.source.height, depth);
|
||||
}
|
||||
}
|
||||
|
||||
private draw(args: Omit<MenuDrawArgs<T>, "drawLabel">): void {
|
||||
this.labelMotion.row(args.image, args.index, args.selected && args.focused,
|
||||
drawLabel => this.options.draw({ ...args, drawLabel }));
|
||||
}
|
||||
|
||||
/** 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;
|
||||
@@ -317,11 +355,12 @@ export class Menu<T> {
|
||||
const selected = index === this.selected;
|
||||
const scratch = selected && this.options.highlight !== false;
|
||||
if (!scratch && y >= clipTop && y + height <= clipBottom) {
|
||||
this.options.draw({ image: target, item, index, x, y, width, height, selected, focused });
|
||||
this.draw({ image: target, item, index, x, y, width, height, selected, focused });
|
||||
return;
|
||||
}
|
||||
const row = new GrayImage(width, height, 0);
|
||||
this.options.draw({ image: row, item, index, x: 0, y: 0, width, height, selected, focused });
|
||||
this.draw({ image: row, item, index, x: 0, y: 0, width, height, selected, focused });
|
||||
this.copyRowPresentations(row, target, x, y, clipTop, clipBottom, 0);
|
||||
const skip = Math.max(0, clipTop - y);
|
||||
target.bitBlt(row.withDrawsBaked(), x, y + skip,
|
||||
{ sy: skip, height: Math.min(height, clipBottom - y) - skip, transparentZero: true });
|
||||
@@ -352,6 +391,9 @@ export class Menu<T> {
|
||||
if (rowTop + height <= top || rowTop >= bottom) continue;
|
||||
this.drawRow(strip, index, 0, rowTop - top, box.width, height, focused, 0, strip.height);
|
||||
}
|
||||
// A reading pass has its own clipped timeline. Snap vertical motion rather
|
||||
// than baking that pass into a strip or leaving it at the wrong row position.
|
||||
if (strip.draws.some(d => d.kind === "image" && d.presentation)) return null;
|
||||
// Crop to the columns with ink; the static paint shows the rest unchanged.
|
||||
const baked = strip.withDrawsBaked();
|
||||
let left = baked.width, right = -1;
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { DrawExpression as E } from "../graphics/draw-expression";
|
||||
import { DrawOp, type ListCall } from "../graphics/display-list";
|
||||
import { GrayImage, type UiFont } from "../graphics/image";
|
||||
import { menuAnimationDurationMs } from "./menu-animation-pref";
|
||||
|
||||
export type MenuLabelPainter = (font: UiFont, x: number, y: number, width: number, text: string, value: number) => void;
|
||||
const contexts = new WeakMap<GrayImage, { motion: MenuLabelMotion; index: number; active: boolean }>();
|
||||
let nextToken = 0x40000000;
|
||||
|
||||
/** Shared row builders inherit their menu's focus and timeline without changing their public signature. */
|
||||
export function drawMenuLabel(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number): void {
|
||||
const context = contexts.get(image);
|
||||
if (context) context.motion.draw(image, font, x, y, width, text, value, context.index, context.active);
|
||||
else drawStatic(image, font, x, y, width, text, value);
|
||||
}
|
||||
|
||||
function drawStatic(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number): void {
|
||||
if (width <= 0) return;
|
||||
if (font.measureText(text) <= width) image.drawText(font, x, y, text, value);
|
||||
else {
|
||||
const clip = new GrayImage(width, font.lineHeight);
|
||||
clip.drawText(font, 0, 0, text, value);
|
||||
image.bitBlt(clip.withDrawsBaked(), x, y, { transparentZero: true });
|
||||
}
|
||||
}
|
||||
|
||||
/** One bounded, firmware-driven reading pass per selection/content/geometry/focus change. No JS timers. */
|
||||
export class MenuLabelMotion {
|
||||
private key = "";
|
||||
private timeline = { token: 0, startedAt: 0 };
|
||||
|
||||
row(image: GrayImage, index: number, active: boolean, draw: (drawLabel: MenuLabelPainter) => void): void {
|
||||
contexts.set(image, { motion: this, index, active });
|
||||
try { draw((font, x, y, width, text, value) => drawMenuLabel(image, font, x, y, width, text, value)); }
|
||||
finally { contexts.delete(image); }
|
||||
}
|
||||
|
||||
reset(): void { this.key = ""; }
|
||||
|
||||
draw(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number, index: number, active: boolean): void {
|
||||
width = Math.max(0, Math.floor(width));
|
||||
const measured = Math.ceil(font.measureText(text));
|
||||
drawStatic(image, font, x, y, width, text, value);
|
||||
if (!active) return;
|
||||
const travel = menuAnimationDurationMs(Math.max(600, Math.ceil((measured - width) * 1000 / 30)));
|
||||
if (!width || measured <= width || !travel || font.lineHeight > 480) { this.reset(); return; }
|
||||
// Bounded tiles, never a label-wide BLE image. Extreme labels use the static fallback.
|
||||
const tileWidth = Math.min(640, Math.floor(65530 / font.lineHeight) * 2);
|
||||
if (tileWidth <= 0 || Math.ceil(measured / tileWidth) > 32) return;
|
||||
const key = JSON.stringify([index, text, width, font.lineHeight, measured, x, y, value, travel]);
|
||||
if (this.key !== key) {
|
||||
this.key = key;
|
||||
this.timeline = { token: nextToken++, startedAt: Date.now() };
|
||||
}
|
||||
const distance = measured - width;
|
||||
// Hold the beginning, traverse at reading speed, hold the end, then return and stop.
|
||||
const offset = E.progress(travel, 900).mul(E.f32(distance))
|
||||
.sub(E.progress(travel, 1800 + travel).mul(E.f32(distance))).toInt();
|
||||
const resources: GrayImage[] = [];
|
||||
const calls: ListCall[] = [];
|
||||
for (let left = 0; left < measured; left += tileWidth) {
|
||||
const tile = new GrayImage(Math.min(tileWidth, measured - left), font.lineHeight, 16);
|
||||
tile.drawText(font, -left, 0, text, value);
|
||||
resources.push(tile.withDrawsBaked());
|
||||
calls.push({ op: DrawOp.RECT_COPY, resource: resources.length - 1, x: 0, y: 0,
|
||||
width: tile.width, height: tile.height, dx: E.i32(left).sub(offset), dy: 0,
|
||||
clip: { x: 0, y: 0, width, height: font.lineHeight } });
|
||||
}
|
||||
image.drawDisplayList({ resources, calls, timeline: this.timeline }, x, y, width, font.lineHeight);
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -7,7 +7,7 @@ import { clamp } from "../util/numeric-util";
|
||||
import { normalizeAnimationSpeed } from "./animation-speed";
|
||||
import { Layer, LayerContext, PaintBelow } from "./layers";
|
||||
import { MENU_ANIMATION_KEY, setMenuAnimationReader } from "./menu-animation-pref";
|
||||
import { Menu, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "./menu-core";
|
||||
import { Menu, drawMenuLabel, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "./menu-core";
|
||||
|
||||
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
|
||||
import { LIST_ROW_TEXT_INSET, centeredTextY, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
|
||||
@@ -179,7 +179,7 @@ export function submenuItem(
|
||||
render: ({ image, x, y, width, height, selected, disabled, text }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const value = disabled ? 70 : selected ? 255 : 200;
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, text, value);
|
||||
drawMenuLabel(image, font, x, y + LIST_ROW_TEXT_INSET, width - font.measureText("▶") - 12, text, value);
|
||||
drawSubmenuIndicator(image, font, x, y, width, height, value);
|
||||
},
|
||||
};
|
||||
@@ -200,7 +200,7 @@ export function drawToggleMenuItem(
|
||||
const switchX = x + width - switchWidth - 2;
|
||||
// Center the switch on the row's line box (legacy y+1 at the 12px default).
|
||||
const switchY = y + Math.max(1, Math.round((listRowHeight(font) - 2 - switchHeight) / 2));
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, label, 200);
|
||||
drawMenuLabel(image, font, x, y + LIST_ROW_TEXT_INSET, switchX - x - 8, label, 200);
|
||||
const offFill = selected ? 1 : 18;
|
||||
image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8);
|
||||
image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8);
|
||||
@@ -218,8 +218,8 @@ export function drawRightValueMenuItem(
|
||||
label: string,
|
||||
value: string,
|
||||
): void {
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, label, 200);
|
||||
const valueX = x + width - font.measureText(value) - 2;
|
||||
drawMenuLabel(image, font, x, y + LIST_ROW_TEXT_INSET, valueX - x - 8, label, 200);
|
||||
image.drawText(font, valueX, y + LIST_ROW_TEXT_INSET, value, 220);
|
||||
}
|
||||
|
||||
@@ -268,7 +268,8 @@ export class MenuLayer implements Layer {
|
||||
ctx: this.paintCtx!,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, y + LIST_ROW_TEXT_INSET, item.label,
|
||||
drawMenuLabel(image, font, x + MENU_ROW_TEXT_INSET, y + LIST_ROW_TEXT_INSET,
|
||||
width - 2 * MENU_ROW_TEXT_INSET, item.label,
|
||||
disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { DrawOp } from "../../graphics/display-list";
|
||||
import { flattenPlanes } from "../../graphics/plane";
|
||||
import { InputEvent } from "../gestures";
|
||||
import { Layer, LayerActions, LayerContext, LayerStack, PaintBelow } from "../layers";
|
||||
import { appViewportRect, appViewportSize, SHELL_OPAQUE_BLACK } from "./geometry";
|
||||
|
||||
// The modal covers most of the min-height app viewport, leaving a little of
|
||||
// the foreground app visible around the edges. Both its size and position are
|
||||
// The modal box covers most of the min-height app viewport. Retained vector
|
||||
// clears hide the app outside its opaque rect. Size and position are
|
||||
// computed per paint: the display-mode setting changes the viewport size at
|
||||
// runtime, and the vertical position setting moves the band.
|
||||
const MODAL_MARGIN = 14;
|
||||
@@ -33,9 +34,11 @@ export function modalRect(): { x: number; y: number; width: number; height: numb
|
||||
|
||||
/**
|
||||
* A shell overlay hosting an inner layer stack in a bordered box over the
|
||||
* app viewport (used for new-notification popups). The inner stack paints at
|
||||
* the modal's interior size; its pixels blit onto an opaque black backdrop,
|
||||
* so inner value-0 pixels read as black, not transparent.
|
||||
* app viewport (used for new-notification popups). It replaces the shell's
|
||||
* lower layers. Only the box is opaque, so its cropped image fits the 64 KiB
|
||||
* resource limit; a resource-free retained list clears the outside margins
|
||||
* over separate app surfaces. The inner stack paints at the modal's interior
|
||||
* size; inner value-0 pixels read as black, not transparent.
|
||||
*/
|
||||
export class ShellModalLayer implements Layer {
|
||||
private readonly stack: LayerStack;
|
||||
@@ -57,8 +60,14 @@ export class ShellModalLayer implements Layer {
|
||||
this.hooks.removed?.();
|
||||
}
|
||||
|
||||
paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const image = paintBelow();
|
||||
paint(ctx: LayerContext, _paintBelow: PaintBelow): GrayImage {
|
||||
// Do not call paintBelow: retained chrome/menu animations must not remain
|
||||
// beneath the notification. Leave the outer margins transparent so the
|
||||
// scene encoder crops to the resource-sized modal, not the whole display.
|
||||
// Native dim=0 still preserves a 1/0 halftone; clipped CLEAR calls below
|
||||
// instead produce literal black without changing shared dimming semantics.
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height);
|
||||
// Track a display-mode switch while the modal is up, so the inner stack
|
||||
// paints at the same size the box below is drawn with.
|
||||
const interior = modalInterior();
|
||||
@@ -78,6 +87,19 @@ export class ShellModalLayer implements Layer {
|
||||
image.drawRect(rect.x, rect.y, rect.width, rect.height, 110);
|
||||
image.bitBlt(inner, innerX, innerY, { transparentZero: true });
|
||||
top.image.copyPresentationsInto(image, innerX + top.x, innerY + top.y);
|
||||
// ShellScene replays retained calls after the opaque box. Clear only the
|
||||
// complement of that box, keeping the inner menu's presentation order.
|
||||
// These are vector clips, not tiled image resources; no full-screen image
|
||||
// is uploaded. Clips move with the scene's stereo depth just like the box.
|
||||
const margins = [
|
||||
{ x: 0, y: 0, width, height: rect.y },
|
||||
{ x: 0, y: rect.y + rect.height, width, height: height - rect.y - rect.height },
|
||||
{ x: 0, y: rect.y, width: rect.x, height: rect.height },
|
||||
{ x: rect.x + rect.width, y: rect.y, width: width - rect.x - rect.width, height: rect.height },
|
||||
];
|
||||
image.drawDisplayList({ resources: [], calls: margins
|
||||
.filter(clip => clip.width > 0 && clip.height > 0)
|
||||
.map(clip => ({ op: DrawOp.CLEAR, color: 0, clip })) }, 0, 0, width, height);
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user