diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index d96191c4..67b40735 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -94,7 +94,8 @@ import type { AppSwitcherPosition } from "../dashboard-settings"; * 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 opens the shell-owned system menu - * (Focus app switcher, Voice input, Brightness, Close window, Debug) without reaching the + * (Focus app switcher, Voice input, Brightness, Close current app, Close all apps, + * 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. @@ -109,7 +110,7 @@ export type ShellWindow = { title: string; /** Compositor surface this window renders to; configured at connect / launch. */ surfaceId: string; - /** Whether the system menu offers Close window (the launcher is pinned). */ + /** Whether the system menu can close this window (the launcher is pinned). */ closeable: boolean; /** * True when tap-then-hold currently opens the window's own context menu @@ -655,6 +656,12 @@ class Shell { if (window) this.closeWindow(window.windowId); } + /** Snapshot ids: close callbacks may remove themselves or other windows. */ + closeAllWindows(): void { + const ids = this.windows.filter(window => window.closeable).map(window => window.windowId); + for (const id of ids) this.closeWindow(id); + } + getWindows(): readonly ShellWindow[] { return this.windows; } @@ -1073,6 +1080,12 @@ class Shell { if (this.activeVoiceLayer || !this.stack.isAtBase()) { return { shell: true, window: false }; } + // The switcher owns its menu even when the selected app uses a hold + // for a game move or voice capture. + if (this.focus === "sidebar") { + this.openEscapeMenu(); + return { shell: true, window: false }; + } const window = this.foregroundWindow(); if (!window) { return { shell: true, window: false }; @@ -1096,6 +1109,10 @@ class Shell { // first), which answers with its own menu or asks for the system menu // when it has none. if (event.type === "short-then-long-press") { + if (this.focus === "sidebar" && !this.activeVoiceLayer && this.stack.isAtBase()) { + this.openEscapeMenu(); + return { shell: true, window: false }; + } const window = this.foregroundWindow(); if (window?.holdToTalk) { this.openEscapeMenu(); @@ -1913,7 +1930,7 @@ class Shell { /** * The system/escape menu: the entries every window shares (Focus app - * switcher, Voice input, Brightness, Close window) plus Debug. Shell-owned and + * switcher, Voice input, Brightness, Close current app, Close all apps) plus Debug. Shell-owned and * shell-drawn (never the app's), so an unresponsive app can always be * 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 @@ -1926,16 +1943,16 @@ class Shell { const items: MenuItem[] = []; if (foreground.closeable) { items.push({ - label: "Close window", + label: "Close current app", onSelect: (ctx) => { // Pop the menu first (its onRemoved returns focus to the sidebar), // then close the window the menu was opened over. ctx.stack.pop(); - this.closeForegroundWindow(); + this.closeWindow(foreground.windowId); }, }); } - // Close window sits first but the menu opens on Focus app switcher, so a + // Close current app sits first but the menu opens on Focus app switcher, so a // reflexive tap never closes the window. const initialSelection = items.length; items.push( @@ -1959,6 +1976,14 @@ class Shell { }, }]), ); + items.push({ + label: "Close all apps", + disabled: () => !this.windows.some(window => window.closeable), + onSelect: (ctx) => { + ctx.stack.pop(); + this.closeAllWindows(); + }, + }); if (brightnessSetting.get() !== "auto") { items.push({ label: "Brightness", diff --git a/app/ui/window-menu.ts b/app/ui/window-menu.ts index 07aab3a6..4dae4307 100644 --- a/app/ui/window-menu.ts +++ b/app/ui/window-menu.ts @@ -9,7 +9,7 @@ import type { WorkerAppReply } from "./shell/worker-window"; * The window context menu, opened by tap-then-hold: an app's own menu, * holding its app-specific actions. The shell's system menu (long-press) * 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 + * Close current app, Close all apps — so an app with nothing of its own to offer has no menu of * its own: tap-then-hold 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 (a window that diff --git a/docs/validation-app-switcher-menu.md b/docs/validation-app-switcher-menu.md new file mode 100644 index 00000000..4682c76e --- /dev/null +++ b/docs/validation-app-switcher-menu.md @@ -0,0 +1,35 @@ +# App switcher close menu + +Feature branch: `feat/app-switcher-close-menu`, based on `main` so it can be +reviewed independently. The combined build lives on `integration/device-preferences`. + +With the app switcher focused, long-press or tap-then-hold opens the existing +shell-owned System menu. “Close current app” closes the selected window when it +is closeable. It is absent for the pinned launcher (the large application grid). +“Close all apps” closes every closeable window, including background windows and +multiple windows belonging to one app. Pinned windows remain available. + +Both actions reuse the shell's normal close/cleanup path. The menu closes before +the windows do, and focus returns to the switcher. It opens on “Focus app switcher” +to avoid an accidental close. With no closeable windows, “Close all apps” is disabled. +No new preferences or persistence format are introduced. + +Software: `tests/app-switcher-menu.test.cjs` covers both gestures, apps claiming +holds/hold-to-talk, launcher exclusion, captured close targets, cancellation, +default selection, multiple windows, reentrant removal, callback errors and +screen-off behavior. Existing in-app gesture routing remains unchanged. + +Manual validation on glasses remains pending: + +1. Open several apps, focus the app switcher, and open the menu with both gestures. +2. Close the selected app; verify its surface disappears and other apps remain. +3. Select the large application grid; verify “Close current app” is absent. +4. Use “Close all apps”; verify the launcher remains usable and apps can reopen. +5. Repeat for left/right/bottom/popup switcher positions and with a running game, + a hold-to-talk app and an app exposing multiple windows. +6. Check cancel/back and the initial selection do not close anything. Check both + menu gestures still work normally when focus is inside an app. + +Closing windows retains each app's existing lifecycle semantics; it does not +uninstall apps or reset their preferences. Existing app-specific shutdown behavior +(such as a remote terminal connection) is the same as closing its window normally. diff --git a/tests/app-switcher-menu.test.cjs b/tests/app-switcher-menu.test.cjs new file mode 100644 index 00000000..ca9c89f7 --- /dev/null +++ b/tests/app-switcher-menu.test.cjs @@ -0,0 +1,148 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +function fixture(options = {}) { + const source = ts.createSourceFile('shell.ts', fs.readFileSync(path.join(__dirname, '../app/ui/shell/shell.ts'), 'utf8'), ts.ScriptTarget.Latest, true); + const shellClass = source.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'Shell'); + const names = new Set(['routeInput', 'openEscapeMenu', 'closeAllWindows', 'closeWindow', 'removeWindow', + 'foregroundWindow', 'noteWindowVisible', 'mostRecentWindowIndex', 'yieldFocusToSidebar']); + const methods = shellClass.members.filter(node => names.has(node.name?.getText(source))); + assert.equal(methods.length, names.size); + const overlay = source.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'ShellOverlayMenuLayer'); + class MenuLayer { + constructor(title, items) { this.title = title; this.items = items; } + selectItem(index) { this.selectedIndex = index; } + } + const context = { + exports: {}, MenuLayer, console: { warn: () => {} }, + appViewportRect: () => ({ x: 0, y: 0, width: 576 }), CONTEXT_MENU_DIM: 0.5, + brightnessSetting: { get: () => 'auto' }, gestureHints: () => 'App menu', + GESTURE_SHORT_THEN_LONG_PRESS: 'tap-hold', makeInputEvent: event => event, + isWatchInput: event => event.source === 'watch', isDirectionalInput: () => false, + }; + vm.runInNewContext(ts.transpileModule(`${overlay.getText(source)}\nexport class Shell { ${methods.map(node => node.getText(source)).join('\n')} }`, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + const s = new context.exports.Shell(); + const closed = [], delivered = [], warnings = [], renders = []; + context.console.warn = message => warnings.push(message); + const window = (id, closeable = true, extra = {}) => ({ + appId: id, windowId: id, closeable, + handleInput: async event => delivered.push([id, event.type]), + requestRender: () => renders.push(id), + close: () => closed.push(id), + ...extra, + }); + const launcher = window('launcher', false); + const app = window('current', true, options.app); + const background = window('background'); + const layers = []; + s.stack = { + isAtBase: () => !layers.length, + push: layer => layers.push(layer), + pop: () => layers.pop()?.onRemoved?.(), + popIfTop: predicate => { if (!layers.length || !predicate(layers.at(-1))) return false; s.stack.pop(); return true; }, + handleInput: async event => { if (event.type === 'double-click') s.stack.pop(); }, + }; + Object.assign(s, { + windows: [launcher, app, background], selectedIndex: 1, focus: 'sidebar', screenOn: true, + mruWindowIds: ['current', 'background', 'launcher'], attention: new Map(), + config: { voiceInputEnabled: false, requestShellRender() {}, onWindowsChanged() {} }, + cancelEscapeMenuTimer() {}, startEscapeMenuTimer() { s.escapeStarted = true; }, + lastInput: null, + }); + const input = type => s.routeInput({ type, source: 'ring', timestampMs: 100 }, 1); + const menu = () => layers.at(-1); + const choose = label => { + const item = menu().items.find(item => item.label === label); + assert.ok(item, label); + item.onSelect({ stack: s.stack }); + }; + return { s, input, menu, choose, closed, delivered, warnings, renders, launcher, app, background, window }; +} + +test('switcher opens its menu on hold or tap-hold even for an app claiming holds or hold-to-talk', async () => { + for (const type of ['long-press', 'short-then-long-press']) { + for (const app of [{}, { claimsLongPress: () => true }, { holdToTalk: true }]) { + const h = fixture({ app }); + await h.input(type); + assert.ok(h.menu()); + assert.ok(h.menu().items.some(item => item.label === 'Close current app')); + assert.ok(h.menu().items.some(item => item.label === 'Close all apps')); + assert.equal(h.menu().items[h.menu().selectedIndex].label, 'Focus app switcher'); + assert.deepEqual(h.delivered, [['current', 'system-menu-opened']]); + assert.equal(h.s.escapeStarted, undefined); + assert.equal(h.s.focus, 'sidebar'); + } + } +}); + +test('close current app closes the captured window and preserves the launcher and background windows', async () => { + const h = fixture(); + await h.input('long-press'); + h.s.selectedIndex = 2; // An asynchronous focus change must not redirect the close action. + h.choose('Close current app'); + assert.deepEqual(h.closed, ['current']); + assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher', 'background']); + assert.equal(h.s.focus, 'sidebar'); + assert.equal(h.menu(), undefined); +}); + +test('launcher offers close all but never close current; close all leaves only the launcher', async () => { + const h = fixture(); + h.s.selectedIndex = 0; + await h.input('short-then-long-press'); + assert.ok(!h.menu().items.some(item => item.label === 'Close current app')); + h.choose('Close all apps'); + assert.deepEqual(h.closed, ['current', 'background']); + assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher']); + assert.equal(h.s.selectedIndex, 0); + assert.equal(h.s.focus, 'sidebar'); + await h.input('long-press'); + assert.equal(h.menu().items.find(item => item.label === 'Close all apps').disabled(), true); +}); + +test('close all uses normal cleanup despite reentrant removal, multiple windows per app and throwing callbacks', () => { + const h = fixture(); + h.s.windows.push(h.window('another', true, { appId: 'current' }), h.window('pinned', false)); + h.app.close = () => { h.closed.push('current'); h.s.removeWindow('current'); }; + h.background.close = () => { h.closed.push('background'); throw new Error('failed cleanup'); }; + h.s.closeAllWindows(); + assert.deepEqual(h.closed, ['current', 'background', 'another']); + assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher', 'pinned']); + assert.equal(h.warnings.length, 1); + assert.equal(h.s.foregroundWindow().windowId, 'launcher'); +}); + +test('default selection and cancel do not close apps; an open overlay never receives a second menu', async () => { + const h = fixture(); + await h.input('long-press'); + h.choose(h.menu().items[h.menu().selectedIndex].label); + assert.deepEqual(h.closed, []); + await h.input('long-press'); + const menu = h.menu(); + await h.input('long-press'); + assert.equal(h.menu(), menu); + await h.input('double-click'); + assert.equal(h.menu(), undefined); + assert.deepEqual(h.closed, []); +}); + +test('in-app hold actions and app menus keep their existing routing; asleep switcher stays asleep', async () => { + for (const [app, type] of [[{ claimsLongPress: () => true }, 'long-press'], [{ holdToTalk: true }, 'long-press'], [{}, 'short-then-long-press']]) { + const h = fixture({ app }); + h.s.focus = 'window'; + await h.input(type); + assert.deepEqual(h.delivered, [['current', type]]); + assert.equal(h.menu(), undefined); + } + const h = fixture(); + h.s.screenOn = false; + await h.input('long-press'); + assert.equal(h.menu(), undefined); + assert.deepEqual(h.closed, []); +});