iOS QR scanning for opening EvenHub apps

This commit is contained in:
James Babcock committed 2026-09-19 03:06:56 -07:00
1 parent e2b35ada1a
commit 68b459751e
8 files changed
+242 -14

No files matched your search

-2
View File
@@ -116,8 +116,6 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc
},
{
label: "Load app from QR code",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: (ctx) => {
const layer = new LoadAppFromQrLayer(appContext);
ctx.stack.push(layer);
+17 -12
View File
@@ -7,7 +7,7 @@ import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/g
import { Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import { developerAppUrlSetting } from "../../ui/dashboard-settings";
import { isQrScannerAvailable, scanQrCode } from "../../native/qr-scan";
import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan";
const MARGIN = 20;
@@ -114,20 +114,23 @@ export class LoadAppFromUrlLayer implements Layer {
}
/**
* "Load app from QR code": hand off to an installed scanner app, then launch
* "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 = "No QR scanner on this phone (needs Play Services or a scanner app).";
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;
}
@@ -135,6 +138,11 @@ export class LoadAppFromQrLayer implements Layer {
void this.scan(ctx);
}
onRemoved(): void {
this.removed = true;
if (this.busy) cancelQrScan();
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
@@ -156,12 +164,7 @@ export class LoadAppFromQrLayer implements Layer {
handleInput(event: InputEvent, 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);
}
this.open(ctx);
return;
case "double-click":
ctx.stack.pop();
@@ -176,6 +179,7 @@ export class LoadAppFromQrLayer implements Layer {
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;
@@ -184,17 +188,18 @@ export class LoadAppFromQrLayer implements Layer {
this.status = `Not a URL: ${scanned}`;
return;
}
developerAppUrlSetting.set(scanned);
developerAppUrlSetting.set(scanned.trim());
this.status = `Loading ${scanned}`;
ctx.actions.requestRender();
await openEvenHubUrl(this.ctx, scanned);
ctx.stack.pop();
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;
ctx.actions.requestRender();
if (!this.removed) ctx.actions.requestRender();
}
}
}
+24
View File
@@ -0,0 +1,24 @@
declare const FaceclawQrScanner: any
let activeScanner: any = null
export function isQrScannerAvailable(): boolean { return FaceclawQrScanner.isAvailable() }
export function scanQrCode(): Promise<string | null> {
if (activeScanner) return Promise.reject(new Error('A QR scan is already open.'))
return new Promise((resolve, reject) => {
const scanner = FaceclawQrScanner.new()
activeScanner = scanner
let settled = false
const finish = (text: string | null, error: string | null) => {
if (settled) return
settled = true
if (activeScanner === scanner) activeScanner = null
error ? reject(new Error(String(error))) : resolve(text == null ? null : String(text))
}
try { scanner.startWithCompletion(finish) }
catch (error) { finish(null, String(error)) }
})
}
export function cancelQrScan(): void { activeScanner?.cancel() }
+4
View File
@@ -22,6 +22,10 @@ const SCAN_ACTION = "com.google.zxing.client.android.SCAN";
// Arbitrary, only has to be distinct from other startActivityForResult callers.
const SCAN_REQUEST_CODE = 0x51d0;
// Android's scanner is owned by Play Services or another app. The load layer
// ignores its late result if the user leaves; iOS also closes its own camera.
export function cancelQrScan(): void {}
function currentActivity(): any {
return Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null;
}