diff --git a/ACKNOWLEDGEMENTS b/ACKNOWLEDGEMENTS index 8fb2897b..f84f2126 100644 --- a/ACKNOWLEDGEMENTS +++ b/ACKNOWLEDGEMENTS @@ -7,3 +7,8 @@ and the sherpa-onnx KWS Zipformer GigaSpeech model published by the k2-fsa/sherp community. G2 microphone LC3 decoding uses Google's liblc3: https://github.com/google/liblc3 + +The optional TerminusV proportional font is by ohnonot, derived from Dimitar +Zhekov's Terminus: https://framagit.org/ohnonot/terv-terc +It is distributed under the SIL Open Font License; see +app/fonts/terminusv/LICENSE. diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index 5308c378..9a829561 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -1004,7 +1004,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { enqueueFirmwareDebugFlagsLocked(); } - if (messageToPrewrite == null && handleHeartbeat()) { + // A frame ready to send right now: don't inject a fresh + // heartbeat in front of it (the image's own ack resets the + // firmware heartbeat timer, so the heartbeat is redundant). + boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp + && pendingMessages.isEmpty() && inFlightMessages.isEmpty() + && now >= imageRetryAfterMs + && !getDesiredFingerprint().equals(displayedFingerprint); + if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) { return ConnectionOptions.IDLE_SLEEP_MS; } @@ -1086,7 +1093,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { return message.message.length + 2 > 232; } - private boolean handleHeartbeat() { + private boolean handleHeartbeat(boolean imageWaiting) { long now = SystemClock.elapsedRealtime(); boolean heartbeatEligible = !shutdownRequested && warmedUp && fixedLayoutCreated; boolean heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat"); @@ -1096,6 +1103,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { boolean heartbeatBlocksLeftWrites = heartbeatReady || heartbeatPending; if (heartbeatReady && !heartbeatPending && inFlightMessages.isEmpty()) { + if (imageWaiting && !heartbeatUrgent) { + // Defer to the waiting frame: sending it now satisfies the + // firmware heartbeat deadline (its ack resets the timer), and + // the heartbeat still fires once we reach the URGENT threshold + // if rendering goes quiet again. Preserves the pending-heartbeat + // inter-lens-sync invariant below (that path is untouched). + return false; + } Log.i(TAG, "Writing heartbeat"); OutboundMessage heartbeatMessage = createHeartbeatMessage(); lastHeartbeatSentAtMs = now; @@ -1479,9 +1494,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { // known-displayed; displayedBmp is cleared on connect/reset/timeout // paths, so a non-empty value means display state is trusted. byte[] incrementalPayload = null; + BleImageOptimizer.IncrementalPlan incrementalPlan = null; if (connectionOptions.INCREMENTAL_FRAMES && displayedBmp.length > 0) { - incrementalPayload = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp, nextImageFrameId); - if (incrementalPayload != null) { + incrementalPlan = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp, nextImageFrameId); + if (incrementalPlan != null) { + incrementalPayload = incrementalPlan.payload; // advance only when a delta is actually emitted, so consecutive // deltas carry consecutive ids (CFW skip/reorder detection) nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1; @@ -1493,9 +1510,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE); FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan"); if (incrementalPayload != null) { + // changed/box are packed-byte counts (2px each); clusters>1 with + // changed<; constructor(opts: { @@ -22,15 +31,18 @@ export class BdfFont { descent: number; defaultChar: number; glyphs: Map; + fallback?: BdfFont; }) { this.ascent = opts.ascent; this.descent = opts.descent; this.lineHeight = opts.ascent + opts.descent; this.defaultChar = opts.defaultChar; this.glyphs = opts.glyphs; + this.fallback = opts.fallback; } - static parse(source: string): BdfFont { + static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number } = {}): BdfFont { + const minEncoding = opts.minEncoding ?? 0; const lines = source.replace(/\r/g, "").split("\n"); let ascent = 10; let descent = 2; @@ -92,7 +104,7 @@ export class BdfFont { continue; } if (inner === "ENDCHAR") { - if (encoding >= 0) { + if (encoding >= minEncoding) { glyphs.set(encoding, { encoding, dwidthX: dwidthX || bbxWidth, @@ -108,15 +120,25 @@ export class BdfFont { } } - return new BdfFont({ ascent, descent, defaultChar, glyphs }); + return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback }); + } + + /** Exact lookup for a single code point, with no default-char substitution. */ + private getExactGlyph(codePoint: number): Glyph | undefined { + return this.glyphs.get(codePoint) ?? this.fallback?.getExactGlyph(codePoint); } getGlyph(codePoint: number): Glyph | undefined { - return this.glyphs.get(codePoint) ?? this.glyphs.get(this.defaultChar) ?? this.glyphs.get(63); + return ( + this.getExactGlyph(codePoint) ?? + this.glyphs.get(this.defaultChar) ?? + this.glyphs.get(63) ?? + this.fallback?.getGlyph(codePoint) + ); } hasGlyph(codePoint: number): boolean { - return this.glyphs.has(codePoint); + return this.glyphs.has(codePoint) || (this.fallback?.hasGlyph(codePoint) ?? false); } measureText(text: string): number { @@ -128,34 +150,85 @@ export class BdfFont { } } -const cachedEmbeddedFonts = new Map(); - -function loadEmbeddedFont(path: string): BdfFont { - const cached = cachedEmbeddedFonts.get(path); - if (cached) { - return cached; - } - const file = knownFolders.currentApp().getFile(path); - const text = file.readTextSync(); - const font = BdfFont.parse(text); - cachedEmbeddedFonts.set(path, font); - return font; -} - const embeddedFonts = { "terminus12": "fonts/terminus/ter-u12n.bdf", "terminus16": "fonts/terminus/ter-u16n.bdf", "terminus24": "fonts/terminus/ter-u24n.bdf", "terminus32": "fonts/terminus/ter-u32n.bdf", -} + // TerminusV is a proportional (variable-width) Terminus derivative that only + // covers ISO-8859-1. At 12px it shares Terminus's baseline, so we fall back + // to Terminus 12 for any code point outside Latin-1. minEncoding 32 drops + // TerminusV's private low-encoding drawing glyphs so control code points fall + // through to Terminus too. + "terminusv12": "fonts/terminusv/terv12n.bdf", +} as const; type EmbeddedFontName = keyof typeof embeddedFonts; +const fontFallbacks: Partial> = { + terminusv12: "terminus12", +}; +const fontMinEncoding: Partial> = { + terminusv12: 32, +}; + +const cachedEmbeddedFonts = new Map(); + +function loadEmbeddedFont(name: EmbeddedFontName): BdfFont { + const cached = cachedEmbeddedFonts.get(name); + if (cached) { + return cached; + } + const file = knownFolders.currentApp().getFile(embeddedFonts[name]); + const text = file.readTextSync(); + const fallbackName = fontFallbacks[name]; + const font = BdfFont.parse(text, { + fallback: fallbackName ? loadEmbeddedFont(fallbackName) : undefined, + minEncoding: fontMinEncoding[name], + }); + cachedEmbeddedFonts.set(name, font); + return font; +} + export function getFont(font: EmbeddedFontName): BdfFont { - return loadEmbeddedFont(embeddedFonts[font]); + return loadEmbeddedFont(font); +} + +// User-selectable UI typeface. Only the small (12px) font has an alternative, +// since TerminusV ships no larger sizes; medium/large stay Terminus. The +// storage key and values are re-used by the settings UI in dashboard-settings. +export const UI_FONT_SETTING_KEY = "display.uiFont"; +export const UI_FONT_VALUES = ["terminus", "terminusv"] as const; +export type UiFontChoice = (typeof UI_FONT_VALUES)[number]; + +const SMALL_FONT_FOR_CHOICE: Record = { + terminus: "terminus12", + terminusv: "terminusv12", +}; + +// Reading the setting hits a JNI bridge, and getDefaultSmallFont() is called +// once per menu item per paint, so cache the resolved choice and invalidate it +// when the setting changes (in any isolate). +let cachedSmallFontChoice: UiFontChoice | null = null; +let smallFontListenerInstalled = false; + +function currentSmallFontName(): EmbeddedFontName { + if (!smallFontListenerInstalled) { + onSettingsStoreChanged((key) => { + if (key === UI_FONT_SETTING_KEY) cachedSmallFontChoice = null; + }); + smallFontListenerInstalled = true; + } + if (cachedSmallFontChoice === null) { + const raw = getStringSetting(UI_FONT_SETTING_KEY, "terminus"); + cachedSmallFontChoice = (UI_FONT_VALUES as readonly string[]).includes(raw) + ? (raw as UiFontChoice) + : "terminus"; + } + return SMALL_FONT_FOR_CHOICE[cachedSmallFontChoice]; } export function getDefaultSmallFont(): BdfFont { - return getFont("terminus12"); + return getFont(currentSmallFontName()); } export function getDefaultMediumFont(): BdfFont { return getFont("terminus16"); diff --git a/app/graphics/image.ts b/app/graphics/image.ts index 85f557d2..244e9cdc 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -129,7 +129,7 @@ export class GrayImage { text: string; value: number; }): void { - const lines = wrapText(font, text, this.width); + const lines = wrapText(font, text, width); for (let i = 0; i < lines.length; i++) { this.drawText(font, x, y + i * font.lineHeight, lines[i]!, value); } diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index 4d52184f..aa06623a 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -5,7 +5,13 @@ import { toUint8Array } from "../util/array-util"; declare const com: any; const ICON_SIZE = 24; -const ICON_CACHE_MS = 5_000; +// Backstop TTL only: the icon caches are invalidated eagerly whenever a +// notification is posted or dismissed (see invalidateIconCaches callers), so +// the tray is kept fresh by invalidation, not by expiry. A short TTL just +// forced a blocking main-thread re-fetch (~40-100ms of Java icon +// rasterization) every few seconds — usually producing a no-change frame that +// was then discarded. A long backstop keeps that off the render hot path. +const ICON_CACHE_MS = 60_000; let cachedIcons: GrayImage[] = []; let cachedAtMs = 0; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index e657c7c6..8a60c0cf 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -6,7 +6,12 @@ import { setBooleanSetting, setStringSetting, } from "~/native/settings-store"; -import { getDefaultSmallFont } from "~/graphics/bdffont"; +import { + getDefaultSmallFont, + UI_FONT_SETTING_KEY, + UI_FONT_VALUES, + type UiFontChoice, +} from "~/graphics/bdffont"; import { wrapText } from "~/graphics/textwrap"; import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, MenuLayer } from "./menu"; import { DashboardInputEvent, Layer, type LayerContext } from "./layers"; @@ -180,6 +185,15 @@ export const batteryDisplayModeSetting = new ConfigSettingEnum({ + id: "uiFont", + label: "Font", + storageKey: UI_FONT_SETTING_KEY, + defaultValue: "terminus", + values: UI_FONT_VALUES, + formatValue: uiFontLabel, +}); + export const timeFormatSetting = new ConfigSettingEnum({ id: "timeFormat", label: "Time format", @@ -322,6 +336,10 @@ export function timeFormatLabel(value: TimeFormat): string { return value === "12h" ? "12-hour" : "24-hour"; } +export function uiFontLabel(value: UiFontChoice): string { + return value === "terminusv" ? "TerminusV" : "Terminus"; +} + export function loadNightscoutSettings(): NightscoutSettings { return { siteUrl: nightscoutSiteUrlSetting.get(), diff --git a/app/ui/dashboard/about-page.ts b/app/ui/dashboard/about-page.ts deleted file mode 100644 index 4559f800..00000000 --- a/app/ui/dashboard/about-page.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { getDefaultSmallFont } from "~/graphics/bdffont"; -import { GrayImage } from "~/graphics/image"; -import { getDashboardLogo } from "~/graphics/logo"; -import { DashboardInputEvent, Layer, LayerContext, LayerStack } from "../layers"; - -export class AboutPage implements Layer { - 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, { transparentZero: true }); - } - image.drawText(dashboardFont, 108, 48, "Faceclaw", 220); - image.drawText(dashboardFont, 108, 64, "Dashboard prototype", 180); - - image.drawTextWrapped({ - font: dashboardFont, - x: 22, y: 128, - 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 - }); - return image; - } - - handleInput(event: DashboardInputEvent, ctx: { stack: LayerStack }): void { - if (event.type === "double-click") { - ctx.stack.pop(); - } - } -} diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index 7ecafdb9..2bd2fa8d 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -1,139 +1,120 @@ -import { APP_VIEWPORT } from "../shell/geometry"; -import { MenuLayer, type MenuLayout } from "../menu"; +import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDashboardLogo } from "../../graphics/logo"; import { shell } from "../shell/shell"; -import { batteryDisplayModeSetting, elevenLabsApiKeySetting, enumSettingMenuItem, firmwareDebugFlagsSetting, rawScreenshotsEnabledSetting, saveVoiceRecordingsSetting, terminalAuthTokenSetting, terminalHostSetting, terminalPortSetting, textSettingMenuItem, timeFormatSetting, toggleSettingMenuItem, voiceProviderSetting, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, screenTimeoutSetting } from "../dashboard-settings"; -import { AboutPage } from "./about-page"; +import { + batteryDisplayModeSetting, + elevenLabsApiKeySetting, + enumSettingMenuItem, + firmwareDebugFlagsSetting, + rawScreenshotsEnabledSetting, + saveVoiceRecordingsSetting, + terminalAuthTokenSetting, + terminalHostSetting, + terminalPortSetting, + textSettingMenuItem, + timeFormatSetting, + toggleSettingMenuItem, + uiFontSetting, + voiceProviderSetting, + nightscoutSiteUrlSetting, + nightscoutApiTokenSetting, + screenTimeoutSetting, +} from "../dashboard-settings"; +import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel"; -// Rendered in the Settings app window (viewport-sized); cap height to it. -const SETTINGS_MENU_LAYOUT: MenuLayout = { x: 8, y: 8, width: 272, maxHeight: APP_VIEWPORT.height - 16 }; - -export function createSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings", - [ - { - label: "Display", - onSelect: (ctx) => { - ctx.stack.push(createDisplaySettingsMenuLayer()); - }, - }, - { - label: "Voice", - onSelect: (ctx) => { - ctx.stack.push(createVoiceSettingsMenuLayer()); - }, - }, - { - label: "Integrations", - onSelect: (ctx) => { - 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(); - }, - }, - ], - SETTINGS_MENU_LAYOUT, - ); +/** The Settings app's master-detail panel (sections on the left, contents on the right). */ +export function createSettingsPanelLayer(): SettingsPanelLayer { + return new SettingsPanelLayer(settingsSections()); } -function createTerminalSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Terminal", - [ - textSettingMenuItem(terminalHostSetting), - textSettingMenuItem(terminalPortSetting), - textSettingMenuItem(terminalAuthTokenSetting), - ], - SETTINGS_MENU_LAYOUT, - ); -} - -function createDeveloperSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Developer", - [ - toggleSettingMenuItem(rawScreenshotsEnabledSetting), - toggleSettingMenuItem(saveVoiceRecordingsSetting), - toggleSettingMenuItem(firmwareDebugFlagsSetting), - ], - SETTINGS_MENU_LAYOUT, - ); -} - -function createDisplaySettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Display", - [ - enumSettingMenuItem(screenTimeoutSetting, { - onChange: () => { - shell.noteUserActivity(); +function settingsSections(): SettingsSection[] { + return [ + { + label: "Display", + items: [ + enumSettingMenuItem(screenTimeoutSetting, { + onChange: () => { + shell.noteUserActivity(); + }, + }), + // Controls the top-bar battery indicators (icon vs percentage). + enumSettingMenuItem(batteryDisplayModeSetting), + // Controls the top-bar clock (24-hour vs 12-hour). + enumSettingMenuItem(timeFormatSetting), + // Selects the UI body typeface (Terminus vs proportional TerminusV). + enumSettingMenuItem(uiFontSetting), + ], + }, + { + label: "Voice", + items: [enumSettingMenuItem(voiceProviderSetting), textSettingMenuItem(elevenLabsApiKeySetting)], + }, + { + label: "Integrations", + items: [ + { + label: "Nightscout", + onSelect: (ctx) => { + openSettingsSubMenu(ctx, "Nightscout", [ + textSettingMenuItem(nightscoutSiteUrlSetting), + textSettingMenuItem(nightscoutApiTokenSetting), + ]); + }, }, - }), - // Controls the top-bar battery indicators (icon vs percentage). - enumSettingMenuItem(batteryDisplayModeSetting), - // Controls the top-bar clock (24-hour vs 12-hour). - enumSettingMenuItem(timeFormatSetting), - ], - SETTINGS_MENU_LAYOUT, - ); -} - -function createVoiceSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Voice", - [ - enumSettingMenuItem(voiceProviderSetting), - textSettingMenuItem(elevenLabsApiKeySetting), - ], - SETTINGS_MENU_LAYOUT, - ); -} - -function createIntegrationsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Integrations", - [ - { - label: "Nightscout", - onSelect: (ctx) => { - ctx.stack.push(createNightscoutSettingsMenuLayer()); + ], + }, + { + label: "Terminal", + items: [ + textSettingMenuItem(terminalHostSetting), + textSettingMenuItem(terminalPortSetting), + textSettingMenuItem(terminalAuthTokenSetting), + ], + }, + { + label: "Developer", + items: [ + toggleSettingMenuItem(rawScreenshotsEnabledSetting), + toggleSettingMenuItem(saveVoiceRecordingsSetting), + toggleSettingMenuItem(firmwareDebugFlagsSetting), + ], + }, + { + label: "About", + items: [], + renderDetail: renderAbout, + }, + { + label: "Quit", + items: [ + { + label: "Disconnect from glasses", + onSelect: async (ctx) => { + ctx.stack.clearToBase(); + await ctx.actions.disconnect(); + }, }, - }, - ], - SETTINGS_MENU_LAYOUT, - ); + ], + }, + ]; } -export function createNightscoutSettingsMenuLayer(): MenuLayer { - return new MenuLayer( - "Settings > Integrations > Nightscout", - [ - textSettingMenuItem(nightscoutSiteUrlSetting), - textSettingMenuItem(nightscoutApiTokenSetting), - ], - SETTINGS_MENU_LAYOUT, - ); -} \ No newline at end of file +function renderAbout(args: { image: import("../../graphics/image").GrayImage; x: number; y: number; width: number }): void { + const { image, x, y, width } = args; + const font = getDefaultSmallFont(); + const logo = getDashboardLogo(); + if (logo) { + image.bitBlt(logo, x, y + 4, { transparentZero: true }); + } + const textX = logo ? x + logo.width + 12 : x; + image.drawText(font, textX, y + 8, "Faceclaw", 220); + image.drawText(font, textX, y + 24, "Dashboard prototype", 170); + image.drawTextWrapped({ + font, + x, + y: y + 64, + width, + 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: 170, + }); +} diff --git a/app/ui/dashboard/settings-panel.ts b/app/ui/dashboard/settings-panel.ts new file mode 100644 index 00000000..121a0f75 --- /dev/null +++ b/app/ui/dashboard/settings-panel.ts @@ -0,0 +1,192 @@ +import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { GrayImage } from "../../graphics/image"; +import { clamp } from "../../util/numeric-util"; +import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures"; +import { DashboardInputEvent, Layer, LayerContext } from "../layers"; +import { drawSelectionHighlight, MenuItem, MenuLayer, type MenuLayout } from "../menu"; +import { shell } from "../shell/shell"; + +/** + * One left-column entry. `items` are the right-column rows (reusing the shared + * MenuItem builders). `renderDetail`, when set, replaces the row list with + * custom informational content (e.g. About) and makes the pane non-interactive. + */ +export type SettingsSection = { + label: string; + items: MenuItem[]; + renderDetail?: (args: { + image: GrayImage; + x: number; + y: number; + width: number; + height: number; + ctx: LayerContext; + }) => void; +}; + +const PAD = 8; +const ROW_H = 20; +const LEFT_W = 150; +// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the +// settings items never use it (they act via ctx only). +const NO_MENU = new MenuLayer(null, []); + +/** + * Two-column (master-detail) settings UI. The left column lists sections; the + * right column previews the highlighted section's contents. A tap moves focus + * into the right column, a double-tap moves it back out (and from the left + * column, out to the sidebar). Third-level menus open as centered modals. + */ +export class SettingsPanelLayer implements Layer { + private leftIndex = 0; + private rightIndex = 0; + private focus: "left" | "right" = "left"; + private leftScroll = 0; + private rightScroll = 0; + + constructor(private readonly sections: SettingsSection[]) {} + + private section(): SettingsSection { + return this.sections[clamp(this.leftIndex, 0, this.sections.length - 1)]!; + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const appFocused = ctx.stack.isFocused(); + + this.leftIndex = clamp(this.leftIndex, 0, this.sections.length - 1); + const section = this.section(); + const rightItems = section.items; + this.rightIndex = clamp(this.rightIndex, 0, Math.max(0, rightItems.length - 1)); + + const top = PAD; + const listBottom = height - PAD - 12; // leave a row for the gesture hint + + // Left column: section labels. + const leftRows = Math.max(1, ((listBottom - top) / ROW_H) | 0); + this.leftScroll = clampScroll(this.leftIndex, this.leftScroll, leftRows, this.sections.length); + for (let i = this.leftScroll; i < Math.min(this.sections.length, this.leftScroll + leftRows); i++) { + const rowY = top + (i - this.leftScroll) * ROW_H; + const selected = i === this.leftIndex; + if (selected) { + drawSelectionHighlight(image, PAD - 2, rowY, LEFT_W, ROW_H - 2, appFocused && this.focus === "left", 6); + } + image.drawText(font, PAD + 8, rowY + 4, this.sections[i]!.label, selected ? 255 : 200); + } + + // Column divider (a light rule, not a full border). + const divX = PAD + LEFT_W + 6; + image.drawLine(divX, top, divX, listBottom, 40); + + // Right column: the highlighted section's contents. + const rightX = divX + 14; + const rightW = width - rightX - PAD; + if (section.renderDetail) { + section.renderDetail({ image, x: rightX, y: top, width: rightW, height: listBottom - top, ctx }); + } else if (rightItems.length) { + const rightRows = Math.max(1, ((listBottom - top) / ROW_H) | 0); + this.rightScroll = clampScroll(this.rightIndex, this.rightScroll, rightRows, rightItems.length); + for (let i = this.rightScroll; i < Math.min(rightItems.length, this.rightScroll + rightRows); i++) { + const item = rightItems[i]!; + const rowY = top + (i - this.rightScroll) * ROW_H; + // A selection only appears once focus is in the right column; before + // that the pane is a preview of what tapping would open. + const selected = this.focus === "right" && i === this.rightIndex; + if (selected) { + drawSelectionHighlight(image, rightX - 2, rowY, rightW + 2, ROW_H - 2, appFocused, 6); + } + if (item.render) { + item.render({ image, x: rightX + 8, y: rowY, width: rightW - 16, height: ROW_H - 3, selected, text: item.label, ctx }); + } else { + image.drawText(font, rightX + 8, rowY + 4, item.label, selected ? 255 : 200); + } + } + } + + const hint = + this.focus === "left" + ? `${GESTURE_SCROLL} section ${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} exit` + : `${GESTURE_SCROLL} item ${GESTURE_CLICK} select ${GESTURE_DOUBLE_CLICK} back`; + image.drawText(font, PAD, height - 10, hint, 110); + return image; + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + if (this.focus === "left") { + switch (event.type) { + case "scroll-up": + this.leftIndex = (this.leftIndex + this.sections.length - 1) % this.sections.length; + this.resetRight(); + return; + case "scroll-down": + this.leftIndex = (this.leftIndex + 1) % this.sections.length; + this.resetRight(); + return; + case "click": + // Only enter sections that have something interactive on the right. + if (this.section().items.length) { + this.focus = "right"; + this.resetRight(); + } + return; + case "double-click": + shell.yieldFocusToSidebar(); + return; + default: + return; + } + } + + const items = this.section().items; + switch (event.type) { + case "scroll-up": + if (items.length) this.rightIndex = (this.rightIndex + items.length - 1) % items.length; + return; + case "scroll-down": + if (items.length) this.rightIndex = (this.rightIndex + 1) % items.length; + return; + case "click": + if (items.length) await items[clamp(this.rightIndex, 0, items.length - 1)]!.onSelect(ctx, NO_MENU); + return; + case "double-click": + this.focus = "left"; + return; + default: + return; + } + } + + private resetRight(): void { + this.rightIndex = 0; + this.rightScroll = 0; + } +} + +/** + * Open a third-level menu as a centered, bordered modal over the panel. Reuses + * MenuLayer (which draws the border and self-closes on double-click). + */ +export function openSettingsSubMenu(ctx: LayerContext, title: string, items: MenuItem[]): void { + const { width, height } = ctx.stack.getBaseSize(); + const menuW = Math.min(320, width - 40); + const menuH = 16 + PAD + items.length * ROW_H + PAD; + const layout: MenuLayout = { + x: ((width - menuW) / 2) | 0, + y: ((height - menuH) / 2) | 0, + width: menuW, + minHeight: menuH, + maxHeight: menuH, + }; + ctx.stack.push(new MenuLayer(title, items, layout)); +} + +function clampScroll(selected: number, scroll: number, visible: number, count: number): number { + if (selected < scroll) { + scroll = selected; + } else if (selected >= scroll + visible) { + scroll = selected - visible + 1; + } + return clamp(scroll, 0, Math.max(0, count - visible)); +} diff --git a/app/ui/shell/settings-app.ts b/app/ui/shell/settings-app.ts index 1cf90cb2..d8c59709 100644 --- a/app/ui/shell/settings-app.ts +++ b/app/ui/shell/settings-app.ts @@ -1,8 +1,8 @@ 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 { createSettingsPanelLayer } from "../dashboard/settings-menus"; +import { createInProcessWindow } from "./in-process-window"; import { type ShellWindow } from "./shell"; export const SETTINGS_WINDOW_ID = "settings"; @@ -40,7 +40,9 @@ export function createSettingsAppWindow(options: SettingsAppOptions): SettingsAp icon: "settings", closeable: true, actions: options.actions, - baseLayer: new YieldAtRootLayer(createSettingsMenuLayer()), + // Not wrapped in YieldAtRootLayer: the panel routes double-click itself + // (right column -> left column, then left column -> sidebar). + baseLayer: createSettingsPanelLayer(), submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface,