ags(notifications): better code, performance improvements

This commit is contained in:
retrozinndev
2025-05-17 19:01:48 -03:00
parent 62c9589b2c
commit a043bce496
+30 -39
View File
@@ -1,10 +1,6 @@
import { AstalIO, execAsync, Gio, GObject, property, register, signal, timeout } from "astal"; import { AstalIO, execAsync, Gio, GObject, property, register, signal, timeout } from "astal";
import AstalNotifd from "gi://AstalNotifd"; 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 { export interface HistoryNotification {
id: number; id: number;
@@ -27,6 +23,10 @@ class Notifications extends GObject.Object {
#connections: Array<number> = []; #connections: Array<number> = [];
#historyLimit: number = 10; #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() @property()
public get notifications() { return this.#notifications }; public get notifications() { return this.#notifications };
@@ -65,11 +65,9 @@ class Notifications extends GObject.Object {
this.#connections.push( this.#connections.push(
AstalNotifd.get_default().connect("notified", (notifd, id) => { AstalNotifd.get_default().connect("notified", (notifd, id) => {
const notification = notifd.get_notification(id); const notification = notifd.get_notification(id);
const notifTimeout = notification.urgency === AstalNotifd.Urgency.LOW ? const notifTimeout = Notifications[`NOTIFICATION_TIMEOUT_${
NOTIFICATION_TIMEOUT_LOW this.getUrgencyString(notification.urgency).toUpperCase()
: (notification.urgency === AstalNotifd.Urgency.CRITICAL ? }` as keyof typeof Notifications] as number;
NOTIFICATION_TIMEOUT_URGENT
: NOTIFICATION_TIMEOUT_NORMAL);
if(this.getNotifd().dontDisturb) { if(this.getNotifd().dontDisturb) {
this.addHistory(notification, () => notification.dismiss()); this.addHistory(notification, () => notification.dismiss());
@@ -79,7 +77,7 @@ class Notifications extends GObject.Object {
this.addNotification(notification, () => { this.addNotification(notification, () => {
if(notification.urgency !== AstalNotifd.Urgency.CRITICAL || if(notification.urgency !== AstalNotifd.Urgency.CRITICAL ||
(notification.urgency === AstalNotifd.Urgency.CRITICAL && (notification.urgency === AstalNotifd.Urgency.CRITICAL &&
NOTIFICATION_TIMEOUT_URGENT > 0)) { notifTimeout > 0)) {
let notifTimer: (AstalIO.Time|undefined) = undefined; let notifTimer: (AstalIO.Time|undefined) = undefined;
let replacedConnectionId: number; let replacedConnectionId: number;
@@ -97,10 +95,10 @@ class Notifications extends GObject.Object {
notifTimer = timeout(notifTimeout, removeFun); notifTimer = timeout(notifTimeout, removeFun);
replacedConnectionId = this.connect("notification-replaced", (_, id: number) => { replacedConnectionId = this.connect("notification-replaced", (_, id: number) => {
if(notification.id === id) { if(notification.id !== id) return;
notifTimer?.cancel();
notifTimer = timeout(notifTimeout, removeFun); notifTimer?.cancel();
} notifTimer = timeout(notifTimeout, removeFun);
}); });
} }
}); });
@@ -216,13 +214,13 @@ class Notifications extends GObject.Object {
onAdded && onAdded(notif); onAdded && onAdded(notif);
} }
public clearHistory(): void { public async clearHistory(): Promise<void> {
const hist = this.#history.reverse(); this.#history.reverse().map((notif) => {
hist.map((notif) => {
this.#history = this.history.filter((n) => n.id !== notif.id); this.#history = this.history.filter((n) => n.id !== notif.id);
this.emit("history-removed", notif.id); this.emit("history-removed", notif.id);
this.notify("history");
}); });
this.notify("history");
} }
public removeHistory(notif: (HistoryNotification|number)): void { 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 { private addNotification(notif: AstalNotifd.Notification, onAdded?: (notif: AstalNotifd.Notification) => void): void {
const newArray = this.#notifications.reverse().filter((item) => item.id !== notif.id); for(let i = 0; i < this.#notifications.length; i++) {
if(newArray !== this.notifications) { const item = this.#notifications[i];
this.emit("notification-replaced", notif.id);
if(item.id !== notif.id) continue;
this.#notifications.splice(i, 1);
this.emit("notification-replaced", item.id);
break;
} }
newArray.push(notif); this.#notifications.unshift(notif);
this.#notifications = newArray.reverse();
this.notify("notifications"); this.notify("notifications");
this.emit("notification-added", notif); this.emit("notification-added", notif);
onAdded && onAdded(notif); onAdded?.(notif);
} }
public removeNotification(notif: (AstalNotifd.Notification|number)): void { public removeNotification(notif: (AstalNotifd.Notification|number)): void {
@@ -274,25 +276,14 @@ class Notifications extends GObject.Object {
this.#notificationsOnHold.add(notif.id); this.#notificationsOnHold.add(notif.id);
} }
public toggleDoNotDisturb(): boolean { public toggleDoNotDisturb(value?: boolean): boolean {
if(AstalNotifd.get_default().dontDisturb) { value = value ?? !AstalNotifd.get_default().dontDisturb;
AstalNotifd.get_default().dontDisturb = false; AstalNotifd.get_default().dontDisturb = value;
return false;
}
AstalNotifd.get_default().dontDisturb = true; return value;
return true;
} }
public getNotifd(): AstalNotifd.Notifd { return AstalNotifd.get_default(); } 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 }; export { Notifications };