Context menus: gesture switch between app and system context menus; gesture help text; move items from app to system menu

This commit is contained in:
James Babcock committed 2026-09-02 21:14:40 -07:00
1 parent 061f50e711
commit 987f442897
15 files changed
+343 -205

No files matched your search

+68 -49
View File
@@ -1,20 +1,29 @@
import { GrayImage } from "../graphics/image";
import { singlePlane, type Plane } from "../graphics/plane";
import { type InputEvent } from "./gestures";
import { GESTURE_SHORT_THEN_LONG_PRESS, gestureHints, type InputEvent } from "./gestures";
import { type Layer, LayerStack, noopLayerActions } from "./layers";
import { MenuLayer, type MenuItem, type MenuLayout } from "./menu";
import type { WorkerAppReply } from "./shell/worker-window";
/**
* The window long-press menu. By convention every app answers a long-press by
* opening one of these: its app-specific actions followed by the default
* entries (Voice input, Close window). This is a convenience path, not the
* safety net — the shell separately opens its own escape menu when the press
* is held long enough, so an unresponsive app can always be closed.
* The window long-press menu: an app's own context menu, holding its
* app-specific actions. The shell's system menu (tap-then-hold, or a press
* held past the escape threshold) carries the entries every window shares —
* Focus app switcher, Voice input, Close window — so an app with nothing of
* its own to offer has no menu of its own: a long-press then opens the system
* menu in its place, so both gestures land on the same menu. The system menu
* is also the safety net: shell-owned, so an unresponsive app can always be
* closed.
*/
/** Centered over the viewport; visually matches the shell's escape menu position. */
export const WINDOW_MENU_LAYOUT: MenuLayout = { x: "center", y: 8, width: 272, minHeight: 150 };
/** Centered over the viewport; visually matches the shell's system menu position. */
export const WINDOW_MENU_LAYOUT: MenuLayout = {
x: "center",
y: 8,
width: 272,
minHeight: 150,
footer: gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "system menu"]]),
};
export class WindowMenuLayer extends MenuLayer {
constructor(title: string | null, items: MenuItem[]) {
@@ -23,8 +32,22 @@ export class WindowMenuLayer extends MenuLayer {
}
export type WindowMenuOptions = {
windowId: string;
post: (reply: WorkerAppReply) => void;
/** Menu title: the window's title, read at open time. */
title: () => string;
/**
* The app's menu entries, built at open time so they reflect current
* state. Empty means the window has no context menu: open() asks the shell
* for the system menu instead.
*/
items: () => MenuItem[];
/**
* False while the app gives long-press a meaning of its own (a game move)
* instead of opening this menu. Default: always true. Only informs the
* shell's system-menu hint; what a long-press does stays the app's call.
*/
longPressOpensMenu?: () => boolean;
/** Paint the window content the menu draws over (a fresh, mutable image). */
paintBase: () => GrayImage;
size: { width: number; height: number };
@@ -36,11 +59,14 @@ export type WindowMenuOptions = {
* a persistent LayerStack. Wraps a short-lived stack so MenuLayer (and the
* ctx.stack.pop() convention in item callbacks) works unchanged.
*
* Usage: route input to handleInput while isOpen(); paint() returns the
* window content with the menu drawn over it.
* Usage: answer a long-press with open(); route input to handleInput while
* isOpen(); paint every frame through paint(), which returns the window
* content with the menu drawn over it and keeps the shell told whether the
* window currently has a menu (the system menu's gesture hint depends on it).
*/
export class WindowMenu {
private stack: LayerStack | null = null;
private reportedAvailable: boolean | null = null;
constructor(private readonly options: WindowMenuOptions) {}
@@ -48,8 +74,22 @@ export class WindowMenu {
return this.stack !== null;
}
open(items: MenuItem[]): void {
/** True when a long-press currently opens this menu with something in it. */
isAvailable(): boolean {
return (this.options.longPressOpensMenu?.() ?? true) && this.options.items().length > 0;
}
/**
* Open the menu, or the shell's system menu when this window has no
* entries. `items` overrides the window's own entries for an app that
* reuses this host for another list (a per-row action menu).
*/
open(items: MenuItem[] = this.options.items()): void {
if (this.stack) return;
if (!items.length) {
this.options.post({ type: "open-system-menu", windowId: this.options.windowId });
return;
}
const base: Layer = {
paint: () => this.options.paintBase(),
handleInput: () => {},
@@ -59,9 +99,23 @@ export class WindowMenu {
this.stack = stack;
}
/** Paint the window content with the menu over it (content alone if closed). */
paint(): Plane[] {
return this.stack ? this.stack.paint() : singlePlane(this.options.paintBase());
/**
* Paint the window content with the menu over it. `content` paints the
* closed-state content (default: paintBase alone), for windows whose
* ordinary frame has more planes than the menu needs beneath it.
*/
paint(content?: () => Plane[]): Plane[] {
this.syncAvailability();
if (this.stack) return this.stack.paint();
return content ? content() : singlePlane(this.options.paintBase());
}
/** Tell the shell when this window gains or loses its context menu. */
private syncAvailability(): void {
const available = this.isAvailable();
if (available === this.reportedAvailable) return;
this.reportedAvailable = available;
this.options.post({ type: "set-app-menu-available", windowId: this.options.windowId, available });
}
/** Route an input event to the menu; the menu closes by popping itself. */
@@ -81,38 +135,3 @@ export class WindowMenu {
}
}
}
/**
* The default entries every window menu ends with, in worker form: the
* actions cross to the shell as WorkerAppReply messages. In-process windows
* build the equivalent items against the shell directly (in-process-window.ts).
*/
export function defaultWindowMenuItems(
windowId: string,
post: (reply: WorkerAppReply) => void,
): MenuItem[] {
return [
{
// Defocus the app (hand focus to the sidebar) without closing it.
label: "Focus app switcher",
onSelect: (ctx) => {
ctx.stack.pop();
post({ type: "yield-focus", windowId });
},
},
{
label: "Voice input",
onSelect: (ctx) => {
ctx.stack.pop();
post({ type: "start-voice-input", windowId });
},
},
{
label: "Close window",
onSelect: (ctx) => {
ctx.stack.pop();
post({ type: "close-window-request", windowId });
},
},
];
}