[gnome-builder] devhelp: Add close search bar and find next/prev keyboard shortcuts



commit 09d97a379a55328456dddd973b344bde8270d6b3
Author: vanadiae <vanadiae35 gmail com>
Date:   Mon Aug 2 15:57:24 2021 +0200

    devhelp: Add close search bar and find next/prev keyboard shortcuts
    
    I had to clean up things a bit to make it work.

 src/plugins/devhelp/gbp-devhelp-page.c    |   4 +-
 src/plugins/devhelp/gbp-devhelp-search.c  | 128 +++++++++++++++++++-----
 src/plugins/devhelp/gbp-devhelp-search.ui | 160 +++++++++++++++---------------
 3 files changed, 186 insertions(+), 106 deletions(-)
---
diff --git a/src/plugins/devhelp/gbp-devhelp-page.c b/src/plugins/devhelp/gbp-devhelp-page.c
index 1d19c6dc8..ae32fbaba 100644
--- a/src/plugins/devhelp/gbp-devhelp-page.c
+++ b/src/plugins/devhelp/gbp-devhelp-page.c
@@ -358,8 +358,8 @@ gbp_devhelp_page_init (GbpDevhelpPage *self)
 
   setup_webview (self->web_view);
 
-  gtk_overlay_add_overlay (self->devhelp_overlay,
-                           GTK_WIDGET (self->search_revealer));
+  gtk_widget_show (GTK_WIDGET (self->search));
+  gtk_overlay_add_overlay (self->devhelp_overlay, GTK_WIDGET (self->search));
 
   gbp_devhelp_search_set_devhelp (self->search,
                                   self->web_controller,
diff --git a/src/plugins/devhelp/gbp-devhelp-search.c b/src/plugins/devhelp/gbp-devhelp-search.c
index 4391122a6..fd884ba62 100644
--- a/src/plugins/devhelp/gbp-devhelp-search.c
+++ b/src/plugins/devhelp/gbp-devhelp-search.c
@@ -18,10 +18,11 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#define I_ g_intern_string
+
 #define G_LOG_DOMAIN "gbp-devhelp-search"
 #define MAX_SEARCH 100
 
-#include <fcntl.h>
 #include <glib/gi18n.h>
 #include <libide-editor.h>
 #include <webkit2/webkit2.h>
@@ -31,28 +32,6 @@
 
 G_DEFINE_TYPE (GbpDevhelpSearch, gbp_devhelp_search, GTK_TYPE_BIN)
 
-static void
-close_clicked_cb (GtkButton        *button,
-                  GbpDevhelpSearch *self)
-{
-  g_assert (GBP_IS_DEVHELP_SEARCH (self));
-
-  webkit_find_controller_search_finish (self->web_controller);
-  gtk_revealer_set_reveal_child (self->search_revealer, FALSE);
-}
-
-static void
-search_button_clicked_cb (GtkButton        *button,
-                          GbpDevhelpSearch *self)
-{
-  g_assert (GBP_IS_DEVHELP_SEARCH (self));
-
-  if (button == self->search_prev_button)
-    webkit_find_controller_search_previous (self->web_controller);
-  else
-    webkit_find_controller_search_next (self->web_controller);
-}
-
 static void
 search_text_changed_cb (IdeTaggedEntry   *search_entry,
                         GbpDevhelpSearch *self)
@@ -90,11 +69,77 @@ search_revealer_cb (GtkRevealer      *search_revealer,
     gtk_revealer_set_reveal_child (self->search_revealer, FALSE);
 }
 
+static void
+gbp_devhelp_search_actions_close (GSimpleAction *action,
+                                  GVariant      *param,
+                                  gpointer       user_data)
+{
+  GbpDevhelpSearch *self = (GbpDevhelpSearch *)user_data;
+
+  g_assert (GBP_IS_DEVHELP_SEARCH (self));
+
+  webkit_find_controller_search_finish (self->web_controller);
+  gtk_revealer_set_reveal_child (self->search_revealer, FALSE);
+}
+
+static void
+gbp_devhelp_search_actions_find_next (GSimpleAction *action,
+                                      GVariant      *param,
+                                      gpointer       user_data)
+{
+  GbpDevhelpSearch *self = (GbpDevhelpSearch *)user_data;
+
+  g_assert (GBP_IS_DEVHELP_SEARCH (self));
+
+  if (gtk_revealer_get_reveal_child (self->search_revealer) &&
+      webkit_find_controller_get_search_text (self->web_controller))
+    webkit_find_controller_search_next (self->web_controller);
+}
+
+static void
+gbp_devhelp_search_actions_find_previous (GSimpleAction *action,
+                                          GVariant      *param,
+                                          gpointer       user_data)
+{
+  GbpDevhelpSearch *self = (GbpDevhelpSearch *)user_data;
+
+  g_assert (GBP_IS_DEVHELP_SEARCH (self));
+
+  if (gtk_revealer_get_reveal_child (self->search_revealer) &&
+      webkit_find_controller_get_search_text (self->web_controller))
+    webkit_find_controller_search_previous (self->web_controller);
+}
+
+static void
+gbp_devhelp_search_grab_focus (GtkWidget *widget)
+{
+  GbpDevhelpSearch *self = (GbpDevhelpSearch *)widget;
+
+  g_assert (GBP_IS_DEVHELP_SEARCH (self));
+
+  gtk_widget_grab_focus (GTK_WIDGET (self->search_entry));
+}
+
+static void
+gbp_devhelp_search_finalize (GObject *object)
+{
+  GbpDevhelpSearch *self = (GbpDevhelpSearch *)object;
+
+  g_assert (GBP_IS_DEVHELP_SEARCH (self));
+
+  gtk_widget_insert_action_group (GTK_WIDGET (self), "devhelp-search", NULL);
+}
+
 static void
 gbp_devhelp_search_class_init (GbpDevhelpSearchClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
+  object_class->finalize = gbp_devhelp_search_finalize;
+
+  widget_class->grab_focus = gbp_devhelp_search_grab_focus;
+
   gtk_widget_class_set_template_from_resource (widget_class, "/plugins/devhelp/gbp-devhelp-search.ui");
 
   gtk_widget_class_bind_template_child (widget_class, GbpDevhelpSearch, search_prev_button);
@@ -104,17 +149,48 @@ gbp_devhelp_search_class_init (GbpDevhelpSearchClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GbpDevhelpSearch, search_revealer);
 }
 
+static const GActionEntry actions[] = {
+  { "close", gbp_devhelp_search_actions_close },
+  { "find-next", gbp_devhelp_search_actions_find_next },
+  { "find-previous", gbp_devhelp_search_actions_find_previous },
+};
+
 static void
 gbp_devhelp_search_init (GbpDevhelpSearch *self)
 {
+  g_autoptr(GSimpleActionGroup) group = NULL;
+  DzlShortcutController *controller;
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
-  g_signal_connect (self->search_prev_button, "clicked", G_CALLBACK (search_button_clicked_cb), self);
-  g_signal_connect (self->search_next_button, "clicked", G_CALLBACK (search_button_clicked_cb), self);
-  g_signal_connect (self->close_button, "clicked", G_CALLBACK (close_clicked_cb), self);
   g_signal_connect (self->search_entry, "search-changed", G_CALLBACK (search_text_changed_cb), self);
   g_signal_connect (self->search_revealer, "notify::child-revealed", G_CALLBACK (search_revealer_cb), self);
+
+  group = g_simple_action_group_new ();
+  g_action_map_add_action_entries (G_ACTION_MAP (group),
+                                   actions,
+                                   G_N_ELEMENTS (actions),
+                                   self);
+  gtk_widget_insert_action_group (GTK_WIDGET (self),
+                                  "devhelp-search",
+                                  G_ACTION_GROUP (group));
+
+  controller = dzl_shortcut_controller_find (GTK_WIDGET (self));
+  dzl_shortcut_controller_add_command_action (controller,
+                                              I_("org.gnome.builder.devhelp-search.close"),
+                                              "Escape",
+                                              DZL_SHORTCUT_PHASE_CAPTURE | DZL_SHORTCUT_PHASE_GLOBAL,
+                                              I_("devhelp-search.close"));
+  dzl_shortcut_controller_add_command_action (controller,
+                                              I_("org.gnome.builder.devhelp-search.find-next"),
+                                              "<Primary>g",
+                                              DZL_SHORTCUT_PHASE_CAPTURE | DZL_SHORTCUT_PHASE_GLOBAL,
+                                              I_("devhelp-search.find-next"));
+  dzl_shortcut_controller_add_command_action (controller,
+                                              I_("org.gnome.builder.devhelp-search.find-previous"),
+                                              "<Primary><shift>g",
+                                              DZL_SHORTCUT_PHASE_CAPTURE | DZL_SHORTCUT_PHASE_GLOBAL,
+                                              I_("devhelp-search.find-previous"));
 }
 
 void
diff --git a/src/plugins/devhelp/gbp-devhelp-search.ui b/src/plugins/devhelp/gbp-devhelp-search.ui
index 5f690fd7a..bf5112925 100644
--- a/src/plugins/devhelp/gbp-devhelp-search.ui
+++ b/src/plugins/devhelp/gbp-devhelp-search.ui
@@ -1,116 +1,120 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <object class="GtkRevealer" id="search_revealer">
+  <template class="GbpDevhelpSearch" parent="GtkBin">
     <property name="halign">end</property>
     <property name="valign">start</property>
-    <property name="visible">true</property>
     <child>
-      <object class="GtkFrame">
+      <object class="GtkRevealer" id="search_revealer">
         <property name="visible">true</property>
-        <property name="margin-end">12</property>
-        <style>
-          <class name="search-frame"/>
-        </style>
         <child>
-          <object class="GtkGrid">
+          <object class="GtkFrame">
             <property name="visible">true</property>
-            <property name="can_focus">false</property>
-            <property name="row_spacing">8</property>
-            <property name="column_spacing">8</property>
+            <property name="margin-end">12</property>
+            <style>
+              <class name="search-frame"/>
+            </style>
             <child>
-              <object class="IdeTaggedEntry" id="search_entry">
+              <object class="GtkGrid">
                 <property name="visible">true</property>
-                <property name="can_focus">true</property>
-                <property name="width-chars">20</property>
-                <property name="max-width-chars">30</property>
-                <property name="primary_icon_name">edit-find-symbolic</property>
-                <property name="primary_icon_activatable">false</property>
-                <property name="primary_icon_sensitive">false</property>
-              </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkBox">
-                <property name="homogeneous">true</property>
-                <property name="visible">true</property>
-                <property name="can_focus">false</property>
-                <property name="valign">center</property>
-                <style>
-                  <class name="linked"/>
-                </style>
+                <property name="row_spacing">8</property>
+                <property name="column_spacing">8</property>
+                <child>
+                  <object class="IdeTaggedEntry" id="search_entry">
+                    <property name="visible">true</property>
+                    <property name="width-chars">20</property>
+                    <property name="max-width-chars">30</property>
+                    <property name="primary_icon_name">edit-find-symbolic</property>
+                    <property name="primary_icon_activatable">false</property>
+                    <property name="primary_icon_sensitive">false</property>
+                    <property name="tooltip-text" translatable="yes">Reveal search bar with Ctrl+F</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
                 <child>
-                  <object class="GtkButton" id="search_prev_button">
+                  <object class="GtkBox">
+                    <property name="homogeneous">true</property>
                     <property name="visible">true</property>
-                    <property name="can_focus">false</property>
-                    <property name="receives_default">true</property>
+                    <property name="valign">center</property>
+                    <style>
+                      <class name="linked"/>
+                    </style>
                     <child>
-                      <object class="GtkImage">
+                      <object class="GtkButton" id="search_prev_button">
                         <property name="visible">true</property>
-                        <property name="can_focus">false</property>
-                        <property name="icon_name">go-up-symbolic</property>
-                        <property name="icon_size">1</property>
+                        <property name="receives_default">true</property>
+                        <property name="tooltip-text" translatable="yes">Previous search result 
(Ctrl+Shift+G)</property>
+                        <property name="action-name">devhelp-search.find-previous</property>
+                        <child>
+                          <object class="GtkImage">
+                            <property name="visible">true</property>
+                            <property name="icon_name">go-up-symbolic</property>
+                            <property name="icon_size">1</property>
+                          </object>
+                        </child>
                       </object>
+                      <packing>
+                        <property name="expand">false</property>
+                        <property name="fill">true</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkButton" id="search_next_button">
+                        <property name="visible">true</property>
+                        <property name="receives_default">true</property>
+                        <property name="tooltip-text" translatable="yes">Next search result 
(Ctrl+G)</property>
+                        <property name="action-name">devhelp-search.find-next</property>
+                        <child>
+                          <object class="GtkImage">
+                            <property name="visible">true</property>
+                            <property name="icon_name">go-down-symbolic</property>
+                            <property name="icon_size">1</property>
+                          </object>
+                        </child>
+                      </object>
+                      <packing>
+                        <property name="expand">false</property>
+                        <property name="fill">true</property>
+                        <property name="position">1</property>
+                      </packing>
                     </child>
                   </object>
                   <packing>
-                    <property name="expand">false</property>
-                    <property name="fill">true</property>
-                    <property name="position">0</property>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">0</property>
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkButton" id="search_next_button">
+                  <object class="GtkButton" id="close_button">
                     <property name="visible">true</property>
-                    <property name="can_focus">false</property>
-                    <property name="receives_default">true</property>
+                    <property name="halign">center</property>
+                    <property name="valign">center</property>
+                    <property name="focus_on_click">false</property>
+                    <property name="tooltip-text" translatable="yes">Close search bar (Escape key)</property>
+                    <property name="action-name">devhelp-search.close</property>
+                    <style>
+                      <class name="close"/>
+                    </style>
                     <child>
                       <object class="GtkImage">
                         <property name="visible">true</property>
-                        <property name="can_focus">false</property>
-                        <property name="icon_name">go-down-symbolic</property>
-                        <property name="icon_size">1</property>
+                        <property name="icon_name">window-close-symbolic</property>
                       </object>
                     </child>
                   </object>
                   <packing>
-                    <property name="expand">false</property>
-                    <property name="fill">true</property>
-                    <property name="position">1</property>
+                    <property name="left_attach">3</property>
+                    <property name="top_attach">0</property>
                   </packing>
                 </child>
               </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkButton" id="close_button">
-                <property name="visible">true</property>
-                <property name="halign">center</property>
-                <property name="valign">center</property>
-                <property name="focus_on_click">false</property>
-                <style>
-                  <class name="close"/>
-                </style>
-                <child>
-                  <object class="GtkImage">
-                    <property name="visible">true</property>
-                    <property name="icon_name">window-close-symbolic</property>
-                  </object>
-                </child>
-              </object>
-              <packing>
-                <property name="left_attach">3</property>
-                <property name="top_attach">0</property>
-              </packing>
             </child>
           </object>
         </child>
       </object>
     </child>
-  </object>
+  </template>
 </interface>


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