From 68ab5998565688dbddc0338342ec10e3720a444a Mon Sep 17 00:00:00 2001 From: James Babcock Date: Tue, 7 Jul 2026 00:52:47 -0700 Subject: [PATCH] Migrate things into new UI design --- .../com/faceclaw/app/FaceclawWebSocket.java | 18 +- .../java/com/faceclaw/app/util/BmpUtil.java | 5 +- app/g2/dashboard-controller.ts | 218 ++++++-- app/native/g2mirror-client.ts | 43 ++ app/ui/apps/terminal.ts | 316 ------------ app/ui/dashboard-settings.ts | 14 +- app/ui/dashboard.ts | 21 +- app/ui/dashboard/about-page.ts | 14 +- app/ui/dashboard/root-menu.ts | 54 +- app/ui/dashboard/settings-menus.ts | 57 ++- app/ui/layers.ts | 9 + app/ui/notifications.ts | 136 +++-- app/ui/shell/chrome-layer.ts | 5 +- app/ui/shell/in-process-window.ts | 117 +++++ app/ui/shell/launcher-app.ts | 55 +- app/ui/shell/modal-layer.ts | 49 ++ app/ui/shell/notifications-app.ts | 36 ++ app/ui/shell/settings-app.ts | 54 ++ app/ui/shell/shell.ts | 28 + app/ui/shell/worker-window.ts | 137 +++-- app/workers/terminal-app.worker.ts | 478 ++++++++++++++++++ 21 files changed, 1262 insertions(+), 602 deletions(-) delete mode 100644 app/ui/apps/terminal.ts create mode 100644 app/ui/shell/in-process-window.ts create mode 100644 app/ui/shell/modal-layer.ts create mode 100644 app/ui/shell/notifications-app.ts create mode 100644 app/ui/shell/settings-app.ts create mode 100644 app/workers/terminal-app.worker.ts diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java index d94e5832..0d4bb2c5 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java @@ -14,14 +14,16 @@ import okhttp3.WebSocketListener; /** * Thin okhttp WebSocket wrapper for the TypeScript side. Text frames only - * (the g2mirror protocol is JSON-in-text-frames). All listener callbacks are - * posted to the main thread, where the NativeScript runtime expects them. + * (the g2mirror protocol is JSON-in-text-frames). Listener callbacks are + * posted to the Looper of the thread that constructed this object, so a JS + * isolate (main thread or app worker) always receives them on its own + * thread; a Looper-less constructing thread falls back to the main thread. */ public class FaceclawWebSocket { private static final String TAG = "FaceclawWebSocket"; - private static final Handler mainHandler = new Handler(Looper.getMainLooper()); private static volatile OkHttpClient sharedClient; + private final Handler callbackHandler; private final WebSocket socket; private volatile boolean closeRequested; @@ -35,6 +37,8 @@ public class FaceclawWebSocket { if (listener == null) { throw new IllegalArgumentException("listener is required"); } + Looper looper = Looper.myLooper(); + callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); Request.Builder builder = new Request.Builder().url(url.trim()); if (headerName != null && !headerName.isEmpty() && headerValue != null) { builder.addHeader(headerName, headerValue); @@ -51,7 +55,7 @@ public class FaceclawWebSocket { + " headers=[" + headerLog.toString().trim() + "]"); socket = getClient().newWebSocket(request, new WebSocketListener() { @Override public void onOpen(WebSocket webSocket, Response response) { - mainHandler.post(() -> { + callbackHandler.post(() -> { try { listener.onOpen(); } catch (Throwable t) { @@ -61,7 +65,7 @@ public class FaceclawWebSocket { } @Override public void onMessage(WebSocket webSocket, String text) { - mainHandler.post(() -> { + callbackHandler.post(() -> { try { listener.onTextMessage(text); } catch (Throwable t) { @@ -75,7 +79,7 @@ public class FaceclawWebSocket { } @Override public void onClosed(WebSocket webSocket, int code, String reason) { - mainHandler.post(() -> { + callbackHandler.post(() -> { try { listener.onClosed(code, reason == null ? "" : reason); } catch (Throwable t) { @@ -89,7 +93,7 @@ public class FaceclawWebSocket { return; } final String message = t == null ? "unknown websocket failure" : String.valueOf(t); - mainHandler.post(() -> { + callbackHandler.post(() -> { try { listener.onFailure(message); } catch (Throwable inner) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java index e86c31f7..225888d7 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java @@ -85,10 +85,13 @@ public class BmpUtil { } private static int grayToNibble(int value) { + // Truncate rather than round up: the shell's color-key convention + // paints intentional black as 1 (0 being transparent), which must + // land on nibble 0, not a faintly-visible level 1. if (value <= 0) { return 0; } - return Math.min(15, (value + 15) >> 4); + return Math.min(15, value >> 4); } private static void putUint16Le(byte[] buf, int offset, int value) { diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 9278c2e6..35924525 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -18,7 +18,6 @@ import { closeDashboardTextSettingEditor, drawDashboard, logToDashboard, - openAndroidNotificationFromSleep, openTelepromptDocument, receiveDashboardWindowInput, resetDashboardSleepTimerAndWake, @@ -29,7 +28,20 @@ import { inputEventToString, rawInputEventToInputEvent, shell, type ShellInputOu import { makeLetterWindowIcon } from "../ui/shell/chrome-layer"; import { WorkerAppHost } from "../ui/shell/worker-window"; import { createLauncherWindow, LAUNCHER_SURFACE_ID } from "../ui/shell/launcher-app"; +import { + createSettingsAppWindow, + SETTINGS_SURFACE_ID, + SETTINGS_WINDOW_ID, + type SettingsAppWindow, +} from "../ui/shell/settings-app"; +import { + createNotificationsAppWindow, + NOTIFICATIONS_SURFACE_ID, + NOTIFICATIONS_WINDOW_ID, +} from "../ui/shell/notifications-app"; +import { type InProcessWindow } from "../ui/shell/in-process-window"; import { APP_VIEWPORT } from "../ui/shell/geometry"; +import { type LayerActions } from "../ui/layers"; import { elevenLabsApiKeySetting, nightscoutApiTokenSetting, @@ -180,12 +192,18 @@ class DashboardController { private queuedFrameId = 0; private shellRenderInProgress = false; private shellRenderQueued = false; + private nextShellRenderWantsFreshData = false; // Last painted surface images, for compositing the phone-side preview. private lastShellImage: GrayImage | null = null; private lastWindowImage: GrayImage | null = null; - // One shared worker hosts every stopwatch window; spawned on first launch. - private stopwatchHost: WorkerAppHost | null = null; + // 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; + private notificationsApp: InProcessWindow | null = null; + private sharedActions!: Omit; private lastForegroundNotificationUpdateAtMs = 0; private lastConnectedPreviewUpdateAtMs = 0; // Consumed by the next renderDashboard: repaint with data sources not @@ -202,6 +220,7 @@ class DashboardController { stopVoiceCapture: () => this.stopVoiceCapture(), playBuzzerNote: (note: number, oct: number, beat: number) => this.playBuzzerNote(note, oct, beat), }; + this.sharedActions = sharedActions; setDashboardActions({ ...sharedActions, requestRender: () => { @@ -240,7 +259,12 @@ class DashboardController { ...sharedActions, requestRender: () => shell.foregroundWindow()?.requestRender(), }, - apps: [{ appId: "stopwatch", label: "Stopwatch" }], + apps: [ + { appId: "stopwatch", label: "Stopwatch" }, + { appId: "terminal", label: "Terminal" }, + { appId: "notifications", label: "Notifications" }, + { appId: "settings", label: "Settings" }, + ], launchApp: (appId) => this.launchApp(appId), submitFrame: (image, paintMs, frameId) => this.submitWindowFrame(LAUNCHER_SURFACE_ID, image, paintMs, frameId), @@ -736,11 +760,16 @@ class DashboardController { /** * Finish the active edit from the phone side (e.g. the IME's done key): - * ends the edit session and navigates the glasses out of the edit page. + * ends the edit session and navigates the glasses out of the edit page, + * in whichever window hosts it (the Settings app or the dashboard). */ finishActiveTextSettingEdit(): void { if (!this.activeTextSetting) return; this.endTextSettingEdit(); + if (this.settingsApp?.closeTextEditor()) { + this.settingsApp.requestRender(); + return; + } if (!closeDashboardTextSettingEditor()) return; if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { @@ -774,6 +803,11 @@ class DashboardController { } private previewOrRenderAfterTextSettingChange(label: string): void { + // Echo phone-side keystrokes into the glasses editor, wherever it lives. + if (this.settingsApp?.isTextEditorOnTop()) { + this.settingsApp.requestRender(); + return; + } if (this.phase === "connected" && this.communicator) { void this.requestRender("interval").catch((error) => { this.appendLog(`${label} update failed: ${this.formatError(error)}`); @@ -966,37 +1000,125 @@ class DashboardController { } } + /** Launch or focus the (in-process, singleton) Settings app. */ + private async launchSettingsApp(): Promise { + if (this.settingsApp) { + 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; + shell.registerWindow(settingsApp.window); + // Configure the surface before foregrounding so the first frame has + // somewhere to land. + await this.configureWindowSurface(SETTINGS_SURFACE_ID, false); + shell.focusWindow(SETTINGS_WINDOW_ID); + this.requestShellRender(); + this.appendLog("launched settings"); + } + + /** Launch or focus the (in-process, singleton) Notifications app. */ + private async launchNotificationsApp(): Promise { + if (this.notificationsApp) { + shell.focusWindow(NOTIFICATIONS_WINDOW_ID); + this.requestShellRender(); + return; + } + const notificationsApp = createNotificationsAppWindow({ + actions: { + ...this.sharedActions, + requestRender: () => {}, // rebound by createInProcessWindow + }, + submitFrame: (image, paintMs, frameId) => + this.submitWindowFrame(NOTIFICATIONS_SURFACE_ID, image, paintMs, frameId), + setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(NOTIFICATIONS_SURFACE_ID, visible), + removeSurface: () => this.removeWindowSurface(NOTIFICATIONS_SURFACE_ID), + onClosed: () => { + this.notificationsApp = null; + }, + }); + this.notificationsApp = notificationsApp; + shell.registerWindow(notificationsApp.window); + await this.configureWindowSurface(NOTIFICATIONS_SURFACE_ID, false); + shell.focusWindow(NOTIFICATIONS_WINDOW_ID); + this.requestShellRender(); + this.appendLog("launched notifications"); + } + + /** Get or spawn the worker host for an app. */ + private ensureAppHost(appId: string): WorkerAppHost | null { + 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 === "stopwatch") { + worker = new Worker("../workers/stopwatch-app.worker"); + } else if (appId === "terminal") { + worker = new Worker("../workers/terminal-app.worker"); + } else { + return null; + } + const host = new WorkerAppHost({ + appId, + worker, + viewport: { width: APP_VIEWPORT.width, height: APP_VIEWPORT.height }, + configureSurface: (surfaceId, visible) => this.configureWindowSurface(surfaceId, visible), + setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible), + removeSurface: (surfaceId) => this.removeWindowSurface(surfaceId), + requestShellRender: () => this.requestShellRender(), + }); + this.appHosts.set(appId, host); + return host; + } + /** - * Launch an app from the launcher: allocate a window (in the app's shared - * worker, spawned on first launch), configure its surface, and foreground - * it. Launching an already-running app opens another window of it. + * 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. */ private async launchApp(appId: string): Promise { - if (appId !== "stopwatch") { + if (appId === "settings") { + await this.launchSettingsApp(); + return; + } + if (appId === "notifications") { + await this.launchNotificationsApp(); + return; + } + const host = this.ensureAppHost(appId); + if (!host) { this.appendLog(`unknown app: ${appId}`); return; } - if (!this.stopwatchHost) { - this.stopwatchHost = new WorkerAppHost("stopwatch", new Worker("../workers/stopwatch-app.worker")); + 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", focus: true }); + this.appendLog("launched terminal:hub"); + return; } const serial = this.nextWindowSerial++; - const windowId = `stopwatch:${serial}`; - const surfaceId = `window:${windowId}`; - const window = this.stopwatchHost.openWindow({ - windowId, - surfaceId, - title: `Stopwatch ${serial}`, - iconLetter: "S", - viewport: { width: APP_VIEWPORT.width, height: APP_VIEWPORT.height }, - setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(surfaceId, visible), - removeSurface: () => this.removeWindowSurface(surfaceId), - }); - shell.registerWindow(window); - // Configure the surface before foregrounding so the worker's first frame - // has somewhere to land. - await this.configureWindowSurface(surfaceId, false); - shell.focusWindow(windowId); - this.requestShellRender(); + const windowId = `${appId}:${serial}`; + host.openWindow({ windowId, title: `Stopwatch ${serial}`, iconLetter: "S", focus: true }); this.appendLog(`launched ${windowId}`); } @@ -1078,13 +1200,21 @@ class DashboardController { private async renderShell(): Promise { const frameId = frameTimings.startFrame("render:shell"); - beginRenderPass(true); + const wantFreshData = this.nextShellRenderWantsFreshData; + this.nextShellRenderWantsFreshData = false; + beginRenderPass(!wantFreshData); const paintStartedAtMs = Date.now(); const image = frameTimings.span(frameId, "paint", () => frameTimings.runWithFrame(frameId, () => shell.paintSurface()), ); const paintMs = Date.now() - paintStartedAtMs; - endRenderPass(); + const paintUsedStaleData = endRenderPass(); + if (paintUsedStaleData) { + // Repaint with fresh data (e.g. notification icons) once this frame is + // out; mirrors renderDashboard's stale-data contract. + this.nextShellRenderWantsFreshData = true; + this.requestShellRender(); + } this.lastShellImage = image; if (!this.communicator || this.phase === "charging") { frameTimings.finishFrame(frameId, "discarded: shell render with no active connection"); @@ -1123,22 +1253,24 @@ class DashboardController { } private async handleAndroidNotificationPosted(notificationKey: string): Promise { - const changed = openAndroidNotificationFromSleep(notificationKey); - if (!changed) { - if (this.phase === "connected" && this.communicator) { - await this.requestRender("interval"); - } - return; - } - - this.appendLog("android notification woke dashboard"); - if (this.phase === "connected" && this.communicator) { + // Keep the Notifications app's list fresh if it is open. + this.notificationsApp?.requestRender(); + if (!notificationKey) { this.requestShellRender(); - await this.requestRender("initial"); return; } - const image = drawDashboard(); - this.updateDisplayPreviewFromImage(image); + // New notifications open a shell modal over the app viewport; if the + // screen was off, wake for it and go back to sleep when it is closed. + // Waking while already on would steal focus, so only wake from sleep. + const wokeScreen = shell.isScreenOn() ? false : shell.wake("sidebar"); + if (wokeScreen) { + this.appendLog("android notification woke the screen"); + } + shell.openNotificationModal(notificationKey, wokeScreen); + this.requestShellRender(); + if (this.phase === "connected" && this.communicator) { + await this.requestRender("interval"); + } } private async playBuzzerNote(note: number, oct: number, beat: number): Promise { diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index 80b3b4ff..96ac05e3 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -17,6 +17,10 @@ export type G2MirrorSession = { socket: string; pid: number; cwdHint: string; + /** Unix epoch ms of the terminal's last bell, or null if none observed. */ + lastBellAt: number | null; + /** Window title the app last set (xterm OSC 0/2), or null if none observed. */ + title: string | null; }; export type G2MirrorPhase = @@ -57,6 +61,8 @@ export class G2MirrorClient { private readonly terminalDataListeners = new Set<(data: Uint8Array, kind: TerminalDataKind) => void>(); private readonly sessionAttachedListeners = new Set<(command: string) => void>(); private readonly sessionDetachedListeners = new Set<(reason: string) => void>(); + private readonly bellListeners = new Set<(socket: string, lastBellAtMs: number) => void>(); + private readonly titleListeners = new Set<(socket: string, title: string) => void>(); constructor(private readonly options: G2MirrorClientOptions) {} @@ -90,6 +96,18 @@ export class G2MirrorClient { return () => this.sessionDetachedListeners.delete(listener); } + /** Unsolicited bell notification for any monitored terminal (rate-limited server-side). */ + onBell(listener: (socket: string, lastBellAtMs: number) => void): () => void { + this.bellListeners.add(listener); + return () => this.bellListeners.delete(listener); + } + + /** Unsolicited title change for any monitored terminal. */ + onTitle(listener: (socket: string, title: string) => void): () => void { + this.titleListeners.add(listener); + return () => this.titleListeners.delete(listener); + } + start(): void { if (this.ws) return; this.stopped = false; @@ -209,11 +227,36 @@ export class G2MirrorClient { socket: String(item?.socket ?? ""), pid: Number(item?.pid) || 0, cwdHint: String(item?.cwd_hint ?? ""), + lastBellAt: typeof item?.last_bell_at === "number" ? item.last_bell_at : null, + title: typeof item?.title === "string" ? item.title : null, })) .filter((session: G2MirrorSession) => session.socket.length > 0); this.emitState(); return; } + case "bell": { + const socket = String(message.socket ?? ""); + const lastBellAt = Number(message.last_bell_at) || Date.now(); + if (!socket) return; + const session = this.sessions.find((s) => s.socket === socket); + if (session) session.lastBellAt = lastBellAt; + for (const listener of Array.from(this.bellListeners)) { + listener(socket, lastBellAt); + } + return; + } + case "title": { + const socket = String(message.socket ?? ""); + const title = String(message.title ?? ""); + if (!socket) return; + const session = this.sessions.find((s) => s.socket === socket); + if (session) session.title = title; + for (const listener of Array.from(this.titleListeners)) { + listener(socket, title); + } + this.emitState(); + return; + } case "connect": { // Session accepted us (relayed from the wrapper). this.clearListRefreshTimer(); diff --git a/app/ui/apps/terminal.ts b/app/ui/apps/terminal.ts deleted file mode 100644 index 78dedf35..00000000 --- a/app/ui/apps/terminal.ts +++ /dev/null @@ -1,316 +0,0 @@ -import { getFont } from "~/graphics/bdffont"; -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "~/graphics/image"; -import { - G2MirrorClient, - type G2MirrorSession, - type G2MirrorState, -} from "~/native/g2mirror-client"; -import { - terminalAuthTokenSetting, - terminalHostSetting, - terminalPortSetting, - textSettingMenuItem, -} from "../dashboard-settings"; -import { TOP_LEFT_MENU_LAYOUT } from "../dashboard"; -import { MenuLayer } from "../menu"; -import { type DashboardInputEvent, type Layer, type LayerActions, type LayerContext } from "../layers"; - -// Terminus-12 has a 6x12 cell, so the full 576x288 lens is a 96x24 grid — -// this is the size we declare in the g2mirror init handshake, fixed per -// websocket connection. -const terminalFont = getFont("terminus12"); -const CELL_WIDTH = 6; -const CELL_HEIGHT = 12; -const TERMINAL_COLS = G2_LENS_WIDTH / CELL_WIDTH; -const TERMINAL_ROWS = G2_LENS_HEIGHT / CELL_HEIGHT; -const DEVICE_NAME = "Faceclaw G2"; - -const HUB_ROW_HEIGHT = 20; -const HUB_LIST_TOP = 64; - -type HubItem = { - label: string; - onSelect: (ctx: LayerContext) => void; -}; - -/** - * The Terminal app hub: connection status, list of live g2mirror sessions, - * and the app-local settings menu. Selecting a session attaches and opens - * the full-screen readonly terminal view. - */ -export class TerminalAppLayer implements Layer { - private client: G2MirrorClient | null = null; - private clientState: G2MirrorState | null = null; - private selectedIndex = 0; - private lastCtx: LayerContext | null = null; - private activeViewLayer: TerminalViewLayer | null = null; - private unsubscribers: Array<() => void> = []; - - constructor(private readonly actions: LayerActions) { - selfTestEmulator(); - this.startClient(); - } - - private startClient(): void { - this.stopClient(); - const host = terminalHostSetting.get().trim(); - if (!host) return; - const port = parseInt(terminalPortSetting.get(), 10) || 8737; - const client = new G2MirrorClient({ - host, - port, - authToken: terminalAuthTokenSetting.get(), - deviceName: DEVICE_NAME, - cols: TERMINAL_COLS, - rows: TERMINAL_ROWS, - }); - this.client = client; - this.clientState = client.state(); - this.unsubscribers.push( - client.onStateChange((state) => { - this.clientState = state; - this.actions.requestRender(); - }), - client.onSessionAttached(() => { - this.openViewLayer(); - }), - client.onSessionDetached(() => { - this.closeViewLayer(); - }), - ); - client.start(); - } - - private stopClient(): void { - for (const unsubscribe of this.unsubscribers.splice(0)) { - unsubscribe(); - } - this.client?.stop(); - this.client = null; - this.clientState = null; - } - - private openViewLayer(): void { - const ctx = this.lastCtx; - if (!ctx || !this.client || this.activeViewLayer) return; - const viewLayer = new TerminalViewLayer(this.client, this.actions, () => { - if (this.activeViewLayer === viewLayer) { - this.activeViewLayer = null; - } - }); - this.activeViewLayer = viewLayer; - ctx.stack.push(viewLayer); - this.actions.requestRender(); - } - - private closeViewLayer(): void { - const ctx = this.lastCtx; - const viewLayer = this.activeViewLayer; - if (!ctx || !viewLayer) return; - ctx.stack.popIfTop((layer) => layer === viewLayer); - this.actions.requestRender(); - } - - paint(ctx: LayerContext): GrayImage { - this.lastCtx = ctx; - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - image.drawText(terminalFont, 18, 14, "Terminal", 220); - image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52); - image.drawText(terminalFont, 24, 38, this.statusLine(), 170); - - let listTop = HUB_LIST_TOP; - if (!terminalHostSetting.get().trim()) { - image.drawText(terminalFont, 24, 56, "To set up a server, see:", 150); - image.drawText(terminalFont, 24, 70, "https://github.com/jimrandomh/g2mirror", 190); - listTop += 32; - } - - const items = this.hubItems(); - this.selectedIndex = Math.max(0, Math.min(this.selectedIndex, items.length - 1)); - for (let index = 0; index < items.length; index++) { - const y = listTop + index * HUB_ROW_HEIGHT; - if (y + HUB_ROW_HEIGHT > G2_LENS_HEIGHT - 40) break; - const selected = index === this.selectedIndex; - if (selected) { - image.fillRoundedRect(20, y - 2, G2_LENS_WIDTH - 40, HUB_ROW_HEIGHT - 1, 15); - image.drawRoundedRect(20, y - 2, G2_LENS_WIDTH - 40, HUB_ROW_HEIGHT - 1, 45); - } - image.drawText(terminalFont, 32, y + 2, items[index]!.label, selected ? 255 : 200); - } - - image.drawText(terminalFont, 24, G2_LENS_HEIGHT - 36, "Double-click to go back", 110); - return image; - } - - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { - this.lastCtx = ctx; - const items = this.hubItems(); - switch (event.type) { - case "double-click": - ctx.stack.pop(); - return; - case "scroll-up": - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - return; - case "scroll-down": - this.selectedIndex = Math.min(items.length - 1, this.selectedIndex + 1); - return; - case "click": { - const item = items[Math.max(0, Math.min(this.selectedIndex, items.length - 1))]; - item?.onSelect(ctx); - return; - } - default: - return; - } - } - - onRemoved(): void { - this.stopClient(); - } - - private statusLine(): string { - const host = terminalHostSetting.get().trim(); - if (!host) { - return "No host configured. Open Settings below."; - } - return this.clientState?.status ?? "Not connected."; - } - - private hubItems(): HubItem[] { - const items: HubItem[] = []; - const state = this.clientState; - const phase = state?.phase ?? "idle"; - - if (phase === "connected" || phase === "attached") { - for (const session of state?.sessions ?? []) { - items.push({ - label: sessionLabel(session), - onSelect: () => { - this.client?.connectSession(session.socket); - }, - }); - } - if (!state?.sessions.length) { - items.push({ - label: "(no live sessions; run g2mirror )", - onSelect: () => { - this.client?.listSessions(); - }, - }); - } - } - if (phase === "idle" || phase === "failed") { - items.push({ - label: terminalHostSetting.get().trim() ? "Connect" : "Connect (host not set)", - onSelect: () => { - this.startClient(); - }, - }); - } - items.push({ - label: "Settings", - onSelect: (ctx) => { - ctx.stack.push(createTerminalSettingsMenuLayer()); - }, - }); - return items; - } -} - -/** Full-screen readonly mirror of the attached session. */ -class TerminalViewLayer implements Layer { - private readonly emulator: import("./terminal-emulator").TerminalEmulator; - private receivedData = false; - private unsubscribeData: (() => void) | null = null; - - constructor( - private readonly client: G2MirrorClient, - private readonly actions: LayerActions, - private readonly onClosed: () => void, - ) { - // Lazy import keeps xterm out of the startup path; it is only needed - // once a terminal is actually viewed. - const { TerminalEmulator } = require("./terminal-emulator") as typeof import("./terminal-emulator"); - this.emulator = new TerminalEmulator(TERMINAL_COLS, TERMINAL_ROWS); - this.unsubscribeData = client.onTerminalData((data, kind) => { - if (kind === "snapshot") { - this.emulator.reset(); - } - this.receivedData = true; - this.emulator.write(data, () => this.actions.requestRender()); - }); - client.view(); - } - - paint(): GrayImage { - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - if (!this.receivedData) { - image.drawText(terminalFont, 24, 130, "Waiting for terminal output...", 170); - image.drawText(terminalFont, 24, 150, "Double-click to go back", 110); - return image; - } - const cursor = this.emulator.cursor(); - image.fillRect(cursor.x * CELL_WIDTH, cursor.y * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT, 70); - const lines = this.emulator.visibleLines(); - for (let row = 0; row < lines.length; row++) { - const line = lines[row]!; - if (line.length === 0) continue; - image.drawText(terminalFont, 0, row * CELL_HEIGHT, line, 200); - } - return image; - } - - handleInput(event: DashboardInputEvent, ctx: LayerContext): void { - // Readonly for now: protocol v1 has no keyboard input, so scrolls and - // clicks are ignored rather than forwarded. - if (event.type === "double-click") { - ctx.stack.pop(); - } - } - - onRemoved(): void { - this.unsubscribeData?.(); - this.unsubscribeData = null; - // Let the wrapped app resize back for the person at the real keyboard, - // and free the session for other clients. - this.client.unview(); - this.client.disconnectSession(); - this.onClosed(); - } -} - -// One-time check that the xterm-headless emulator evaluates and parses in -// this JS runtime, run at app open so a broken dependency surfaces in the log -// immediately instead of mid-session-attach. -let emulatorSelfTested = false; -function selfTestEmulator(): void { - if (emulatorSelfTested) return; - emulatorSelfTested = true; - try { - const { TerminalEmulator } = require("./terminal-emulator") as typeof import("./terminal-emulator"); - const emulator = new TerminalEmulator(8, 2); - emulator.write(Uint8Array.from([0x68, 0x69, 0x1b, 0x5b, 0x31, 0x6d, 0x21]), () => { - console.log(`terminal emulator self-test: ${JSON.stringify(emulator.visibleLines())}`); - }); - } catch (error) { - console.error("terminal emulator failed to load:", error); - } -} - -function createTerminalSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Terminal > Settings", - [ - textSettingMenuItem(terminalHostSetting), - textSettingMenuItem(terminalPortSetting), - textSettingMenuItem(terminalAuthTokenSetting), - ], - TOP_LEFT_MENU_LAYOUT, - ); -} - -function sessionLabel(session: G2MirrorSession): string { - const hint = session.cwdHint.replace(/^_+/, "").replace(/_+/g, "/"); - return `${hint || "session"} (pid ${session.pid})`; -} diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 6f718a9e..103dfa25 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -9,7 +9,6 @@ import { getDefaultSmallFont } from "~/graphics/bdffont"; import { wrapText } from "~/graphics/textwrap"; import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, MenuLayer } from "./menu"; import { DashboardInputEvent, Layer, type LayerContext } from "./layers"; -import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "~/graphics/image"; import { GrayImage } from "~/graphics/image"; export type DashboardSlotId = "bottom-left" | "bottom-right"; @@ -504,17 +503,20 @@ function truncateSetting(value: string, maxLength = 22): string { export class EditTextSettingLayer implements Layer { constructor(private readonly setting: ConfigSettingString) {} - paint(): GrayImage { + paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52); + // Sized to the hosting stack (full lens in the dashboard window, app + // viewport in the settings app). + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + image.drawRect(12, 12, width - 24, height - 24, 52); image.drawText(font, 22, 16, this.setting.glassesEditTitle, 220); - const message = wrapText(font, "Look at the phone app to type a value.", G2_LENS_WIDTH - 48); + const message = wrapText(font, "Look at the phone app to type a value.", width - 48); for (let index = 0; index < message.length; index++) { image.drawText(font, 22, 52 + index * 14, message[index]!, 200); } image.drawText(font, 22, 110, truncateSetting(this.setting.get(), 52), 220); - image.drawText(font, 22, 252, "Double-click to go back", 110); + image.drawText(font, 22, height - 36, "Double-click to go back", 110); return image; } diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index 697a7f63..648643bd 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -3,8 +3,7 @@ import { mediaControllerBridge } from "../native/media-controller"; import { nightscoutBridge } from "../native/nightscout-bridge"; import { getDashboardPlugin, type DashboardPluginCardBounds, type DashboardPluginState } from "./dashboard-plugins"; import { EditTextSettingLayer, isNightscoutSettingsConfigured, type DashboardSlotId, DashboardPluginId, bottomRightSlotSetting, bottomLeftSlotSetting, dashboardSlotIds } from "./dashboard-settings"; -import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers"; -import { SingleNotificationLayer } from "./notifications"; +import { Layer, LayerActions, LayerStack, type DashboardInputEvent } from "./layers"; import { TelepromptLayer } from "./apps/teleprompt"; import { drawSystemCard } from "./dashboard/system-card"; import { createRootMenuLayer } from "./dashboard/root-menu"; @@ -87,24 +86,6 @@ export function openTelepromptDocument(text?: string): void { dashboardLayers.push(new TelepromptLayer(dashboardState.telepromptDocumentText)); } -export function openAndroidNotificationFromSleep(notificationKey: string, nowMs = Date.now()): boolean { - if (!notificationKey || shell.isScreenOn()) return false; - - shell.wake("window", nowMs); - dashboardLayers.clearToBase(); - dashboardLayers.push(createRootMenuLayer()); - dashboardLayers.push(new SingleNotificationLayer(notificationKey, { - origin: "new-notification-trigger", - closeNewNotificationTrigger: closeNewNotificationTrigger, - })); - return true; -} - -function closeNewNotificationTrigger(ctx: LayerContext): void { - shell.sleep(); - ctx.stack.clearToBase(); -} - export function resetDashboardSleepTimerAndWake(nowMs = Date.now()): boolean { if (!shell.wake("sidebar", nowMs)) return false; dashboardLayers.clearToBase(); diff --git a/app/ui/dashboard/about-page.ts b/app/ui/dashboard/about-page.ts index ca0dbdf6..4559f800 100644 --- a/app/ui/dashboard/about-page.ts +++ b/app/ui/dashboard/about-page.ts @@ -1,16 +1,18 @@ import { getDefaultSmallFont } from "~/graphics/bdffont"; -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "~/graphics/image"; +import { GrayImage } from "~/graphics/image"; import { getDashboardLogo } from "~/graphics/logo"; -import { DashboardInputEvent, Layer, LayerStack } from "../layers"; +import { DashboardInputEvent, Layer, LayerContext, LayerStack } from "../layers"; export class AboutPage implements Layer { - paint(): GrayImage { - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + paint(ctx: LayerContext): GrayImage { + // Sized to the hosting stack (the Settings app viewport). + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); const logo = getDashboardLogo(); const dashboardFont = getDefaultSmallFont(); image.drawText(dashboardFont, 22, 16, "About Faceclaw", 220); if (logo) { - image.bitBlt(logo, 22, 42); + image.bitBlt(logo, 22, 42, { transparentZero: true }); } image.drawText(dashboardFont, 108, 48, "Faceclaw", 220); image.drawText(dashboardFont, 108, 64, "Dashboard prototype", 180); @@ -18,7 +20,7 @@ export class AboutPage implements Layer { image.drawTextWrapped({ font: dashboardFont, x: 22, y: 128, - width: G2_LENS_WIDTH - 44, + width: width - 44, text: "By James Babcock. Distributed under the GNU General Public License, version 3. Version 0.1.0. Too much of an early janky development prototype to have proper numbered releases.", value: 180 }); diff --git a/app/ui/dashboard/root-menu.ts b/app/ui/dashboard/root-menu.ts index 8af97456..ae69d8ba 100644 --- a/app/ui/dashboard/root-menu.ts +++ b/app/ui/dashboard/root-menu.ts @@ -4,14 +4,11 @@ import { getDashboardPlugin, isBlankDashboardPlugin } from "../dashboard-plugins import { DashboardSlotId, dashboardSlotIds, isNightscoutSettingsConfigured } from "../dashboard-settings"; import { DashboardInputEvent, LayerContext } from "../layers"; import { MenuLayer } from "../menu"; -import { NotificationsListLayer } from "../notifications"; -import { createNightscoutSettingsMenuLayer, createSettingsMenuLayer } from "./settings-menus"; +import { createNightscoutSettingsMenuLayer } from "./settings-menus"; import { TelepromptLayer } from "../apps/teleprompt"; -import { TerminalAppLayer } from "../apps/terminal"; import { TranscribeLayer } from "../apps/transcribe"; import { ScreenTestLayer } from "../apps/screen-test"; import { BuzzerDemoLayer } from "../apps/buzzer-demo"; -import { AboutPage } from "./about-page"; import { GrayImage } from "~/graphics/image"; import { shell } from "../shell/shell"; @@ -38,21 +35,6 @@ export function createRootMenuLayer(): MenuLayer { ctx.stack.push(createAppsMenuLayer()); }, }, - { - label: "Notifications", - onSelect: (ctx) => { - ctx.stack.push(new NotificationsListLayer((ctx) => { - ctx.stack.clearToBase(); - ctx.stack.push(createRootMenuLayer()); - })); - }, - }, - { - label: "Settings", - onSelect: (ctx) => { - ctx.stack.push(createSettingsMenuLayer()); - }, - }, ...dashboardSlotIds .filter((slot) => !isBlankDashboardPlugin(getPluginIdForSlot(slot))) .map((slot) => ({ @@ -64,12 +46,6 @@ export function createRootMenuLayer(): MenuLayer { image.drawText(rootMenuFont, x, y + 3, pluginLabelForSlot(slot), 200); }, })), - { - label: "System", - onSelect: (ctx) => { - ctx.stack.push(createSystemMenuLayer()); - }, - }, ], TOP_LEFT_MENU_LAYOUT, ); @@ -93,12 +69,6 @@ function createAppsMenuLayer(): MenuLayer { layer.start(ctx); }, }, - { - label: "Terminal", - onSelect: (ctx) => { - ctx.stack.push(new TerminalAppLayer(ctx.actions)); - }, - }, { label: "Screen test", onSelect: (ctx) => { @@ -116,28 +86,6 @@ function createAppsMenuLayer(): MenuLayer { ); } -function createSystemMenuLayer(): MenuLayer { - return new MenuLayer( - "System", - [ - { - label: "About", - onSelect: (ctx) => { - ctx.stack.push(new AboutPage()); - }, - }, - { - label: "Quit / Disconnect", - onSelect: async (ctx) => { - ctx.stack.clearToBase(); - await ctx.actions.disconnect(); - }, - }, - ], - TOP_LEFT_MENU_LAYOUT, - ); -} - function openDashboardSlot(slot: DashboardSlotId, ctx: LayerContext): void { const pluginId = getPluginIdForSlot(slot); if (isBlankDashboardPlugin(pluginId)) { diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index a803715a..189dd1b4 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -1,7 +1,13 @@ -import { TOP_LEFT_MENU_LAYOUT } from "../dashboard"; +import { APP_VIEWPORT } from "../shell/geometry"; +import { type MenuLayout } from "../menu"; + +// Settings menus render in both the dashboard window (full lens) and the +// Settings app window (viewport-sized); cap heights to the smaller of the two. +const SETTINGS_MENU_LAYOUT: MenuLayout = { x: 8, y: 8, width: 272, maxHeight: APP_VIEWPORT.height - 16 }; import { shell } from "../shell/shell"; -import { batteryDisplayModeSetting, dashboardSlotIds, elevenLabsApiKeySetting, enumSettingMenuItem, firmwareDebugFlagsSetting, rawScreenshotsEnabledSetting, saveVoiceRecordingsSetting, showSignalStrengthSetting, showAndroidNotificationsSetting, showBatteryIndicatorsSetting, showFaceclawLogoSetting, systemCardNameSetting, textSettingMenuItem, toggleSettingMenuItem, voiceProviderSetting, dashboardSlotSettings, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, screenTimeoutSetting } from "../dashboard-settings"; +import { batteryDisplayModeSetting, dashboardSlotIds, elevenLabsApiKeySetting, enumSettingMenuItem, firmwareDebugFlagsSetting, rawScreenshotsEnabledSetting, saveVoiceRecordingsSetting, showSignalStrengthSetting, showAndroidNotificationsSetting, showBatteryIndicatorsSetting, showFaceclawLogoSetting, systemCardNameSetting, terminalAuthTokenSetting, terminalHostSetting, terminalPortSetting, textSettingMenuItem, toggleSettingMenuItem, voiceProviderSetting, dashboardSlotSettings, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, screenTimeoutSetting } from "../dashboard-settings"; import { MenuLayer } from "../menu"; +import { AboutPage } from "./about-page"; export function createSettingsMenuLayer(): MenuLayer { return new MenuLayer( @@ -31,14 +37,45 @@ export function createSettingsMenuLayer(): MenuLayer { ctx.stack.push(createIntegrationsMenuLayer()); }, }, + { + label: "Terminal", + onSelect: (ctx) => { + ctx.stack.push(createTerminalSettingsMenuLayer()); + }, + }, { label: "Developer", onSelect: (ctx) => { ctx.stack.push(createDeveloperSettingsMenuLayer()); }, }, + { + label: "About", + onSelect: (ctx) => { + ctx.stack.push(new AboutPage()); + }, + }, + { + label: "Quit / Disconnect", + onSelect: async (ctx) => { + ctx.stack.clearToBase(); + await ctx.actions.disconnect(); + }, + }, ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, + ); +} + +function createTerminalSettingsMenuLayer(): MenuLayer { + return new MenuLayer( + "Settings > Terminal", + [ + textSettingMenuItem(terminalHostSetting), + textSettingMenuItem(terminalPortSetting), + textSettingMenuItem(terminalAuthTokenSetting), + ], + SETTINGS_MENU_LAYOUT, ); } @@ -50,7 +87,7 @@ function createDeveloperSettingsMenuLayer(): MenuLayer { toggleSettingMenuItem(saveVoiceRecordingsSetting), toggleSettingMenuItem(firmwareDebugFlagsSetting), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -64,7 +101,7 @@ function createDisplaySettingsMenuLayer(): MenuLayer { }, }), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -75,7 +112,7 @@ function createVoiceSettingsMenuLayer(): MenuLayer { enumSettingMenuItem(voiceProviderSetting), textSettingMenuItem(elevenLabsApiKeySetting), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -93,7 +130,7 @@ function createDashboardSettingsMenuLayer(): MenuLayer { style: "submenu", })), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -108,7 +145,7 @@ function createSystemCardSettingsMenuLayer(): MenuLayer { toggleSettingMenuItem(showAndroidNotificationsSetting), toggleSettingMenuItem(showSignalStrengthSetting), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -123,7 +160,7 @@ function createIntegrationsMenuLayer(): MenuLayer { }, }, ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } @@ -134,6 +171,6 @@ export function createNightscoutSettingsMenuLayer(): MenuLayer { textSettingMenuItem(nightscoutSiteUrlSetting), textSettingMenuItem(nightscoutApiTokenSetting), ], - TOP_LEFT_MENU_LAYOUT, + SETTINGS_MENU_LAYOUT, ); } \ No newline at end of file diff --git a/app/ui/layers.ts b/app/ui/layers.ts index 6069328b..123f01b5 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -75,6 +75,11 @@ export class LayerStack { } } + /** Whether the top layer matches (without popping it). */ + topMatches(predicate: (layer: Layer) => boolean): boolean { + return this.layers.length > 1 && predicate(this.layers[this.layers.length - 1]!); + } + /** Pop the top layer only if it matches; returns whether a layer was popped. */ popIfTop(predicate: (layer: Layer) => boolean): boolean { if (this.layers.length > 1 && predicate(this.layers[this.layers.length - 1]!)) { @@ -94,6 +99,10 @@ export class LayerStack { return this.layers.length === 1; } + getBaseSize(): { width: number; height: number } { + return { width: this.baseWidth, height: this.baseHeight }; + } + setActions(actions: Partial): void { Object.assign(this.ctx.actions, actions); } diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index fd0cd44e..9af8340c 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -1,7 +1,7 @@ import { clamp } from "~/util/numeric-util"; import { formatRelativeTime } from "~/util/date-util"; import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont"; -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; +import { GrayImage } from "../graphics/image"; import { wrapText } from "../graphics/textwrap"; import { dismissNotification, @@ -17,16 +17,16 @@ import { type DashboardInputEvent, type Layer, type LayerContext, type PaintBelo const PAGE_X = 12; const PAGE_Y = 12; const LIST_TOP = 38; -const LIST_BOTTOM = G2_LENS_HEIGHT; const CARD_X = 20; -const CARD_WIDTH = G2_LENS_WIDTH - 40; const ICON_SIZE = 24; const ICON_TEXT_GAP = 8; const CARD_TEXT_X = 10 + ICON_SIZE + ICON_TEXT_GAP; -const CARD_TEXT_WIDTH = CARD_WIDTH - CARD_TEXT_X - 14; const LINE_HEIGHT = 14; const CARD_GAP = 6; const MAX_NOTIFICATIONS = 50; +// Right-hand action menu of the detail view. +const DETAIL_MENU_WIDTH = 148; +const DETAIL_CONTENT_X = 24; /** Icon for a paint pass: allow-stale, reporting staleness to the render loop. */ function iconForNotification(key: string): GrayImage | null { @@ -48,48 +48,56 @@ type DetailMenuItem = | { kind: "action"; label: string; action: AndroidNotificationAction } | { kind: "dismiss"; label: string }; -export type SingleNotificationLayerOrigin = "notifications-dashboard" | "new-notification-trigger"; +export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal"; type SingleNotificationLayerOptions = { origin: SingleNotificationLayerOrigin; - closeNewNotificationTrigger?: (ctx: LayerContext) => void; + /** Close hook for the modal origin (the layer is the modal stack's base, so pop() cannot close it). */ + closeModal?: (ctx: LayerContext) => void; }; +/** + * Scrollable list of active Android notifications. Sized to its hosting + * stack (the Notifications app viewport). Selecting one pushes the detail + * view. + */ export class NotificationsListLayer implements Layer { private selectedKey = ""; - constructor(private readonly exitToRootMenu?: (ctx: LayerContext) => void) {} - paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const cardWidth = width - 2 * CARD_X; + const cardTextWidth = cardWidth - CARD_TEXT_X - 14; const notifications = readActiveNotifications(MAX_NOTIFICATIONS); const selectedIndex = this.resolveSelectedIndex(notifications); const layouts = notifications.map((notification, index) => - buildNotificationCardLayout(font, notification, index === selectedIndex), + buildNotificationCardLayout(font, notification, index === selectedIndex, cardTextWidth), ); image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notifications", 220); - image.drawText(font, G2_LENS_WIDTH - 96, PAGE_Y + 9, `${selectedIndex+1}/${notifications.length}`, 150); + image.drawText(font, width - 96, PAGE_Y + 9, `${selectedIndex + 1}/${notifications.length}`, 150); if (!notifications.length) { image.drawText(font, 24, 72, "No current Android notifications.", 190); - image.drawText(font, 24, 252, "Double-click to return to dashboard", 110); + image.drawText(font, 24, height - 36, "Double-click: back to sidebar", 110); return image; } - const scrollY = scrollForSelected(layouts, selectedIndex, LIST_BOTTOM - LIST_TOP); + const listBottom = height; + const scrollY = scrollForSelected(layouts, selectedIndex, listBottom - LIST_TOP); let cursorY = LIST_TOP - scrollY; for (let index = 0; index < layouts.length; index++) { const layout = layouts[index]!; - if (cursorY + layout.height >= LIST_TOP && cursorY <= LIST_BOTTOM) { + if (cursorY + layout.height >= LIST_TOP && cursorY <= listBottom) { // Icons are only resolved for cards actually drawn, so a long list // does not fetch icons for everything below the fold. const icon = iconForNotification(layout.notification.key); - drawNotificationCard(image, font, layout, CARD_X, cursorY, CARD_WIDTH, index === selectedIndex, icon); + drawNotificationCard(image, font, layout, CARD_X, cursorY, cardWidth, index === selectedIndex, icon); } cursorY += layout.height + CARD_GAP; - if (cursorY > LIST_BOTTOM + 80) break; + if (cursorY > listBottom + 80) break; } return image; @@ -99,11 +107,9 @@ export class NotificationsListLayer implements Layer { const notifications = readActiveNotifications(MAX_NOTIFICATIONS); const selectedIndex = this.resolveSelectedIndex(notifications); if (event.type === "double-click") { - if (this.exitToRootMenu) { - this.exitToRootMenu(ctx); - } else { - ctx.stack.clearToBase(); - } + // At the app's root this is intercepted by the yield wrapper; reached + // only if hosted somewhere deeper, where popping is right. + ctx.stack.pop(); return; } if (!notifications.length) return; @@ -118,7 +124,7 @@ export class NotificationsListLayer implements Layer { } if (event.type === "click") { ctx.stack.push(new SingleNotificationLayer(notifications[selectedIndex]!.key, { - origin: "notifications-dashboard", + origin: "notifications-list", })); } } @@ -137,6 +143,11 @@ export class NotificationsListLayer implements Layer { } } +/** + * One notification's full content plus its action menu. Sized to its hosting + * stack: the Notifications app viewport, or the interior of the shell's + * new-notification modal. + */ export class SingleNotificationLayer implements Layer { private selectedMenuIndex = 0; @@ -147,7 +158,8 @@ export class SingleNotificationLayer implements Layer { paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { const font = getDefaultSmallFont(); - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); const notification = readActiveNotifications(MAX_NOTIFICATIONS).find((item) => item.key === this.notificationKey); if (!notification) { @@ -156,8 +168,8 @@ export class SingleNotificationLayer implements Layer { const menu = buildDetailMenu(notification); this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1)); - drawDetailContent(image, font, notification, iconForNotification(notification.key)); - drawDetailMenu(image, font, menu, this.selectedMenuIndex); + drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height); + drawDetailMenu(image, font, menu, this.selectedMenuIndex, width); return image; } @@ -170,7 +182,7 @@ export class SingleNotificationLayer implements Layer { const menu = buildDetailMenu(notification); if (event.type === "double-click") { - ctx.stack.pop(); + this.close(ctx); return; } if (event.type === "scroll-up") { @@ -185,7 +197,7 @@ export class SingleNotificationLayer implements Layer { const item = menu[this.selectedMenuIndex]!; if (item.kind === "back") { - ctx.stack.pop(); + this.close(ctx); } else if (item.kind === "action") { invokeNotificationAction(this.notificationKey, item.action.index); if (!readActiveNotifications(MAX_NOTIFICATIONS).some((item) => item.key === this.notificationKey)) { @@ -197,37 +209,47 @@ export class SingleNotificationLayer implements Layer { } } - private closeUnavailableNotification(ctx: LayerContext, paintBelow?: PaintBelow): GrayImage { - if (this.options.origin === "new-notification-trigger") { - this.options.closeNewNotificationTrigger?.(ctx); - return new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + /** Leave the detail view, whatever hosts it. */ + private close(ctx: LayerContext): void { + if (this.options.origin === "new-notification-modal") { + this.options.closeModal?.(ctx); + } else { + ctx.stack.pop(); } + } - ctx.stack.pop(); - return paintBelow ? paintBelow() : new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + private closeUnavailableNotification(ctx: LayerContext, paintBelow?: PaintBelow): GrayImage { + this.close(ctx); + const { width, height } = ctx.stack.getBaseSize(); + return paintBelow ? paintBelow() : new GrayImage(width, height, 0); } } -function buildNotificationCardLayout(font: BdfFont, notification: AndroidNotification, selected: boolean): CardLayout { +function buildNotificationCardLayout( + font: BdfFont, + notification: AndroidNotification, + selected: boolean, + cardTextWidth: number, +): CardLayout { const lines: string[] = []; const time = formatRelativeTime(notification.postTime); const title = notificationTitle(notification); const timeSuffix = time ? ` ${time}` : ""; - lines.push(truncateToWidth(font, `${title}${timeSuffix}`, CARD_TEXT_WIDTH)); + lines.push(truncateToWidth(font, `${title}${timeSuffix}`, cardTextWidth)); const appName = notification.appName || notification.packageName; const body = primaryNotificationBody(notification); if (selected) { if (appName && appName !== title) { - lines.push(truncateToWidth(font, appName, CARD_TEXT_WIDTH)); + lines.push(truncateToWidth(font, appName, cardTextWidth)); } if (body) { - lines.push(...wrapText(font, body, CARD_TEXT_WIDTH).slice(0, 4)); + lines.push(...wrapText(font, body, cardTextWidth).slice(0, 4)); } } else if (body) { - lines.push(truncateToWidth(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH)); + lines.push(truncateToWidth(font, wrapText(font, body, cardTextWidth)[0]!, cardTextWidth)); } if (notification.actions.length) { lines.push(`${notification.actions.length} quick action${notification.actions.length === 1 ? "" : "s"}`); @@ -249,12 +271,12 @@ function drawNotificationCard( selected: boolean, icon: GrayImage | null, ): void { - const fill = selected ? 15 : 0; + const fill = selected ? 15 : 1; const stroke = selected ? 110 : 38; image.fillRoundedRect(x, y, width, layout.height, fill, 8); image.drawRoundedRect(x, y, width, layout.height, stroke, 8); if (icon) { - image.bitBlt(icon, x + 10, y + 8); + image.bitBlt(icon, x + 10, y + 8, { transparentZero: true }); } for (let index = 0; index < layout.lines.length; index++) { const value = index === 0 ? 140 : selected ? 235 : 185; @@ -275,13 +297,21 @@ function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewpor return clamp(centered | 0, 0, maxScroll); } -function drawDetailContent(image: GrayImage, font: BdfFont, notification: AndroidNotification, icon: GrayImage | null): void { - const contentX = 24; - const contentWidth = 360; +function drawDetailContent( + image: GrayImage, + font: BdfFont, + notification: AndroidNotification, + icon: GrayImage | null, + width: number, + height: number, +): void { + const contentX = DETAIL_CONTENT_X; + const menuX = width - DETAIL_MENU_WIDTH - 24; + const contentWidth = menuX - contentX - 20; image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notification", 220); let appLineX = contentX; if (icon) { - image.bitBlt(icon, contentX, 36); + image.bitBlt(icon, contentX, 36, { transparentZero: true }); appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP; } image.drawText(font, appLineX, 42, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150); @@ -299,27 +329,27 @@ function drawDetailContent(image: GrayImage, font: BdfFont, notification: Androi lines.push(...wrapText(font, meta, contentWidth)); } - for (let index = 0; index < Math.min(lines.length, 14); index++) { + const maxLines = Math.max(1, ((height - 64 - 14) / LINE_HEIGHT) | 0); + for (let index = 0; index < Math.min(lines.length, maxLines); index++) { const line = lines[index]!; image.drawText(font, contentX, 64 + index * LINE_HEIGHT, line, index === 0 ? 230 : 190); } - if (lines.length > 14) { - image.drawText(font, contentX, 260, "...", 140); + if (lines.length > maxLines) { + image.drawText(font, contentX, height - 24, "...", 140); } } -function drawDetailMenu(image: GrayImage, font: BdfFont, menu: DetailMenuItem[], selectedIndex: number): void { - const menuX = 404; +function drawDetailMenu(image: GrayImage, font: BdfFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void { + const menuX = width - DETAIL_MENU_WIDTH - 24; const menuY = 24; - const menuWidth = 148; for (let index = 0; index < menu.length; index++) { const y = menuY + index * 22; const selected = index === selectedIndex; if (selected) { - image.fillRoundedRect(menuX - 8, y - 2, menuWidth, 19, 18, 6); - image.drawRoundedRect(menuX - 8, y - 2, menuWidth, 19, 60, 6); + image.fillRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, 19, 18, 6); + image.drawRoundedRect(menuX - 8, y - 2, DETAIL_MENU_WIDTH, 19, 60, 6); } - const label = truncateToWidth(font, menu[index]!.label, menuWidth - 12); + const label = truncateToWidth(font, menu[index]!.label, DETAIL_MENU_WIDTH - 12); image.drawText(font, menuX, y + 2, label, selected ? 255 : 185); } } diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index dc0b7dcf..db20986b 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -11,6 +11,8 @@ const ICON_MARGIN_X = ((SIDEBAR_WIDTH - ICON_SIZE) / 2) | 0; const ICON_SPACING = 8; const NOTIFICATION_ICON_SIZE = 24; const BORDER_VALUE = 40; +const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; export type ShellChromeWindow = { windowId: string; @@ -82,7 +84,8 @@ export class ShellChromeLayer implements Layer { image.drawLine(SIDEBAR_WIDTH, TOP_BAR_HEIGHT - 1, G2_LENS_WIDTH - 1, TOP_BAR_HEIGHT - 1, BORDER_VALUE); const now = new Date(); - const clock = `${now.getHours()}:${String(now.getMinutes()).padStart(2, "0")}`; + const clock = `${WEEKDAYS[now.getDay()]} ${now.getDate()} ${MONTHS[now.getMonth()]} ` + + `${now.getHours()}:${String(now.getMinutes()).padStart(2, "0")}`; const clockX = SIDEBAR_WIDTH + 10; const textY = Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0); image.drawText(font, clockX, textY, clock, 210); diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts new file mode 100644 index 00000000..27a04b64 --- /dev/null +++ b/app/ui/shell/in-process-window.ts @@ -0,0 +1,117 @@ +import { GrayImage } from "../../graphics/image"; +import { beginRenderPass, endRenderPass } from "../../util/render-freshness"; +import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, PaintBelow } from "../layers"; +import { makeLetterWindowIcon } from "./chrome-layer"; +import { APP_VIEWPORT } from "./geometry"; +import { shell, type ShellWindow } from "./shell"; + +/** + * A window whose app logic runs on the main thread (launcher, settings): + * hosts a viewport-sized LayerStack, renders it on input or request, and + * submits frames through a controller-provided submitter. Java-side dedup + * finishes no-change frames, so unconditional resubmits keep frame ownership + * simple. + */ +export type InProcessWindowOptions = { + appId: string; + windowId: string; + title: string; + iconLetter: string; + closeable: boolean; + /** Shared actions; requestRender is rebound to this window's render. */ + actions: LayerActions; + baseLayer: Layer; + submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise; + setSurfaceVisible: (visible: boolean) => void; + removeSurface?: () => void; + onClosed?: () => void; +}; + +export type InProcessWindow = { + window: ShellWindow; + stack: LayerStack; + requestRender: () => void; +}; + +export function createInProcessWindow(options: InProcessWindowOptions): InProcessWindow { + const requestRender = () => { + void render(0).catch((error) => { + console.error(`${options.windowId} render failed: ${error}`); + }); + }; + const stack = new LayerStack( + options.baseLayer, + { ...options.actions, requestRender }, + { width: APP_VIEWPORT.width, height: APP_VIEWPORT.height }, + ); + + // Data sources with caches (e.g. notification icons) may serve stale data + // to keep the paint fast; when they do, schedule one follow-up render that + // requires fresh data (same contract as the dashboard render loop). + let nextRenderWantsFreshData = false; + + async function render(frameId: number): Promise { + const wantFreshData = nextRenderWantsFreshData; + nextRenderWantsFreshData = false; + beginRenderPass(!wantFreshData); + const paintStartedAtMs = Date.now(); + const image = stack.paint(); + const paintUsedStaleData = endRenderPass(); + await options.submitFrame(image, Date.now() - paintStartedAtMs, frameId); + if (paintUsedStaleData) { + nextRenderWantsFreshData = true; + requestRender(); + } + } + + const window: ShellWindow = { + appId: options.appId, + windowId: options.windowId, + title: options.title, + surfaceId: `window:${options.windowId}`, + closeable: options.closeable, + close: () => { + options.onClosed?.(); + options.removeSurface?.(); + }, + drawIcon: makeLetterWindowIcon(options.iconLetter), + handleInput: async (event, frameId) => { + await stack.handleInput(event); + await render(frameId); + }, + requestRender, + setForeground: (foreground) => { + options.setSurfaceVisible(foreground); + }, + }; + return { window, stack, requestRender }; +} + +/** + * Wrap an app's root layer so double-click at the root yields focus to the + * shell sidebar (the standard leave-the-app gesture) instead of being + * swallowed by the layer's own back handling. + */ +export class YieldAtRootLayer implements Layer { + constructor(private readonly inner: Layer) {} + + get paintOverBase(): boolean | undefined { + return this.inner.paintOverBase; + } + + paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + return this.inner.paint(ctx, paintBelow); + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + if (event.type === "double-click") { + shell.yieldFocusToSidebar(); + return; + } + await this.inner.handleInput(event, ctx); + } + + onRemoved(): void { + this.inner.onRemoved?.(); + } +} diff --git a/app/ui/shell/launcher-app.ts b/app/ui/shell/launcher-app.ts index e84d298d..1c6752ef 100644 --- a/app/ui/shell/launcher-app.ts +++ b/app/ui/shell/launcher-app.ts @@ -1,9 +1,9 @@ import { GrayImage } from "../../graphics/image"; -import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack } from "../layers"; +import { LayerActions } from "../layers"; import { MenuLayer } from "../menu"; -import { makeLetterWindowIcon } from "./chrome-layer"; import { APP_VIEWPORT } from "./geometry"; -import { shell, type ShellWindow } from "./shell"; +import { createInProcessWindow, YieldAtRootLayer } from "./in-process-window"; +import { type ShellWindow } from "./shell"; export type LauncherAppEntry = { appId: string; @@ -23,24 +23,13 @@ export type LauncherOptions = { export const LAUNCHER_WINDOW_ID = "launcher"; export const LAUNCHER_SURFACE_ID = "window:launcher"; -/** The launcher's root menu; backing out of it yields to the sidebar. */ -class LauncherMenuLayer extends MenuLayer { - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { - if (event.type === "double-click") { - shell.yieldFocusToSidebar(); - return; - } - await super.handleInput(event, ctx); - } -} - /** * The launcher: a pinned, uncloseable in-process window listing launchable * apps. Selecting an app asks the controller to launch it (new window, * possibly in an existing app worker) and foregrounds the new window. */ export function createLauncherWindow(options: LauncherOptions): ShellWindow { - const baseLayer: Layer = new LauncherMenuLayer( + const menu = new MenuLayer( "Apps", options.apps.map((app) => ({ label: app.label, @@ -56,37 +45,15 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow { maxHeight: APP_VIEWPORT.height - 16, }, ); - const stack = new LayerStack(baseLayer, options.actions, { - width: APP_VIEWPORT.width, - height: APP_VIEWPORT.height, - }); - - async function render(frameId: number): Promise { - const paintStartedAtMs = Date.now(); - const image = stack.paint(); - await options.submitFrame(image, Date.now() - paintStartedAtMs, frameId); - } - - return { + return createInProcessWindow({ appId: "launcher", windowId: LAUNCHER_WINDOW_ID, title: "Apps", - surfaceId: LAUNCHER_SURFACE_ID, + iconLetter: "A", closeable: false, - drawIcon: makeLetterWindowIcon("A"), - handleInput: async (event, frameId) => { - await stack.handleInput(event); - // Java-side dedup finishes no-change frames, so an unconditional - // resubmit keeps frame ownership simple. - await render(frameId); - }, - requestRender: () => { - void render(0).catch((error) => { - console.error(`launcher render failed: ${error}`); - }); - }, - setForeground: (foreground) => { - options.setSurfaceVisible(foreground); - }, - }; + actions: options.actions, + baseLayer: new YieldAtRootLayer(menu), + submitFrame: options.submitFrame, + setSurfaceVisible: options.setSurfaceVisible, + }).window; } diff --git a/app/ui/shell/modal-layer.ts b/app/ui/shell/modal-layer.ts new file mode 100644 index 00000000..ad02dcfe --- /dev/null +++ b/app/ui/shell/modal-layer.ts @@ -0,0 +1,49 @@ +import { GrayImage } from "../../graphics/image"; +import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, PaintBelow } from "../layers"; +import { APP_VIEWPORT, SHELL_OPAQUE_BLACK } from "./geometry"; + +// The modal covers most of the app viewport, leaving a little of the +// foreground app visible around the edges. +const MODAL_MARGIN = 14; +const MODAL_PADDING = 4; +export const MODAL_RECT = { + x: APP_VIEWPORT.x + MODAL_MARGIN, + y: APP_VIEWPORT.y + MODAL_MARGIN, + width: APP_VIEWPORT.width - 2 * MODAL_MARGIN, + height: APP_VIEWPORT.height - 2 * MODAL_MARGIN, +} as const; + +export const MODAL_INTERIOR = { + width: MODAL_RECT.width - 2 * MODAL_PADDING, + height: MODAL_RECT.height - 2 * MODAL_PADDING, +} as const; + +/** + * A shell overlay hosting an inner layer stack in a bordered box over the + * app viewport (used for new-notification popups). The inner stack paints at + * the modal's interior size; its pixels blit onto an opaque black backdrop, + * so inner value-0 pixels read as black, not transparent. + */ +export class ShellModalLayer implements Layer { + private readonly stack: LayerStack; + + constructor(baseLayer: Layer, actions: LayerActions) { + this.stack = new LayerStack(baseLayer, actions, { + width: MODAL_INTERIOR.width, + height: MODAL_INTERIOR.height, + }); + } + + paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + const image = paintBelow(); + const inner = this.stack.paint(); + image.fillRoundedRect(MODAL_RECT.x, MODAL_RECT.y, MODAL_RECT.width, MODAL_RECT.height, SHELL_OPAQUE_BLACK, 8); + image.drawRoundedRect(MODAL_RECT.x, MODAL_RECT.y, MODAL_RECT.width, MODAL_RECT.height, 110, 8); + image.bitBlt(inner, MODAL_RECT.x + MODAL_PADDING, MODAL_RECT.y + MODAL_PADDING, { transparentZero: true }); + return image; + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + await this.stack.handleInput(event); + } +} diff --git a/app/ui/shell/notifications-app.ts b/app/ui/shell/notifications-app.ts new file mode 100644 index 00000000..654104cb --- /dev/null +++ b/app/ui/shell/notifications-app.ts @@ -0,0 +1,36 @@ +import { GrayImage } from "../../graphics/image"; +import { LayerActions } from "../layers"; +import { NotificationsListLayer } from "../notifications"; +import { createInProcessWindow, YieldAtRootLayer, type InProcessWindow } from "./in-process-window"; + +export const NOTIFICATIONS_WINDOW_ID = "notifications"; +export const NOTIFICATIONS_SURFACE_ID = "window:notifications"; + +export type NotificationsAppOptions = { + actions: LayerActions; + submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise; + setSurfaceVisible: (visible: boolean) => void; + removeSurface: () => void; + onClosed: () => void; +}; + +/** + * The Notifications app: the Android-notification list (previously a + * dashboard page) in its own in-process window; selecting a notification + * opens the detail view with its quick actions. + */ +export function createNotificationsAppWindow(options: NotificationsAppOptions): InProcessWindow { + return createInProcessWindow({ + appId: "notifications", + windowId: NOTIFICATIONS_WINDOW_ID, + title: "Notifications", + iconLetter: "N", + closeable: true, + actions: options.actions, + baseLayer: new YieldAtRootLayer(new NotificationsListLayer()), + submitFrame: options.submitFrame, + setSurfaceVisible: options.setSurfaceVisible, + removeSurface: options.removeSurface, + onClosed: options.onClosed, + }); +} diff --git a/app/ui/shell/settings-app.ts b/app/ui/shell/settings-app.ts new file mode 100644 index 00000000..e76db83e --- /dev/null +++ b/app/ui/shell/settings-app.ts @@ -0,0 +1,54 @@ +import { GrayImage } from "../../graphics/image"; +import { LayerActions } from "../layers"; +import { EditTextSettingLayer } from "../dashboard-settings"; +import { createSettingsMenuLayer } from "../dashboard/settings-menus"; +import { createInProcessWindow, YieldAtRootLayer } from "./in-process-window"; +import { type ShellWindow } from "./shell"; + +export const SETTINGS_WINDOW_ID = "settings"; +export const SETTINGS_SURFACE_ID = "window:settings"; + +export type SettingsAppOptions = { + actions: LayerActions; + submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise; + setSurfaceVisible: (visible: boolean) => void; + removeSurface: () => void; + onClosed: () => void; +}; + +export type SettingsAppWindow = { + window: ShellWindow; + requestRender: () => void; + /** Whether the glasses-side text-setting editor is the top layer. */ + isTextEditorOnTop: () => boolean; + /** Pop the text-setting editor if it is on top; returns whether it was. */ + closeTextEditor: () => boolean; +}; + +/** + * The Settings app: the settings menu tree (previously a dashboard submenu) + * hosted in its own in-process window. In-process because the text-setting + * editor is synchronized with the phone-side TextField through the + * controller, which lives on the main thread. + */ +export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAppWindow { + const { window, stack, requestRender } = createInProcessWindow({ + appId: "settings", + windowId: SETTINGS_WINDOW_ID, + title: "Settings", + iconLetter: "Se", + closeable: true, + actions: options.actions, + baseLayer: new YieldAtRootLayer(createSettingsMenuLayer()), + submitFrame: options.submitFrame, + setSurfaceVisible: options.setSurfaceVisible, + removeSurface: options.removeSurface, + onClosed: options.onClosed, + }); + return { + window, + requestRender, + isTextEditorOnTop: () => stack.topMatches((layer) => layer instanceof EditTextSettingLayer), + closeTextEditor: () => stack.popIfTop((layer) => layer instanceof EditTextSettingLayer), + }; +} diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index e97750bf..68a52028 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -4,7 +4,9 @@ import type { RawInputEvent } from "../../native/faceclaw-communicator"; import { DashboardInputEvent, LayerActions, LayerStack } from "../layers"; import { MenuLayer, type MenuItem } from "../menu"; import { VoiceInputLayer } from "../apps/voice-input"; +import { SingleNotificationLayer } from "../notifications"; import { ShellChromeLayer, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer"; +import { ShellModalLayer } from "./modal-layer"; import { SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry"; /** @@ -223,6 +225,32 @@ class Shell { return true; } + /** + * Show a new notification in a shell modal over the app viewport. If the + * notification woke the screen, closing the modal goes back to sleep + * (matching the old sleep-popup behavior). + */ + openNotificationModal(notificationKey: string, wokeScreen: boolean): void { + if (!this.screenOn) return; + const modal: ShellModalLayer = new ShellModalLayer( + new SingleNotificationLayer(notificationKey, { + origin: "new-notification-modal", + closeModal: () => this.closeNotificationModal(modal, wokeScreen), + }), + this.config.actions, + ); + this.stack.push(modal); + this.config.requestShellRender(); + } + + private closeNotificationModal(modal: ShellModalLayer, wokeScreen: boolean): void { + this.stack.popIfTop((layer) => layer === modal); + if (wokeScreen) { + this.sleep(); + } + this.config.requestShellRender(); + } + /** Called by a window when the user backs out of its root (double-tap). */ yieldFocusToSidebar(): void { if (this.focus === "sidebar") return; diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index e41fa0c0..cc260c7a 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -15,45 +15,78 @@ export type WorkerAppMessage = | { type: "foreground"; windowId: string; foreground: boolean } | { type: "screen"; on: boolean }; -export type WorkerAppReply = { type: "yield-focus"; windowId: string }; +export type WorkerAppReply = + | { type: "yield-focus"; windowId: string } + | { + /** App-initiated window (e.g. a terminal view opened from the hub list). */ + type: "open-window-request"; + windowId: string; + title: string; + iconLetter: string; + focus?: boolean; + } + | { type: "set-title"; windowId: string; title: string } + | { type: "set-attention"; windowId: string; attention: boolean }; -export type WorkerWindowOptions = { +export type WorkerWindowSpec = { + /** Unique across the shell; namespace with the appId (e.g. "terminal:view:3"). */ windowId: string; title: string; iconLetter: string; - surfaceId: string; + /** Foreground and focus the window once its surface exists. */ + focus?: boolean; +}; + +export type WorkerAppHostOptions = { + appId: string; + worker: Worker; viewport: { width: number; height: number }; - /** Flip the window's compositor surface visibility. */ - setSurfaceVisible: (visible: boolean) => void; - /** Remove the window's compositor surface (on close). */ - removeSurface: () => void; + /** Create/refresh a window surface on the compositor (no-op when disconnected). */ + configureSurface: (surfaceId: string, visible: boolean) => Promise; + setSurfaceVisible: (surfaceId: string, visible: boolean) => void; + removeSurface: (surfaceId: string) => void; + requestShellRender: () => void; }; /** * Owns the Worker for one app and adapts its windows to the shell's window - * interface: forwards input and lifecycle over postMessage, relays - * yield-focus back to the shell, and tells the worker when windows open and - * close. The worker submits frames straight to the Java compositor, so no - * pixels cross this boundary. + * interface: forwards input and lifecycle over postMessage, relays worker + * requests (yield-focus, new windows, attention flags) back to the shell, + * and manages compositor surfaces for the app's windows. The worker submits + * frames straight to the Java compositor, so no pixels cross this boundary. */ export class WorkerAppHost { private readonly openWindows = new Set(); - constructor( - private readonly appId: string, - private readonly worker: Worker, - ) { - worker.onmessage = (event: MessageEvent) => { + constructor(private readonly options: WorkerAppHostOptions) { + options.worker.onmessage = (event: MessageEvent) => { const message = event.data as WorkerAppReply | undefined; - if (message?.type === "yield-focus") { - // Only the focused window's yield is meaningful. - if (shell.foregroundWindow()?.windowId === message.windowId) { - shell.yieldFocusToSidebar(); - } + if (!message) return; + switch (message.type) { + case "yield-focus": + // Only the focused window's yield is meaningful. + if (shell.foregroundWindow()?.windowId === message.windowId) { + shell.yieldFocusToSidebar(); + } + break; + case "open-window-request": + this.openWindow({ + windowId: message.windowId, + title: message.title, + iconLetter: message.iconLetter, + focus: message.focus, + }); + break; + case "set-attention": + shell.setWindowAttention(message.windowId, message.attention); + break; + case "set-title": + // Titles are informational for now (sidebar shows icons only). + break; } }; - worker.onerror = (error) => { - console.error(`worker app ${appId} error: ${JSON.stringify(error)}`); + options.worker.onerror = (error) => { + console.error(`worker app ${options.appId} error: ${JSON.stringify(error)}`); }; } @@ -61,35 +94,40 @@ export class WorkerAppHost { return this.openWindows.size; } - openWindow(options: WorkerWindowOptions): ShellWindow { - this.openWindows.add(options.windowId); + /** Open a window of this app and register it with the shell. */ + openWindow(spec: WorkerWindowSpec): ShellWindow { + const surfaceId = `window:${spec.windowId}`; + this.openWindows.add(spec.windowId); this.post({ type: "open-window", - windowId: options.windowId, - surfaceId: options.surfaceId, - viewport: options.viewport, + windowId: spec.windowId, + surfaceId, + viewport: this.options.viewport, }); - return { - appId: this.appId, - windowId: options.windowId, - title: options.title, - surfaceId: options.surfaceId, + const window: ShellWindow = { + appId: this.options.appId, + windowId: spec.windowId, + title: spec.title, + surfaceId, closeable: true, close: () => { - this.openWindows.delete(options.windowId); - this.post({ type: "close-window", windowId: options.windowId }); - options.removeSurface(); + this.openWindows.delete(spec.windowId); + this.post({ type: "close-window", windowId: spec.windowId }); + this.options.removeSurface(surfaceId); }, - drawIcon: makeLetterWindowIcon(options.iconLetter), + drawIcon: makeLetterWindowIcon(spec.iconLetter), handleInput: (event, frameId) => { - this.post({ type: "input", windowId: options.windowId, event, frameId }); + this.post({ type: "input", windowId: spec.windowId, event, frameId }); }, requestRender: () => { - this.post({ type: "render", windowId: options.windowId }); + this.post({ type: "render", windowId: spec.windowId }); }, setForeground: (foreground) => { - options.setSurfaceVisible(foreground); - this.post({ type: "foreground", windowId: options.windowId, foreground }); + this.options.setSurfaceVisible(surfaceId, foreground); + if (foreground) { + shell.setWindowAttention(spec.windowId, false); + } + this.post({ type: "foreground", windowId: spec.windowId, foreground }); }, setScreenOn: (on) => { // Screen state is per-app, but sending per-window keeps the protocol @@ -97,9 +135,24 @@ export class WorkerAppHost { this.post({ type: "screen", on }); }, }; + shell.registerWindow(window); + // Configure the surface before any foregrounding so the worker's first + // frame has somewhere to land. + void this.options + .configureSurface(surfaceId, false) + .then(() => { + if (spec.focus) { + shell.focusWindow(spec.windowId); + } + this.options.requestShellRender(); + }) + .catch((error) => { + console.error(`surface setup for ${spec.windowId} failed: ${error}`); + }); + return window; } private post(message: WorkerAppMessage): void { - this.worker.postMessage(message); + this.options.worker.postMessage(message); } } diff --git a/app/workers/terminal-app.worker.ts b/app/workers/terminal-app.worker.ts new file mode 100644 index 00000000..6aff58c6 --- /dev/null +++ b/app/workers/terminal-app.worker.ts @@ -0,0 +1,478 @@ +/** + * Terminal app, hosted in its own worker thread. Window model: + * - "terminal:hub": the window opened from the launcher; shows connection + * status and the list of live g2mirror sessions. One control websocket + * backs it and also carries unsolicited bell/title notifications. + * - "terminal:view:N": opened by selecting a session in the hub; each has + * its own websocket connection (the protocol allows one attached session + * per connection) and its own xterm emulator. + * + * Bells for a session with an open, non-foregrounded view window set that + * window's sidebar attention flag (cleared by the host on foregrounding). + * Frames are painted here and submitted directly to the Java compositor from + * this worker's thread. + */ +import "@nativescript/core/globals"; +import { GrayImage } from "../graphics/image"; +import { getFont } from "../graphics/bdffont"; +import * as frameTimings from "../native/frame-timings"; +import { + G2MirrorClient, + type G2MirrorSession, + type G2MirrorState, +} from "../native/g2mirror-client"; +import { onSettingsStoreChanged } from "../native/settings-store"; +import { + terminalAuthTokenSetting, + terminalHostSetting, + terminalPortSetting, +} from "../ui/dashboard-settings"; +import { TerminalEmulator } from "../ui/apps/terminal-emulator"; +import type { DashboardInputEvent } from "../ui/layers"; +import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window"; + +declare const global: any; +declare const com: any; + +// Terminus-12 has a 6x12 cell; the grid size is derived from the viewport at +// the first open-window and declared in each websocket init handshake. +const terminalFont = getFont("terminus12"); +const CELL_WIDTH = 6; +const CELL_HEIGHT = 12; +const DEVICE_NAME = "Faceclaw G2"; +const HUB_ROW_HEIGHT = 20; +const RENDER_COALESCE_MS = 33; + +type BaseWindow = { + windowId: string; + surfaceId: string; + foreground: boolean; + renderScheduled: boolean; + lastSubmittedFingerprint: string; +}; + +type HubWindow = BaseWindow & { + kind: "hub"; + selectedIndex: number; +}; + +type ViewWindow = BaseWindow & { + kind: "view"; + socket: string; + label: string; + client: G2MirrorClient; + emulator: TerminalEmulator; + receivedData: boolean; + attachRequested: boolean; + status: string; + unsubscribers: Array<() => void>; +}; + +type TerminalWindow = HubWindow | ViewWindow; + +const windows = new Map(); +const pendingViews = new Map(); +let nextViewSerial = 1; +let viewportWidth = 0; +let viewportHeight = 0; +let gridCols = 0; +let gridRows = 0; +let screenOn = true; + +// Control connection: session listing for the hub, plus unsolicited +// bell/title notifications for every monitored terminal. Lives as long as +// the worker so bells keep flowing even if the hub window is closed. +let controlClient: G2MirrorClient | null = null; +let controlState: G2MirrorState | null = null; +let controlUnsubscribers: Array<() => void> = []; + +function post(message: WorkerAppReply): void { + global.postMessage(message); +} + +global.onmessage = (event: { data: WorkerAppMessage }) => { + const message = event.data; + switch (message.type) { + case "open-window": + openWindow(message.windowId, message.surfaceId, message.viewport); + break; + case "close-window": + closeWindow(message.windowId); + break; + case "input": { + const window = windows.get(message.windowId); + if (!window) { + frameTimings.finishFrame(message.frameId, "discarded: unknown terminal window"); + break; + } + handleInput(window, message.event as DashboardInputEvent, message.frameId); + break; + } + case "render": { + const window = windows.get(message.windowId); + if (window) renderAndSubmit(window, 0); + break; + } + case "foreground": { + const window = windows.get(message.windowId); + if (!window) break; + window.foreground = message.foreground; + if (window.foreground) renderAndSubmit(window, 0); + break; + } + case "screen": + screenOn = message.on; + if (screenOn) { + for (const window of windows.values()) { + if (window.foreground) renderAndSubmit(window, 0); + } + } + break; + } +}; + +// Restart the control connection when the g2mirror settings change (edited +// in the dashboard's Settings menu, which lives in the main isolate). +onSettingsStoreChanged((key) => { + if (!key.startsWith("terminal.")) return; + if (gridCols > 0) { + startControlClient(); + } +}); + +function openWindow(windowId: string, surfaceId: string, viewport: { width: number; height: number }): void { + viewportWidth = viewport.width; + viewportHeight = viewport.height; + gridCols = Math.floor(viewportWidth / CELL_WIDTH); + gridRows = Math.floor(viewportHeight / CELL_HEIGHT); + + const pendingView = pendingViews.get(windowId); + if (pendingView) { + pendingViews.delete(windowId); + windows.set(windowId, createViewWindow(windowId, surfaceId, pendingView.socket, pendingView.label)); + return; + } + windows.set(windowId, { + kind: "hub", + windowId, + surfaceId, + foreground: false, + renderScheduled: false, + lastSubmittedFingerprint: "", + selectedIndex: 0, + }); + if (!controlClient) { + startControlClient(); + } +} + +function closeWindow(windowId: string): void { + const window = windows.get(windowId); + if (!window) return; + if (window.kind === "view") { + for (const unsubscribe of window.unsubscribers.splice(0)) { + unsubscribe(); + } + window.client.stop(); + } + windows.delete(windowId); +} + +function clientOptions() { + return { + host: terminalHostSetting.get().trim(), + port: parseInt(terminalPortSetting.get(), 10) || 8737, + authToken: terminalAuthTokenSetting.get(), + deviceName: DEVICE_NAME, + cols: gridCols, + rows: gridRows, + }; +} + +function startControlClient(): void { + stopControlClient(); + const options = clientOptions(); + if (!options.host) { + controlState = null; + renderHubWindows(); + return; + } + const client = new G2MirrorClient(options); + controlClient = client; + controlState = client.state(); + controlUnsubscribers.push( + client.onStateChange((state) => { + controlState = state; + renderHubWindows(); + }), + client.onBell((socket) => { + routeBell(socket); + }), + ); + client.start(); +} + +function stopControlClient(): void { + for (const unsubscribe of controlUnsubscribers.splice(0)) { + unsubscribe(); + } + controlClient?.stop(); + controlClient = null; + controlState = null; +} + +function routeBell(socket: string): void { + for (const window of windows.values()) { + if (window.kind === "view" && window.socket === socket && !window.foreground) { + post({ type: "set-attention", windowId: window.windowId, attention: true }); + } + } +} + +function renderHubWindows(): void { + for (const window of windows.values()) { + if (window.kind === "hub") scheduleRender(window); + } +} + +function createViewWindow(windowId: string, surfaceId: string, socket: string, label: string): ViewWindow { + const client = new G2MirrorClient(clientOptions()); + const window: ViewWindow = { + kind: "view", + windowId, + surfaceId, + foreground: false, + renderScheduled: false, + lastSubmittedFingerprint: "", + socket, + label, + client, + emulator: new TerminalEmulator(gridCols, gridRows), + receivedData: false, + attachRequested: false, + status: "Connecting...", + unsubscribers: [], + }; + window.unsubscribers.push( + client.onStateChange((state) => { + if (state.phase === "connected" && !window.attachRequested) { + window.attachRequested = true; + client.connectSession(socket); + } + window.status = state.status; + scheduleRender(window); + }), + client.onSessionAttached(() => { + client.view(); + scheduleRender(window); + }), + client.onTerminalData((data, kind) => { + if (kind === "snapshot") { + window.emulator.reset(); + } + window.receivedData = true; + window.emulator.write(data, () => scheduleRender(window)); + }), + client.onSessionDetached((reason) => { + window.status = `Detached (${reason}).`; + scheduleRender(window); + }), + ); + client.start(); + return window; +} + +function handleInput(window: TerminalWindow, event: DashboardInputEvent, frameId: number): void { + if (event.type === "double-click") { + frameTimings.finishFrame(frameId, "discarded: terminal yielded focus"); + post({ type: "yield-focus", windowId: window.windowId }); + return; + } + if (window.kind === "hub") { + handleHubInput(window, event, frameId); + return; + } + // View windows are readonly (protocol v1 has no keyboard input). + frameTimings.finishFrame(frameId, "discarded: terminal view ignored input"); +} + +type HubItem = { + label: string; + onSelect?: () => void; +}; + +function hubItems(): HubItem[] { + const items: HubItem[] = []; + const state = controlState; + const phase = state?.phase ?? "idle"; + + if (phase === "connected" || phase === "attached") { + for (const session of state?.sessions ?? []) { + items.push({ + label: sessionLabel(session), + onSelect: () => openViewWindowFor(session), + }); + } + if (!state?.sessions.length) { + items.push({ + label: "(no live sessions; run g2mirror )", + onSelect: () => controlClient?.listSessions(), + }); + } + } + if (phase === "idle" || phase === "failed") { + items.push({ + label: terminalHostSetting.get().trim() ? "Connect" : "Connect (host not set)", + onSelect: () => startControlClient(), + }); + } + return items; +} + +function handleHubInput(window: HubWindow, event: DashboardInputEvent, frameId: number): void { + const items = hubItems(); + switch (event.type) { + case "scroll-up": + window.selectedIndex = Math.max(0, window.selectedIndex - 1); + renderAndSubmit(window, frameId); + return; + case "scroll-down": + window.selectedIndex = Math.min(items.length - 1, window.selectedIndex + 1); + renderAndSubmit(window, frameId); + return; + case "click": { + const item = items[Math.max(0, Math.min(window.selectedIndex, items.length - 1))]; + item?.onSelect?.(); + renderAndSubmit(window, frameId); + return; + } + default: + frameTimings.finishFrame(frameId, "discarded: terminal hub ignored input"); + return; + } +} + +function openViewWindowFor(session: G2MirrorSession): void { + const windowId = `terminal:view:${nextViewSerial++}`; + const label = sessionLabel(session); + pendingViews.set(windowId, { socket: session.socket, label }); + post({ type: "open-window-request", windowId, title: label, iconLetter: "T", focus: true }); +} + +function paint(window: TerminalWindow): GrayImage { + return window.kind === "hub" ? paintHub(window) : paintView(window); +} + +function paintHub(window: HubWindow): GrayImage { + const image = new GrayImage(viewportWidth, viewportHeight, 0); + image.drawText(terminalFont, 18, 10, "Terminal", 220); + image.drawRect(12, 8, viewportWidth - 24, viewportHeight - 16, 52); + image.drawText(terminalFont, 24, 30, hubStatusLine(), 170); + + let listTop = 52; + if (!terminalHostSetting.get().trim()) { + image.drawText(terminalFont, 24, 46, "Set host in Settings > Terminal, see:", 150); + image.drawText(terminalFont, 24, 60, "https://github.com/jimrandomh/g2mirror", 190); + listTop += 28; + } + + const items = hubItems(); + window.selectedIndex = Math.max(0, Math.min(window.selectedIndex, items.length - 1)); + for (let index = 0; index < items.length; index++) { + const y = listTop + index * HUB_ROW_HEIGHT; + if (y + HUB_ROW_HEIGHT > viewportHeight - 30) break; + const selected = index === window.selectedIndex; + if (selected) { + image.fillRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 15); + image.drawRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 45); + } + image.drawText(terminalFont, 32, y + 2, items[index]!.label, selected ? 255 : 200); + } + + image.drawText(terminalFont, 24, viewportHeight - 24, "Double-click: back to sidebar", 110); + return image; +} + +function hubStatusLine(): string { + if (!terminalHostSetting.get().trim()) { + return "No host configured."; + } + return controlState?.status ?? "Not connected."; +} + +function paintView(window: ViewWindow): GrayImage { + const image = new GrayImage(viewportWidth, viewportHeight, 0); + if (!window.receivedData) { + image.drawText(terminalFont, 24, 110, window.status, 170); + image.drawText(terminalFont, 24, 130, "Double-click: back to sidebar", 110); + return image; + } + const cursor = window.emulator.cursor(); + image.fillRect(cursor.x * CELL_WIDTH, cursor.y * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT, 70); + const lines = window.emulator.visibleLines(); + for (let row = 0; row < lines.length; row++) { + const line = lines[row]!; + if (line.length === 0) continue; + image.drawText(terminalFont, 0, row * CELL_HEIGHT, line, 200); + } + return image; +} + +/** Coalesce bursty repaint triggers (terminal output) into ~30fps renders. */ +function scheduleRender(window: TerminalWindow): void { + if (window.renderScheduled) return; + window.renderScheduled = true; + setTimeout(() => { + window.renderScheduled = false; + renderAndSubmit(window, 0); + }, RENDER_COALESCE_MS); +} + +function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void { + if (!window.foreground || !screenOn) { + frameTimings.finishFrame(inputFrameId, "discarded: terminal window not visible"); + return; + } + const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`); + try { + const paintStartedAtMs = Date.now(); + const image = frameTimings.span(frameId, "paint", () => + frameTimings.runWithFrame(frameId, () => paint(window)), + ); + const paintMs = Date.now() - paintStartedAtMs; + const fingerprint = image.fingerprint(); + if (fingerprint === window.lastSubmittedFingerprint) { + frameTimings.finishFrame(frameId, "discarded: terminal content unchanged"); + return; + } + const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); + if (!communicator) { + frameTimings.finishFrame(frameId, "discarded: no active communicator"); + return; + } + const buffer = image.to8bppBuffer(); + communicator.submitSurfaceFrame( + buffer.buffer, + window.surfaceId, + 0, + 0, + image.width, + image.height, + fingerprint, + paintMs, + frameId, + ); + window.lastSubmittedFingerprint = fingerprint; + } catch (error) { + frameTimings.finishFrame(frameId, "discarded: terminal render failed"); + console.error(`terminal worker render failed: ${error}`); + } +} + +function sessionLabel(session: G2MirrorSession): string { + if (session.title) { + return `${session.title} (pid ${session.pid})`; + } + const hint = session.cwdHint.replace(/^_+/, "").replace(/_+/g, "/"); + return `${hint || "session"} (pid ${session.pid})`; +}