import { fromData, toData } from '../native/kotlin-data' export type SurfaceRect = { x: number; y: number; width: number; height: number } export type SurfaceConfiguration = SurfaceRect & { zOrder: number transparency: 'opaque' | 'color-key' } declare const FaceclawKitIosSurfaceCompositor: any /** Retained pixels, clipping, layers and composition live in shared Kotlin. */ export class SurfaceCompositor { private native: any private readonly surfaces = new Set() constructor(readonly width: number, readonly height: number) { if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) throw new Error('Invalid compositor size') this.native = FaceclawKitIosSurfaceCompositor.alloc().initWithWidthHeight(width, height) } configureSurface(id: string, options: SurfaceConfiguration): void { if (![options.x, options.y, options.width, options.height, options.zOrder].every(Number.isInteger) || options.width <= 0 || options.height <= 0) throw new Error('Invalid surface geometry') this.native.configureIdXYWidthHeightZOrderTransparent(id, options.x, options.y, options.width, options.height, options.zOrder, options.transparency === 'color-key') this.surfaces.add(id) } removeSurface(id: string): void { this.native.removeId(id); this.surfaces.delete(id) } 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 { 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, draws ? toData(new Uint8Array(draws)) : null) } setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) } composite(): Uint8Array { return fromData(this.native.composite()) } /** Called on the main queue after each step of an animation the preview is replaying. */ setPreviewAnimationListener(listener: (() => void) | null): void { this.native.setPreviewAnimationListenerListener(listener) } }