forked from Blendman974/faceclaw
feat: isolate notification popups and scroll selected menu labels
This commit is contained in:
1 parent
b4a2b2931b
commit
22df4d76d9
14 files changed
+577
-26
No files matched your search
+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;
|
||||
|
||||
Reference in new issue
Block a user