From 7230ea4e69926b6986aaf41d3b5fa4c004aa06d6 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Tue, 7 Jul 2020 16:23:55 +1000 Subject: [PATCH] Plugin.ActionBar: New class added for plugins that use action bars --- po/POTFILES.in | 1 + src/client/meson.build | 1 + src/client/plugin/plugin-action-bar.vala | 184 +++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 src/client/plugin/plugin-action-bar.vala diff --git a/po/POTFILES.in b/po/POTFILES.in index 0d02fc76..acc160a3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -92,6 +92,7 @@ src/client/folder-list/folder-list-search-branch.vala src/client/folder-list/folder-list-special-grouping.vala src/client/folder-list/folder-list-tree.vala src/client/plugin/plugin-account.vala +src/client/plugin/plugin-action-bar.vala src/client/plugin/plugin-actionable.vala src/client/plugin/plugin-application.vala src/client/plugin/plugin-composer.vala diff --git a/src/client/meson.build b/src/client/meson.build index d6134ba9..ca995fe5 100644 --- a/src/client/meson.build +++ b/src/client/meson.build @@ -113,6 +113,7 @@ client_vala_sources = files( 'folder-list/folder-list-special-grouping.vala', 'plugin/plugin-account.vala', + 'plugin/plugin-action-bar.vala', 'plugin/plugin-actionable.vala', 'plugin/plugin-application.vala', 'plugin/plugin-composer.vala', diff --git a/src/client/plugin/plugin-action-bar.vala b/src/client/plugin/plugin-action-bar.vala new file mode 100644 index 00000000..18eaac6d --- /dev/null +++ b/src/client/plugin/plugin-action-bar.vala @@ -0,0 +1,184 @@ +/* + * Copyright © 2020 Michael Gratton + * + * This software is licensed under the GNU Lesser General Public License + * (version 2.1 or later). See the COPYING file in this distribution. + */ + +/** + * Enables plugins to display an action bar. + * + * Action bars are horizontal containers for buttons, menu buttons and + * labels that can be added individually or in groups, at the start, + * centre, or end of the bar. These interface items are added by + * creating an appropriate {@link Item} instance and calling {@link + * append_item}. + * + * The {@link Actionable} instances added to the action bar must have + * their actions registered either globally for the application using + * {@link Application.register_action} or locally for a specific UI + * element, for example using {@link Composer.register_action}. + */ +public class Plugin.ActionBar : Geary.BaseObject { + + + /** + * Determines the position of a widget added to an action bar. + * + * @see append_item + */ + public enum Position { + /** + * The widget is added at the start of the action bar. + * + * The start of the bar is the left side in locales with + * left-to-right writing direction, and the right side in + * right-to-left locales. + */ + START, + + /** The widget is added in the centre of the action bar. */ + CENTRE, + + /** + * The widget is added at the end of the action bar. + * + * The end of the bar is the right side in locales with + * left-to-right writing direction, and the left side in + * right-to-left locales. + */ + END; + } + + + /** Denotes an object that can be added to an action bar. */ + public interface Item: GLib.Object { + + } + + /** A text label item for an action bar. */ + public class LabelItem: GLib.Object, Item { + + + public string text { get; private set; } + + /** Constructs a text label item for an action bar. */ + public LabelItem(string text) { + this.text = text; + } + + } + + /** A button item for an action bar. */ + public class ButtonItem: GLib.Object, Item { + + + public Actionable action { get; private set; } + + + /** Constructs a button item for an action bar. */ + public ButtonItem(Actionable action) { + this.action = action; + } + + } + + /** A menu for an action bar. */ + public class MenuItem: GLib.Object, Item { + + + public string label { get; private set; } + public GLib.MenuModel menu { get; private set; } + + + /** Constructs a menu item for an action bar. */ + public MenuItem(string label, GLib.MenuModel menu) { + this.label = label; + this.menu = menu; + } + + } + + /** + * A group of items for an action bar. + * + * Groups will be displayed in a way that indicates they are + * related, for example as pill buttons. Items in the group are + * laid out in the same direction as the current locale's writing + * direction. + */ + public class GroupItem: GLib.Object, Item { + + + private Gee.List items = new Gee.LinkedList(); + + + /** Constructs a button item for an action bar. */ + public GroupItem(Gee.Collection? items = null) { + if (items != null) { + this.items.add_all(items); + } + } + + /** Appends an item to end of the group. */ + public void append_item(Item item) { + this.items.add(item); + } + + /** Returns a read-only list of items in the group. */ + public Gee.List get_items() { + return this.items.read_only_view; + } + + } + + + private Gee.List start_items = new Gee.LinkedList(); + private Gee.List centre_items = new Gee.LinkedList(); + private Gee.List end_items = new Gee.LinkedList(); + + + /** Constructs a new, empty action bar. */ + public ActionBar() { + } + + /** + * Appends an item to the action bar in the given location. + * + * Items at the same position are laid out in the same direction + * as the current locale's writing direction. + */ + public void append_item(Item item, Position item_position) { + switch (item_position) { + case START: + this.start_items.add(item); + break; + case CENTRE: + this.centre_items.add(item); + break; + case END: + this.end_items.add(item); + break; + } + } + + /** Returns a read-only list of items at the given position. */ + public Gee.List get_items(Position item_position) { + Gee.List? items = null; + switch (item_position) { + case START: + items = this.start_items.read_only_view; + break; + + case CENTRE: + items = this.centre_items.read_only_view; + break; + + case END: + items = this.end_items.read_only_view; + break; + } + return items; + } + +}