Glanceboard: Adjust styling; show timers on system card

This commit is contained in:
James Babcock committed 2026-09-11 19:10:26 -07:00
1 parent f0da7382e5
commit cd1e815174
3 files changed
+105 -9

No files matched your search

+38 -4
View File
@@ -1,4 +1,5 @@
import { type GrayImage } from "../../../graphics/image";
import { GrayImage } from "../../../graphics/image";
import { renderIcon } from "../../../graphics/icons";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { truncateText } from "../../../graphics/textwrap";
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
@@ -7,6 +8,33 @@ import { type GlanceWidget } from "../widget";
const PAD = 8;
const ART_SIZE = 96;
/** The idle note: the Music app's icon, at this size and the dimmest shade. */
const IDLE_NOTE_SIZE = 64;
const IDLE_NOTE_VALUE = 20;
let idleNote: GrayImage | null | undefined;
/**
* The music icon reduced to a single dim shade: every pixel the renderer
* lit at least half becomes IDLE_NOTE_VALUE, the rest stay transparent
* (scaling the antialiased original down would quantize its edges away).
* Built once and kept, so drawImage's content-addressed cache holds.
*/
function idleNoteImage(): GrayImage | null {
if (idleNote !== undefined) return idleNote;
const icon = renderIcon("music", IDLE_NOTE_SIZE);
if (!icon) {
idleNote = null;
return null;
}
const note = new GrayImage(icon.width, icon.height, 0);
for (let y = 0; y < icon.height; y++) {
for (let x = 0; x < icon.width; x++) {
if (icon.getPixel(x, y) >= 128) note.setPixel(x, y, IDLE_NOTE_VALUE);
}
}
idleNote = note;
return note;
}
const ART_X = PAD;
const PROGRESS_BAR_HEIGHT = 5;
@@ -52,9 +80,15 @@ export class MusicWidget implements GlanceWidget {
const media = mediaControllerBridge.snapshot();
this.media = media;
if (!media.available || (!media.title && !media.artist)) {
image.drawText(small, PAD, PAD, "Music", 150);
const text = media.accessEnabled ? "Nothing playing" : "Notification access needed";
image.drawText(small, PAD, PAD + small.lineHeight + 6, text, 140);
if (!media.accessEnabled) {
// Actionable, so it stays as text.
image.drawText(small, PAD, PAD, "Music", 150);
image.drawText(small, PAD, PAD + small.lineHeight + 6, "Notification access needed", 140);
return;
}
// Nothing playing: a dim note, centred, and nothing else.
const note = idleNoteImage();
if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0);
return;
}
+62 -5
View File
@@ -15,6 +15,10 @@ import {
} from "../../../ui/dashboard-settings";
import { shell } from "../../../ui/shell/shell";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
import { truncateText } from "../../../graphics/textwrap";
import { timerEngine } from "../../timer/timer-engine";
import { formatCountdown, sortTimers, timerDisplayName, timerPhase, timerRemainingMs } from "../../timer/timer-model";
import { lineStep } from "../../../ui/metrics";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -26,32 +30,55 @@ const BATTERY_LABEL_VALUE = 150;
type BatteryItem = { label: string; percent: number; charging: boolean };
/**
* Date and time, the phone's notification icons, and the phone/G2/R1
* battery indicators: the top bar's contents, laid out for a card. Battery
* style and per-device visibility follow Settings > Display > Battery
* indicators, so the card agrees with the bar.
* Date and time, the phone's notification icons, the phone/G2/R1 battery
* indicators, and the Timers app's countdowns: the top bar's contents plus
* timers, laid out for a card. Battery style and per-device visibility
* follow Settings > Display > Battery indicators, so the card agrees with
* the bar. Countdowns tick once a second only while one is running.
*/
export class SystemCardWidget implements GlanceWidget {
private requestRender: (() => void) | null = null;
private minuteTimer: ReturnType<typeof setTimeout> | null = null;
private secondTimer: ReturnType<typeof setInterval> | null = null;
private unsubscribeNotifications: (() => void) | null = null;
private unsubscribeSettings: (() => void) | null = null;
private unsubscribeTimers: (() => void) | null = null;
start(requestRender: () => void): void {
this.requestRender = requestRender;
this.scheduleMinuteTick();
this.unsubscribeNotifications = onAndroidNotificationPosted(() => this.requestRender?.());
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
this.unsubscribeTimers = timerEngine.onChange(() => {
this.syncSecondTick();
this.requestRender?.();
});
this.syncSecondTick();
}
stop(): void {
this.requestRender = null;
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
this.minuteTimer = null;
if (this.secondTimer !== null) clearInterval(this.secondTimer);
this.secondTimer = null;
this.unsubscribeNotifications?.();
this.unsubscribeNotifications = null;
this.unsubscribeSettings?.();
this.unsubscribeSettings = null;
this.unsubscribeTimers?.();
this.unsubscribeTimers = null;
}
/** Countdowns change every second; run that clock only while one is running. */
private syncSecondTick(): void {
const running = timerEngine.timers().some((timer) => timerPhase(timer) === "running");
if (running && this.secondTimer === null) {
this.secondTimer = setInterval(() => this.requestRender?.(), 1_000);
} else if (!running && this.secondTimer !== null) {
clearInterval(this.secondTimer);
this.secondTimer = null;
}
}
/** Repaint on the minute boundary, so the clock never shows a stale minute. */
@@ -82,9 +109,12 @@ export class SystemCardWidget implements GlanceWidget {
image.drawText(timeFont, PAD, y, timeText, 230);
y += timeFont.lineHeight + 2;
image.drawText(medium, PAD, y, formatClockDate(now), 170);
y += medium.lineHeight + 4;
// Notification icons along the bottom, as many as fit.
// Notification icons along the bottom, as many as fit; the Timers app's
// countdowns take the band between the date and the icons.
const iconY = image.height - PAD - NOTIFICATION_ICON_SIZE;
drawTimers(image, small, PAD, y, textWidth, iconY - 4, now.getTime());
const maxIcons = Math.max(0, ((image.width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
if (maxIcons > 0) {
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
@@ -171,3 +201,30 @@ function drawBatteryValue(
const icon = drawBattery(item.percent, item.charging);
image.bitBlt(icon, x, lineTop + Math.max(0, ((font.lineHeight - gaugeHeight) / 2) | 0), { transparentZero: true });
}
/**
* The Timers app's countdowns, soonest first: remaining time (or "Done" once
* rung, inverted so it is noticed; "paused" for a paused one) and the
* timer's name, one per line while they fit between `top` and `bottom`.
*/
function drawTimers(image: GrayImage, font: UiFont, x: number, top: number, width: number, bottom: number, nowMs: number): void {
const timers = sortTimers(timerEngine.timers(), nowMs);
const step = lineStep(font);
let y = top;
for (const timer of timers) {
if (y + font.lineHeight > bottom) break;
const phase = timerPhase(timer);
const clock = phase === "rung" ? "Done" : formatCountdown(timerRemainingMs(timer, nowMs));
const clockWidth = font.measureText(clock);
if (phase === "rung") {
image.fillRect(x - 2, y - 1, clockWidth + 4, font.lineHeight + 2, 230);
image.drawText(font, x, y, clock, 1);
} else {
image.drawText(font, x, y, clock, phase === "paused" ? 120 : 200);
}
const nameX = x + clockWidth + 8;
const name = phase === "paused" ? `${timerDisplayName(timer)} (paused)` : timerDisplayName(timer);
image.drawText(font, nameX, y, truncateText(font, name, Math.max(0, x + width - nameX)), 160);
y += step;
}
}
+5
View File
@@ -266,6 +266,11 @@ export class TimerEngine {
);
}
/** Every countdown timer (running, paused or rung), unsorted. */
timers(): readonly CountdownTimer[] {
return this.state.timers;
}
ringingItems(): RingingItem[] {
const items: RingingItem[] = [];
for (const timer of this.state.timers) {