[baobab] Simplify "active-chart" property handling



commit 5092eb57fa220e83664d7fd576b23bd985fa0006
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Sat Jul 12 16:38:27 2014 +0200

    Simplify "active-chart" property handling
    
    We can use g_settings_create_action() to automatically manage
    the GSettings and the GtkStack property. But to do so we need to
    use an intermediate stack for the spinner, so to keep the bijective
    correspondence gsettings state <-> chart stack state.

 src/baobab-main-window.ui |   51 ++++++++++++++++++++++++--------------------
 src/baobab-window.vala    |   46 ++++++++--------------------------------
 2 files changed, 37 insertions(+), 60 deletions(-)
---
diff --git a/src/baobab-main-window.ui b/src/baobab-main-window.ui
index 1ed9762..8fecee1 100644
--- a/src/baobab-main-window.ui
+++ b/src/baobab-main-window.ui
@@ -319,33 +319,38 @@
                     <property name="hexpand">True</property>
                     <property name="vexpand">True</property>
                     <child>
-                      <object class="GtkStack" id="chart_stack">
+                      <object class="GtkStack" id="spinner_stack">
                         <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="transition_type">crossfade</property>
-                        <child>
-                          <object class="BaobabRingschart" id="rings_chart">
-                            <property name="visible">True</property>
-                            <property name="hexpand">True</property>
-                            <property name="vexpand">True</property>
-                          </object>
-                          <packing>
-                            <property name="name">rings</property>
-                            <property name="title" translatable="yes">Rings Chart</property>
-                            <property name="icon_name">view-ringschart-symbolic</property>
-                          </packing>
-                        </child>
                         <child>
-                          <object class="BaobabTreemap" id="treemap_chart">
+                          <object class="GtkStack" id="chart_stack">
                             <property name="visible">True</property>
-                            <property name="hexpand">True</property>
-                            <property name="vexpand">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="transition_type">crossfade</property>
+                            <child>
+                              <object class="BaobabRingschart" id="rings_chart">
+                                <property name="visible">True</property>
+                                <property name="hexpand">True</property>
+                                <property name="vexpand">True</property>
+                              </object>
+                              <packing>
+                                <property name="name">rings</property>
+                                <property name="title" translatable="yes">Rings Chart</property>
+                                <property name="icon_name">view-ringschart-symbolic</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="BaobabTreemap" id="treemap_chart">
+                                <property name="visible">True</property>
+                                <property name="hexpand">True</property>
+                                <property name="vexpand">True</property>
+                              </object>
+                              <packing>
+                                <property name="name">treemap</property>
+                                <property name="title" translatable="yes">Treemap Chart</property>
+                                <property name="icon_name">view-treemap-symbolic</property>
+                              </packing>
+                            </child>
                           </object>
-                          <packing>
-                            <property name="name">treemap</property>
-                            <property name="title" translatable="yes">Treemap Chart</property>
-                            <property name="icon_name">view-treemap-symbolic</property>
-                          </packing>
                         </child>
                         <child>
                           <object class="GtkSpinner" id="spinner">
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index e5a49b1..3baf13d 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -68,6 +68,8 @@ namespace Baobab {
         [GtkChild]
         private Gtk.Stack chart_stack;
         [GtkChild]
+        private Gtk.Stack spinner_stack;
+        [GtkChild]
         private Gtk.StackSwitcher chart_stack_switcher;
         [GtkChild]
         private Chart rings_chart;
@@ -80,14 +82,9 @@ namespace Baobab {
 
         static Gdk.Cursor busy_cursor;
 
-        void radio_activate (SimpleAction action, Variant? parameter) {
-            action.change_state (parameter);
-        }
-
         private const GLib.ActionEntry[] action_entries = {
             { "gear-menu", on_show_gear_menu_activate , null, "false", null},
             { "show-home-page", on_show_home_page_activate },
-            { "active-chart", radio_activate, "s", "'rings'", on_chart_type_changed },
             { "scan-home", on_scan_home_activate },
             { "scan-folder", on_scan_folder_activate },
             { "scan-remote", on_scan_remote_activate },
@@ -131,22 +128,21 @@ namespace Baobab {
             ui_settings = Application.get_default ().ui_settings;
 
             add_action_entries (action_entries, this);
+            var action = ui_settings.create_action ("active-chart");
+            add_action (action);
 
             location_list.set_adjustment (location_scrolled_window.get_vadjustment ());
             location_list.set_action (on_scan_location_activate);
             location_list.update ();
 
-            var action = lookup_action ("scan-remote") as SimpleAction;
-            action.set_enabled (ConnectServer.available ());
+            (lookup_action ("scan-remote") as SimpleAction).set_enabled (ConnectServer.available ());
 
             setup_treeview ();
 
             infobar_close_button.clicked.connect (() => { clear_message (); });
 
-            lookup_action ("active-chart").change_state (ui_settings.get_value ("active-chart"));
-
-            chart_stack.notify["visible-child-name"].connect (on_chart_stack_child_changed);
-            chart_stack.destroy.connect (() => { chart_stack.notify.disconnect 
(on_chart_stack_child_changed); });
+            ui_settings.bind ("active-chart", chart_stack, "visible-child-name", SettingsBindFlags.DEFAULT);
+            chart_stack.destroy.connect (() => { Settings.unbind (chart_stack, "visible-child-name"); });
 
             rings_chart.item_activated.connect (on_chart_item_activated);
             treemap_chart.item_activated.connect (on_chart_item_activated);
@@ -185,12 +181,6 @@ namespace Baobab {
             show ();
         }
 
-        void on_chart_stack_child_changed () {
-            if (chart_stack.visible_child_name != null) {
-                lookup_action ("active-chart").change_state (chart_stack.visible_child_name);
-            }
-        }
-
         void on_show_gear_menu_activate (SimpleAction action) {
             var state = action.get_state ().get_boolean ();
             action.set_state (new Variant.boolean (!state));
@@ -205,22 +195,6 @@ namespace Baobab {
             set_ui_state (home_page, false);
         }
 
-        void on_chart_type_changed (SimpleAction action, Variant value) {
-            switch (value as string) {
-            case "rings":
-                chart_stack.visible_child = rings_chart;
-                break;
-            case "treemap":
-                chart_stack.visible_child = treemap_chart;
-                break;
-            default:
-                return;
-            }
-
-            ui_settings.set_value ("active-chart", value);
-            action.set_state (value);
-        }
-
         void on_scan_home_activate () {
             scan_directory (File.new_for_path (GLib.Environment.get_home_dir ()));
         }
@@ -498,15 +472,13 @@ namespace Baobab {
             if (busy) {
                 cursor = busy_cursor;
                 disable_drop ();
-                (lookup_action ("active-chart") as SimpleAction).set_enabled (false);
                 chart_stack_switcher.sensitive = false;
-                chart_stack.visible_child = spinner;
+                spinner_stack.visible_child = spinner;
                 spinner.start ();
             } else {
                 enable_drop ();
-                (lookup_action ("active-chart") as SimpleAction).set_enabled (true);
                 spinner.stop ();
-                lookup_action ("active-chart").change_state (ui_settings.get_value ("active-chart"));
+                spinner_stack.visible_child = chart_stack;
                 chart_stack_switcher.sensitive = true;
             }
 


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