From 4576a5080c51918907c89629cefe389945179d71 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 17 May 2020 15:17:07 +0200 Subject: Make Tray icons more robust on Linux (#748) --- src/lib/DBus.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/lib/DBus.js (limited to 'src/lib/DBus.js') diff --git a/src/lib/DBus.js b/src/lib/DBus.js new file mode 100644 index 000000000..957912efa --- /dev/null +++ b/src/lib/DBus.js @@ -0,0 +1,49 @@ +import { + sessionBus, +} from 'dbus-next'; +import { + isLinux, +} from '../environment'; + +export default class DBus { + bus = null; + + constructor(trayIcon) { + 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 + 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', + }); + this.bus._signals.on(mangled, (msg) => { + 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