diff options
author | Timm Bäder <mail@baedert.org> | 2020-04-29 16:39:22 +0200 |
---|---|---|
committer | Timm Bäder <mail@baedert.org> | 2020-05-05 08:20:10 +0200 |
commit | fb9b54d4b209afc10d7794dd7356e436ee9aa36e (patch) | |
tree | de6944e65f580479eb0a17395c8f6944118a3c46 /gtk/gtkshortcutcontroller.c | |
parent | cdb4d7112593191611d0666c3da82ee2d036b9e0 (diff) | |
download | gtk+-fb9b54d4b209afc10d7794dd7356e436ee9aa36e.tar.gz |
shortcutcontroller: Use a GArray instead of a linked list
Diffstat (limited to 'gtk/gtkshortcutcontroller.c')
-rw-r--r-- | gtk/gtkshortcutcontroller.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c index a3c62ead72..33199ee5d6 100644 --- a/gtk/gtkshortcutcontroller.c +++ b/gtk/gtkshortcutcontroller.c @@ -283,7 +283,6 @@ shortcut_data_free (gpointer data) ShortcutData *sdata = data; g_object_unref (sdata->shortcut); - g_free (sdata); } static gboolean @@ -294,13 +293,12 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller, gboolean enable_mnemonics) { GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (controller); - guint i; - GSList *shortcuts = NULL; - GSList *l; + int i, p; + GArray *shortcuts = NULL; gboolean has_exact = FALSE; gboolean retval = FALSE; - for (i = 0; i < g_list_model_get_n_items (self->shortcuts); i++) + for (i = 0, p = g_list_model_get_n_items (self->shortcuts); i < p; i++) { GtkShortcut *shortcut; ShortcutData *data; @@ -325,8 +323,8 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller, case GDK_KEY_MATCH_EXACT: if (!has_exact) { - g_slist_free_full (shortcuts, shortcut_data_free); - shortcuts = NULL; + if (shortcuts) + g_array_set_size (shortcuts, 0); } has_exact = TRUE; break; @@ -353,20 +351,28 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller, continue; } - data = g_new0 (ShortcutData, 1); + if (G_UNLIKELY (!shortcuts)) + { + shortcuts = g_array_sized_new (FALSE, TRUE, sizeof (ShortcutData), 8); + g_array_set_clear_func (shortcuts, shortcut_data_free); + } + + g_array_set_size (shortcuts, shortcuts->len + 1); + data = &g_array_index (shortcuts, ShortcutData, shortcuts->len - 1); data->shortcut = shortcut; data->index = index; data->widget = widget; - - shortcuts = g_slist_append (shortcuts, data); } - for (l = shortcuts; l; l = l->next) + if (!shortcuts) + return retval; + + for (i = shortcuts->len - 1, p = shortcuts->len; i >= 0; i--) { - ShortcutData *data = l->data; + const ShortcutData *data = &g_array_index (shortcuts, ShortcutData, i); if (gtk_shortcut_action_activate (gtk_shortcut_get_action (data->shortcut), - shortcuts->next == NULL ? GTK_SHORTCUT_ACTION_EXCLUSIVE : 0, + i == p - 1 ? GTK_SHORTCUT_ACTION_EXCLUSIVE : 0, data->widget, gtk_shortcut_get_arguments (data->shortcut))) { @@ -376,7 +382,7 @@ gtk_shortcut_controller_run_controllers (GtkEventController *controller, } } - g_slist_free_full (shortcuts, shortcut_data_free); + g_array_free (shortcuts, TRUE); return retval; } |