[gnome-mines/wip/theming-support] Implement theme switching



commit 34c70eab7e64eb2bffa2c0f4c8c4d2105b63aadd
Author: Robert Roth <robert roth off gmail com>
Date:   Wed Jan 7 19:03:05 2015 +0200

    Implement theme switching

 src/gnome-mines.vala           |   19 +++++++---
 src/theme-selector-dialog.vala |   70 +++++++++++++++++++++++++++++++++-------
 2 files changed, 71 insertions(+), 18 deletions(-)
---
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 628be73..b1a6a1f 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -41,6 +41,7 @@ public class Mines : Gtk.Application
     private Gtk.Box paused_box;
     private Gtk.ScrolledWindow scrolled;
     private Gtk.Stack stack;
+    private ThemeSelectorDialog theme_dialog;
 
     private Gtk.Label clock_label;
 
@@ -137,6 +138,12 @@ public class Mines : Gtk.Application
         {
             warning ("Error loading css styles from %s: %s", theme_css_path, e.message);
         }
+        window.get_window ().invalidate_rect (null, true);
+        window.queue_draw ();
+        Gtk.StyleContext.reset_widgets (Gdk.Screen.get_default ());
+
+        if (theme_dialog != null)
+            theme_dialog.queue_draw ();
     }
 
     protected override void startup ()
@@ -174,7 +181,7 @@ public class Mines : Gtk.Application
         {
             warning ("Could not load game UI: %s", e.message);
         }
-
+        settings.changed[KEY_THEME].connect (() => { set_game_theme (settings.get_string (KEY_THEME)); });
         set_game_theme (settings.get_string (KEY_THEME));
 
         add_action_entries (action_entries, this);
@@ -479,12 +486,12 @@ public class Mines : Gtk.Application
 
     private int show_theme_selector ()
     {
-        var dialog = new ThemeSelectorDialog ();
-        dialog.modal = true;
-        dialog.transient_for = window;
+        theme_dialog = new ThemeSelectorDialog ();
+        //dialog.modal = true;
+        theme_dialog.transient_for = window;
 
-        var result = dialog.run ();
-        dialog.destroy ();
+        var result = theme_dialog.run ();
+        theme_dialog.destroy ();
 
         return result;
     }
diff --git a/src/theme-selector-dialog.vala b/src/theme-selector-dialog.vala
index 9ee2aab..6b1e9f6 100644
--- a/src/theme-selector-dialog.vala
+++ b/src/theme-selector-dialog.vala
@@ -31,6 +31,9 @@ private class PreviewField : Minefield
 public class ThemeSelectorDialog : Gtk.Dialog
 {
 
+    private Gtk.Button previous;
+    private Gtk.Button next;
+    private Settings settings;
     public List<string> list_themes ()
     {
         string themes_dir = Path.build_path (Path.DIR_SEPARATOR_S, DATA_DIRECTORY, "themes");
@@ -43,48 +46,91 @@ public class ThemeSelectorDialog : Gtk.Dialog
         FileInfo info = null;
         while ((info = enumerator.next_file (null)) != null) {
             if (info.get_file_type () == FileType.DIRECTORY) {
-                stdout.printf ("%s\n", info.get_name ());
                 themes.append (info.get_name ());
             }
         }
         return themes;
     }
 
+    private Gtk.Widget create_preview_widget () {
+        var view = new MinefieldView (settings);
+        view.minefield = new PreviewField ();
+
+        var frame = new Gtk.AspectFrame (null, 0.5f, 0.5f, 1.0f, false);
+        frame.border_width = 6;
+        frame.add (view);
+        reveal_nonmines (view);
+        return frame;
+    }
+
     public ThemeSelectorDialog ( )
     {
         title = _("Select theme");
 
         var overlay = new Gtk.Overlay ();
-        var frame = new Gtk.AspectFrame (null, 0.5f, 0.5f, 1.0f, false);
-        frame.border_width = 6;
         get_content_area ().pack_start (overlay, true, true, 0);
 
-        var previous = new Gtk.Button.from_icon_name ("go-previous", Gtk.IconSize.LARGE_TOOLBAR);
+        previous = new Gtk.Button.from_icon_name ("go-previous", Gtk.IconSize.LARGE_TOOLBAR);
         previous.show ();
         previous.valign = Gtk.Align.CENTER;
         previous.halign = Gtk.Align.START;
         previous.get_style_context ().add_class ("navigation");
         overlay.add_overlay (previous);
 
-        var next = new Gtk.Button.from_icon_name ("go-next", Gtk.IconSize.LARGE_TOOLBAR);
+        next = new Gtk.Button.from_icon_name ("go-next", Gtk.IconSize.LARGE_TOOLBAR);
         next.show ();
         next.valign = Gtk.Align.CENTER;
         next.halign = Gtk.Align.END;
         next.get_style_context ().add_class ("navigation");
         overlay.add_overlay (next);
 
-        var settings = new Settings ("org.gnome.mines");
+        settings = new Settings ("org.gnome.mines");
         settings.delay ();
 
-        var view = new MinefieldView (settings);
-        view.minefield = new PreviewField ();
-        frame.add (view);
-        overlay.add (frame);
+        var themes = list_themes ();
+        var current_theme = settings.get_string ("theme");
+        var current_index = 0;
+        for (int i = 0; i < themes.length (); i++)
+        {
+            var theme = themes.nth_data (i);
+
+            if (current_theme == theme) {
+                current_index = i;
+            }
+        }
+
+        next.clicked.connect (() => {
+            switch_theme_preview (++current_index, themes);
+            update_sensitivities (themes, current_index);
+        });
+
+        previous.clicked.connect (() => {
+            switch_theme_preview (--current_index, themes);
+            update_sensitivities (themes, current_index);
+        });
+
+        update_sensitivities (themes, current_index);
+        overlay.add (create_preview_widget ());
         overlay.show_all ();
 
-        reveal_nonmines (view);
         set_size_request (320, 300);
-        resizable = false;
+//        resizable = false;
+    }
+
+    private void switch_theme_preview (int to_index, List<string> themes)
+    {
+        settings.set_string ("theme", themes.nth_data (to_index));
+        settings.apply ();
+        this.queue_draw ();
+        this.get_window ().invalidate_rect (null, true);
+        this.present ();
+        //this.show_all ();
+    }
+
+    private void update_sensitivities (List themes, int current_index)
+    {
+        next.set_sensitive (current_index < themes.length ()-1);
+        previous.set_sensitive (current_index > 0);
     }
 
     private void reveal_nonmines (MinefieldView view)


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