[geary/mjog/mail-merge-plugin: 52/71] Plugin.Composer: Support plugins for providing composer action bars




commit d2d3e6ee75f53a11d273884dc399e347b572a8d9
Author: Michael Gratton <mike vee net>
Date:   Tue Jul 7 16:26:56 2020 +1000

    Plugin.Composer: Support plugins for providing composer action bars
    
    Add and implement `Composer.set_action_bar`.

 .../application/application-plugin-manager.vala    | 85 ++++++++++++++++++++++
 src/client/composer/composer-widget.vala           |  8 +-
 src/client/plugin/plugin-composer.vala             |  8 ++
 3 files changed, 100 insertions(+), 1 deletion(-)
---
diff --git a/src/client/application/application-plugin-manager.vala 
b/src/client/application/application-plugin-manager.vala
index 4be609810..021d0bf59 100644
--- a/src/client/application/application-plugin-manager.vala
+++ b/src/client/application/application-plugin-manager.vala
@@ -378,6 +378,7 @@ public class Application.PluginManager : GLib.Object {
         private weak ApplicationImpl application;
         private GLib.SimpleActionGroup? action_group = null;
         private GLib.Menu? menu_items = null;
+        private Gtk.ActionBar? action_bar = null;
         private string action_group_name;
 
 
@@ -443,6 +444,90 @@ public class Application.PluginManager : GLib.Object {
             );
         }
 
+        public void set_action_bar(Plugin.ActionBar plugin_bar) {
+            if (this.action_bar != null) {
+                this.action_bar.hide();
+                this.action_bar.destroy();
+                this.action_bar = null;
+            }
+
+            this.action_bar = new Gtk.ActionBar();
+            Gtk.Box? centre = null;
+            foreach (var pos in new Plugin.ActionBar.Position[] { START, CENTRE, END}) {
+                foreach (var item in plugin_bar.get_items(pos)) {
+                    var widget = widget_for_item(item);
+                    switch (pos) {
+                    case START:
+                        this.action_bar.pack_start(widget);
+                        break;
+
+                    case CENTRE:
+                        if (centre == null) {
+                            centre = new Gtk.Box(HORIZONTAL, 0);
+                            this.action_bar.set_center_widget(centre);
+                        }
+                        centre.add(widget);
+                        break;
+
+                    case END:
+                        this.action_bar.pack_end(widget);
+                        break;
+                    }
+                }
+            }
+
+            this.action_bar.show_all();
+            this.backing.add_action_bar(this.action_bar);
+        }
+
+        private Gtk.Widget? widget_for_item(Plugin.ActionBar.Item item) {
+            var item_type = item.get_type();
+            if (item_type == typeof(Plugin.ActionBar.LabelItem)) {
+                var label = new Gtk.Label(
+                    ((Plugin.ActionBar.LabelItem) item).text
+                );
+                return label;
+            }
+            if (item_type == typeof(Plugin.ActionBar.ButtonItem)) {
+                var button_item = item as Plugin.ActionBar.ButtonItem;
+                var button = new Gtk.Button.with_label(button_item.action.label);
+                button.set_action_name(
+                    this.action_group_name + "." + button_item.action.action.name
+                );
+                if (button_item.action.action_target != null) {
+                    button.set_action_target_value(button_item.action.action_target);
+                }
+                return button;
+            }
+            if (item_type == typeof(Plugin.ActionBar.MenuItem)) {
+                var menu_item = item as Plugin.ActionBar.MenuItem;
+
+                var label = new Gtk.Box(HORIZONTAL, 6);
+                label.add(new Gtk.Label(menu_item.label));
+                label.add(new Gtk.Image.from_icon_name(
+                    "pan-up-symbolic", Gtk.IconSize.BUTTON
+                ));
+
+                var button = new Gtk.MenuButton();
+                button.direction = Gtk.ArrowType.UP;
+                button.use_popover = true;
+                button.menu_model = menu_item.menu;
+                button.add(label);
+
+                return button;
+            }
+            if (item_type == typeof(Plugin.ActionBar.GroupItem)) {
+                var group_items = item as Plugin.ActionBar.GroupItem;
+                var box = new Gtk.Box(HORIZONTAL, 0);
+                box.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED);
+                foreach (var group_item in group_items.get_items()) {
+                    box.add(widget_for_item(group_item));
+                }
+                return box;
+            }
+
+            return null;
+        }
 
     }
 
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index bf076597f..92bf5117a 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -1046,8 +1046,14 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
         }
     }
 
+    /** Adds an action bar to the composer. */
+    public void add_action_bar(Gtk.ActionBar to_add) {
+        this.action_bar_box.pack_start(to_add);
+        this.action_bar_box.reorder_child(to_add, 0);
+    }
+
     /** Overrides the draft folder as a destination for saving. */
-    internal async void set_save_to_override(Geary.Folder? save_to)
+    public async void set_save_to_override(Geary.Folder? save_to)
         throws GLib.Error {
         this.save_to = save_to;
         yield reopen_draft_manager();
diff --git a/src/client/plugin/plugin-composer.vala b/src/client/plugin/plugin-composer.vala
index 269ed2be5..8407336d4 100644
--- a/src/client/plugin/plugin-composer.vala
+++ b/src/client/plugin/plugin-composer.vala
@@ -114,5 +114,13 @@ public interface Plugin.Composer : Geary.BaseObject {
      */
     public abstract void append_menu_item(Actionable menu_item);
 
+    /**
+     * Sets an action bar for the plugin on this composer.
+     *
+     * If any existing action bar for this plugin has previously been
+     * set, it is first removed.
+     */
+    public abstract void set_action_bar(ActionBar action_bar);
+
 
 }


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