diff options
Diffstat (limited to 'src/lib/DBus.ts')
-rw-r--r-- | src/lib/DBus.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lib/DBus.ts b/src/lib/DBus.ts new file mode 100644 index 000000000..b1febc2d1 --- /dev/null +++ b/src/lib/DBus.ts | |||
@@ -0,0 +1,55 @@ | |||
1 | import { MessageBus, sessionBus } from 'dbus-next'; | ||
2 | import { isLinux } from '../environment'; | ||
3 | |||
4 | export default class DBus { | ||
5 | bus: MessageBus | null = null; | ||
6 | |||
7 | trayIcon: any; | ||
8 | |||
9 | constructor(trayIcon: any) { | ||
10 | this.trayIcon = trayIcon; | ||
11 | } | ||
12 | |||
13 | start() { | ||
14 | if (!isLinux || this.bus) return; | ||
15 | |||
16 | try { | ||
17 | this.bus = sessionBus(); | ||
18 | } catch { | ||
19 | // Error connecting to the bus. | ||
20 | return; | ||
21 | } | ||
22 | |||
23 | // HACK Hook onto the MessageBus to track StatusNotifierWatchers | ||
24 | // @ts-expect-error Property '_addMatch' does not exist on type 'MessageBus'. | ||
25 | this.bus._addMatch( | ||
26 | "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',path='/org/freedesktop/DBus',member='NameOwnerChanged'", | ||
27 | ); | ||
28 | const mangled = JSON.stringify({ | ||
29 | path: '/org/freedesktop/DBus', | ||
30 | interface: 'org.freedesktop.DBus', | ||
31 | member: 'NameOwnerChanged', | ||
32 | }); | ||
33 | // @ts-expect-error Property '_signals' does not exist on type 'MessageBus'. | ||
34 | this.bus._signals.on(mangled, (msg: { body: [any, any, any] }) => { | ||
35 | const [name, oldOwner, newOwner] = msg.body; | ||
36 | if ( | ||
37 | name === 'org.kde.StatusNotifierWatcher' && | ||
38 | oldOwner !== newOwner && | ||
39 | newOwner !== '' | ||
40 | ) { | ||
41 | // Leave ample time for the StatusNotifierWatcher to be initialized | ||
42 | setTimeout(() => { | ||
43 | this.trayIcon.recreateIfVisible(); | ||
44 | }, 400); | ||
45 | } | ||
46 | }); | ||
47 | } | ||
48 | |||
49 | stop() { | ||
50 | if (!this.bus) return; | ||
51 | |||
52 | this.bus.disconnect(); | ||
53 | this.bus = null; | ||
54 | } | ||
55 | } | ||