[blam/gtk-builder] Bring back a basic version of themes



commit 3ed4eedcf418687e858e5506c8ead985f1a5b855
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Sat Oct 12 15:27:41 2013 +0200

    Bring back a basic version of themes

 src/ItemView.cs     |   35 +++++++++++++++++++++---
 src/Theme.cs        |   30 +++++++++++++++++++-
 src/ThemeManager.cs |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 6 deletions(-)
---
diff --git a/src/ItemView.cs b/src/ItemView.cs
index f16182b..ff14589 100644
--- a/src/ItemView.cs
+++ b/src/ItemView.cs
@@ -39,15 +39,42 @@ namespace Blam
                        }
                }
 
-               public ItemView() : base()
+               public ThemeManager ThemeManager { get; private set; }
+
+               public ItemView()
+                       : this(new ThemeManager())
+               {
+               }
+
+               /// <summary>
+               /// Create an ItemView with a particular theme manager.
+               /// </summary>
+               /// <param name="themeManager">Theme manager to use.</param>
+               public ItemView(ThemeManager themeManager)
                {
+                       ThemeManager = themeManager;
                }
 
                void UpdateView()
                {
-                       string text = current.Summary != null ? current.Summary.Text
-                               : current.Content != null ? (current.Content as TextSyndicationContent).Text 
: null;
-                       LoadString(text, null, null, Defines.APP_DATADIR);
+                       var text = current.Summary != null ? current.Summary.Text : null;
+                       text = text ?? (current.Content != null ? (current.Content as 
TextSyndicationContent).Text : null);
+
+                       var title   = current.Title.Text;
+                       var theme   = ThemeManager.Current;
+                       var baseDir = theme.Path;
+                       var link    = Catalog.GetString("Show in browser");
+                       var url     = current.Links.Count == 0 ? current.Id : current.Links[0].Uri.ToString();
+
+                       var replaces = new Dictionary<string, string> {
+                               {"text", text},
+                               {"title", title},
+                               {"link", link},
+                               {"url", url},
+                               {"localbase", baseDir},
+                       };
+
+                       LoadString(theme.Render(replaces), null, null, url);
                }
        }
 }
diff --git a/src/Theme.cs b/src/Theme.cs
index 6ad3942..12b456a 100644
--- a/src/Theme.cs
+++ b/src/Theme.cs
@@ -8,8 +8,34 @@
 using System.IO;
 using System.Collections.Generic;
 
+namespace Blam
+{
+       using Theme = Imendio.Blam.Theme;
+
+       public interface ITheme
+       {
+               string Render(IDictionary<string,string> replaces);
+               string Path { get; }
+       }
+
+
+       public class EmptyTheme : ITheme
+       {
+               public string Path {
+                       get {
+                               return string.Empty;
+                       }
+               }
+
+               public string Render(IDictionary<string,string> replaces)
+               {
+                       return replaces["text"];
+               }
+       }
+}
+
 namespace Imendio.Blam {
-       public class Theme {
+       public class Theme : global::Blam.ITheme {
                private string mName = "";
                private string mPath = "";
 
@@ -34,7 +60,7 @@ namespace Imendio.Blam {
                        }
                }
 
-               public string Render(Dictionary<string,string> replaces)
+               public string Render(IDictionary<string,string> replaces)
                {
                        string str = mThemeHtml;
 
diff --git a/src/ThemeManager.cs b/src/ThemeManager.cs
index d605198..4778cc5 100644
--- a/src/ThemeManager.cs
+++ b/src/ThemeManager.cs
@@ -7,8 +7,83 @@
 
 using GConf;
 using System;
+using System.Linq;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+
+namespace Blam
+{
+       using Theme = Imendio.Blam.Theme;
+       using Defines = Imendio.Blam.Defines;
+       using Preference = PreferencesDialog.Preference;
+
+       public class ThemeManager : INotifyPropertyChanged
+       {
+               public event PropertyChangedEventHandler PropertyChanged;
+
+               List<ITheme> themes;
+
+               ITheme current;
+               public ITheme Current {
+                       get {
+                               return current;
+                       }
+
+                       private set {
+                               current = value;
+                               OnNotifyPropertyChanged();
+                       }
+               }
+
+               public ThemeManager()
+               {
+                       themes = new List<ITheme>();
+                       loadThemes(Defines.PERSONAL_THEME_DIR);
+                       loadThemes(Defines.THEME_DIR);
+
+                       var themePath = Conf.Get(Preference.THEME, Defines.DEFAULT_THEME);
+                       var theme = themes.Find(t => t.Path.Equals(themePath));
+                       // 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;
+               }
+
+               /// <summary>
+               /// Loads the themes available in a particular directory
+               /// </summary>
+               /// <param name="path">Path to look in</param>
+               void loadThemes(string path)
+               {
+                       try {
+                               var dirs = Directory.GetDirectories(path);
+
+                               foreach (string dir in dirs) {
+                                       try {
+                                               themes.Add(new Theme(dir));
+                                       } catch {
+                                               /* Do nothing, just continue to the next one */
+                                       }
+                               }
+                       } catch (DirectoryNotFoundException) {
+                               /* This dir doesn't exist, no sweat. */
+                               return;
+                       }
+               }
+
+               void OnNotifyPropertyChanged([CallerMemberName] string name = "")
+               {
+                       if (PropertyChanged != null) {
+                               PropertyChanged(this, new PropertyChangedEventArgs(name));
+                       }
+               }
+
+       }
+}
 
 namespace Imendio.Blam {
        


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