Files
faceclaw/app/phone-ui/main-view-model.ts
T
James Babcock b99aa85964 Move phone screen connection status and warnings to the top bar
<details>
<summary>Prompts Used</summary>
On the main page, there's a triple-dot menu. Its icon is currently black-on-dark (this is true on both a light-mode device and a dark-mode device; the top bark has a dark background in both cases). Make that icon white instead.

We have a connection-status indicator on the main page, that says "Disconnected" or "Connected" or "Reconnecting". Let's move this onto the top-bar, as white text, right-aligned next to the triple-dot menu.

We have several problem-status warnings, eg for not having battery-optimization permission, that appear on the main page in the form of a large button below the screen preview. Let's convert all of thes into a yellow triangle ! icon that appears on the top bar, in between the connection-status indicator and the triple-dot menu. Tapping it will open a modal that shows the message that was previously shown, with a button to do the suggested action.

> All three changes are in, and the project builds and typechecks cleanly. Here's what I did:
>
> **White overflow icon** — The root cause is that NativeScript builds its toolbar programmatically, so the `theme` overlay in `NativeScriptToolbarStyle` (an XML-inflation-time attribute) never applied, and the overflow icon fell back to the light activity theme's dark `textColorSecondary`. I added an `actionOverflowButtonStyle` with a white `android:tint` to `AppThemeBase` in `App_Resources/Android/src/main/res/values/styles.xml`, so the icon is white on every page's bar (they all share the dark #424242 background), in both light and dark device themes.
>
> **Connection status in the top bar** — The ActionBar now has a custom titleView (`main-page.xml:5`): "Faceclaw" on the left, and a right-aligned white status label next to the menu. Since the old status string could be a whole sentence ("Reconnecting after ...", "Failed: ..."), the bar shows a short label derived from the connection phase — Connected / Connecting / Reconnecting / Disconnected / Charging / Disconnecting / Failed — and tapping it opens an alert with the full status message, so failure details aren't lost. The old status label below the controls is removed.
>
> **Warnings → ⚠️ icon + modal** — The three inline warning blocks (Even-app conflict, firmware, battery optimization) are gone from the page body in both portrait and landscape. A yellow warning-triangle emoji now sits between the status label and the overflow menu whenever any warning is active. Tapping it opens an in-page modal (same overlay pattern as the existing text-setting editor) titled "Attention needed", listing each active warning with its original message and action button, plus a Close button. The modal closes when you take an action, and auto-closes if the last warning clears while it's open. One side effect: the landscape layout previously showed the firmware warning without its install button — in the modal it now has the button like everything else.
>
> Worth an on-device check: the overflow-icon tint (theme change, so a full reinstall rather than hot reload), and that the titleView labels sit vertically centered against the toolbar height.
<hr>
Thanks. That all looks good, except there are some stylign issues on the warning modal. On a wide tablet, the modal is full-width, which is too wide. When the device is dark-mode, the "Attention needed" title is white-on-light-gray. The title and the warning text need some left/right padding between them and the edges of the modal.

> All three styling issues are fixed and the build is clean:
>
> - **Too wide on tablets**: the modal's width now comes from a `warningsModalWidth` getter in the view model — screen width minus the margins on phones, capped at 480 DIPs on tablets — and it's centered horizontally. It re-computes on orientation change along with the other layout metrics.
> - **White title in dark mode**: the theme's `.ns-dark .h2` rule was beating my `.warnings-modal Label` rule on specificity. I added a `.ns-dark .warnings-modal Label` selector so all text in the modal stays dark-on-light in both modes (the modal deliberately keeps its light look, matching the text-setting modal).
> - **Missing edge padding**: NativeScript ScrollViews don't apply their own padding to their content, so the `padding: 16` on the modal was silently doing nothing. It now lives on an inner `warnings-modal-content` StackLayout; the border/background/margin stay on the ScrollView.
>
> One note: the text-setting editor modal (`system-name-modal`) uses the same `h2`-on-light-background pattern, so its title likely has the same white-on-light problem in dark mode — I left it alone since you only flagged the warnings modal, but it'd be the same one-line fix if you want it.
</details>
2026-08-25 23:09:21 -07:00

984 lines
34 KiB
TypeScript

import {
Application,
Dialogs,
Frame,
ImageSource,
Observable,
Screen,
SwipeDirection,
type GestureEventData,
type SwipeGestureEventData,
type TouchGestureEventData,
type View,
} from "@nativescript/core";
import { dashboardController, type MirrorTouchKind } from "../g2/dashboard-controller";
import { shell } from "../ui/shell/shell";
import {
brightnessSetting,
DISPLAY_MODE_VALUES,
displayModeLabel,
displayModeSetting,
mirrorTouchSetting,
onAnySettingChanged,
showBrightnessControlSetting,
showScreenSizeSetting,
type BrightnessSetting,
type DisplayModeSetting,
} from "../ui/dashboard-settings";
import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses";
import { isAutoReconnectSuppressed, resumeAutoReconnect } from "../g2/reconnect-policy";
import { formatErrorMessage } from "../util/format-error";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
const LENS_ASPECT_RATIO = G2_LENS_WIDTH / G2_LENS_HEIGHT;
type LayoutOrientation = "portrait" | "landscape";
export class MainViewModel extends Observable {
private _status = "Disconnected.";
private _displayPreview: ImageSource | null = null;
private _displayPreviewMessage = "";
private _layoutOrientation: LayoutOrientation = this.readLayoutOrientation();
private _activeTextSettingId: string | null = null;
private _activeTextEditorTitle = "";
private _activeTextSettingTitle = "";
private _activeTextSettingValue = "";
private _activeTextSettingInputKind: "text" | "email" | "password" = "text";
private _secondaryTextSettingId: string | null = null;
private _secondaryTextSettingTitle = "";
private _secondaryTextSettingValue = "";
private _secondaryTextSettingInputKind: "text" | "email" | "password" = "text";
private _evenAppConflictMessage = "";
private _evenAppConflictWarningVisible = false;
private _firmwareWarningMessage = "";
private _firmwareWarningVisible = false;
private _screenRecordingActive = false;
private _batteryOptimizationWarningVisible = false;
private _warningsModalVisible = false;
private _phase: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" = "disconnected";
// A new view model is built on every navigation to the main page; these
// module-level subscriptions must die with it (see dispose) or each
// round-trip to another page leaks a listener that pins the dead model.
private readonly unsubscribers: Array<() => void> = [];
constructor() {
super();
this.attach();
}
/**
* Subscribe to the controller and settings. Idempotent; the page calls it
* again from `loaded` because an app suspend fires unloaded/loaded (which
* dispose the model) without a navigation building a fresh one. Subscribing
* delivers the current snapshot, so a re-attached model catches up.
*/
attach(): void {
if (this.unsubscribers.length > 0) return;
this.unsubscribers.push(dashboardController.subscribe((snapshot) => {
this.status = snapshot.status;
this.displayPreview = snapshot.displayPreview;
this.displayPreviewMessage = snapshot.displayPreviewMessage;
this.phase = snapshot.phase;
this.activeTextSettingId = snapshot.activeTextSettingId;
this.activeTextEditorTitle = snapshot.activeTextEditorTitle;
this.activeTextSettingTitle = snapshot.activeTextSettingTitle;
this.activeTextSettingValue = snapshot.activeTextSettingValue;
this.activeTextSettingInputKind = snapshot.activeTextSettingInputKind;
this.secondaryTextSettingId = snapshot.secondaryTextSettingId;
this.secondaryTextSettingTitle = snapshot.secondaryTextSettingTitle;
this.secondaryTextSettingValue = snapshot.secondaryTextSettingValue;
this.secondaryTextSettingInputKind = snapshot.secondaryTextSettingInputKind;
this.evenAppConflictMessage = snapshot.evenAppConflictMessage;
this.evenAppConflictWarningVisible = snapshot.evenAppConflictWarningVisible;
this.firmwareWarningMessage = snapshot.firmwareWarningMessage;
this.firmwareWarningVisible = snapshot.firmwareWarningVisible;
this.screenRecordingActive = snapshot.screenRecordingActive;
this.batteryOptimizationWarningVisible = snapshot.batteryOptimizationWarningVisible;
this.refreshPadFocusLine();
}));
// Brightness / display mode can change from the glasses' Settings app too.
this.unsubscribers.push(onAnySettingChanged(() => this.refreshDisplayControls()));
}
/** Detach from the controller and settings; the page calls this when it lets go of the model. */
dispose(): void {
for (const unsubscribe of this.unsubscribers.splice(0)) {
unsubscribe();
}
}
get status(): string {
return this._status;
}
set status(value: string) {
if (this._status !== value) {
this._status = value;
this.notifyPropertyChange("status", value);
this.notifyPropertyChange("connectionStatusLabel", this.connectionStatusLabel);
}
}
/**
* Short connection indicator for the action bar. The full status string
* (which can be a sentence, e.g. a failure reason) stays available by
* tapping the indicator.
*/
get connectionStatusLabel(): string {
switch (this._phase) {
case "connected":
return "Connected";
case "charging":
return "Charging";
case "disconnecting":
return "Disconnecting";
case "connecting":
// The transport reports retry loops as "Reconnecting..." with the
// phase still "connecting"; keep that distinction visible.
return this._status.startsWith("Reconnecting") ? "Reconnecting" : "Connecting";
default:
return this._status.startsWith("Failed") ? "Failed" : "Disconnected";
}
}
onConnectionStatusTap(): void {
void Dialogs.alert({ title: "Connection status", message: this._status, okButtonText: "OK" });
}
get displayPreview(): ImageSource | null {
return this._displayPreview;
}
set displayPreview(value: ImageSource | null) {
if (this._displayPreview !== value) {
this._displayPreview = value;
this.notifyPropertyChange("displayPreview", value);
this.notifyPropertyChange("hasDisplayPreview", this.hasDisplayPreview);
this.notifyPropertyChange("displayPreviewVisibility", this.displayPreviewVisibility);
}
}
get displayPreviewMessage(): string {
return this._displayPreviewMessage;
}
set displayPreviewMessage(value: string) {
if (this._displayPreviewMessage !== value) {
this._displayPreviewMessage = value;
this.notifyPropertyChange("displayPreviewMessage", value);
this.notifyPropertyChange("displayPreviewMessageVisibility", this.displayPreviewMessageVisibility);
this.notifyPropertyChange("displayPreviewVisibility", this.displayPreviewVisibility);
}
}
get hasDisplayPreview(): boolean {
return this._displayPreview !== null;
}
/**
* The preview and its stand-in message are mutually exclusive and occupy the
* same box, so the form doesn't reflow when one replaces the other.
*/
get displayPreviewVisibility(): "visible" | "collapse" {
return this.hasDisplayPreview && !this._displayPreviewMessage ? "visible" : "collapse";
}
get displayPreviewMessageVisibility(): "visible" | "collapse" {
return this._displayPreviewMessage ? "visible" : "collapse";
}
get displayPreviewHeight(): number {
return Screen.mainScreen.widthDIPs / LENS_ASPECT_RATIO;
}
get landscapeDisplayPreviewWidth(): number {
return Math.floor(this.landscapeDisplayPreviewHeight * LENS_ASPECT_RATIO);
}
get landscapeDisplayPreviewHeight(): number {
// Height keeps the vertical footprint the preview had at the old 2:1
// aspect; the width is derived from it, so a wider lens aspect can't
// grow the preview past the side panel.
const screenWidth = Screen.mainScreen.widthDIPs;
// Must match the landscape grid's fixed side-panel column in main-page.xml.
const sidePanelWidth = 360;
const availableWidth = Math.max(240, Math.floor(screenWidth - sidePanelWidth - 56));
return Math.floor(availableWidth / 2);
}
/** Near-full-width on phones, capped on tablets (the 32 clears the 16 margins). */
get warningsModalWidth(): number {
return Math.min(Screen.mainScreen.widthDIPs - 32, 480);
}
get portraitLayoutVisibility(): "visible" | "collapse" {
return this._layoutOrientation === "portrait" ? "visible" : "collapse";
}
get landscapeLayoutVisibility(): "visible" | "collapse" {
return this._layoutOrientation === "landscape" ? "visible" : "collapse";
}
refreshLayoutMetrics(): void {
const nextOrientation = this.readLayoutOrientation();
if (this._layoutOrientation !== nextOrientation) {
this._layoutOrientation = nextOrientation;
this.notifyPropertyChange("portraitLayoutVisibility", this.portraitLayoutVisibility);
this.notifyPropertyChange("landscapeLayoutVisibility", this.landscapeLayoutVisibility);
}
this.notifyPropertyChange("displayPreviewHeight", this.displayPreviewHeight);
this.notifyPropertyChange("landscapeDisplayPreviewWidth", this.landscapeDisplayPreviewWidth);
this.notifyPropertyChange("landscapeDisplayPreviewHeight", this.landscapeDisplayPreviewHeight);
this.notifyPropertyChange("warningsModalWidth", this.warningsModalWidth);
}
get activeTextSettingId(): string | null {
return this._activeTextSettingId;
}
set activeTextSettingId(value: string | null) {
if (this._activeTextSettingId !== value) {
this._activeTextSettingId = value;
this.notifyPropertyChange("activeTextSettingId", value);
this.notifyPropertyChange("textSettingEditorVisibility", this.textSettingEditorVisibility);
this.notifyPropertyChange("isTextSettingEditorActive", this.isTextSettingEditorActive);
}
}
get activeTextSettingTitle(): string {
return this._activeTextSettingTitle;
}
get activeTextEditorTitle(): string {
return this._activeTextEditorTitle;
}
set activeTextEditorTitle(value: string) {
if (this._activeTextEditorTitle !== value) {
this._activeTextEditorTitle = value;
this.notifyPropertyChange("activeTextEditorTitle", value);
}
}
set activeTextSettingTitle(value: string) {
if (this._activeTextSettingTitle !== value) {
this._activeTextSettingTitle = value;
this.notifyPropertyChange("activeTextSettingTitle", value);
}
}
get activeTextSettingValue(): string {
return this._activeTextSettingValue;
}
set activeTextSettingValue(value: string) {
if (this._activeTextSettingValue !== value) {
this._activeTextSettingValue = value;
this.notifyPropertyChange("activeTextSettingValue", value);
}
}
get activeTextSettingInputKind(): "text" | "email" | "password" {
return this._activeTextSettingInputKind;
}
set activeTextSettingInputKind(value: "text" | "email" | "password") {
if (this._activeTextSettingInputKind !== value) {
this._activeTextSettingInputKind = value;
this.notifyPropertyChange("activeTextSettingKeyboardType", this.activeTextSettingKeyboardType);
this.notifyPropertyChange("activeTextSettingSecure", this.activeTextSettingSecure);
}
}
get activeTextSettingKeyboardType(): "email" | "text" {
return this._activeTextSettingInputKind === "email" ? "email" : "text";
}
get activeTextSettingSecure(): boolean {
return this._activeTextSettingInputKind === "password";
}
get secondaryTextSettingId(): string | null {
return this._secondaryTextSettingId;
}
set secondaryTextSettingId(value: string | null) {
if (this._secondaryTextSettingId !== value) {
this._secondaryTextSettingId = value;
this.notifyPropertyChange("secondaryTextSettingId", value);
this.notifyPropertyChange("secondaryTextSettingVisibility", this.secondaryTextSettingVisibility);
this.notifyPropertyChange("hasSecondaryTextSetting", this.hasSecondaryTextSetting);
this.notifyPropertyChange("primaryTextSettingReturnKeyType", this.primaryTextSettingReturnKeyType);
}
}
get secondaryTextSettingTitle(): string {
return this._secondaryTextSettingTitle;
}
set secondaryTextSettingTitle(value: string) {
if (this._secondaryTextSettingTitle !== value) {
this._secondaryTextSettingTitle = value;
this.notifyPropertyChange("secondaryTextSettingTitle", value);
}
}
get secondaryTextSettingValue(): string {
return this._secondaryTextSettingValue;
}
set secondaryTextSettingValue(value: string) {
if (this._secondaryTextSettingValue !== value) {
this._secondaryTextSettingValue = value;
this.notifyPropertyChange("secondaryTextSettingValue", value);
}
}
get secondaryTextSettingInputKind(): "text" | "email" | "password" {
return this._secondaryTextSettingInputKind;
}
set secondaryTextSettingInputKind(value: "text" | "email" | "password") {
if (this._secondaryTextSettingInputKind !== value) {
this._secondaryTextSettingInputKind = value;
this.notifyPropertyChange("secondaryTextSettingKeyboardType", this.secondaryTextSettingKeyboardType);
this.notifyPropertyChange("secondaryTextSettingSecure", this.secondaryTextSettingSecure);
}
}
get secondaryTextSettingKeyboardType(): "email" | "text" {
return this._secondaryTextSettingInputKind === "email" ? "email" : "text";
}
get secondaryTextSettingSecure(): boolean {
return this._secondaryTextSettingInputKind === "password";
}
get hasSecondaryTextSetting(): boolean {
return this._secondaryTextSettingId !== null;
}
get secondaryTextSettingVisibility(): "visible" | "collapse" {
return this.hasSecondaryTextSetting ? "visible" : "collapse";
}
get primaryTextSettingReturnKeyType(): "next" | "done" {
return this.hasSecondaryTextSetting ? "next" : "done";
}
get isTextSettingEditorActive(): boolean {
return this._activeTextSettingId !== null;
}
get textSettingEditorVisibility(): "visible" | "collapse" {
return this.isTextSettingEditorActive ? "visible" : "collapse";
}
get evenAppConflictMessage(): string {
return this._evenAppConflictMessage;
}
set evenAppConflictMessage(value: string) {
if (this._evenAppConflictMessage !== value) {
this._evenAppConflictMessage = value;
this.notifyPropertyChange("evenAppConflictMessage", value);
}
}
get evenAppConflictWarningVisible(): boolean {
return this._evenAppConflictWarningVisible;
}
set evenAppConflictWarningVisible(value: boolean) {
if (this._evenAppConflictWarningVisible !== value) {
this._evenAppConflictWarningVisible = value;
this.notifyPropertyChange("evenAppConflictWarningVisible", value);
this.notifyPropertyChange("evenAppConflictWarningVisibility", this.evenAppConflictWarningVisibility);
this.refreshWarningIndicator();
}
}
get evenAppConflictWarningVisibility(): "visible" | "collapse" {
return this._evenAppConflictWarningVisible ? "visible" : "collapse";
}
get firmwareWarningMessage(): string {
return this._firmwareWarningMessage;
}
set firmwareWarningMessage(value: string) {
if (this._firmwareWarningMessage !== value) {
this._firmwareWarningMessage = value;
this.notifyPropertyChange("firmwareWarningMessage", value);
}
}
get firmwareWarningVisible(): boolean {
return this._firmwareWarningVisible;
}
set firmwareWarningVisible(value: boolean) {
if (this._firmwareWarningVisible !== value) {
this._firmwareWarningVisible = value;
this.notifyPropertyChange("firmwareWarningVisible", value);
this.notifyPropertyChange("firmwareWarningVisibility", this.firmwareWarningVisibility);
this.refreshWarningIndicator();
}
}
get firmwareWarningVisibility(): "visible" | "collapse" {
return this._firmwareWarningVisible ? "visible" : "collapse";
}
get screenRecordingActive(): boolean {
return this._screenRecordingActive;
}
set screenRecordingActive(value: boolean) {
if (this._screenRecordingActive !== value) {
this._screenRecordingActive = value;
this.notifyPropertyChange("screenRecordingActive", value);
this.notifyPropertyChange("stopRecordingButtonVisibility", this.stopRecordingButtonVisibility);
}
}
get stopRecordingButtonVisibility(): "visible" | "collapse" {
return this._screenRecordingActive ? "visible" : "collapse";
}
onTakeScreenshotTap(): void {
try {
dashboardController.saveScreenshot();
} catch (error) {
console.error("screenshot failed", error);
}
}
onRecordScreenTap(): void {
try {
dashboardController.startScreenRecording();
} catch (error) {
console.error("screen recording start failed", error);
}
}
onStopRecordingTap(): void {
try {
dashboardController.stopScreenRecording();
} catch (error) {
console.error("screen recording stop failed", error);
}
}
get batteryOptimizationWarningVisible(): boolean {
return this._batteryOptimizationWarningVisible;
}
set batteryOptimizationWarningVisible(value: boolean) {
if (this._batteryOptimizationWarningVisible !== value) {
this._batteryOptimizationWarningVisible = value;
this.notifyPropertyChange("batteryOptimizationWarningVisible", value);
this.notifyPropertyChange("batteryOptimizationWarningVisibility", this.batteryOptimizationWarningVisibility);
this.refreshWarningIndicator();
}
}
get batteryOptimizationWarningVisibility(): "visible" | "collapse" {
return this._batteryOptimizationWarningVisible ? "visible" : "collapse";
}
onAllowBackgroundUsageTap(): void {
this.setWarningsModalVisible(false);
dashboardController.requestBatteryOptimizationExemption();
}
// ---- the action bar's warning triangle and the modal behind it ----
get anyWarningVisible(): boolean {
return (
this._evenAppConflictWarningVisible ||
this._firmwareWarningVisible ||
this._batteryOptimizationWarningVisible
);
}
get warningIconVisibility(): "visible" | "collapse" {
return this.anyWarningVisible ? "visible" : "collapse";
}
get warningsModalVisibility(): "visible" | "collapse" {
return this._warningsModalVisible ? "visible" : "collapse";
}
onWarningIconTap(): void {
this.setWarningsModalVisible(true);
}
onWarningsModalCloseTap(): void {
this.setWarningsModalVisible(false);
}
private setWarningsModalVisible(value: boolean): void {
if (this._warningsModalVisible !== value) {
this._warningsModalVisible = value;
this.notifyPropertyChange("warningsModalVisibility", this.warningsModalVisibility);
}
}
private refreshWarningIndicator(): void {
this.notifyPropertyChange("warningIconVisibility", this.warningIconVisibility);
// Don't leave the modal open showing nothing once the last warning clears.
if (!this.anyWarningVisible) {
this.setWarningsModalVisible(false);
}
}
get phase(): "disconnected" | "connecting" | "connected" | "charging" | "disconnecting" {
return this._phase;
}
set phase(value: "disconnected" | "connecting" | "connected" | "charging" | "disconnecting") {
if (this._phase !== value) {
this._phase = value;
this.notifyPropertyChange("phase", value);
this.notifyPropertyChange("buttonLabel", this.buttonLabel);
this.notifyPropertyChange("canRun", this.canRun);
this.notifyPropertyChange("connectItemEnabled", this.connectItemEnabled);
this.notifyPropertyChange("connectionStatusLabel", this.connectionStatusLabel);
}
}
get buttonLabel(): string {
switch (this.phase) {
case "connecting":
case "connected":
case "charging":
return "Disconnect";
case "disconnecting":
return "Disconnecting...";
default:
return "Connect";
}
}
get canRun(): boolean {
return this.phase !== "connecting" && this.phase !== "disconnecting";
}
/**
* Unlike the other canRun-gated menu items, Connect/Disconnect stays live
* while connecting: Disconnect is the only way out of a reconnection-attempt
* loop short of force-stopping the app.
*/
get connectItemEnabled(): boolean {
return this.phase !== "disconnecting";
}
async onTap(): Promise<void> {
if (!this.connectItemEnabled) return;
try {
if (this.phase === "disconnected") {
await dashboardController.connect();
} else {
await dashboardController.disconnect();
}
} catch (error) {
const message = this.formatError(error);
if (!this.status.startsWith("Failed:")) {
this.status = `Failed: ${message}`;
}
}
}
/**
* Try to connect automatically on reaching the main page. No-op if already
* connecting/connected, if the app is in the manual-disconnected state
* (the user picked Disconnect, the flash flow owns the glasses, or the
* firmware was found incompatible), or if no glasses are configured
* (e.g. preview-only users, who have nothing to connect to).
*/
async autoConnect(): Promise<void> {
if (this.phase !== "disconnected") return;
if (isAutoReconnectSuppressed()) return;
const addresses = loadDeviceAddresses();
if (!isValidMacAddress(addresses.right) || !isValidMacAddress(addresses.left)) return;
try {
await dashboardController.connect();
} catch {
// The controller surfaces failures via status/log; nothing to add here.
}
}
onConfigureTap(): void {
if (!this.canRun) return;
Frame.topmost()?.navigate("phone-ui/config-page");
}
onPermissionsTap(): void {
Frame.topmost()?.navigate({
moduleName: "phone-ui/permissions-page",
context: { onboarding: false },
});
}
/**
* Live scan that names each nearby pair by model, colour, and serial and
* checks both arms belong together. A connected arm stops advertising, so
* drop the current link first. disconnect() enters the manual-disconnected
* state; pairing is a detour, not a Disconnect, so lift the suppression
* right away — nothing dials the glasses until the main page's autoConnect
* runs again on the way back.
*/
async onPairGlassesTap(): Promise<void> {
if (!this.canRun) return;
if (this.phase === "connected" || this.phase === "charging" || this.phase === "connecting") {
try {
await dashboardController.disconnect();
} catch {
// proceed anyway; the pairing page reports what it hears
}
resumeAutoReconnect();
}
Frame.topmost()?.navigate({ moduleName: "phone-ui/pairing-page", context: { onboarding: false } });
}
async onInstallFirmwareTap(): Promise<void> {
this.setWarningsModalVisible(false);
await this.openFlashPage("install");
}
async onUninstallFirmwareTap(): Promise<void> {
await this.openFlashPage("uninstall");
}
private async openFlashPage(mode: "install" | "uninstall"): Promise<void> {
// The flasher needs the glasses to itself, so drop the main connection first.
if (this.phase === "connected" || this.phase === "charging") {
try {
await dashboardController.disconnect();
} catch {
// proceed anyway; the flash page surfaces any connection trouble
}
}
Frame.topmost()?.navigate({
moduleName: "phone-ui/onboarding-flash-page",
context: { mode, fromOnboarding: false },
});
}
onTextSettingTextChange(args: { value?: string; object?: { text?: string } }): void {
dashboardController.setActiveTextSettingValue(
args.object?.text ?? args.value ?? "",
this.activeTextSettingId ?? undefined,
);
}
onPrimaryTextSettingReturnPress(args: { object?: { text?: string; page?: { getViewById?: (id: string) => { focus?: () => void } | null } } }): void {
// Commit the field's actual text at done-time, in case the final
// keystroke's textChange hadn't landed yet.
const text = args?.object?.text;
if (typeof text === "string") {
dashboardController.setActiveTextSettingValue(text, this.activeTextSettingId ?? undefined);
}
if (this.hasSecondaryTextSetting) {
args.object?.page?.getViewById?.("secondarySettingsTextField")?.focus?.();
return;
}
dashboardController.finishActiveTextSettingEdit();
}
onSecondaryTextSettingTextChange(args: { value?: string; object?: { text?: string } }): void {
dashboardController.setActiveTextSettingValue(
args.object?.text ?? args.value ?? "",
this.secondaryTextSettingId ?? undefined,
);
}
onSecondaryTextSettingReturnPress(args: { object?: { text?: string } }): void {
const text = args?.object?.text;
if (typeof text === "string") {
dashboardController.setActiveTextSettingValue(text, this.secondaryTextSettingId ?? undefined);
}
dashboardController.finishActiveTextSettingEdit();
}
onOpenEvenAppSettingsTap(): void {
this.setWarningsModalVisible(false);
dashboardController.openEvenAppSettings();
}
async onSyntheticUpTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("scroll-up");
}
async onSyntheticDownTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("scroll-down");
}
async onSyntheticLeftTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("double-click");
}
async onSyntheticRightTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("click");
}
async onSyntheticLongPressTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("long-press");
}
async onSyntheticMicTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("wakeword");
}
// ---- the phone's own controller: touchpad, d-pad, mirror touch ----
//
// Everything here is the watch scheme (origin "watch"): spatial swipes,
// tap = select, double-tap / two fingers = back, hold = menu. The ring row
// above stays on the ring's own scheme.
private padTwoFingerDown = false;
/** What the next gesture lands on, as the watch pad shows it. */
get padFocusLine(): string {
if (this.phase !== "connected" && this.phase !== "charging") return "Glasses disconnected";
if (!shell.isScreenOn()) return "Display off";
const foreground = shell.getForegroundApp();
return foreground ? foreground.title : "Launcher";
}
private refreshPadFocusLine(): void {
this.notifyPropertyChange("padFocusLine", this.padFocusLine);
}
async onPadTap(): Promise<void> {
if (this.padTwoFingerDown) return;
await dashboardController.injectSyntheticRingInput("click", "watch");
this.refreshPadFocusLine();
}
async onPadDoubleTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("double-click", "watch");
this.refreshPadFocusLine();
}
async onPadLongPress(): Promise<void> {
await dashboardController.injectSyntheticRingInput("long-press", "watch");
this.refreshPadFocusLine();
}
async onPadSwipe(args: SwipeGestureEventData): Promise<void> {
await dashboardController.injectSyntheticRingInput(swipeKind(args.direction), "watch");
this.refreshPadFocusLine();
}
/** Two fingers down and up without moving: back (the watch's two-finger tap). */
async onPadTouch(args: TouchGestureEventData): Promise<void> {
const count = args.getPointerCount();
if (args.action === "down" || args.action === "move") {
if (count >= 2) this.padTwoFingerDown = true;
return;
}
if (args.action === "up" || args.action === "cancel") {
const twoFinger = this.padTwoFingerDown;
if (twoFinger) {
// Let the single-tap recognizer's delayed tap see the flag first.
setTimeout(() => {
this.padTwoFingerDown = false;
}, 400);
if (args.action === "up") {
await dashboardController.injectSyntheticRingInput("double-click", "watch");
this.refreshPadFocusLine();
}
}
}
}
async onDpadUp(): Promise<void> {
await dashboardController.injectSyntheticRingInput("swipe-up", "watch");
this.refreshPadFocusLine();
}
async onDpadDown(): Promise<void> {
await dashboardController.injectSyntheticRingInput("swipe-down", "watch");
this.refreshPadFocusLine();
}
async onDpadLeft(): Promise<void> {
await dashboardController.injectSyntheticRingInput("swipe-left", "watch");
this.refreshPadFocusLine();
}
async onDpadRight(): Promise<void> {
await dashboardController.injectSyntheticRingInput("swipe-right", "watch");
this.refreshPadFocusLine();
}
async onDpadSelect(): Promise<void> {
await dashboardController.injectSyntheticRingInput("click", "watch");
this.refreshPadFocusLine();
}
async onBackTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("double-click", "watch");
this.refreshPadFocusLine();
}
async onMenuTap(): Promise<void> {
await dashboardController.injectSyntheticRingInput("long-press", "watch");
this.refreshPadFocusLine();
}
// ---- touching the mirror itself ----
// Toggled from Settings > Phone display > Touch mirror; read per gesture,
// so no change notification is needed.
get mirrorTouchEnabled(): boolean {
return mirrorTouchSetting.get();
}
private mirrorFraction(args: GestureEventData & { getX?: () => number; getY?: () => number }): { nx: number; ny: number } | null {
const view = args.object as View | undefined;
const size = view?.getActualSize?.();
if (!view || !size || !size.width || !size.height || !args.getX || !args.getY) return null;
// NativeScript's gesture getX/getY are view-local DIPs (the view's own
// MotionEvent coordinates), so they divide straight into the view's size.
return { nx: args.getX() / size.width, ny: args.getY() / size.height };
}
private async mirrorGesture(kind: MirrorTouchKind, args: GestureEventData): Promise<void> {
if (!this.mirrorTouchEnabled) return;
const at = this.mirrorFraction(args) ?? { nx: 0.5, ny: 0.5 };
await dashboardController.handleMirrorTouch(kind, at.nx, at.ny);
this.refreshPadFocusLine();
}
onMirrorTap(args: GestureEventData): Promise<void> {
return this.mirrorGesture("tap", args);
}
onMirrorDoubleTap(args: GestureEventData): Promise<void> {
return this.mirrorGesture("double-tap", args);
}
onMirrorLongPress(args: GestureEventData): Promise<void> {
return this.mirrorGesture("long-press", args);
}
onMirrorSwipe(args: SwipeGestureEventData): Promise<void> {
return this.mirrorGesture(swipeKind(args.direction), args);
}
// ---- display mode and brightness, beside the mirror ----
//
// Both controls are hidden unless enabled in Settings > Phone display
// (the settings themselves stay reachable through the Settings app).
get screenSizeControlVisibility(): "visible" | "collapse" {
return showScreenSizeSetting.get() ? "visible" : "collapse";
}
get brightnessControlVisibility(): "visible" | "collapse" {
return showBrightnessControlSetting.get() ? "visible" : "collapse";
}
get displayControlsVisibility(): "visible" | "collapse" {
return showScreenSizeSetting.get() || showBrightnessControlSetting.get() ? "visible" : "collapse";
}
get displayModeLabel(): string {
return displayModeLabel(displayModeSetting.get()) + " ▾";
}
async onDisplayModeTap(): Promise<void> {
const current = displayModeSetting.get();
const options = DISPLAY_MODE_VALUES.map((value) => displayModeLabel(value) + (value === current ? " ✓" : ""));
const picked = await Dialogs.action({ title: "Display mode", cancelButtonText: "Cancel", actions: options });
const index = options.indexOf(picked);
if (index < 0) return;
const value = DISPLAY_MODE_VALUES[index] as DisplayModeSetting;
if (value !== current) displayModeSetting.set(value);
this.notifyPropertyChange("displayModeLabel", this.displayModeLabel);
}
get brightnessAuto(): boolean {
return brightnessSetting.get() === "auto";
}
get brightnessSliderEnabled(): boolean {
return !this.brightnessAuto;
}
/** The slider's position; while Auto, the last manual level (or 50). */
get brightnessPercent(): number {
const value = brightnessSetting.get();
if (value === "auto") return this.lastManualBrightness;
const numeric = parseInt(value, 10);
return Number.isFinite(numeric) ? numeric : 50;
}
private lastManualBrightness = 50;
onBrightnessChange(args: { value?: number; object?: { value?: number } }): void {
if (this.brightnessAuto) return;
const raw = typeof args.value === "number" ? args.value : Number(args.object?.value ?? NaN);
if (!Number.isFinite(raw)) return;
// The setting only has every tenth level; snap to the nearest.
const level = Math.min(100, Math.max(0, Math.round(raw / 10) * 10));
this.lastManualBrightness = level;
const value = String(level) as BrightnessSetting;
if (brightnessSetting.get() !== value) brightnessSetting.set(value);
}
onBrightnessAutoChange(args: { value?: boolean; object?: { checked?: boolean } }): void {
const on = typeof args.value === "boolean" ? args.value : Boolean(args.object?.checked);
if (on) {
if (brightnessSetting.get() !== "auto") {
this.lastManualBrightness = this.brightnessPercent;
brightnessSetting.set("auto");
}
} else if (brightnessSetting.get() === "auto") {
brightnessSetting.set(String(this.lastManualBrightness) as BrightnessSetting);
}
this.refreshDisplayControls();
}
private refreshDisplayControls(): void {
this.notifyPropertyChange("brightnessAuto", this.brightnessAuto);
this.notifyPropertyChange("brightnessSliderEnabled", this.brightnessSliderEnabled);
this.notifyPropertyChange("brightnessPercent", this.brightnessPercent);
this.notifyPropertyChange("displayModeLabel", this.displayModeLabel);
this.notifyPropertyChange("screenSizeControlVisibility", this.screenSizeControlVisibility);
this.notifyPropertyChange("brightnessControlVisibility", this.brightnessControlVisibility);
this.notifyPropertyChange("displayControlsVisibility", this.displayControlsVisibility);
}
private readLayoutOrientation(): LayoutOrientation {
const applicationOrientation = Application.orientation();
if (applicationOrientation === "landscape" || applicationOrientation === "portrait") {
return applicationOrientation;
}
return Screen.mainScreen.widthDIPs > Screen.mainScreen.heightDIPs ? "landscape" : "portrait";
}
private formatError(error: unknown): string {
return formatErrorMessage(error, 240);
}
}
/** NativeScript swipe direction -> the watch-scheme directional gesture. */
function swipeKind(direction: SwipeDirection): "swipe-up" | "swipe-down" | "swipe-left" | "swipe-right" {
switch (direction) {
case SwipeDirection.up:
return "swipe-up";
case SwipeDirection.down:
return "swipe-down";
case SwipeDirection.left:
return "swipe-left";
default:
return "swipe-right";
}
}