[devhelp] Window: replace use of GtkAccelGroup + closure by a GAction with param



commit 8976d8f3c3a976d0d09d418ebd1ceca618ff8c28
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Feb 1 22:13:42 2018 +0100

    Window: replace use of GtkAccelGroup + closure by a GAction with param
    
    To have more modern code, easier to understand.

 src/dh-app.c    |   30 ++++++++++++++++++++++++++++
 src/dh-window.c |   59 +++++++++++++-----------------------------------------
 2 files changed, 44 insertions(+), 45 deletions(-)
---
diff --git a/src/dh-app.c b/src/dh-app.c
index 8b2583c..aa9929b 100644
--- a/src/dh-app.c
+++ b/src/dh-app.c
@@ -240,10 +240,40 @@ add_action_entries (DhApp *app)
 }
 
 static void
+setup_go_to_tab_accelerators (GtkApplication *app)
+{
+        const gchar *accels[] = {NULL, NULL};
+        gint key_num;
+
+        for (key_num = 1; key_num <= 9; key_num++) {
+                gchar *accel;
+                gchar *detailed_action_name;
+
+                accel = g_strdup_printf ("<Alt>%d", key_num);
+                accels[0] = accel;
+
+                detailed_action_name = g_strdup_printf ("win.go-to-tab(uint16 %d)", key_num - 1);
+
+                gtk_application_set_accels_for_action (app, detailed_action_name, accels);
+
+                g_free (accel);
+                g_free (detailed_action_name);
+        }
+
+        /* On a typical keyboard the 0 is after 9, so it's the equivalent of 10
+         * (9 starting from 0).
+         */
+        accels[0] = "<Alt>0";
+        gtk_application_set_accels_for_action (app, "win.go-to-tab(uint16 9)", accels);
+}
+
+static void
 setup_accelerators (GtkApplication *app)
 {
         const gchar *accels[] = {NULL, NULL, NULL, NULL};
 
+        setup_go_to_tab_accelerators (app);
+
         accels[0] = "<Control>0";
         gtk_application_set_accels_for_action (app, "win.zoom-default", accels);
 
diff --git a/src/dh-window.c b/src/dh-window.c
index 0279d93..a12156d 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -49,11 +49,6 @@ typedef struct {
         DhLink *selected_search_link;
 } DhWindowPrivate;
 
-static guint tab_accel_keys[] = {
-        GDK_KEY_1, GDK_KEY_2, GDK_KEY_3, GDK_KEY_4, GDK_KEY_5,
-        GDK_KEY_6, GDK_KEY_7, GDK_KEY_8, GDK_KEY_9, GDK_KEY_0
-};
-
 static const
 struct
 {
@@ -235,6 +230,19 @@ prev_tab_cb (GSimpleAction *action,
 }
 
 static void
+go_to_tab_cb (GSimpleAction *action,
+              GVariant      *parameter,
+              gpointer       user_data)
+{
+        DhWindow *window = DH_WINDOW (user_data);
+        DhWindowPrivate *priv = dh_window_get_instance_private (window);
+        guint16 tab_num;
+
+        tab_num = g_variant_get_uint16 (parameter);
+        gtk_notebook_set_current_page (priv->notebook, tab_num);
+}
+
+static void
 print_cb (GSimpleAction *action,
           GVariant      *parameter,
           gpointer       user_data)
@@ -498,6 +506,7 @@ add_action_entries (DhWindow *window)
                 { "new-tab", new_tab_cb },
                 { "next-tab", next_tab_cb },
                 { "prev-tab", prev_tab_cb },
+                { "go-to-tab", go_to_tab_cb, "q" },
                 { "print", print_cb },
                 { "close", close_cb },
 
@@ -557,37 +566,11 @@ settings_fonts_changed_cb (DhSettings  *settings,
 }
 
 static void
-window_web_view_tab_accel_cb (GtkAccelGroup   *accel_group,
-                              GObject         *object,
-                              guint            key,
-                              GdkModifierType  mod,
-                              DhWindow        *window)
-{
-        DhWindowPrivate *priv = dh_window_get_instance_private (window);
-        gint page_num;
-        guint i;
-
-        page_num = -1;
-        for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
-                if (tab_accel_keys[i] == key) {
-                        page_num = i;
-                        break;
-                }
-        }
-
-        if (page_num != -1)
-                gtk_notebook_set_current_page (priv->notebook, page_num);
-}
-
-static void
 dh_window_init (DhWindow *window)
 {
         DhWindowPrivate *priv = dh_window_get_instance_private (window);
         GtkApplication *app;
         DhSettings *settings;
-        GtkAccelGroup *accel_group;
-        GClosure *closure;
-        guint i;
 
         gtk_widget_init_template (GTK_WIDGET (window));
 
@@ -605,20 +588,6 @@ dh_window_init (DhWindow *window)
                                  G_CALLBACK (settings_fonts_changed_cb),
                                  window,
                                  0);
-
-        accel_group = gtk_accel_group_new ();
-        gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
-        for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
-                closure = g_cclosure_new (G_CALLBACK (window_web_view_tab_accel_cb),
-                                          window,
-                                          NULL);
-
-                gtk_accel_group_connect (accel_group,
-                                         tab_accel_keys[i],
-                                         GDK_MOD1_MASK,
-                                         0,
-                                         closure);
-        }
 }
 
 static void


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