diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java index 3638c03a..8e8f96ef 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java @@ -35,9 +35,11 @@ import java.util.Set; * {@link #handleMessage}; it is forwarded to the JS listener on the thread * that registered it (the main isolate), mirroring FaceclawSettings. Messages * that arrive before a listener exists (the watch poked a phone whose JS - * dashboard is not up yet) are kept for a short while and replayed on - * registration; the watch is told with an ack carrying jsReady=false so it - * can say "open Faceclaw on your phone". + * dashboard is not up yet) are acked with jsReady=false so the watch can say + * "open Faceclaw on your phone"; only idempotent state requests are kept for + * replay on registration — the watch already reported gestures and commands + * as failed, and executing them tens of seconds later would act on whatever + * the glasses happen to show by then. * * Outbound: {@link #publishState} mirrors the dashboard state into a Data * Layer item (delivered even if the watch is out of range right now, and @@ -283,17 +285,20 @@ public final class FaceclawWearBridge { boolean delivered = dispatchMessage(path, json, nodeId); if (delivered) return; - // No JS listener yet: keep the message briefly, and tell the watch so - // it can explain why nothing is happening. - synchronized (lock) { - long now = System.currentTimeMillis(); - while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { - pending.pollFirst(); + // No JS listener yet: tell the watch so it can explain why nothing is + // happening. Only state requests are kept for replay (see class doc); + // everything else is dropped after the failure ack. + if (PATH_STATE_REQUEST.equals(path)) { + synchronized (lock) { + long now = System.currentTimeMillis(); + while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { + pending.pollFirst(); + } + if (pending.size() >= PENDING_LIMIT) { + pending.pollFirst(); + } + pending.addLast(new PendingMessage(path, json, nodeId, now)); } - if (pending.size() >= PENDING_LIMIT) { - pending.pollFirst(); - } - pending.addLast(new PendingMessage(path, json, nodeId, now)); } sendAck(nodeId, readSeq(json), false, false, "Faceclaw is not running on the phone. Open it to connect the glasses."); } diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index b8d3114a..0a9b8a9b 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -56,7 +56,7 @@ import { ALL_APPS } from "../apps/all-apps"; import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition"; import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window"; import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence"; -import { appViewportRect, SIDEBAR_WIDTH, sidebarWidth, type WindowHeightMode } from "../ui/shell/geometry"; +import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry"; import { type LayerActions } from "../ui/layers"; import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, watchWakesDisplaySetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; @@ -1449,6 +1449,13 @@ class DashboardController { return; } if (!shell.isScreenOn()) { + // The same wake policy as the rest of the watch scheme: honour the + // "input wakes display" setting; with it off, only a double-tap (a + // normal double-click) reaches the dark display. + if (!watchWakesDisplaySetting.get()) { + if (kind === "double-tap") await this.injectSyntheticRingInput("double-click", "watch"); + return; + } shell.wake(shell.getFocus()); this.requestShellRender(); } @@ -1458,7 +1465,7 @@ class DashboardController { } const x = Math.round(Math.min(1, Math.max(0, nx)) * G2_LENS_WIDTH); const y = Math.round(Math.min(1, Math.max(0, ny)) * G2_LENS_HEIGHT); - const stripShown = sidebarWidth() > 0 || shell.getFocus() === "sidebar"; + const stripShown = sidebarStripVisible(shell.getFocus()); this.appendLog(`mirror tap at ${x},${y}`); if (!shell.hasOverlay() && stripShown && x < SIDEBAR_WIDTH) { const target = shell.windowAtSidebarPoint(x, y); @@ -2163,6 +2170,18 @@ class DashboardController { private updateCompositePreview(): void { if (!this.communicator) return; + // The floor comes first (and stamps even when backgrounded below): with + // it after the background check, per-frame refresh requests from a + // backgrounded app never advance the timestamp and every one of them + // pays the foreground-activity lookup. + const now = Date.now(); + if ( + this.lastConnectedPreviewUpdateAtMs > 0 && + now - this.lastConnectedPreviewUpdateAtMs < CONNECTED_PREVIEW_MIN_UPDATE_MS + ) { + return; + } + this.lastConnectedPreviewUpdateAtMs = now; // The connected foreground service intentionally keeps this controller // alive after the phone UI is backgrounded. Do not keep constructing // 640x480 Android Bitmaps for a window that cannot display them: besides @@ -2172,14 +2191,6 @@ class DashboardController { const activity = Application.android.foregroundActivity; if (!activity || !activity.hasWindowFocus()) return; } - const now = Date.now(); - if ( - this.lastConnectedPreviewUpdateAtMs > 0 && - now - this.lastConnectedPreviewUpdateAtMs < CONNECTED_PREVIEW_MIN_UPDATE_MS - ) { - return; - } - this.lastConnectedPreviewUpdateAtMs = now; if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) { this.lastRecordCaptureAtMs = now; this.communicator.recordScreenFrame(); diff --git a/app/g2/wear-remote.ts b/app/g2/wear-remote.ts index cae1d982..bfc69543 100644 --- a/app/g2/wear-remote.ts +++ b/app/g2/wear-remote.ts @@ -141,7 +141,7 @@ export class WearRemote { }); shell.onAssistantActivity((event) => this.forwardAssistantActivity(event)); shell.onAlertShown((text) => { - if (watchMirrorAssistantSetting.get()) { + if (this.watchReachable && watchMirrorAssistantSetting.get()) { wearBridge.sendEvent({ type: "alert", text }); } }); @@ -425,8 +425,9 @@ export class WearRemote { ack(true); return; case "close-assistant": - shell.closeAssistant(); - ack(true); + // Idempotent: closing an assistant that isn't open still succeeds, + // but tell the watch which of the two happened. + ack(true, shell.closeAssistant() ? "" : "The assistant was not open."); return; case "display-mode": { const value = String(payload.value ?? ""); @@ -446,6 +447,10 @@ export class WearRemote { private forwardAssistantActivity(event: AssistantActivityEvent): void { if (!watchMirrorAssistantSetting.get()) return; + // No watch to mirror to: sending anyway is not free — with no known + // watch nodes the Java side runs a capability lookup per event, which a + // streaming reply would repeat several times a second. + if (!this.watchReachable) return; if (event.phase === "streaming") { // Coalesce: the newest text wins, sent no more often than the interval. this.assistantPendingText = event.text; diff --git a/app/phone-ui/main-page.ts b/app/phone-ui/main-page.ts index 2e761f31..8692bae2 100644 --- a/app/phone-ui/main-page.ts +++ b/app/phone-ui/main-page.ts @@ -33,6 +33,9 @@ function cleanupPage(page: Page): void { } state.model.off(Observable.propertyChangeEvent, state.propertyChangeHandler) Application.off(Application.orientationChangedEvent, state.orientationHandler) + // navigatingTo builds a fresh model each visit; drop the old one's + // controller/settings subscriptions or every navigation leaks a listener. + state.model.dispose() setPageState(page, undefined) } @@ -77,6 +80,9 @@ export function loaded(args: EventData) { dashboardController.refreshEvenAppStatus() const model = page.bindingContext as MainViewModel | null + // Re-subscribe after a suspend/resume cycle (unloaded disposed the model); + // a no-op on the first load after construction. + model?.attach() // Automatically connect on reaching the main page (no-op if already // connected or if no glasses are configured). void model?.autoConnect() diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index 8bb40afe..b4fa43e4 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -54,9 +54,25 @@ export class MainViewModel extends Observable { private _batteryOptimizationWarningVisible = 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(); - dashboardController.subscribe((snapshot) => { + 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; @@ -77,9 +93,16 @@ export class MainViewModel extends Observable { this.screenRecordingActive = snapshot.screenRecordingActive; this.batteryOptimizationWarningVisible = snapshot.batteryOptimizationWarningVisible; this.refreshPadFocusLine(); - }); + })); // Brightness / display mode can change from the glasses' Settings app too. - onAnySettingChanged(() => this.refreshDisplayControls()); + 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 { @@ -148,7 +171,8 @@ export class MainViewModel extends Observable { // 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; - const sidePanelWidth = 260; + // 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); } @@ -624,7 +648,6 @@ export class MainViewModel extends Observable { // tap = select, double-tap / two fingers = back, hold = menu. The ring row // above stays on the ring's own scheme. - private padPointers = 0; private padTwoFingerDown = false; /** What the next gesture lands on, as the watch pad shows it. */ @@ -664,13 +687,11 @@ export class MainViewModel extends Observable { async onPadTouch(args: TouchGestureEventData): Promise { const count = args.getPointerCount(); if (args.action === "down" || args.action === "move") { - this.padPointers = Math.max(this.padPointers, count); if (count >= 2) this.padTwoFingerDown = true; return; } if (args.action === "up" || args.action === "cancel") { const twoFinger = this.padTwoFingerDown; - this.padPointers = 0; if (twoFinger) { // Let the single-tap recognizer's delayed tap see the flag first. setTimeout(() => { @@ -735,12 +756,9 @@ export class MainViewModel extends Observable { const view = args.object as View | undefined; const size = view?.getActualSize?.(); if (!view || !size || !size.width || !size.height || !args.getX || !args.getY) return null; - // On Android the gesture's getX/getY are screen-absolute (in DIPs), not - // view-local; the view's own screen position makes them local. - const origin = view.getLocationOnScreen?.() ?? { x: 0, y: 0 }; - const localX = args.getX() - origin.x; - const localY = args.getY() - origin.y; - return { nx: localX / size.width, ny: localY / size.height }; + // 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 { diff --git a/app/ui/layers.ts b/app/ui/layers.ts index f36076cc..38f153e8 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -238,6 +238,21 @@ export class LayerStack { } } + /** + * Pop layers down to and including the given one, wherever it sits in the + * stack (e.g. dismissing the assistant while a follow-up voice dialog is + * open above it). Returns false if the layer isn't stacked (the base layer + * can never be popped). Removal callbacks fire top-down. + */ + popThrough(layer: Layer): boolean { + const index = this.layers.indexOf(layer); + if (index <= 0) return false; + for (const removed of this.layers.splice(index).reverse()) { + notifyRemoved(removed); + } + return true; + } + isAtBase(): boolean { return this.layers.length === 1; } @@ -271,9 +286,15 @@ export class LayerStack { await top.handleInput(delivered, this.ctx); } - /** Pass a mirror touch to the base layer when nothing is stacked on it. */ + /** + * Pass a mirror touch to the base layer when nothing is stacked on it. + * With a layer stacked (a menu, a dialog) the touch is consumed without + * acting: positions mean nothing to the overlay, and falling back to a + * blind synthetic click would activate whatever happens to be highlighted + * rather than what was tapped. + */ async hitTest(x: number, y: number): Promise { - if (this.layers.length !== 1) return false; + if (this.layers.length !== 1) return true; const base = this.layers[0]!; if (!base.hitTest) return false; return await base.hitTest(x, y, this.ctx); diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index f5b1eaa7..517b5ffa 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -13,7 +13,7 @@ import { minWindowTop, SHELL_OPAQUE_BLACK, SIDEBAR_WIDTH, - sidebarWidth, + sidebarStripVisible, TOP_BAR_HEIGHT, windowTop, type WindowHeightMode, @@ -51,6 +51,25 @@ function columnLeft(variant: SidebarVariant, column: number): number { return SIDEBAR_WIDTH - (variant.columns - column) * variant.columnWidth; } +/** + * Column and top y of a visible sidebar slot; `position` counts from the + * first visible (scrolled-to) icon. Slots fill the rightmost column top to + * bottom, then overflow leftward. The single source of slot geometry for + * both drawing and hit testing. + */ +function slotPosition(variant: SidebarVariant, listTop: number, position: number): { column: number; y: number } { + const rows = rowsPerColumn(variant); + return { + column: variant.columns - 1 - ((position / rows) | 0), + y: listTop + (position % rows) * (variant.iconSize + ICON_SPACING), + }; +} + +/** Top y of the sidebar's icon list (below the band's top bar). */ +function sidebarListTop(): number { + return minWindowTop() + TOP_BAR_HEIGHT + LIST_MARGIN; +} + /** * Left edge of the sidebar region that actually holds icons: the one-column * variant leaves a dead strip left of its single right-aligned column. @@ -169,7 +188,7 @@ export class ShellChromeLayer implements Layer { const state = this.getState(); // Full-panel mode: the strip is an overlay, present only while the user // is in it (the window underneath keeps its full width the rest of the time). - if (sidebarWidth() > 0 || state.focus === "sidebar") { + if (sidebarStripVisible(state.focus)) { this.drawSidebar(image, state); } this.drawTopBar(image, state); @@ -189,14 +208,10 @@ export class ShellChromeLayer implements Layer { windowIndexAt(x: number, y: number, windowCount: number): number | null { if (x < 0 || x >= SIDEBAR_WIDTH || windowCount === 0) return null; const variant = sidebarVariant(windowCount); - const rows = rowsPerColumn(variant); - const listTop = minWindowTop() + TOP_BAR_HEIGHT + LIST_MARGIN; - const itemStride = variant.iconSize + ICON_SPACING; - const lastVisible = Math.min(windowCount, this.scrollRow + rows * variant.columns); + const listTop = sidebarListTop(); + const lastVisible = Math.min(windowCount, this.scrollRow + rowsPerColumn(variant) * variant.columns); for (let index = this.scrollRow; index < lastVisible; index++) { - const position = index - this.scrollRow; - const column = variant.columns - 1 - ((position / rows) | 0); - const slotY = listTop + (position % rows) * itemStride; + const { column, y: slotY } = slotPosition(variant, listTop, index - this.scrollRow); const left = columnLeft(variant, column); if (x >= left && x < left + variant.columnWidth && y >= slotY - 2 && y < slotY + variant.iconSize + 2) { return index; @@ -223,20 +238,11 @@ export class ShellChromeLayer implements Layer { const iconMarginX = ((variant.columnWidth - iconSize) / 2) | 0; // Column index of the rightmost column (the one that fills first). const firstColumn = variant.columns - 1; - const listTop = bandTop + TOP_BAR_HEIGHT + LIST_MARGIN; - const itemStride = iconSize + ICON_SPACING; - const rows = rowsPerColumn(variant); - const visibleCount = rows * variant.columns; + const listTop = sidebarListTop(); + const visibleCount = rowsPerColumn(variant) * variant.columns; this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, state.selectedIndex, visibleCount, count); const lastVisible = Math.min(count, this.scrollRow + visibleCount); - const slotOf = (index: number) => { - const position = index - this.scrollRow; - const columnsFromRight = (position / rows) | 0; - return { - column: firstColumn - columnsFromRight, - y: listTop + (position % rows) * itemStride, - }; - }; + const slotOf = (index: number) => slotPosition(variant, listTop, index - this.scrollRow); // The selection is a "diversion" of the sidebar/main separator line: the // line bulges right around the selected icon (rounded on the left, open @@ -297,7 +303,7 @@ export class ShellChromeLayer implements Layer { const barTop = windowTop(state.foregroundHeightMode); // The bar spans the app viewport; with the sidebar overlaid (full-panel // mode, sidebar focused) it still starts past the strip. - const barLeft = sidebarWidth() > 0 || state.focus === "sidebar" ? SIDEBAR_WIDTH : 0; + const barLeft = sidebarStripVisible(state.focus) ? SIDEBAR_WIDTH : 0; image.fillRect(barLeft, barTop, G2_LENS_WIDTH - barLeft, TOP_BAR_HEIGHT, SHELL_OPAQUE_BLACK); image.drawLine(barLeft, barTop + TOP_BAR_HEIGHT - 1, G2_LENS_WIDTH - 1, barTop + TOP_BAR_HEIGHT - 1, BORDER_VALUE); diff --git a/app/ui/shell/geometry.ts b/app/ui/shell/geometry.ts index 528c9058..d5ff437e 100644 --- a/app/ui/shell/geometry.ts +++ b/app/ui/shell/geometry.ts @@ -26,6 +26,14 @@ export function sidebarWidth(): number { return displayMode() === "640x480" ? 0 : SIDEBAR_WIDTH; } +/** + * Whether the sidebar strip is on screen: always when it reserves width; in + * the full-panel mode (where it overlays the window) only while it has focus. + */ +export function sidebarStripVisible(focus: "sidebar" | "window"): boolean { + return sidebarWidth() > 0 || focus === "sidebar"; +} + /** * The height an app's requested mode actually gets: the band mode honours * it, the taller modes give every window the whole screen. diff --git a/app/ui/shell/modal-layer.ts b/app/ui/shell/modal-layer.ts index 0ddc62ef..5971e348 100644 --- a/app/ui/shell/modal-layer.ts +++ b/app/ui/shell/modal-layer.ts @@ -4,16 +4,20 @@ import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, Pai import { appViewportRect, appViewportSize, SHELL_OPAQUE_BLACK } from "./geometry"; // The modal covers most of the min-height app viewport, leaving a little of -// the foreground app visible around the edges. Its size is fixed (min-height -// windows have a fixed viewport size); its position follows the vertical -// position setting, so it is computed per paint. +// the foreground app visible around the edges. Both its size and position are +// computed per paint: the display-mode setting changes the viewport size at +// runtime, and the vertical position setting moves the band. const MODAL_MARGIN = 14; const MODAL_PADDING = 4; -export const MODAL_INTERIOR = { - width: appViewportSize("min").width - 2 * MODAL_MARGIN - 2 * MODAL_PADDING, - height: appViewportSize("min").height - 2 * MODAL_MARGIN - 2 * MODAL_PADDING, -} as const; +/** Content (inner layer stack) size of the modal box. */ +function modalInterior(): { width: number; height: number } { + const viewport = appViewportSize("min"); + return { + width: viewport.width - 2 * MODAL_MARGIN - 2 * MODAL_PADDING, + height: viewport.height - 2 * MODAL_MARGIN - 2 * MODAL_PADDING, + }; +} /** Screen rect of the modal box, aligned to the min-height window band. */ export function modalRect(): { x: number; y: number; width: number; height: number } { @@ -36,17 +40,18 @@ export class ShellModalLayer implements Layer { private readonly stack: LayerStack; constructor(baseLayer: Layer, actions: LayerActions) { - this.stack = new LayerStack(baseLayer, actions, { - width: MODAL_INTERIOR.width, - height: MODAL_INTERIOR.height, - }); + this.stack = new LayerStack(baseLayer, actions, modalInterior()); } - paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage { const image = paintBelow(); + // Track a display-mode switch while the modal is up, so the inner stack + // paints at the same size the box below is drawn with. + const interior = modalInterior(); + this.stack.setBaseSize(interior); // Flattening bakes the inner stack's planes (glyphs included) so the blit // below transplants the finished modal content into this layer's plane. - const inner = flattenPlanes(this.stack.paint(), MODAL_INTERIOR); + const inner = flattenPlanes(this.stack.paint(), interior); const rect = modalRect(); image.fillRoundedRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK, 8); image.drawRoundedRect(rect.x, rect.y, rect.width, rect.height, 110, 8); @@ -54,7 +59,7 @@ export class ShellModalLayer implements Layer { return image; } - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + async handleInput(event: DashboardInputEvent, _ctx: LayerContext): Promise { await this.stack.handleInput(event); } } diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index 759ee9e9..6ac5494a 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -43,7 +43,7 @@ import { ToolDebugMenuLayer } from "./tool-debug-layer"; import { toolRegistry } from "../../assistant/tool-registry"; import { minWindowTop, - SIDEBAR_WIDTH, + sidebarWidth, TOP_BAR_HEIGHT, windowBandHeight, windowTop, @@ -157,9 +157,10 @@ const LONG_PRESS_ESCAPE_MENU_MS = 4000; class ShellOverlayMenuLayer extends MenuLayer { constructor(items: MenuItem[], private readonly onClosed: () => void) { // Aligned to the min-height window band (like the sidebar), wherever the - // vertical position setting currently puts it. + // vertical position setting currently puts it; past the sidebar strip + // where one reserves width (none in the full-panel mode). super(null, items, { - x: SIDEBAR_WIDTH + 8, + x: sidebarWidth() + 8, y: minWindowTop() + TOP_BAR_HEIGHT + 8, width: 272, minHeight: 0, @@ -858,9 +859,12 @@ class Shell { } } - /** Dismiss the assistant overlay (cancelling any in-flight turn), as Done would. */ - closeAssistant(): void { - this.closeAssistantLayer(); + /** + * Dismiss the assistant overlay (cancelling any in-flight turn), as Done + * would. Returns whether an overlay was actually open to close. + */ + closeAssistant(): boolean { + return this.closeAssistantLayer(); } private ensureAssistantSession(): AssistantSession | null { @@ -998,13 +1002,16 @@ class Shell { this.config.requestShellRender(); } - private closeAssistantLayer(): void { + private closeAssistantLayer(): boolean { // Popping fires the layer's onRemoved, which cancels the turn and clears - // this.assistantLayer. + // this.assistantLayer. popThrough also closes anything stacked above the + // overlay (a follow-up voice dialog, an alert), so a close command works + // no matter what the conversation is showing. const layer = this.assistantLayer; - if (layer) this.stack.popIfTop((top) => top === layer); + const closed = layer ? this.stack.popThrough(layer) : false; this.noteUserActivity(); this.config.requestShellRender(); + return closed; } /** Show a brief text popup on the lenses (assistant show_alert / notices). */ diff --git a/app/ui/shell/tool-debug-layer.ts b/app/ui/shell/tool-debug-layer.ts index 791f66f6..16abcf3f 100644 --- a/app/ui/shell/tool-debug-layer.ts +++ b/app/ui/shell/tool-debug-layer.ts @@ -5,7 +5,7 @@ import { clamp } from "../../util/numeric-util"; import type { ToolDebugEntry } from "../../assistant/tool-registry"; import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "../layers"; import { MenuLayer, drawListScrollbar, type MenuItem } from "../menu"; -import { MIN_WINDOW_HEIGHT, minWindowTop, SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry"; +import { MIN_WINDOW_HEIGHT, minWindowTop, sidebarWidth, TOP_BAR_HEIGHT } from "./geometry"; import { lineStep } from "../metrics"; /** @@ -15,13 +15,20 @@ import { lineStep } from "../metrics"; * page with its full name, gating state, and description. */ -const DIALOG_X = SIDEBAR_WIDTH + 8; const DIALOG_WIDTH = 420; -const DETAIL_WIDTH = G2_LENS_WIDTH - DIALOG_X - 8; // Shell overlays align to the min-height window band; the detail box fills // the band below the top bar with an 8px margin on each side. const DETAIL_HEIGHT = MIN_WINDOW_HEIGHT - TOP_BAR_HEIGHT - 16; +/** Dialog left edge: past the sidebar strip where one reserves width. */ +function dialogX(): number { + return sidebarWidth() + 8; +} + +function detailWidth(): number { + return G2_LENS_WIDTH - dialogX() - 8; +} + /** Dialog top edge, aligned to the min-height band's content area. */ function dialogY(): number { return minWindowTop() + TOP_BAR_HEIGHT + 8; @@ -31,7 +38,7 @@ const DETAIL_PADDING = 14; export class ToolDebugMenuLayer extends MenuLayer { constructor(appId: string | null, entries: ToolDebugEntry[], private readonly onClosed: () => void) { super(appId ? `Assistant tools: ${appId}` : "Assistant tools", buildItems(appId, entries), { - x: DIALOG_X, + x: dialogX(), y: dialogY(), width: DIALOG_WIDTH, minHeight: 0, @@ -78,12 +85,14 @@ class ToolDetailLayer implements Layer { paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage { const image = paintBelow(); const font = getDefaultSmallFont(); + const left = dialogX(); + const width = detailWidth(); const top = dialogY(); - image.fillRoundedRect(DIALOG_X, top, DETAIL_WIDTH, DETAIL_HEIGHT, 1, 10); - image.drawRoundedRect(DIALOG_X, top, DETAIL_WIDTH, DETAIL_HEIGHT, 72, 10); + image.fillRoundedRect(left, top, width, DETAIL_HEIGHT, 1, 10); + image.drawRoundedRect(left, top, width, DETAIL_HEIGHT, 72, 10); - const textX = DIALOG_X + DETAIL_PADDING; - const textWidth = DETAIL_WIDTH - 2 * DETAIL_PADDING - 8; + const textX = left + DETAIL_PADDING; + const textWidth = width - 2 * DETAIL_PADDING - 8; const lines = this.buildLines(textWidth); const detailLineH = lineStep(font); const visibleRowCount = Math.max(1, ((DETAIL_HEIGHT - 2 * DETAIL_PADDING) / detailLineH) | 0); @@ -98,7 +107,7 @@ class ToolDetailLayer implements Layer { if (lines.length > visibleRowCount) { drawListScrollbar( image, - DIALOG_X + DETAIL_WIDTH - 7, + left + width - 7, top + DETAIL_PADDING, DETAIL_HEIGHT - 2 * DETAIL_PADDING, this.scrollRow, diff --git a/wear/PROTOCOL.md b/wear/PROTOCOL.md index b4489194..b3bc0b46 100644 --- a/wear/PROTOCOL.md +++ b/wear/PROTOCOL.md @@ -19,7 +19,7 @@ answered on `/faceclaw/ack`. | Path | Payload | Effect | | --- | --- | --- | | `/faceclaw/input` | `{gesture, steps?}` | A ring gesture through the same path as the phone UI's test buttons. `gesture` ∈ `click`, `double-click`, `scroll-up`, `scroll-down`, `long-press` (complete short hold), `long-press-start`, `long-press-release`, `wakeword` ("Hey Even"), `swipe-up`, `swipe-down`, `swipe-left`, `swipe-right` (spatial input only a watch can produce: delivered to the glasses UI as `swipe-*` events for components that navigate spatially — launcher grid, settings columns, sidebar, Music — with a scroll / click / double-click fallback elsewhere). `steps` (1–12) repeats a scroll. Refused while the glasses are disconnected. The lock screen is honoured exactly as for the ring (only a double-click reaches it, toggling the locked display). | -| `/faceclaw/command` | `{command, …}` | `launch-app {appId}`, `focus-window {windowId}`, `close-window {windowId}`, `sidebar`, `wake`, `sleep`, `lock`, `unlock`, `connect`, `disconnect`, `close-assistant`. Everything but connect/disconnect needs a connection; everything but lock/unlock is refused while locked; `unlock` also needs the phone's "Watch can unlock glasses" setting. | +| `/faceclaw/command` | `{command, …}` | `launch-app {appId}`, `focus-window {windowId}`, `close-window {windowId}`, `sidebar`, `wake`, `sleep`, `lock`, `unlock`, `connect`, `disconnect`, `close-assistant`, `display-mode {value}` (`value` ∈ `576x288`, `576x480`, `640x480`; sets the phone's Display > Display mode). Everything but connect/disconnect needs a connection; everything but lock/unlock is refused while locked; `unlock` also needs the phone's "Watch can unlock glasses" setting. | | `/faceclaw/assistant` | `{text}` | Send a query to the assistant (`shell.sendToAssistant`) — the reply shows on the glasses and streams back to the watch as events. | | `/faceclaw/text` | `{text}` | Type text into the foreground window (`receiveTextInput`, e.g. the terminal). Refused when the window doesn't take text. | | `/faceclaw/state/request` | `{}` | Re-publish the state item even if unchanged. | @@ -53,10 +53,15 @@ dashboard state, re-sent whenever it changes (and at least every 30 s): "foreground": {"appId": "music", "title": "Music"}, "windows": [{"windowId": "…", "title": "…", "appId": "…", "focused": true, "closeable": true, "acceptsText": false}], "apps": [{"appId": "timer", "title": "Timer"}], + "displayMode": "576x288", "remoteEnabled": true, "canUnlock": true, "mirrorAssistant": true, "assistantAvailable": true } ``` +`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. + `apps` is the launcher grid plus installed EvenHub packages, i.e. exactly what the assistant's `apps.launch` tool accepts. diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/MainActivity.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/MainActivity.kt index 22c795ab..d1697802 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/MainActivity.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/MainActivity.kt @@ -50,6 +50,13 @@ class MainActivity : ComponentActivity() { override fun onStop() { cancelStemHold() + // A stopped activity never sees the key-up, and the phone keeps the + // hold (and its escape-menu countdown) running until it hears the + // release — so end an in-progress hold before dropping the link. + if (stemHoldSent) { + stemHoldSent = false + link.sendGesture(Gesture.LONG_PRESS_RELEASE) + } link.stop() super.onStop() } diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/PhoneLink.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/PhoneLink.kt index 37a7ac25..0934fa9b 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/PhoneLink.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/PhoneLink.kt @@ -267,7 +267,10 @@ class PhoneLink(context: Context) : private suspend fun loadStateItem() { try { - val uri = Uri.Builder().scheme("wear").path(Protocol.PATH_STATE).build() + // The item lives under the phone node's authority + // (wear:///faceclaw/state); the "*" host matches any node, + // where a hostless Uri would match nothing. + val uri = Uri.parse("wear://*${Protocol.PATH_STATE}") val items = dataClient.getDataItems(uri).await() try { for (item in items) applyStateItem(item) 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 1e0c0d71..32916cb0 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/Protocol.kt @@ -7,8 +7,6 @@ import org.json.JSONObject * (paths) and app/g2/wear-remote.ts (payloads); see ../../PROTOCOL.md. */ object Protocol { - const val PROTOCOL_VERSION = 1 - /** Advertised by the phone app so the watch can find it. */ const val CAPABILITY_PHONE = "faceclaw_phone" @@ -92,7 +90,6 @@ data class WindowEntry( /** The phone's mirrored dashboard state (Data Layer item /faceclaw/state). */ data class PhoneState( - val protocol: Int, val version: String, val phase: String, val status: String, @@ -120,7 +117,6 @@ data class PhoneState( val o = JSONObject(json) val foreground = o.optJSONObject("foreground") PhoneState( - protocol = o.optInt("protocol", 1), version = o.optString("version", ""), phase = o.optString("phase", "disconnected"), status = o.optString("status", ""), diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/RemoteScreen.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/RemoteScreen.kt index 275f656e..3d5acc00 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/RemoteScreen.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/RemoteScreen.kt @@ -37,6 +37,9 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.compose.LocalLifecycleOwner import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.wear.compose.material.Button import androidx.wear.compose.material.ButtonDefaults @@ -207,16 +210,12 @@ fun RemoteScreen( // Motion-sensor gestures only count on a wrist (see OnBodyMonitor). val onBody = remember { OnBodyMonitor(view.context) } - DisposableEffect(onBody) { - onBody.start() - onDispose { onBody.stop() } - } + WhileStartedEffect(onBody, start = onBody::start, stop = onBody::stop) // Fingertip (pinch) taps from the motion sensors, only while the pad is up. val fingerTapsEnabled = prefs.fingerTaps - DisposableEffect(fingerTapsEnabled, link, haptics) { - if (!fingerTapsEnabled) return@DisposableEffect onDispose { } - val detector = FingerTapDetector( + val fingerTapDetector = remember(fingerTapsEnabled, link, haptics) { + if (!fingerTapsEnabled) null else FingerTapDetector( context = view.context, sensitivity = { currentPrefs.tapSensitivity }, quietUntil = { haptics.quietUntil() }, @@ -224,23 +223,20 @@ fun RemoteScreen( onTap = { flashUntil = System.currentTimeMillis() + FLASH_MS; click() }, onDoubleTap = { flashUntil = System.currentTimeMillis() + FLASH_MS; doubleClick() }, ) - detector.start() - onDispose { detector.stop() } } + WhileStartedEffect(fingerTapDetector, start = { fingerTapDetector?.start() }, stop = { fingerTapDetector?.stop() }) // Two quick wrist twists = back, from the gyroscope, only while the pad is up. val wristTwistEnabled = prefs.wristTwist - DisposableEffect(wristTwistEnabled, link, haptics) { - if (!wristTwistEnabled) return@DisposableEffect onDispose { } - val detector = WristTwistDetector( + val wristTwistDetector = remember(wristTwistEnabled, link, haptics) { + if (!wristTwistEnabled) null else WristTwistDetector( context = view.context, sensitivity = { currentPrefs.twistSensitivity }, quietUntil = { haptics.quietUntil() }, onBody = { onBody.onBody }, onDoubleTwist = { flashUntil = System.currentTimeMillis() + FLASH_MS; doubleClick() }, ) - detector.start() - onDispose { detector.stop() } } + WhileStartedEffect(wristTwistDetector, start = { wristTwistDetector?.start() }, stop = { wristTwistDetector?.stop() }) LaunchedEffect(litTick) { if (litTick == 0L) return@LaunchedEffect delay(ARROW_LIT_MS) @@ -357,6 +353,32 @@ fun RemoteScreen( } } +/** + * Run start/stop with the composition AND the activity lifecycle: started on + * ON_START (or immediately if already started), stopped on ON_STOP and on + * leaving the composition. A plain DisposableEffect keeps sensors registered + * while the activity sits stopped-but-cached (palmed screen, back on the watch + * face), which at gyroscope/accelerometer rates is a real battery drain. + */ +@Composable +private fun WhileStartedEffect(key: Any?, start: () -> Unit, stop: () -> Unit) { + val lifecycleOwner = LocalLifecycleOwner.current + DisposableEffect(lifecycleOwner, key) { + val observer = LifecycleEventObserver { _, event -> + when (event) { + Lifecycle.Event.ON_START -> start() + Lifecycle.Event.ON_STOP -> stop() + else -> Unit + } + } + lifecycleOwner.lifecycle.addObserver(observer) + onDispose { + lifecycleOwner.lifecycle.removeObserver(observer) + stop() + } + } +} + @Composable private fun ArrowGlyph(glyph: String, lit: Boolean, modifier: Modifier) { Text( diff --git a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/TouchpadGestures.kt b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/TouchpadGestures.kt index a63bd8cb..1dee96cc 100644 --- a/wear/app/src/main/kotlin/com/faceclaw/wear/ui/TouchpadGestures.kt +++ b/wear/app/src/main/kotlin/com/faceclaw/wear/ui/TouchpadGestures.kt @@ -39,57 +39,71 @@ suspend fun PointerInputScope.detectTouchpadGestures(callbacks: TouchpadCallback var maxPointers = 1 var moved = false var longPressed = false + var longPressEnded = false var displacement = Offset.Zero var releaseTime = downTime - while (true) { - val remaining = if (longPressed || moved || maxPointers > 1) { - HOLD_POLL_MS - } else { - longPressTimeout - (System.currentTimeMillis() - wallClockAt(downTime)) - } - val event = withTimeoutOrNull(remaining.coerceAtLeast(1L)) { awaitPointerEvent() } - if (event == null) { - if (!longPressed && !moved && maxPointers == 1) { - longPressed = true - callbacks.onLongPressStart() + // The finally clause pairs every onLongPressStart with an + // onLongPressEnd, whatever ends the gesture: a parent consuming the + // events (the break below) or the pointerInput coroutine being + // cancelled mid-hold. The phone treats a start without a release as a + // hold still in progress, so dropping the release leaves a stuck + // long-press (and eventually the shell escape menu). + try { + while (true) { + val remaining = if (longPressed || moved || maxPointers > 1) { + HOLD_POLL_MS + } else { + longPressTimeout - (System.currentTimeMillis() - wallClockAt(downTime)) } - continue - } - // Something above us (the system, a parent) took the gesture: - // Compose reports that as already-consumed changes. Drop it rather - // than mistaking the cancellation for a tap. - if (event.changes.any { it.isConsumed }) break - val pressedCount = event.changes.count { it.pressed } - if (pressedCount > maxPointers) maxPointers = pressedCount - val primary = event.changes.firstOrNull { it.id == down.id } ?: event.changes.first() - if (primary.positionChanged()) { - displacement = primary.position - down.position - if (!moved && !longPressed && displacement.getDistance() > slop) moved = true - } - for (change in event.changes) change.consume() - if (pressedCount > 0) continue + val event = withTimeoutOrNull(remaining.coerceAtLeast(1L)) { awaitPointerEvent() } + if (event == null) { + if (!longPressed && !moved && maxPointers == 1) { + longPressed = true + callbacks.onLongPressStart() + } + continue + } + // Something above us (the system, a parent) took the gesture: + // Compose reports that as already-consumed changes. Drop it rather + // than mistaking the cancellation for a tap. + if (event.changes.any { it.isConsumed }) break + val pressedCount = event.changes.count { it.pressed } + if (pressedCount > maxPointers) maxPointers = pressedCount + val primary = event.changes.firstOrNull { it.id == down.id } ?: event.changes.first() + if (primary.positionChanged()) { + displacement = primary.position - down.position + if (!moved && !longPressed && displacement.getDistance() > slop) moved = true + } + for (change in event.changes) change.consume() + if (pressedCount > 0) continue - releaseTime = event.changes.maxOf { it.uptimeMillis } - val duration = releaseTime - downTime - when { - longPressed -> callbacks.onLongPressEnd() - maxPointers >= 2 -> if (moved) callbacks.onTwoFingerSwipe(displacement.x, displacement.y) else callbacks.onTwoFingerTap() - moved -> callbacks.onSwipe(displacement.x, displacement.y, duration) - else -> { - // A plain tap; give a second tap a chance to make it a double. - val second = withTimeoutOrNull(doubleTapTimeout) { awaitFirstDown() } - if (second == null) { - callbacks.onTap(down.position) - } else { - second.consume() - val up = waitForUpOrCancellation() - if (up != null) up.consume() - callbacks.onDoubleTap(down.position) + releaseTime = event.changes.maxOf { it.uptimeMillis } + val duration = releaseTime - downTime + when { + longPressed -> { + longPressEnded = true + callbacks.onLongPressEnd() + } + maxPointers >= 2 -> if (moved) callbacks.onTwoFingerSwipe(displacement.x, displacement.y) else callbacks.onTwoFingerTap() + moved -> callbacks.onSwipe(displacement.x, displacement.y, duration) + else -> { + // A plain tap; give a second tap a chance to make it a double. + val second = withTimeoutOrNull(doubleTapTimeout) { awaitFirstDown() } + if (second == null) { + callbacks.onTap(down.position) + } else { + second.consume() + val up = waitForUpOrCancellation() + if (up != null) up.consume() + callbacks.onDoubleTap(down.position) + } } } + break } - break + } finally { + if (longPressed && !longPressEnded) callbacks.onLongPressEnd() } } }