diff --git a/app/apps/all-apps.ts b/app/apps/all-apps.ts new file mode 100644 index 00000000..c7c4508e --- /dev/null +++ b/app/apps/all-apps.ts @@ -0,0 +1,43 @@ +import { type AppDefinition } from "./app-definition"; +import launcherApp from "./launcher"; +import timerApp from "./timer"; +import terminalApp from "./terminal"; +import filesApp from "./files"; +import musicApp from "./music"; +import nightscoutApp from "./nightscout"; +import transcribeApp from "./transcribe"; +import notificationsApp from "./notifications"; +import calendarApp from "./calendar"; +import weatherApp from "./weather"; +import navigateApp from "./navigate"; +import blocksApp from "./blocks"; +import minesweeperApp from "./minesweeper"; +import freecellApp from "./freecell"; +import pinballApp from "./pinball"; +import debugTestsApp from "./debug-tests"; +import settingsApp from "./settings"; + +/** + * Every app, in launcher-grid order (the launcher itself is first but hidden + * from the grid). The sole registry: the controller, launcher, and assistant + * tools all discover apps here. + */ +export const ALL_APPS: readonly AppDefinition[] = [ + launcherApp, + timerApp, + terminalApp, + filesApp, + musicApp, + nightscoutApp, + transcribeApp, + notificationsApp, + calendarApp, + weatherApp, + navigateApp, + blocksApp, + minesweeperApp, + freecellApp, + pinballApp, + debugTestsApp, + settingsApp, +]; diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts new file mode 100644 index 00000000..da094ecb --- /dev/null +++ b/app/apps/app-definition.ts @@ -0,0 +1,125 @@ +import { GrayImage } from "../graphics/image"; +import { type IconName } from "../graphics/icons"; +import { type LayerActions } from "../ui/layers"; +import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window"; +import { WorkerAppHost } from "../ui/shell/worker-window"; +import { shell } from "../ui/shell/shell"; + +/** + * An app, as seen from outside while it isn't running. Each app directory's + * index.ts default-exports one of these, and all-apps.ts collects them; the + * controller and shell reach apps only through this surface. Meant to grow + * into the sole export of an app package (the boundary for sandboxing and + * dynamic loading). + */ +export type AppDefinition = { + appId: string; + /** Launcher-grid and window-title label. */ + title: string; + /** Launcher-grid icon. */ + icon: IconName; + /** Open the app's window (or focus it if already open). */ + launch: (ctx: AppContext, params?: AppLaunchParams) => Promise; + /** + * Runs once at shell startup, before any launch. For apps that must exist + * from boot (the launcher registers its pinned window here). + */ + boot?: (ctx: AppContext) => void; + /** False for apps that must not appear in the launcher grid (the launcher itself). */ + showInLauncher?: boolean; + /** Present on the app that handles text shared via the Android share intent. */ + openSharedText?: (ctx: AppContext, title: string, text: string) => void; +}; + +export type AppLaunchParams = { + /** Settings-app section deep-link (window menu -> Settings). */ + section?: string; +}; + +/** + * Shell/controller services available to an app's launch and boot callbacks, + * bound to that app's id. This is deliberately the only channel from app + * definitions back into the controller. + */ +export type AppContext = { + /** The id of the app this context was built for. */ + appId: string; + /** Every app, in launcher-grid order. */ + apps: readonly AppDefinition[]; + /** Shared layer actions (voice capture, text-setting edits, buzzer, ...). */ + actions: Omit; + /** Launch (or focus) any app by id through the shell. */ + launchApp: (appId: string, params?: AppLaunchParams) => Promise; + /** Launch-or-focus a main-thread singleton window keyed by windowId. */ + launchInProcessApp: ( + windowId: string, + surfaceId: string, + create: (options: InProcessAppOptions) => InProcessWindow, + ) => Promise; + /** Get this app's shared worker host, spawning the worker on first use. */ + ensureWorkerHost: (createWorker: () => Worker) => WorkerAppHost; + /** Submit a painted frame for a window surface (windows created outside launchInProcessApp). */ + submitWindowFrame: (surfaceId: string, image: GrayImage, paintMs: number, frameId: number) => Promise; + setWindowSurfaceVisible: (surfaceId: string, visible: boolean) => void; + requestShellRender: () => void; + appendLog: (message: string) => void; + /** + * Register (or clear, with null) the window hosting the on-glasses + * text-setting editor, so the controller can sync it with the phone-side + * editor field. + */ + setTextEditorHost: (host: TextEditorHost | null) => void; +}; + +/** See AppContext.setTextEditorHost. */ +export type TextEditorHost = { + /** 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. */ + closeTextEditor: () => boolean; + requestRender: () => void; +}; + +/** + * Launch-or-focus flow shared by the worker-hosted apps: focus the app's + * existing window if one is open, otherwise open a window in the app's shared + * worker (spawned on first launch). + */ +export async function launchWorkerAppWindow( + ctx: AppContext, + options: { + /** Must construct the worker with a string-literal path (webpack worker loader). */ + createWorker: () => Worker; + windowId: string; + title: string; + iconLetter: string; + icon: IconName; + /** + * Which existing window counts as "already launched": any window of this + * app (default), or only options.windowId exactly (multi-window apps like + * the terminal, where launching should reopen the hub even while other + * windows are open). + */ + matchExistingBy?: "appId" | "windowId"; + }, +): Promise { + const existing = shell + .getWindows() + .find((window) => + options.matchExistingBy === "windowId" ? window.windowId === options.windowId : window.appId === ctx.appId, + ); + if (existing) { + shell.focusWindow(existing.windowId); + ctx.requestShellRender(); + return; + } + const host = ctx.ensureWorkerHost(options.createWorker); + host.openWindow({ + windowId: options.windowId, + title: options.title, + iconLetter: options.iconLetter, + icon: options.icon, + focus: true, + }); + ctx.appendLog(`launched ${options.windowId}`); +} diff --git a/app/apps/blocks/index.ts b/app/apps/blocks/index.ts new file mode 100644 index 00000000..0d89db7a --- /dev/null +++ b/app/apps/blocks/index.ts @@ -0,0 +1,17 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const blocksApp: AppDefinition = { + appId: "blocks", + title: "Blocks", + icon: "l-piece", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./blocks-app.worker"), + windowId: "blocks:main", + title: "Blocks", + iconLetter: "B", + icon: "l-piece", + }), +}; + +export default blocksApp; diff --git a/app/apps/calendar/index.ts b/app/apps/calendar/index.ts new file mode 100644 index 00000000..07b98e36 --- /dev/null +++ b/app/apps/calendar/index.ts @@ -0,0 +1,11 @@ +import { type AppDefinition } from "../app-definition"; +import { CALENDAR_SURFACE_ID, CALENDAR_WINDOW_ID, createCalendarAppWindow } from "./calendar-app"; + +const calendarApp: AppDefinition = { + appId: "calendar", + title: "Calendar", + icon: "calendar", + launch: (ctx) => ctx.launchInProcessApp(CALENDAR_WINDOW_ID, CALENDAR_SURFACE_ID, createCalendarAppWindow), +}; + +export default calendarApp; diff --git a/app/apps/debug-tests/index.ts b/app/apps/debug-tests/index.ts new file mode 100644 index 00000000..88d66e7b --- /dev/null +++ b/app/apps/debug-tests/index.ts @@ -0,0 +1,11 @@ +import { type AppDefinition } from "../app-definition"; +import { createDebugTestsAppWindow, DEBUG_TESTS_SURFACE_ID, DEBUG_TESTS_WINDOW_ID } from "./debug-tests-app"; + +const debugTestsApp: AppDefinition = { + appId: "debug-tests", + title: "Debug tests", + icon: "flask-conical", + launch: (ctx) => ctx.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow), +}; + +export default debugTestsApp; diff --git a/app/apps/files/index.ts b/app/apps/files/index.ts new file mode 100644 index 00000000..30227243 --- /dev/null +++ b/app/apps/files/index.ts @@ -0,0 +1,52 @@ +import { type AppContext, type AppDefinition } from "../app-definition"; +import { + createFilesAppWindow, + createImageDocumentWindow, + createTextDocumentWindow, + FILES_SURFACE_ID, + FILES_WINDOW_ID, +} from "./files-app"; + +// Document windows are closeable and non-singleton; the serial keeps their +// windowIds unique. +let nextDocumentSerial = 1; + +function openTextDocumentWindow(ctx: AppContext, title: string, text: string): void { + const windowId = `files:doc:${nextDocumentSerial++}`; + void ctx + .launchInProcessApp(windowId, `window:${windowId}`, (options) => + createTextDocumentWindow(windowId, title, text, options), + ) + .catch((error) => { + ctx.appendLog(`text document window failed: ${error}`); + }); +} + +function openImageDocumentWindow(ctx: AppContext, title: string, path: string): void { + const windowId = `files:doc:${nextDocumentSerial++}`; + void ctx + .launchInProcessApp(windowId, `window:${windowId}`, (options) => + createImageDocumentWindow(windowId, title, path, options), + ) + .catch((error) => { + ctx.appendLog(`image window failed: ${error}`); + }); +} + +const filesApp: AppDefinition = { + appId: "files", + title: "Files", + icon: "folder", + launch: (ctx) => + ctx.launchInProcessApp(FILES_WINDOW_ID, FILES_SURFACE_ID, (options) => + createFilesAppWindow({ + ...options, + openDocumentWindow: (title, text) => openTextDocumentWindow(ctx, title, text), + openImageWindow: (title, path) => openImageDocumentWindow(ctx, title, path), + }), + ), + // A document arriving via Android's Share intent opens as its own window. + openSharedText: (ctx, title, text) => openTextDocumentWindow(ctx, title, text), +}; + +export default filesApp; diff --git a/app/apps/freecell/index.ts b/app/apps/freecell/index.ts new file mode 100644 index 00000000..7a461f1c --- /dev/null +++ b/app/apps/freecell/index.ts @@ -0,0 +1,17 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const freecellApp: AppDefinition = { + appId: "freecell", + title: "Freecell", + icon: "spade", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./freecell-app.worker"), + windowId: "freecell:main", + title: "Freecell", + iconLetter: "F", + icon: "spade", + }), +}; + +export default freecellApp; diff --git a/app/apps/launcher/index.ts b/app/apps/launcher/index.ts new file mode 100644 index 00000000..3b102109 --- /dev/null +++ b/app/apps/launcher/index.ts @@ -0,0 +1,36 @@ +import { shell } from "../../ui/shell/shell"; +import { type AppDefinition } from "../app-definition"; +import { createLauncherWindow, LAUNCHER_SURFACE_ID, LAUNCHER_WINDOW_ID } from "./launcher-app"; + +const launcherApp: AppDefinition = { + appId: "launcher", + title: "Apps", + icon: "layout-grid", + // The launcher is the app grid itself, not an entry in it. + showInLauncher: false, + // Pinned first in the sidebar and the boot foreground; registered at + // startup rather than launched. + boot: (ctx) => { + shell.registerWindow( + createLauncherWindow({ + actions: { + ...ctx.actions, + requestRender: () => shell.foregroundWindow()?.requestRender(), + }, + apps: ctx.apps + .filter((app) => app.showInLauncher !== false) + .map((app) => ({ appId: app.appId, label: app.title, icon: app.icon })), + launchApp: (appId) => ctx.launchApp(appId), + submitFrame: (image, paintMs, frameId) => ctx.submitWindowFrame(LAUNCHER_SURFACE_ID, image, paintMs, frameId), + setSurfaceVisible: (visible) => ctx.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible), + }), + ); + }, + // The launcher window is pinned and always open; launching means focusing. + launch: async (ctx) => { + shell.focusWindow(LAUNCHER_WINDOW_ID); + ctx.requestShellRender(); + }, +}; + +export default launcherApp; diff --git a/app/apps/minesweeper/index.ts b/app/apps/minesweeper/index.ts new file mode 100644 index 00000000..7e88c547 --- /dev/null +++ b/app/apps/minesweeper/index.ts @@ -0,0 +1,17 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const minesweeperApp: AppDefinition = { + appId: "minesweeper", + title: "Minesweeper", + icon: "bomb", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./minesweeper-app.worker"), + windowId: "minesweeper:main", + title: "Minesweeper", + iconLetter: "M", + icon: "bomb", + }), +}; + +export default minesweeperApp; diff --git a/app/apps/music/index.ts b/app/apps/music/index.ts new file mode 100644 index 00000000..c6e13742 --- /dev/null +++ b/app/apps/music/index.ts @@ -0,0 +1,11 @@ +import { type AppDefinition } from "../app-definition"; +import { createMusicAppWindow, MUSIC_SURFACE_ID, MUSIC_WINDOW_ID } from "./music-app"; + +const musicApp: AppDefinition = { + appId: "music", + title: "Music", + icon: "music", + launch: (ctx) => ctx.launchInProcessApp(MUSIC_WINDOW_ID, MUSIC_SURFACE_ID, createMusicAppWindow), +}; + +export default musicApp; diff --git a/app/apps/navigate/index.ts b/app/apps/navigate/index.ts new file mode 100644 index 00000000..a6f2c0cf --- /dev/null +++ b/app/apps/navigate/index.ts @@ -0,0 +1,22 @@ +import { ensureFineLocationPermission } from "../../g2/android-permissions"; +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const navigateApp: AppDefinition = { + appId: "navigate", + title: "Navigate", + icon: "map", + launch: (ctx) => { + // Navigation needs precise location; prompt on the phone while the + // window opens (the worker can't show permission dialogs). + void ensureFineLocationPermission().catch(() => {}); + return launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./navigate-app.worker"), + windowId: "navigate:main", + title: "Navigate", + iconLetter: "N", + icon: "map", + }); + }, +}; + +export default navigateApp; diff --git a/app/apps/nightscout/index.ts b/app/apps/nightscout/index.ts new file mode 100644 index 00000000..078af81c --- /dev/null +++ b/app/apps/nightscout/index.ts @@ -0,0 +1,11 @@ +import { type AppDefinition } from "../app-definition"; +import { createNightscoutAppWindow, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app"; + +const nightscoutApp: AppDefinition = { + appId: "nightscout", + title: "Nightscout", + icon: "nightscout", + launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow), +}; + +export default nightscoutApp; diff --git a/app/apps/notifications/index.ts b/app/apps/notifications/index.ts new file mode 100644 index 00000000..a91538a5 --- /dev/null +++ b/app/apps/notifications/index.ts @@ -0,0 +1,20 @@ +import { isNotificationListenerEnabled, requestNotificationListenerAccess } from "../../native/notification-access"; +import { type AppDefinition } from "../app-definition"; +import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from "./notifications-app"; + +const notificationsApp: AppDefinition = { + appId: "notifications", + title: "Notifications", + icon: "bell", + launch: (ctx) => { + // Without notification-listener access the tray reads as empty; prompt + // the user on the phone so the on-glasses "grant permission" message is + // actionable. The app still opens to show that message. + if (!isNotificationListenerEnabled()) { + requestNotificationListenerAccess(); + } + return ctx.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow); + }, +}; + +export default notificationsApp; diff --git a/app/apps/notifications/notifications-app.ts b/app/apps/notifications/notifications-app.ts index 39399c73..168de962 100644 --- a/app/apps/notifications/notifications-app.ts +++ b/app/apps/notifications/notifications-app.ts @@ -1,4 +1,5 @@ import { NotificationsListLayer } from "../../ui/notifications"; +import { onAndroidNotificationPosted } from "../../native/notification-icons"; import { createInProcessWindow, YieldAtRootLayer, @@ -15,7 +16,9 @@ export const NOTIFICATIONS_SURFACE_ID = "window:notifications"; * opens the detail view with its quick actions. */ export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow { - return createInProcessWindow({ + // Newly posted notifications repaint the list while the window is open. + let offNotificationPosted: (() => void) | null = null; + const created = createInProcessWindow({ appId: "notifications", windowId: NOTIFICATIONS_WINDOW_ID, title: "Notifications", @@ -27,6 +30,11 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, - onClosed: options.onClosed, + onClosed: () => { + offNotificationPosted?.(); + options.onClosed(); + }, }); + offNotificationPosted = onAndroidNotificationPosted(() => created.requestRender()); + return created; } diff --git a/app/apps/pinball/index.ts b/app/apps/pinball/index.ts new file mode 100644 index 00000000..2f7856de --- /dev/null +++ b/app/apps/pinball/index.ts @@ -0,0 +1,17 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const pinballApp: AppDefinition = { + appId: "pinball", + title: "Pinball", + icon: "pinball", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./pinball-app.worker"), + windowId: "pinball:main", + title: "Pinball", + iconLetter: "P", + icon: "pinball", + }), +}; + +export default pinballApp; diff --git a/app/apps/settings/index.ts b/app/apps/settings/index.ts new file mode 100644 index 00000000..39a5bed9 --- /dev/null +++ b/app/apps/settings/index.ts @@ -0,0 +1,45 @@ +import { shell } from "../../ui/shell/shell"; +import { type AppDefinition } from "../app-definition"; +import { + createSettingsAppWindow, + SETTINGS_SURFACE_ID, + SETTINGS_WINDOW_ID, + type SettingsAppWindow, +} from "./settings-app"; + +// The Settings app is a singleton; tracked so a relaunch with a section +// deep-link can focus that section in the open window. +let activeSettingsApp: SettingsAppWindow | null = null; + +const settingsApp: AppDefinition = { + appId: "settings", + title: "Settings", + icon: "settings", + launch: async (ctx, params) => { + if (activeSettingsApp) { + if (params?.section) activeSettingsApp.focusSection(params.section); + shell.focusWindow(SETTINGS_WINDOW_ID); + ctx.requestShellRender(); + return; + } + await ctx.launchInProcessApp(SETTINGS_WINDOW_ID, SETTINGS_SURFACE_ID, (options) => { + const app = createSettingsAppWindow({ + ...options, + onClosed: () => { + // Closing mid-edit must not leave the phone-side editor dangling. + void options.actions.endTextSettingEdit(); + ctx.setTextEditorHost(null); + activeSettingsApp = null; + options.onClosed(); + }, + }); + activeSettingsApp = app; + // The controller echoes phone-side edits into the glasses editor. + ctx.setTextEditorHost(app); + return app.inProcess; + }); + if (params?.section) activeSettingsApp?.focusSection(params.section); + }, +}; + +export default settingsApp; diff --git a/app/apps/settings/settings-app.ts b/app/apps/settings/settings-app.ts index 9cec23f4..092e0887 100644 --- a/app/apps/settings/settings-app.ts +++ b/app/apps/settings/settings-app.ts @@ -2,7 +2,7 @@ import { GrayImage } from "../../graphics/image"; import { LayerActions } from "../../ui/layers"; import { EditTextSettingLayer } from "../../ui/dashboard-settings"; import { createSettingsPanelLayer } from "../../ui/dashboard/settings-menus"; -import { createInProcessWindow } from "../../ui/shell/in-process-window"; +import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window"; import { type ShellWindow } from "../../ui/shell/shell"; export const SETTINGS_WINDOW_ID = "settings"; @@ -18,6 +18,8 @@ export type SettingsAppOptions = { export type SettingsAppWindow = { window: ShellWindow; + /** The full in-process window record, for hosts that manage window lifecycle. */ + inProcess: InProcessWindow; requestRender: () => void; /** Select a section in the left column by label (e.g. "Terminal"). */ focusSection: (label: string) => void; @@ -35,7 +37,7 @@ export type SettingsAppWindow = { */ export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAppWindow { const panel = createSettingsPanelLayer(); - const { window, stack, requestRender } = createInProcessWindow({ + const inProcess = createInProcessWindow({ appId: "settings", windowId: SETTINGS_WINDOW_ID, title: "Settings", @@ -51,8 +53,10 @@ export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAp removeSurface: options.removeSurface, onClosed: options.onClosed, }); + const { window, stack, requestRender } = inProcess; return { window, + inProcess, requestRender, focusSection: (label) => { panel.focusSection(label); diff --git a/app/apps/terminal/index.ts b/app/apps/terminal/index.ts new file mode 100644 index 00000000..9de1e7e9 --- /dev/null +++ b/app/apps/terminal/index.ts @@ -0,0 +1,20 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const terminalApp: AppDefinition = { + appId: "terminal", + title: "Terminal", + icon: "terminal", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./terminal-app.worker"), + windowId: "terminal:hub", + title: "Terminal", + iconLetter: "T", + icon: "terminal", + // Session windows stay independent: launching reopens the hub even + // while terminal sessions are open. + matchExistingBy: "windowId", + }), +}; + +export default terminalApp; diff --git a/app/apps/timer/index.ts b/app/apps/timer/index.ts new file mode 100644 index 00000000..a2cf51b8 --- /dev/null +++ b/app/apps/timer/index.ts @@ -0,0 +1,17 @@ +import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; + +const timerApp: AppDefinition = { + appId: "timer", + title: "Timer", + icon: "timer", + launch: (ctx) => + launchWorkerAppWindow(ctx, { + createWorker: () => new Worker("./timer-app.worker"), + windowId: "timer:main", + title: "Timer", + iconLetter: "T", + icon: "timer", + }), +}; + +export default timerApp; diff --git a/app/apps/transcribe/index.ts b/app/apps/transcribe/index.ts new file mode 100644 index 00000000..ebd3c0f3 --- /dev/null +++ b/app/apps/transcribe/index.ts @@ -0,0 +1,18 @@ +import { type AppDefinition } from "../app-definition"; +import { createTranscribeAppWindow, TRANSCRIBE_SURFACE_ID, TRANSCRIBE_WINDOW_ID } from "./transcribe-app"; + +const transcribeApp: AppDefinition = { + appId: "transcribe", + title: "Transcribe", + icon: "mic", + launch: (ctx) => + ctx.launchInProcessApp(TRANSCRIBE_WINDOW_ID, TRANSCRIBE_SURFACE_ID, (options) => + createTranscribeAppWindow({ + ...options, + startContinuousVoiceCapture: () => void options.actions.startContinuousVoiceCapture(), + stopContinuousVoiceCapture: () => void options.actions.stopContinuousVoiceCapture(), + }), + ), +}; + +export default transcribeApp; diff --git a/app/apps/weather/index.ts b/app/apps/weather/index.ts new file mode 100644 index 00000000..8245202d --- /dev/null +++ b/app/apps/weather/index.ts @@ -0,0 +1,11 @@ +import { type AppDefinition } from "../app-definition"; +import { createWeatherAppWindow, WEATHER_SURFACE_ID, WEATHER_WINDOW_ID } from "./weather-app"; + +const weatherApp: AppDefinition = { + appId: "weather", + title: "Weather", + icon: "cloud-sun", + launch: (ctx) => ctx.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow), +}; + +export default weatherApp; diff --git a/app/assistant/window-tools.ts b/app/assistant/window-tools.ts index 123a2214..792a3c48 100644 --- a/app/assistant/window-tools.ts +++ b/app/assistant/window-tools.ts @@ -5,7 +5,7 @@ * mirrors what the user can do from the launcher grid and the sidebar. */ import { shell, type ShellWindow } from "../ui/shell/shell"; -import type { LauncherAppEntry } from "../apps/launcher/launcher-app"; +import type { AppDefinition } from "../apps/app-definition"; import { disbandFolder, FOLDER_NAME_MAX_LENGTH, @@ -18,7 +18,7 @@ import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registr export type WindowToolsOptions = { /** The launcher's app list; fixes the ids apps.launch accepts. */ - apps: LauncherAppEntry[]; + apps: readonly AppDefinition[]; /** Launch (or focus) an app exactly as the launcher grid would. */ launchApp: (appId: string) => Promise | void; /** Repaint the shell surface so the sidebar reflects a focus change. */ diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 75335585..806db6d6 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -8,14 +8,13 @@ import { OsEventTypeName, } from "./events"; import { loadDeviceAddresses } from "./device-addresses"; -import { ensureBlePermissions, ensureFineLocationPermission, ensureVoicePermissions } from "./android-permissions"; +import { ensureBlePermissions, ensureVoicePermissions } from "./android-permissions"; import { FaceclawCommunicatorBridge, type RawInputEvent } from "../native/faceclaw-communicator"; import * as frameTimings from "../native/frame-timings"; import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service"; import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout-bridge"; import { onAndroidNotificationPosted } from "../native/notification-icons"; -import { isNotificationListenerEnabled, requestNotificationListenerAccess } from "../native/notification-access"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage } from "./firmware-compat"; @@ -32,55 +31,13 @@ import { registerNavigateTools } from "../assistant/navigate-tools"; import { assistantBridge } from "../assistant/bridge-client"; import { registerWindowTools } from "../assistant/window-tools"; import { WorkerAppHost } from "../ui/shell/worker-window"; +import { ALL_APPS } from "../apps/all-apps"; import { - createLauncherWindow, - LAUNCHER_SURFACE_ID, - type LauncherAppEntry, -} from "../apps/launcher/launcher-app"; -import { - createSettingsAppWindow, - SETTINGS_SURFACE_ID, - SETTINGS_WINDOW_ID, - type SettingsAppWindow, -} from "../apps/settings/settings-app"; -import { - createNotificationsAppWindow, - NOTIFICATIONS_SURFACE_ID, - NOTIFICATIONS_WINDOW_ID, -} from "../apps/notifications/notifications-app"; -import { - createCalendarAppWindow, - CALENDAR_SURFACE_ID, - CALENDAR_WINDOW_ID, -} from "../apps/calendar/calendar-app"; -import { - createWeatherAppWindow, - WEATHER_SURFACE_ID, - WEATHER_WINDOW_ID, -} from "../apps/weather/weather-app"; -import { - createDebugTestsAppWindow, - DEBUG_TESTS_SURFACE_ID, - DEBUG_TESTS_WINDOW_ID, -} from "../apps/debug-tests/debug-tests-app"; -import { - createFilesAppWindow, - createImageDocumentWindow, - createTextDocumentWindow, - FILES_SURFACE_ID, - FILES_WINDOW_ID, -} from "../apps/files/files-app"; -import { - createNightscoutAppWindow, - NIGHTSCOUT_SURFACE_ID, - NIGHTSCOUT_WINDOW_ID, -} from "../apps/nightscout/nightscout-app"; -import { createMusicAppWindow, MUSIC_SURFACE_ID, MUSIC_WINDOW_ID } from "../apps/music/music-app"; -import { - createTranscribeAppWindow, - TRANSCRIBE_SURFACE_ID, - TRANSCRIBE_WINDOW_ID, -} from "../apps/transcribe/transcribe-app"; + type AppContext, + type AppDefinition, + type AppLaunchParams, + type TextEditorHost, +} from "../apps/app-definition"; import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window"; import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence"; import { appViewportRect, type WindowHeightMode } from "../ui/shell/geometry"; @@ -159,24 +116,7 @@ const EVEN_APP_DETECTED_MESSAGE = "The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it."; // The launcher grid's app list; also fixes the app ids apps.launch accepts. -const LAUNCHER_APPS: LauncherAppEntry[] = [ - { appId: "timer", label: "Timer", icon: "timer" }, - { appId: "terminal", label: "Terminal", icon: "terminal" }, - { appId: "files", label: "Files", icon: "folder" }, - { appId: "music", label: "Music", icon: "music" }, - { appId: "nightscout", label: "Nightscout", icon: "nightscout" }, - { appId: "transcribe", label: "Transcribe", icon: "mic" }, - { appId: "notifications", label: "Notifications", icon: "bell" }, - { appId: "calendar", label: "Calendar", icon: "calendar" }, - { appId: "weather", label: "Weather", icon: "cloud-sun" }, - { appId: "navigate", label: "Navigate", icon: "map" }, - { appId: "blocks", label: "Blocks", icon: "l-piece" }, - { appId: "minesweeper", label: "Minesweeper", icon: "bomb" }, - { appId: "freecell", label: "Freecell", icon: "spade" }, - { appId: "pinball", label: "Pinball", icon: "pinball" }, - { appId: "debug-tests", label: "Debug tests", icon: "flask-conical" }, - { appId: "settings", label: "Settings", icon: "settings" }, -]; +const LAUNCHABLE_APPS = ALL_APPS.filter((app) => app.showInLauncher !== false); function createInitialDisplayPreview(): ImageSource | null { return grayImageToPreviewSource(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0)); @@ -281,10 +221,9 @@ class DashboardController { private nextShellRenderWantsFreshData = false; // One shared worker per app hosts all its windows; spawned on first launch. private readonly appHosts = new Map(); - private nextWindowSerial = 1; - // The Settings app is in-process (its text editor syncs with the phone UI - // through this controller); tracked so edit flows reach its window. - private settingsApp: SettingsAppWindow | null = null; + // The window hosting the on-glasses text-setting editor (the Settings app + // registers itself); edit flows from the phone UI reach it through this. + private textEditorHost: TextEditorHost | null = null; // Other in-process singleton apps, keyed by windowId. private readonly inProcessApps = new Map(); private sharedActions!: Omit; @@ -314,7 +253,7 @@ class DashboardController { registerNavigateTools((appId) => this.launchApp(appId)); // apps.* tools mirror the launcher grid and sidebar (launch, focus, close). registerWindowTools({ - apps: LAUNCHER_APPS, + apps: LAUNCHABLE_APPS, launchApp: (appId) => this.launchApp(appId), requestShellRender: () => this.requestShellRender(), }); @@ -333,22 +272,11 @@ class DashboardController { if (on) this.requestShellRender(); }, }); - // The launcher is pinned first in the sidebar and is the boot foreground. - shell.registerWindow( - createLauncherWindow({ - actions: { - ...sharedActions, - requestRender: () => shell.foregroundWindow()?.requestRender(), - }, - apps: LAUNCHER_APPS, - launchApp: (appId) => this.launchApp(appId), - submitFrame: (image, paintMs, frameId) => - this.submitWindowFrame(LAUNCHER_SURFACE_ID, image, paintMs, frameId), - setSurfaceVisible: (visible) => { - this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible); - }, - }), - ); + // Boot hooks register windows that exist from startup (the launcher, + // pinned first in the sidebar and the boot foreground). + for (const app of ALL_APPS) { + app.boot?.(this.buildAppContext(app)); + } this.offAndroidNotification = onAndroidNotificationPosted((notificationKey) => { void this.handleAndroidNotificationPosted(notificationKey).catch((error) => { this.appendLog(`notification wake failed: ${this.formatError(error)}`); @@ -1256,27 +1184,12 @@ class DashboardController { if (!shell.isScreenOn()) { shell.wake("sidebar"); } - this.openTextDocumentWindow("Shared text", text); - } - - /** Open a text document as its own (closeable, non-singleton) window. */ - private openTextDocumentWindow(title: string, text: string): void { - const windowId = `files:doc:${this.nextWindowSerial++}`; - void this.launchInProcessApp(windowId, `window:${windowId}`, (options) => - createTextDocumentWindow(windowId, title, text, options), - ).catch((error) => { - this.appendLog(`text document window failed: ${this.formatError(error)}`); - }); - } - - /** Open an image file as its own (closeable, non-singleton) window. */ - private openImageDocumentWindow(title: string, path: string): void { - const windowId = `files:doc:${this.nextWindowSerial++}`; - void this.launchInProcessApp(windowId, `window:${windowId}`, (options) => - createImageDocumentWindow(windowId, title, path, options), - ).catch((error) => { - this.appendLog(`image window failed: ${this.formatError(error)}`); - }); + const handler = ALL_APPS.find((app) => app.openSharedText); + if (!handler?.openSharedText) { + this.appendLog("no app handles shared text"); + return; + } + handler.openSharedText(this.buildAppContext(handler), "Shared text", text); } private startTextSettingEdit(setting: ConfigSettingString): void { @@ -1349,8 +1262,8 @@ class DashboardController { finishActiveTextSettingEdit(): void { if (!this.activeTextSetting) return; this.endTextSettingEdit(); - if (this.settingsApp?.closeTextEditor()) { - this.settingsApp.requestRender(); + if (this.textEditorHost?.closeTextEditor()) { + this.textEditorHost.requestRender(); } } @@ -1370,8 +1283,8 @@ class DashboardController { private previewOrRenderAfterTextSettingChange(): void { // Echo phone-side keystrokes into the Settings app's glasses editor. - if (this.settingsApp?.isTextEditorOnTop()) { - this.settingsApp.requestRender(); + if (this.textEditorHost?.isTextEditorOnTop()) { + this.textEditorHost.requestRender(); } } @@ -1466,40 +1379,6 @@ class DashboardController { } } - /** Launch or focus the (in-process, singleton) Settings app, optionally on a section. */ - private async launchSettingsApp(section?: string): Promise { - if (this.settingsApp) { - if (section) this.settingsApp.focusSection(section); - shell.focusWindow(SETTINGS_WINDOW_ID); - this.requestShellRender(); - return; - } - const settingsApp = createSettingsAppWindow({ - actions: { - ...this.sharedActions, - requestRender: () => {}, // rebound by createInProcessWindow - }, - submitFrame: (image, paintMs, frameId) => - this.submitWindowFrame(SETTINGS_SURFACE_ID, image, paintMs, frameId), - setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(SETTINGS_SURFACE_ID, visible), - removeSurface: () => this.removeWindowSurface(SETTINGS_SURFACE_ID), - onClosed: () => { - // Closing mid-edit must not leave the phone-side editor dangling. - this.endTextSettingEdit(); - this.settingsApp = null; - }, - }); - 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. - await this.configureWindowSurface(SETTINGS_SURFACE_ID, false, settingsApp.window.heightMode); - shell.focusWindow(SETTINGS_WINDOW_ID); - this.requestShellRender(); - this.appendLog("launched settings"); - } - /** Launch or focus an in-process singleton app (notifications, debug tests). */ private async launchInProcessApp( windowId: string, @@ -1533,38 +1412,19 @@ class DashboardController { } /** Get or spawn the worker host for an app. */ - private ensureAppHost(appId: string): WorkerAppHost | null { + private ensureWorkerHost(appId: string, createWorker: () => Worker): WorkerAppHost { const existing = this.appHosts.get(appId); if (existing) return existing; - // Worker paths must be string literals for the webpack worker loader. - let worker: Worker; - if (appId === "timer") { - worker = new Worker("../apps/timer/timer-app.worker"); - } else if (appId === "terminal") { - worker = new Worker("../apps/terminal/terminal-app.worker"); - } else if (appId === "navigate") { - worker = new Worker("../apps/navigate/navigate-app.worker"); - } else if (appId === "blocks") { - worker = new Worker("../apps/blocks/blocks-app.worker"); - } else if (appId === "minesweeper") { - worker = new Worker("../apps/minesweeper/minesweeper-app.worker"); - } else if (appId === "freecell") { - worker = new Worker("../apps/freecell/freecell-app.worker"); - } else if (appId === "pinball") { - worker = new Worker("../apps/pinball/pinball-app.worker"); - } else { - return null; - } const host = new WorkerAppHost({ appId, - worker, + worker: createWorker(), configureSurface: (surfaceId, visible, heightMode) => this.configureWindowSurface(surfaceId, visible, heightMode), setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible), removeSurface: (surfaceId) => this.removeWindowSurface(surfaceId), requestShellRender: () => this.requestShellRender(), openSettings: (section) => { - void this.launchSettingsApp(section).catch((error) => { + void this.launchApp("settings", { section }).catch((error) => { this.appendLog(`settings launch failed: ${this.formatError(error)}`); }); }, @@ -1573,6 +1433,26 @@ class DashboardController { return host; } + /** The services an app's launch/boot callbacks may use; see AppContext. */ + private buildAppContext(app: AppDefinition): AppContext { + return { + appId: app.appId, + apps: ALL_APPS, + actions: this.sharedActions, + launchApp: (appId, params) => this.launchApp(appId, params), + launchInProcessApp: (windowId, surfaceId, create) => this.launchInProcessApp(windowId, surfaceId, create), + ensureWorkerHost: (createWorker) => this.ensureWorkerHost(app.appId, createWorker), + submitWindowFrame: (surfaceId, image, paintMs, frameId) => + this.submitWindowFrame(surfaceId, image, paintMs, frameId), + setWindowSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible), + requestShellRender: () => this.requestShellRender(), + appendLog: (message) => this.appendLog(message), + setTextEditorHost: (host) => { + this.textEditorHost = host; + }, + }; + } + /** Save which apps are open (and which is foreground) for restoreOpenApps. */ private persistOpenApps(): void { if (!this.openAppsRestored || this.suppressOpenAppsPersist) return; @@ -1596,7 +1476,7 @@ class DashboardController { this.openAppsRestored = true; const saved = loadPersistedOpenApps(); if (!saved.open.length) return; - const known = new Set(LAUNCHER_APPS.map((app) => app.appId)); + const known = new Set(ALL_APPS.map((app) => app.appId)); this.suppressOpenAppsPersist = true; try { for (const appId of saved.open) { @@ -1620,168 +1500,17 @@ class DashboardController { } /** - * Launch an app from the launcher: open a window in the app's shared - * worker (spawned on first launch) and foreground it. Launching an app - * with an open singleton window (the terminal hub, settings) focuses it. + * Launch an app by id: the launcher grid, assistant tools, window-menu + * Settings item, and open-apps restore all come through here. Apps with an + * open window focus it instead of opening another. */ - private async launchApp(appId: string): Promise { - if (appId === "settings") { - await this.launchSettingsApp(); - return; - } - if (appId === "notifications") { - // Without notification-listener access the tray reads as empty; prompt - // the user on the phone so the on-glasses "grant permission" message is - // actionable. The app still opens to show that message. - if (!isNotificationListenerEnabled()) { - requestNotificationListenerAccess(); - } - await this.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow); - return; - } - if (appId === "calendar") { - await this.launchInProcessApp(CALENDAR_WINDOW_ID, CALENDAR_SURFACE_ID, createCalendarAppWindow); - return; - } - if (appId === "weather") { - await this.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow); - return; - } - if (appId === "debug-tests") { - await this.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow); - return; - } - if (appId === "nightscout") { - await this.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow); - return; - } - if (appId === "music") { - await this.launchInProcessApp(MUSIC_WINDOW_ID, MUSIC_SURFACE_ID, createMusicAppWindow); - return; - } - if (appId === "transcribe") { - await this.launchInProcessApp(TRANSCRIBE_WINDOW_ID, TRANSCRIBE_SURFACE_ID, (options) => - createTranscribeAppWindow({ - ...options, - startContinuousVoiceCapture: () => this.startContinuousVoiceCapture(), - stopContinuousVoiceCapture: () => this.stopContinuousVoiceCapture(), - }), - ); - return; - } - if (appId === "files") { - await this.launchInProcessApp(FILES_WINDOW_ID, FILES_SURFACE_ID, (options) => - createFilesAppWindow({ - ...options, - openDocumentWindow: (title, text) => this.openTextDocumentWindow(title, text), - openImageWindow: (title, path) => this.openImageDocumentWindow(title, path), - }), - ); - return; - } - const host = this.ensureAppHost(appId); - if (!host) { + private async launchApp(appId: string, params?: AppLaunchParams): Promise { + const app = ALL_APPS.find((entry) => entry.appId === appId); + if (!app) { this.appendLog(`unknown app: ${appId}`); return; } - if (appId === "terminal") { - const existingHub = shell.getWindows().find((w) => w.windowId === "terminal:hub"); - if (existingHub) { - shell.focusWindow(existingHub.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ windowId: "terminal:hub", title: "Terminal", iconLetter: "T", icon: "terminal", focus: true }); - this.appendLog("launched terminal:hub"); - return; - } - if (appId === "timer") { - const existing = shell.getWindows().find((window) => window.appId === "timer"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ windowId: "timer:main", title: "Timer", iconLetter: "T", icon: "timer", focus: true }); - this.appendLog("launched timer:main"); - return; - } - if (appId === "blocks") { - const existing = shell.getWindows().find((window) => window.appId === "blocks"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ windowId: "blocks:main", title: "Blocks", iconLetter: "B", icon: "l-piece", focus: true }); - this.appendLog("launched blocks:main"); - return; - } - if (appId === "minesweeper") { - const existing = shell.getWindows().find((window) => window.appId === "minesweeper"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ - windowId: "minesweeper:main", - title: "Minesweeper", - iconLetter: "M", - icon: "bomb", - focus: true, - }); - this.appendLog("launched minesweeper:main"); - return; - } - if (appId === "freecell") { - const existing = shell.getWindows().find((window) => window.appId === "freecell"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ - windowId: "freecell:main", - title: "Freecell", - iconLetter: "F", - icon: "spade", - focus: true, - }); - this.appendLog("launched freecell:main"); - return; - } - if (appId === "pinball") { - const existing = shell.getWindows().find((window) => window.appId === "pinball"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ - windowId: "pinball:main", - title: "Pinball", - iconLetter: "P", - icon: "pinball", - focus: true, - }); - this.appendLog("launched pinball:main"); - return; - } - if (appId === "navigate") { - // Navigation needs precise location; prompt on the phone while the - // window opens (the worker can't show permission dialogs). - void ensureFineLocationPermission().catch(() => {}); - const existing = shell.getWindows().find((window) => window.appId === "navigate"); - if (existing) { - shell.focusWindow(existing.windowId); - this.requestShellRender(); - return; - } - host.openWindow({ windowId: "navigate:main", title: "Navigate", iconLetter: "N", icon: "map", focus: true }); - this.appendLog("launched navigate:main"); - return; - } + await app.launch(this.buildAppContext(app), params); } /** Create/refresh a window surface on the compositor, if connected. */ @@ -1911,8 +1640,6 @@ class DashboardController { } private async handleAndroidNotificationPosted(notificationKey: string): Promise { - // Keep the Notifications app's list fresh if it is open. - this.inProcessApps.get(NOTIFICATIONS_WINDOW_ID)?.requestRender(); if (!notificationKey) { this.requestShellRender(); return;