Skip to content

Commit

Permalink
ui, themes: allow to apply themes in runtime
Browse files Browse the repository at this point in the history
When selecting a theme the Preferences dialog is stylized. If the user
saves the changes the theme will be applied to all the dialogs.
  • Loading branch information
gustavo-iniguez-goya committed Jan 8, 2023
1 parent 9dfcca2 commit afc3fb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion ui/opensnitch/dialogs/preferences.py
Expand Up @@ -57,6 +57,7 @@ def __init__(self, parent=None, appicon=None):
self.popupsCheck.clicked.connect(self._cb_popups_check_toggled)
self.dbFileButton.clicked.connect(self._cb_file_db_clicked)
self.checkUIRules.toggled.connect(self._cb_check_ui_rules_toggled)
self.comboUITheme.currentIndexChanged.connect(self._cb_combo_themes_changed)
self.cmdTimeoutUp.clicked.connect(lambda: self._cb_cmd_spin_clicked(self.spinUITimeout, self.SUM))
self.cmdTimeoutDown.clicked.connect(lambda: self._cb_cmd_spin_clicked(self.spinUITimeout, self.REST))
self.cmdDBMaxDaysUp.clicked.connect(lambda: self._cb_cmd_spin_clicked(self.spinDBMaxDays, self.SUM))
Expand Down Expand Up @@ -151,6 +152,7 @@ def show_node_prefs(self, addr):
self.tabWidget.setCurrentIndex(self.TAB_NODES)

def _load_themes(self):
self.comboUITheme.blockSignals(True)
theme_idx, self._saved_theme = self._themes.get_saved_theme()

self.labelThemeError.setVisible(False)
Expand All @@ -167,6 +169,7 @@ def _load_themes(self):
self.labelThemeError.setText(QC.translate("preferences", "Themes not available. Install qt-material: pip3 install qt-material"))

self.comboUITheme.setCurrentIndex(theme_idx)
self.comboUITheme.blockSignals(False)

def _load_settings(self):
self._default_action = self._cfg.getInt(self._cfg.DEFAULT_ACTION_KEY)
Expand Down Expand Up @@ -416,7 +419,7 @@ def _save_ui_config(self):
int(Config.NOTIFICATION_TYPE_SYSTEM if self.radioSysNotifs.isChecked() else Config.NOTIFICATION_TYPE_QT))

self._themes.save_theme(self.comboUITheme.currentIndex(), self.comboUITheme.currentText())
if self._themes.available() and self._saved_theme != "" and self._saved_theme != self.comboUITheme.currentText():
if self._themes.available() and self._saved_theme != "" and self.comboUITheme.currentText() == QC.translate("preferences", "System"):
Message.ok(
QC.translate("preferences", "UI theme changed"),
QC.translate("preferences", "Restart the GUI in order to apply the new theme"),
Expand Down Expand Up @@ -567,6 +570,9 @@ def _cb_node_needs_update(self):
def _cb_check_ui_rules_toggled(self, state):
self.comboUIRules.setEnabled(state)

def _cb_combo_themes_changed(self, index):
self._themes.change_theme(self, self.comboUITheme.currentText())

def _cb_db_max_days_toggled(self, state):
self._enable_db_cleaner_options(state, 1)

Expand Down
7 changes: 6 additions & 1 deletion ui/opensnitch/service.py
Expand Up @@ -22,7 +22,7 @@
from opensnitch.config import Config
from opensnitch.version import version
from opensnitch.database import Database
from opensnitch.utils import Utils, CleanerTask
from opensnitch.utils import Utils, CleanerTask, Themes
from opensnitch.utils import Message

class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
Expand Down Expand Up @@ -86,6 +86,7 @@ def __init__(self, app, on_exit):
self._remote_lock = Lock()
self._remote_stats = {}

self._themes = Themes()
self._desktop_notifications = DesktopNotifications()
self._setup_interfaces()
self._setup_icons()
Expand Down Expand Up @@ -349,6 +350,10 @@ def _on_settings_saved(self):
elif self._cfg.getBool(Config.DEFAULT_DB_PURGE_OLDEST) == False and self._cleaner != None:
self._stop_db_cleaner()

theme_idx, theme_name = self._themes.get_saved_theme()
if theme_idx > 0:
self._themes.load_theme(self._app)

def _stop_db_cleaner(self):
if self._cleaner != None:
self._cleaner.stop()
Expand Down
7 changes: 7 additions & 0 deletions ui/opensnitch/utils.py
Expand Up @@ -158,6 +158,13 @@ def load_theme(self, app):
except Exception as e:
print("Themes.load_theme() exception:", e)

def change_theme(self, window, theme_name):
try:
invert = "light" in theme_name
Themes.qtmaterial_apply_stylesheet(window, theme=theme_name, invert_secondary=invert)
except Exception as e:
print("Themes.change_theme() exception:", e, " - ", window, theme_name)

def list_local_themes(self):
themes = []
if not Themes.AVAILABLE:
Expand Down

0 comments on commit afc3fb8

Please sign in to comment.