From 6cd7d5fff9640fd3efdc52fd0e36d8391fb758b7 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 28 Aug 2026 18:36:35 -0700 Subject: [PATCH] Watch display shows if glasses are charging or silent mode --- app/g2/dashboard-controller.ts | 41 +++++++++++++++---- app/g2/glasses-display-state.ts | 34 +++++++++++++++ app/g2/wear-remote.ts | 7 +++- app/phone-ui/main-view-model.ts | 5 +-- tests/glasses-display-state.test.cjs | 29 +++++++++++++ tests/tsconfig.json | 1 + wear/PROTOCOL.md | 6 ++- .../main/kotlin/com/faceclaw/wear/Protocol.kt | 7 +++- .../kotlin/com/faceclaw/wear/ui/Common.kt | 5 +++ .../com/faceclaw/wear/ui/StatusScreen.kt | 7 +++- 10 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 app/g2/glasses-display-state.ts create mode 100644 tests/glasses-display-state.test.cjs diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 2ce9de76..66565521 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -66,6 +66,7 @@ import { uninstallEvenHubPackage, } from "../apps/evenhub/installed-apps"; import { closeRunningPackage, launchInstalledPackage } from "../apps/evenhub/manager"; +import { glassesDisplayLabel } from "./glasses-display-state"; type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" | "disconnecting"; @@ -300,12 +301,13 @@ class DashboardController { requestShellRender: () => this.requestShellRender(), onWindowsChanged: () => { this.persistOpenApps(); - this.wearRemote?.schedulePublish(); + // The foreground title is mirrored on both remote-control faces. + this.emit(); }, onScreenStateChanged: (on) => { this.handleScreenStateChanged(on); if (on) this.requestShellRender(); - this.wearRemote?.schedulePublish(); + this.emit(); }, }); // Boot hooks register windows that exist from startup (the launcher, @@ -353,6 +355,8 @@ class DashboardController { status: this.status, glassesLocked: this.glassesLocked, glassesWorn: this.glassesWorn, + silentMode: this.silentMode, + lastHeadsetBattery: this.lastHeadsetBattery, }), appendLog: (line) => this.appendLog(line), }); @@ -847,12 +851,15 @@ class DashboardController { /** * Message to show in place of the display preview, or "" to show the preview. * - * Both cases look identical to a dead pair of glasses from the phone side: - * silent mode blanks the display and swallows input while the BLE session - * stays up, and a battery that just ran out simply stops answering. + * These states look identical to a dead pair of glasses from the phone side: + * charging and silent mode both make the display unavailable while BLE stays + * up, and a battery that just ran out simply stops answering. */ private displayPreviewMessage(): string { - if (this.silentMode && (this.phase === "connected" || this.phase === "charging")) { + if (this.phase === "charging") { + return this.glassesDisplayLabel(); + } + if (this.silentMode && this.phase === "connected") { return "Connected (Silent mode enabled)"; } const connectionFailing = this.phase === "disconnected" || this.phase === "connecting"; @@ -866,6 +873,17 @@ class DashboardController { return ""; } + /** What occupies the foreground-title line on the watch-style controls. */ + glassesDisplayLabel(): string { + return glassesDisplayLabel({ + phase: this.phase, + silentMode: this.silentMode, + screenOn: shell.isScreenOn(), + battery: this.lastHeadsetBattery, + foregroundTitle: shell.getForegroundApp()?.title ?? null, + }); + } + /** * Re-check the Doze exemption (cheap system call, but cached so snapshot() * stays trivial). Called on connect, page load, and after the user answers @@ -1047,16 +1065,21 @@ class DashboardController { this.handlePhoneLockState(locked); }); this.offBattery = communicator.onBatteryState((state) => { - this.lastHeadsetBattery = state.battery >= 0 ? state.battery : null; + // An unavailable reading must not erase the useful last-known value, + // especially while the glasses remain reachable in their case. + const hasBatteryLevel = Number.isInteger(state.battery) && state.battery >= 0 && state.battery <= 100; + if (hasBatteryLevel) this.lastHeadsetBattery = state.battery; shell.setBatteryLevels({ - headset: state.battery, + headset: hasBatteryLevel ? state.battery : this.lastHeadsetBattery, headsetCharging: state.chargingStatus > 0, }); if ((this.phase === "connected" || this.phase === "charging") && this.communicator) { // Repaint the top bar (battery indicators live in the shell chrome). this.requestShellRender(); } - this.wearRemote?.schedulePublish(); + // Battery belongs in the charging stand-in on the phone as well as in + // the watch state, so refresh both consumers on every report. + this.emit(); }); this.offEvenAppConflict = communicator.onEvenAppConflict((message) => { this.refreshEvenAppStatus(); diff --git a/app/g2/glasses-display-state.ts b/app/g2/glasses-display-state.ts new file mode 100644 index 00000000..88f951ae --- /dev/null +++ b/app/g2/glasses-display-state.ts @@ -0,0 +1,34 @@ +export type GlassesDisplayState = { + phase: string; + silentMode: boolean; + screenOn: boolean; + battery: number | null; + foregroundTitle: string | null; +}; + +/** Compact status shown where the foreground-window title normally lives. */ +export function glassesDisplayLabel(state: GlassesDisplayState): string { + // Charging wins over every session-local state: the glasses are in their + // case, so neither the lock screen nor the foreground window is visible. + if (state.phase === "charging") { + return chargingDisplayLabel(state.battery); + } + if (state.phase !== "connected") { + return "Glasses disconnected"; + } + if (state.silentMode) { + return "Silent mode"; + } + if (!state.screenOn) { + return "Display off"; + } + return state.foregroundTitle || "Launcher"; +} + +export function chargingDisplayLabel(battery: number | null): string { + return validBatteryLevel(battery) ? `Charging · G2 ${battery}%` : "Charging"; +} + +function validBatteryLevel(battery: number | null): battery is number { + return battery !== null && Number.isInteger(battery) && battery >= 0 && battery <= 100; +} diff --git a/app/g2/wear-remote.ts b/app/g2/wear-remote.ts index 99af5c67..d2b7f460 100644 --- a/app/g2/wear-remote.ts +++ b/app/g2/wear-remote.ts @@ -102,6 +102,8 @@ export type WearRemoteHost = { status: string; glassesLocked: boolean; glassesWorn: boolean | null; + silentMode: boolean; + lastHeadsetBattery: number | null; }; appendLog: (line: string) => void; }; @@ -196,8 +198,9 @@ export class WearRemote { locked: state.glassesLocked, worn: state.glassesWorn, listening: voiceActivity.isActive(), - battery: battery.headset, - charging: battery.headsetCharging === true, + battery: state.lastHeadsetBattery, + charging: state.phase === "charging" || battery.headsetCharging === true, + silentMode: state.silentMode, foreground: foreground ? { appId: foreground.appId, title: foreground.title } : null, windows: shell.getWindows().map((window) => ({ windowId: window.windowId, diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index eb4beae3..d313362b 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -766,10 +766,7 @@ export class MainViewModel extends Observable { /** 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"; + return dashboardController.glassesDisplayLabel(); } private refreshPadFocusLine(): void { diff --git a/tests/glasses-display-state.test.cjs b/tests/glasses-display-state.test.cjs new file mode 100644 index 00000000..eed75ca9 --- /dev/null +++ b/tests/glasses-display-state.test.cjs @@ -0,0 +1,29 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { chargingDisplayLabel, glassesDisplayLabel } = require("../.test-build/app/g2/glasses-display-state.js"); + +const connected = { + phase: "connected", + silentMode: false, + screenOn: true, + battery: 72, + foregroundTitle: "Music", +}; + +test("the foreground-title line covers visible, dark, and silent displays", () => { + assert.equal(glassesDisplayLabel(connected), "Music"); + assert.equal(glassesDisplayLabel({ ...connected, foregroundTitle: null }), "Launcher"); + assert.equal(glassesDisplayLabel({ ...connected, screenOn: false }), "Display off"); + assert.equal(glassesDisplayLabel({ ...connected, screenOn: false, silentMode: true }), "Silent mode"); +}); + +test("charging takes precedence and includes the last-known G2 battery", () => { + assert.equal( + glassesDisplayLabel({ ...connected, phase: "charging", silentMode: true, screenOn: false }), + "Charging · G2 72%", + ); + assert.equal(chargingDisplayLabel(null), "Charging"); + assert.equal(chargingDisplayLabel(-1), "Charging"); + assert.equal(chargingDisplayLabel(101), "Charging"); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index e8c694a6..daaaeded 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -15,6 +15,7 @@ "../app/g2/ble-proximity.ts", "../app/g2/even-advertisement.ts", "../app/g2/glasses-artwork.ts", + "../app/g2/glasses-display-state.ts", "../app/g2/pairing-candidates.ts" ] } diff --git a/wear/PROTOCOL.md b/wear/PROTOCOL.md index 485c5c9b..a94c2a4e 100644 --- a/wear/PROTOCOL.md +++ b/wear/PROTOCOL.md @@ -49,7 +49,7 @@ dashboard state, re-sent whenever it changes (and at least every 30 s): "protocol": 1, "version": "…", "phase": "connected", "status": "…", "connected": true, "screenOn": true, "locked": false, "worn": true, "listening": false, - "battery": 78, "charging": false, + "battery": 78, "charging": false, "silentMode": false, "foreground": {"appId": "music", "title": "Music"}, "windows": [{"windowId": "…", "title": "…", "appId": "…", "focused": true, "closeable": true, "acceptsText": false}], "apps": [{"appId": "timer", "title": "Timer"}], @@ -60,6 +60,10 @@ dashboard state, re-sent whenever it changes (and at least every 30 s): } ``` +`battery` is the current or last-known G2 percentage. `charging` and +`silentMode` identify the two states in which the glasses display is not +visible even though the BLE session remains available. + `protocol` is reserved for incompatible revisions of this format (nothing checks it yet); `displayMode` mirrors the phone's Display > Display mode and is what the `display-mode` command changes. `crownClockwiseNext` mirrors the diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt index 7221a2ab..f2d0903a 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt @@ -100,9 +100,10 @@ data class PhoneState( val listening: Boolean, /** Display mode wire value ("576x288" | "576x480" | "640x480"). */ val displayMode: String, - // The state item also carries "battery"; the watch doesn't show it (the - // glasses' own top bar does), so it is left unparsed. + /** Current or last-known G2 charge percentage. */ + val battery: Int?, val charging: Boolean, + val silentMode: Boolean, val foreground: AppEntry?, val windows: List, val apps: List, @@ -129,7 +130,9 @@ data class PhoneState( worn = if (o.isNull("worn")) null else o.optBoolean("worn"), listening = o.optBoolean("listening", false), displayMode = o.optString("displayMode", "576x288"), + battery = if (o.isNull("battery")) null else o.optInt("battery", -1).takeIf { it in 0..100 }, charging = o.optBoolean("charging", false), + silentMode = o.optBoolean("silentMode", false), foreground = foreground?.let { AppEntry(it.optString("appId", ""), it.optString("title", "")) }, diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/Common.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/Common.kt index 8dc7b6c9..e571e861 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/Common.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/Common.kt @@ -63,6 +63,8 @@ fun padLine(state: PhoneState?, link: LinkStatus): StatusLine = when { state == null -> StatusLine("Waiting for phone", StatusTone.BUSY) state.remoteEnabled == false -> StatusLine("Watch control off", StatusTone.PROBLEM) state.connected -> when { + state.charging || state.phase == "charging" -> StatusLine(chargingLine(state.battery), StatusTone.BUSY) + state.silentMode -> StatusLine("Silent mode", StatusTone.OFF) state.locked -> StatusLine("Locked", StatusTone.OFF) !state.screenOn -> StatusLine("●● Wake", StatusTone.OFF) state.listening -> StatusLine("Listening…", StatusTone.BUSY) @@ -73,6 +75,9 @@ fun padLine(state: PhoneState?, link: LinkStatus): StatusLine = when { else -> StatusLine("Glasses disconnected", StatusTone.OFF) } +private fun chargingLine(battery: Int?): String = + if (battery != null) "Charging · G2 $battery%" else "Charging" + fun StatusTone.color(): Color = when (this) { StatusTone.GOOD -> Color(0xFF7BD88F) StatusTone.BUSY -> Color(0xFFF2C14E) diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/StatusScreen.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/StatusScreen.kt index a95fa84d..d34f659c 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/StatusScreen.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/StatusScreen.kt @@ -42,10 +42,13 @@ fun StatusScreen(link: PhoneLink, haptics: Haptics, onOpenSettings: () -> Unit) ScalingLazyColumn(state = listState, modifier = Modifier.fillMaxSize()) { item { ListHeader { Text("Glasses") } } item { - // No battery figure here: the glasses' own top bar shows it. + // Ordinarily the glasses' own top bar shows battery. While + // charging that screen is inside the case, so show it here. val phaseLabel = when { state == null -> "No status yet" - connected -> "Connected" + if (state?.charging == true) " · charging" else "" + state?.charging == true || state?.phase == "charging" -> + state?.battery?.let { "Charging · G2 $it%" } ?: "Charging" + connected -> "Connected" state?.phase == "connecting" -> "Connecting…" state?.phase == "disconnecting" -> "Disconnecting…" else -> "Disconnected"