-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathnotify.ts
187 lines (160 loc) · 5.41 KB
/
notify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import App from '../app.js';
import GLib from 'gi://GLib?version=2.0';
import { type Urgency, type Hints } from '../service/notifications.js';
type ClosedReason = ReturnType<typeof _CLOSED_REASON>
// libnotify is not async, so it halts the js engine
// when the notification daemon is in the same process
// so when the daemon acquires the dbus name
// it will switch this to true, so we know to
// use the builtin daemon instead of libnotify
export const daemon = {
running: false,
};
const _URGENCY = (urgency: Urgency) => {
switch (urgency) {
case 'low': return 0;
case 'critical': return 2;
default: return 1;
}
};
const _CLOSED_REASON = (reason: number) => {
switch (reason) {
case -1: return 'unset';
case 1: return 'timeout';
case 2: return 'dismissed';
case 3: return 'closed';
default: return 'undefined';
}
};
/*
* this module gets loaded on startup, so in order
* to make libnotify an optional dependency we do this
*/
async function libnotify() {
try {
const Notify = (await import('gi://Notify')).default;
if (Notify.is_initted())
return Notify;
Notify.init(null);
return Notify;
} catch (error) {
console.error(Error('Missing dependency: libnotify'));
return null;
}
}
export interface NotificationArgs {
appName?: string
body?: string
iconName?: string
id?: number
summary?: string
urgency?: Urgency
category?: string
actions?: {
[label: string]: () => void,
}
timeout?: number
onClosed?: (reason: ClosedReason) => void
// hints
actionIcons?: boolean;
desktopEntry?: string;
image?: string;
resident?: boolean;
soundFile?: string;
soundName?: string;
suppressSound?: boolean;
transient?: boolean;
x?: number;
y?: number;
}
export async function notify(args: NotificationArgs): Promise<number>
export async function notify(
summary: string, body?: string, iconName?: string): Promise<number>
export async function notify(
argsOrSummary: NotificationArgs | string,
body = '',
iconName = '',
): Promise<number> {
const args = typeof argsOrSummary === 'object'
? argsOrSummary
: {
summary: argsOrSummary,
body,
iconName,
};
if (daemon.running) {
const { default: Daemon } = await import('../service/notifications.js');
const actions = Object.entries(args.actions || {}).map(([label, callback], i) => ({
id: `${i}`, label, callback,
}));
const hints: Hints = {
'action-icons': new GLib.Variant('b', args.actionIcons ?? false),
'category': new GLib.Variant('s', args.category ?? ''),
'desktop-entry': new GLib.Variant('s', args.desktopEntry ?? ''),
'image-path': new GLib.Variant('s', args.image ?? ''),
'resident': new GLib.Variant('b', args.resident ?? false),
'sound-file': new GLib.Variant('s', args.soundFile ?? ''),
'sound-name': new GLib.Variant('s', args.soundName ?? ''),
'suppress-sound': new GLib.Variant('b', args.suppressSound ?? false),
'transient': new GLib.Variant('b', args.transient ?? false),
'urgency': new GLib.Variant('i', args.urgency ?? 1),
};
if (args.x !== undefined)
hints['x'] = new GLib.Variant('i', args.x);
if (args.y !== undefined)
hints['y'] = new GLib.Variant('i', args.y);
const id = Daemon.Notify(
args.appName || App.applicationId!,
args.id || 0,
args.iconName || '',
args.summary || '',
args.body || '',
actions.flatMap(({ id, label }) => [id, label]),
hints,
args.timeout || 0,
);
Daemon.getNotification(id)?.connect('invoked', (_, actionId: string) => {
const action = actions.find(({ id }) => id === actionId);
if (action)
action.callback();
});
return id;
}
const Notify = await libnotify();
if (!Notify) {
console.error(Error('missing dependency: libnotify'));
return -1;
}
const n = new Notify.Notification({
summary: args.summary ?? '',
body: args.body ?? '',
id: args.id ?? 0,
iconName: args.iconName ?? '',
appName: args.appName ?? Notify.get_app_name(),
});
n.set_urgency(_URGENCY(args.urgency ?? 'normal'));
n.set_timeout(args.timeout ?? 0);
const hint = (key: string, type: 'b' | 's' | 'i', value?: boolean | string | number) => {
if (value)
n.set_hint(key, new GLib.Variant(type, value));
};
hint('action-icons', 'b', args.actionIcons);
hint('desktop-entry', 's', args.desktopEntry);
hint('image-path', 's', args.image);
hint('resident', 'b', args.resident);
hint('sound-file', 's', args.soundFile);
hint('sound-name', 's', args.soundName);
hint('suppress-sound', 'b', args.suppressSound);
hint('transient', 'b', args.transient);
hint('x', 'i', args.x);
hint('y', 'i', args.y);
Object.keys(args.actions || {}).forEach((action, i) => {
n.add_action(`${i}`, action, args.actions![action]);
});
n.connect('closed', () => {
if (args.onClosed)
args.onClosed(_CLOSED_REASON(n.get_closed_reason()));
});
n.show();
return n.id;
}