Migrate things into new UI design

This commit is contained in:
James Babcock committed 2026-07-07 00:52:47 -07:00
1 parent 1133f952e9
commit 68ab599856
21 files changed
+1262 -602

No files matched your search

-316
View File
@@ -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<void> {
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 <command>)",
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})`;
}
+8 -6
View File
@@ -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;
}
+1 -20
View File
@@ -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();
+8 -6
View File
@@ -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
});
+1 -53
View File
@@ -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)) {
+47 -10
View File
@@ -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,
);
}
+9
View File
@@ -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<LayerActions>): void {
Object.assign(this.ctx.actions, actions);
}
+83 -53
View File
@@ -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);
}
}
+4 -1
View File
@@ -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);
+117
View File
@@ -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<void>;
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<void> {
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<void> {
if (event.type === "double-click") {
shell.yieldFocusToSidebar();
return;
}
await this.inner.handleInput(event, ctx);
}
onRemoved(): void {
this.inner.onRemoved?.();
}
}
+11 -44
View File
@@ -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<void> {
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<void> {
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;
}
+49
View File
@@ -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<void> {
await this.stack.handleInput(event);
}
}
+36
View File
@@ -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<void>;
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,
});
}
+54
View File
@@ -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<void>;
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),
};
}
+28
View File
@@ -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;
+95 -42
View File
@@ -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<void>;
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<string>();
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);
}
}