Glanceboard has separately configurable font

This commit is contained in:
James Babcock committed 2026-09-30 21:10:11 -07:00
1 parent def027943c
commit 4bc3551135
12 files changed
+187 -65

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
import { UiFontOverride } from "../../graphics/ui-fonts";
/**
* The font Glanceboard widgets paint with: the Glanceboard's own Font
* setting, or the Display UI font while that is unset. Its small, medium and
* large roles carry the same line-height guarantees as getDefault*Font.
*/
export const glanceFont = new UiFontOverride("glanceboard.font");
+8 -1
View File
@@ -2,6 +2,7 @@ import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
@@ -10,6 +11,7 @@ import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { GlanceBoard } from "./board";
import { glanceFont } from "./glance-font";
import {
clearConflictingSlots,
glanceboardEnabledSetting,
@@ -298,7 +300,7 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
}
/** Settings: layout, sleep gestures, duration, slot lines, and stereo depth. */
/** Settings: layout, sleep gestures, duration, slot lines, widget font, and stereo depth. */
function openGlanceSettings(ctx: LayerContext): void {
const items: MenuItem[] = [
enumSettingMenuItem(glanceLayoutSetting),
@@ -306,6 +308,11 @@ function openGlanceSettings(ctx: LayerContext): void {
toggleSettingMenuItem(glanceShowOnLongPressSetting),
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
toggleSettingMenuItem(glanceShowLinesSetting),
uiFontOverridePickerMenuItem(glanceFont, {
title: "Glanceboard font",
description:
"Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Display.",
}),
enumSettingMenuItem(glanceDepthSetting),
];
ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width)));
@@ -1,10 +1,10 @@
import { type GrayImage, type UiFont } from "../../../graphics/image";
import { truncateText } from "../../../graphics/textwrap";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { hasCalendarPermission } from "../../../native/calendar-permissions";
import { getCalendarReadState, onCalendarChanged, readUpcomingEvents, type CalendarEvent } from "../../../native/calendar";
import { lineStep } from "../../../ui/metrics";
import { dayHeaderLabel, formatEventTime } from "../../calendar/calendar";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -38,8 +38,8 @@ export class CalendarWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const small = glanceFont.small();
const medium = glanceFont.medium();
const step = lineStep(small);
const textWidth = image.width - 2 * PAD;
@@ -1,11 +1,11 @@
import { type GrayImage } from "../../../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { hasLocationPermission } from "../../../native/location-permissions";
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
import { getNorthReference, resolveHeading } from "../../compass/heading";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
/** Owner token for the refcounted magnetometer, distinct from the Compass app's. */
@@ -63,8 +63,8 @@ export class CompassWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const medium = glanceFont.medium();
const small = glanceFont.small();
const { width, height } = image;
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
+4 -4
View File
@@ -1,9 +1,9 @@
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";
import { clamp } from "../../../util/numeric-util";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -75,8 +75,8 @@ export class MusicWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const medium = glanceFont.medium();
const small = glanceFont.small();
const media = mediaControllerBridge.snapshot();
this.media = media;
if (!media.available || (!media.title && !media.artist)) {
@@ -142,7 +142,7 @@ export class MusicWidget implements GlanceWidget {
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
} else {
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
const font = getDefaultSmallFont();
const font = glanceFont.small();
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
}
@@ -1,5 +1,4 @@
import { GrayImage } from "../../../graphics/image";
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { truncateText } from "../../../graphics/textwrap";
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
import { isNightscoutSettingsConfigured, loadNightscoutThresholds } from "../../../ui/dashboard-settings";
@@ -13,6 +12,7 @@ import {
formatDelta,
isNightscoutPointStale,
} from "../../nightscout/nightscout";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -48,8 +48,8 @@ export class NightscoutWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const small = getDefaultSmallFont();
const large = getDefaultLargeFont();
const small = glanceFont.small();
const large = glanceFont.large();
const state = this.state;
const nowMs = Date.now();
const step = lineStep(small);
+4 -4
View File
@@ -1,6 +1,5 @@
import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery";
import { type GrayImage, type UiFont } from "../../../graphics/image";
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
import { readPhoneBatteryState } from "../../../native/phone-battery";
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
@@ -20,6 +19,7 @@ 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 { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -97,9 +97,9 @@ export class SystemCardWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const large = getDefaultLargeFont();
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const large = glanceFont.large();
const medium = glanceFont.medium();
const small = glanceFont.small();
const now = new Date();
// Clock and date down the left; batteries take the right edge.
@@ -1,6 +1,5 @@
import { type GrayImage } from "../../../graphics/image";
import { truncateText } from "../../../graphics/textwrap";
import { getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { lineStep } from "../../../ui/metrics";
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
import {
@@ -10,6 +9,7 @@ import {
TERMINAL_SESSIONS_STATE_KEY,
type TerminalSessionsSnapshot,
} from "../../terminal/session-list";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -72,7 +72,7 @@ export class TerminalWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const font = getDefaultSmallFont();
const font = glanceFont.small();
const step = lineStep(font);
const textWidth = image.width - 2 * PAD;
const snapshot = this.snapshot;
+78 -24
View File
@@ -111,21 +111,49 @@ export function fontSelectionLabel(selection: UiFontSelection): string {
// --- Resolution, cached per isolate ---
const resolved = new Map<string, UiFont>();
let invalidationInstalled = false;
type UiFontRole = "small" | "medium" | "large";
function cacheKey(role: string): string {
return role;
const UI_FONT_KEYS: readonly string[] = [UI_FONT_SELECTION_KEY, LEGACY_UI_FONT_KEY];
/**
* The three role fonts for one selection source, resolved lazily and cached
* until a change to any of `keys` (the settings the selection reads).
*/
class RoleFonts {
private readonly resolved = new Map<UiFontRole, UiFont>();
constructor(
readonly keys: readonly string[],
private readonly selection: () => UiFontSelection,
) {}
get(role: UiFontRole): UiFont {
installInvalidation();
const cached = this.resolved.get(role);
if (cached) return cached;
const font = resolveRole(this.selection(), role);
this.resolved.set(role, font);
return font;
}
clear(): void {
this.resolved.clear();
}
}
const roleFontSets: RoleFonts[] = [];
const uiRoleFonts = new RoleFonts(UI_FONT_KEYS, getUiFontSelection);
roleFontSets.push(uiRoleFonts);
let invalidationInstalled = false;
function installInvalidation(): void {
if (invalidationInstalled) return;
invalidationInstalled = true;
onSettingsStoreChanged((key) => {
if (key === UI_FONT_SELECTION_KEY || key === LEGACY_UI_FONT_KEY || key === TERMINAL_FONT_SELECTION_KEY) {
resolved.clear();
cachedTerminalConfig = null;
for (const fonts of roleFontSets) {
if (fonts.keys.includes(key)) fonts.clear();
}
if (key === TERMINAL_FONT_SELECTION_KEY) cachedTerminalConfig = null;
});
}
@@ -148,46 +176,72 @@ function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | nu
return null;
}
function resolveRole(role: "small" | "medium" | "large"): UiFont {
installInvalidation();
const key = cacheKey(role);
const cached = resolved.get(key);
if (cached) return cached;
const selection = getUiFontSelection();
let font: UiFont | null = null;
function resolveRole(selection: UiFontSelection, role: UiFontRole): UiFont {
// Enforce the guaranteed small-font range even against a stale or
// hand-edited selection: an out-of-range selection falls back to bitmap for
// every role (medium/large derive from small, so their documented ranges
// follow and the roles never mix faces).
if (selection.kind === "ttf" && uiFontSizeAllowed(installedFontPath(selection.file), selection.size)) {
font =
const font =
role === "small"
? resolveTtf(selection)
: resolveTtf(selection, role === "medium" ? MEDIUM_MIN_LINE_HEIGHT : LARGE_MIN_LINE_HEIGHT);
if (font) return font;
}
if (!font) {
font = bitmapForRole(selection.kind === "bitmap" ? selection.face : "terminus", role);
}
resolved.set(key, font);
return font;
return bitmapForRole(selection.kind === "bitmap" ? selection.face : "terminus", role);
}
// Only the small (12px) bitmap font has a TerminusV alternative (it ships no
// larger sizes); bitmap medium/large stay Terminus, as before the picker.
function bitmapForRole(face: BitmapFace, role: "small" | "medium" | "large"): BdfFont {
function bitmapForRole(face: BitmapFace, role: UiFontRole): BdfFont {
if (role === "medium") return getFont("terminus16");
if (role === "large") return getFont("terminus24");
return getFont(face === "terminusv" ? "terminusv12" : "terminus12");
}
export function getDefaultSmallFont(): UiFont {
return resolveRole("small");
return uiRoleFonts.get("small");
}
export function getDefaultMediumFont(): UiFont {
return resolveRole("medium");
return uiRoleFonts.get("medium");
}
export function getDefaultLargeFont(): UiFont {
return resolveRole("large");
return uiRoleFonts.get("large");
}
/**
* A per-app UI font that defaults to the Display UI font: a selection stored
* as JSON under `storageKey`, or nothing to inherit. Its small/medium/large
* fonts derive from the selection exactly as the UI font's roles do, with
* the same line-height guarantees, so layouts written against
* getDefault*Font work unchanged.
*/
export class UiFontOverride {
private readonly fonts: RoleFonts;
constructor(readonly storageKey: string) {
this.fonts = new RoleFonts([storageKey, ...UI_FONT_KEYS], () => this.getSelection() ?? getUiFontSelection());
roleFontSets.push(this.fonts);
}
/** The app's own selection, or null when it inherits the UI font. */
getSelection(): UiFontSelection | null {
return parseFontSelection(getStringSetting(this.storageKey, ""));
}
setSelection(selection: UiFontSelection | null): void {
setStringSetting(this.storageKey, selection ? JSON.stringify(selection) : "");
}
small(): UiFont {
return this.fonts.get("small");
}
medium(): UiFont {
return this.fonts.get("medium");
}
large(): UiFont {
return this.fonts.get("large");
}
}
// --- Terminal font ---
+72 -20
View File
@@ -4,8 +4,9 @@
* styles), and a pixel size, with a live preview line rendered in the draft
* font. Selections only persist on Save; double-click cancels.
*
* Used for both the UI font (Display section) and the terminal font
* (Terminal section, filtered to monospace faces).
* Used for the UI font (Display section), the terminal font (Terminal
* section, filtered to monospace faces), and per-app overrides of the UI font
* (UiFontOverride, which add an "inherit" face ahead of the others).
*/
import { getFont } from "../graphics/bdffont";
import { GrayImage, type UiFont } from "../graphics/image";
@@ -20,6 +21,7 @@ import {
setTerminalFontSelection,
setUiFontSelection,
type BitmapFace,
type UiFontOverride,
type UiFontSelection,
} from "../graphics/ui-fonts";
import { type InputEvent } from "./gestures";
@@ -31,6 +33,11 @@ import type { Layer, LayerContext } from "./layers";
const SIZE_CHOICES = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28] as const;
const DEFAULT_TTF_SIZE = 16;
const PREVIEW_TEXT = "The quick brown fox jumps over 0123456789";
const INHERIT_UI_FONT_LABEL = "Same as UI font";
const UI_BITMAP_FACES: FontPickerOptions["bitmapFaces"] = [
{ face: "terminus", label: "Terminus" },
{ face: "terminusv", label: "TerminusV" },
];
type FontPickerOptions = {
title: string;
@@ -43,8 +50,12 @@ type FontPickerOptions = {
sizeAllowed?: (path: string, size: number) => boolean;
/** Bitmap faces offered ahead of the installed TTF families. */
bitmapFaces: readonly { face: BitmapFace; label: string }[];
get(): UiFontSelection;
set(selection: UiFontSelection): void;
/** When set, the first face choice is this label for "no selection of
* its own" (get/set null), which previews and stores as inheriting. */
inheritLabel?: string;
/** Null (inherit) only when inheritLabel is set. */
get(): UiFontSelection | null;
set(selection: UiFontSelection | null): void;
};
type FontFamily = {
@@ -52,8 +63,9 @@ type FontFamily = {
fonts: InstalledFont[];
};
/** The draft being edited: a bitmap face, or a TTF family + style + size. */
/** The draft being edited: inherit, a bitmap face, or a TTF family + style + size. */
type Draft =
| { kind: "inherit" }
| { kind: "bitmap"; face: BitmapFace }
| { kind: "ttf"; family: FontFamily; font: InstalledFont; size: number };
@@ -77,7 +89,8 @@ export class FontPickerLayer implements Layer {
this.draft = this.draftFromSelection(options.get());
}
private draftFromSelection(selection: UiFontSelection): Draft {
private draftFromSelection(selection: UiFontSelection | null): Draft {
if (selection === null) return { kind: "inherit" };
if (selection.kind === "ttf") {
for (const family of this.families) {
const font = family.fonts.find((f) => f.fileName === selection.file);
@@ -91,14 +104,20 @@ export class FontPickerLayer implements Layer {
return { kind: "bitmap", face };
}
private draftSelection(): UiFontSelection {
return this.draft.kind === "bitmap"
? { kind: "bitmap", face: this.draft.face }
: { kind: "ttf", file: this.draft.font.fileName, size: this.draft.size };
private draftSelection(): UiFontSelection | null {
switch (this.draft.kind) {
case "inherit":
return null;
case "bitmap":
return { kind: "bitmap", face: this.draft.face };
case "ttf":
return { kind: "ttf", file: this.draft.font.fileName, size: this.draft.size };
}
}
/** The draft's concrete font for the preview (bitmap fallback if unloadable). */
private previewFont(): UiFont {
if (this.draft.kind === "inherit") return getDefaultSmallFont();
if (this.draft.kind === "ttf") {
const font = TtfFont.load(this.draft.font.path, this.draft.size);
if (font) return font;
@@ -154,18 +173,19 @@ export class FontPickerLayer implements Layer {
}
private rowDisabled(row: RowId): boolean {
return (row === "weight" || row === "size") && this.draft.kind === "bitmap";
return (row === "weight" || row === "size") && this.draft.kind !== "ttf";
}
private rowValue(row: RowId): string {
if (row === "face") {
if (this.draft.kind === "inherit") return this.options.inheritLabel ?? "";
if (this.draft.kind === "bitmap") {
return this.options.bitmapFaces.find((b) => b.face === (this.draft as { face: BitmapFace }).face)?.label
?? "Terminus";
}
return this.draft.family.label;
}
if (this.draft.kind === "bitmap") return "-";
if (this.draft.kind !== "ttf") return "-";
if (row === "weight") return this.draft.font.style || "Regular";
return String(this.draft.size);
}
@@ -198,6 +218,16 @@ export class FontPickerLayer implements Layer {
private openFaceMenu(ctx: LayerContext): void {
const items: MenuItem[] = [];
const inheritLabel = this.options.inheritLabel;
if (inheritLabel !== undefined) {
items.push({
label: inheritLabel,
onSelect: (innerCtx) => {
this.draft = { kind: "inherit" };
innerCtx.stack.pop();
},
});
}
for (const bitmap of this.options.bitmapFaces) {
items.push({
label: `${bitmap.label} (bitmap)`,
@@ -216,10 +246,13 @@ export class FontPickerLayer implements Layer {
},
});
}
const inheritRows = inheritLabel !== undefined ? 1 : 0;
const currentIndex =
this.draft.kind === "bitmap"
? Math.max(0, this.options.bitmapFaces.findIndex((b) => b.face === (this.draft as { face: BitmapFace }).face))
: this.options.bitmapFaces.length + this.families.indexOf(this.draft.family);
this.draft.kind === "inherit"
? 0
: this.draft.kind === "bitmap"
? inheritRows + Math.max(0, this.options.bitmapFaces.findIndex((b) => b.face === (this.draft as { face: BitmapFace }).face))
: inheritRows + this.options.bitmapFaces.length + this.families.indexOf(this.draft.family);
openModalMenu(ctx, "Font Face", items, Math.max(0, currentIndex));
}
@@ -306,15 +339,33 @@ export function uiFontPickerMenuItem(): MenuItem {
title: "UI font",
monospaceOnly: false,
sizeAllowed: uiFontSizeAllowed,
bitmapFaces: [
{ face: "terminus", label: "Terminus" },
{ face: "terminusv", label: "TerminusV" },
],
bitmapFaces: UI_BITMAP_FACES,
get: getUiFontSelection,
set: setUiFontSelection,
});
}
/**
* Settings row for an app's own UI font, defaulting to the Display UI font
* (same faces and size bounds as the UI font picker).
*/
export function uiFontOverridePickerMenuItem(
override: UiFontOverride,
options: { title: string; description: string },
): MenuItem {
return fontPickerMenuItem({
rowLabel: "Font",
description: options.description,
title: options.title,
monospaceOnly: false,
sizeAllowed: uiFontSizeAllowed,
bitmapFaces: UI_BITMAP_FACES,
inheritLabel: INHERIT_UI_FONT_LABEL,
get: () => override.getSelection(),
set: (selection) => override.setSelection(selection),
});
}
/** Settings row for the terminal font (monospace faces only). */
export function terminalFontPickerMenuItem(): MenuItem {
return fontPickerMenuItem({
@@ -337,6 +388,7 @@ function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; des
ctx.stack.push(new FontPickerLayer(options));
},
render: ({ image, x, y, width }) => {
const selection = options.get();
drawRightValueMenuItem(
image,
getDefaultSmallFont(),
@@ -344,7 +396,7 @@ function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; des
y,
width,
options.rowLabel,
fontSelectionLabel(options.get()),
selection ? fontSelectionLabel(selection) : options.inheritLabel ?? "",
);
},
};
+1
View File
@@ -166,6 +166,7 @@ function screenFixture() {
const calendar = load('app/apps/calendar/calendar.ts', modules('../../'));
const widget = load('app/apps/glanceboard/widgets/calendar-widget.ts', {
...modules('../../../'), '../../calendar/calendar': calendar,
'../glance-font': { glanceFont: { small: () => font, medium: () => font } },
}, { ...f.clock, setInterval: fn => { timers.set(++nextTimer, fn); return nextTimer; }, clearInterval: id => timers.delete(id) });
return { ...f, ...calendar, ...widget, timers, Image };
}
+1 -1
View File
@@ -73,7 +73,7 @@ function paintWidget(smallHeight, largeHeight, height = 144, measurements = {})
};
const { NightscoutWidget } = load('app/apps/glanceboard/widgets/nightscout-widget.ts', {
'../../../graphics/image': { GrayImage },
'../../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => ({ ...small, lineHeight: largeHeight }) },
'../glance-font': { glanceFont: { small: () => small, large: () => ({ ...small, lineHeight: largeHeight }) } },
'../../../graphics/textwrap': require('../.test-build/app/graphics/textwrap.js'),
'../../../native/nightscout-bridge': { nightscoutBridge: { snapshot: () => state } },
'../../../ui/dashboard-settings': { isNightscoutSettingsConfigured: () => true, loadNightscoutThresholds: () => limits },