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.
176 lines
7.4 KiB
TypeScript
176 lines
7.4 KiB
TypeScript
import type { GrayImage, UiFont } from "../../graphics/image";
|
|
import { installedFontPath } from "../../graphics/installed-fonts";
|
|
import { wrapText } from "../../graphics/textwrap";
|
|
import { TtfFont } from "../../graphics/ttf-font";
|
|
import {
|
|
defaultUiFontSize,
|
|
fontSelectionLabel,
|
|
getDefaultSmallFont,
|
|
getUiFontSelection,
|
|
setUiFontSelection,
|
|
uiFontSizeAllowed,
|
|
} from "../../graphics/ui-fonts";
|
|
import { FONT_SIZE_CHOICES } from "../../ui/font-picker";
|
|
import type { InputEvent } from "../../ui/gestures";
|
|
import { Menu, type MenuBox, type MenuDrawArgs } from "../../ui/menu-core";
|
|
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
|
import type { Edges } from "../../ui/viewport";
|
|
import { onboardingFont } from "./onboarding-fonts";
|
|
import { drawPageHeading, PAGE_TEXT_INSET, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
|
|
|
|
const CONTENT_GAP = 10;
|
|
const COLUMN_GAP = 12;
|
|
const PREVIEW_PADDING = 10;
|
|
const ROW_GAP = 2;
|
|
/** Between a size and its "Recommended" note. */
|
|
const NOTE_GAP = 8;
|
|
/** The sizes offered here; Settings has the full range. */
|
|
const SIZE_CHOICES = FONT_SIZE_CHOICES.filter((size) => size >= 12 && size <= 18);
|
|
const RECOMMENDED_NOTE = "Recommended";
|
|
/** Drawn while they fit, so smaller sizes visibly show more words. */
|
|
const PREVIEW_PARAGRAPHS = [
|
|
"This is how text will look on your glasses.",
|
|
"Smaller sizes fit more words on the display at once, while larger sizes are easier to read at a " +
|
|
"glance or on the move. Most people find the recommended size a good balance between the two.",
|
|
];
|
|
|
|
/** The UI font's face, as far as this page is concerned: resizable (TTF) or not (bitmap). */
|
|
type Face =
|
|
| { kind: "sized"; file: string; path: string; sizes: number[] }
|
|
| { kind: "fixed"; label: string };
|
|
|
|
/**
|
|
* The UI font's size, 12 to 18px (the default size recommended, and
|
|
* selected at first: 16px, or 14px on the RayNeo iO),
|
|
* beside a preview of text in the selected size. The face and weight stay
|
|
* as they are (they live in the Settings font picker); a bitmap face has
|
|
* only one size, so the page just says so. The size is saved on tap, not
|
|
* while scrolling.
|
|
*/
|
|
export class TextSizePage implements OnboardingPage {
|
|
readonly immersive = true;
|
|
private face: Face = { kind: "fixed", label: "Terminus" };
|
|
private readonly sizeMenu = new Menu<number>({
|
|
wrap: false,
|
|
rowGap: ROW_GAP,
|
|
highlight: { radius: 8 },
|
|
getHeight: () => listRowHeight(onboardingFont("body")),
|
|
draw: (args) => this.drawSizeRow(args),
|
|
});
|
|
|
|
enter(): void {
|
|
// Re-read on every visit: the face may have changed in Settings meanwhile.
|
|
const selection = getUiFontSelection();
|
|
if (selection.kind === "ttf") {
|
|
const path = installedFontPath(selection.file);
|
|
const sizes = SIZE_CHOICES.filter((size) => uiFontSizeAllowed(path, size));
|
|
if (sizes.length) {
|
|
this.face = { kind: "sized", file: selection.file, path, sizes };
|
|
// The recommended size the first time; on a return, what was left selected.
|
|
this.sizeMenu.setItems(sizes, nearestIndex(sizes, this.sizeMenu.selectedItem ?? defaultUiFontSize()));
|
|
return;
|
|
}
|
|
}
|
|
// An unloadable TTF selection renders as Terminus, so call it that.
|
|
this.face = { kind: "fixed", label: selection.kind === "bitmap" ? fontSelectionLabel(selection) : "Terminus" };
|
|
}
|
|
|
|
paint(image: GrayImage, focused: boolean, padding: Edges<number>): void {
|
|
// The preview's frame keeps inside the padding of the window's edges.
|
|
const right = image.width - padding.right;
|
|
const bottom = image.height - padding.bottom;
|
|
if (this.face.kind === "fixed") {
|
|
const top = drawPageHeading(
|
|
image,
|
|
padding,
|
|
"Text size",
|
|
`Your UI font, ${this.face.label}, comes in one size. To choose a size, pick a different font in ` +
|
|
"Settings > Customization. Tap to continue.",
|
|
3,
|
|
) + CONTENT_GAP;
|
|
drawPreview(image, getDefaultSmallFont(), { x: padding.left, y: top, width: right - padding.left, height: bottom - top });
|
|
return;
|
|
}
|
|
const top = drawPageHeading(
|
|
image,
|
|
padding,
|
|
"Text size",
|
|
"Swipe to choose a size, then tap to continue. The typeface and weight can be changed in Settings.",
|
|
) + CONTENT_GAP;
|
|
const body = onboardingFont("body");
|
|
// TTF widths are fractional; the menu's row images need whole pixels.
|
|
const columnWidth = Math.ceil(2 * PAGE_TEXT_INSET + Math.max(...this.face.sizes.map((size) => body.measureText(sizeLabel(size)))) +
|
|
NOTE_GAP + onboardingFont("detail").measureText(RECOMMENDED_NOTE));
|
|
// The list scrolls, so it runs to the window's bottom edge, with that
|
|
// edge's padding after its last row.
|
|
const menuBox = { x: padding.left, y: top, width: columnWidth, height: Math.max(0, image.height - top), padBottom: padding.bottom };
|
|
this.sizeMenu.paint(image, menuBox, focused);
|
|
const previewX = menuBox.x + menuBox.width + COLUMN_GAP;
|
|
const size = this.sizeMenu.selectedItem;
|
|
const font = (size !== null ? TtfFont.load(this.face.path, size) : null) ?? getDefaultSmallFont();
|
|
drawPreview(image, font, { x: previewX, y: top, width: right - previewX, height: bottom - top });
|
|
}
|
|
|
|
handleInput(event: InputEvent, nav: OnboardingNav): void {
|
|
switch (event.type) {
|
|
case "scroll-up":
|
|
case "scroll-down":
|
|
if (this.face.kind === "sized") void this.sizeMenu.handleInput(event);
|
|
return;
|
|
case "click":
|
|
this.save();
|
|
nav.next();
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
private save(): void {
|
|
const size = this.sizeMenu.selectedItem;
|
|
if (this.face.kind !== "sized" || size === null) return;
|
|
const current = getUiFontSelection();
|
|
if (current.kind === "ttf" && current.file === this.face.file && current.size === size) return;
|
|
setUiFontSelection({ kind: "ttf", file: this.face.file, size });
|
|
}
|
|
|
|
private drawSizeRow({ image, item: size, x, y, height, selected }: MenuDrawArgs<number>): void {
|
|
const body = onboardingFont("body");
|
|
const label = sizeLabel(size);
|
|
image.drawText(body, x + PAGE_TEXT_INSET, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 190);
|
|
if (size !== defaultUiFontSize()) return;
|
|
const detail = onboardingFont("detail");
|
|
const noteY = y + Math.round((height - detail.lineHeight) / 2);
|
|
image.drawText(detail, x + PAGE_TEXT_INSET + body.measureText(label) + NOTE_GAP, noteY, RECOMMENDED_NOTE, selected ? 210 : 150);
|
|
}
|
|
}
|
|
|
|
function sizeLabel(size: number): string {
|
|
return `${size} px`;
|
|
}
|
|
|
|
/** A framed sample in `font`: paragraphs of text, as many whole lines as fit. */
|
|
function drawPreview(image: GrayImage, font: UiFont, box: MenuBox): void {
|
|
image.drawRoundedRect(box.x, box.y, box.width, box.height, 60, 8);
|
|
const left = box.x + PREVIEW_PADDING;
|
|
const width = box.width - 2 * PREVIEW_PADDING;
|
|
const bottom = box.y + box.height - PREVIEW_PADDING;
|
|
let y = box.y + PREVIEW_PADDING;
|
|
for (const [index, paragraph] of PREVIEW_PARAGRAPHS.entries()) {
|
|
if (index > 0) y += font.lineHeight >> 1;
|
|
for (const line of wrapText(font, paragraph, width)) {
|
|
if (y + font.lineHeight > bottom) return;
|
|
image.drawText(font, left, y, line, 220);
|
|
y += font.lineHeight;
|
|
}
|
|
}
|
|
}
|
|
|
|
function nearestIndex(values: readonly number[], target: number): number {
|
|
let best = 0;
|
|
for (let index = 1; index < values.length; index++) {
|
|
if (Math.abs(values[index]! - target) < Math.abs(values[best]! - target)) best = index;
|
|
}
|
|
return best;
|
|
}
|