import { KeyboardInputViewModel } from './keyboard-input-view-model' import { Application, Builder, Button, Color, Dialogs, Frame, GridLayout, Image, Label, Page, TextView, type PropertyChangeData, type TouchGestureEventData, type View } from '@nativescript/core' import { isPreviewOnlyMode } from './onboarding-state' import { isAutoReconnectSuppressed, resumeAutoReconnect, suppressAutoReconnect } from '../g2/reconnect-policy' import { RemoteControlsViewModel } from './remote-controls-view-model' import { IosPreviewController } from '../g2/ios-preview-controller' import { PhoneGestureRecognizer } from './phone-gestures' import { BleBandwidthMeter } from './ble-bandwidth-meter' import { sampleBleTraffic } from '../native/ble-traffic' import { glassesModelName, onAnySettingChanged, mirrorTouchSetting, showBleBandwidthSetting } from '../ui/dashboard-settings' import { loadDeviceAddresses } from '../g2/device-addresses' import { deviceAddressError } from '../g2/ios-peripheral-identity' import { formatBatteryLine } from '../g2/glasses-display-state' import { shell } from '../ui/shell/shell' export function createMainPage(): Page { const page = new Page() page.actionBarHidden = true page.className = 'main-page' page.iosOverflowSafeAreaEnabled = false const root = new GridLayout() root.rows = 'auto,*' page.content = root const header = new GridLayout() 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, 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, 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, 4); header.addChild(overflow); root.addChild(header) const body = new GridLayout() GridLayout.setRow(body, 1); root.addChild(body) const mirror = new Image() mirror.stretch = 'fill'; mirror.backgroundColor = new Color('black') mirror.accessibilityIdentifier = 'glasses-preview' mirror.accessibilityLabel = 'Interactive glasses screen preview' mirror.horizontalAlignment = 'left'; mirror.verticalAlignment = 'middle'; body.addChild(mirror) // Load exactly the same XML and binding model as Android. Only input dispatch // and the surrounding platform navigation/runtime remain iOS-specific. const model = new RemoteControlsViewModel() const controls = new GridLayout() const remote = Builder.load('~/phone-ui/remote-controls') remote.bindingContext = model controls.addChild(remote); body.addChild(controls) const keyboardModel = new KeyboardInputViewModel() const keyboardPanel = Builder.load('~/phone-ui/keyboard-input-panel') keyboardPanel.bindingContext = keyboardModel GridLayout.setRow(keyboardPanel, 1); root.addChild(keyboardPanel) let focusTimer: ReturnType | null = null // The Settings tab's battery row. iOS reports charging sessions as // connected, so "connected" is the whole live-link condition here. let glassesLinked = false const refreshBatteryLine = () => { model.batteryLine = formatBatteryLine(shell.getBatteryLevels(), glassesLinked, glassesModelName()) } const keyboardField = () => keyboardPanel.getViewById('keyboardInputField') const controller = new IosPreviewController((image, label) => { mirror.imageSource = image; model.set('padFocusLine', label) }, message => { void Dialogs.alert({ title: 'Faceclaw', message, okButtonText: 'OK' }) }, connection => { state.text = ({ connected: 'Connected', disconnected: 'Preview', connecting: 'Connecting', retrying: 'Reconnecting', disconnecting: 'Disconnecting', error: 'Failed' })[connection.phase] warning.visibility = connection.firmwareWarning ? 'visible' : 'collapse' glassesLinked = connection.phase === 'connected'; refreshBatteryLine() }, session => { keyboardModel.setSession(session) controls.visibility = session ? 'collapse' : 'visible' if (focusTimer !== null) clearTimeout(focusTimer) focusTimer = null 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 { suppressAutoReconnect() await controller.disconnect() Frame.topmost()?.navigate({ moduleName, context }) } function openMenu(): void { const connected = ['connected', 'connecting', 'retrying'].includes(controller.connectionState?.phase ?? '') const configured = !deviceAddressError(loadDeviceAddresses()) const menu = UIAlertController.alertControllerWithTitleMessagePreferredStyle(null, null, UIAlertControllerStyle.ActionSheet) const add = (label: string, action: (() => void) | null) => { const item = UIAlertAction.actionWithTitleStyleHandler(label, UIAlertActionStyle.Default, () => { // Finish dismissal before presenting a share sheet or another page. menu.dismissViewControllerAnimatedCompletion(true, () => action?.()) }) item.enabled = action !== null menu.addAction(item) } if (configured) add(connected ? 'Disconnect' : 'Connect', () => { if (connected) { suppressAutoReconnect(); void controller.disconnect() } else { resumeAutoReconnect(); void controller.connect() } }) add('Pair glasses', () => { void navigateDevicePage('phone-ui/onboarding-unpair-page') }) add('Permissions', () => Frame.topmost()?.navigate({ moduleName: 'phone-ui/permissions-page' })) // Keep Android's menu order/text. These services still require Android; // disabled items make the platform limitation visible without dead actions. add('Conversations', null) add('Take screenshot', mirror.imageSource ? () => { const share = UIActivityViewController.alloc().initWithActivityItemsApplicationActivities([mirror.imageSource.ios], null) if (share.popoverPresentationController) { share.popoverPresentationController.sourceView = overflow.ios share.popoverPresentationController.sourceRect = overflow.ios.bounds } page.ios.presentViewControllerAnimatedCompletion(share, true, null) } : null) add('Record screen', null) if (configured) add('Uninstall custom firmware', () => { void navigateDevicePage('phone-ui/onboarding-flash-page', { mode: 'uninstall', fromOnboarding: false }) }) menu.addAction(UIAlertAction.actionWithTitleStyleHandler('Cancel', UIAlertActionStyle.Cancel, null)) if (menu.popoverPresentationController) { menu.popoverPresentationController.sourceView = overflow.ios menu.popoverPresentationController.sourceRect = overflow.ios.bounds } page.ios.presentViewControllerAnimatedCompletion(menu, true, null) } async function showFirmwareWarning(): Promise { const message = controller.connectionState?.firmwareWarning if (!message) return const install = await Dialogs.confirm({ title: 'Attention needed', message, okButtonText: 'Install custom firmware', cancelButtonText: 'Close' }) if (install) await navigateDevicePage('phone-ui/onboarding-flash-page', { mode: 'install', fromOnboarding: false }) } async function openDevices(): Promise { const connected = ['connected', 'connecting', 'retrying'].includes(controller.connectionState?.phase ?? '') const choice = await Dialogs.action({ title: 'Connection status', message: controller.connectionState?.status ?? 'Preview only', cancelButtonText: 'Cancel', actions: ['Pair glasses', 'Configure devices', connected ? 'Disconnect' : 'Connect', 'Check firmware', 'Install custom firmware', 'Uninstall custom firmware', 'Connection details'] }) if (['Pair glasses', 'Configure devices', 'Check firmware', 'Install custom firmware', 'Uninstall custom firmware'].includes(choice)) { suppressAutoReconnect() await controller.disconnect() if (choice === 'Pair glasses') { Frame.topmost()?.navigate({ moduleName: 'phone-ui/onboarding-unpair-page' }) } else if (choice === 'Configure devices') { Frame.topmost()?.navigate({ moduleName: 'phone-ui/config-page', context: { onboarding: isPreviewOnlyMode() } }) } else if (choice === 'Check firmware') { Frame.topmost()?.navigate({ moduleName: 'phone-ui/onboarding-firmware-check-page' }) } else { Frame.topmost()?.navigate({ moduleName: 'phone-ui/onboarding-flash-page', context: { mode: choice === 'Uninstall custom firmware' ? 'uninstall' : 'install', fromOnboarding: false } }) } } else if (choice === 'Connect') { resumeAutoReconnect(); await controller.connect() } else if (choice === 'Disconnect') { suppressAutoReconnect(); await controller.disconnect() } else if (choice === 'Connection details') await Dialogs.alert({ title: 'Connection details', message: controller.connectionDetails, okButtonText: 'OK' }) } const watchInput = new PhoneGestureRecognizer(gesture => controller.gesture(gesture, 'watch')) const ringInput = new PhoneGestureRecognizer(gesture => controller.gesture(gesture, 'ring')) const mirrorInput = new PhoneGestureRecognizer((gesture, x, y) => { if (!mirrorTouchSetting.get()) return const size = mirror.getActualSize() if (size.width > 0 && size.height > 0) controller.gesture(gesture, 'mirror', x / size.width, y / size.height) }) const touch = (recognizer: PhoneGestureRecognizer) => (event: TouchGestureEventData) => { if (!['down', 'move', 'up', 'cancel'].includes(event.action)) return recognizer.touch({ action: event.action as 'down' | 'move' | 'up' | 'cancel', x: event.getX(), y: event.getY(), pointers: event.getPointerCount() }) } model.set('onPadTouch', touch(watchInput)) model.set('onRingPadTouch', touch(ringInput)) model.set('onSyntheticMicTap', () => controller.startVoiceInput()) model.set('onKeyboardTap', () => { controller.typeIntoApp() }) mirror.on('touch', touch(mirrorInput)) const cancelGestures = () => { watchInput.cancel(); ringInput.cancel(); mirrorInput.cancel() } model.on('propertyChange', (args: PropertyChangeData) => { if (args.propertyName === 'watchTabVisibility') cancelGestures() }) let controlsLayoutTimer: ReturnType | null = null model.set('onControlsContentLayoutChanged', (args: { object: View }) => { if (controlsLayoutTimer !== null) clearTimeout(controlsLayoutTimer) // Defer binding updates out of UIKit's layout pass. controlsLayoutTimer = setTimeout(() => { controlsLayoutTimer = null const { width, height } = args.object.getActualSize() model.set('watchFaceWidth', Math.min(345, Math.max(0, width))) model.set('watchFaceHeight', Math.min(230, Math.max(0, height - 2))) model.set('ringTouchpadHeight', Math.min(250, Math.max(0, height - 2))) }, 0) }) const bandwidth = new Label() bandwidth.accessibilityIdentifier = 'ble-bandwidth-indicator' bandwidth.className = 'ble-bandwidth-indicator'; bandwidth.isUserInteractionEnabled = false bandwidth.horizontalAlignment = 'center'; bandwidth.verticalAlignment = 'bottom' bandwidth.visibility = 'collapse' GridLayout.setRow(bandwidth, 1); root.addChild(bandwidth) const bandwidthMeter = new BleBandwidthMeter() let bandwidthTimer: ReturnType | null = null let foreground = false function stopBandwidth(): void { if (bandwidthTimer !== null) clearInterval(bandwidthTimer) bandwidthTimer = null; bandwidthMeter.reset() } function syncBandwidth(): void { if (!foreground) { stopBandwidth(); return } const enabled = showBleBandwidthSetting.get() bandwidth.visibility = enabled ? 'visible' : 'collapse' if (!enabled) { stopBandwidth(); return } if (bandwidthTimer !== null) return const refresh = () => { bandwidth.text = bandwidthMeter.sample(sampleBleTraffic(), Date.now()) } refresh(); bandwidthTimer = setInterval(refresh, 1000) } let lastLayout = '' function layout(): void { const size = body.getActualSize() if (size.width <= 0 || size.height <= 0) return const landscape = root.getActualSize().width > root.getActualSize().height const key = `${size.width}:${size.height}:${landscape}` if (key === lastLayout) return lastLayout = key; cancelGestures() body.className = landscape ? 'landscape-layout' : '' root.className = landscape ? 'main-landscape' : '' body.rows = landscape ? '*' : 'auto,*'; body.columns = landscape ? '*,345' : '*' controls.padding = landscape ? 0 : 20 GridLayout.setRow(controls, landscape ? 0 : 1); GridLayout.setColumn(controls, landscape ? 1 : 0) const width = landscape ? Math.max(0, Math.min(size.width - 345, size.height * 4 / 3)) : size.width mirror.width = width; mirror.height = width * 3 / 4 } // iOS emits layoutChanged inside its layout pass. Defer mutations so // NativeScript does not clear our new layout request at the end of that pass. let layoutTimer: ReturnType | null = null body.on('layoutChanged', () => { if (layoutTimer !== null) return layoutTimer = setTimeout(() => { layoutTimer = null; layout() }, 0) }) const pause = () => { foreground = false; stopBandwidth() if (layoutTimer !== null) clearTimeout(layoutTimer) layoutTimer = null if (controlsLayoutTimer !== null) clearTimeout(controlsLayoutTimer) controlsLayoutTimer = null if (focusTimer !== null) clearTimeout(focusTimer) focusTimer = null keyboardField()?.dismissSoftInput() cancelGestures(); controller.pause() } const resume = () => { foreground = true; controller.resume(); syncBandwidth() } let keyboardObserver: any = null let offSettings: (() => void) | null = null let offBattery: (() => void) | null = null page.on('loaded', () => { if (!keyboardObserver) keyboardObserver = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock( UIKeyboardWillChangeFrameNotification, null, NSOperationQueue.mainQueue, notification => { const frame = notification.userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue const view = page.ios.view const local = view.convertRectFromView(frame, null) root.paddingBottom = Math.max(0, view.bounds.size.height - local.origin.y - view.safeAreaInsets.bottom) }) Application.on(Application.suspendEvent, pause); Application.on(Application.resumeEvent, resume) offSettings?.(); offSettings = onAnySettingChanged(() => { model.refreshDisplayControls(); syncBandwidth() }) offBattery?.(); offBattery = shell.onBatteryLevelsChanged(refreshBatteryLine) model.refreshDisplayControls(); refreshBatteryLine(); resume() if (!isPreviewOnlyMode() && !isAutoReconnectSuppressed() && !deviceAddressError(loadDeviceAddresses())) void controller.connect() }) page.on('unloaded', () => { if (keyboardObserver) NSNotificationCenter.defaultCenter.removeObserver(keyboardObserver) keyboardObserver = null root.paddingBottom = 0 pause(); offSettings?.(); offSettings = null; offBattery?.(); offBattery = null Application.off(Application.suspendEvent, pause); Application.off(Application.resumeEvent, resume) }) return page } export const createPage = createMainPage