[geary/wip/gtkactions: 1/4] First stab at a Gtk->GLib .Action bridge



commit c7fb7ba77d1ec1fcf10688ef4f39841a3a67e689
Author: Charles Lindsay <chaz yorba org>
Date:   Mon Dec 9 15:59:04 2013 -0800

    First stab at a Gtk->GLib .Action bridge

 src/CMakeLists.txt                               |    1 +
 src/client/application/geary-action-adapter.vala |   59 ++++++++++++++++++++++
 src/client/application/geary-controller.vala     |   44 +++++++++++-----
 3 files changed, 91 insertions(+), 13 deletions(-)
---
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9f8278a..871f8e5 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -290,6 +290,7 @@ engine/util/util-trillian.vala
 )
 
 set(CLIENT_SRC
+client/application/geary-action-adapter.vala
 client/application/geary-application.vala
 client/application/geary-args.vala
 client/application/geary-config.vala
diff --git a/src/client/application/geary-action-adapter.vala 
b/src/client/application/geary-action-adapter.vala
new file mode 100644
index 0000000..ff31997
--- /dev/null
+++ b/src/client/application/geary-action-adapter.vala
@@ -0,0 +1,59 @@
+/* Copyright 2013 Yorba Foundation
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+/**
+ * A bridge between Gtk.Action and GLib.Action.  Doesn't handle stateful
+ * Actions.  Also, assumes most properties of the Gtk.Action won't change
+ * during its life.
+ */
+public class Geary.ActionAdapter : SimpleAction {
+    private delegate void RecursionGuardFunc();
+    
+    private Gtk.Action gtk_action;
+    private bool recursing = false;
+    
+    public ActionAdapter(Gtk.Action gtk_action) {
+        Object(name: gtk_action.name);
+        
+        this.gtk_action = gtk_action;
+        gtk_action.activate.connect(on_gtk_activated);
+        gtk_action.notify["sensitive"].connect(on_gtk_sensitive_changed);
+        
+        activate.connect(on_activated);
+        notify["enabled"].connect(on_enabled_changed);
+    }
+    
+    private void guard_recursion(RecursionGuardFunc f) {
+        if (recursing)
+            return;
+        
+        recursing = true;
+        f();
+        recursing = false;
+    }
+    
+    private void on_gtk_activated() {
+        guard_recursion(() => activate(null));
+    }
+    
+    private void on_gtk_sensitive_changed() {
+        guard_recursion(() => {
+            if (enabled != gtk_action.sensitive)
+                set_enabled(gtk_action.sensitive);
+        });
+    }
+    
+    private void on_activated() {
+        guard_recursion(() => gtk_action.activate());
+    }
+    
+    private void on_enabled_changed() {
+        guard_recursion(() => {
+            if (gtk_action.sensitive != enabled)
+                gtk_action.sensitive = enabled;
+        });
+    }
+}
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index af171d6..9db35a3 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -144,15 +144,9 @@ public class GearyController : Geary.BaseObject {
         IconFactory.instance.init();
         
         // Setup actions.
-        GearyApplication.instance.actions.add_actions(create_actions(), this);
-        GearyApplication.instance.actions.add_toggle_actions(create_toggle_actions(), this);
-        GearyApplication.instance.ui_manager.insert_action_group(
-            GearyApplication.instance.actions, 0);
+        setup_actions();
         GearyApplication.instance.load_ui_file("accelerators.ui");
         
-        // some actions need a little extra help
-        prepare_actions();
-        
         // Listen for attempts to close the application.
         GearyApplication.instance.exiting.connect(on_application_exiting);
         
@@ -387,12 +381,36 @@ public class GearyController : Geary.BaseObject {
         return entries;
     }
     
-    private void prepare_actions() {
-        GearyApplication.instance.get_action(ACTION_NEW_MESSAGE).is_important = true;
-        GearyApplication.instance.get_action(ACTION_REPLY_TO_MESSAGE).is_important = true;
-        GearyApplication.instance.get_action(ACTION_REPLY_ALL_MESSAGE).is_important = true;
-        GearyApplication.instance.get_action(ACTION_FORWARD_MESSAGE).is_important = true;
-        GearyApplication.instance.get_action(ACTION_DELETE_MESSAGE).is_important = true;
+    private void setup_actions() {
+        const string[] important_actions = {
+            ACTION_NEW_MESSAGE,
+            ACTION_REPLY_TO_MESSAGE,
+            ACTION_REPLY_ALL_MESSAGE,
+            ACTION_FORWARD_MESSAGE,
+            ACTION_DELETE_MESSAGE,
+        };
+        
+        Gtk.ActionGroup action_group = GearyApplication.instance.actions;
+        Gee.ArrayList<Action> actions = new Gee.ArrayList<Action>();
+        
+        Gtk.ActionEntry[] action_entries = create_actions();
+        action_group.add_actions(action_entries, this);
+        foreach (Gtk.ActionEntry e in action_entries) {
+            Gtk.Action action = action_group.get_action(e.name);
+            assert(action != null);
+            
+            if (e.name in important_actions)
+                action.is_important = true;
+            actions.add(new Geary.ActionAdapter(action));
+        }
+        
+        Gtk.ToggleActionEntry[] toggle_action_entries = create_toggle_actions();
+        action_group.add_toggle_actions(toggle_action_entries, this);
+        // Don't worry about toggle actions in our action list.
+        
+        foreach (Action a in actions)
+            GearyApplication.instance.add_action(a);
+        GearyApplication.instance.ui_manager.insert_action_group(action_group, 0);
     }
     
     private void open_account(Geary.Account account) {


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