Files
faceclaw/app/phone-ui/main-view-model.ts
T
James Babcock b9e5e56ac8 Refine the UI of the main screen bottom area
<details>
<summary>Prompts Used</summary>
Thanks. Next up, we're going to work on the main screen bottom input area.

We will have a distinct state for keyboard-input mode (ie when you select a setting with a text field). While in that state, we'll hide other inputs (ie the simulated watch and ring input buttons) and put the text input (which is currently a modal on top of the screen preview) in the space below the screen preview. If it doesn't fit, given the reduced screen height while the keyboard is open, it will overlap the screen preview. When leaving a text-input screen, we will close the keyboard.

When the keyboard isn't open, we will have a tab bar-style row of buttons underneath the screen preview, with three tabs: Settings, Watch, and Ring. The Settings tab will have brightness and screen size options (we will drop the options in the Phone Display section of the Settings app which would hide these settings). The Ring tab will have a set of buttons corresponding to simulated R1 ring inputs (the main page has recently had this, but currently doesn't seem to). The Watch tab will have a simulated watch face for simulated Android Wear watch inputs (this is currently on the main page).

> Done — builds and typechecks cleanly. Here's the shape of it:
>
> **Keyboard-input mode**
> - When a text setting becomes active, the whole tabbed controls area collapses (`controlsVisibility` on the `rc:remote-controls` instances in both orientations), leaving the space below the preview for the editor.
> - The text editor overlay is now anchored to the **bottom** of the page instead of the top. Combined with the keyboard resizing the page, it sits in the space below the preview and only rides up over the preview when the keyboard leaves too little room. To make that resize behavior deterministic, I added `android:windowSoftInputMode="adjustResize"` to the activity in the manifest (previously unset, so Android picked pan-vs-resize itself; note this applies app-wide, which is generally the better behavior for the other pages with text fields too).
> - When the glasses navigate away from the text-input screen (`activeTextSettingId` goes null), the page now dismisses the soft keyboard, and the tab area comes back.
>
> **Tab bar** (`remote-controls.xml` rewritten)
> - Three tabs — Settings, Watch, Ring — as a segmented row of buttons; the selected tab is remembered across page navigations (module-level, in-memory), defaulting to Watch.
> - **Settings**: screen-size picker (labeled row) and the brightness slider + Auto switch, now always shown.
> - **Watch**: the simulated watch face unchanged (touchpad with d-pad overlay and legend) plus the Back/Menu row.
> - **Ring**: a 3×2 grid of larger labeled buttons for the R1 inputs — ▲ Up, ▼ Down, ● Click, ●● Double, ‒ Hold, and Hey Even (the wakeword, which moved here from the old shared action row since it's a ring-scheme input).
>
> **Settings app**: `showBrightnessControlSetting` and `showScreenSizeSetting` are deleted outright (definitions, menu items, and the view-model visibility getters). The Phone display section keeps Preview color and Touch mirror. Users' old stored values for the deleted keys are simply orphaned, which is harmless.
>
> Things worth checking on hardware: the editor's position with the keyboard open (especially on a short screen where it should overlap the preview), keyboard dismissal when backing out of a text setting on the glasses, and dark-mode contrast of the tab buttons (I inverted the selected/unselected colors for `.ns-dark`).
<hr>
Keyboard-open view: Looks good. One minor (preexisting) issue: The input title (eg "Agent bridge port") appears twice.

Keyboard-closed view: It looks like the tabs are vertically centered when they should be closer to the screen preview; and, the tab contents don't seem to be visible at all.

> Both issues fixed, verified on the tablet, and the fixed build is installed on both devices.
>
> **Tab bar centered / contents invisible** — I reproduced it on the tablet and traced it with a native view-hierarchy dump. The visibility bindings and tab switching were actually working fine; the bug was a NativeScript layout quirk: a `GridLayout` with no `rows` attribute gets a single implicit row that behaves like `*` (star). The old page measured the controls inside an `auto` grid row (unbounded, so the star row collapsed to content), but the restructured page puts them in the outer grid's `*` row (bounded measure) — so the rows-less tab-bar grid ballooned to fill all remaining height, centered its buttons in the stretch, and left zero height for the tab contents below it. The fix is explicit `rows="auto"` on the tab bar, both Settings rows, and the Back/Menu row in `remote-controls.xml`. Verified all three tabs on-device: Watch shows the touchpad + Back/Menu, Settings shows the screen-size picker and brightness row, Ring shows the six input buttons, all sitting directly under the preview.
>
> **Duplicate input title** — for a single-field edit, the editor title and the field's own label are the same string (`editorTitle` defaults to the field label), and the page rendered both. The per-field label is now only shown when a second field exists (where the labels actually distinguish the fields); single-field edits show just the one heading.
>
> One thing I noticed but couldn't test while disconnected: the keyboard-mode change is untouched except for that label, so it should still look as you saw it. There's also a pre-existing console error on page load — `keyboardType="text"` is not a valid NativeScript keyboard type (only email/number/etc. are), so the binding logs an error and falls back to the default keyboard. Harmless, but the clean fix would be to leave `keyboardType` unset for plain text; happy to do that if you want.
<hr>
Thanks, that is now working. Next up let's add a microphone button to the tab bar, as the rightmost button. Use an icon rather than text, and make it narrower than the others. The button will leave the tab selection unchanged, and trigger as if the wakeword ("Hey Even") was spoken. This will replace the "Hey Even" button in the Ring section.

In the Watch section, we will vertically center the emulated-watch UI within the remaining space beneath the tab bar, and set a maximum width corresponding to an aspect ratio for the simulated watch face of 1.5:1.

> Done and verified on the tablet; both devices have the new build.
>
> **Mic button** — added as a fourth, narrower (56dp) column at the right end of the tab bar, showing the 🎤 emoji. It uses the plain tab style, never shows the selected state, and taps route to the existing `onSyntheticMicTap` (ring "wakeword"), leaving the tab selection untouched. The "Hey Even" button is gone from the Ring tab, which now has just the five ring inputs (Up / Down / Click on the top row, Double / Hold below).
>
> **Watch tab centering + aspect cap** — the component root is now a `rows="auto,*"` grid (tab bar on top, the three sections sharing the star cell), and the portrait page gives the controls the full star row (with Stop recording moved to an `auto` row beneath, so it still appears when recording). The watch section — face plus the Back/Menu row — is `verticalAlignment="center"` in the leftover height and width-capped at `min(available width, 1.5 × 230dp face height ≈ 345dp)` via a new `watchFaceWidth` getter that recomputes on orientation change (in landscape the side panel's 328dp effectively governs). Verified on the tablet: the face sits centered between the tab bar and screen bottom at the capped width, and Settings/Ring stay top-aligned.
>
> One aesthetic note: on the Samsung tablet the 🎤 emoji renders as a gray studio mic rather than a colorful one — legible, but if you'd rather have a crisper icon I could switch to a font-icon or a tinted drawable.
<hr>
Thanks. For the microphone button, let's use `~/Downloads/noun-microphone-1368757.svg`.

> Done — the mic button now uses your Noun Project SVG instead of the emoji, verified on the tablet and installed on both devices.
>
> Since NativeScript Buttons are text-only, I converted the SVG's two paths into an Android VectorDrawable (`App_Resources/Android/src/main/res/drawable/ic_tab_mic.xml`, with a comment crediting the source file) and made the mic control a button-styled GridLayout tile holding a tinted `Image`. It keeps the 56dp width and the `.tab-button` look, plus a small `android-elevation` so it casts the same shadow as the real MaterialButtons beside it. The icon tints `#333333` in light mode and `#cccccc` in dark mode via `tint-color`, matching the tab label colors, and the tap still fires the wakeword without touching tab selection.
>
> One licensing note: Noun Project icons are CC BY unless you have a Pro license — if you don't, the icon's creator should be credited somewhere user-visible (e.g. an about/credits page) before a public release.
<hr>
(I do have a Nounproject pro license)
Thanks, the icon renders correctly, but it's off-center (top and left rather than centered on both axes), and the height and drop shadow of the mic button now doesn't match the other buttons in the tab strip.

> Fixed and verified on the tablet; both devices have the build.
>
> Two things were wrong, both variations of the same NativeScript grid quirk from before:
>
> - **Off-center icon**: the tile's row was effectively auto-sized — row heights are locked in at measure time, and inside the tab bar's `auto` row the tile was measured unbounded, so its cell hugged the 20dp icon and the layout-time stretch dumped the leftover space below/right of it. Giving the tile an explicit `height: 48` bounds its measure, so its `rows="*"` cell genuinely fills and the icon's center alignments work — confirmed by native bounds (icon at exactly (72−30)/2 within the tile on both axes).
> - **Height/shadow mismatch**: MaterialButtons render 48dp tall inside a 60dp layout box (6dp built-in top/bottom insets), while the tile was stretching to the full 60dp row. It now gets `margin: 6 2` plus the 48dp height, so its edges and its elevation shadow align with the neighboring buttons.
>
> The 48dp is derived from the tab buttons' current font/padding metrics, so if the tab-button styling ever changes materially, that value would need to follow — I left a comment in `app.css` tying them together.
</details>
2026-08-26 00:44:22 -07:00

1051 lines
37 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,
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";
type ControlsTab = "settings" | "watch" | "ring";
// Survives navigation round-trips (a fresh view model is built per visit) but
// not process restarts.
let lastControlsTab: ControlsTab = "watch";
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);
}
/**
* The simulated watch face (and the Back/Menu row under it) is capped at a
* 1.5:1 face aspect; on narrow phones the available width governs instead.
*/
get watchFaceWidth(): number {
// Must match .touchpad height in app.css.
const faceHeight = 230;
const available =
this._layoutOrientation === "landscape"
? // The landscape side panel column (see main-page.xml) minus its
// m-l-16 margin and the controls' own 8+8 margins.
360 - 32
: Screen.mainScreen.widthDIPs - 56; // p-20 padding + controls margins
return Math.min(available, Math.round(faceHeight * 1.5));
}
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);
this.notifyPropertyChange("watchFaceWidth", this.watchFaceWidth);
}
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);
// Keyboard-input mode: the tabbed controls make way for the editor.
this.notifyPropertyChange("controlsVisibility", this.controlsVisibility);
}
}
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);
}
// ---- the tabbed controls area below the mirror ----
//
// Three tabs: Settings (screen size + brightness), Watch (the simulated
// watch face), Ring (simulated R1 inputs). The whole area collapses while a
// text setting is being edited so the editor gets the space instead.
private _controlsTab: ControlsTab = lastControlsTab;
get controlsVisibility(): "visible" | "collapse" {
return this.isTextSettingEditorActive ? "collapse" : "visible";
}
get settingsTabVisibility(): "visible" | "collapse" {
return this._controlsTab === "settings" ? "visible" : "collapse";
}
get watchTabVisibility(): "visible" | "collapse" {
return this._controlsTab === "watch" ? "visible" : "collapse";
}
get ringTabVisibility(): "visible" | "collapse" {
return this._controlsTab === "ring" ? "visible" : "collapse";
}
get settingsTabClass(): string {
return this._controlsTab === "settings" ? "tab-button tab-button-selected" : "tab-button";
}
get watchTabClass(): string {
return this._controlsTab === "watch" ? "tab-button tab-button-selected" : "tab-button";
}
get ringTabClass(): string {
return this._controlsTab === "ring" ? "tab-button tab-button-selected" : "tab-button";
}
onSettingsTabTap(): void {
this.setControlsTab("settings");
}
onWatchTabTap(): void {
this.setControlsTab("watch");
}
onRingTabTap(): void {
this.setControlsTab("ring");
}
private setControlsTab(tab: ControlsTab): void {
if (this._controlsTab === tab) return;
this._controlsTab = tab;
// Remembered across navigations (module-level) so the page comes back on
// the tab it left on; deliberately not persisted to disk.
lastControlsTab = tab;
this.notifyPropertyChange("settingsTabVisibility", this.settingsTabVisibility);
this.notifyPropertyChange("watchTabVisibility", this.watchTabVisibility);
this.notifyPropertyChange("ringTabVisibility", this.ringTabVisibility);
this.notifyPropertyChange("settingsTabClass", this.settingsTabClass);
this.notifyPropertyChange("watchTabClass", this.watchTabClass);
this.notifyPropertyChange("ringTabClass", this.ringTabClass);
}
// ---- display mode and brightness, on the Settings tab ----
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);
}
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";
}
}