Redesign handling of long-press gesture and context menus

This commit is contained in:
James Babcock committed 2026-07-22 22:45:14 -07:00
1 parent 3452945322
commit afc05d6a19
10 files changed
+455 -56

No files matched your search

+82
View File
@@ -31,6 +31,8 @@ import {
} from "../ui/dashboard-settings";
import { TerminalEmulator } from "../ui/apps/terminal-emulator";
import type { DashboardInputEvent } from "../ui/layers";
import type { MenuItem } from "../ui/menu";
import { defaultWindowMenuItems, WindowMenu } from "../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window";
declare const global: any;
@@ -58,6 +60,8 @@ type BaseWindow = {
focused: boolean;
renderScheduled: boolean;
lastSubmittedFingerprint: string;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
};
type HubWindow = BaseWindow & {
@@ -195,6 +199,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
focused: false,
renderScheduled: false,
lastSubmittedFingerprint: "",
menu: null,
selectedIndex: 0,
});
if (!controlClient) {
@@ -284,6 +289,7 @@ function createViewWindow(windowId: string, surfaceId: string, socket: string, l
focused: false,
renderScheduled: false,
lastSubmittedFingerprint: "",
menu: null,
socket,
label,
client,
@@ -344,7 +350,76 @@ function createViewWindow(windowId: string, surfaceId: string, socket: string, l
return window;
}
/** The window's long-press menu, created lazily so window literals stay simple. */
function windowMenu(window: TerminalWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: viewportWidth, height: viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function windowMenuItems(window: TerminalWindow): MenuItem[] {
const items: MenuItem[] = [
{
label: "Settings",
onSelect: (ctx) => {
ctx.stack.pop();
post({ type: "open-settings", section: "Terminal" });
},
},
];
if (window.kind === "hub") {
const phase = controlState?.phase;
if (phase === "connected" || phase === "attached") {
items.push({
label: "Disconnect",
onSelect: (ctx) => {
ctx.stack.pop();
stopControlClient();
renderHubWindows();
},
});
}
} else {
items.push(
{
label: "Send <Enter>",
onSelect: (ctx) => {
ctx.stack.pop();
window.client.sendInput("\r");
},
},
{
label: "Send <Esc>",
onSelect: (ctx) => {
ctx.stack.pop();
window.client.sendInput("\u001b");
},
},
);
}
items.push(...defaultWindowMenuItems(window.windowId, post));
return items;
}
function handleInput(window: TerminalWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`terminal menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (event.type === "long-press") {
windowMenu(window).open(windowMenuItems(window));
renderAndSubmit(window, frameId);
return;
}
if (event.type === "double-click") {
frameTimings.finishFrame(frameId, "discarded: terminal yielded focus");
post({ type: "yield-focus", windowId: window.windowId });
@@ -493,6 +568,13 @@ function openViewWindowFor(session: G2MirrorSession): void {
}
function paint(window: TerminalWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: TerminalWindow): GrayImage {
return window.kind === "hub" ? paintHub(window) : paintView(window);
}
+44 -1
View File
@@ -13,6 +13,7 @@ import {
scheduleTimerNotification,
} from "../native/timer-notifications";
import type { DashboardInputEvent } from "../ui/layers";
import { defaultWindowMenuItems, WindowMenu } from "../ui/window-menu";
import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window";
import {
GESTURE_CLICK,
@@ -40,6 +41,10 @@ type TimerWindow = {
viewportWidth: number;
viewportHeight: number;
foreground: boolean;
/** Whether this window is the shell's input target (pushed with each message). */
focused: boolean;
/** Long-press window menu; created on first open. */
menu: WindowMenu | null;
view: TimerView;
stopwatchAction: number;
timerSelection: number;
@@ -81,6 +86,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
viewportWidth: message.viewport.width,
viewportHeight: message.viewport.height,
foreground: false,
focused: false,
menu: null,
view: "stopwatch",
stopwatchAction: 0,
timerSelection: 0,
@@ -106,18 +113,22 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
frameTimings.finishFrame(message.frameId, "discarded: unknown timer window");
break;
}
window.focused = message.focused;
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
case "render": {
const window = windows.get(message.windowId);
if (window) renderAndSubmit(window, 0);
if (!window) break;
window.focused = message.focused;
renderAndSubmit(window, 0);
break;
}
case "foreground": {
const window = windows.get(message.windowId);
if (!window) break;
window.foreground = message.foreground;
window.focused = message.focused;
updateRenderTimer(window);
if (window.foreground) renderAndSubmit(window, 0);
break;
@@ -129,7 +140,32 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
};
/** The window's long-press menu (default entries only), created lazily. */
function windowMenu(window: TimerWindow): WindowMenu {
if (!window.menu) {
window.menu = new WindowMenu({
size: { width: window.viewportWidth, height: window.viewportHeight },
paintBase: () => paintContent(window),
isFocused: () => window.focused,
});
}
return window.menu;
}
function handleInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
// An open window menu owns all input (it closes itself via pop).
if (window.menu?.isOpen()) {
window.menu
.handleInput(event)
.catch((error) => console.error(`timer menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
return;
}
if (event.type === "long-press") {
windowMenu(window).open(defaultWindowMenuItems(window.windowId, post));
renderAndSubmit(window, frameId);
return;
}
if (event.type === "double-click") {
if (window.view === "editor") {
window.view = "timers";
@@ -322,6 +358,13 @@ function updateRenderTimer(window: TimerWindow): void {
}
function paint(window: TimerWindow): GrayImage {
if (window.menu?.isOpen()) {
return window.menu.paint();
}
return paintContent(window);
}
function paintContent(window: TimerWindow): GrayImage {
syncExpiredTimers();
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
if (window.view === "stopwatch") {