♻️ ags(notifications): move getUrgencyString() to Notifications module
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { AstalIO, 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
|
export let
|
||||||
@@ -126,6 +126,71 @@ class Notifications extends GObject.Object {
|
|||||||
return this.instance;
|
return this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async sendNotification(props: {
|
||||||
|
urgency?: AstalNotifd.Urgency;
|
||||||
|
appName?: string;
|
||||||
|
image?: string;
|
||||||
|
summary: string;
|
||||||
|
body?: string;
|
||||||
|
replaceId?: number;
|
||||||
|
actions?: Array<{
|
||||||
|
id?: (string|number);
|
||||||
|
text: string;
|
||||||
|
onAction?: () => void
|
||||||
|
}>
|
||||||
|
}): Promise<{
|
||||||
|
id?: (string|number);
|
||||||
|
text: string;
|
||||||
|
onAction?: () => void
|
||||||
|
}|null|void> {
|
||||||
|
|
||||||
|
return await execAsync([
|
||||||
|
"notify-send",
|
||||||
|
...(props.urgency ? [
|
||||||
|
"-u", this.getUrgencyString(props.urgency)
|
||||||
|
] : []), ...(props.appName ? [
|
||||||
|
"-a", props.appName
|
||||||
|
] : []), ...(props.image ? [
|
||||||
|
"-i", props.image
|
||||||
|
] : []), ...(props.actions ? props.actions.map((action) =>
|
||||||
|
[ "-A", action.text ]
|
||||||
|
).join("\s") : []), ...(props.replaceId ? [
|
||||||
|
"-r", props.replaceId.toString()
|
||||||
|
] : []), props.summary, props.body ? props.body : ""
|
||||||
|
]).then((stdout) => {
|
||||||
|
stdout = stdout.trim();
|
||||||
|
if(!stdout) {
|
||||||
|
if(props.actions && props.actions.length > 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(props.actions && props.actions.length > 0) {
|
||||||
|
const action = props.actions[Number.parseInt(stdout)];
|
||||||
|
action?.onAction?.();
|
||||||
|
|
||||||
|
return action ?? undefined;
|
||||||
|
}
|
||||||
|
}).catch((err: Gio.IOErrorEnum) => {
|
||||||
|
console.error(`Notifications: Couldn't send notification! Is the daemon running? Stderr:\n${
|
||||||
|
err.message ? `${err.message}\n` : ""}Stack: ${err.stack}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUrgencyString(urgency: AstalNotifd.Notification|AstalNotifd.Urgency) {
|
||||||
|
switch((urgency instanceof AstalNotifd.Notification) ?
|
||||||
|
urgency.urgency : urgency) {
|
||||||
|
|
||||||
|
case AstalNotifd.Urgency.LOW:
|
||||||
|
return "low";
|
||||||
|
case AstalNotifd.Urgency.CRITICAL:
|
||||||
|
return "critical";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "normal";
|
||||||
|
}
|
||||||
|
|
||||||
private addHistory(notif: AstalNotifd.Notification, onAdded?: (notif: AstalNotifd.Notification) => void): void {
|
private addHistory(notif: AstalNotifd.Notification, onAdded?: (notif: AstalNotifd.Notification) => void): void {
|
||||||
if(!notif) return;
|
if(!notif) return;
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,6 @@ import { HistoryNotification, Notifications } from "../scripts/notifications";
|
|||||||
import { GLib } from "astal";
|
import { GLib } from "astal";
|
||||||
import { getAppIcon } from "../scripts/apps";
|
import { getAppIcon } from "../scripts/apps";
|
||||||
|
|
||||||
export function getUrgencyString(notif: AstalNotifd.Notification) {
|
|
||||||
switch(notif.urgency) {
|
|
||||||
case AstalNotifd.Urgency.LOW:
|
|
||||||
return "low";
|
|
||||||
case AstalNotifd.Urgency.CRITICAL:
|
|
||||||
return "critical";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "normal";
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNotificationImage(notif: AstalNotifd.Notification|HistoryNotification): (string|undefined) {
|
function getNotificationImage(notif: AstalNotifd.Notification|HistoryNotification): (string|undefined) {
|
||||||
const img = notif.image ?? notif.appIcon;
|
const img = notif.image ?? notif.appIcon;
|
||||||
@@ -64,7 +54,8 @@ export function NotificationWidget(notification: AstalNotifd.Notification|number
|
|||||||
hexpand: true,
|
hexpand: true,
|
||||||
vexpand: false,
|
vexpand: false,
|
||||||
child: new Widget.Box({
|
child: new Widget.Box({
|
||||||
className: `notification ${ (notification instanceof AstalNotifd.Notification) ? getUrgencyString(notification) : "" }`,
|
className: `notification ${ (notification instanceof AstalNotifd.Notification) ?
|
||||||
|
Notifications.getDefault().getUrgencyString(notification.urgency) : "" }`,
|
||||||
homogeneous: false,
|
homogeneous: false,
|
||||||
expand: true,
|
expand: true,
|
||||||
orientation: Gtk.Orientation.VERTICAL,
|
orientation: Gtk.Orientation.VERTICAL,
|
||||||
|
|||||||
Reference in New Issue
Block a user