mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Most of the builtin apps had some padding between the window edge and their content, because they expect to be displayed with a line border on their edge. We added a customization option to remove the border, so, that padding is not always necessary. Make the layouts dynamically adjust, to omit the padding when it isn't needed.
437 lines
16 KiB
TypeScript
437 lines
16 KiB
TypeScript
/**
|
|
* Modal font picker for font settings: choose a face (bitmap Terminus
|
|
* variants or any installed TTF family), a weight (the family's installed
|
|
* 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 the UI font (Settings > Customization), the terminal font (the
|
|
* Terminal app's settings, 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";
|
|
import { TtfFont } from "../graphics/ttf-font";
|
|
import { listInstalledFonts, type InstalledFont } from "../graphics/installed-fonts";
|
|
import { wrapText } from "../graphics/textwrap";
|
|
import {
|
|
fontSelectionLabel,
|
|
getDefaultSmallFont,
|
|
uiFontSizeAllowed,
|
|
getTerminalFontSelection,
|
|
getUiFontSelection,
|
|
setTerminalFontSelection,
|
|
setUiFontSelection,
|
|
type BitmapFace,
|
|
type UiFontOverride,
|
|
type UiFontSelection,
|
|
} from "../graphics/ui-fonts";
|
|
import { type InputEvent } from "./gestures";
|
|
import { drawRightValueMenuItem, openModalMenu, type MenuItem } from "./menu";
|
|
import { Menu, type MenuDrawArgs } from "./menu-core";
|
|
import { LIST_ROW_TEXT_INSET, listRowHeight } from "./metrics";
|
|
import type { Layer, LayerContext } from "./layers";
|
|
|
|
/** Pixel sizes offered for TTF faces (also by the onboarding's text-size page). */
|
|
export const FONT_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;
|
|
/** Row text (and the title, in line with it) from the rows' highlight boxes. */
|
|
const ROW_TEXT_INSET = 10;
|
|
/** The title: below the window's top padding, and the gap under its line. */
|
|
const TITLE_OFFSET_Y = 2;
|
|
const TITLE_GAP = 10;
|
|
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;
|
|
/** Only offer faces where every style is monospace (terminal picker). */
|
|
monospaceOnly: boolean;
|
|
/** When set, sizes where this returns false are hidden (and drafts are
|
|
* clamped to an allowed size on face/weight changes). The UI-font picker
|
|
* uses it to keep getDefaultSmallFont's line height within its guaranteed
|
|
* 8..21px range; the terminal picker has no such bound. */
|
|
sizeAllowed?: (path: string, size: number) => boolean;
|
|
/** Bitmap faces offered ahead of the installed TTF families. */
|
|
bitmapFaces: readonly { face: BitmapFace; label: string }[];
|
|
/** 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 = {
|
|
label: string;
|
|
fonts: InstalledFont[];
|
|
};
|
|
|
|
/** 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 };
|
|
|
|
const ROWS = ["face", "weight", "size", "save"] as const;
|
|
type RowId = (typeof ROWS)[number];
|
|
|
|
export class FontPickerLayer implements Layer {
|
|
private readonly families: FontFamily[];
|
|
private draft: Draft;
|
|
private readonly menu = new Menu<RowId>({
|
|
items: ROWS,
|
|
wrap: true,
|
|
rowGap: 2,
|
|
highlight: { radius: 8 },
|
|
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
|
draw: (args) => this.drawRow(args),
|
|
});
|
|
|
|
constructor(private readonly options: FontPickerOptions) {
|
|
this.families = collectFamilies(options.monospaceOnly);
|
|
this.draft = this.draftFromSelection(options.get());
|
|
}
|
|
|
|
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);
|
|
if (font) return { kind: "ttf", family, font, size: this.clampSize(font, selection.size) };
|
|
}
|
|
}
|
|
const face =
|
|
selection.kind === "bitmap" && this.options.bitmapFaces.some((b) => b.face === selection.face)
|
|
? selection.face
|
|
: this.options.bitmapFaces[0]?.face ?? "terminus";
|
|
return { kind: "bitmap", face };
|
|
}
|
|
|
|
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;
|
|
}
|
|
return this.draft.kind === "bitmap" && this.draft.face === "terminusv"
|
|
? getFont("terminusv12")
|
|
: getFont("terminus12");
|
|
}
|
|
|
|
paint(ctx: LayerContext): GrayImage {
|
|
const font = getDefaultSmallFont();
|
|
const { width, height, padding } = ctx.stack.getBaseSize();
|
|
const image = new GrayImage(width, height, 0);
|
|
const focused = ctx.stack.isFocused();
|
|
// The rows' highlight boxes span the window inside the padding suggested
|
|
// for its sides; the title and the rows' text sit ROW_TEXT_INSET in.
|
|
const rowX = padding.left + ROW_TEXT_INSET;
|
|
const rowWidth = width - padding.left - padding.right - 2 * ROW_TEXT_INSET;
|
|
const titleY = padding.top + TITLE_OFFSET_Y;
|
|
image.drawText(font, rowX, titleY, this.options.title, 220);
|
|
|
|
const rowHeight = listRowHeight(font);
|
|
const rowsTop = titleY + font.lineHeight + TITLE_GAP;
|
|
const rowsHeight = ROWS.length * rowHeight;
|
|
const bottom = height - padding.bottom;
|
|
const preview = this.previewFont();
|
|
const info = `Line height: ${preview.lineHeight}px`;
|
|
|
|
// Preview under the rows, where its sample line and the info line fit
|
|
// (the G2's window): a separator, then the sample line in the draft font.
|
|
const previewTop = rowsTop + rowsHeight + 10;
|
|
if (previewTop + 8 + preview.lineHeight + 6 + font.lineHeight <= bottom) {
|
|
this.menu.paint(image, { x: rowX - ROW_TEXT_INSET, y: rowsTop, width: rowWidth + 2 * ROW_TEXT_INSET, height: rowsHeight }, focused);
|
|
image.drawLine(rowX - ROW_TEXT_INSET, previewTop, rowX + rowWidth + ROW_TEXT_INSET, previewTop, 60);
|
|
const sample =
|
|
preview.measureText(PREVIEW_TEXT) <= rowWidth + 2 * ROW_TEXT_INSET ? PREVIEW_TEXT : "The quick brown fox 0123";
|
|
preview.drawText(image, rowX - 4, previewTop + 8, sample, 230);
|
|
image.drawText(font, rowX - 4, previewTop + 8 + preview.lineHeight + 6, info, 110);
|
|
return image;
|
|
}
|
|
|
|
// Else beside them (a short window, as on the RayNeo iO), the sample
|
|
// wrapped to the column, as many lines as fit above the info line.
|
|
const columnWidth = Math.round(rowWidth / 2);
|
|
this.menu.paint(image, { x: rowX - ROW_TEXT_INSET, y: rowsTop, width: columnWidth + 2 * ROW_TEXT_INSET, height: Math.min(rowsHeight, bottom - rowsTop) }, focused);
|
|
const dividerX = rowX + columnWidth + 2 * ROW_TEXT_INSET;
|
|
image.drawLine(dividerX, rowsTop, dividerX, Math.min(rowsTop + rowsHeight, bottom), 60);
|
|
const previewX = dividerX + 12;
|
|
const lines = wrapText(preview, PREVIEW_TEXT, width - padding.right - previewX);
|
|
const infoY = bottom - font.lineHeight;
|
|
let y = rowsTop + 4;
|
|
for (const line of lines) {
|
|
if (y + preview.lineHeight > infoY - 6) break;
|
|
preview.drawText(image, previewX, y, line, 230);
|
|
y += preview.lineHeight;
|
|
}
|
|
image.drawText(font, previewX, Math.min(infoY, y + 6), info, 110);
|
|
return image;
|
|
}
|
|
|
|
/** Row box = highlight box; text sits ROW_TEXT_INSET in from its left and right edges. */
|
|
private drawRow({ image, item: row, x, y, width, selected }: MenuDrawArgs<RowId>): void {
|
|
const font = getDefaultSmallFont();
|
|
const textX = x + ROW_TEXT_INSET;
|
|
const textWidth = width - 2 * ROW_TEXT_INSET;
|
|
if (row === "save") {
|
|
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, "Save", selected ? 255 : 200);
|
|
return;
|
|
}
|
|
const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size";
|
|
const value = this.rowValue(row);
|
|
if (this.rowDisabled(row)) {
|
|
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, label, 70);
|
|
image.drawText(font, textX + textWidth - font.measureText(value) - 2, y + LIST_ROW_TEXT_INSET, value, 70);
|
|
} else {
|
|
drawRightValueMenuItem(image, font, textX, y, textWidth, label, value);
|
|
}
|
|
}
|
|
|
|
private rowDisabled(row: RowId): boolean {
|
|
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 !== "ttf") return "-";
|
|
if (row === "weight") return this.draft.font.style || "Regular";
|
|
return String(this.draft.size);
|
|
}
|
|
|
|
handleInput(event: InputEvent, ctx: LayerContext): void {
|
|
switch (event.type) {
|
|
case "scroll-up":
|
|
case "scroll-down":
|
|
void this.menu.handleInput(event);
|
|
return;
|
|
case "click": {
|
|
const row = this.menu.selectedItem;
|
|
if (!row || this.rowDisabled(row)) return;
|
|
if (row === "face") this.openFaceMenu(ctx);
|
|
else if (row === "weight") this.openWeightMenu(ctx);
|
|
else if (row === "size") this.openSizeMenu(ctx);
|
|
else {
|
|
this.options.set(this.draftSelection());
|
|
ctx.stack.pop();
|
|
}
|
|
return;
|
|
}
|
|
case "double-click":
|
|
ctx.stack.pop();
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
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)`,
|
|
onSelect: (innerCtx) => {
|
|
this.draft = { kind: "bitmap", face: bitmap.face };
|
|
innerCtx.stack.pop();
|
|
},
|
|
});
|
|
}
|
|
for (const family of this.families) {
|
|
items.push({
|
|
label: family.label,
|
|
onSelect: (innerCtx) => {
|
|
this.setFamily(family);
|
|
innerCtx.stack.pop();
|
|
},
|
|
});
|
|
}
|
|
const inheritRows = inheritLabel !== undefined ? 1 : 0;
|
|
const currentIndex =
|
|
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));
|
|
}
|
|
|
|
/** Switch families, preferring Light (nearest available) and carrying the size over. */
|
|
private setFamily(family: FontFamily): void {
|
|
const previous = this.draft;
|
|
const targetOrder = 300;
|
|
const size = previous.kind === "ttf" ? previous.size : DEFAULT_TTF_SIZE;
|
|
let font = family.fonts[0]!;
|
|
for (const candidate of family.fonts) {
|
|
if (Math.abs(candidate.weightOrder - targetOrder) < Math.abs(font.weightOrder - targetOrder)) {
|
|
font = candidate;
|
|
}
|
|
}
|
|
this.draft = { kind: "ttf", family, font, size: this.clampSize(font, size) };
|
|
}
|
|
|
|
/**
|
|
* The nearest allowed size for this face (line-height bounds differ by
|
|
* face, so a face/weight change can invalidate the carried-over size).
|
|
*/
|
|
private clampSize(font: InstalledFont, size: number): number {
|
|
const allowed = this.options.sizeAllowed;
|
|
if (!allowed || allowed(font.path, size)) return size;
|
|
let best = -1;
|
|
for (const candidate of FONT_SIZE_CHOICES) {
|
|
if (!allowed(font.path, candidate)) continue;
|
|
if (best < 0 || Math.abs(candidate - size) < Math.abs(best - size)) best = candidate;
|
|
}
|
|
return best > 0 ? best : size;
|
|
}
|
|
|
|
private openWeightMenu(ctx: LayerContext): void {
|
|
if (this.draft.kind !== "ttf") return;
|
|
const draft = this.draft;
|
|
const items = draft.family.fonts.map((font): MenuItem => ({
|
|
label: font.style || "Regular",
|
|
onSelect: (innerCtx) => {
|
|
this.draft = { ...draft, font, size: this.clampSize(font, draft.size) };
|
|
innerCtx.stack.pop();
|
|
},
|
|
}));
|
|
openModalMenu(ctx, "Weight", items, Math.max(0, draft.family.fonts.indexOf(draft.font)));
|
|
}
|
|
|
|
private openSizeMenu(ctx: LayerContext): void {
|
|
if (this.draft.kind !== "ttf") return;
|
|
const draft = this.draft;
|
|
const sizes = FONT_SIZE_CHOICES.filter((size) =>
|
|
this.options.sizeAllowed === undefined || this.options.sizeAllowed(draft.font.path, size),
|
|
);
|
|
const items = sizes.map((size): MenuItem => ({
|
|
label: String(size),
|
|
onSelect: (innerCtx) => {
|
|
this.draft = { ...draft, size };
|
|
innerCtx.stack.pop();
|
|
},
|
|
}));
|
|
openModalMenu(ctx, "Size", items, Math.max(0, sizes.indexOf(draft.size as (typeof FONT_SIZE_CHOICES)[number])));
|
|
}
|
|
}
|
|
|
|
/** Installed fonts grouped into families (styles sorted by weight). */
|
|
function collectFamilies(monospaceOnly: boolean): FontFamily[] {
|
|
const byLabel = new Map<string, FontFamily>();
|
|
for (const font of listInstalledFonts()) {
|
|
if (monospaceOnly && !font.monospace) continue;
|
|
let family = byLabel.get(font.family);
|
|
if (!family) {
|
|
family = { label: font.family, fonts: [] };
|
|
byLabel.set(font.family, family);
|
|
}
|
|
family.fonts.push(font);
|
|
}
|
|
return [...byLabel.values()];
|
|
}
|
|
|
|
/** Settings row for the UI font: label + current value, opens the picker. */
|
|
export function uiFontPickerMenuItem(): MenuItem {
|
|
return fontPickerMenuItem({
|
|
rowLabel: "Font",
|
|
description:
|
|
"Typeface for UI text on the glasses: a bitmap Terminus variant or any installed TTF face at a chosen weight and size. Install more fonts from the Files app.",
|
|
title: "UI font",
|
|
monospaceOnly: false,
|
|
sizeAllowed: uiFontSizeAllowed,
|
|
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({
|
|
rowLabel: "Font",
|
|
description:
|
|
"Typeface for terminal windows, limited to fixed-width faces. Takes effect for newly opened terminal windows.",
|
|
title: "Terminal font",
|
|
monospaceOnly: true,
|
|
bitmapFaces: [{ face: "terminus", label: "Terminus" }],
|
|
get: getTerminalFontSelection,
|
|
set: setTerminalFontSelection,
|
|
});
|
|
}
|
|
|
|
function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; description: string }): MenuItem {
|
|
return {
|
|
label: options.rowLabel,
|
|
description: options.description,
|
|
onSelect: (ctx) => {
|
|
ctx.stack.push(new FontPickerLayer(options));
|
|
},
|
|
render: ({ image, x, y, width }) => {
|
|
const selection = options.get();
|
|
drawRightValueMenuItem(
|
|
image,
|
|
getDefaultSmallFont(),
|
|
x,
|
|
y,
|
|
width,
|
|
options.rowLabel,
|
|
selection ? fontSelectionLabel(selection) : options.inheritLabel ?? "",
|
|
);
|
|
},
|
|
};
|
|
}
|