From 7e59f4a58de1cefdde739a671b836c37691a72b9 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sat, 17 Oct 2026 12:28:23 -0700 Subject: [PATCH] Fix some iOS crashes; show the ring connection setting --- app/graphics/surface-compositor.ios.ts | 18 ++++++-- app/native/faceclaw-communicator.ios.ts | 5 +- app/native/preview-display.ios.ts | 14 ++++++ app/ui/dashboard/settings-menus.ts | 3 +- tests/ios-app-host.test.cjs | 19 ++++++-- tests/ios-communicator-surfaces.test.cjs | 59 ++++++++++++++++++++++-- 6 files changed, 103 insertions(+), 15 deletions(-) diff --git a/app/graphics/surface-compositor.ios.ts b/app/graphics/surface-compositor.ios.ts index 17adfa02..500f54da 100644 --- a/app/graphics/surface-compositor.ios.ts +++ b/app/graphics/surface-compositor.ios.ts @@ -20,17 +20,25 @@ export class SurfaceCompositor { this.surfaces.add(id) } removeSurface(id: string): void { this.native.removeId(id); this.surfaces.delete(id) } - setSurfaceVisible(id: string, visible: boolean): void { this.native.visibleIdVisible(id, visible) } - setSurfaceDepth(id: string, depth: number): void { this.native.depthIdDepth(id, depth) } + hasSurface(id: string): boolean { return this.surfaces.has(id) } + // Keep lifecycle errors catchable in JS; an undeclared Kotlin exception + // crossing the Objective-C boundary terminates the process. + private requireSurface(id: string): void { + if (!this.surfaces.has(id)) throw new Error(`Unknown surface: ${id}`) + } + setSurfaceVisible(id: string, visible: boolean): void { this.requireSurface(id); this.native.visibleIdVisible(id, visible) } + setSurfaceDepth(id: string, depth: number): void { + this.requireSurface(id) + if (!Number.isInteger(depth) || depth < -128 || depth > 127) throw new Error(`Invalid surface depth ${depth} for ${id}`) + this.native.depthIdDepth(id, depth) + } setUnderlayDim(belowZOrder: number, factor: number): void { if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') this.native.dimBelowFactor(belowZOrder, factor) } setScreenBlanked(blanked: boolean): void { this.native.blankBlanked(blanked) } submitSurfaceFrame(id: string, pixels: Uint8Array, rect: SurfaceRect, draws: ArrayBuffer | null = null): void { - // Keep lifecycle errors catchable in JS; an undeclared Kotlin exception - // crossing the Objective-C boundary terminates the process. - if (!this.surfaces.has(id)) throw new Error(`Unknown surface: ${id}`) + this.requireSurface(id) if (![rect.x, rect.y, rect.width, rect.height].every(Number.isInteger) || rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) throw new Error('Invalid frame buffer or rectangle') this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height, diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts index f09025c8..6df07c9b 100644 --- a/app/native/faceclaw-communicator.ios.ts +++ b/app/native/faceclaw-communicator.ios.ts @@ -524,7 +524,10 @@ export class FaceclawCommunicatorBridge { id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height), Math.round(options.zOrder), transparency, ); - if (configured !== false) this.nativeSurfaces.add(id); + // Kotlin logged why; reject so callers don't treat the surface as live (as on Android, + // where the compositor's exception reaches JS). + if (configured === false) throw new Error(`Could not configure surface ${id}`); + this.nativeSurfaces.add(id); }); } diff --git a/app/native/preview-display.ios.ts b/app/native/preview-display.ios.ts index ce85084f..d01d3f5f 100644 --- a/app/native/preview-display.ios.ts +++ b/app/native/preview-display.ios.ts @@ -85,12 +85,25 @@ export class PreviewDisplayTarget implements DisplayTarget { this.composited(); } + /** + * As on the live bridge (FaceclawCommunicatorBridge.hasNativeSurface): a call that lands + * after its window closed, or on a target that replaced the one it was configured on, is + * dropped rather than failing the caller. + */ + private hasSurface(id: string, what: string): boolean { + if (this.compositor.hasSurface(id)) return true; + console.log(`[ios-preview] ${what} for unknown surface ${id} dropped`); + return false; + } + async setSurfaceVisible(id: string, visible: boolean): Promise { + if (!this.hasSurface(id, "setSurfaceVisible")) return; this.compositor.setSurfaceVisible(id, Boolean(visible)); this.composited(); } async setSurfaceDepth(id: string, depth: number): Promise { + if (!this.hasSurface(id, "setSurfaceDepth")) return; this.compositor.setSurfaceDepth(id, Math.round(depth)); } @@ -113,6 +126,7 @@ export class PreviewDisplayTarget implements DisplayTarget { _frameId = 0, glyphs: ArrayBuffer | null = null, ): Promise { + if (!this.hasSurface(surfaceId, "frame")) return; this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp, { x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs); this.composited(); diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index b80390e4..ed240e21 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -236,7 +236,8 @@ function settingsSections(): SettingsSection[] { if (!global.isIOS) return sections; const deferred = new Set(["Watch"]); return sections.map(section => { - if (section.label === "Developer") return { ...section, items: [toggleSettingMenuItem(showBleBandwidthSetting)] }; + if (section.label === "Developer") return { ...section, items: [enumSettingMenuItem(ringConnectionModeSetting), + toggleSettingMenuItem(showBleBandwidthSetting)] }; // No downloadable on-device models: iOS's on-device option is Apple's recognizer. if (section.label === "Voice") return { label: "Voice", items: [enumSettingMenuItem(wakeWordActionSetting), enumSettingMenuItem(voiceProviderSetting)] }; diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs index 078c8951..86e5a079 100644 --- a/tests/ios-app-host.test.cjs +++ b/tests/ios-app-host.test.cjs @@ -257,20 +257,29 @@ test('iOS configures an in-process surface before submitting constructor-trigger }); test('iOS compositor rejects missing and removed surfaces before crossing into Kotlin', () => { - let nativeSubmissions = 0; - const native = { configureIdXYWidthHeightZOrderTransparent() {}, removeId() {}, submitDrawsIdDataXYWidthHeightDraws() { nativeSubmissions++; } }; + let nativeCalls = 0; + const native = { configureIdXYWidthHeightZOrderTransparent() {}, removeId() {}, submitDrawsIdDataXYWidthHeightDraws() { nativeCalls++; }, + visibleIdVisible() { nativeCalls++; }, depthIdDepth() { nativeCalls++; } }; const api = load('app/graphics/surface-compositor.ios.ts', { require: () => ({ toData: data => data }), FaceclawKitIosSurfaceCompositor: { alloc: () => ({ initWithWidthHeight: () => native }) }, }); const c = new api.SurfaceCompositor(2, 1), pixels = new Uint8Array([1, 2]); const rect = { x: 0, y: 0, width: 2, height: 1 }; - assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/); + const rejectsAll = () => { + assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/); + assert.throws(() => c.setSurfaceVisible('nightscout', true), /Unknown surface/); + assert.throws(() => c.setSurfaceDepth('nightscout', 1), /Unknown surface/); + }; + rejectsAll(); c.configureSurface('nightscout', { ...rect, zOrder: 0, transparency: 'opaque' }); c.submitSurfaceFrame('nightscout', pixels, rect); + c.setSurfaceVisible('nightscout', true); + c.setSurfaceDepth('nightscout', 1); + assert.throws(() => c.setSurfaceDepth('nightscout', 200), /Invalid surface depth/); c.removeSurface('nightscout'); - assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/); - assert.equal(nativeSubmissions, 1); + rejectsAll(); + assert.equal(nativeCalls, 3); }); test('iOS opens the on-glasses onboarding on a new acknowledged frame, once per run while due', () => { diff --git a/tests/ios-communicator-surfaces.test.cjs b/tests/ios-communicator-surfaces.test.cjs index c84d629f..dc6bdc73 100644 --- a/tests/ios-communicator-surfaces.test.cjs +++ b/tests/ios-communicator-surfaces.test.cjs @@ -3,13 +3,19 @@ const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); // FaceclawCommunicatorBridge over a fake Kotlin session that, like the real -// SurfaceCompositor, rejects calls for surfaces it does not hold. -function harness() { +// SurfaceCompositor, rejects calls for surfaces it does not hold. Configuring +// an id in `refused` fails the way IosGlassesSession reports it: false. +function harness(refused = new Set()) { const calls = []; const surfaces = new Set(); const session = { setRequiredFirmwareRevisionRevision() {}, setListenerListener() {}, setPreviewAnimationListenerListener() {}, - configureSurfaceIdXYWidthHeightZOrderTransparency(id) { calls.push(['configure', id]); surfaces.add(id); return true; }, + configureSurfaceIdXYWidthHeightZOrderTransparency(id) { + calls.push(['configure', id]); + if (refused.has(id)) return false; + surfaces.add(id); + return true; + }, removeSurfaceId(id) { calls.push(['remove', id]); surfaces.delete(id); }, setSurfaceVisibleIdVisible(id) { calls.push(['visible', id]); if (!surfaces.has(id)) throw new Error(`unknown surface ${id}`); return true; }, setSurfaceDepthIdDepth(id) { calls.push(['depth', id]); if (!surfaces.has(id)) throw new Error(`unknown surface ${id}`); return true; }, @@ -74,3 +80,50 @@ test('frames for a live surface still go through, including the idle inline path await h.drain(); await Promise.all([reconfigured, frame]); assert.deepEqual(h.calls.slice(-2), [['configure', 'window:calc'], ['frame', 'window:calc']]); }); + +test('a surface Kotlin refuses to configure rejects, and its frames never reach Kotlin', async () => { + const h = harness(new Set(['window:bad'])); + // Handler attached up front: the rejection lands while drain() runs. + const configured = assert.rejects(h.bridge.configureSurface('window:bad', options), /Could not configure surface window:bad/); + const frame = h.bridge.submitSurfaceFrame('window:bad', new Uint8Array(2), rect, 'f'); + await h.drain(); + await Promise.all([configured, frame]); + assert.deepEqual(h.calls, [['configure', 'window:bad']]); +}); + +test('the preview target drops calls for surfaces it does not hold instead of reaching Kotlin', async () => { + // Like IosSurfaceCompositor: these throw undeclared Kotlin exceptions, which abort the process. + const calls = []; + const surfaces = new Set(); + const held = (what, id) => { calls.push([what, id]); if (!surfaces.has(id)) throw new Error(`unknown surface ${id}`); }; + const native = { + setPreviewAnimationListenerListener() {}, + configureIdXYWidthHeightZOrderTransparent(id) { calls.push(['configure', id]); surfaces.add(id); }, + removeId(id) { calls.push(['remove', id]); surfaces.delete(id); }, + visibleIdVisible: id => held('visible', id), + depthIdDepth: id => held('depth', id), + submitDrawsIdDataXYWidthHeightDraws: id => held('frame', id), + }; + const kotlinData = { toData: bytes => bytes, fromData: data => data }; + const load = loader({ + FaceclawKitIosSurfaceCompositor: { alloc: () => ({ initWithWidthHeight: () => native }) }, console: { log() {} }, + }, { + '@nativescript/core': { File: {}, knownFolders: {}, path: {} }, + '../graphics/image': { G2_LENS_WIDTH: 576, G2_LENS_HEIGHT: 288 }, + './ios-graphics': { previewPixels() {} }, + './kotlin-data': kotlinData, '../native/kotlin-data': kotlinData, + }); + const { PreviewDisplayTarget } = load('app/native/preview-display.ios.ts'); + const target = new PreviewDisplayTarget(); + // A Glanceboard show() that outlived the session: the preview target replacing it never configured glance. + await target.setSurfaceDepth('glance', 2); + await target.setSurfaceVisible('glance', true); + await target.submitSurfaceFrame('glance', new Uint8Array(2), rect, 'f'); + await target.configureSurface('window:calc', options); + await target.submitSurfaceFrame('window:calc', new Uint8Array(2), rect, 'f'); + await target.removeSurface('window:calc'); + // A worker frame that lands after its window closed. + await target.submitSurfaceFrame('window:calc', new Uint8Array(2), rect, 'g'); + await target.setSurfaceVisible('window:calc', false); + assert.deepEqual(calls, [['configure', 'window:calc'], ['frame', 'window:calc'], ['remove', 'window:calc']]); +});