From bfe8847d72cd0893230f2e654242658214943e61 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Sat, 2 Oct 2021 09:24:32 +0200 Subject: chore: convert various files from JS to TS (#2010) --- src/lib/DBus.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/lib/DBus.ts (limited to 'src/lib/DBus.ts') 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 @@ +import { MessageBus, sessionBus } from 'dbus-next'; +import { isLinux } from '../environment'; + +export default class DBus { + bus: MessageBus | null = null; + + trayIcon: any; + + constructor(trayIcon: any) { + this.trayIcon = trayIcon; + } + + start() { + if (!isLinux || this.bus) return; + + try { + this.bus = sessionBus(); + } catch { + // Error connecting to the bus. + return; + } + + // HACK Hook onto the MessageBus to track StatusNotifierWatchers + // @ts-expect-error Property '_addMatch' does not exist on type 'MessageBus'. + this.bus._addMatch( + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',path='/org/freedesktop/DBus',member='NameOwnerChanged'", + ); + const mangled = JSON.stringify({ + path: '/org/freedesktop/DBus', + interface: 'org.freedesktop.DBus', + member: 'NameOwnerChanged', + }); + // @ts-expect-error Property '_signals' does not exist on type 'MessageBus'. + this.bus._signals.on(mangled, (msg: { body: [any, any, any] }) => { + const [name, oldOwner, newOwner] = msg.body; + if ( + name === 'org.kde.StatusNotifierWatcher' && + oldOwner !== newOwner && + newOwner !== '' + ) { + // Leave ample time for the StatusNotifierWatcher to be initialized + setTimeout(() => { + this.trayIcon.recreateIfVisible(); + }, 400); + } + }); + } + + stop() { + if (!this.bus) return; + + this.bus.disconnect(); + this.bus = null; + } +} -- cgit v1.2.3-70-g09d2