[gnome-builder] devhelp: Fix Ctrl+F shortcut by using an action shortcut



commit feca93463e63e0cb9eb397e0b5fe4b646879a6a5
Author: vanadiae <vanadiae35 gmail com>
Date:   Sun Aug 1 20:48:00 2021 +0200

    devhelp: Fix Ctrl+F shortcut by using an action shortcut
    
    We can't use gtk_binding_entry_add_signal() because it requires us
    to have the end focus (like an entry or button has) with can-focus=True,
    which we can't have because we're just a container and not a "primitive"
    widget. I'm not sure if that shortcut ever worked, and how though.
    
    So instead use an action shortcut, and get back the search bar ;)

 src/plugins/devhelp/gbp-devhelp-page.c | 48 ++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 20 deletions(-)
---
diff --git a/src/plugins/devhelp/gbp-devhelp-page.c b/src/plugins/devhelp/gbp-devhelp-page.c
index 8d74b6a1f..20f035118 100644
--- a/src/plugins/devhelp/gbp-devhelp-page.c
+++ b/src/plugins/devhelp/gbp-devhelp-page.c
@@ -18,6 +18,8 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#define I_ g_intern_string
+
 #include <devhelp/devhelp.h>
 #include <glib/gi18n.h>
 #include <webkit2/webkit2.h>
@@ -44,15 +46,9 @@ enum {
   LAST_PROP
 };
 
-enum {
-  SEARCH_REVEAL,
-  LAST_SIGNAL
-};
-
 G_DEFINE_TYPE (GbpDevhelpPage, gbp_devhelp_page, IDE_TYPE_PAGE)
 
 static GParamSpec *properties [LAST_PROP];
-static guint signals [LAST_SIGNAL];
 
 void
 gbp_devhelp_page_set_uri (GbpDevhelpPage *self,
@@ -169,12 +165,17 @@ gbp_devhelp_page_get_property (GObject    *object,
 }
 
 static void
-gbp_devhelp_search_reveal (GbpDevhelpPage *self)
+gbp_devhelp_page_actions_reveal_search (GSimpleAction *action,
+                                        GVariant      *param,
+                                        gpointer       user_data)
 {
+  GbpDevhelpPage *self = (GbpDevhelpPage *)user_data;
+
   g_assert (GBP_IS_DEVHELP_PAGE (self));
 
   webkit_web_view_can_execute_editing_command (self->web_view, WEBKIT_EDITING_COMMAND_COPY, NULL, NULL, 
NULL);
   gtk_revealer_set_reveal_child (self->search_revealer, TRUE);
+  gtk_widget_grab_focus (GTK_WIDGET (self->search));
 }
 
 static void
@@ -187,6 +188,16 @@ gbp_devhelp_focus_in_event (GbpDevhelpPage *self,
   gtk_revealer_set_reveal_child (self->search_revealer, FALSE);
 }
 
+static void
+gbp_devhelp_page_finalize (GObject *object)
+{
+  GbpDevhelpPage *self = (GbpDevhelpPage *)object;
+
+  g_assert (GBP_IS_DEVHELP_PAGE (self));
+
+  gtk_widget_insert_action_group (GTK_WIDGET (self), "devhelp-view", NULL);
+}
+
 static void
 gbp_devhelp_page_class_init (GbpDevhelpPageClass *klass)
 {
@@ -196,6 +207,7 @@ gbp_devhelp_page_class_init (GbpDevhelpPageClass *klass)
 
   object_class->set_property = gbp_devhelp_page_set_property;
   object_class->get_property = gbp_devhelp_page_get_property;
+  object_class->finalize = gbp_devhelp_page_finalize;
 
   view_class->create_split = gbp_devhelp_page_create_split;
 
@@ -206,19 +218,6 @@ gbp_devhelp_page_class_init (GbpDevhelpPageClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY));
 
-  signals [SEARCH_REVEAL] =
-    g_signal_new_class_handler ("search-reveal",
-                                G_TYPE_FROM_CLASS (klass),
-                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
-                                G_CALLBACK (gbp_devhelp_search_reveal),
-                                NULL, NULL, NULL,
-                                G_TYPE_NONE, 0);
-
-  gtk_binding_entry_add_signal (gtk_binding_set_by_class (klass),
-                                GDK_KEY_f,
-                                GDK_CONTROL_MASK,
-                                "search-reveal", 0);
-
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 
   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/devhelp/gbp-devhelp-page.ui");
@@ -310,12 +309,14 @@ setup_webview (WebKitWebView *web_view)
 
 static const GActionEntry actions[] = {
   { "print", gbp_devhelp_page_actions_print },
+  { "reveal-search", gbp_devhelp_page_actions_reveal_search },
 };
 
 static void
 gbp_devhelp_page_init (GbpDevhelpPage *self)
 {
   g_autoptr(GSimpleActionGroup) group = NULL;
+  DzlShortcutController *controller;
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
@@ -357,4 +358,11 @@ gbp_devhelp_page_init (GbpDevhelpPage *self)
   gtk_widget_insert_action_group (GTK_WIDGET (self),
                                   "devhelp-view",
                                   G_ACTION_GROUP (group));
+
+  controller = dzl_shortcut_controller_find (GTK_WIDGET (self));
+  dzl_shortcut_controller_add_command_action (controller,
+                                              I_("org.gnome.builder.devhelp-view.reveal-search"),
+                                              "<Primary>f",
+                                              DZL_SHORTCUT_PHASE_CAPTURE | DZL_SHORTCUT_PHASE_GLOBAL,
+                                              I_("devhelp-view.reveal-search"));
 }


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