[file-roller/wip/jtojnar/gtk4: 2/3] Remove the use of GtkAccelGroup




commit d908ab68dd10fd316304d6a33e499a4b1d1f860b
Author: Jan Tojnar <jtojnar gmail com>
Date:   Mon Dec 13 17:19:26 2021 +0100

    Remove the use of GtkAccelGroup

 src/fr-window-actions-entries.h |  22 ++++----
 src/fr-window.c                 |  24 +--------
 src/gtk-utils.c                 | 113 ++++++----------------------------------
 src/gtk-utils.h                 |  10 ++--
 4 files changed, 33 insertions(+), 136 deletions(-)
---
diff --git a/src/fr-window-actions-entries.h b/src/fr-window-actions-entries.h
index 2c728fde..08c8f3b0 100644
--- a/src/fr-window-actions-entries.h
+++ b/src/fr-window-actions-entries.h
@@ -63,17 +63,17 @@ static const GActionEntry fr_window_actions[] = {
 
 
 static const FrAccelerator fr_window_accelerators[] = {
-       { "close", "<Control>w" },
-       { "deselect-all", "<Shift><Control>a" },
-       { "edit-copy", "<Control>c" },
-       { "extract-files", "<Control>e" },
-       { "find", "<Control>f" },
-       { "reload", "<Control>r" },
-       { "rename", "F2" },
-       { "save-as", "<Shift><Control>s" },     
-       { "select-all", "<control>a" },
-       { "view-properties", "<alt>Return" },
-       { "view-sidebar", "F9" }
+       { "win.close", "<Control>w" },
+       { "win.deselect-all", "<Shift><Control>a" },
+       { "win.edit-copy", "<Control>c" },
+       { "win.extract-files", "<Control>e" },
+       { "win.find", "<Control>f" },
+       { "win.reload", "<Control>r" },
+       { "win.rename", "F2" },
+       { "win.save-as", "<Shift><Control>s" }, 
+       { "win.select-all", "<control>a" },
+       { "win.view-properties", "<alt>Return" },
+       { "win.view-sidebar", "F9" }
 };
 
 
diff --git a/src/fr-window.c b/src/fr-window.c
index 2ac29f5a..f796c7c5 100644
--- a/src/fr-window.c
+++ b/src/fr-window.c
@@ -247,7 +247,6 @@ struct _FrWindowPrivate {
        GtkTreePath       *list_hover_path;
        GtkTreeViewColumn *filename_column;
        GtkWindowGroup    *window_group;
-       GtkAccelGroup     *accel_group;
        GHashTable        *named_dialogs;
 
        gboolean         filter_mode;
@@ -571,7 +570,6 @@ fr_window_free_private_data (FrWindow *window)
        _g_object_unref (window->priv->cancellable);
        g_hash_table_unref (window->priv->named_dialogs);
        g_object_unref (window->priv->window_group);
-       g_object_unref (window->priv->accel_group);
 }
 
 
@@ -911,12 +909,10 @@ fr_window_init (FrWindow *window)
        window->priv->cancellable = g_cancellable_new ();
        window->priv->compression = FR_COMPRESSION_NORMAL;
        window->priv->window_group = gtk_window_group_new ();
-       window->priv->accel_group = gtk_accel_group_new ();
        window->priv->populating_file_list = FALSE;
        window->priv->named_dialogs = g_hash_table_new (g_str_hash, g_str_equal);
 
        gtk_window_group_add_window (window->priv->window_group, GTK_WINDOW (window));
-       gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accel_group);
 
        window->archive = NULL;
 }
@@ -5476,33 +5472,17 @@ fr_window_attach (FrWindow      *window,
 /* -- fr_window_add_accelerators -- */
 
 
-static GtkAccelGroup *
-gth_window_get_accel_group (FrWindow *window)
-{
-       if (window->priv->accel_group == NULL) {
-               window->priv->accel_group = gtk_accel_group_new ();
-               gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accel_group);
-       }
-
-       return window->priv->accel_group;
-}
-
-
 static void
 fr_window_add_accelerators (FrWindow                 *window,
                            const FrAccelerator      *accelerators,
                            int                       n_accelerators)
 {
-       GtkAccelGroup *accel_group;
        int            i;
 
-       accel_group = gth_window_get_accel_group (window);
        for (i = 0; i < n_accelerators; i++) {
                const FrAccelerator *acc = accelerators + i;
 
-               _gtk_window_add_accelerator_for_action (GTK_WINDOW (window),
-                                                       accel_group,
-                                                       acc->action_name,
+               _gtk_add_accelerator_for_action (acc->action_name,
                                                        acc->accelerator,
                                                        NULL);
        }
@@ -5831,7 +5811,7 @@ fr_window_construct (FrWindow *window)
                 gtk_widget_show_all (button);
                 gtk_header_bar_pack_end (GTK_HEADER_BAR (window->priv->headerbar), button);
 
-                _gtk_window_add_accelerators_from_menu ((GTK_WINDOW (window)), menu);
+                _gtk_add_accelerators_from_menu (menu);
 
                 g_object_unref (builder);
         }
diff --git a/src/gtk-utils.c b/src/gtk-utils.c
index 0bbd4492..9400a055 100644
--- a/src/gtk-utils.c
+++ b/src/gtk-utils.c
@@ -715,95 +715,33 @@ _gtk_header_bar_create_image_toggle_button (const char       *icon_name,
 /* -- _gtk_window_add_accelerator_for_action -- */
 
 
-typedef struct {
-       GtkWindow *window;
-       char      *action_name;
-       GVariant  *target;
-} AccelData;
-
-
-static void
-accel_data_free (gpointer  user_data,
-                 GClosure *closure)
-{
-       AccelData *accel_data = user_data;
-
-       g_return_if_fail (accel_data != NULL);
-
-       if (accel_data->target != NULL)
-               g_variant_unref (accel_data->target);
-       g_free (accel_data->action_name);
-       g_free (accel_data);
-}
-
-
-static gboolean
-window_accelerator_activated_cb (GtkAccelGroup *accel_group,
-                                GObject                *object,
-                                guint           key,
-                                GdkModifierType         mod,
-                                gpointer                user_data)
-{
-       AccelData *accel_data = user_data;
-       GAction   *action;
-
-       action = g_action_map_lookup_action (G_ACTION_MAP (accel_data->window), accel_data->action_name);
-       if (action != NULL) {
-               g_action_activate (action, accel_data->target);
-               return GDK_EVENT_STOP;
-       }
-
-       return GDK_EVENT_PROPAGATE;
-}
-
-
 void
-_gtk_window_add_accelerator_for_action (GtkWindow      *window,
-                                       GtkAccelGroup   *accel_group,
-                                       const char      *action_name,
+_gtk_add_accelerator_for_action (const char    *action_name,
                                        const char      *accel,
                                        GVariant        *target)
 {
-       AccelData       *accel_data;
-       guint            key;
-       GdkModifierType  mods;
-       GClosure        *closure;
+       g_autofree gchar *detailed_action = NULL;
 
        if ((action_name == NULL) || (accel == NULL))
                return;
 
-       if (g_str_has_prefix (action_name, "app."))
-               return;
+       if (target == NULL) {
+               detailed_action = g_strdup(action_name);
+       } else {
+               g_autofree gchar *target_string = g_variant_print (target, TRUE);
+               detailed_action = g_strconcat (action_name, "(", target_string, ")", NULL);
+       }
 
-       accel_data = g_new0 (AccelData, 1);
-       accel_data->window = window;
-       /* remove the win. prefix from the action name */
-       if (g_str_has_prefix (action_name, "win."))
-               accel_data->action_name = g_strdup (action_name + strlen ("win."));
-       else
-               accel_data->action_name = g_strdup (action_name);
-       if (target != NULL)
-               accel_data->target = g_variant_ref (target);
+       _gtk_application_add_accelerator_for_action (GTK_APPLICATION (g_application_get_default ()), 
detailed_action, accel);
 
-       gtk_accelerator_parse (accel, &key, &mods);
-       closure = g_cclosure_new (G_CALLBACK (window_accelerator_activated_cb),
-                                 accel_data,
-                                 accel_data_free);
-       gtk_accel_group_connect (accel_group,
-                                key,
-                                mods,
-                                0,
-                                closure);
 }
 
 
-/* -- _gtk_window_add_accelerators_from_menu --  */
+/* -- _gtk_add_accelerators_from_menu --  */
 
 
 static void
-add_accelerators_from_menu_item (GtkWindow      *window,
-                                GtkAccelGroup  *accel_group,
-                                GMenuModel     *model,
+add_accelerators_from_menu_item (GMenuModel     *model,
                                 int             item)
 {
        GMenuAttributeIter      *iter;
@@ -813,6 +751,7 @@ add_accelerators_from_menu_item (GtkWindow      *window,
        const char              *action = NULL;
        GVariant                *target = NULL;
 
+
        iter = g_menu_model_iterate_item_attributes (model, item);
        while (g_menu_attribute_iter_get_next (iter, &key, &value)) {
                if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
@@ -825,21 +764,15 @@ add_accelerators_from_menu_item (GtkWindow      *window,
        }
        g_object_unref (iter);
 
-       _gtk_window_add_accelerator_for_action (window,
-                                               accel_group,
-                                               action,
-                                               accel,
-                                               target);
+       _gtk_add_accelerator_for_action (action, accel, target);
 
        if (target != NULL)
                g_variant_unref (target);
 }
 
 
-static void
-add_accelerators_from_menu (GtkWindow      *window,
-                           GtkAccelGroup  *accel_group,
-                           GMenuModel     *model)
+void
+_gtk_add_accelerators_from_menu (GMenuModel *model)
 {
        int              i;
        GMenuLinkIter   *iter;
@@ -847,11 +780,11 @@ add_accelerators_from_menu (GtkWindow      *window,
        GMenuModel      *m;
 
        for (i = 0; i < g_menu_model_get_n_items (model); i++) {
-               add_accelerators_from_menu_item (window, accel_group, model, i);
+               add_accelerators_from_menu_item (model, i);
 
                iter = g_menu_model_iterate_item_links (model, i);
                while (g_menu_link_iter_get_next (iter, &key, &m)) {
-                       add_accelerators_from_menu (window, accel_group, m);
+                       _gtk_add_accelerators_from_menu (m);
                        g_object_unref (m);
                }
                g_object_unref (iter);
@@ -859,18 +792,6 @@ add_accelerators_from_menu (GtkWindow      *window,
 }
 
 
-void
-_gtk_window_add_accelerators_from_menu (GtkWindow  *window,
-                                       GMenuModel *menu)
-{
-       GtkAccelGroup *accel_group;
-
-       accel_group = gtk_accel_group_new ();
-       add_accelerators_from_menu (window, accel_group, menu);
-       gtk_window_add_accel_group (window, accel_group);
-}
-
-
 gboolean
 _gtk_settings_get_dialogs_use_header (void)
 {
diff --git a/src/gtk-utils.h b/src/gtk-utils.h
index 8dfa4cca..69c7df75 100644
--- a/src/gtk-utils.h
+++ b/src/gtk-utils.h
@@ -105,15 +105,11 @@ GtkWidget *   _gtk_header_bar_create_image_toggle_button
                                                   (const char       *icon_name,
                                                    const char       *tooltip,
                                                    const char       *action_name);
-void          _gtk_window_add_accelerator_for_action
-                                                  (GtkWindow        *window,
-                                                   GtkAccelGroup    *accel_group,
-                                                   const char       *action_name,
+void          _gtk_add_accelerator_for_action
+                                                  (const char       *action_name,
                                                    const char       *accel,
                                                    GVariant         *target);
-void          _gtk_window_add_accelerators_from_menu
-                                                  (GtkWindow        *window,
-                                                   GMenuModel       *menu);
+void          _gtk_add_accelerators_from_menu (GMenuModel       *menu);
 gboolean      _gtk_settings_get_dialogs_use_header (void);
 void          _gtk_application_add_accelerator_for_action
                                                   (GtkApplication   *app,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]