Files
faceclaw/app/apps/onboarding/onboarding-page.ts
T

57 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import type { InputEvent } from "../../ui/gestures";
import { onboardingFont } from "./onboarding-fonts";
/** Page-to-flow navigation, handed to a page's input handler. */
export type OnboardingNav = {
/** Go to the next page, or finish onboarding from the last one. */
next: () => void;
};
/**
* One onboarding step. The flow owns the page list, back navigation
* (double-click) and finishing; a page paints itself into the window's
* viewport and handles every other gesture.
*/
export interface OnboardingPage {
/**
* Show the page immersive (full-screen): the whole 640×480 panel, with
* the app switcher and status bar hidden. Otherwise it is an ordinary
* window in the chosen layout.
*/
readonly immersive?: boolean;
/** Whether the page belongs in the flow right now; skipped both ways when false. */
applies?(): boolean;
/** The page became current, going forward or back. */
enter?(): void;
paint(image: GrayImage, focused: boolean): void;
handleInput(event: InputEvent, nav: OnboardingNav): void | Promise<void>;
}
/** Left margin and title position shared by the pages (the UI-wide (18, 10) title spot). */
export const PAGE_X = 18;
export const TITLE_Y = 10;
/**
* Draw a page's title and its wrapped explanation (at most `maxLines`
* lines) in the onboarding fonts. Returns the y just below the explanation.
*/
export function drawPageHeading(
image: GrayImage,
title: string,
explanation: string,
maxLines = 2,
): number {
const titleFont = onboardingFont("title");
const detailFont = onboardingFont("detail");
image.drawText(titleFont, PAGE_X, TITLE_Y, title, 230);
let y = TITLE_Y + titleFont.lineHeight + 2;
const lines = wrapText(detailFont, explanation, image.width - 2 * PAGE_X).slice(0, maxLines);
for (const line of lines) {
image.drawText(detailFont, PAGE_X, y, line, 160);
y += detailFont.lineHeight;
}
return y;
}