Plugin.Composer: Support registering composer menu items

Allow plugins to register per-composer actions and add menu items to
their own section in the composer's menu.
This commit is contained in:
Michael Gratton 2020-07-03 18:03:47 +10:00 committed by Michael James Gratton
parent ec44114612
commit 41de9e537b
3 changed files with 82 additions and 0 deletions

View file

@ -380,12 +380,18 @@ public class Application.PluginManager : GLib.Object {
private Composer.Widget backing;
private weak ApplicationImpl application;
private GLib.SimpleActionGroup? action_group = null;
private GLib.Menu? menu_items = null;
private string action_group_name;
public ComposerImpl(Composer.Widget backing,
ApplicationImpl application) {
this.backing = backing;
this.application = application;
this.action_group_name = application.plugin.action_group_name + "-cmp";
}
public void save_to_folder(Plugin.Folder? location) {
@ -411,6 +417,37 @@ public class Application.PluginManager : GLib.Object {
this.application.backing.controller.present_composer(this.backing);
}
public void register_action(GLib.Action action) {
if (this.action_group == null) {
this.action_group = new GLib.SimpleActionGroup();
this.backing.insert_action_group(
this.action_group_name,
this.action_group
);
}
this.action_group.add_action(action);
}
public void deregister_action(GLib.Action action) {
this.action_group.remove_action(action.get_name());
}
public void append_menu_item(Plugin.Actionable menu_item) {
if (this.menu_items == null) {
this.menu_items = new GLib.Menu();
this.backing.insert_menu_section(this.menu_items);
}
this.menu_items.append(
menu_item.label,
GLib.Action.print_detailed_name(
this.action_group_name + "." + menu_item.action.name,
menu_item.action_target
)
);
}
}

View file

@ -1104,6 +1104,16 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
}
}
/**
* Inserts a menu section into the composer's menu.
*/
public void insert_menu_section(GLib.MenuModel section) {
var menu = this.more_options_button.menu_model as GLib.Menu;
if (menu != null) {
menu.insert_section(0, null, section);
}
}
/** Overrides the draft folder as a destination for saving. */
internal async void set_save_to_override(Geary.Folder? save_to)
throws GLib.Error {

View file

@ -80,4 +80,39 @@ public interface Plugin.Composer : Geary.BaseObject {
*/
public abstract void save_to_folder(Plugin.Folder? location);
/**
* Registers a plugin action with this specific composer.
*
* Once registered, the action will be available for use in user
* interface elements such as {@link Actionable}.
*
* @see deregister_action
*/
public abstract void register_action(GLib.Action action);
/**
* De-registers a plugin action, removing it from this composer.
*
* Makes a previously registered no longer available.
*
* @see register_action
*/
public abstract void deregister_action(GLib.Action action);
/**
* Adds a menu item to the composer's menu.
*
* The menu item will be added to a section unique to this plugin
* on the composer's menu. The item's action must be registered
* either with the application via {@link
* Application.register_action} if it is a global action, or with
* the composer via {@link register_action} if it is
* composer-specific for it to be successfully activated.
*
* @see register_action
* @see Application.register_action
*/
public abstract void append_menu_item(Actionable menu_item);
}