forked from Blendman974/faceclaw
Add options to load an app from a URL or a QR code
(The QR code reading does not work yet)
This commit is contained in:
1 parent
6e4ece5dc7
commit
92cd05b6ca
22 files changed
+685
-93
No files matched your search
@@ -16,7 +16,7 @@ import blocksApp from "./blocks";
|
||||
import minesweeperApp from "./minesweeper";
|
||||
import freecellApp from "./freecell";
|
||||
import pinballApp from "./pinball";
|
||||
import debugTestsApp from "./debug-tests";
|
||||
import developerApp from "./developer";
|
||||
import evenhubApp from "./evenhub";
|
||||
import settingsApp from "./settings";
|
||||
|
||||
@@ -43,7 +43,7 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
||||
minesweeperApp,
|
||||
freecellApp,
|
||||
pinballApp,
|
||||
debugTestsApp,
|
||||
developerApp,
|
||||
evenhubApp,
|
||||
settingsApp,
|
||||
];
|
||||
@@ -1,70 +0,0 @@
|
||||
import { MenuLayer } from "../../ui/menu";
|
||||
import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
|
||||
export const DEBUG_TESTS_WINDOW_ID = "debug-tests";
|
||||
export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests";
|
||||
|
||||
/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */
|
||||
export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const menu = new MenuLayer(
|
||||
"Debug tests",
|
||||
[
|
||||
{
|
||||
label: "Dither test",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ScreenTestLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Buzzer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new BuzzerDemoLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Accelerometer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Show resource usage",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ResourceUsageLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
showBorder: false,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
},
|
||||
);
|
||||
return createInProcessWindow({
|
||||
appId: "debug-tests",
|
||||
windowId: DEBUG_TESTS_WINDOW_ID,
|
||||
title: "Debug tests",
|
||||
iconLetter: "Db",
|
||||
icon: "flask-conical",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(menu),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createDebugTestsAppWindow, DEBUG_TESTS_SURFACE_ID, DEBUG_TESTS_WINDOW_ID } from "./debug-tests-app";
|
||||
|
||||
const debugTestsApp: AppDefinition = {
|
||||
appId: "debug-tests",
|
||||
title: "Debug tests",
|
||||
icon: "flask-conical",
|
||||
launch: (ctx) => ctx.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow),
|
||||
};
|
||||
|
||||
export default debugTestsApp;
|
||||
+1
-1
@@ -16,7 +16,7 @@ const RECONCILE_INTERVAL_MS = 400;
|
||||
|
||||
/**
|
||||
* Accelerometer demo: turns the IMU stream on while this page is visible (the
|
||||
* Debug tests window is foregrounded and the screen is on), off otherwise, and
|
||||
* Developer window is foregrounded and the screen is on), off otherwise, and
|
||||
* shows the latest x/y/z reading with a simple per-axis bar.
|
||||
*
|
||||
* The IMU stream is experimental — the firmware may never populate it — so the
|
||||
File renamed without changes.
@@ -0,0 +1,131 @@
|
||||
import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu";
|
||||
import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
|
||||
import { type AppContext } from "../app-definition";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import { LIST_ROW_TEXT_INSET } from "../../ui/metrics";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
|
||||
export const DEVELOPER_WINDOW_ID = "developer";
|
||||
export const DEVELOPER_SURFACE_ID = "window:developer";
|
||||
|
||||
const MENU_LAYOUT = {
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
showBorder: false,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
};
|
||||
|
||||
/** A row that opens a nested page: the label plus a right-edge ">". */
|
||||
function submenuItem(label: string, onSelect: MenuItem["onSelect"]): MenuItem {
|
||||
return {
|
||||
label,
|
||||
onSelect,
|
||||
render: ({ image, x, y, width, height, selected, disabled, text }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const value = disabled ? 70 : selected ? 255 : 200;
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, text, value);
|
||||
drawSubmenuIndicator(image, font, x, y, width, height, value);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The diagnostic demos, one level down from the root menu. */
|
||||
function debugTestsMenu(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"Debug tests",
|
||||
[
|
||||
{
|
||||
label: "Dither test",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ScreenTestLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Buzzer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new BuzzerDemoLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Accelerometer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Developer app: tools for building and debugging on the glasses. The two
|
||||
* "Load app" entries run an EvenHub app straight off a web server, without
|
||||
* packaging it into an .ehpk first; the diagnostic demos live one level down
|
||||
* under "Debug tests".
|
||||
*/
|
||||
export function createDeveloperAppWindow(appContext: AppContext, options: InProcessAppOptions): InProcessWindow {
|
||||
const menu = new MenuLayer(
|
||||
"Developer",
|
||||
[
|
||||
{
|
||||
label: "Load app from URL",
|
||||
onSelect: (ctx) => {
|
||||
const layer = new LoadAppFromUrlLayer(appContext);
|
||||
ctx.stack.push(layer);
|
||||
layer.open(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Load app from QR code",
|
||||
onSelect: (ctx) => {
|
||||
const layer = new LoadAppFromQrLayer(appContext);
|
||||
ctx.stack.push(layer);
|
||||
layer.open(ctx);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Show resource usage",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ResourceUsageLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
submenuItem("Debug tests", (ctx) => {
|
||||
ctx.stack.push(debugTestsMenu());
|
||||
}),
|
||||
],
|
||||
MENU_LAYOUT,
|
||||
);
|
||||
let created: InProcessWindow | null = null;
|
||||
created = createInProcessWindow({
|
||||
appId: "developer",
|
||||
windowId: DEVELOPER_WINDOW_ID,
|
||||
title: "Developer",
|
||||
iconLetter: "Dv",
|
||||
icon: "wrench",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
// Dictating a URL is the one thing worth speaking at in this app; the
|
||||
// load pages take the text and every other page ignores it.
|
||||
receiveTextInput: (text) => {
|
||||
if (created?.stack.receiveTextInput(text)) created.requestRender();
|
||||
},
|
||||
baseLayer: new YieldAtRootLayer(menu),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
return created;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { type AppDefinition } from "../app-definition";
|
||||
import { createDeveloperAppWindow, DEVELOPER_SURFACE_ID, DEVELOPER_WINDOW_ID } from "./developer-app";
|
||||
|
||||
const developerApp: AppDefinition = {
|
||||
appId: "developer",
|
||||
title: "Developer",
|
||||
icon: "wrench",
|
||||
launch: (ctx) =>
|
||||
ctx.launchInProcessApp(DEVELOPER_WINDOW_ID, DEVELOPER_SURFACE_ID, (options) =>
|
||||
createDeveloperAppWindow(ctx, options),
|
||||
),
|
||||
};
|
||||
|
||||
export default developerApp;
|
||||
@@ -0,0 +1,207 @@
|
||||
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 } from "../../ui/gestures";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import { developerAppUrlSetting } from "../../ui/dashboard-settings";
|
||||
import { isQrScannerAvailable, scanQrCode } from "../../native/qr-scan";
|
||||
|
||||
const MARGIN = 20;
|
||||
|
||||
/**
|
||||
* "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();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const step = lineStep(font);
|
||||
const textWidth = width - 2 * MARGIN;
|
||||
|
||||
image.drawText(font, MARGIN, 8, "Load app from URL", 220);
|
||||
let y = 8 + step + 6;
|
||||
image.drawText(font, MARGIN, y, "Type the app's URL in the phone app", 150);
|
||||
y += step;
|
||||
image.drawText(font, MARGIN, 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, MARGIN, 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, MARGIN, y, line, 150);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
|
||||
const footer = this.launching
|
||||
? "Loading..."
|
||||
: `${GESTURE_CLICK} load ${GESTURE_DOUBLE_CLICK} cancel`;
|
||||
image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, 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": hand off to an installed scanner app, 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;
|
||||
|
||||
constructor(private readonly ctx: AppContext) {}
|
||||
|
||||
/** Launches the scanner; call right after pushing the layer. */
|
||||
open(ctx: LayerContext): void {
|
||||
if (!isQrScannerAvailable()) {
|
||||
this.status = "No QR scanner app is installed on the phone.";
|
||||
ctx.actions.requestRender();
|
||||
return;
|
||||
}
|
||||
this.busy = true;
|
||||
void this.scan(ctx);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const step = lineStep(font);
|
||||
|
||||
image.drawText(font, MARGIN, 8, "Load app from QR code", 220);
|
||||
let y = 8 + step + 6;
|
||||
for (const line of wrapText(font, this.status, width - 2 * MARGIN).slice(0, 5)) {
|
||||
image.drawText(font, MARGIN, y, line, 170);
|
||||
y += step;
|
||||
}
|
||||
|
||||
const footer = this.busy ? `${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_CLICK} scan again ${GESTURE_DOUBLE_CLICK} back`;
|
||||
image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "click":
|
||||
if (!this.busy && isQrScannerAvailable()) {
|
||||
this.busy = true;
|
||||
this.status = "Starting scanner...";
|
||||
ctx.actions.requestRender();
|
||||
void this.scan(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 (scanned === null) {
|
||||
this.status = "Scan cancelled.";
|
||||
return;
|
||||
}
|
||||
if (!isLoadableAppUrl(scanned)) {
|
||||
this.status = `Not a URL: ${scanned}`;
|
||||
return;
|
||||
}
|
||||
developerAppUrlSetting.set(scanned);
|
||||
this.status = `Loading ${scanned}`;
|
||||
ctx.actions.requestRender();
|
||||
await openEvenHubUrl(this.ctx, scanned);
|
||||
ctx.stack.pop();
|
||||
} catch (error) {
|
||||
this.status = cleanError(error);
|
||||
this.ctx.appendLog(`evenhub qr launch failed: ${this.status}`);
|
||||
} finally {
|
||||
this.busy = false;
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cleanError(error: unknown): string {
|
||||
return String((error as Error)?.message ?? error)
|
||||
.replace(/[\x00-\x1f]+/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
}
|
||||
File renamed without changes.
@@ -17,7 +17,7 @@ export class ScreenTestLayer implements Layer {
|
||||
private scrollRow = 0;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
// Sized to the hosting stack (the Debug tests app viewport).
|
||||
// Sized to the hosting stack (the Developer app viewport).
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const font = getDefaultSmallFont();
|
||||
@@ -10,7 +10,7 @@
|
||||
import { type AppContext, type AppDefinition } from "../app-definition";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { EvenHubStoreLayer } from "./store-layer";
|
||||
import { launchPackage } from "./manager";
|
||||
import { launchPackage, launchUrl, normalizeAppUrl } from "./manager";
|
||||
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
|
||||
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
|
||||
@@ -26,6 +26,20 @@ export function openEvenHubPackage(ctx: AppContext, ehpkPath: string): Promise<v
|
||||
return launchPackage(ctx, ehpkPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch an app straight from a live web server instead of an .ehpk — the
|
||||
* Developer app's "Load app from URL" / "Load app from QR code" entries, for
|
||||
* running an app off a dev server without packaging it first.
|
||||
*/
|
||||
export function openEvenHubUrl(ctx: AppContext, url: string): Promise<void> {
|
||||
return launchUrl(ctx, url);
|
||||
}
|
||||
|
||||
/** Whether a string can be loaded by openEvenHubUrl, for validating input. */
|
||||
export function isLoadableAppUrl(url: string): boolean {
|
||||
return normalizeAppUrl(url) !== null;
|
||||
}
|
||||
|
||||
const evenhubApp: AppDefinition = {
|
||||
appId: "evenhub",
|
||||
title: "EvenHub",
|
||||
|
||||
@@ -18,7 +18,8 @@ import {
|
||||
readBinaryFile,
|
||||
writeBinaryFile,
|
||||
} from "../../native/file-access";
|
||||
import { parseEhpk, parseManifest, utf8Decode } from "./ehpk";
|
||||
import { parseEhpk, parseManifest, utf8Decode, type EvenHubManifest } from "./ehpk";
|
||||
import { type EvenHubPermission } from "./permissions";
|
||||
import { EvenHubSession } from "./session";
|
||||
import { createEvenHubWindow } from "./evenhub-window";
|
||||
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
|
||||
@@ -96,8 +97,23 @@ export async function launchPackage(
|
||||
}
|
||||
}
|
||||
|
||||
await startApp(ctx, manifest, `${baseDir}/dist`, "", appId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared tail of every launch: build the session and its WebView, register the
|
||||
* pair, and open the glasses window. `distDir` serves a packaged app offline;
|
||||
* `remoteUrl`, when set, loads the app from the network instead.
|
||||
*/
|
||||
async function startApp(
|
||||
ctx: AppContext,
|
||||
manifest: EvenHubManifest,
|
||||
distDir: string,
|
||||
remoteUrl: string,
|
||||
appId: string,
|
||||
): Promise<void> {
|
||||
const windowId = `evenhub:app:${nextSerial++}`;
|
||||
const session = new EvenHubSession(manifest, `${baseDir}/dist`, ctx.appendLog);
|
||||
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl);
|
||||
const webView = createEvenHubWebView(session);
|
||||
session.attachWebView({
|
||||
evaluateJs: webView.evaluateJs,
|
||||
@@ -119,6 +135,61 @@ export async function launchPackage(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch an EvenHub app served by a live web server (the Developer app's
|
||||
* "Load app from URL" / QR flows), rather than one unpacked from an .ehpk.
|
||||
*
|
||||
* There is no manifest to read, so one is synthesized: the URL identifies the
|
||||
* app (a stable packageId keeps its localStorage across reloads) and every
|
||||
* permission is treated as declared, on the grounds that the user pointed
|
||||
* Faceclaw at this URL themselves. Loading the same URL again replaces the
|
||||
* running instance, which is what a reload after an edit should do.
|
||||
*/
|
||||
export async function launchUrl(ctx: AppContext, url: string): Promise<void> {
|
||||
const normalized = normalizeAppUrl(url);
|
||||
if (!normalized) {
|
||||
ctx.appendLog(`evenhub: not an http(s) URL: ${url}`);
|
||||
throw new Error("Not an http:// or https:// URL.");
|
||||
}
|
||||
const manifest = synthesizeUrlManifest(normalized);
|
||||
closeRunningPackage(manifest.packageId);
|
||||
await startApp(ctx, manifest, "", normalized, "evenhub");
|
||||
}
|
||||
|
||||
/** Accept a bare "example.com/app" as https; reject anything not http(s). */
|
||||
export function normalizeAppUrl(raw: string): string | null {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
||||
if (!/^https?:\/\/[^/?#\s]+/i.test(withScheme)) return null;
|
||||
return withScheme;
|
||||
}
|
||||
|
||||
/** Every permission name the host knows how to honor, granted to URL apps. */
|
||||
const DEV_URL_PERMISSIONS: EvenHubPermission[] = [
|
||||
{ name: "network" },
|
||||
{ name: "location" },
|
||||
{ name: "g2-microphone" },
|
||||
{ name: "phone-microphone" },
|
||||
];
|
||||
|
||||
function synthesizeUrlManifest(url: string): EvenHubManifest {
|
||||
const match = /^https?:\/\/([^/?#]+)([^?#]*)/i.exec(url);
|
||||
const authority = match?.[1] ?? url;
|
||||
const path = (match?.[2] ?? "").replace(/\/index\.html?$/i, "").replace(/\/+$/, "");
|
||||
const idSource = `${authority}${path}`.toLowerCase().replace(/[^a-z0-9.-]+/g, "-");
|
||||
return {
|
||||
packageId: `dev.url.${idSource.slice(0, 96)}`,
|
||||
name: authority,
|
||||
version: "dev",
|
||||
entrypoint: "",
|
||||
networkWhitelist: [],
|
||||
permissions: DEV_URL_PERMISSIONS,
|
||||
privacyPolicyUrl: "",
|
||||
raw: { url },
|
||||
};
|
||||
}
|
||||
|
||||
/** Launch an installed app as a launcher-addressable singleton. */
|
||||
export function launchInstalledPackage(ctx: AppContext, app: InstalledEvenHubApp): Promise<void> {
|
||||
return launchPackage(ctx, installedEvenHubPackagePath(app.packageId), {
|
||||
|
||||
@@ -314,6 +314,12 @@ export type EvenHubWindowHooks = {
|
||||
export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenHubCompassClient {
|
||||
readonly manifest: EvenHubManifest;
|
||||
readonly distDir: string;
|
||||
/**
|
||||
* For a developer-loaded app served from a live web server: the absolute
|
||||
* URL to open. Empty for a packaged app, which is served offline out of
|
||||
* distDir instead (see webview.ts).
|
||||
*/
|
||||
readonly remoteUrl: string;
|
||||
|
||||
private page: EvenHubPage | null = null;
|
||||
private pageCreated = false;
|
||||
@@ -352,9 +358,15 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private readonly grantedApiKeys = new Set<string>();
|
||||
private log: (message: string) => void;
|
||||
|
||||
constructor(manifest: EvenHubManifest, distDir: string, log: (message: string) => void) {
|
||||
constructor(
|
||||
manifest: EvenHubManifest,
|
||||
distDir: string,
|
||||
log: (message: string) => void,
|
||||
remoteUrl = "",
|
||||
) {
|
||||
this.manifest = manifest;
|
||||
this.distDir = distDir;
|
||||
this.remoteUrl = remoteUrl;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
* from a fake per-app origin plus document-start injection of the
|
||||
* flutter_inappwebview bridge shim) and so the page keeps running JS while the
|
||||
* host app is backgrounded (see FaceclawEvenHubWebView / the host).
|
||||
*
|
||||
* A session with a remoteUrl (the Developer app's "Load app from URL") skips
|
||||
* the offline serving entirely and loads that URL from the network; the shim
|
||||
* is registered as a document-start script instead of being spliced into the
|
||||
* served HTML.
|
||||
*/
|
||||
import { Application, Utils } from "@nativescript/core";
|
||||
import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from "./session";
|
||||
@@ -33,6 +38,15 @@ function originHost(session: EvenHubSession): string {
|
||||
return `${safe}.evenhub.invalid`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The allowed-origin rule for a remote app's document-start script: the
|
||||
* loaded URL's own origin, so the shim isn't handed to third-party frames.
|
||||
*/
|
||||
function originRule(url: string): string {
|
||||
const match = /^(https?:\/\/[^/?#]+)/i.exec(url);
|
||||
return match ? match[1]! : "*";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the app's WebView, wire it to the session, attach it to the keep-alive
|
||||
* host, and start loading. Must run on the main thread.
|
||||
@@ -56,12 +70,26 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
},
|
||||
});
|
||||
|
||||
const host = originHost(session);
|
||||
const remoteUrl = session.remoteUrl;
|
||||
// Remote apps load from their own server, so nothing is intercepted and the
|
||||
// fake per-app origin is unused.
|
||||
const host = remoteUrl ? "" : originHost(session);
|
||||
// Stock bridge shim first, then the Faceclaw extensions global (both at
|
||||
// document-start, before any app JS).
|
||||
const injectScript = EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`);
|
||||
const documentStartInstalled = remoteUrl
|
||||
? com.faceclaw.app.FaceclawEvenHubDocumentStart.install(webView, injectScript, originRule(remoteUrl)) === true
|
||||
: false;
|
||||
webView.setWebViewClient(
|
||||
new com.faceclaw.app.FaceclawEvenHubWebViewClient(session.distDir, host, injectScript, listener),
|
||||
new com.faceclaw.app.FaceclawEvenHubWebViewClient(
|
||||
session.distDir,
|
||||
host,
|
||||
injectScript,
|
||||
// onPageStarted injection is the fallback when the document-start API
|
||||
// is unavailable; packaged apps get the shim spliced into their HTML.
|
||||
!!remoteUrl && !documentStartInstalled,
|
||||
listener,
|
||||
),
|
||||
);
|
||||
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
|
||||
|
||||
@@ -79,7 +107,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
|
||||
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
|
||||
nativeHost.attach(activity, webView);
|
||||
webView.loadUrl(`https://${host}/${session.manifest.entrypoint}`);
|
||||
webView.loadUrl(remoteUrl || `https://${host}/${session.manifest.entrypoint}`);
|
||||
|
||||
return {
|
||||
native: webView,
|
||||
|
||||
Reference in new issue
Block a user