From ca7e5575bf413bf746bc4b5397149e05638db2fc Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 25 Jun 2018 16:45:56 +0200 Subject: [PATCH] option to set a dark theme for Qt qdarkstyle is now a new dependency - note that it is only for qt and qt is not strictly a dependency, but it is pure python and relatively small --- contrib/requirements/requirements.txt | 1 + gui/qt/__init__.py | 16 +++++++++++++++- gui/qt/main_window.py | 12 ++++++++++++ gui/qt/util.py | 4 ++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/contrib/requirements/requirements.txt b/contrib/requirements/requirements.txt index 227ec1cd9..26b1b3d3a 100644 --- a/contrib/requirements/requirements.txt +++ b/contrib/requirements/requirements.txt @@ -7,3 +7,4 @@ protobuf dnspython jsonrpclib-pelix PySocks>=1.6.6 +qdarkstyle<3.0 diff --git a/gui/qt/__init__.py b/gui/qt/__init__.py index 35fabc95c..a2c216cdf 100644 --- a/gui/qt/__init__.py +++ b/gui/qt/__init__.py @@ -117,8 +117,22 @@ class ElectrumGui: 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) - ColorScheme.update_from_widget(QWidget()) + + def set_dark_theme_if_needed(self): + use_dark_theme = self.config.get('qt_gui_color_theme', 'default') == 'dark' + if use_dark_theme: + try: + import qdarkstyle + self.app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) + except BaseException as e: + use_dark_theme = False + print_error('Error setting dark theme: {}'.format(e)) + # Even if we ourselves don't set the dark theme, + # the OS/window manager/etc might set *a dark theme*. + # Hence, try to choose colors accordingly: + ColorScheme.update_from_widget(QWidget(), force_dark=use_dark_theme) def build_tray_menu(self): # Avoid immediate GC of old menu when window closed via its action diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index 6db2b07c8..4a5069715 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -2790,6 +2790,18 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): qr_combo.currentIndexChanged.connect(on_video_device) gui_widgets.append((qr_label, qr_combo)) + colortheme_combo = QComboBox() + colortheme_combo.addItem(_('Light'), 'default') + colortheme_combo.addItem(_('Dark'), 'dark') + index = colortheme_combo.findData(self.config.get('qt_gui_color_theme', 'default')) + colortheme_combo.setCurrentIndex(index) + colortheme_label = QLabel(_('Color theme') + ':') + def on_colortheme(x): + self.config.set_key('qt_gui_color_theme', colortheme_combo.itemData(x), True) + self.need_restart = True + colortheme_combo.currentIndexChanged.connect(on_colortheme) + gui_widgets.append((colortheme_label, colortheme_combo)) + usechange_cb = QCheckBox(_('Use change addresses')) usechange_cb.setChecked(self.wallet.use_change) if not self.config.is_modifiable('use_change'): usechange_cb.setEnabled(False) diff --git a/gui/qt/util.py b/gui/qt/util.py index 497b41086..50ed0a5c3 100644 --- a/gui/qt/util.py +++ b/gui/qt/util.py @@ -706,8 +706,8 @@ class ColorScheme: return brightness < (255*3/2) @staticmethod - def update_from_widget(widget): - if ColorScheme.has_dark_background(widget): + def update_from_widget(widget, force_dark=False): + if force_dark or ColorScheme.has_dark_background(widget): ColorScheme.dark_scheme = True