Files
faceclaw/app/apps/developer/load-app.ts
T
James Babcock a02b849200 Borderless modes have different padding inside the builtin apps
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.
2026-11-11 15:19:29 -08:00

217 lines
7.1 KiB
TypeScript

import { GrayImage } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { wrapText } from "../../graphics/textwrap";
import { type AppContext } from "../app-definition";
import { isLoadableAppUrl, openEvenHubUrl } from "../evenhub";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import { developerAppUrlSetting } from "../../ui/dashboard-settings";
import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan";
/**
* "Load app from URL": the glasses side of the phone text editor. Entering the
* page opens the editor on developerAppUrlSetting, the typed URL echoes here
* as it changes, and a click (or the phone keyboard's done key) launches it as
* an EvenHub app. The draft persists, so reloading after an edit-and-rebuild is
* one click.
*/
export class LoadAppFromUrlLayer implements Layer {
private error = "";
private launching = false;
constructor(private readonly ctx: AppContext) {}
/** Opens the phone editor; call right after pushing the layer. */
open(ctx: LayerContext): void {
void ctx.actions.startTextSettingsEdit([developerAppUrlSetting], "Load app from URL", () => {
void this.launch(ctx);
});
}
/** Leaving the page by any route closes the phone editor with it. */
onRemoved(): void {
void this.ctx.actions.endTextSettingEdit();
}
/** Dictated text fills the draft, same as typing it on the phone. */
receiveTextInput(text: string): void {
developerAppUrlSetting.set(text);
this.error = "";
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// The page's text fills the window inside the padding suggested for its
// edges: the title at the top, the hint line at the bottom.
const { width, height, padding } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const step = lineStep(font);
const left = padding.left;
const textWidth = Math.max(0, width - padding.left - padding.right);
image.drawText(font, left, padding.top, "Load app from URL", 220);
let y = padding.top + step + 6;
image.drawText(font, left, y, "Type the app's URL in the phone app", 150);
y += step;
image.drawText(font, left, y, "(or use voice input from the menu).", 150);
y += step + 8;
const url = developerAppUrlSetting.get();
for (const line of wrapText(font, url || "(empty)", textWidth).slice(0, 3)) {
image.drawText(font, left, y, line, url ? 230 : 110);
y += step;
}
if (this.error) {
y += 4;
for (const line of wrapText(font, this.error, textWidth).slice(0, 2)) {
image.drawText(font, left, y, line, 150);
y += step;
}
}
const footer = this.launching
? "Loading..."
: `${GESTURE_CLICK} load ${GESTURE_DOUBLE_CLICK} cancel`;
image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110);
return image;
}
handleInput(event: InputEvent, ctx: LayerContext): void {
switch (event.type) {
case "click":
void this.launch(ctx);
return;
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
private async launch(ctx: LayerContext): Promise<void> {
if (this.launching) return;
const url = developerAppUrlSetting.get();
if (!isLoadableAppUrl(url)) {
this.error = url ? "Not an http:// or https:// URL." : "Enter a URL first.";
ctx.actions.requestRender();
return;
}
this.launching = true;
this.error = "";
ctx.actions.requestRender();
try {
await openEvenHubUrl(this.ctx, url);
ctx.stack.pop();
} catch (error) {
this.error = cleanError(error);
this.ctx.appendLog(`evenhub url launch failed: ${this.error}`);
} finally {
this.launching = false;
ctx.actions.requestRender();
}
}
}
/**
* "Load app from QR code": open the phone's scanner, then launch
* whatever URL it decodes. The scanned URL is kept in developerAppUrlSetting so
* the URL page can reload it later without rescanning.
*/
export class LoadAppFromQrLayer implements Layer {
private status = "Starting scanner...";
private busy = false;
private removed = false;
constructor(private readonly ctx: AppContext) {}
/** Launches the scanner; call right after pushing the layer. */
open(ctx: LayerContext): void {
if (this.busy || this.removed) return;
if (!isQrScannerAvailable()) {
this.status = global.isIOS ? "No camera available. Scan using a physical iPhone."
: "No QR scanner on this phone (needs Play Services or a scanner app).";
ctx.actions.requestRender();
return;
}
this.busy = true;
void this.scan(ctx);
}
onRemoved(): void {
this.removed = true;
if (this.busy) cancelQrScan();
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
// Laid out like the URL page: text inside the padding suggested for the
// window's edges, the hint line at the bottom.
const { width, height, padding } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const step = lineStep(font);
const left = padding.left;
image.drawText(font, left, padding.top, "Load app from QR code", 220);
let y = padding.top + step + 6;
for (const line of wrapText(font, this.status, Math.max(0, width - padding.left - padding.right)).slice(0, 5)) {
image.drawText(font, left, y, line, 170);
y += step;
}
const footer = this.busy ? `${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_CLICK} scan again ${GESTURE_DOUBLE_CLICK} back`;
image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110);
return image;
}
handleInput(event: InputEvent, ctx: LayerContext): void {
switch (event.type) {
case "click":
this.open(ctx);
return;
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
private async scan(ctx: LayerContext): Promise<void> {
try {
this.status = "Point the phone camera at the QR code.";
ctx.actions.requestRender();
const scanned = await scanQrCode();
if (this.removed) return;
if (scanned === null) {
this.status = "Scan cancelled.";
return;
}
if (!isLoadableAppUrl(scanned)) {
this.status = `Not a URL: ${scanned}`;
return;
}
developerAppUrlSetting.set(scanned.trim());
this.status = `Loading ${scanned}`;
ctx.actions.requestRender();
await openEvenHubUrl(this.ctx, scanned);
if (!this.removed) ctx.stack.pop();
} catch (error) {
if (this.removed) return;
this.status = cleanError(error);
this.ctx.appendLog(`evenhub qr launch failed: ${this.status}`);
} finally {
this.busy = false;
if (!this.removed) ctx.actions.requestRender();
}
}
}
function cleanError(error: unknown): string {
return String((error as Error)?.message ?? error)
.replace(/[\x00-\x1f]+/g, " ")
.replace(/\s+/g, " ")
.trim();
}