forked from Blendman974/faceclaw
Redesign handling of long-press gesture and context menus
This commit is contained in:
1 parent
3452945322
commit
afc05d6a19
10 files changed
+455
-56
No files matched your search
@@ -836,9 +836,10 @@ class DashboardController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Launch or focus the (in-process, singleton) Settings app. */
|
||||
private async launchSettingsApp(): Promise<void> {
|
||||
/** Launch or focus the (in-process, singleton) Settings app, optionally on a section. */
|
||||
private async launchSettingsApp(section?: string): Promise<void> {
|
||||
if (this.settingsApp) {
|
||||
if (section) this.settingsApp.focusSection(section);
|
||||
shell.focusWindow(SETTINGS_WINDOW_ID);
|
||||
this.requestShellRender();
|
||||
return;
|
||||
@@ -859,6 +860,7 @@ class DashboardController {
|
||||
},
|
||||
});
|
||||
this.settingsApp = settingsApp;
|
||||
if (section) settingsApp.focusSection(section);
|
||||
shell.registerWindow(settingsApp.window);
|
||||
// Configure the surface before foregrounding so the first frame has
|
||||
// somewhere to land.
|
||||
@@ -921,6 +923,11 @@ class DashboardController {
|
||||
setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible),
|
||||
removeSurface: (surfaceId) => this.removeWindowSurface(surfaceId),
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
openSettings: (section) => {
|
||||
void this.launchSettingsApp(section).catch((error) => {
|
||||
this.appendLog(`settings launch failed: ${this.formatError(error)}`);
|
||||
});
|
||||
},
|
||||
});
|
||||
this.appHosts.set(appId, host);
|
||||
return host;
|
||||
|
||||
@@ -50,6 +50,15 @@ export class SettingsPanelLayer implements Layer {
|
||||
return this.sections[clamp(this.leftIndex, 0, this.sections.length - 1)]!;
|
||||
}
|
||||
|
||||
/** Select a left-column section by label (deep link, e.g. from an app's menu). */
|
||||
focusSection(label: string): void {
|
||||
const index = this.sections.findIndex((section) => section.label === label);
|
||||
if (index < 0) return;
|
||||
this.leftIndex = index;
|
||||
this.focus = "left";
|
||||
this.resetRight();
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
|
||||
@@ -40,6 +40,19 @@ export type LayerActions = {
|
||||
playBuzzerSequence: (payload: Uint8Array) => Promise<void> | void;
|
||||
};
|
||||
|
||||
/** Do-nothing actions, for stacks whose layers never use them (or as a base to spread over). */
|
||||
export const noopLayerActions: LayerActions = {
|
||||
requestRender: () => {},
|
||||
disconnect: () => {},
|
||||
startTextSettingEdit: () => {},
|
||||
endTextSettingEdit: () => {},
|
||||
startVoiceCapture: () => {},
|
||||
stopVoiceCapture: () => {},
|
||||
startContinuousVoiceCapture: () => {},
|
||||
stopContinuousVoiceCapture: () => {},
|
||||
playBuzzerSequence: () => {},
|
||||
};
|
||||
|
||||
export type PaintBelow = () => GrayImage;
|
||||
|
||||
function notifyRemoved(layer: Layer | undefined): void {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { beginRenderPass, endRenderPass } from "../../util/render-freshness";
|
||||
import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, PaintBelow } from "../layers";
|
||||
import { type MenuItem } from "../menu";
|
||||
import { WindowMenuLayer } from "../window-menu";
|
||||
import { windowIcon } from "./chrome-layer";
|
||||
import { type IconName } from "../../graphics/icons";
|
||||
import { APP_VIEWPORT } from "./geometry";
|
||||
@@ -21,6 +24,12 @@ export type InProcessWindowOptions = {
|
||||
/** Lucide icon name for the sidebar indicator; falls back to iconLetter. */
|
||||
icon?: IconName;
|
||||
closeable: boolean;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
menuItems?: () => MenuItem[];
|
||||
/** Shared actions; requestRender is rebound to this window's render. */
|
||||
actions: LayerActions;
|
||||
baseLayer: Layer;
|
||||
@@ -62,8 +71,15 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
// to keep the paint fast; when they do, schedule one follow-up render that
|
||||
// requires fresh data (same contract as the dashboard render loop).
|
||||
let nextRenderWantsFreshData = false;
|
||||
let closed = false;
|
||||
|
||||
async function render(frameId: number): Promise<void> {
|
||||
if (closed) {
|
||||
// The surface is gone (e.g. Close window picked from this window's own
|
||||
// menu); dropping the frame beats submitting to a removed surface.
|
||||
frameTimings.finishFrame(frameId, "discarded: window closed");
|
||||
return;
|
||||
}
|
||||
const wantFreshData = nextRenderWantsFreshData;
|
||||
nextRenderWantsFreshData = false;
|
||||
beginRenderPass(!wantFreshData);
|
||||
@@ -77,6 +93,31 @@ 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 openWindowMenu = () => {
|
||||
if (stack.topMatches((layer) => layer instanceof WindowMenuLayer)) return;
|
||||
const items: MenuItem[] = [...(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);
|
||||
},
|
||||
});
|
||||
}
|
||||
stack.push(new WindowMenuLayer(items));
|
||||
};
|
||||
|
||||
const window: ShellWindow = {
|
||||
appId: options.appId,
|
||||
windowId: options.windowId,
|
||||
@@ -84,6 +125,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
surfaceId: `window:${options.windowId}`,
|
||||
closeable: options.closeable,
|
||||
close: () => {
|
||||
closed = true;
|
||||
// Fire onRemoved for any pushed layers so they release resources (e.g. a
|
||||
// demo that enabled a hardware stream) even when closed from within.
|
||||
stack.clearToBase();
|
||||
@@ -92,6 +134,13 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
|
||||
},
|
||||
drawIcon: windowIcon(options.icon, options.iconLetter),
|
||||
handleInput: async (event, frameId) => {
|
||||
// The default long-press response: the window menu. Handled here (not
|
||||
// per-layer) so it works over submenus and app content alike.
|
||||
if (event.type === "long-press") {
|
||||
openWindowMenu();
|
||||
await render(frameId);
|
||||
return;
|
||||
}
|
||||
await stack.handleInput(event);
|
||||
await render(frameId);
|
||||
},
|
||||
|
||||
@@ -19,6 +19,8 @@ export type SettingsAppOptions = {
|
||||
export type SettingsAppWindow = {
|
||||
window: ShellWindow;
|
||||
requestRender: () => void;
|
||||
/** Select a section in the left column by label (e.g. "Terminal"). */
|
||||
focusSection: (label: string) => void;
|
||||
/** Whether the glasses-side text-setting editor is the top layer. */
|
||||
isTextEditorOnTop: () => boolean;
|
||||
/** Pop the text-setting editor if it is on top; returns whether it was. */
|
||||
@@ -32,6 +34,7 @@ export type SettingsAppWindow = {
|
||||
* controller, which lives on the main thread.
|
||||
*/
|
||||
export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAppWindow {
|
||||
const panel = createSettingsPanelLayer();
|
||||
const { window, stack, requestRender } = createInProcessWindow({
|
||||
appId: "settings",
|
||||
windowId: SETTINGS_WINDOW_ID,
|
||||
@@ -42,7 +45,7 @@ export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAp
|
||||
actions: options.actions,
|
||||
// Not wrapped in YieldAtRootLayer: the panel routes double-click itself
|
||||
// (right column -> left column, then left column -> sidebar).
|
||||
baseLayer: createSettingsPanelLayer(),
|
||||
baseLayer: panel,
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
@@ -51,6 +54,10 @@ export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAp
|
||||
return {
|
||||
window,
|
||||
requestRender,
|
||||
focusSection: (label) => {
|
||||
panel.focusSection(label);
|
||||
requestRender();
|
||||
},
|
||||
isTextEditorOnTop: () => stack.topMatches((layer) => layer instanceof EditTextSettingLayer),
|
||||
closeTextEditor: () => stack.popIfTop((layer) => layer instanceof EditTextSettingLayer),
|
||||
};
|
||||
|
||||
+104
-52
@@ -1,7 +1,7 @@
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
|
||||
import { EvenAIStatus, EventSourceType, OsEventTypeList } from "../../g2/events";
|
||||
import type { RawInputEvent } from "../../native/faceclaw-communicator";
|
||||
import { DashboardInputEvent, LayerActions, LayerStack } from "../layers";
|
||||
import { DashboardInputEvent, LayerActions, LayerStack, noopLayerActions } from "../layers";
|
||||
import { MenuLayer, type MenuItem } from "../menu";
|
||||
import { VoiceInputLayer } from "../apps/voice-input";
|
||||
import { SingleNotificationLayer } from "../notifications";
|
||||
@@ -17,15 +17,17 @@ import { SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry";
|
||||
|
||||
/**
|
||||
* The shell: owns the window registry, focus, screen on/off, and the shell
|
||||
* surface (sidebar + top bar + shell overlays such as the long-press menu and
|
||||
* the push-to-talk dialog). Runs on the main thread; windows will later live
|
||||
* in worker threads, today the only window is the in-process dashboard.
|
||||
* surface (sidebar + top bar + shell overlays such as the escape menu and
|
||||
* the voice dialog). Runs on the main thread; windows are hosted in-process
|
||||
* or in per-app worker threads.
|
||||
*
|
||||
* Input flow: every event enters via receiveInput. The shell consumes its
|
||||
* reserved gestures (long-press, and everything while the sidebar or a shell
|
||||
* overlay has focus) and forwards the rest to the focused window. Windows
|
||||
* never see events the shell consumed, and the shell keeps working when a
|
||||
* window's handler hangs (the long-press menu is the escape hatch).
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export type ShellWindow = {
|
||||
@@ -34,7 +36,7 @@ export type ShellWindow = {
|
||||
title: string;
|
||||
/** Compositor surface this window renders to; configured at connect / launch. */
|
||||
surfaceId: string;
|
||||
/** Whether the shell's long-press menu offers Close Window (launcher and dashboard are pinned). */
|
||||
/** Whether close menu entries (app menu default, shell escape menu) apply (the launcher is pinned). */
|
||||
closeable: boolean;
|
||||
/** App-side cleanup when the shell closes the window (worker notification, surface removal). */
|
||||
close?: () => void;
|
||||
@@ -72,19 +74,16 @@ export type ShellInputOutcome = { shell: boolean; window: boolean };
|
||||
|
||||
type FocusKind = "sidebar" | "window";
|
||||
|
||||
const noopActions: LayerActions = {
|
||||
requestRender: () => {},
|
||||
disconnect: () => {},
|
||||
startTextSettingEdit: () => {},
|
||||
endTextSettingEdit: () => {},
|
||||
startVoiceCapture: () => {},
|
||||
stopVoiceCapture: () => {},
|
||||
startContinuousVoiceCapture: () => {},
|
||||
stopContinuousVoiceCapture: () => {},
|
||||
playBuzzerSequence: () => {},
|
||||
};
|
||||
const noopActions: LayerActions = noopLayerActions;
|
||||
|
||||
/** Long-press overlay menu; closing it returns focus to the sidebar. */
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
const LONG_PRESS_ESCAPE_MENU_MS = 4000;
|
||||
|
||||
/** Shell-surface overlay menu (the escape menu); closing it returns focus to the sidebar. */
|
||||
class ShellOverlayMenuLayer extends MenuLayer {
|
||||
constructor(items: MenuItem[], private readonly onClosed: () => void) {
|
||||
super(null, items, {
|
||||
@@ -112,6 +111,7 @@ class Shell {
|
||||
// notification icons and the battery indicators.
|
||||
private readonly trayIcons = new Map<string, GrayImage>();
|
||||
private activeVoiceLayer: VoiceInputLayer | null = null;
|
||||
private escapeMenuTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private readonly actions: LayerActions = { ...noopActions };
|
||||
private config: ShellConfig = {
|
||||
actions: noopActions,
|
||||
@@ -186,9 +186,9 @@ class Shell {
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
/** Close the foreground window via the shell (long-press menu action). */
|
||||
closeForegroundWindow(): void {
|
||||
const window = this.foregroundWindow();
|
||||
/** Close a window by id, if it is closeable (menu actions route here). */
|
||||
closeWindow(windowId: string): void {
|
||||
const window = this.windows.find((w) => w.windowId === windowId);
|
||||
if (!window || !window.closeable) return;
|
||||
try {
|
||||
window.close?.();
|
||||
@@ -198,6 +198,12 @@ class Shell {
|
||||
this.removeWindow(window.windowId);
|
||||
}
|
||||
|
||||
/** Close the foreground window via the shell (escape menu action). */
|
||||
closeForegroundWindow(): void {
|
||||
const window = this.foregroundWindow();
|
||||
if (window) this.closeWindow(window.windowId);
|
||||
}
|
||||
|
||||
getWindows(): readonly ShellWindow[] {
|
||||
return this.windows;
|
||||
}
|
||||
@@ -253,6 +259,7 @@ class Shell {
|
||||
/** Turn the screen off, closing any shell overlays. Sidebar selection is kept. */
|
||||
sleep(): void {
|
||||
if (!this.screenOn) return;
|
||||
this.cancelEscapeMenuTimer();
|
||||
this.screenOn = false;
|
||||
this.stack.clearToBase();
|
||||
for (const window of this.windows) {
|
||||
@@ -350,6 +357,12 @@ class Shell {
|
||||
|
||||
this.lastInputAtMs = Date.now();
|
||||
|
||||
// Anything but the long-press itself means the press ended (or the event
|
||||
// stream moved on), so the escape countdown stops.
|
||||
if (event.type !== "long-press") {
|
||||
this.cancelEscapeMenuTimer();
|
||||
}
|
||||
|
||||
if (!this.screenOn) {
|
||||
if (event.type === "double-click") {
|
||||
this.wake("sidebar");
|
||||
@@ -358,21 +371,37 @@ class Shell {
|
||||
return { shell: false, window: false };
|
||||
}
|
||||
|
||||
// Long-press is reserved by the shell: PTT from the sidebar, the overlay
|
||||
// menu from inside a window. Windows never see it.
|
||||
// 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.
|
||||
if (event.type === "long-press") {
|
||||
if (!this.activeVoiceLayer && this.stack.isAtBase()) {
|
||||
if (this.focus === "sidebar") {
|
||||
this.openVoiceDialog();
|
||||
} else {
|
||||
this.openOverlayMenu();
|
||||
}
|
||||
this.startEscapeMenuTimer();
|
||||
if (this.activeVoiceLayer || !this.stack.isAtBase()) {
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
return { shell: true, window: false };
|
||||
const window = this.foregroundWindow();
|
||||
if (!window) {
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
if (this.focus === "sidebar") {
|
||||
this.focus = "window";
|
||||
}
|
||||
// The window owns frameId from here (render or explicit finish).
|
||||
await window.handleInput(event, frameId);
|
||||
return { shell: true, window: true };
|
||||
}
|
||||
if (event.type === "long-press-release") {
|
||||
this.activeVoiceLayer?.endCapture();
|
||||
return { shell: true, window: false };
|
||||
if (this.activeVoiceLayer || !this.stack.isAtBase() || this.focus !== "window") {
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
const window = this.foregroundWindow();
|
||||
if (!window) {
|
||||
return { shell: true, window: false };
|
||||
}
|
||||
await window.handleInput(event, frameId);
|
||||
return { shell: false, window: true };
|
||||
}
|
||||
|
||||
if (!this.stack.isAtBase()) {
|
||||
@@ -476,24 +505,46 @@ class Shell {
|
||||
this.foregroundWindow()?.receiveTextInput?.(text);
|
||||
}
|
||||
|
||||
private openOverlayMenu(): void {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
startVoiceInput(): void {
|
||||
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
|
||||
// The transcript is aimed at the window whose menu requested it.
|
||||
this.focus = "window";
|
||||
this.openVoiceDialog(true);
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
private startEscapeMenuTimer(): void {
|
||||
this.cancelEscapeMenuTimer();
|
||||
this.escapeMenuTimer = setTimeout(() => {
|
||||
this.escapeMenuTimer = null;
|
||||
this.openEscapeMenu();
|
||||
}, LONG_PRESS_ESCAPE_MENU_MS);
|
||||
}
|
||||
|
||||
private cancelEscapeMenuTimer(): void {
|
||||
if (this.escapeMenuTimer !== null) {
|
||||
clearTimeout(this.escapeMenuTimer);
|
||||
this.escapeMenuTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The held-long-press safeguard menu. Shell-owned and shell-drawn (never
|
||||
* the app's), so an unresponsive app can always be closed. It opens over
|
||||
* whatever the app did with the long-press, including its own menu.
|
||||
*/
|
||||
private openEscapeMenu(): void {
|
||||
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
|
||||
const foreground = this.foregroundWindow();
|
||||
if (!foreground?.closeable) return;
|
||||
const items: MenuItem[] = [
|
||||
{
|
||||
label: "Voice input",
|
||||
onSelect: (ctx) => {
|
||||
// Pop the menu first (its onRemoved yields focus to the sidebar),
|
||||
// then take focus back: the transcript is aimed at the window the
|
||||
// menu was opened over. The button is no longer held here, so the
|
||||
// dialog finishes on click instead of long-press-release.
|
||||
ctx.stack.pop();
|
||||
this.focus = "window";
|
||||
this.openVoiceDialog(true);
|
||||
},
|
||||
},
|
||||
];
|
||||
if (foreground?.closeable) {
|
||||
items.push({
|
||||
label: "Close window",
|
||||
onSelect: (ctx) => {
|
||||
// Pop the menu first (its onRemoved returns focus to the sidebar),
|
||||
@@ -501,9 +552,10 @@ class Shell {
|
||||
ctx.stack.pop();
|
||||
this.closeForegroundWindow();
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
];
|
||||
this.stack.push(new ShellOverlayMenuLayer(items, () => this.yieldFocusToSidebar()));
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
private chromeState(): ShellChromeState {
|
||||
|
||||
@@ -36,6 +36,21 @@ 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. */
|
||||
type: "start-voice-input";
|
||||
windowId: string;
|
||||
}
|
||||
| {
|
||||
/** Window-menu pick: close this window (the shell owns the close path). */
|
||||
type: "close-window-request";
|
||||
windowId: string;
|
||||
}
|
||||
| {
|
||||
/** Open or focus the Settings app, optionally jumping to a section. */
|
||||
type: "open-settings";
|
||||
section?: string;
|
||||
}
|
||||
| {
|
||||
/**
|
||||
* Set or clear the app's top-bar tray icon. Pixels ride the JSON
|
||||
@@ -66,6 +81,8 @@ export type WorkerAppHostOptions = {
|
||||
setSurfaceVisible: (surfaceId: string, visible: boolean) => void;
|
||||
removeSurface: (surfaceId: string) => void;
|
||||
requestShellRender: () => void;
|
||||
/** Open or focus the Settings app, optionally selecting a section. */
|
||||
openSettings: (section?: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -107,6 +124,22 @@ export class WorkerAppHost {
|
||||
case "set-attention":
|
||||
shell.setWindowAttention(message.windowId, message.attention);
|
||||
break;
|
||||
case "start-voice-input":
|
||||
// Menus only open on the focused window, but re-check foreground:
|
||||
// the reply crosses a thread boundary and focus may have moved.
|
||||
if (shell.foregroundWindow()?.windowId === message.windowId) {
|
||||
shell.startVoiceInput();
|
||||
}
|
||||
break;
|
||||
case "close-window-request":
|
||||
if (this.openWindows.has(message.windowId)) {
|
||||
shell.closeWindow(message.windowId);
|
||||
this.options.requestShellRender();
|
||||
}
|
||||
break;
|
||||
case "open-settings":
|
||||
this.options.openSettings(message.section);
|
||||
break;
|
||||
case "set-tray-icon": {
|
||||
let icon: GrayImage | null = null;
|
||||
if (message.icon) {
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import {
|
||||
noopLayerActions,
|
||||
LayerStack,
|
||||
type DashboardInputEvent,
|
||||
type Layer,
|
||||
} 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.
|
||||
*/
|
||||
|
||||
/** Viewport-relative layout; visually matches the shell's escape menu position. */
|
||||
export const WINDOW_MENU_LAYOUT: MenuLayout = { x: 8, y: 8, width: 272, minHeight: 0 };
|
||||
|
||||
export class WindowMenuLayer extends MenuLayer {
|
||||
constructor(items: MenuItem[]) {
|
||||
super(null, items, WINDOW_MENU_LAYOUT);
|
||||
}
|
||||
}
|
||||
|
||||
export type WindowMenuOptions = {
|
||||
/** Paint the window content the menu draws over (a fresh, mutable image). */
|
||||
paintBase: () => GrayImage;
|
||||
size: { width: number; height: number };
|
||||
isFocused: () => boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Menu host for worker apps, which paint pixels directly rather than through
|
||||
* 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.
|
||||
*/
|
||||
export class WindowMenu {
|
||||
private stack: LayerStack | null = null;
|
||||
|
||||
constructor(private readonly options: WindowMenuOptions) {}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.stack !== null;
|
||||
}
|
||||
|
||||
open(items: MenuItem[]): void {
|
||||
if (this.stack) return;
|
||||
const base: Layer = {
|
||||
paint: () => this.options.paintBase(),
|
||||
handleInput: () => {},
|
||||
};
|
||||
const stack = new LayerStack(base, { ...noopLayerActions }, this.options.size, this.options.isFocused);
|
||||
stack.push(new WindowMenuLayer(items));
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
/** Paint the window content with the menu over it (content alone if closed). */
|
||||
paint(): GrayImage {
|
||||
return this.stack ? this.stack.paint() : this.options.paintBase();
|
||||
}
|
||||
|
||||
/** Route an input event to the menu; the menu closes by popping itself. */
|
||||
async handleInput(event: DashboardInputEvent): Promise<void> {
|
||||
const stack = this.stack;
|
||||
if (!stack) return;
|
||||
await stack.handleInput(event);
|
||||
if (stack.isAtBase()) {
|
||||
this.stack = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 [
|
||||
{
|
||||
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 });
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in new issue
Block a user