From 14c5ceeacf8011a67cfa34638e0420bd0e27f09e Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 3 Apr 2021 05:42:32 +0200 Subject: [PATCH] qt init: (trivial) refactor system tray --- electrum/gui/qt/__init__.py | 18 +++++++++++++----- electrum/gui/qt/main_window.py | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py index 57815aaff..aeda87966 100644 --- a/electrum/gui/qt/__init__.py +++ b/electrum/gui/qt/__init__.py @@ -122,16 +122,19 @@ class ElectrumGui(Logger): self.network_updated_signal_obj = QNetworkUpdatedSignalObject() self._num_wizards_in_progress = 0 self._num_wizards_lock = threading.Lock() - # init tray self.dark_icon = self.config.get("dark_icon", False) + self.tray = None + self._init_tray() + self.app.new_window_signal.connect(self.start_new_window) + self.set_dark_theme_if_needed() + run_hook('init_qt', self) + + def _init_tray(self): self.tray = QSystemTrayIcon(self.tray_icon(), None) self.tray.setToolTip('Electrum') self.tray.activated.connect(self.tray_activated) self.build_tray_menu() self.tray.show() - self.app.new_window_signal.connect(self.start_new_window) - self.set_dark_theme_if_needed() - run_hook('init_qt', self) def set_dark_theme_if_needed(self): use_dark_theme = self.config.get('qt_gui_color_theme', 'default') == 'dark' @@ -150,6 +153,8 @@ class ElectrumGui(Logger): ColorScheme.update_from_widget(QWidget(), force_dark=use_dark_theme) def build_tray_menu(self): + if not self.tray: + return # Avoid immediate GC of old menu when window closed via its action if self.tray.contextMenu() is None: m = QMenu() @@ -179,6 +184,8 @@ class ElectrumGui(Logger): return read_QIcon('electrum_light_icon.png') def toggle_tray_icon(self): + if not self.tray: + return self.dark_icon = not self.dark_icon self.config.set_key("dark_icon", self.dark_icon, True) self.tray.setIcon(self.tray_icon()) @@ -383,7 +390,8 @@ class ElectrumGui(Logger): # clipboard persistence. see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html event = QtCore.QEvent(QtCore.QEvent.Clipboard) self.app.sendEvent(self.app.clipboard(), event) - self.tray.hide() + if self.tray: + self.tray.hide() self.app.aboutToQuit.connect(clean_up) # main loop diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index acae82c38..01575043c 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -992,7 +992,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): text = _("Not connected") icon = read_QIcon("status_disconnected.png") - self.tray.setToolTip("%s (%s)" % (text, self.wallet.basename())) + if self.tray: + self.tray.setToolTip("%s (%s)" % (text, self.wallet.basename())) self.balance_label.setText(text) if self.status_button: self.status_button.setIcon(icon)