[blam/gtk3] React to changes in the user's theme selection



commit a08f7305f633ea773970580127e5161a788efc55
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Sun Oct 13 13:17:54 2013 +0200

    React to changes in the user's theme selection

 src/Dialogs.cs      |   23 +++++++++++------------
 src/ThemeManager.cs |   22 +++++++++++++++-------
 src/dialogs.ui      |    2 +-
 3 files changed, 27 insertions(+), 20 deletions(-)
---
diff --git a/src/Dialogs.cs b/src/Dialogs.cs
index 2e5960b..7fe22a1 100644
--- a/src/Dialogs.cs
+++ b/src/Dialogs.cs
@@ -247,7 +247,7 @@ namespace Blam
                Spinnable autoRefreshInterval;
                Togglable ignoreCertErrors;
                Togglable refreshOnStartup;
-               ComboBox theme;
+               ComboBoxText theme;
                Togglable markReadDelay;
                Spinnable markReadDelayTime;
 
@@ -263,35 +263,32 @@ namespace Blam
                        refreshOnStartup = new Togglable(bld, "refresh-on-startup", "refresh-on-startup");
                        markReadDelay = new Togglable(bld, "mark-read-delay", "mark-read-delay");
                        markReadDelayTime = new Spinnable(bld,"mark-read-delay-time", "mark-read-delay-time");
-                       theme = bld.GetObject<ComboBox>("theme");
+                       theme = bld.GetObject<ComboBoxText>("theme");
 
                        this.themeManager = themeManager;
                }
 
                void fillThemes()
                {
-                       theme.Clear();
-
-                       CellRendererText cell = new CellRendererText();
-                       theme.PackStart(cell, false);
-                       theme.AddAttribute(cell, "text", 0);
-                       ListStore store = new ListStore(typeof(string));
-                       theme.Model = store;
+                       ListStore store = (ListStore)theme.Model;
+                       store.Clear();
 
                        var current = themeManager.Current;
                        var list = themeManager.Themes;
 
                        foreach (var t in list) {
-                               var iter = store.AppendValues(t.Name);
-                               if (t == current)
-                                       theme.SetActiveIter(iter);
+                               theme.Append(t.Name, t.Name);
                        }
+
+                       theme.ActiveId = current.Name;
                }
 
                public new void Run()
                {
                        fillThemes();
 
+                       var themeSelection = Observable.FromEventPattern(theme, "Changed");
+
                        var toggles = Observable.Merge(
                                reverseEntries.Toggle,
                                autoRefresh.Toggle,
@@ -305,11 +302,13 @@ namespace Blam
 
                        var toggleSub = toggles.Subscribe(kv => Conf.Preferences.SetBoolean(kv.Key, 
kv.Value));
                        var numSub = nums.Subscribe(kv => Conf.Preferences.SetInt(kv.Key, kv.Value));
+                       var themeSub = themeSelection.Subscribe(_ => Conf.Preferences.SetString("theme", 
theme.ActiveText));
 
                        base.Run();
 
                        toggleSub.Dispose();
                        numSub.Dispose();
+                       themeSub.Dispose();
                }
        }
 
diff --git a/src/ThemeManager.cs b/src/ThemeManager.cs
index c03b72a..686c805 100644
--- a/src/ThemeManager.cs
+++ b/src/ThemeManager.cs
@@ -5,7 +5,7 @@
 // (C) 2005 Imendio AB
 //
 
-//using GConf;
+using GLib;
 using System;
 using System.Linq;
 using System.Collections;
@@ -13,6 +13,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
+using System.Reactive.Linq;
 
 namespace Blam
 {
@@ -44,17 +45,24 @@ namespace Blam
 
                public ThemeManager()
                {
+                       var themeChange = Observable.FromEventPattern<ChangedArgs>(Conf.Preferences, 
"Changed")
+                               .Where(x => x.EventArgs.Key == "theme")
+                               .Select(x => x.EventArgs.Key)
+                               .StartWith(Conf.Preferences.GetString("theme"));
+
                        themes = new List<ITheme>();
                        loadThemes(Defines.PERSONAL_THEME_DIR);
                        loadThemes(Defines.THEME_DIR);
 
-                       var wanted = Conf.Preferences.GetString("theme");
-                       var theme = themes.Find(t => t.Name.Equals(wanted));
-                       // if the user's theme isn't found, take the first of the list and fallback
-                       // to an empty theme if the list is empty
-                       theme = theme ?? themes.FirstOrDefault() ?? new EmptyTheme();
+                       themeChange.Subscribe(name => {
+                               var wanted = Conf.Preferences.GetString("theme");
+                               var theme = themes.Find(t => t.Name.Equals(wanted));
+                               // if the user's theme isn't found, take the first of the list and fallback
+                               // to an empty theme if the list is empty
+                               theme = theme ?? themes.FirstOrDefault() ?? new EmptyTheme();
+                               Current = theme;
+                       });
 
-                       Current = theme;
                }
 
                /// <summary>
diff --git a/src/dialogs.ui b/src/dialogs.ui
index dbd1059..2d1f47b 100644
--- a/src/dialogs.ui
+++ b/src/dialogs.ui
@@ -845,7 +845,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkComboBox" id="theme">
+                      <object class="GtkComboBoxText" id="theme">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="border_width">1</property>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]