From 2c04d99dfbcae4dace6d3dc7ad6507a75e54300e Mon Sep 17 00:00:00 2001 From: James Babcock Date: Wed, 8 Jul 2026 13:51:59 -0700 Subject: [PATCH] Change Nightscout icon, app-switcher sidebar highlight style --- .../java/com/faceclaw/app/IconRenderer.java | 22 +- CLAUDE.md | 11 +- app/g2/dashboard-controller.ts | 18 +- app/graphics/icons.ts | 22 +- app/ui/shell/chrome-layer.ts | 100 +++++++-- app/ui/shell/launcher-app.ts | 197 +++++++++++++++--- app/ui/shell/nightscout-app.ts | 2 +- app/ui/shell/shell.ts | 6 + images/nightscout.svg | 1 + 9 files changed, 311 insertions(+), 68 deletions(-) create mode 100644 images/nightscout.svg diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java index 864c906c..53b0673b 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java @@ -31,15 +31,20 @@ public final class IconRenderer { return new byte[0]; } try { - float viewW = 24f, viewH = 24f; + float minX = 0f, minY = 0f, viewW = 24f, viewH = 24f; Matcher vb = VIEWBOX_RE.matcher(svg); if (vb.find()) { String[] parts = vb.group(1).trim().split("[\\s,]+"); if (parts.length == 4) { + minX = Float.parseFloat(parts[0]); + minY = Float.parseFloat(parts[1]); viewW = Float.parseFloat(parts[2]); viewH = Float.parseFloat(parts[3]); } } + // Lucide-style icons are stroked outlines (fill="none"); everything + // else (Noun Project glyphs, brand logos) is a filled shape. + boolean stroked = svg.contains("fill=\"none\""); Path path = new Path(); Matcher m = TAG_RE.matcher(svg); while (m.find()) { @@ -52,15 +57,20 @@ public final class IconRenderer { Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); float scale = Math.min(size / viewW, size / viewH); - // Center the (square) viewBox in the bitmap. + // Center the (square) viewBox in the bitmap, then map its origin. canvas.translate((size - viewW * scale) / 2f, (size - viewH * scale) / 2f); canvas.scale(scale, scale); + canvas.translate(-minX, -minY); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setStyle(Paint.Style.STROKE); - paint.setStrokeWidth(strokeWidth); - paint.setStrokeCap(Paint.Cap.ROUND); - paint.setStrokeJoin(Paint.Join.ROUND); paint.setColor(0xffffffff); + if (stroked) { + paint.setStyle(Paint.Style.STROKE); + paint.setStrokeWidth(strokeWidth); + paint.setStrokeCap(Paint.Cap.ROUND); + paint.setStrokeJoin(Paint.Join.ROUND); + } else { + paint.setStyle(Paint.Style.FILL); + } canvas.drawPath(path, paint); int[] pixels = new int[size * size]; diff --git a/CLAUDE.md b/CLAUDE.md index a31349a0..d62af127 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,12 +9,13 @@ If build.sh exists, it's a machine-specific wrapper that sets some environment variables to point to where tools are installed, then runs `nativescript build android`. If build_and_run.sh exists, it does everything build.sh does plus - `native run android --device --justlaunch`, + `nativescript run android --device --justlaunch`, which will launch the app on the developer's phone if it is plugged in and has -adb enabled. If no phone is connected, it will fail and the output will contain -the string "Cannot find connected devices." To lint and typecheck, prefer to -use build.sh; if the user asks you to run or test the app, use build_and_run.sh -and then use `adb logcat` to view the results. +adb enabled. If no phone is connected, it will still build but the install step +will fail and the output will contain the string "Cannot find connected +devices." To lint and typecheck, prefer to use build.sh; if the user asks you +to run or test the app, use build_and_run.sh and then use `adb logcat` to view +the results. If you are working on low-level communication bits, consider checking out https://github.com/Commute773/g2-kit-unofficial/ and referring to ble/docs/ diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index f82e2803..d9e9422e 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -205,15 +205,15 @@ class DashboardController { requestRender: () => shell.foregroundWindow()?.requestRender(), }, apps: [ - { appId: "stopwatch", label: "Stopwatch" }, - { appId: "terminal", label: "Terminal" }, - { appId: "teleprompt", label: "Teleprompt" }, - { appId: "music", label: "Music" }, - { appId: "nightscout", label: "Nightscout" }, - { appId: "transcribe", label: "Transcribe" }, - { appId: "notifications", label: "Notifications" }, - { appId: "debug-tests", label: "Debug tests" }, - { appId: "settings", label: "Settings" }, + { appId: "stopwatch", label: "Stopwatch", icon: "timer" }, + { appId: "terminal", label: "Terminal", icon: "terminal" }, + { appId: "teleprompt", label: "Teleprompt", icon: "file-text" }, + { appId: "music", label: "Music", icon: "music" }, + { appId: "nightscout", label: "Nightscout", icon: "nightscout" }, + { appId: "transcribe", label: "Transcribe", icon: "mic" }, + { appId: "notifications", label: "Notifications", icon: "bell" }, + { appId: "debug-tests", label: "Debug tests", icon: "flask-conical" }, + { appId: "settings", label: "Settings", icon: "settings" }, ], launchApp: (appId) => this.launchApp(appId), submitFrame: (image, paintMs, frameId) => diff --git a/app/graphics/icons.ts b/app/graphics/icons.ts index 27577971..80202f18 100644 --- a/app/graphics/icons.ts +++ b/app/graphics/icons.ts @@ -19,25 +19,27 @@ const ICON_STROKE_WIDTH = 2; // against upstream if an icon needs updating. export const ICON_SVGS = { "layout-grid": - '', + '', timer: - '', + '', terminal: - '', + '', "file-text": - '', + '', music: - '', + '', activity: - '', + '', bell: - '', + '', "flask-conical": - '', + '', settings: - '', + '', mic: - '', + '', + nightscout: + '', } as const; export type IconName = keyof typeof ICON_SVGS; diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index 49995335..ab9a279d 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -22,7 +22,8 @@ export type ShellChromeWindow = { windowId: string; title: string; attention: boolean; - drawIcon: (image: GrayImage, x: number, y: number, size: number) => void; + /** Draw the window's indicator icon. inverted = black-on-white (focused tab). */ + drawIcon: (image: GrayImage, x: number, y: number, size: number, inverted?: boolean) => void; }; export type ShellChromeState = { @@ -36,23 +37,39 @@ export type ShellChromeState = { /** Placeholder window icon: rounded outline with a single letter. */ export function makeLetterWindowIcon(letter: string): ShellChromeWindow["drawIcon"] { - return (image, x, y, size) => { + return (image, x, y, size, inverted) => { const font = getDefaultMediumFont(); - image.drawRoundedRect(x, y, size, size, 120, 6); + if (!inverted) { + image.drawRoundedRect(x, y, size, size, 120, 6); + } const textX = x + Math.max(0, ((size - font.measureText(letter)) / 2) | 0); const textY = y + Math.max(0, ((size - font.lineHeight) / 2) | 0); - image.drawText(font, textX, textY, letter, 210); + image.drawText(font, textX, textY, letter, inverted ? SHELL_OPAQUE_BLACK : 210); }; } /** Window icon rendered from an SVG (Lucide), rendered once per size and cached. */ export function makeSvgWindowIcon(name: IconName): ShellChromeWindow["drawIcon"] { - return (image, x, y, size) => { + return (image, x, y, size, inverted) => { const icon = renderIcon(name, size); if (!icon) return; const dx = x + Math.max(0, ((size - icon.width) / 2) | 0); const dy = y + Math.max(0, ((size - icon.height) / 2) | 0); - image.bitBlt(icon, dx, dy, { transparentZero: true }); + if (!inverted) { + image.bitBlt(icon, dx, dy, { transparentZero: true }); + return; + } + // Invert onto a white background: coverage becomes darkness. Full coverage + // maps to SHELL_OPAQUE_BLACK (1) rather than 0, since 0 is the surface's + // transparent color key. + for (let row = 0; row < icon.height; row++) { + for (let col = 0; col < icon.width; col++) { + const coverage = icon.pixels[row * icon.width + col] ?? 0; + if (coverage > 0) { + image.setPixel(dx + col, dy + row, Math.max(SHELL_OPAQUE_BLACK, 255 - coverage)); + } + } + } }; } @@ -87,7 +104,6 @@ export class ShellChromeLayer implements Layer { private drawSidebar(image: GrayImage, state: ShellChromeState): void { image.fillRect(0, 0, SIDEBAR_WIDTH, G2_LENS_HEIGHT, SHELL_OPAQUE_BLACK); - image.drawLine(SIDEBAR_WIDTH - 1, 0, SIDEBAR_WIDTH - 1, G2_LENS_HEIGHT - 1, BORDER_VALUE); // Scroll the icon list to keep the selection visible; chevrons mark // windows off-screen above/below. @@ -104,16 +120,34 @@ export class ShellChromeLayer implements Layer { this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, count - visibleCount)); const lastVisible = Math.min(count, this.scrollRow + visibleCount); + // The selection is a "diversion" of the sidebar/main separator line: the + // line bulges right around the selected icon (rounded on the left, open + // to the main area on the right), so the separator is drawn with a gap + // there. When the sidebar has focus, the diversion is filled white and + // the icon drawn inverted. + const sep = SIDEBAR_WIDTH - 1; + const selVisible = state.selectedIndex >= this.scrollRow && state.selectedIndex < lastVisible; + const selTabTop = selVisible ? listTop + (state.selectedIndex - this.scrollRow) * itemStride - 2 : 0; + const selTabBottom = selTabTop + ICON_SIZE + 4; + if (selVisible) { + image.drawLine(sep, 0, sep, selTabTop, BORDER_VALUE); + image.drawLine(sep, selTabBottom, sep, G2_LENS_HEIGHT - 1, BORDER_VALUE); + } else { + image.drawLine(sep, 0, sep, G2_LENS_HEIGHT - 1, BORDER_VALUE); + } + for (let index = this.scrollRow; index < lastVisible; index++) { const window = state.windows[index]!; const y = listTop + (index - this.scrollRow) * itemStride; - if (index === state.selectedIndex) { - const highlight = state.focus === "sidebar" ? 200 : 70; - image.drawRoundedRect(0, y - 2, SIDEBAR_WIDTH - 3, ICON_SIZE + 4, highlight, 6); + const selected = index === state.selectedIndex; + const focused = selected && state.focus === "sidebar"; + if (selected) { + drawSelectionTab(image, y - 2, y + ICON_SIZE + 2, focused); } - window.drawIcon(image, ICON_MARGIN_X, y, ICON_SIZE); + window.drawIcon(image, ICON_MARGIN_X, y, ICON_SIZE, focused); if (window.attention) { - image.fillRoundedRect(ICON_MARGIN_X + ICON_SIZE - 7, y - 1, 8, 8, 255, 4); + // Black dot on the white focused tab, white dot otherwise. + image.fillRoundedRect(ICON_MARGIN_X + ICON_SIZE - 7, y - 1, 8, 8, focused ? SHELL_OPAQUE_BLACK : 255, 4); } } @@ -223,6 +257,48 @@ function drawTrayIcons(image: GrayImage, trayIcons: GrayImage[], rightEdge: numb return x; } +const TAB_RADIUS = 6; +// How far the diversion pokes past the separator into the main area. +const TAB_EXTEND = 1; +const TAB_STROKE = 150; + +/** + * Draw the selection "diversion" of the separator line around a sidebar icon: + * rounded on the left, square and open on the right, extending a few px past + * the separator into the main area. Focused = filled white; otherwise an + * outline. + */ +function drawSelectionTab(image: GrayImage, top: number, bottom: number, focused: boolean): void { + const right = SIDEBAR_WIDTH - 1 + TAB_EXTEND; + if (focused) { + for (let yy = top; yy < bottom; yy++) { + const inset = tabLeftInset(yy, top, bottom, TAB_RADIUS); + image.fillRect(inset, yy, right - inset + 1, 1, 255); + } + return; + } + // Outline: curved left edge (per-row) plus straight top and bottom edges. + for (let yy = top; yy < bottom; yy++) { + image.setPixel(tabLeftInset(yy, top, bottom, TAB_RADIUS), yy, TAB_STROKE); + } + image.drawLine(tabLeftInset(top, top, bottom-1, TAB_RADIUS), top, right, top, TAB_STROKE); + image.drawLine(tabLeftInset(bottom-1, top, bottom-1, TAB_RADIUS), bottom-1, right, bottom-1, TAB_STROKE); +} + +/** Left boundary x of the tab at row yy: 0 in the middle, curving to the rounded left corners. */ +function tabLeftInset(yy: number, top: number, bottom: number, radius: number): number { + const cy = yy + 0.5; + let dy = 0; + if (cy < top + radius) { + dy = top + radius - cy; + } else if (cy > bottom - radius) { + dy = cy - (bottom - radius); + } else { + return 0; + } + return Math.max(0, Math.round(radius - Math.sqrt(Math.max(0, radius * radius - dy * dy)))); +} + /** Small triangle marker for sidebar overflow; direction -1 = up, 1 = down. */ function drawChevron(image: GrayImage, centerX: number, y: number, direction: -1 | 1): void { const half = 5; diff --git a/app/ui/shell/launcher-app.ts b/app/ui/shell/launcher-app.ts index 23427dc1..dd44ee8d 100644 --- a/app/ui/shell/launcher-app.ts +++ b/app/ui/shell/launcher-app.ts @@ -1,13 +1,21 @@ +import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; import { GrayImage } from "../../graphics/image"; -import { LayerActions } from "../layers"; -import { MenuLayer } from "../menu"; -import { APP_VIEWPORT } from "./geometry"; -import { createInProcessWindow, YieldAtRootLayer } from "./in-process-window"; -import { type ShellWindow } from "./shell"; +import { renderIcon, type IconName } from "../../graphics/icons"; +import { clamp } from "../../util/numeric-util"; +import { + GESTURE_CLICK, + GESTURE_DOUBLE_CLICK, + GESTURE_SCROLL, +} from "../gestures"; +import { DashboardInputEvent, Layer, LayerActions, LayerContext } from "../layers"; +import { drawSelectionHighlight } from "../menu"; +import { createInProcessWindow } from "./in-process-window"; +import { shell, type ShellWindow } from "./shell"; export type LauncherAppEntry = { appId: string; label: string; + icon: IconName; }; export type LauncherOptions = { @@ -23,28 +31,156 @@ export type LauncherOptions = { export const LAUNCHER_WINDOW_ID = "launcher"; export const LAUNCHER_SURFACE_ID = "window:launcher"; +const COLS = 5; +// Row height is sized so 3.5 rows fit the viewport: three full rows are +// always visible, and a fourth (half-clipped) row peeks in to signal that +// there are more apps to scroll to. +const VISIBLE_ROWS = 3.5; +const FULL_ROWS = Math.floor(VISIBLE_ROWS); +const GRID_TOP = 6; +const FOOTER_HEIGHT = 16; +const ICON_SIZE = 44; +const LABEL_GAP = 2; + +type LauncherMode = "row" | "item"; + /** - * 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. + * The launcher grid: app icons with labels, arranged in a 3-column grid. + * Navigation has two levels so the max number of swipes to any app is halved: + * entering from the sidebar starts in "row" mode (scroll picks a row); a tap + * drops into "item" mode on that row, defaulting to the middle column (scroll + * picks the app); a tap launches it. Double-click backs out one level (item → + * row), and from the top level yields to the sidebar. + */ +class LauncherGridLayer implements Layer { + private mode: LauncherMode = "row"; + private selectedRow = 0; + private selectedCol = 1; + private scrollRow = 0; + private wasFocused = false; + + constructor(private readonly options: LauncherOptions) {} + + private rowCount(): number { + return Math.max(1, Math.ceil(this.options.apps.length / COLS)); + } + + private itemsInRow(row: number): number { + return Math.max(0, Math.min(COLS, this.options.apps.length - row * COLS)); + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const focused = ctx.stack.isFocused(); + // Re-entering the launcher (focus gained) resets to row selection. + if (focused && !this.wasFocused) { + this.mode = "row"; + } + this.wasFocused = focused; + + const rows = this.rowCount(); + this.selectedRow = clamp(this.selectedRow, 0, rows - 1); + this.selectedCol = clamp(this.selectedCol, 0, Math.max(0, this.itemsInRow(this.selectedRow) - 1)); + + const gridBottom = height - FOOTER_HEIGHT; + const rowH = (gridBottom - GRID_TOP) / VISIBLE_ROWS; + const colW = width / COLS; + + // Scroll to keep the selected row among the fully-visible rows. + if (this.selectedRow < this.scrollRow) { + this.scrollRow = this.selectedRow; + } else if (this.selectedRow > this.scrollRow + FULL_ROWS - 1) { + this.scrollRow = this.selectedRow - (FULL_ROWS - 1); + } + this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows - FULL_ROWS)); + + const rowY = (row: number) => GRID_TOP + (row - this.scrollRow) * rowH; + + // Selection highlight (row band, or a single cell in item mode). + const selY = rowY(this.selectedRow); + if (this.mode === "row") { + drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6); + } else { + drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6); + } + + for (let index = 0; index < this.options.apps.length; index++) { + const app = this.options.apps[index]!; + const row = Math.floor(index / COLS); + if (row < this.scrollRow) continue; + const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2); + if (blockTop >= gridBottom) break; // fully below the grid + const centerX = (index % COLS) * colW + colW / 2; + const icon = renderIcon(app.icon, ICON_SIZE); + if (icon) { + // Clip the icon at the grid bottom so a peeking row shows only its top. + const clipHeight = Math.min(icon.height, Math.floor(gridBottom - blockTop)); + image.bitBlt(icon, Math.round(centerX - icon.width / 2), Math.round(blockTop), { + height: clipHeight, + transparentZero: true, + }); + } + const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP); + if (labelY + font.lineHeight <= gridBottom) { + const label = truncate(font, app.label, colW - 8); + image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210); + } + } + + const hint = + this.mode === "row" + ? `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick ${GESTURE_DOUBLE_CLICK} back` + : `${GESTURE_SCROLL} app ${GESTURE_CLICK} launch ${GESTURE_DOUBLE_CLICK} row`; + image.drawText(font, 8, height - 14, hint, 110); + return image; + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + const rows = this.rowCount(); + switch (event.type) { + case "scroll-up": + case "scroll-down": { + const delta = event.type === "scroll-down" ? 1 : -1; + if (this.mode === "row") { + this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1); + } else { + this.selectedCol = clamp(this.selectedCol + delta, 0, Math.max(0, this.itemsInRow(this.selectedRow) - 1)); + } + return; + } + case "click": { + if (this.mode === "row") { + this.mode = "item"; + // Default to the middle column (clamped to the row's app count). + this.selectedCol = Math.min(Math.floor(COLS / 2), this.itemsInRow(this.selectedRow) - 1); + } else { + const index = this.selectedRow * COLS + this.selectedCol; + const app = this.options.apps[index]; + if (app) await this.options.launchApp(app.appId); + } + return; + } + case "double-click": + if (this.mode === "item") { + this.mode = "row"; + } else { + shell.yieldFocusToSidebar(); + } + return; + default: + return; + } + } +} + +/** + * The launcher: a pinned, uncloseable in-process window presenting the app + * grid. Selecting an app asks the controller to launch it and foregrounds the + * new window. */ export function createLauncherWindow(options: LauncherOptions): ShellWindow { - const menu = new MenuLayer( - "Apps", - options.apps.map((app) => ({ - label: app.label, - onSelect: async () => { - await options.launchApp(app.appId); - }, - })), - { - x: 8, - y: 8, - width: 272, - minHeight: 0, - maxHeight: APP_VIEWPORT.height - 16, - }, - ); return createInProcessWindow({ appId: "launcher", windowId: LAUNCHER_WINDOW_ID, @@ -53,8 +189,19 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow { icon: "layout-grid", closeable: false, actions: options.actions, - baseLayer: new YieldAtRootLayer(menu), + // Not wrapped in YieldAtRootLayer: the grid handles double-click itself to + // back out of item selection before yielding to the sidebar. + baseLayer: new LauncherGridLayer(options), submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, }).window; } + +function truncate(font: BdfFont, text: string, maxWidth: number): string { + if (font.measureText(text) <= maxWidth) return text; + let out = text; + while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) { + out = out.slice(0, -1); + } + return `${out}...`; +} diff --git a/app/ui/shell/nightscout-app.ts b/app/ui/shell/nightscout-app.ts index bdbd6f7c..4f91533d 100644 --- a/app/ui/shell/nightscout-app.ts +++ b/app/ui/shell/nightscout-app.ts @@ -32,7 +32,7 @@ export function createNightscoutAppWindow(options: InProcessAppOptions): InProce windowId: NIGHTSCOUT_WINDOW_ID, title: "Nightscout", iconLetter: "Ns", - icon: "activity", + icon: "nightscout", closeable: true, actions: options.actions, baseLayer: new YieldAtRootLayer(new NightscoutLayer()), diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index c90f21f7..bb0f7dd6 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -273,6 +273,9 @@ class Shell { yieldFocusToSidebar(): void { if (this.focus === "sidebar") return; this.focus = "sidebar"; + // Repaint the window so its selection highlight dims to the unfocused + // style this frame. + this.foregroundWindow()?.requestRender(); this.config.requestShellRender(); } @@ -353,6 +356,9 @@ class Shell { case "click": if (this.windows.length) { this.focus = "window"; + // Repaint the window now so its selection highlight reflects focus + // this frame, not one frame late. + this.foregroundWindow()?.requestRender(); } return { shell: true, window: false }; default: diff --git a/images/nightscout.svg b/images/nightscout.svg new file mode 100644 index 00000000..00566f3e --- /dev/null +++ b/images/nightscout.svg @@ -0,0 +1 @@ + \ No newline at end of file