Swap app and system context menu gestures

This commit is contained in:
James Babcock committed 2026-09-02 21:17:15 -07:00
1 parent 987f442897
commit 83c3732ba7
23 files changed
+213 -153

No files matched your search

+10 -8
View File
@@ -31,11 +31,11 @@ export type InProcessWindowOptions = {
/** Window height: the standard 288px band ("min", default) or full screen ("max"). */
heightMode?: WindowHeightMode;
/**
* The window's long-press context menu: app-specific entries only (the
* The window's tap-then-hold 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.
* the shell's system menu, on long-press). 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 tap-then-hold opens the system menu instead.
*/
menuItems?: () => MenuItem[];
/** Shared actions; requestRender is rebound to this window's render. */
@@ -132,7 +132,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
const appMenuItems = () => options.menuItems?.() ?? [];
// The window's long-press menu: the app's own entries, or the shell's
// The window's tap-then-hold 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;
@@ -173,10 +173,12 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
}
return;
}
// The default long-press response: the window menu (or the system
// The default tap-then-hold 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") {
// submenus and app content alike. A plain long-press never arrives:
// the shell keeps it for the system menu (no in-process window claims
// it).
if (event.type === "short-then-long-press") {
openWindowMenu();
await render(frameId);
return;
+66 -40
View File
@@ -5,7 +5,7 @@ import { EvenAIStatus, EventSourceType, OsEventTypeList, WatchGestureType } from
import type { RawInputEvent } from "../../native/faceclaw-communicator";
import {
directionalFallback,
GESTURE_LONG_PRESS,
GESTURE_SHORT_THEN_LONG_PRESS,
gestureHints,
InputEvent,
type InputEventPayload,
@@ -59,13 +59,14 @@ import {
*
* Input flow: every event enters via receiveInput. The shell consumes
* 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
* rest to the focused window. Long-press opens the shell-owned system menu
* (Focus app switcher, Voice input, Close window, Debug) without reaching the
* app, so the shell keeps working when a window's handler hangs; a window
* that claims long-press for a move of its own gets it forwarded instead, and
* holding the press past the escape threshold still opens the system menu.
* The 2.2.9 tap-then-hold gesture goes to the foreground window (from the
* sidebar it focuses the window first); by convention apps answer it with
* 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 = {
@@ -77,12 +78,18 @@ export type ShellWindow = {
/** 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
* True when tap-then-hold 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.
* app-menu hint and a tap-then-hold over the open system menu leaves it
* open instead of switching menus.
*/
hasAppMenu?: () => boolean;
/**
* True while the window gives long-press a meaning of its own (a game
* move). The shell then forwards long-presses to it instead of opening the
* system menu; holding the press past the escape threshold still opens it.
*/
claimsLongPress?: () => boolean;
/**
* True when the window gives swipe-left / swipe-right (watch directional
* input) a meaning; otherwise the shell forwards directionalFallback(event).
@@ -171,9 +178,10 @@ export type FocusKind = "sidebar" | "window";
const noopActions: LayerActions = noopLayerActions;
/**
* Hold a long-press this much past the long-press event (which the firmware
* itself only fires after a shorter hold) and the shell opens its own escape
* menu — the recovery path when an app ignores or mishandles the gesture.
* For a window that claims long-press: hold the press this much past the
* long-press event (which the firmware itself only fires after a shorter
* hold) and the shell opens the system menu anyway — the recovery path when
* the app ignores or mishandles the gesture.
*/
const LONG_PRESS_ESCAPE_MENU_MS = 4000;
@@ -652,28 +660,46 @@ class Shell {
return { shell: false, window: false };
}
// The 2.2.9 tap-then-hold gesture is a direct, shell-owned escape hatch.
// Unlike an ordinary long-press it never reaches the app or starts the
// fallback timer: it opens the same system menu that an extended hold
// would eventually open. Its later generic release is consumed while the
// shell overlay is active and therefore cannot leak into the app.
if (event.type === "short-then-long-press") {
this.openEscapeMenu();
return { shell: true, window: false };
// Long-press is the shell's own gesture: it opens the system menu
// directly, never reaching the app — over the app's own context menu
// too (the window closes that on system-menu-opened), while an already
// open system menu just stays. Its later generic release is consumed
// while the shell overlay is active and therefore cannot leak into the
// app. The exception is a window that claims long-press for a move of
// its own: it gets the press forwarded, with the escape timer running
// so that holding the press long enough still opens the system menu.
if (event.type === "long-press") {
if (this.activeVoiceLayer || !this.stack.isAtBase()) {
return { shell: true, window: false };
}
const window = this.foregroundWindow();
if (!window) {
return { shell: true, window: false };
}
if (!window.claimsLongPress?.()) {
this.openEscapeMenu();
return { shell: true, window: false };
}
this.startEscapeMenuTimer();
if (this.focus === "sidebar") {
this.focus = "window";
window.onFocus?.(this.lastInput);
}
// The window owns frameId from here (render or explicit finish).
await window.handleInput(event, frameId);
return { shell: true, window: true };
}
// Long-press goes to the foreground window (from the sidebar it focuses
// 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();
// The 2.2.9 tap-then-hold gesture is the app's context-menu gesture: it
// goes to the foreground window (from the sidebar it focuses the window
// first), which answers with its own menu or asks for the system menu
// when it has none.
if (event.type === "short-then-long-press") {
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. A window without a menu of its own would only ask
// for the system menu back, so for it the open menu stays.
// Over the open system menu it switches to the app's context menu:
// close the system menu and deliver the gesture to the 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?.() &&
@@ -1184,7 +1210,7 @@ class Shell {
}
/**
* A window's answer to a long-press when it has no context menu of its
* A window's answer to tap-then-hold 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.
*/
@@ -1197,9 +1223,9 @@ class Shell {
* 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.
* closed. It opens for long-press (over the app's own menu too), after an
* extended hold in a window that claims long-press, and on a window's
* request when tap-then-hold finds it has no menu of its own.
*/
private openEscapeMenu(): void {
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
@@ -1249,11 +1275,11 @@ class Shell {
this.openToolDebugDialog();
},
});
// 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).
// Gesture help: tap-then-hold switches to the app's own context menu,
// when the app has one (otherwise it opens this very menu, not worth a
// hint).
const footer = foreground.hasAppMenu?.()
? gestureHints([[GESTURE_LONG_PRESS, "app menu"]])
? gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "app menu"]])
: undefined;
layer = new ShellOverlayMenuLayer(items, footer, () => this.yieldFocusToSidebar());
layer.selectItem(initialSelection);
+19 -16
View File
@@ -73,7 +73,7 @@ export type WorkerAppReply =
}
| {
/**
* The window's answer to a long-press when it has no context menu of
* The window's answer to tap-then-hold when it has no context menu of
* its own: open the shell's system menu in its place.
*/
type: "open-system-menu";
@@ -81,13 +81,16 @@ export type WorkerAppReply =
}
| {
/**
* 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.
* The window's current gesture bindings, posted on change. hasAppMenu:
* tap-then-hold opens a context menu with something in it (the system
* menu shows its app-menu hint only then). claimsLongPress: the app
* gives long-press a meaning of its own, so the shell forwards it
* rather than opening the system menu (see ShellWindow).
*/
type: "set-app-menu-available";
type: "set-window-gestures";
windowId: string;
available: boolean;
hasAppMenu: boolean;
claimsLongPress: boolean;
}
| {
/** Open or focus the Settings app, optionally jumping to a section. */
@@ -190,8 +193,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>();
/** Per-window gesture bindings, as last reported (see set-window-gestures). */
private readonly windowGestures = new Map<string, { hasAppMenu: boolean; claimsLongPress: boolean }>();
private readonly pendingToolCalls = new Map<string, PendingToolCall>();
private nextCallSerial = 1;
/**
@@ -266,12 +269,11 @@ export class WorkerAppHost {
shell.openSystemMenu(message.windowId);
}
break;
case "set-app-menu-available":
if (message.available) {
this.appMenuAvailable.add(message.windowId);
} else {
this.appMenuAvailable.delete(message.windowId);
}
case "set-window-gestures":
this.windowGestures.set(message.windowId, {
hasAppMenu: message.hasAppMenu,
claimsLongPress: message.claimsLongPress,
});
break;
case "open-settings":
this.options.openSettings(message.section);
@@ -346,10 +348,11 @@ export class WorkerAppHost {
closeable: true,
acceptsDirectional: spec.acceptsDirectional,
heightMode,
hasAppMenu: () => this.appMenuAvailable.has(spec.windowId),
hasAppMenu: () => this.windowGestures.get(spec.windowId)?.hasAppMenu ?? false,
claimsLongPress: () => this.windowGestures.get(spec.windowId)?.claimsLongPress ?? false,
close: () => {
this.openWindows.delete(spec.windowId);
this.appMenuAvailable.delete(spec.windowId);
this.windowGestures.delete(spec.windowId);
// Withdraw this window's tools and fail any in-flight calls to it.
toolRegistry.removeAppTools(spec.windowId);
this.failPendingToolCallsFor(spec.windowId);