From 819e64fdfe8eee716de4db87b75cf299cf2f0db6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Br=C3=B6tzmann?= <mailtrash@posteo.de>
Date: Sun, 17 Nov 2019 14:55:53 +0100
Subject: [PATCH] Themes: Switch active theme when adding or removing theme

When adding a new theme, it is automatically applied. This enables users
to view their changes directly without switching the active theme in
Preferences first. Deleting the currently active theme will fallback to default.
---
 gajim/gtk/themes.py | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/gajim/gtk/themes.py b/gajim/gtk/themes.py
index 660a8213c0..bcbc3cc0da 100644
--- a/gajim/gtk/themes.py
+++ b/gajim/gtk/themes.py
@@ -284,6 +284,21 @@ def _on_add_new_theme(self, *args):
         self._remove_theme_button.set_sensitive(True)
         iter_ = self._theme_store.append([name])
         self._select_theme_row(iter_)
+        self._apply_theme(name)
+
+    def _apply_theme(self, theme):
+        app.config.set('roster_theme', theme)
+        app.css_config.change_theme(theme)
+        app.nec.push_incoming_event(NetworkEvent('theme-update'))
+
+        # Begin repainting themed widgets throughout
+        app.interface.roster.repaint_themed_widgets()
+        app.interface.roster.change_roster_style(None)
+
+        # Update Preferences theme combobox
+        window = get_app_window('Preferences')
+        if window is not None:
+            window.update_theme_list()
 
     @staticmethod
     def _create_theme_name():
@@ -298,15 +313,11 @@ def _on_remove_theme(self, *args):
             return
 
         theme = store[iter_][Column.THEME]
-        if theme == app.config.get('roster_theme'):
-            ErrorDialog(
-                _('Active Theme'),
-                _('You tried to delete the currently active theme. '
-                  'Please switch to a different theme first.'),
-                transient_for=self)
-            return
 
         def _remove_theme():
+            if theme == app.config.get('roster_theme'):
+                self._apply_theme('default')
+
             app.css_config.remove_theme(theme)
             store.remove(iter_)
 
@@ -316,10 +327,15 @@ def _remove_theme():
                 self._add_option_button.set_sensitive(False)
                 self._clear_options()
 
+        text = _('Do you want to delete this theme?')
+        if theme == app.config.get('roster_theme'):
+            text = _('This is the theme you are currently using.\n'
+                     'Do you want to delete this theme?')
+
         NewConfirmationDialog(
             _('Delete'),
             _('Delete Theme'),
-            _('Do you want to permanently delete this theme?'),
+            text,
             [DialogButton.make('Cancel'),
              DialogButton.make('Delete',
                                callback=_remove_theme)],
-- 
GitLab