Add EvenHub icon to phone preview screen

This commit is contained in:
James Babcock committed 2026-10-12 02:33:22 -07:00
1 parent 14d1f7f509
commit ccbf523e9a
8 files changed
+100 -8

No files matched your search

+2
View File
@@ -2,6 +2,8 @@
@interface FaceclawGraphics : NSObject
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green;
/** A glasses icon as white-on-transparent (gray becomes alpha), for the dark header. */
+ (UIImage *)whiteIcon:(NSData *)gray width:(NSInteger)width height:(NSInteger)height;
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
+ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight;
+17
View File
@@ -76,6 +76,23 @@
CGColorSpaceRelease(space);
return result;
}
+ (UIImage *)whiteIcon:(NSData *)gray width:(NSInteger)width height:(NSInteger)height {
if (width <= 0 || height <= 0 || gray.length != width * height) return nil;
NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4];
const uint8_t *src = gray.bytes;
uint8_t *dst = rgba.mutableBytes;
// Premultiplied white: every channel equals the alpha.
for (NSInteger i = 0; i < width * height; i++) memset(dst + i * 4, src[i], 4);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)rgba);
CGImageRef image = CGImageCreate(width, height, 8, 32, width * 4, space,
kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast, provider, NULL, false, kCGRenderingIntentDefault);
UIImage *result = image ? [UIImage imageWithCGImage:image scale:1 orientation:UIImageOrientationUp] : nil;
if (image) CGImageRelease(image);
CGDataProviderRelease(provider);
CGColorSpaceRelease(space);
return result;
}
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size {
if (size <= 0 || size > 1024) return nil;
SVGKImage *image = [SVGKImage imageWithData:[svg dataUsingEncoding:NSUTF8StringEncoding]];
+2
View File
@@ -35,6 +35,8 @@ Page.main-page {
.main-page .action-bar-title { font-size: 18; font-weight: bold; color: #ffffff; }
.main-page .action-bar-status { font-size: 14; color: #ffffff; margin-right: 12; }
.main-page .action-bar-warning { font-size: 18; padding: 4 6; }
/* Matches Android's 32x32 padded icon; the Image inside is 21x21. */
.main-page .action-bar-app-icon { width: 32; height: 32; margin-right: 8; }
.main-page .overflow-button {
background-color: transparent;
color: #ffffff;
+6 -1
View File
@@ -23,6 +23,9 @@ window.__faceclawEvenHub = { postMessage: function(name, args, id) {
export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
const host = FaceclawEvenHubWebView.new()
host.packageIdentifier = session.manifest.packageId
// The manager's close callback for the shown phone UI; the native back
// button hides the host itself and reports it as a 'hidden' event.
let onClose: (() => void) | null = null
host.eventHandler = (json: string) => {
try {
const event = JSON.parse(json)
@@ -30,6 +33,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
session.handleBridgeCall(event.name, event.args, event.id)
else if (event.kind === 'loaded') session.webViewLoaded()
else if (event.kind === 'error') session.webViewFailed(String(event.message))
else if (event.kind === 'hidden') { const close = onClose; onClose = null; close?.() }
else if (event.kind === 'console') console.log(`[evenhub:${session.manifest.name}] ${event.message}`)
} catch (error) { console.warn(`EvenHub bridge: ${error}`) }
}
@@ -41,5 +45,6 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
}, 0)
return { native: host, evaluateJs: js => host.evaluate(js),
destroy: () => { clearTimeout(loading); host.destroy() },
showOnPhone: () => host.showOnPhone(), hideOnPhone: () => host.hideOnPhone() }
showOnPhone: close => { onClose = close; host.showOnPhone() },
hideOnPhone: () => { onClose = null; host.hideOnPhone() } }
}
+25 -3
View File
@@ -7,7 +7,7 @@ import { launcherEntries } from '../apps/launcher'
import { getInstalledEvenHubAppById, installedEvenHubPackageId, uninstallEvenHubPackage } from '../apps/evenhub/installed-apps'
import { isInstalledPackagePresent } from '../apps/evenhub/updates'
import { openEvenHubStoreForPackage } from '../apps/evenhub'
import { launchInstalledPackage, closeRunningPackage } from '../apps/evenhub/manager'
import { launchInstalledPackage, closeRunningPackage, onPhoneShownChanged, phoneUiButton, showOnPhone, type PhoneUiButton } from '../apps/evenhub/manager'
import { registerSystemTools } from '../assistant/system-tools'
import { registerWindowTools } from '../assistant/window-tools'
import { registerNavigateTools } from '../assistant/navigate-tools'
@@ -128,6 +128,8 @@ export class IosPreviewController {
private nextWorkerFrame = 0
private inputQueue: Promise<void> = Promise.resolve()
private lastLayout = ''
/** The phone-UI button last pushed to the page (onEvenHubPhoneUiChanged). */
private evenHubPhoneUi: PhoneUiButton | null = null
private readonly appHosts = new Map<string, WorkerAppHost>()
private readonly inProcessApps = new Map<string, InProcessWindow>()
private readonly batteryObservers: any[] = []
@@ -162,7 +164,8 @@ export class IosPreviewController {
constructor(private readonly onFrame: (image: ImageSource, focus: string) => void,
private readonly onError: (message: string) => void,
private readonly onConnectionState: (state: SessionState) => void = () => {},
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) {
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {},
private readonly onEvenHubPhoneUiChanged: (button: PhoneUiButton | null) => void = () => {}) {
this.ensurePreviewDisplay()
shell.configure({
actions: this.actions,
@@ -177,7 +180,7 @@ export class IosPreviewController {
prepareVoiceCapture: () => this.prepareVoiceCapture(),
getScreenTimeoutMs: () => null,
requestShellRender: () => this.requestShellRender(),
onWindowsChanged: () => this.requestShellRender(),
onWindowsChanged: () => { this.requestShellRender(); this.syncEvenHubPhoneUi() },
onScreenStateChanged: on => {
if (on) this.glance.dismiss()
void this.display?.setScreenBlanked(!on).catch(error => this.fail(error))
@@ -220,6 +223,8 @@ export class IosPreviewController {
assistantAvailable: () => shell.isAssistantAvailable(),
assistant: text => shell.sendToAssistant(text),
})
// The header's EvenHub phone-UI button hides while that UI is showing.
onPhoneShownChanged(() => this.syncEvenHubPhoneUi())
registerSystemTools()
registerWindowTools({ apps: ALL_APPS.filter(app => !iosAppUnavailableReason(app.appId)),
launchApp: id => this.launchApp(id), requestShellRender: () => this.requestShellRender() })
@@ -279,6 +284,7 @@ export class IosPreviewController {
this.relayout()
shell.foregroundWindow()?.requestRender()
this.requestShellRender()
this.syncEvenHubPhoneUi()
}
pause(): void {
if (!this.active) return
@@ -289,6 +295,22 @@ export class IosPreviewController {
this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
this.flushBluetoothLog()
}
/** Overlay an EvenHub app's phone UI (the header's app-icon button). */
showEvenHubPhoneUi(windowId: string): void {
showOnPhone(windowId)
}
/**
* Tell the page when the foreground window's EvenHub phone-UI button
* changes. A paused controller skips this and catches up on resume.
*/
private syncEvenHubPhoneUi(): void {
if (!this.active) return
const button = phoneUiButton()
const current = this.evenHubPhoneUi
if (current?.windowId === button?.windowId && current?.icon === button?.icon) return
this.evenHubPhoneUi = button
this.onEvenHubPhoneUiChanged(button)
}
private get runtimeNeeded(): boolean {
return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase)
}
+19
View File
@@ -0,0 +1,19 @@
import { type ImageSource } from '@nativescript/core'
import { type GrayImage } from '../graphics/image'
import { previewPixels, whiteIconPixels } from './ios-graphics'
/** Build the phone-UI preview for a frame (brightened like Android's). */
export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
const baked = image.withDrawsBaked()
return previewPixels(baked.pixels, baked.width, baked.height)
}
/**
* A glasses icon as a white-on-transparent phone image (gray becomes alpha),
* for the dark header.
*/
export function grayImageToWhiteIconSource(image: GrayImage): ImageSource | null {
const baked = image.withDrawsBaked()
return whiteIconPixels(baked.pixels, baked.width, baked.height)
}
+9
View File
@@ -12,6 +12,15 @@ export function previewPixels(pixels: Uint8Array, width: number, height: number,
return new ImageSource(native)
}
/** Gray pixels as a white-on-transparent image (gray becomes alpha). */
export function whiteIconPixels(pixels: Uint8Array, width: number, height: number): ImageSource {
const copy = new Uint8Array(pixels)
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
const native = FaceclawGraphics.whiteIconWidthHeight(data, width, height)
if (!native) throw new Error('Could not create icon image')
return new ImageSource(native)
}
export function rasterizeSvg(svg: string, size: number): GrayImage | null {
const data = FaceclawGraphics.renderSVGSize(svg, size)
if (!data) return null
+20 -4
View File
@@ -20,26 +20,37 @@ export function createMainPage(): Page {
root.rows = 'auto,*'
page.content = root
const header = new GridLayout()
header.columns = '*,auto,auto,auto'; header.className = 'main-header'
header.columns = '*,auto,auto,auto,auto'; header.className = 'main-header'
const title = new Label()
title.text = 'Faceclaw'; title.className = 'action-bar-title'
header.addChild(title)
// The foreground EvenHub app's icon, which opens its phone UI. UIImageView
// ignores padding, so a wrapper supplies Android's wider tap target.
const appIconButton = new GridLayout()
appIconButton.className = 'action-bar-app-icon'; appIconButton.verticalAlignment = 'middle'
appIconButton.visibility = 'collapse'; appIconButton.accessible = true
const appIcon = new Image()
appIcon.width = 21; appIcon.height = 21
appIconButton.addChild(appIcon)
let phoneUiWindowId: string | null = null
appIconButton.on('tap', () => { if (phoneUiWindowId) controller.showEvenHubPhoneUi(phoneUiWindowId) })
GridLayout.setColumn(appIconButton, 1); header.addChild(appIconButton)
const state = new Label()
state.text = 'Preview'; state.className = 'action-bar-status'
state.verticalAlignment = 'middle'
state.on('tap', () => { void openDevices() })
GridLayout.setColumn(state, 1); header.addChild(state)
GridLayout.setColumn(state, 2); header.addChild(state)
// Android's warning triangle: shown while the glasses report firmware Faceclaw can't run.
const warning = new Label()
warning.text = '\u26A0\uFE0F'; warning.className = 'action-bar-warning'
warning.accessibilityLabel = 'Attention needed'; warning.verticalAlignment = 'middle'
warning.visibility = 'collapse'
warning.on('tap', () => { void showFirmwareWarning() })
GridLayout.setColumn(warning, 2); header.addChild(warning)
GridLayout.setColumn(warning, 3); header.addChild(warning)
const overflow = new Button()
overflow.text = '⋮'; overflow.accessibilityLabel = 'More options'; overflow.className = 'overflow-button'
overflow.on('tap', () => openMenu())
GridLayout.setColumn(overflow, 3); header.addChild(overflow); root.addChild(header)
GridLayout.setColumn(overflow, 4); header.addChild(overflow); root.addChild(header)
const body = new GridLayout()
GridLayout.setRow(body, 1); root.addChild(body)
const mirror = new Image()
@@ -76,6 +87,11 @@ export function createMainPage(): Page {
if (session) {
focusTimer = setTimeout(() => { focusTimer = null; keyboardField()?.focus() }, 0)
} else keyboardField()?.dismissSoftInput()
}, button => {
phoneUiWindowId = button?.icon ? button.windowId : null
appIcon.imageSource = button?.icon ?? null
appIconButton.visibility = phoneUiWindowId ? 'visible' : 'collapse'
appIconButton.accessibilityLabel = button ? `Open ${button.title} on phone` : ''
})
async function navigateDevicePage(moduleName: string, context?: object): Promise<void> {
suppressAutoReconnect()