Add a consolidated single export to each app

This commit is contained in:
James Babcock committed 2026-08-03 00:51:57 -07:00
1 parent 9b3832d7a7
commit f38366df6e
23 files changed
+597 -337

No files matched your search

+43
View File
@@ -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,
];
+125
View File
@@ -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<void>;
/**
* 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<LayerActions, "requestRender">;
/** Launch (or focus) any app by id through the shell. */
launchApp: (appId: string, params?: AppLaunchParams) => Promise<void>;
/** Launch-or-focus a main-thread singleton window keyed by windowId. */
launchInProcessApp: (
windowId: string,
surfaceId: string,
create: (options: InProcessAppOptions) => InProcessWindow,
) => Promise<void>;
/** 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<void>;
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<void> {
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}`);
}
+17
View File
@@ -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;
+11
View File
@@ -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;
+11
View File
@@ -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;
+52
View File
@@ -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;
+17
View File
@@ -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;
+36
View File
@@ -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;
+17
View File
@@ -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;
+11
View File
@@ -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;
+22
View File
@@ -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;
+11
View File
@@ -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;
+20
View File
@@ -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;
+10 -2
View File
@@ -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;
}
+17
View File
@@ -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;
+45
View File
@@ -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;
+6 -2
View File
@@ -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);
+20
View File
@@ -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;
+17
View File
@@ -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;
+18
View File
@@ -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;
+11
View File
@@ -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;