Add Dense style for battery indicators

This commit is contained in:
James Babcock committed 2026-10-01 22:56:09 -07:00
1 parent 46fded856a
commit 566592c074
10 files changed
+309 -39

No files matched your search

+18 -10
View File
@@ -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;
+1 -1
View File
@@ -60,7 +60,7 @@ const TRAY_ICON = imageFromAsciiArt(
" # # ",
" ### ### ",
],
220,
{ "#": 220 },
);
const MENU_LAYOUT = {
+1 -1
View File
@@ -32,7 +32,7 @@ const MIC_ICON = imageFromAsciiArt(
" # ",
" ###### ",
],
220,
{ "#": 220 },
);
export type TranscribeAppOptions = InProcessAppOptions & {
+146 -9
View File
@@ -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<Record<BatteryDevice, GrayImage>> = {
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();
+12 -5
View File
@@ -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<Record<string, number>>;
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;
+4 -3
View File
@@ -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<BatteryDisplayMod
label: "Style",
storageKey: "dashboard.systemCard.batteryDisplayMode",
defaultValue: "stacked",
values: ["icon", "percentage", "stacked", "stacked-percentage"],
values: ["icon", "percentage", "stacked", "stacked-percentage", "dense"],
formatValue: batteryDisplayModeLabel,
description: "How the top bar shows battery levels: a gauge icon or exact percentage beside the label, or a compact gauge or percentage with the label stacked above it.",
description: "How the top bar shows battery levels: a gauge icon or exact percentage beside the label, a compact gauge or percentage with the label stacked above it, or (dense) a device icon beside each gauge with two indicators stacked per column.",
});
/** Below this charge level a "Below 50%" indicator becomes visible. */
@@ -1062,6 +1062,7 @@ export function batteryDisplayModeLabel(value: BatteryDisplayMode): string {
if (value === "percentage") return "Percentage";
if (value === "stacked") return "Stacked";
if (value === "stacked-percentage") return "Stacked percentage";
if (value === "dense") return "Dense";
return "Icon";
}
+20 -10
View File
@@ -4,7 +4,7 @@ import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
import { activeAmbientCards } from "./ambient-cards";
import { BATTERY_ICON_WIDTH, drawBattery } from "../../graphics/battery";
import { BATTERY_ICON_WIDTH, DENSE_BATTERY_BLOCK_HEIGHT, drawBattery, drawDenseBatteries, type BatteryDevice } from "../../graphics/battery";
import { readActiveNotificationIcons } from "../../native/notification-icons";
import { readPhoneBatteryState } from "../../native/phone-battery";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../util/render-freshness";
@@ -377,34 +377,39 @@ export class ShellChromeLayer implements Layer {
* Labelled battery indicators for the phone, Wear OS watch, G2, and R1,
* right-aligned in the top bar. The watch one exists only while a watch
* running the Faceclaw watch app is reachable (no placeholder otherwise).
* The Settings > 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
+19
View File
@@ -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"/);
});
+87
View File
@@ -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));
});
+1
View File
@@ -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",