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

+17 -36
View File
@@ -31,9 +31,11 @@ export type InProcessWindowOptions = {
/** Window height: the standard 288px band ("min", default) or full screen ("max"). */
heightMode?: WindowHeightMode;
/**
* App-specific entries for the window's long-press menu, listed ahead of
* the default Voice input / Close window entries. Called at open time, so
* the items can reflect current app state.
* The window's long-press context menu: app-specific entries only (the
* shared Focus app switcher / Voice input / Close window entries live in
* the shell's system menu). Called at open time, so the items can reflect
* current app state. Omitted or empty means the window has no menu of its
* own, and a long-press opens the system menu instead.
*/
menuItems?: () => MenuItem[];
/** Shared actions; requestRender is rebound to this window's render. */
@@ -128,39 +130,16 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
}
}
// The window's long-press menu: app-specific items, then the defaults every
// window shares. In-process apps run on the main thread, so the default
// items act on the shell directly (workers post messages instead).
const appMenuItems = () => options.menuItems?.() ?? [];
// The window's long-press menu: the app's own entries, or the shell's
// system menu when it has none (so both gestures land on the same menu).
const openWindowMenu = () => {
if (stack.topMatches((layer) => layer instanceof WindowMenuLayer)) return;
// "Focus app switcher" first: long-press then tap defocuses the app
// (hands focus to the sidebar) without closing it — the reliable way out
// for apps that consume double-click.
const items: MenuItem[] = [
{
label: "Focus app switcher",
onSelect: (ctx) => {
ctx.stack.pop();
shell.yieldFocusToSidebar();
},
},
...(options.menuItems?.() ?? []),
];
items.push({
label: "Voice input",
onSelect: (ctx) => {
ctx.stack.pop();
shell.startVoiceInput();
},
});
if (options.closeable) {
items.push({
label: "Close window",
onSelect: (ctx) => {
ctx.stack.pop();
shell.closeWindow(options.windowId);
},
});
const items = appMenuItems();
if (!items.length) {
shell.openSystemMenu(options.windowId);
return;
}
stack.push(new WindowMenuLayer(options.title, items));
};
@@ -175,6 +154,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
// directional or falls back to click / double-click.
acceptsDirectional: true,
heightMode,
hasAppMenu: () => appMenuItems().length > 0,
close: () => {
closed = true;
// Fire onRemoved for any pushed layers so they release resources (e.g. a
@@ -193,8 +173,9 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
}
return;
}
// The default long-press response: the window menu. Handled here (not
// per-layer) so it works over submenus and app content alike.
// The default long-press response: the window menu (or the system
// menu in its place). Handled here (not per-layer) so it works over
// submenus and app content alike.
if (event.type === "long-press") {
openWindowMenu();
await render(frameId);
+87 -21
View File
@@ -5,6 +5,8 @@ import { EvenAIStatus, EventSourceType, OsEventTypeList, WatchGestureType } from
import type { RawInputEvent } from "../../native/faceclaw-communicator";
import {
directionalFallback,
GESTURE_LONG_PRESS,
gestureHints,
InputEvent,
type InputEventPayload,
type InputSource,
@@ -59,10 +61,11 @@ import {
* everything while the sidebar or a shell overlay has focus and forwards the
* rest to the focused window. Long-press goes to the foreground window (from
* the sidebar it focuses the window first); by convention apps answer it with
* a window menu that ends in Voice input / Close window. Holding the press
* past the escape threshold opens the shell's own escape menu, so the shell
* keeps working when a window's handler hangs. The 2.2.9 tap-then-hold gesture
* opens that shell menu immediately.
* their own context menu, or ask for the system menu when they have none.
* The system menu (Focus app switcher, Voice input, Close window, Debug) is
* shell-owned: the 2.2.9 tap-then-hold gesture opens it directly, and holding
* a long-press past the escape threshold opens it too, so the shell keeps
* working when a window's handler hangs.
*/
export type ShellWindow = {
@@ -71,8 +74,15 @@ export type ShellWindow = {
title: string;
/** Compositor surface this window renders to; configured at connect / launch. */
surfaceId: string;
/** Whether close menu entries (app menu default, shell escape menu) apply (the launcher is pinned). */
/** Whether the system menu offers Close window (the launcher is pinned). */
closeable: boolean;
/**
* True when a long-press currently opens the window's own context menu
* (with at least one entry). While false, the system menu shows no
* "long-press: app menu" hint and a long-press over the open system menu
* leaves it open instead of switching menus.
*/
hasAppMenu?: () => boolean;
/**
* True when the window gives swipe-left / swipe-right (watch directional
* input) a meaning; otherwise the shell forwards directionalFallback(event).
@@ -167,9 +177,16 @@ const noopActions: LayerActions = noopLayerActions;
*/
const LONG_PRESS_ESCAPE_MENU_MS = 4000;
/** Shell-surface overlay menu (the escape menu); closing it returns focus to the sidebar. */
/** Shell-surface overlay menu (the system menu); closing it returns focus to the sidebar. */
class ShellOverlayMenuLayer extends MenuLayer {
constructor(items: MenuItem[], private readonly onClosed: () => void) {
/**
* Set before popping to keep focus on the window: an entry that acts on
* the window (Voice input aims its transcript at it) must not first hand
* focus to the sidebar.
*/
keepWindowFocus = false;
constructor(items: MenuItem[], footer: string | undefined, private readonly onClosed: () => void) {
// Aligned to the min-height window band (like the sidebar), wherever the
// vertical position setting currently puts it; centered over the
// application area, i.e. the part of the screen past the sidebar strip
@@ -180,11 +197,12 @@ class ShellOverlayMenuLayer extends MenuLayer {
y: minWindowTop() + TOP_BAR_HEIGHT + 8,
width,
minHeight: 150,
footer,
});
}
onRemoved(): void {
this.onClosed();
if (!this.keepWindowFocus) this.onClosed();
}
}
@@ -645,16 +663,20 @@ class Shell {
}
// Long-press goes to the foreground window (from the sidebar it focuses
// the window first); apps conventionally answer it with their window
// menu. The escape timer runs regardless of what the app does with it:
// holding the press long enough opens the shell's own menu.
// the window first); apps conventionally answer it with their context
// menu, or ask for the system menu when they have none. The escape timer
// runs regardless of what the app does with it: holding the press long
// enough opens the shell's own menu.
if (event.type === "long-press") {
this.startEscapeMenuTimer();
const window = this.foregroundWindow();
// A long-press while the system menu is open switches to the app's
// context menu: close the system menu and deliver the press to the
// window as usual (apps conventionally answer it with their menu).
// window as usual. A window without a menu of its own would only ask
// for the system menu back, so for it the open menu stays.
if (
!this.activeVoiceLayer &&
window?.hasAppMenu?.() &&
this.stack.popIfTop((layer) => layer instanceof ShellOverlayMenuLayer)
) {
this.config.requestShellRender();
@@ -662,7 +684,6 @@ class Shell {
if (this.activeVoiceLayer || !this.stack.isAtBase()) {
return { shell: true, window: false };
}
const window = this.foregroundWindow();
if (!window) {
return { shell: true, window: false };
}
@@ -931,9 +952,9 @@ class Shell {
/**
* Open the voice dialog aimed at the foreground window. Called when the
* user picks Voice input from a window's menu (in-process directly, worker
* apps via a start-voice-input message). The menu click already ended the
* press, so the dialog finishes on click instead of long-press-release.
* user picks Voice input from the system menu (or an app asks via a
* start-voice-input message). The menu click already ended the press, so
* the dialog finishes on click instead of long-press-release.
*/
startVoiceInput(): void {
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
@@ -1163,15 +1184,28 @@ class Shell {
}
/**
* The system/escape menu. Shell-owned and shell-drawn (never the app's), so
* an unresponsive app can always be closed. It opens after an extended hold
* or immediately for tap-then-hold, over whatever the app did with an earlier
* ordinary long-press, including opening its own menu.
* A window's answer to a long-press when it has no context menu of its
* own: open the system menu in its place, so both gestures land on the
* same menu. Ignored unless the window is still the foreground one.
*/
openSystemMenu(windowId: string): void {
if (this.foregroundWindow()?.windowId !== windowId) return;
this.openEscapeMenu();
}
/**
* The system/escape menu: the entries every window shares (Focus app
* switcher, Voice input, Close window) plus Debug. Shell-owned and
* shell-drawn (never the app's), so an unresponsive app can always be
* closed. It opens for tap-then-hold, after an extended hold (over whatever
* the app did with the ordinary long-press, including opening its own
* menu), and on a window's request when it has no menu of its own.
*/
private openEscapeMenu(): void {
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
const foreground = this.foregroundWindow();
if (!foreground) return;
let layer: ShellOverlayMenuLayer;
const items: MenuItem[] = [];
if (foreground.closeable) {
items.push({
@@ -1184,6 +1218,30 @@ class Shell {
},
});
}
// Close window sits first but the menu opens on Focus app switcher, so a
// reflexive tap never closes the window.
const initialSelection = items.length;
items.push(
{
// Defocus the app (hand focus to the sidebar) without closing it: the
// reliable way out of an app that consumes double-click. Closing the
// menu is what yields focus, so popping is the whole action.
label: "Focus app switcher",
onSelect: (ctx) => {
ctx.stack.pop();
},
},
{
label: "Voice input",
onSelect: (ctx) => {
// The transcript is aimed at the foreground window, so keep focus
// there through the pop instead of yielding to the sidebar.
layer.keepWindowFocus = true;
ctx.stack.pop();
this.startVoiceInput();
},
},
);
items.push({
label: "Debug",
onSelect: (ctx) => {
@@ -1191,7 +1249,15 @@ class Shell {
this.openToolDebugDialog();
},
});
this.stack.push(new ShellOverlayMenuLayer(items, () => this.yieldFocusToSidebar()));
// Gesture help: a long-press switches to the app's own context menu, when
// the app has one (otherwise it either does something app-specific or
// opens this very menu, neither worth a hint).
const footer = foreground.hasAppMenu?.()
? gestureHints([[GESTURE_LONG_PRESS, "app menu"]])
: undefined;
layer = new ShellOverlayMenuLayer(items, footer, () => this.yieldFocusToSidebar());
layer.selectItem(initialSelection);
this.stack.push(layer);
// Tell the window the system menu opened over it: an app with its own
// context menu up closes it, so the two context menus never stack.
void foreground.handleInput(makeInputEvent({ type: "system-menu-opened" }), 0);
+36 -2
View File
@@ -62,15 +62,33 @@ export type WorkerAppReply =
| { type: "set-title"; windowId: string; title: string }
| { type: "set-attention"; windowId: string; attention: boolean }
| {
/** Window-menu pick: open the shell's voice dialog aimed at this window. */
/** Open the shell's voice dialog aimed at this window (a menu pick). */
type: "start-voice-input";
windowId: string;
}
| {
/** Window-menu pick: close this window (the shell owns the close path). */
/** Close this window (the shell owns the close path). */
type: "close-window-request";
windowId: string;
}
| {
/**
* The window's answer to a long-press when it has no context menu of
* its own: open the shell's system menu in its place.
*/
type: "open-system-menu";
windowId: string;
}
| {
/**
* Whether a long-press currently opens the window's own context menu
* (with something in it). Posted on change; the shell's system menu
* shows its "long-press: app menu" hint only while this is true.
*/
type: "set-app-menu-available";
windowId: string;
available: boolean;
}
| {
/** Open or focus the Settings app, optionally jumping to a section. */
type: "open-settings";
@@ -172,6 +190,8 @@ const TOOL_CALL_HOST_TIMEOUT_MS = 15_000;
export class WorkerAppHost {
private readonly openWindows = new Set<string>();
/** Windows that reported a context menu of their own (see set-app-menu-available). */
private readonly appMenuAvailable = new Set<string>();
private readonly pendingToolCalls = new Map<string, PendingToolCall>();
private nextCallSerial = 1;
/**
@@ -241,6 +261,18 @@ export class WorkerAppHost {
this.options.requestShellRender();
}
break;
case "open-system-menu":
if (this.openWindows.has(message.windowId)) {
shell.openSystemMenu(message.windowId);
}
break;
case "set-app-menu-available":
if (message.available) {
this.appMenuAvailable.add(message.windowId);
} else {
this.appMenuAvailable.delete(message.windowId);
}
break;
case "open-settings":
this.options.openSettings(message.section);
break;
@@ -314,8 +346,10 @@ export class WorkerAppHost {
closeable: true,
acceptsDirectional: spec.acceptsDirectional,
heightMode,
hasAppMenu: () => this.appMenuAvailable.has(spec.windowId),
close: () => {
this.openWindows.delete(spec.windowId);
this.appMenuAvailable.delete(spec.windowId);
// Withdraw this window's tools and fail any in-flight calls to it.
toolRegistry.removeAppTools(spec.windowId);
this.failPendingToolCallsFor(spec.windowId);