forked from Blendman974/faceclaw
Add option to auto-dismiss notifications after a timer
This commit is contained in:
1 parent
9ba5c1988d
commit
448a057d2d
15 files changed
+329
-30
No files matched your search
@@ -40,12 +40,21 @@ 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, private readonly removed?: () => void) {
|
||||
constructor(
|
||||
baseLayer: Layer,
|
||||
actions: LayerActions,
|
||||
private readonly hooks: {
|
||||
/** The modal left the shell stack, however it went. */
|
||||
removed?: () => void;
|
||||
/** Input reached the modal (before its inner stack handles it). */
|
||||
input?: (event: InputEvent) => void;
|
||||
} = {},
|
||||
) {
|
||||
this.stack = new LayerStack(baseLayer, actions, modalInterior());
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed?.();
|
||||
this.hooks.removed?.();
|
||||
}
|
||||
|
||||
paint(_ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
@@ -73,6 +82,7 @@ export class ShellModalLayer implements Layer {
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, _ctx: LayerContext): Promise<void> {
|
||||
this.hooks.input?.(event);
|
||||
await this.stack.handleInput(event);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ export function notificationPopupDelayMs(notification: { foregroundService?: boo
|
||||
/** Runs `run` after `delayMs`; returns a function that cancels it. */
|
||||
export type ScheduleFn = (run: () => void, delayMs: number) => () => void;
|
||||
|
||||
const scheduleTimeout: ScheduleFn = (run, delayMs) => {
|
||||
export const scheduleTimeout: ScheduleFn = (run, delayMs) => {
|
||||
const timer = setTimeout(run, delayMs);
|
||||
return () => clearTimeout(timer);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { scheduleTimeout, type ScheduleFn } from "./notification-popup-delay";
|
||||
|
||||
/**
|
||||
* Closes a new-notification popup that is left untouched for its timeout
|
||||
* ("Display new notifications"). The host restarts the countdown when the
|
||||
* popup opens and on each interaction with it (any input, including moving
|
||||
* its menu selection), and stops it when the popup goes away.
|
||||
*/
|
||||
export class NotificationPopupTimeout {
|
||||
private cancel: (() => void) | null = null;
|
||||
|
||||
/** `timeoutMs` null never expires (the popup stays until dismissed). */
|
||||
constructor(
|
||||
private readonly timeoutMs: number | null,
|
||||
private readonly expired: () => void,
|
||||
private readonly schedule: ScheduleFn = scheduleTimeout,
|
||||
) {}
|
||||
|
||||
/** Start the countdown over. */
|
||||
restart(): void {
|
||||
this.stop();
|
||||
if (this.timeoutMs === null) return;
|
||||
this.cancel = this.schedule(() => {
|
||||
this.cancel = null;
|
||||
this.expired();
|
||||
}, this.timeoutMs);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.cancel?.();
|
||||
this.cancel = null;
|
||||
}
|
||||
|
||||
/** Whether a countdown is running (for tests). */
|
||||
isRunning(): boolean {
|
||||
return this.cancel !== null;
|
||||
}
|
||||
}
|
||||
+26
-5
@@ -45,8 +45,11 @@ import {
|
||||
veniceWebSearchSetting,
|
||||
batteryIndicatorSettingsKey,
|
||||
brightnessSetting,
|
||||
newNotificationDisplaySetting,
|
||||
newNotificationDisplayTimeoutMs,
|
||||
onAnySettingChanged,
|
||||
openAiApiKeySetting,
|
||||
shouldPopUpNewNotification,
|
||||
statusBarPositionSetting,
|
||||
statusBarVisibilitySetting,
|
||||
timeFormatSetting,
|
||||
@@ -65,8 +68,8 @@ import {
|
||||
} from "./chrome-layer";
|
||||
import { ShellModalLayer } from "./modal-layer";
|
||||
import { NotificationModalQueue } from "./notification-modal-queue";
|
||||
import { NotificationPopupTimeout } from "./notification-popup-timeout";
|
||||
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";
|
||||
@@ -483,7 +486,7 @@ class Shell {
|
||||
isAvailable: (notificationKey) => {
|
||||
if (this.appNotifications.has(notificationKey)) return true;
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
return !!notification && shouldShowNotificationOnGlasses(notification.packageName);
|
||||
return !!notification && shouldPopUpNewNotification(notification.packageName);
|
||||
},
|
||||
sleep: () => this.sleep(),
|
||||
});
|
||||
@@ -929,18 +932,36 @@ class Shell {
|
||||
const content = appNotification
|
||||
? null
|
||||
: new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", onClose: close });
|
||||
// A phone notification's popup closes once left untouched for the
|
||||
// "Display new notifications" time, as if closed by hand: the notification
|
||||
// stays on the phone and in the status bar. An app's own notification
|
||||
// (e.g. a Nightscout alert) is nowhere else to be found, so it stays up.
|
||||
const timeout = new NotificationPopupTimeout(
|
||||
appNotification ? null : newNotificationDisplayTimeoutMs(newNotificationDisplaySetting.get()),
|
||||
() => {
|
||||
// Something over the popup (a voice dialog, the system menu) is in
|
||||
// use; count that as interaction rather than closing underneath it.
|
||||
if (this.stack.topMatches((layer) => layer === popup.modal)) close();
|
||||
else timeout.restart();
|
||||
},
|
||||
);
|
||||
const popup: NotificationPopup = {
|
||||
content,
|
||||
modal: new ShellModalLayer(
|
||||
appNotification ? appNotification.createLayer(close) : content!,
|
||||
this.config.actions,
|
||||
() => {
|
||||
this.appNotifications.delete(notificationKey);
|
||||
this.notificationModals.removed(popup, this.screenOn);
|
||||
{
|
||||
removed: () => {
|
||||
timeout.stop();
|
||||
this.appNotifications.delete(notificationKey);
|
||||
this.notificationModals.removed(popup, this.screenOn);
|
||||
},
|
||||
input: () => timeout.restart(),
|
||||
},
|
||||
),
|
||||
};
|
||||
this.stack.push(popup.modal);
|
||||
timeout.restart();
|
||||
return popup;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user