✨ ags(notifications): hold notification on hover
This commit is contained in:
@@ -23,6 +23,7 @@ class Notifications extends GObject.Object {
|
|||||||
|
|
||||||
#notifications: Array<AstalNotifd.Notification> = [];
|
#notifications: Array<AstalNotifd.Notification> = [];
|
||||||
#history: Array<HistoryNotification> = [];
|
#history: Array<HistoryNotification> = [];
|
||||||
|
#notificationsOnHold: Set<number> = new Set<number>();
|
||||||
#connections: Array<number> = [];
|
#connections: Array<number> = [];
|
||||||
#historyLimit: number = 10;
|
#historyLimit: number = 10;
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ class Notifications extends GObject.Object {
|
|||||||
@signal(Number)
|
@signal(Number)
|
||||||
declare notificationRemoved: (id: number) => void;
|
declare notificationRemoved: (id: number) => void;
|
||||||
|
|
||||||
@signal(Object)
|
@signal(Object) // It's an Object, beacuase HistoryNotification is just an interface
|
||||||
declare historyAdded: (notification: AstalNotifd.Notification) => void;
|
declare historyAdded: (notification: AstalNotifd.Notification) => void;
|
||||||
|
|
||||||
@signal(Number)
|
@signal(Number)
|
||||||
@@ -85,6 +86,8 @@ class Notifications extends GObject.Object {
|
|||||||
|
|
||||||
const removeFun = () => { // Funny name haha lmao remove fun :skull:
|
const removeFun = () => { // Funny name haha lmao remove fun :skull:
|
||||||
notifTimer = undefined;
|
notifTimer = undefined;
|
||||||
|
if(this.#notificationsOnHold.has(notification.id)) return;
|
||||||
|
|
||||||
this.addHistory(notification, () => {
|
this.addHistory(notification, () => {
|
||||||
replacedConnectionId && this.disconnect(replacedConnectionId);
|
replacedConnectionId && this.disconnect(replacedConnectionId);
|
||||||
this.removeNotification(id);
|
this.removeNotification(id);
|
||||||
@@ -181,6 +184,9 @@ class Notifications extends GObject.Object {
|
|||||||
|
|
||||||
public removeNotification(notif: (AstalNotifd.Notification|number)): void {
|
public removeNotification(notif: (AstalNotifd.Notification|number)): void {
|
||||||
const notificationId = (notif instanceof AstalNotifd.Notification) ? notif.id : notif;
|
const notificationId = (notif instanceof AstalNotifd.Notification) ? notif.id : notif;
|
||||||
|
this.#notificationsOnHold.has(notificationId) &&
|
||||||
|
this.#notificationsOnHold.delete(notificationId);
|
||||||
|
|
||||||
this.#notifications = this.#notifications.filter((item: AstalNotifd.Notification) =>
|
this.#notifications = this.#notifications.filter((item: AstalNotifd.Notification) =>
|
||||||
item.id !== notificationId);
|
item.id !== notificationId);
|
||||||
|
|
||||||
@@ -189,6 +195,20 @@ class Notifications extends GObject.Object {
|
|||||||
this.emit("notification-removed", notificationId);
|
this.emit("notification-removed", notificationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getNotificationById(id: number): AstalNotifd.Notification|undefined {
|
||||||
|
return this.#notifications.filter(notif => notif.id === id)?.[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public holdNotification(notif: (AstalNotifd.Notification|number)): void {
|
||||||
|
notif = (typeof notif === "number") ?
|
||||||
|
this.getNotificationById(notif)!
|
||||||
|
: notif;
|
||||||
|
|
||||||
|
if(!notif) return;
|
||||||
|
|
||||||
|
this.#notificationsOnHold.add(notif.id);
|
||||||
|
}
|
||||||
|
|
||||||
public toggleDoNotDisturb(): boolean {
|
public toggleDoNotDisturb(): boolean {
|
||||||
if(AstalNotifd.get_default().dontDisturb) {
|
if(AstalNotifd.get_default().dontDisturb) {
|
||||||
AstalNotifd.get_default().dontDisturb = false;
|
AstalNotifd.get_default().dontDisturb = false;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Astal, Gtk, Widget } from "astal/gtk3";
|
import { Astal, Gtk, Widget } from "astal/gtk3";
|
||||||
import AstalNotifd from "gi://AstalNotifd";
|
import AstalNotifd from "gi://AstalNotifd";
|
||||||
import { Separator } from "./Separator";
|
import { Separator } from "./Separator";
|
||||||
import { HistoryNotification } from "../scripts/notifications";
|
import { HistoryNotification, Notifications } from "../scripts/notifications";
|
||||||
import { GLib } from "astal";
|
import { GLib } from "astal";
|
||||||
|
|
||||||
export function getUrgencyString(notif: AstalNotifd.Notification) {
|
export function getUrgencyString(notif: AstalNotifd.Notification) {
|
||||||
@@ -32,14 +32,16 @@ function getNotificationImage(notif: AstalNotifd.Notification|HistoryNotificatio
|
|||||||
|
|
||||||
export function NotificationWidget(notification: AstalNotifd.Notification|number|HistoryNotification,
|
export function NotificationWidget(notification: AstalNotifd.Notification|number|HistoryNotification,
|
||||||
onClose?: (notif: AstalNotifd.Notification|HistoryNotification) => void,
|
onClose?: (notif: AstalNotifd.Notification|HistoryNotification) => void,
|
||||||
showTime?: boolean /* It's showTime :speaking_head: :boom: :bangbang: */): Gtk.Widget {
|
showTime?: boolean /* It's showTime :speaking_head: :boom: :bangbang: */,
|
||||||
|
holdOnHover?: boolean): Gtk.Widget {
|
||||||
|
|
||||||
notification = (typeof notification === "number") ?
|
notification = (typeof notification === "number") ?
|
||||||
AstalNotifd.get_default().get_notification(notification)
|
AstalNotifd.get_default().get_notification(notification)
|
||||||
: notification;
|
: notification;
|
||||||
|
|
||||||
const body: string = notification.body.split(' ').map(strPart => {
|
const body: string = notification.body.split(' ').map(strPart => {
|
||||||
if(/^\<.*\>.*\<.*\>$/.test(strPart)) return strPart;
|
if(/^\<(.*)\>/.test(strPart) || /<\/(.*)\>$/.test(strPart))
|
||||||
|
return strPart;
|
||||||
|
|
||||||
return strPart.length >= 25 ? `${strPart.substring(0, 22)}...`
|
return strPart.length >= 25 ? `${strPart.substring(0, 22)}...`
|
||||||
: strPart
|
: strPart
|
||||||
@@ -48,12 +50,16 @@ export function NotificationWidget(notification: AstalNotifd.Notification|number
|
|||||||
return new Widget.EventBox({
|
return new Widget.EventBox({
|
||||||
onClick: () => {
|
onClick: () => {
|
||||||
if(notification instanceof AstalNotifd.Notification) {
|
if(notification instanceof AstalNotifd.Notification) {
|
||||||
const viewAction = notification.actions.filter(action => action.label.toLowerCase() === "view")?.[0];
|
const viewAction = notification.actions.filter(action =>
|
||||||
if(viewAction) notification.invoke(viewAction.id);
|
action.label.toLowerCase() === "view")?.[0];
|
||||||
|
|
||||||
|
viewAction && notification.invoke(viewAction.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose && onClose(notification);
|
onClose?.(notification);
|
||||||
},
|
},
|
||||||
|
onHover: () => holdOnHover && Notifications.getDefault().holdNotification(notification.id),
|
||||||
|
onHoverLost: () => onClose?.(notification),
|
||||||
hexpand: true,
|
hexpand: true,
|
||||||
vexpand: false,
|
vexpand: false,
|
||||||
child: new Widget.Box({
|
child: new Widget.Box({
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ export const FloatingNotifications = (mon: number) => new Widget.Window({
|
|||||||
homogeneous: false,
|
homogeneous: false,
|
||||||
visible: bind(Notifications.getDefault(), "notifications").as(notifs => notifs.length > 0),
|
visible: bind(Notifications.getDefault(), "notifications").as(notifs => notifs.length > 0),
|
||||||
children: bind(Notifications.getDefault(), "notifications").as((notifs) =>
|
children: bind(Notifications.getDefault(), "notifications").as((notifs) =>
|
||||||
notifs.map((item) => NotificationWidget(item, () => Notifications.getDefault().removeNotification(item)))),
|
notifs.map((item) => NotificationWidget(item,
|
||||||
|
() => Notifications.getDefault().removeNotification(item),
|
||||||
|
false, true))
|
||||||
|
),
|
||||||
} as Widget.BoxProps)
|
} as Widget.BoxProps)
|
||||||
} as Widget.WindowProps);
|
} as Widget.WindowProps);
|
||||||
|
|||||||
Reference in New Issue
Block a user