forked from Blendman974/faceclaw
Fix several bugs that could lead to reconnect-looping
Fix misinterpretation of EvenHub event type 5. Notification modals stacked, in a way that's O(n) in device resource memory; replace with a queue.
This commit is contained in:
1 parent
e5303e7b3e
commit
38da8e6168
15 files changed
+396
-52
No files matched your search
@@ -198,6 +198,19 @@ export class LayerStack {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove one stacked layer wherever it sits, leaving the layers above it in
|
||||
* place (e.g. a notification modal that closes under a menu opened over it).
|
||||
* Returns false if the layer isn't stacked (the base layer can never be removed).
|
||||
*/
|
||||
remove(layer: Layer): boolean {
|
||||
const index = this.layers.indexOf(layer);
|
||||
if (index <= 0) return false;
|
||||
this.layers.splice(index, 1);
|
||||
notifyRemoved(layer);
|
||||
return true;
|
||||
}
|
||||
|
||||
isAtBase(): boolean {
|
||||
return this.layers.length === 1;
|
||||
}
|
||||
|
||||
@@ -40,10 +40,14 @@ export function modalRect(): { x: number; y: number; width: number; height: numb
|
||||
export class ShellModalLayer implements Layer {
|
||||
private readonly stack: LayerStack;
|
||||
|
||||
constructor(baseLayer: Layer, actions: LayerActions) {
|
||||
constructor(baseLayer: Layer, actions: LayerActions, private readonly removed?: () => void) {
|
||||
this.stack = new LayerStack(baseLayer, actions, modalInterior());
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed?.();
|
||||
}
|
||||
|
||||
paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const image = paintBelow();
|
||||
// Track a display-mode switch while the modal is up, so the inner stack
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* New-notification modals, one at a time. Each modal is a near-64 KiB
|
||||
* on-glasses surface, and stacking one per posted notification overflowed the
|
||||
* glasses' resource cache (#42). Notifications that arrive while a modal is
|
||||
* open wait here and open in turn as each one closes.
|
||||
*
|
||||
* `M` is the host's modal handle; the host creates and stacks modals, and
|
||||
* reports back how each one left the stack.
|
||||
*/
|
||||
export interface NotificationModalHost<M> {
|
||||
/** Create and stack a modal showing this notification. */
|
||||
open(notificationKey: string): M;
|
||||
/** Whether a queued notification is still worth opening (not dismissed, popups still on). */
|
||||
isAvailable(notificationKey: string): boolean;
|
||||
/** Put the screen back to sleep. */
|
||||
sleep(): void;
|
||||
}
|
||||
|
||||
interface Entry {
|
||||
readonly notificationKey: string;
|
||||
/** Whether this notification woke the screen, so closing the last modal sleeps again. */
|
||||
readonly wokeScreen: boolean;
|
||||
}
|
||||
|
||||
export class NotificationModalQueue<M> {
|
||||
private current: (Entry & { readonly modal: M }) | null = null;
|
||||
private readonly queued: Entry[] = [];
|
||||
|
||||
constructor(private readonly host: NotificationModalHost<M>) {}
|
||||
|
||||
/**
|
||||
* A notification was posted. Opens it now, or queues it behind the open
|
||||
* modal. A repost of the open notification needs nothing: its modal repaints
|
||||
* from the live notification.
|
||||
*/
|
||||
post(notificationKey: string, wokeScreen: boolean): void {
|
||||
if (this.current) {
|
||||
if (this.current.notificationKey !== notificationKey
|
||||
&& !this.queued.some((entry) => entry.notificationKey === notificationKey)) {
|
||||
this.queued.push({ notificationKey, wokeScreen });
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.current = { modal: this.host.open(notificationKey), notificationKey, wokeScreen };
|
||||
}
|
||||
|
||||
/**
|
||||
* The user closed `modal`. Opens the next queued notification; once none
|
||||
* are left, goes back to sleep if any of them woke the screen. Call before
|
||||
* unstacking the modal, so the removal it reports is already accounted for.
|
||||
*/
|
||||
closed(modal: M): void {
|
||||
const current = this.current;
|
||||
if (current?.modal !== modal) return;
|
||||
this.current = null;
|
||||
if (!this.openNext(current.wokeScreen) && current.wokeScreen) {
|
||||
this.host.sleep();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `modal` left the stack some other way. Going to sleep clears popups (the
|
||||
* notifications stay in the Notifications app), so a screen that is off
|
||||
* drops the queue; otherwise the next notification opens.
|
||||
*/
|
||||
removed(modal: M, screenOn: boolean): void {
|
||||
const current = this.current;
|
||||
if (current?.modal !== modal) return;
|
||||
this.current = null;
|
||||
if (!screenOn) {
|
||||
this.queued.length = 0;
|
||||
return;
|
||||
}
|
||||
this.openNext(current.wokeScreen);
|
||||
}
|
||||
|
||||
/** Notifications waiting behind the open modal (for diagnostics and tests). */
|
||||
queuedKeys(): string[] {
|
||||
return this.queued.map((entry) => entry.notificationKey);
|
||||
}
|
||||
|
||||
private openNext(wokeScreen: boolean): boolean {
|
||||
while (this.queued.length > 0) {
|
||||
const next = this.queued.shift()!;
|
||||
if (!this.host.isAvailable(next.notificationKey)) continue;
|
||||
this.post(next.notificationKey, wokeScreen || next.wokeScreen);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+34
-15
@@ -50,6 +50,9 @@ import {
|
||||
import { onAmbientCardsChanged } from "./ambient-cards";
|
||||
import { ShellChromeLayer, sidebarContentSpan, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer";
|
||||
import { ShellModalLayer } from "./modal-layer";
|
||||
import { NotificationModalQueue } from "./notification-modal-queue";
|
||||
import { ALL_NOTIFICATIONS, readActiveNotifications } from "../../native/notification-icons";
|
||||
import { shouldShowNotificationOnGlasses } from "../../native/notification-sources";
|
||||
import { ToolDebugMenuLayer } from "./tool-debug-layer";
|
||||
import type { InProcessWindow } from "./in-process-window";
|
||||
import { BrightnessPickerLayer } from "./brightness-picker-layer";
|
||||
@@ -399,6 +402,14 @@ class Shell {
|
||||
private readonly trayIcons = new Map<string, GrayImage>();
|
||||
private activeVoiceLayer: VoiceInputLayer | null = null;
|
||||
private activeKeyboardLayer: KeyboardInputLayer | null = null;
|
||||
private readonly notificationModals = new NotificationModalQueue<ShellModalLayer>({
|
||||
open: (notificationKey) => this.pushNotificationModal(notificationKey),
|
||||
isAvailable: (notificationKey) => {
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
return !!notification && shouldShowNotificationOnGlasses(notification.packageName);
|
||||
},
|
||||
sleep: () => this.sleep(),
|
||||
});
|
||||
private conversations: AssistantConversations | null = null;
|
||||
private get assistantSession(): AssistantSession | null {
|
||||
return this.conversations?.current().session ?? null;
|
||||
@@ -762,28 +773,36 @@ class Shell {
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a new notification in a shell modal over the app viewport. If the
|
||||
* notification woke the screen, closing the modal goes back to sleep
|
||||
* Show a new notification in a shell modal over the app viewport, or queue
|
||||
* it behind the one already open (see NotificationModalQueue). If
|
||||
* notifications woke the screen, closing the last modal goes back to sleep
|
||||
* (matching the old sleep-popup behavior).
|
||||
*/
|
||||
openNotificationModal(notificationKey: string, wokeScreen: boolean): void {
|
||||
if (!this.screenOn) return;
|
||||
const modal: ShellModalLayer = new ShellModalLayer(
|
||||
new SingleNotificationLayer(notificationKey, {
|
||||
origin: "new-notification-modal",
|
||||
onClose: () => this.closeNotificationModal(modal, wokeScreen),
|
||||
}),
|
||||
this.config.actions,
|
||||
);
|
||||
this.stack.push(modal);
|
||||
this.notificationModals.post(notificationKey, wokeScreen);
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
private closeNotificationModal(modal: ShellModalLayer, wokeScreen: boolean): void {
|
||||
this.stack.popIfTop((layer) => layer === modal);
|
||||
if (wokeScreen) {
|
||||
this.sleep();
|
||||
}
|
||||
private pushNotificationModal(notificationKey: string): ShellModalLayer {
|
||||
const modal: ShellModalLayer = new ShellModalLayer(
|
||||
new SingleNotificationLayer(notificationKey, {
|
||||
origin: "new-notification-modal",
|
||||
onClose: () => this.closeNotificationModal(modal),
|
||||
}),
|
||||
this.config.actions,
|
||||
() => this.notificationModals.removed(modal, this.screenOn),
|
||||
);
|
||||
this.stack.push(modal);
|
||||
return modal;
|
||||
}
|
||||
|
||||
private closeNotificationModal(modal: ShellModalLayer): void {
|
||||
// The queue first: it opens the next notification or puts the screen back
|
||||
// to sleep, and then ignores the removal below. remove() rather than a
|
||||
// pop, so a menu opened over the modal doesn't leave it stuck.
|
||||
this.notificationModals.closed(modal);
|
||||
this.stack.remove(modal);
|
||||
this.config.requestShellRender();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user