From a043bce4965142eb91ebad31c7205c0a6a3c76cf Mon Sep 17 00:00:00 2001 From: retrozinndev Date: Sat, 17 May 2025 19:01:48 -0300 Subject: [PATCH] :zap: ags(notifications): better code, performance improvements --- ags/scripts/notifications.ts | 69 ++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/ags/scripts/notifications.ts b/ags/scripts/notifications.ts index 51e2649..d3bc8f7 100644 --- a/ags/scripts/notifications.ts +++ b/ags/scripts/notifications.ts @@ -1,10 +1,6 @@ import { AstalIO, execAsync, Gio, GObject, property, register, signal, timeout } from "astal"; import AstalNotifd from "gi://AstalNotifd"; -export let - NOTIFICATION_TIMEOUT_URGENT: number = 0, - NOTIFICATION_TIMEOUT_NORMAL: number = 4000, - NOTIFICATION_TIMEOUT_LOW: number = 2000; export interface HistoryNotification { id: number; @@ -27,6 +23,10 @@ class Notifications extends GObject.Object { #connections: Array = []; #historyLimit: number = 10; + public static NOTIFICATION_TIMEOUT_CRITICAL: number = 0; + public static NOTIFICATION_TIMEOUT_NORMAL: number = 6000; + public static NOTIFICATION_TIMEOUT_LOW: number = 4000; + @property() public get notifications() { return this.#notifications }; @@ -65,11 +65,9 @@ class Notifications extends GObject.Object { this.#connections.push( AstalNotifd.get_default().connect("notified", (notifd, id) => { const notification = notifd.get_notification(id); - const notifTimeout = notification.urgency === AstalNotifd.Urgency.LOW ? - NOTIFICATION_TIMEOUT_LOW - : (notification.urgency === AstalNotifd.Urgency.CRITICAL ? - NOTIFICATION_TIMEOUT_URGENT - : NOTIFICATION_TIMEOUT_NORMAL); + const notifTimeout = Notifications[`NOTIFICATION_TIMEOUT_${ + this.getUrgencyString(notification.urgency).toUpperCase() + }` as keyof typeof Notifications] as number; if(this.getNotifd().dontDisturb) { this.addHistory(notification, () => notification.dismiss()); @@ -79,7 +77,7 @@ class Notifications extends GObject.Object { this.addNotification(notification, () => { if(notification.urgency !== AstalNotifd.Urgency.CRITICAL || (notification.urgency === AstalNotifd.Urgency.CRITICAL && - NOTIFICATION_TIMEOUT_URGENT > 0)) { + notifTimeout > 0)) { let notifTimer: (AstalIO.Time|undefined) = undefined; let replacedConnectionId: number; @@ -97,10 +95,10 @@ class Notifications extends GObject.Object { notifTimer = timeout(notifTimeout, removeFun); replacedConnectionId = this.connect("notification-replaced", (_, id: number) => { - if(notification.id === id) { - notifTimer?.cancel(); - notifTimer = timeout(notifTimeout, removeFun); - } + if(notification.id !== id) return; + + notifTimer?.cancel(); + notifTimer = timeout(notifTimeout, removeFun); }); } }); @@ -216,13 +214,13 @@ class Notifications extends GObject.Object { onAdded && onAdded(notif); } - public clearHistory(): void { - const hist = this.#history.reverse(); - hist.map((notif) => { + public async clearHistory(): Promise { + this.#history.reverse().map((notif) => { this.#history = this.history.filter((n) => n.id !== notif.id); this.emit("history-removed", notif.id); - this.notify("history"); }); + + this.notify("history"); } public removeHistory(notif: (HistoryNotification|number)): void { @@ -235,16 +233,20 @@ class Notifications extends GObject.Object { } private addNotification(notif: AstalNotifd.Notification, onAdded?: (notif: AstalNotifd.Notification) => void): void { - const newArray = this.#notifications.reverse().filter((item) => item.id !== notif.id); - if(newArray !== this.notifications) { - this.emit("notification-replaced", notif.id); + for(let i = 0; i < this.#notifications.length; i++) { + const item = this.#notifications[i]; + + if(item.id !== notif.id) continue; + + this.#notifications.splice(i, 1); + this.emit("notification-replaced", item.id); + break; } - newArray.push(notif); - this.#notifications = newArray.reverse(); + this.#notifications.unshift(notif); this.notify("notifications"); this.emit("notification-added", notif); - onAdded && onAdded(notif); + onAdded?.(notif); } public removeNotification(notif: (AstalNotifd.Notification|number)): void { @@ -274,25 +276,14 @@ class Notifications extends GObject.Object { this.#notificationsOnHold.add(notif.id); } - public toggleDoNotDisturb(): boolean { - if(AstalNotifd.get_default().dontDisturb) { - AstalNotifd.get_default().dontDisturb = false; - return false; - } + public toggleDoNotDisturb(value?: boolean): boolean { + value = value ?? !AstalNotifd.get_default().dontDisturb; + AstalNotifd.get_default().dontDisturb = value; - AstalNotifd.get_default().dontDisturb = true; - return true; + return value; } public getNotifd(): AstalNotifd.Notifd { return AstalNotifd.get_default(); } - - connect(signal: string, callback: (...args: any[]) => void): number { - return super.connect(signal, callback); - } - - disconnect(id: number) { - super.disconnect(id); - } } export { Notifications };