From 061b2eb2155b7bc7c6f0e766ecd32ef832ad52c3 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 30 Aug 2026 14:57:24 -0700 Subject: [PATCH] Update the launcher, Files app and builtin games to better handle watch swipe gestures --- CHANGELOG | 1 + app/apps/app-definition.ts | 3 + app/apps/blocks/blocks-app.worker.ts | 23 +++- app/apps/blocks/index.ts | 1 + app/apps/files/file-browser.ts | 129 +++++++++++++++++- app/apps/files/files-app.ts | 1 + app/apps/freecell/freecell-app.worker.ts | 26 +++- app/apps/freecell/index.ts | 1 + app/apps/launcher/launcher-app.ts | 49 +++++-- app/apps/minesweeper/index.ts | 1 + .../minesweeper/minesweeper-app.worker.ts | 32 ++++- app/apps/pinball/index.ts | 1 + app/apps/pinball/pinball-app.worker.ts | 32 ++++- app/ui/shell/in-process-window.ts | 3 + app/ui/shell/shell.ts | 48 +++++++ app/ui/shell/worker-window.ts | 7 + 16 files changed, 323 insertions(+), 35 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bc76655d..cbae3acf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ ## 0.6.1 - Future * Settings import/export scripts work on release builds * Fixed an issue where the onboarding flow failed to detect the existing glasses firmware version +* Update launcher, files, and game UIs to work better with watch input ## 0.6 - 2026-08-29 - Start keeping a changelog and making signed binaries via GitHub Releases diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts index eadf3f06..f5872232 100644 --- a/app/apps/app-definition.ts +++ b/app/apps/app-definition.ts @@ -106,6 +106,8 @@ export async function launchWorkerAppWindow( * windows are open). */ matchExistingBy?: "appId" | "windowId"; + /** Deliver watch swipes raw instead of the fallback (see WorkerWindowSpec). */ + acceptsDirectional?: boolean; }, ): Promise { const existing = shell @@ -125,6 +127,7 @@ export async function launchWorkerAppWindow( iconLetter: options.iconLetter, icon: options.icon, focus: true, + acceptsDirectional: options.acceptsDirectional, }); ctx.appendLog(`launched ${options.windowId}`); } diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index e5ef2faa..296e4d63 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -4,7 +4,8 @@ * foreground and the screen is on (losing either auto-pauses). * * Controls (in play): scroll moves the piece, click rotates, long-press hard - * drops, double-click pauses. Paused: click resumes, double-click yields + * drops, double-click pauses. Watch swipes are spatial: left/right move, + * up rotates, down hard-drops. Paused: click resumes, double-click yields * focus, long-press opens the window menu. */ import "@nativescript/core/globals"; @@ -18,6 +19,7 @@ import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import { + directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, @@ -303,10 +305,11 @@ function windowMenu(window: BlocksWindow): WindowMenu { } function handleInput(window: BlocksWindow, event: InputEvent, frameId: number): void { - // An open window menu owns all input (it closes itself via pop). + // An open window menu owns all input (it closes itself via pop); menus are + // list UIs, so watch swipes take their standard fallback meanings there. if (window.menu?.isOpen()) { window.menu - .handleInput(event) + .handleInput(directionalFallback(event)) .catch((error) => console.error(`blocks menu input failed: ${error}`)) .then(() => renderAndSubmit(window, frameId)); return; @@ -322,11 +325,21 @@ function handleInput(window: BlocksWindow, event: InputEvent, frameId: number): function handlePlayingInput(window: BlocksWindow, event: InputEvent, frameId: number): void { switch (event.type) { case "scroll-up": + case "swipe-left": tryMove(window, -1, 0); break; case "scroll-down": + case "swipe-right": tryMove(window, 1, 0); break; + // Watch swipes are spatial: left/right move the piece, up rotates it, + // down hard-drops (like long-press). + case "swipe-up": + tryRotate(window); + break; + case "swipe-down": + hardDrop(window); + break; case "click": tryRotate(window); break; @@ -345,9 +358,9 @@ function handlePlayingInput(window: BlocksWindow, event: InputEvent, frameId: nu renderAndSubmit(window, frameId); } -/** Input while paused or game over. */ +/** Input while paused or game over. Swipes take their standard fallback meanings. */ function handleIdleInput(window: BlocksWindow, event: InputEvent, frameId: number): void { - switch (event.type) { + switch (directionalFallback(event).type) { case "click": if (window.phase === "game-over") resetGame(window); window.phase = "playing"; diff --git a/app/apps/blocks/index.ts b/app/apps/blocks/index.ts index 0d89db7a..b7e0f279 100644 --- a/app/apps/blocks/index.ts +++ b/app/apps/blocks/index.ts @@ -11,6 +11,7 @@ const blocksApp: AppDefinition = { title: "Blocks", iconLetter: "B", icon: "l-piece", + acceptsDirectional: true, }), }; diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index 7d8f028e..819f53ef 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -15,8 +15,9 @@ import { statPath, type DirectoryEntry, } from "../../native/file-access"; -import { type InputEvent } from "../../ui/gestures"; +import { directionalFallback, isWatchInput, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; +import { shell } from "../../ui/shell/shell"; const LIST_X = 20; @@ -114,9 +115,17 @@ type IconMode = "row" | "item"; * click drops into the row, scroll picks the item); list view is a flat list. * Click descends into a directory or picks a supported file; double-click * backs out one level (item selection, then parent directory, then Places, - * then leaving the browser). Sized to its hosting stack. + * then leaving the browser). That is the ring's scheme; in icons view the + * watch (see handleIconsWatchInput) skips row mode and moves one cell at a + * time in four directions, exactly as in the launcher. Sized to its hosting + * stack. */ export class FileBrowserLayer implements Layer { + // Watch swipes are spatial in icons view: up/down move between rows, + // left/right between columns. From the leftmost column, left keeps going + // out (parent directory, Places, then the sidebar). List view maps swipes + // through directionalFallback itself. + readonly acceptsDirectional = true; /** Current directory, or null at the Places level. */ private location: string | null = null; /** The Places entry we descended through; going up from it returns to Places. */ @@ -203,11 +212,16 @@ export class FileBrowserLayer implements Layer { } if (selected) { - if (this.iconMode === "row") { - drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6); - } else { + // Same preview rule as the launcher: while defocused with the watch + // as the last-used source, show the cell a click would land on (watch + // focus enters item mode directly; see onFocus) instead of a row band + // the watch scheme never shows. + const cellHighlight = this.iconMode === "item" || (!focused && shell.lastInputWasWatch()); + if (cellHighlight) { this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1); drawSelectionHighlight(image, this.selectedCol * colW + 4, y + 1, colW - 8, rowH - 2, focused, 6); + } else { + drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6); } } @@ -234,12 +248,33 @@ export class FileBrowserLayer implements Layer { async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (filesViewModeSetting.get() === "list") { - await this.handleListInput(event, ctx); + // The flat list has no spatial meaning for swipes; give them the + // standard scroll / select / back meanings (the layer opted into + // directional delivery for the icons view, so the stack won't). + await this.handleListInput(directionalFallback(event), ctx); + } else if (isWatchInput(event)) { + await this.handleIconsWatchInput(event, ctx); } else { await this.handleIconsInput(event, ctx); } } + /** + * Focus arriving from the watch goes straight to item selection, as in the + * launcher: the watch has left/right swipes, so it never needs row mode. + * Any other source keeps the two-level scheme and enters in row mode. + */ + onFocus(lastInput: InputEvent | null): void { + if (filesViewModeSetting.get() !== "icons") return; + if (lastInput && isWatchInput(lastInput)) { + this.iconMode = "item"; + const iconRows = buildIconRows(this.flatRows()); + this.clampColToRow(iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]); + } else { + this.iconMode = "row"; + } + } + private async handleListInput(event: InputEvent, ctx: LayerContext): Promise { const rows = this.flatRows(); switch (event.type) { @@ -321,6 +356,88 @@ export class FileBrowserLayer implements Layer { } } + /** + * The watch's scheme for the icons view — the launcher's: there is no row + * mode, the selection is always one cell (a special row counts as its + * single full-width item). Up/down (and the crown) move between rows + * keeping the column, right/left move within the row, and left from the + * leftmost column keeps going out — parent directory, Places, then the + * sidebar. Row mode is restored on the way out so a ring user finds the + * grid as they left it. + */ + private async handleIconsWatchInput(event: InputEvent, ctx: LayerContext): Promise { + const iconRows = buildIconRows(this.flatRows()); + this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1)); + const row = iconRows[this.selectedRow]; + if (this.iconMode === "row") { + this.iconMode = "item"; + this.clampColToRow(row); + } + switch (event.type) { + case "swipe-up": + case "swipe-down": + case "scroll-up": + case "scroll-down": { + const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1; + this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1)); + // Keep the column; a shorter row (or a special one) clamps it. + this.clampColToRow(iconRows[this.selectedRow]); + return; + } + case "swipe-right": + if (row?.kind === "grid") { + this.selectedCol = clamp(this.selectedCol + 1, 0, row.items.length - 1); + } + return; + case "swipe-left": + if (row?.kind === "grid" && this.selectedCol > 0) { + this.selectedCol--; + } else { + this.watchNavigateUp(); + } + return; + case "click": { + if (!row) return; + if (row.kind === "special") { + await this.activateItem(row.item, ctx); + } else { + const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)]; + if (item) await this.activateItem(item, ctx); + } + // Descending into a directory resets the selection for the ring (row + // mode at the top); the watch scheme stays in single-item selection. + this.iconMode = "item"; + return; + } + case "double-click": + this.watchNavigateUp(); + return; + default: + return; + } + } + + private clampColToRow(row: IconRow | undefined): void { + if (row?.kind === "grid") { + this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1); + } + } + + /** + * navigateUp for the watch scheme: inside the browser the selection stays + * in item mode (resetSelection/selectPath put it back in row mode for the + * ring); leaving from Places restores row mode on the way out. + */ + private watchNavigateUp(): void { + if (this.location === null) { + this.iconMode = "row"; + this.options.onLeave(); + return; + } + this.navigateUp(); + this.iconMode = "item"; + } + /** * App-specific entries for the window long-press menu: the view switch, * plus bookmark/unbookmark for the selected entry (in icons view only once diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index c3c3ae23..01eaa065 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -64,6 +64,7 @@ export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow // itself is on top; a pushed viewer or dialog gets just the defaults. menuItems: () => (created && !created.stack.isAtBase() ? [] : browser.buildMenuItems()), actions: options.actions, + onFocus: (lastInput) => browser.onFocus(lastInput), baseLayer: browser, submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index aafd581f..d317adf6 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -9,6 +9,8 @@ * legal run that fits (supermoves via empty cells/columns). Double-click * sends the card at the cursor to its foundation, or cancels a pending * selection. Long-press opens the window menu (undo, new game, restart). + * Watch swipes move the cursor spatially: left/right within the row, up/down + * between the top row (cells + foundations) and the cascades. * Safe cards auto-play to the foundations after every move. */ import "@nativescript/core/globals"; @@ -22,7 +24,7 @@ import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu"; import type { MenuItem } from "../../ui/menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; -import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures"; +import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures"; declare const global: any; declare const com: any; @@ -256,10 +258,11 @@ function windowMenu(window: FreecellWindow): WindowMenu { } function handleInput(window: FreecellWindow, event: InputEvent, frameId: number): void { - // An open window menu owns all input (it closes itself via pop). + // An open window menu owns all input (it closes itself via pop); menus are + // list UIs, so watch swipes take their standard fallback meanings there. if (window.menu?.isOpen()) { window.menu - .handleInput(event) + .handleInput(directionalFallback(event)) .catch((error) => console.error(`freecell menu input failed: ${error}`)) .then(() => renderAndSubmit(window, frameId)); return; @@ -280,6 +283,21 @@ function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId: case "scroll-down": window.cursor = (window.cursor + 1) % LOC_COUNT; break; + // Watch swipes are spatial over the two rows of eight columns: up/down + // switch between the top row (free cells + foundations) and the cascades + // keeping the column, left/right move within the row. + case "swipe-up": + if (window.cursor >= LOC_CASCADE0) window.cursor -= LOC_CASCADE0; + break; + case "swipe-down": + if (window.cursor < LOC_CASCADE0) window.cursor += LOC_CASCADE0; + break; + case "swipe-left": + if (window.cursor % LOC_CASCADE0 > 0) window.cursor--; + break; + case "swipe-right": + if (window.cursor % LOC_CASCADE0 < LOC_CASCADE0 - 1) window.cursor++; + break; case "click": if (window.selected === null) { if (locationHasCard(window, window.cursor)) { @@ -312,7 +330,7 @@ function handlePlayingInput(window: FreecellWindow, event: InputEvent, frameId: } function handleWonInput(window: FreecellWindow, event: InputEvent, frameId: number): void { - switch (event.type) { + switch (directionalFallback(event).type) { case "click": newGame(window, false); playSfx(window, SFX_NEW_GAME); diff --git a/app/apps/freecell/index.ts b/app/apps/freecell/index.ts index 7a461f1c..3b639039 100644 --- a/app/apps/freecell/index.ts +++ b/app/apps/freecell/index.ts @@ -11,6 +11,7 @@ const freecellApp: AppDefinition = { title: "Freecell", iconLetter: "F", icon: "spade", + acceptsDirectional: true, }), }; diff --git a/app/apps/launcher/launcher-app.ts b/app/apps/launcher/launcher-app.ts index 056f08bb..099a1b1a 100644 --- a/app/apps/launcher/launcher-app.ts +++ b/app/apps/launcher/launcher-app.ts @@ -74,9 +74,9 @@ type LauncherGridEntry = */ class LauncherGridLayer implements Layer { // Watch swipes are spatial: up/down move between rows, left/right between - // columns. From row mode, right goes into the row (left backs out, like - // double-click); from the first column, left returns to row mode, which - // keeps "left" pointing toward the sidebar all the way out. + // columns. From the leftmost column, left keeps going out: it leaves the + // open folder if there is one, else yields to the sidebar — "left" points + // toward the sidebar all the way out. readonly acceptsDirectional = true; private mode: LauncherMode = "row"; private selectedRow = 0; @@ -87,6 +87,21 @@ class LauncherGridLayer implements Layer { constructor(private readonly options: LauncherOptions) {} + /** + * Focus arriving from the watch goes straight to item selection: the watch + * has left/right swipes, so it never needs row mode (see handleWatchInput), + * and starting there would paint a row band its scheme can't produce. Any + * other source keeps the two-level scheme and enters in row mode. + */ + onFocus(lastInput: InputEvent | null): void { + if (lastInput && isWatchInput(lastInput)) { + this.mode = "item"; + this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(this.entries().length, this.selectedRow) - 1); + } else { + this.mode = "row"; + } + } + /** * The cells to show, computed fresh from the folder state. Self-heals * currentFolder: if the open folder no longer exists (the assistant @@ -150,6 +165,7 @@ class LauncherGridLayer implements Layer { this.mode = "row"; const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder); this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0; + this.selectedCol = index >= 0 ? index % COLS : 0; } /** @@ -270,12 +286,17 @@ class LauncherGridLayer implements Layer { const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH; - // Selection highlight (row band, or a single cell in item mode). + // Selection highlight: a row band, or a single cell in item mode. While + // defocused with the watch as the last-used source, the cell outline is + // shown even in row mode: watch focus enters item mode directly (see + // onFocus), so the outline previews the cell a click would land on + // rather than a row band the watch scheme never shows. + const cellHighlight = this.mode === "item" || (!focused && shell.lastInputWasWatch()); const selY = rowY(this.selectedRow); - if (this.mode === "row") { - drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6); - } else { + if (cellHighlight) { drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6); + } else { + drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6); } for (let index = 0; index < entries.length; index++) { @@ -415,7 +436,7 @@ class LauncherGridLayer implements Layer { } return; case "click": - await this.openSelected(entries); + await this.openSelected(entries, true); return; case "double-click": if (this.currentFolder !== null) { @@ -455,14 +476,19 @@ class LauncherGridLayer implements Layer { return true; } - /** Open the selected cell: enter a folder, or launch the app. */ - private async openSelected(entries: readonly LauncherGridEntry[]): Promise { + /** + * Open the selected cell: enter a folder, or launch the app. A folder + * entered by watch input stays in single-item selection (the watch scheme + * has no row mode), starting at the folder's first cell. + */ + private async openSelected(entries: readonly LauncherGridEntry[], watch = false): Promise { const entry = entries[this.selectedRow * COLS + this.selectedCol]; if (!entry) return; if (entry.kind === "folder") { this.currentFolder = entry.name; - this.mode = "row"; + this.mode = watch ? "item" : "row"; this.selectedRow = 0; + this.selectedCol = watch ? 0 : this.selectedCol; this.scrollRow = 0; } else { // Return to the top grid before launching, so the launcher never @@ -490,6 +516,7 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow { closeable: false, menuItems: () => gridLayer.menuItems(), actions: options.actions, + onFocus: (lastInput) => gridLayer.onFocus(lastInput), // Not wrapped in YieldAtRootLayer: the grid handles double-click itself to // back out of item selection before yielding to the sidebar. baseLayer: gridLayer, diff --git a/app/apps/minesweeper/index.ts b/app/apps/minesweeper/index.ts index 7e88c547..d0772dea 100644 --- a/app/apps/minesweeper/index.ts +++ b/app/apps/minesweeper/index.ts @@ -11,6 +11,7 @@ const minesweeperApp: AppDefinition = { title: "Minesweeper", iconLetter: "M", icon: "bomb", + acceptsDirectional: true, }), }; diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index a1770fdc..20a78218 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -8,6 +8,8 @@ * row, click switches to column-select, double-click pauses. Column-select: * scroll moves along the row, click reveals (or chords a satisfied number), * long-press toggles a flag, double-click returns to row-select. + * Watch swipes skip the two-layer scheme and move the cell cursor in four + * directions; a watch double-click pauses directly. * Paused/won/lost: click resumes or starts a new game, double-click yields * focus, long-press opens the window menu. */ @@ -22,10 +24,12 @@ import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import { + directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, GESTURE_SCROLL, + isWatchInput, type InputEvent, } from "../../ui/gestures"; import { clamp } from "../../util/numeric-util"; @@ -283,10 +287,11 @@ function windowMenu(window: MinesweeperWindow): WindowMenu { } function handleInput(window: MinesweeperWindow, event: InputEvent, frameId: number): void { - // An open window menu owns all input (it closes itself via pop). + // An open window menu owns all input (it closes itself via pop); menus are + // list UIs, so watch swipes take their standard fallback meanings there. if (window.menu?.isOpen()) { window.menu - .handleInput(event) + .handleInput(directionalFallback(event)) .catch((error) => console.error(`minesweeper menu input failed: ${error}`)) .then(() => renderAndSubmit(window, frameId)); return; @@ -300,8 +305,25 @@ function handleInput(window: MinesweeperWindow, event: InputEvent, frameId: numb } function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameId: number): void { + // The watch's swipes move the cell cursor in four directions; its scheme + // has no row-select layer (as in the launcher), so any watch input drops + // to cell selection first and its double-click pauses directly. + const watch = isWatchInput(event); + if (watch) window.selectMode = "column"; const rowMode = window.selectMode === "row"; switch (event.type) { + case "swipe-up": + window.cursorY = clamp(window.cursorY - 1, 0, ROWS - 1); + break; + case "swipe-down": + window.cursorY = clamp(window.cursorY + 1, 0, ROWS - 1); + break; + case "swipe-left": + window.cursorX = clamp(window.cursorX - 1, 0, COLS - 1); + break; + case "swipe-right": + window.cursorX = clamp(window.cursorX + 1, 0, COLS - 1); + break; case "scroll-up": if (rowMode) { window.cursorY = clamp(window.cursorY - 1, 0, ROWS - 1); @@ -331,7 +353,7 @@ function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameI toggleFlag(window); break; case "double-click": - if (rowMode) { + if (rowMode || watch) { window.phase = "paused"; syncClock(window); playSfx(window, SFX_PAUSE); @@ -346,9 +368,9 @@ function handlePlayingInput(window: MinesweeperWindow, event: InputEvent, frameI renderAndSubmit(window, frameId); } -/** Input while paused, won, or lost. */ +/** Input while paused, won, or lost. Swipes take their standard fallback meanings. */ function handleIdleInput(window: MinesweeperWindow, event: InputEvent, frameId: number): void { - switch (event.type) { + switch (directionalFallback(event).type) { case "click": if (window.phase === "paused") { window.phase = "playing"; diff --git a/app/apps/pinball/index.ts b/app/apps/pinball/index.ts index 2f7856de..bc2bd44b 100644 --- a/app/apps/pinball/index.ts +++ b/app/apps/pinball/index.ts @@ -11,6 +11,7 @@ const pinballApp: AppDefinition = { title: "Pinball", iconLetter: "P", icon: "pinball", + acceptsDirectional: true, }), }; diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 31722613..5b3b7f8c 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -14,6 +14,8 @@ * Controls (ball ready): scroll sets launch power, click launches. In play: * click flips both flippers, scroll-up/down flips left/right individually, * long-press nudges the table (three quick nudges tilt), double-click pauses. + * Watch swipes: left/right work the matching flipper, up nudges (or raises + * launch power at the plunger), down lowers launch power. * Paused/game over: click resumes or starts a new game, double-click yields * focus, long-press opens the window menu. */ @@ -29,6 +31,7 @@ import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import { + directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, @@ -468,10 +471,11 @@ function windowMenu(window: PinballWindow): WindowMenu { } function handleInput(window: PinballWindow, event: InputEvent, frameId: number): void { - // An open window menu owns all input (it closes itself via pop). + // An open window menu owns all input (it closes itself via pop); menus are + // list UIs, so watch swipes take their standard fallback meanings there. if (window.menu?.isOpen()) { window.menu - .handleInput(event) + .handleInput(directionalFallback(event)) .catch((error) => console.error(`pinball menu input failed: ${error}`)) .then(() => renderAndSubmit(window, frameId)); return; @@ -501,6 +505,26 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n flip(window, window.flippers[1]!); } break; + // Watch swipes: left/right work the matching flipper; up nudges the + // table (or raises launch power at the plunger), down lowers it. + case "swipe-left": + if (!ready) flip(window, window.flippers[0]!); + break; + case "swipe-right": + if (!ready) flip(window, window.flippers[1]!); + break; + case "swipe-up": + if (ready) { + window.launchPower = clamp(window.launchPower + 1, 1, LAUNCH_SPEEDS.length); + } else { + nudge(window); + } + break; + case "swipe-down": + if (ready) { + window.launchPower = clamp(window.launchPower - 1, 1, LAUNCH_SPEEDS.length); + } + break; case "click": if (ready) { launchBall(window); @@ -529,9 +553,9 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n renderAndSubmit(window, frameId); } -/** Input while paused or game over. */ +/** Input while paused or game over. Swipes take their standard fallback meanings. */ function handleIdleInput(window: PinballWindow, event: InputEvent, frameId: number): void { - switch (event.type) { + switch (directionalFallback(event).type) { case "click": if (window.phase === "game-over") resetGame(window); window.phase = "playing"; diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index a12908ca..f6353f7e 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -44,6 +44,8 @@ export type InProcessWindowOptions = { * offer "Type Into App" for this window. */ receiveTextInput?: (text: string) => void; + /** Input focus moved into this window (see ShellWindow.onFocus). */ + onFocus?: ShellWindow["onFocus"]; baseLayer: Layer; submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; setSurfaceVisible: (visible: boolean) => void; @@ -210,6 +212,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces return handled; }, receiveTextInput: options.receiveTextInput, + onFocus: options.onFocus, setForeground: (foreground) => { options.setSurfaceVisible(foreground); }, diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index 08fbbcc9..33dc052a 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -9,6 +9,7 @@ import { type InputEventPayload, type InputSource, isDirectionalInput, + isWatchInput, makeInputEvent, } from "../gestures"; import { Layer, LayerActions, LayerContext, LayerStack, noopLayerActions } from "../layers"; @@ -111,6 +112,14 @@ export type ShellWindow = { receiveTextInput?: (text: string) => void; /** Foreground state changed: this window's surface is (not) the visible one. */ setForeground?: (foreground: boolean) => void; + /** + * Input focus moved into this window. `lastInput` is the most recent input + * event the shell received — for a focus that a click or swipe caused, the + * event that caused it. A programmatic focus (a worker's focus-window + * request, a wake path) can deliver an older event: check timestampMs + * before treating it as current. + */ + onFocus?: (lastInput: InputEvent | null) => void; /** Screen turned on/off; hidden or screen-off windows should stop painting. */ setScreenOn?: (on: boolean) => void; }; @@ -238,6 +247,8 @@ class Shell { private focus: FocusKind = "sidebar"; private screenOn = true; private lastInputAtMs = Date.now(); + /** The most recent input event received, for windows gaining focus (see ShellWindow.onFocus). */ + private lastInput: InputEvent | null = null; private battery: ShellChromeState["battery"] = { headset: null, headsetCharging: null }; private attention = new Map(); // App-provided top-bar tray icons, keyed by owner id; drawn between the @@ -447,10 +458,18 @@ class Shell { return index === null ? null : this.windows[index] ?? null; } + /** Whether input focus currently targets this window (regardless of screen state). */ + private isFocusTarget(window: ShellWindow | undefined): boolean { + return !!window && this.focus === "window" && this.foregroundWindow() === window; + } + /** Turn the screen on (if off) and set focus. Returns whether it was off. */ wake(focus: FocusKind, nowMs = Date.now()): boolean { this.lastInputAtMs = nowMs; + const gaining = focus === "window" ? this.foregroundWindow() : undefined; + const alreadyFocused = this.isFocusTarget(gaining); this.focus = focus; + if (gaining && !alreadyFocused) gaining.onFocus?.(this.lastInput); if (this.screenOn) return false; this.screenOn = true; this.config.onScreenStateChanged(true); @@ -479,8 +498,11 @@ class Shell { focusWindow(windowId: string): void { const index = this.windows.findIndex((w) => w.windowId === windowId); if (index < 0) return; + const target = this.windows[index]; + const alreadyFocused = this.isFocusTarget(target); this.setSelectedIndex(index); this.focus = "window"; + if (!alreadyFocused) target?.onFocus?.(this.lastInput); } /** Idle timeout: sleep if the configured timeout elapsed. Returns whether it slept. */ @@ -547,6 +569,14 @@ class Shell { } async receiveInput(event: InputEvent, frameId = 0): Promise { + const previous = this.lastInput; + this.lastInput = event; + // A visible window may paint a source-dependent indicator (see + // lastInputEvent); when the source class flips (watch <-> ring/arms), + // repaint it so the indicator follows the device now in use. + if (this.screenOn && previous && isWatchInput(previous) !== isWatchInput(event)) { + this.foregroundWindow()?.requestRender(); + } // The stock lifecycle has already interpreted the physical double tap as // "wake". Keep that directionality if delivery is delayed or duplicated. if (event.type === "display-wake") { @@ -610,6 +640,7 @@ class Shell { } 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); @@ -682,6 +713,22 @@ class Shell { return `fg=${foreground} target=${this.focus}`; } + /** + * The most recent input event received, from any source. Windows whose + * appearance depends on the input device in use (e.g. the launcher's + * defocused selection preview) can read it at paint time; the shell + * repaints the foreground window whenever the source class changes, so + * such paints stay current even while the window is not focused. + */ + lastInputEvent(): InputEvent | null { + return this.lastInput; + } + + /** Whether the most recent input came from the watch (see lastInputEvent). */ + lastInputWasWatch(): boolean { + return this.lastInput !== null && isWatchInput(this.lastInput); + } + /** Whether a window is the current input target (foreground + focus in-window). */ isWindowFocused(windowId: string): boolean { return this.screenOn && this.focus === "window" && this.foregroundWindow()?.windowId === windowId; @@ -718,6 +765,7 @@ class Shell { // sidebar's right). Left has nowhere further to go and is ignored. if (this.windows.length) { this.focus = "window"; + this.foregroundWindow()?.onFocus?.(this.lastInput); // Repaint the window now so its selection highlight reflects focus // this frame, not one frame late. this.foregroundWindow()?.requestRender(); diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index d5e0dcef..26d7c146 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -117,6 +117,12 @@ export type WorkerWindowSpec = { focus?: boolean; /** Window height: the standard 288px band ("min", default) or full screen ("max"). */ heightMode?: WindowHeightMode; + /** + * Deliver watch swipes to the worker as swipe-* events instead of the + * scroll / click / double-click fallback. The worker then owns the meaning + * of all four directions in every window state (see ShellWindow). + */ + acceptsDirectional?: boolean; }; export type WorkerAppHostOptions = { @@ -283,6 +289,7 @@ export class WorkerAppHost { title: spec.title, surfaceId, closeable: true, + acceptsDirectional: spec.acceptsDirectional, heightMode, close: () => { this.openWindows.delete(spec.windowId);