birb/birb

Add BirbActionMenu

5 months ago, Gary Kramlich
bfc924c97a93
Parents de1f67d75292
Children 42f710f0a958
Add BirbActionMenu

This class makes it easy to populate a menu via a signal emission.

Testing Done:
Ran the unit tests under valgrind.

Reviewed at https://reviews.imfreedom.org/r/2902/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/birb/birbactionmenu.c Fri Jan 12 01:49:51 2024 -0600
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2023 Birb Developers
+ *
+ * Birb is the legal property of its developers, whose names are too
+ * numerous to list here. Please refer to the AUTHORS file distributed
+ * with this source distribution
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "birbactionmenu.h"
+
+struct _BirbActionMenu {
+ GObject parent;
+
+ GMenu *menu;
+ GHashTable *action_groups;
+};
+
+/******************************************************************************
+ * GObject Implementation
+ *****************************************************************************/
+G_DEFINE_TYPE(BirbActionMenu, birb_action_menu, G_TYPE_OBJECT)
+
+static void
+birb_action_menu_finalize(GObject *obj) {
+ BirbActionMenu *menu = BIRB_ACTION_MENU(obj);
+
+ g_clear_object(&menu->menu);
+ g_clear_pointer(&menu->action_groups, g_hash_table_unref);
+
+ G_OBJECT_CLASS(birb_action_menu_parent_class)->finalize(obj);
+}
+
+static void
+birb_action_menu_init(BirbActionMenu *menu) {
+ menu->menu = g_menu_new();
+ menu->action_groups = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, g_object_unref);
+}
+
+static void
+birb_action_menu_class_init(BirbActionMenuClass *klass) {
+ GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+
+ obj_class->finalize = birb_action_menu_finalize;
+}
+
+/******************************************************************************
+ * Public API
+ *****************************************************************************/
+BirbActionMenu *
+birb_action_menu_new(void) {
+ return g_object_new(BIRB_TYPE_ACTION_MENU, NULL);
+}
+
+GMenu *
+birb_action_menu_get_menu(BirbActionMenu *menu) {
+ g_return_val_if_fail(BIRB_IS_ACTION_MENU(menu), NULL);
+
+ return menu->menu;
+}
+
+gboolean
+birb_action_menu_add_action_group(BirbActionMenu *menu, const char *prefix,
+ GActionGroup *group)
+{
+ g_return_val_if_fail(BIRB_IS_ACTION_MENU(menu), FALSE);
+ g_return_val_if_fail(prefix != NULL, FALSE);
+ g_return_val_if_fail(G_IS_ACTION_GROUP(group), FALSE);
+
+ if(g_hash_table_contains(menu->action_groups, prefix)) {
+ return FALSE;
+ }
+
+ g_hash_table_insert(menu->action_groups, g_strdup(prefix),
+ g_object_ref(group));
+
+ return TRUE;
+}
+
+void
+birb_action_menu_foreach_action_group(BirbActionMenu *menu,
+ BirbActionMenuForeachActionGroupFunc func,
+ gpointer user_data)
+{
+ GHashTableIter iter;
+ gpointer key = NULL;
+ gpointer value = NULL;
+
+ g_return_if_fail(BIRB_IS_ACTION_MENU(menu));
+ g_return_if_fail(func != NULL);
+
+ g_hash_table_iter_init(&iter, menu->action_groups);
+ while(g_hash_table_iter_next(&iter, &key, &value)) {
+ func(key, value, user_data);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/birb/birbactionmenu.h Fri Jan 12 01:49:51 2024 -0600
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2024 Birb Developers
+ *
+ * Birb is the legal property of its developers, whose names are too
+ * numerous to list here. Please refer to the AUTHORS file distributed
+ * with this source distribution
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#if !defined(BIRB_GLOBAL_HEADER_INSIDE) && !defined(BIRB_COMPILATION)
+# error "only <birb.h> may be included directly"
+#endif
+
+#ifndef BIRB_ACTION_MENU_H
+#define BIRB_ACTION_MENU_H
+
+#include <gio/gio.h>
+
+#include "birbversion.h"
+
+G_BEGIN_DECLS
+
+/**
+ * BirbActionMenuForeachActionGroupFunc:
+ * @prefix: The prefix for the group.
+ * @group: The [iface@Gio.ActionGroup].
+ * @user_data: User data.
+ *
+ * A function pointer for iterating through the [iface@Gio.ActionGroup]s in
+ * a [class@ActionMenu].
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_TYPE_IN_0_1
+typedef void (*BirbActionMenuForeachActionGroupFunc)(const char *prefix, GActionGroup *group, gpointer user_data);
+
+#define BIRB_TYPE_ACTION_MENU (birb_action_menu_get_type())
+
+/**
+ * BirbActionMenu:
+ *
+ * A BirbActionMenu was created to allow interested parties to add items to a
+ * menu by listening to a signal.
+ *
+ * While the primary user interface driving this is GTK, it should be usable
+ * for other interfaces.
+ *
+ * It allows handlers to add menu items and sections by exposing the menu via
+ * [method@ActionMenu.get_menu]. Those handlers can then also add an action
+ * group by passing a `prefix` and the [iface@Gio.ActionGroup] to
+ * [method@ActionMenu.add_action_group].
+ *
+ * User interfaces can then call [method@ActionMenu.foreach_action_group] to
+ * handle the action groups.
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_IN_0_1
+G_DECLARE_FINAL_TYPE(BirbActionMenu, birb_action_menu,
+ BIRB, ACTION_MENU, GObject)
+
+/**
+ * birb_action_menu_new:
+ *
+ * Creates a new action menu.
+ *
+ * Returns: (transfer full): The new instance.
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_IN_0_1
+BirbActionMenu *birb_action_menu_new(void);
+
+/**
+ * birb_action_menu_get_menu:
+ * @menu: The instance.
+ *
+ * Gets the [class@Gio.Menu] from @menu so that you may add items and sections
+ * to it.
+ *
+ * Returns: (transfer none): The menu.
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_IN_0_1
+GMenu *birb_action_menu_get_menu(BirbActionMenu *menu);
+
+/**
+ * birb_action_menu_add_action_group:
+ * @menu: The instance.
+ * @prefix: The prefix for the group.
+ * @group: (transfer none): The [iface@Gio.ActionGroup].
+ *
+ * Adds @group to @menu with @prefix. If @prefix already exists, %FALSE will
+ * be returned and @group will not be added to @menu.
+ *
+ * Returns: %TRUE if the prefix did not exist, otherwise %FALSE.
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_IN_0_1
+gboolean birb_action_menu_add_action_group(BirbActionMenu *menu, const char *prefix, GActionGroup *group);
+
+/**
+ * birb_action_menu_foreach_action_group:
+ * @menu: The instance.
+ * @func: (scope call): The function to call on each action group.
+ * @user_data: Data to pass to @func.
+ *
+ * Calls @func with the prefix and action group from each item in @menu as well
+ * as @user_data.
+ *
+ * Since: 0.1.0
+ */
+BIRB_AVAILABLE_IN_0_1
+void birb_action_menu_foreach_action_group(BirbActionMenu *menu, BirbActionMenuForeachActionGroupFunc func, gpointer user_data);
+
+G_END_DECLS
+
+#endif /* BIRB_ACTION_MENU_H */
--- a/birb/meson.build Tue Dec 19 01:14:43 2023 -0600
+++ b/birb/meson.build Fri Jan 12 01:49:51 2024 -0600
@@ -1,9 +1,11 @@
BIRB_SOURCES = [
+ 'birbactionmenu.c',
'birbqueuedoutputstream.c',
'birbversion.c',
]
BIRB_HEADERS = [
+ 'birbactionmenu.h',
'birbqueuedoutputstream.h',
'birbversion.h',
]
--- a/birb/tests/meson.build Tue Dec 19 01:14:43 2023 -0600
+++ b/birb/tests/meson.build Fri Jan 12 01:49:51 2024 -0600
@@ -1,4 +1,5 @@
PROGRAMS = [
+ 'action_menu',
'queued_output_stream',
]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/birb/tests/test_action_menu.c Fri Jan 12 01:49:51 2024 -0600
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2023 Birb Developers
+ *
+ * Birb is the legal property of its developers, whose names are too
+ * numerous to list here. Please refer to the AUTHORS file distributed
+ * with this source distribution
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
+ */
+
+#include <glib.h>
+#include <string.h>
+
+#include <birb.h>
+
+/******************************************************************************
+ * Callbacks
+ *****************************************************************************/
+static void
+test_action_menu_foreach_action_group_counter(const char *prefix,
+ GActionGroup *group,
+ gpointer data)
+{
+ guint *counter = data;
+
+ g_assert_nonnull(prefix);
+ g_assert_true(G_IS_ACTION_GROUP(group));
+
+ *counter = *counter + 1;
+}
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_action_menu_new(void) {
+ BirbActionMenu *action_menu = NULL;
+ GMenu *menu = NULL;
+ guint counter = 0;
+
+ action_menu = birb_action_menu_new();
+ g_assert_true(BIRB_IS_ACTION_MENU(action_menu));
+
+ menu = birb_action_menu_get_menu(action_menu);
+ g_assert_true(G_IS_MENU(menu));
+
+ birb_action_menu_foreach_action_group(action_menu,
+ test_action_menu_foreach_action_group_counter,
+ &counter);
+
+ g_assert_cmpuint(counter, ==, 0);
+
+ g_assert_finalize_object(action_menu);
+}
+
+static void
+test_action_menu_new_multiple_groups(void) {
+ BirbActionMenu *menu = NULL;
+ GActionGroup *group1 = NULL;
+ GActionGroup *group2 = NULL;
+ guint counter = 0;
+
+ menu = birb_action_menu_new();
+ g_assert_true(BIRB_IS_ACTION_MENU(menu));
+
+ group1 = g_simple_action_group_new();
+ birb_action_menu_add_action_group(menu, "foo", group1);
+
+ group2 = g_simple_action_group_new();
+ birb_action_menu_add_action_group(menu, "bar", group2);
+
+ birb_action_menu_foreach_action_group(menu,
+ test_action_menu_foreach_action_group_counter,
+ &counter);
+
+ g_assert_cmpuint(counter, ==, 2);
+
+ g_assert_finalize_object(menu);
+ g_assert_finalize_object(group1);
+ g_assert_finalize_object(group2);
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+int
+main(int argc, char **argv) {
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_set_nonfatal_assertions();
+
+ g_test_add_func("/action-menu/new", test_action_menu_new);
+ g_test_add_func("/action-menu/new-multiple-groups",
+ test_action_menu_new_multiple_groups);
+
+ return g_test_run();
+}