gactionmap: Add g_action_map_remove_action_entries()

Since we have a convenience method to add actions let's allow to remove
them just as easily. This makes resource cleanup as simple as initially
adding the entries.
This commit is contained in:
Guido Günther 2023-05-24 16:00:30 +02:00 committed by Philip Withnall
parent ac840b954f
commit b3b23072f3
4 changed files with 74 additions and 0 deletions

View File

@ -3559,6 +3559,7 @@ g_action_map_lookup_action
GActionEntry
g_action_map_add_action_entries
g_action_map_add_action
g_action_map_remove_action_entries
g_action_map_remove_action
<SUBSECTION Standard>

View File

@ -283,3 +283,47 @@ g_action_map_add_action_entries (GActionMap *action_map,
g_object_unref (action);
}
}
/**
* g_action_map_remove_action_entries:
* @action_map: The #GActionMap
* @entries: (array length=n_entries) (element-type GActionEntry): a pointer to
* the first item in an array of #GActionEntry structs
* @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated
*
* Remove actions from a #GActionMap. This is meant as the reverse of
* g_action_map_add_action_entries().
*
*
* |[<!-- language="C" -->
* static const GActionEntry entries[] = {
* { "quit", activate_quit },
* { "print-string", activate_print_string, "s" }
* };
*
* void
* add_actions (GActionMap *map)
* {
* g_action_map_add_action_entries (map, entries, G_N_ELEMENTS (entries), NULL);
* }
*
* void
* remove_actions (GActionMap *map)
* {
* g_action_map_remove_action_entries (map, entries, G_N_ELEMENTS (entries));
* }
* ]|
*
* Since: 2.78
*/
void
g_action_map_remove_action_entries (GActionMap *action_map,
const GActionEntry entries[],
gint n_entries)
{
g_return_if_fail (G_IS_ACTION_MAP (action_map));
g_return_if_fail (entries != NULL || n_entries == 0);
for (int i = 0; n_entries < 0 ? entries[i].name != NULL : i < n_entries; i++)
g_action_map_remove_action (action_map, entries[i].name);
}

View File

@ -91,6 +91,10 @@ void g_action_map_add_action_entries (GAction
const GActionEntry *entries,
gint n_entries,
gpointer user_data);
GIO_AVAILABLE_IN_2_78
void g_action_map_remove_action_entries (GActionMap *action_map,
const GActionEntry *entries,
gint n_entries);
G_END_DECLS

View File

@ -408,8 +408,14 @@ test_entries (void)
{ "toggle", NULL, NULL, "false", NULL, { 0 } },
{ "volume", NULL, NULL, "0", change_volume_state, { 0 } },
};
const GActionEntry entries2[] = {
{ "foo", activate_foo, NULL, NULL, NULL, { 0 } },
{ "bar", activate_bar, "s", NULL, NULL, { 0 } },
{ NULL },
};
GSimpleActionGroup *actions;
GVariant *state;
GStrv names;
actions = g_simple_action_group_new ();
g_simple_action_group_add_entries (actions, entries,
@ -465,6 +471,25 @@ test_entries (void)
g_assert_cmpint (g_variant_get_int32 (state), ==, 7);
g_variant_unref (state);
names = g_action_group_list_actions (G_ACTION_GROUP (actions));
g_assert_cmpuint (g_strv_length (names), ==, G_N_ELEMENTS (entries));
g_strfreev (names);
g_action_map_remove_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries));
names = g_action_group_list_actions (G_ACTION_GROUP (actions));
g_assert_cmpuint (g_strv_length (names), ==, 0);
g_strfreev (names);
/* Check addition and removal of %NULL terminated array */
g_action_map_add_action_entries (G_ACTION_MAP (actions), entries2, -1, NULL);
names = g_action_group_list_actions (G_ACTION_GROUP (actions));
g_assert_cmpuint (g_strv_length (names), ==, G_N_ELEMENTS (entries2) - 1);
g_strfreev (names);
g_action_map_remove_action_entries (G_ACTION_MAP (actions), entries2, -1);
names = g_action_group_list_actions (G_ACTION_GROUP (actions));
g_assert_cmpuint (g_strv_length (names), ==, 0);
g_strfreev (names);
g_object_unref (actions);
}