ags(bar, notifications, control-center): add status icons to bar, notification history fixed, notification history below control-center

This commit is contained in:
retrozinndev
2025-03-23 10:17:22 -03:00
parent 30d1fded84
commit 3db477598f
29 changed files with 746 additions and 363 deletions
+79 -20
View File
@@ -6,13 +6,24 @@ export const
NOTIFICATION_TIMEOUT_NORMAL: number = 4000,
NOTIFICATION_TIMEOUT_LOW: number = 2000;
export interface HistoryNotification {
id: number;
appName: string;
body: string;
summary: string;
urgency: AstalNotifd.Urgency;
time: number;
image?: string;
}
@register({ GTypeName: "Notifications" })
class Notifications extends GObject.Object {
private static instance: (Notifications|null) = null;
#notifications: Array<AstalNotifd.Notification> = [];
#history: Array<AstalNotifd.Notification> = [];
#history: Array<HistoryNotification> = [];
#connections: Array<number>;
#historyLimit: number = 10;
@property()
@@ -21,6 +32,14 @@ class Notifications extends GObject.Object {
@property()
public get history() { return this.#history };
@property()
public get historyLimit() { return this.#historyLimit };
public set historyLimit(newValue: number) {
this.#historyLimit = newValue;
this.notify("historyLimit");
}
@signal(AstalNotifd.Notification)
declare notificationAdded: (notification: AstalNotifd.Notification) => void;
@@ -28,7 +47,7 @@ class Notifications extends GObject.Object {
@signal(Number)
declare notificationRemoved: (id: number) => void;
@signal(AstalNotifd.Notification)
@signal(Object)
declare historyAdded: (notification: AstalNotifd.Notification) => void;
@signal(Number)
@@ -42,7 +61,7 @@ class Notifications extends GObject.Object {
super();
this.#connections = [
AstalNotifd.get_default().connect("notified", (notifd, id, _replaced) => {
AstalNotifd.get_default().connect("notified", (notifd, id) => {
const notification = notifd.get_notification(id);
const notifTimeout = notification.urgency === AstalNotifd.Urgency.LOW ?
NOTIFICATION_TIMEOUT_LOW
@@ -50,29 +69,39 @@ class Notifications extends GObject.Object {
NOTIFICATION_TIMEOUT_URGENT
: NOTIFICATION_TIMEOUT_NORMAL);
if(this.getNotifd().dontDisturb) {
this.addHistory(notification, () => notification.dismiss());
return;
}
this.addNotification(notification, () => {
if(notification.urgency !== AstalNotifd.Urgency.CRITICAL ||
(notification.urgency === AstalNotifd.Urgency.CRITICAL &&
NOTIFICATION_TIMEOUT_URGENT > 0)) {
let notifTimer: AstalIO.Time;
let notifTimer: (AstalIO.Time|undefined) = undefined;
let replacedConnectionId: number;
const removeFun = () => { // Funny name haha lmao remove fun :skull:
this.removeNotification(id);
replacedConnectionId && this.disconnect(replacedConnectionId);
notifTimer = undefined;
this.addHistory(notification, () => {
replacedConnectionId && this.disconnect(replacedConnectionId);
this.removeNotification(id);
});
}
notifTimer = timeout(notifTimeout, removeFun);
replacedConnectionId = this.connect("notification-replaced", (_, id: number) => {
if(notification.id === id) {
notifTimer.cancel();
notifTimer?.cancel();
notifTimer = timeout(notifTimeout, removeFun);
}
});
notifTimer = timeout(notifTimeout, removeFun);
}
});
}),
AstalNotifd.get_default().connect("resolved", (notifd, id, _reason) => {
this.removeNotification(id);
this.addHistory(notifd.get_notification(id));
@@ -94,17 +123,41 @@ class Notifications extends GObject.Object {
}
private addHistory(notif: AstalNotifd.Notification, onAdded?: (notif: AstalNotifd.Notification) => void): void {
const newArray = this.#history.reverse().filter((item) => item.id !== notif.id);
newArray.push(notif);
if(!notif) return;
this.#history.length === this.#historyLimit &&
this.removeHistory(this.#history[this.#history.length - 1]);
const newArray = this.#history.length > 0 ? this.#history.reverse().filter((item) => item.id !== notif.id) : [];
newArray.push({
id: notif.id,
appName: notif.appName,
body: notif.body,
summary: notif.summary,
urgency: notif.urgency,
time: notif.time,
image: notif.image ? notif.image : undefined
} as HistoryNotification);
this.#history = newArray.reverse();
this.notify("history");
this.emit("history-added", notif);
this.emit("history-added", this.#history[0]);
onAdded && onAdded(notif);
}
public removeHistory(notif: (AstalNotifd.Notification|number)): void {
const notifId = (notif instanceof AstalNotifd.Notification) ? notif.id : notif;
this.#history = this.#history.filter((item: AstalNotifd.Notification) =>
public clearHistory(): void {
for(let i = 0; i < this.history.length; i++) {
const notif = this.history[this.history.length-1];
if(this.#history.pop()) {
this.emit("history-removed", notif.id);
this.notify("history");
}
}
}
public removeHistory(notif: (HistoryNotification|number)): void {
const notifId = (typeof notif === "number") ? notif : notif.id;
this.#history = this.#history.filter((item: HistoryNotification) =>
item.id !== notifId);
this.notify("history");
@@ -125,21 +178,27 @@ class Notifications extends GObject.Object {
}
public removeNotification(notif: (AstalNotifd.Notification|number)): void {
const notification = (notif instanceof AstalNotifd.Notification) ? notif : AstalNotifd.get_default().get_notification(notif);
const notificationId = (notif instanceof AstalNotifd.Notification) ? notif.id : notif;
this.#notifications = this.#notifications.filter((item: AstalNotifd.Notification) =>
item.id !== notification.id);
item.id !== notificationId);
notification.dismiss();
AstalNotifd.get_default().get_notification(notificationId)?.dismiss();
this.notify("notifications");
this.emit("notification-removed", notification.id);
this.emit("notification-removed", notificationId);
}
public toggleDoNotDisturb(): boolean {
if(AstalNotifd.get_default().dontDisturb) {
AstalNotifd.get_default().dontDisturb = false;
return false;
}
AstalNotifd.get_default().dontDisturb = true;
return true;
}
public getNotifd(): AstalNotifd.Notifd { return AstalNotifd.get_default(); }
connect(signal: string, callback: (...args: any[]) => void): number {
return super.connect(signal, callback);
}