diff --git a/app/apps/glanceboard/widgets/system-card.ts b/app/apps/glanceboard/widgets/system-card.ts index 4c363b39..c952b67d 100644 --- a/app/apps/glanceboard/widgets/system-card.ts +++ b/app/apps/glanceboard/widgets/system-card.ts @@ -1,4 +1,4 @@ -import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery"; +import { BATTERY_ICON_WIDTH, drawBattery, drawDenseBatteries, type BatteryDevice } from "../../../graphics/battery"; import { type GrayImage, type UiFont } from "../../../graphics/image"; import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons"; import { readPhoneBatteryState } from "../../../native/phone-battery"; @@ -28,7 +28,7 @@ const NOTIFICATION_ICON_GAP = 4; const BATTERY_ITEM_GAP = 12; const BATTERY_LABEL_VALUE = 150; -type BatteryItem = { label: string; percent: number; charging: boolean }; +type BatteryItem = { label: string; device: BatteryDevice; percent: number; charging: boolean }; /** * Date and time, the phone's notification icons, the phone/watch/G2/R1 @@ -130,19 +130,25 @@ export class SystemCardWidget implements GlanceWidget { function collectBatteryItems(): BatteryItem[] { const items: BatteryItem[] = []; - const push = (visibility: BatteryIndicatorVisibility, label: string, percent: number | null, charging: boolean | null) => { + const push = ( + visibility: BatteryIndicatorVisibility, + label: string, + device: BatteryDevice, + percent: number | null, + charging: boolean | null, + ) => { if (percent === null || !Number.isFinite(percent)) return; const clamped = Math.max(0, Math.min(100, Math.round(percent))); if (!batteryIndicatorVisible(visibility, clamped)) return; - items.push({ label, percent: clamped, charging: Boolean(charging) }); + items.push({ label, device, percent: clamped, charging: Boolean(charging) }); }; const phone = readPhoneBatteryState(); - push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging); + push(phoneBatteryVisibilitySetting.get(), "Phone", "phone", phone.battery, phone.charging); const levels = shell.getBatteryLevels(); - push(watchBatteryVisibilitySetting.get(), "Watch", levels.watch, levels.watchCharging); - push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging); + push(watchBatteryVisibilitySetting.get(), "Watch", "watch", levels.watch, levels.watchCharging); + push(glassesBatteryVisibilitySetting.get(), "G2", "glasses", levels.headset, levels.headsetCharging); if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) { - push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging); + push(ringBatteryVisibilitySetting.get(), "R1", "ring", levels.ring, levels.ringCharging); } return items; } @@ -150,13 +156,15 @@ function collectBatteryItems(): BatteryItem[] { /** * The battery indicators right-aligned at `right`, in the configured style. * Side-by-side styles put the label beside the gauge (or percentage) on one - * line; stacked styles centre the label above it. Returns the block's left - * edge (or `right` when nothing is shown). + * line; stacked styles centre the label above it; the dense style swaps + * labels for device icons and stacks two indicators per column, as in the + * top bar. Returns the block's left edge (or `right` when nothing is shown). */ function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): number { const items = collectBatteryItems(); if (!items.length) return right; const mode = batteryDisplayModeSetting.get(); + if (mode === "dense") return drawDenseBatteries(image, items, right, top); const percentage = mode === "percentage" || mode === "stacked-percentage"; const stacked = mode === "stacked" || mode === "stacked-percentage"; const gaugeHeight = drawBattery(0, false).height; diff --git a/app/apps/microphones/microphones-app.ts b/app/apps/microphones/microphones-app.ts index ef0d491e..f567e651 100644 --- a/app/apps/microphones/microphones-app.ts +++ b/app/apps/microphones/microphones-app.ts @@ -60,7 +60,7 @@ const TRAY_ICON = imageFromAsciiArt( " # # ", " ### ### ", ], - 220, + { "#": 220 }, ); const MENU_LAYOUT = { diff --git a/app/apps/transcribe/transcribe-app.ts b/app/apps/transcribe/transcribe-app.ts index b0a17639..21ac40d4 100644 --- a/app/apps/transcribe/transcribe-app.ts +++ b/app/apps/transcribe/transcribe-app.ts @@ -32,7 +32,7 @@ const MIC_ICON = imageFromAsciiArt( " # ", " ###### ", ], - 220, + { "#": 220 }, ); export type TranscribeAppOptions = InProcessAppOptions & { diff --git a/app/graphics/battery.ts b/app/graphics/battery.ts index e3b0e1f0..3c8a66fe 100644 --- a/app/graphics/battery.ts +++ b/app/graphics/battery.ts @@ -34,17 +34,154 @@ export const BATTERY_ICON_WIDTH = BODY_WIDTH + NUB_WIDTH; const BATTERY_BOLT_ICON = imageFromAsciiArt( [ - "....#..", - "...##..", - "..##...", - ".######", - "...##..", - "..##...", - "..#....", - ], - 255, + " # ", + " ## ", + " ## ", + " ######", + " ## ", + " ## ", + " # ", + ], { + "#": 255, + " ": 0, + }, ); +const PHONE_ICON = imageFromAsciiArt( + [ + " ###### ", + " #//### ", + " #//### ", + " ###### ", + " ###### ", + " ###### ", + " ###### ", + " ###### ", + " ###### ", + " ###### ", + ], { + "#": 255, + "/": 64, + " ": 0, + } +); + +const GLASSES_ICON = imageFromAsciiArt( + [ + " _ _ ", + "# #", + "# #", + "# #", + "# #", + "####/ /####", + "#___###___#", + "#___# #___#", + " ### ### ", + " ", + ], { + "#": 255, + "/": 192, + "_": 32, + " ": 0, + } +); + +const WATCH_ICON = imageFromAsciiArt( + [ + " // ", + " // ", + " // ", + " #### ", + " #!!# ", + " #!!# ", + " #### ", + " // ", + " // ", + " // ", + ], { + "#": 255, + "!": 192, + "/": 140, + " ": 0, + } +); + +const RING_ICON = imageFromAsciiArt( + [ + " /###/ ", + " /#####/ ", + "/# #/", + "# #", + "# #", + "# #", + "/# #/", + " /# #/ ", + " /###/ ", + " ", + ], { + "#": 255, + "/": 96, + " ": 0, + }, +); + +/** A device with a battery indicator, for the dense style's icon labels. */ +export type BatteryDevice = "phone" | "watch" | "glasses" | "ring"; + +const DEVICE_ICONS: Readonly> = { + phone: PHONE_ICON, + watch: WATCH_ICON, + glasses: GLASSES_ICON, + ring: RING_ICON, +}; + +export type DenseBatteryItem = { device: BatteryDevice; percent: number; charging: boolean }; + +/** Clear rows between the two indicators in a dense column. */ +const DENSE_ROW_GAP = 2; +/** Clear columns between a device icon's slot and its gauge. */ +const DENSE_ICON_GAP = 3; +const DENSE_COLUMN_GAP = 8; + +/** Height of a dense block: two indicator rows and the gap between them. */ +export const DENSE_BATTERY_BLOCK_HEIGHT = 2 * ICON_HEIGHT + DENSE_ROW_GAP; + +/** + * Dense style: each device's icon to the left of its gauge, two indicators + * stacked per column, columns right-aligned at `right` with `top` the upper + * row's top edge. Columns pair from the right, so an odd count leaves the + * first indicator alone in the leftmost column, centered vertically. Within + * a column the gauges line up and each icon is centered in a slot as wide as + * the column's widest icon. Returns the block's left edge (`right` when there + * are no items). + */ +export function drawDenseBatteries( + image: GrayImage, + items: readonly DenseBatteryItem[], + right: number, + top: number, +): number { + if (!items.length) return right; + let x = right; + for (let end = items.length; end > 0; end -= 2) { + const column = items.slice(Math.max(0, end - 2), end); + const slotWidth = Math.max(...column.map((item) => DEVICE_ICONS[item.device].width)); + x -= slotWidth + DENSE_ICON_GAP + BATTERY_ICON_WIDTH; + const firstRowTop = column.length === 2 ? top : top + (((DENSE_BATTERY_BLOCK_HEIGHT - ICON_HEIGHT) / 2) | 0); + for (let row = 0; row < column.length; row++) { + const item = column[row]!; + const rowTop = firstRowTop + row * (ICON_HEIGHT + DENSE_ROW_GAP); + const icon = DEVICE_ICONS[item.device]; + image.bitBlt(icon, x + (((slotWidth - icon.width) / 2) | 0), rowTop, { transparentZero: true }); + image.bitBlt(drawBattery(item.percent, item.charging), x + slotWidth + DENSE_ICON_GAP, rowTop, { + transparentZero: true, + }); + } + x -= DENSE_COLUMN_GAP; + } + return x + DENSE_COLUMN_GAP; +} + /** The empty outline, built once from the geometry constants. */ const EMPTY_BATTERY_ICON = buildEmptyBattery(); diff --git a/app/graphics/image.ts b/app/graphics/image.ts index 340fbccf..f36a0dd7 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -8,17 +8,24 @@ export const G2_LENS_WIDTH = 640; export const G2_LENS_HEIGHT = 480; const DEFAULT_CORNER_RADIUS = 8; -export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayImage { +/** + * Maps ASCII-art characters to gray levels 0-255. Space and "." are 0 unless + * the palette overrides them; any other character must have an entry. + */ +export type AsciiArtPalette = Readonly>; + +export function imageFromAsciiArt(lines: readonly string[], palette: AsciiArtPalette): GrayImage { const width = Math.max(0, ...lines.map((line) => line.length)); const image = new GrayImage(width, lines.length, 0); - const fill = clampByte(value); for (let y = 0; y < lines.length; y++) { const line = lines[y]!; for (let x = 0; x < line.length; x++) { - const pixel = line[x]; - if (pixel && pixel !== " " && pixel !== ".") { - image.pixels[y * width + x] = fill; + const pixel = line[x]!; + const value = palette[pixel] ?? (pixel === " " || pixel === "." ? 0 : undefined); + if (value === undefined) { + throw new Error(`imageFromAsciiArt: no palette entry for ${JSON.stringify(pixel)}`); } + image.pixels[y * width + x] = clampByte(value); } } return image; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 8ae9c943..b4a44548 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -27,7 +27,7 @@ export type NightscoutSettings = { siteUrl: string; apiToken: string; }; -export type BatteryDisplayMode = "icon" | "percentage" | "stacked" | "stacked-percentage"; +export type BatteryDisplayMode = "icon" | "percentage" | "stacked" | "stacked-percentage" | "dense"; /** When a top-bar battery indicator is shown: always, only below 50%, or never. */ export type BatteryIndicatorVisibility = "always" | "low" | "never"; export type TimeFormat = "24h" | "12h"; @@ -235,9 +235,9 @@ export const batteryDisplayModeSetting = new ConfigSettingEnum Customization > Battery indicators submenu picks the style (label beside a gauge icon or percentage, or stacked above - * either) and, per device, whether the indicator shows always, only below - * 50%, or never. Returns the left edge of the battery block. + * The Settings > Customization > Battery indicators submenu picks the + * style (label beside a gauge icon or percentage, stacked above either, or + * dense: device icons beside gauges, two per column) and, per device, + * whether the indicator shows always, only below 50%, or never. Returns + * the left edge of the battery block. */ private drawTopBarBatteries(image: GrayImage, state: ShellChromeState, barTop: number): number { const mode = batteryDisplayModeSetting.get(); const items: BatteryItem[] = []; const phone = readPhoneBatteryState(); if (phone.battery !== null && Number.isFinite(phone.battery)) { - pushBatteryItem(items, phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, Boolean(phone.charging)); + pushBatteryItem(items, phoneBatteryVisibilitySetting.get(), "Phone", "phone", phone.battery, Boolean(phone.charging)); } if (state.battery.watch !== null && Number.isFinite(state.battery.watch)) { - pushBatteryItem(items, watchBatteryVisibilitySetting.get(), "Watch", state.battery.watch, + pushBatteryItem(items, watchBatteryVisibilitySetting.get(), "Watch", "watch", state.battery.watch, Boolean(state.battery.watchCharging)); } if (state.battery.headset !== null && Number.isFinite(state.battery.headset)) { - pushBatteryItem(items, glassesBatteryVisibilitySetting.get(), "G2", state.battery.headset, + pushBatteryItem(items, glassesBatteryVisibilitySetting.get(), "G2", "glasses", state.battery.headset, Boolean(state.battery.headsetCharging)); } if (state.battery.ring !== null && Number.isInteger(state.battery.ring) && state.battery.ring >= 0 && state.battery.ring <= 100) { - pushBatteryItem(items, ringBatteryVisibilitySetting.get(), "R1", state.battery.ring, + pushBatteryItem(items, ringBatteryVisibilitySetting.get(), "R1", "ring", state.battery.ring, Boolean(state.battery.ringCharging)); } if (!items.length) return G2_LENS_WIDTH; if (mode === "stacked" || mode === "stacked-percentage") { return drawStackedBatteries(image, items, barTop, mode === "stacked-percentage"); } + if (mode === "dense") { + return drawDenseBatteries(image, items, G2_LENS_WIDTH - BATTERY_BLOCK_RIGHT_MARGIN, barTop + DENSE_TOP); + } const font = getDefaultSmallFont(); const percentageMode = mode === "percentage"; @@ -440,7 +445,7 @@ export class ShellChromeLayer implements Layer { } } -type BatteryItem = { label: string; percent: number; charging: boolean }; +type BatteryItem = { label: string; device: BatteryDevice; percent: number; charging: boolean }; const BATTERY_BLOCK_RIGHT_MARGIN = 8; const BATTERY_LABEL_VALUE = 150; @@ -450,12 +455,13 @@ function pushBatteryItem( items: BatteryItem[], visibility: BatteryIndicatorVisibility, label: string, + device: BatteryDevice, percent: number, charging: boolean, ): void { const clamped = Math.max(0, Math.min(100, Math.round(percent))); if (!batteryIndicatorVisible(visibility, clamped)) return; - items.push({ label, percent: clamped, charging }); + items.push({ label, device, percent: clamped, charging }); } // Stacked style geometry, in rows below the bar's top edge. The bar has 27 @@ -472,6 +478,10 @@ const STACKED_ICON_TOP = 13; const STACKED_PERCENT_BASELINE = 21; const STACKED_ITEM_GAP = 10; +// Dense style: two 10px indicator rows with a 2px gap span rows 2..23, the +// same vertical center as the stacked styles' ink. +const DENSE_TOP = (((TOP_BAR_HEIGHT - 1 - DENSE_BATTERY_BLOCK_HEIGHT) / 2) | 0); + /** * Stacked styles: each label centered above its gauge icon (or percentage * text), right-aligned in the bar. Returns the left edge of the battery diff --git a/tests/ascii-art.test.cjs b/tests/ascii-art.test.cjs new file mode 100644 index 00000000..85e48a78 --- /dev/null +++ b/tests/ascii-art.test.cjs @@ -0,0 +1,19 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { imageFromAsciiArt } = require('../.test-build/app/graphics/image.js'); + +test('imageFromAsciiArt maps characters through the palette', () => { + const image = imageFromAsciiArt(['#/.', ' _#'], { '#': 255, '/': 128, '_': 64 }); + assert.equal(image.width, 3); + assert.equal(image.height, 2); + assert.deepEqual([...image.pixels], [255, 128, 0, 0, 64, 255]); +}); + +test('imageFromAsciiArt pads short lines and lets the palette override the background', () => { + const image = imageFromAsciiArt(['.#', '.'], { '#': 300, '.': 10 }); + assert.deepEqual([...image.pixels], [10, 255, 10, 0]); +}); + +test('imageFromAsciiArt rejects characters missing from the palette', () => { + assert.throws(() => imageFromAsciiArt(['#x'], { '#': 255 }), /"x"/); +}); diff --git a/tests/dense-batteries.test.cjs b/tests/dense-batteries.test.cjs new file mode 100644 index 00000000..0d446718 --- /dev/null +++ b/tests/dense-batteries.test.cjs @@ -0,0 +1,87 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { GrayImage } = require('../.test-build/app/graphics/image.js'); +const { + BATTERY_ICON_WIDTH, + DENSE_BATTERY_BLOCK_HEIGHT, + drawBattery, + drawDenseBatteries, +} = require('../.test-build/app/graphics/battery.js'); + +const RIGHT = 120; +const TOP = 4; +const LOWER_ROW = DENSE_BATTERY_BLOCK_HEIGHT - drawBattery(0, false).height; + +/** Every top-left position where exactly this gauge was drawn. */ +function findGauges(image, percent, charging) { + const gauge = drawBattery(percent, charging); + const found = []; + for (let y = 0; y + gauge.height <= image.height; y++) { + for (let x = 0; x + gauge.width <= image.width; x++) { + let match = true; + for (let gy = 0; match && gy < gauge.height; gy++) { + for (let gx = 0; match && gx < gauge.width; gx++) { + match = image.pixels[(y + gy) * image.width + x + gx] === gauge.pixels[gy * gauge.width + gx]; + } + } + if (match) found.push({ x, y }); + } + } + return found; +} + +function hasInk(image, x0, x1, y0, y1) { + for (let y = y0; y < y1; y++) { + for (let x = x0; x < x1; x++) { + if (image.pixels[y * image.width + x] > 0) return true; + } + } + return false; +} + +function gaugeAt(image, percent, charging = false) { + const found = findGauges(image, percent, charging); + assert.equal(found.length, 1, `one ${percent}% gauge`); + return found[0]; +} + +test('dense batteries stack two per column, right-aligned', () => { + const image = new GrayImage(RIGHT + 8, 32, 0); + const left = drawDenseBatteries(image, [ + { device: 'phone', percent: 80, charging: false }, + { device: 'watch', percent: 35, charging: true }, + { device: 'glasses', percent: 60, charging: false }, + { device: 'ring', percent: 100, charging: false }, + ], RIGHT, TOP); + const phone = gaugeAt(image, 80); + const watch = gaugeAt(image, 35, true); + const glasses = gaugeAt(image, 60); + const ring = gaugeAt(image, 100); + assert.deepEqual(glasses, { x: RIGHT - BATTERY_ICON_WIDTH, y: TOP }); + assert.deepEqual(ring, { x: RIGHT - BATTERY_ICON_WIDTH, y: TOP + LOWER_ROW }); + assert.equal(phone.y, TOP); + assert.deepEqual(watch, { x: phone.x, y: TOP + LOWER_ROW }); + // Each gauge has its device icon to its left. + assert.ok(hasInk(image, left, phone.x, TOP, TOP + 10)); + assert.ok(hasInk(image, left, watch.x, TOP + LOWER_ROW, TOP + LOWER_ROW + 10)); + assert.ok(hasInk(image, phone.x + BATTERY_ICON_WIDTH, glasses.x, TOP, TOP + 10)); + assert.ok(hasInk(image, watch.x + BATTERY_ICON_WIDTH, ring.x, TOP + LOWER_ROW, TOP + LOWER_ROW + 10)); +}); + +test('an odd count leaves the first indicator alone, vertically centered', () => { + const image = new GrayImage(RIGHT + 8, 32, 0); + drawDenseBatteries(image, [ + { device: 'phone', percent: 80, charging: false }, + { device: 'glasses', percent: 60, charging: false }, + { device: 'ring', percent: 20, charging: false }, + ], RIGHT, TOP); + assert.equal(gaugeAt(image, 80).y, TOP + LOWER_ROW / 2); + assert.deepEqual(gaugeAt(image, 60), { x: RIGHT - BATTERY_ICON_WIDTH, y: TOP }); + assert.deepEqual(gaugeAt(image, 20), { x: RIGHT - BATTERY_ICON_WIDTH, y: TOP + LOWER_ROW }); +}); + +test('no indicators draws nothing', () => { + const image = new GrayImage(RIGHT + 8, 32, 0); + assert.equal(drawDenseBatteries(image, [], RIGHT, TOP), RIGHT); + assert.ok(image.pixels.every((value) => value === 0)); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index fe42d759..1adf047c 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -20,6 +20,7 @@ "../app/apps/terminal/hub-header.ts", "../app/g2/ios-peripheral-identity.ts", "../app/graphics/image.ts", + "../app/graphics/battery.ts", "../app/graphics/plane.ts", "../app/graphics/shell-scene.ts", "../app/ui/metrics.ts",