[geary/mjog/folder-actions: 4/7] Plugin.Application: Add support for registering GLib Actions



commit a872c70a42bf5b06624698acc33c0fb0989cd2f2
Author: Michael Gratton <mike vee net>
Date:   Fri Mar 20 15:14:15 2020 +1100

    Plugin.Application: Add support for registering GLib Actions
    
    Allow plugins to register actions with the app for use in UI widgets.
    Create a per-plugin action group as needed, add to main windows using
    a plugin-specific group name and add/remove registered actions to that.

 .../application/application-plugin-manager.vala    | 45 +++++++++++++++++++++-
 src/client/plugin/plugin-application.vala          | 20 ++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)
---
diff --git a/src/client/application/application-plugin-manager.vala 
b/src/client/application/application-plugin-manager.vala
index 1967e1c1..50988d48 100644
--- a/src/client/application/application-plugin-manager.vala
+++ b/src/client/application/application-plugin-manager.vala
@@ -46,14 +46,40 @@ public class Application.PluginManager : GLib.Object {
     private class ApplicationImpl : Geary.BaseObject, Plugin.Application {
 
 
+        internal string action_group_name { get; private set; }
+
+        private Peas.PluginInfo plugin;
         private Client backing;
         private FolderStoreFactory folders;
+        private GLib.SimpleActionGroup? action_group = null;
 
 
-        public ApplicationImpl(Client backing,
+        public ApplicationImpl(Peas.PluginInfo plugin,
+                               Client backing,
                                FolderStoreFactory folders) {
+            this.plugin = plugin;
             this.backing = backing;
             this.folders = folders;
+            this.action_group_name = plugin.get_module_name().replace(".", "_");
+        }
+
+        public override void register_action(GLib.Action action) {
+            if (this.action_group == null) {
+                this.action_group = new GLib.SimpleActionGroup();
+                this.backing.window_added.connect(on_window_added);
+                foreach (MainWindow main in this.backing.get_main_windows()) {
+                    main.insert_action_group(
+                        this.action_group_name,
+                        this.action_group
+                    );
+                }
+            }
+
+            this.action_group.add_action(action);
+        }
+
+        public override void deregister_action(GLib.Action action) {
+            this.action_group.remove_action(action.get_name());
         }
 
         public override void show_folder(Plugin.Folder folder) {
@@ -63,6 +89,18 @@ public class Application.PluginManager : GLib.Object {
             }
         }
 
+        private void on_window_added(Gtk.Window window) {
+            if (this.action_group != null) {
+                var main = window as MainWindow;
+                if (main != null) {
+                    main.insert_action_group(
+                        this.action_group_name,
+                        this.action_group
+                    );
+                }
+            }
+        }
+
     }
 
 
@@ -202,11 +240,14 @@ public class Application.PluginManager : GLib.Object {
     }
 
     private void on_load_plugin(Peas.PluginInfo info) {
+        var plugin_application = new ApplicationImpl(
+            info, this.application, this.folders_factory
+        );
         var plugin = this.plugins.create_extension(
             info,
             typeof(Plugin.PluginBase),
             "plugin_application",
-            new ApplicationImpl(this.application, this.folders_factory)
+            plugin_application
         ) as Plugin.PluginBase;
         if (plugin != null) {
             bool do_activate = true;
diff --git a/src/client/plugin/plugin-application.vala b/src/client/plugin/plugin-application.vala
index 20310785..0d0687ef 100644
--- a/src/client/plugin/plugin-application.vala
+++ b/src/client/plugin/plugin-application.vala
@@ -15,6 +15,26 @@
 public interface Plugin.Application : Geary.BaseObject {
 
 
+    /**
+     * Registers a plugin action with the application.
+     *
+     * Once registered, the action will be available for use in user
+     * interface elements such as {@see Button}.
+     *
+     * @see unregister_action
+     */
+    public abstract void register_action(GLib.Action action);
+
+    /**
+     * De-registers a plugin action with the application.
+     *
+     * Makes a previously registered no longer available.
+     *
+     * @see register_action
+     */
+    public abstract void deregister_action(GLib.Action action);
+
+    /** Displays a folder in the most recently used main window. */
     public abstract void show_folder(Folder folder);
 
 }


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