[nautilus/wip/antoniof/gtk4-preparation-step-event-controllers: 26/29] query-editor: Drop gtk_search_entry_handle_event()




commit e0d2b6659beb5f25ec3855158acfab89f55308f4
Author: António Fernandes <antoniof gnome org>
Date:   Sun Aug 8 16:18:01 2021 +0100

    query-editor: Drop gtk_search_entry_handle_event()
    
    It's going away in GTK4. Also, as we use a key event controller now,
    we can use gtk_event_controller_key_forward() instead.
    
    This is how GTK4 implements gtk_search_entry_set_key_capture_widget(),
    so reimplement it here. We were not going to inherit anyway, because we
    need a custom widget for a tagged entry in GTK4.

 src/nautilus-query-editor.c | 42 ++++++++++++++++++++++++++++++++++++++----
 src/nautilus-query-editor.h |  6 ++++--
 src/nautilus-window-slot.c  | 28 +++++++++++-----------------
 src/nautilus-window-slot.h  |  6 ++++--
 src/nautilus-window.c       |  4 +---
 5 files changed, 58 insertions(+), 28 deletions(-)
---
diff --git a/src/nautilus-query-editor.c b/src/nautilus-query-editor.c
index 05d2565bd..ff9934b22 100644
--- a/src/nautilus-query-editor.c
+++ b/src/nautilus-query-editor.c
@@ -745,12 +745,46 @@ nautilus_query_editor_set_text (NautilusQueryEditor *self,
     gtk_entry_set_text (GTK_ENTRY (self->entry), text);
 }
 
+static gboolean
+nautilus_gtk_search_entry_is_keynav_event (guint           keyval,
+                                           GdkModifierType state)
+{
+    if (keyval == GDK_KEY_Tab || keyval == GDK_KEY_KP_Tab ||
+        keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up ||
+        keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down ||
+        keyval == GDK_KEY_Left || keyval == GDK_KEY_KP_Left ||
+        keyval == GDK_KEY_Right || keyval == GDK_KEY_KP_Right ||
+        keyval == GDK_KEY_Home || keyval == GDK_KEY_KP_Home ||
+        keyval == GDK_KEY_End || keyval == GDK_KEY_KP_End ||
+        keyval == GDK_KEY_Page_Up || keyval == GDK_KEY_KP_Page_Up ||
+        keyval == GDK_KEY_Page_Down || keyval == GDK_KEY_KP_Page_Down ||
+        ((state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) != 0))
+    {
+        return TRUE;
+    }
+
+    /* Other navigation events should get automatically
+     * ignored as they will not change the content of the entry
+     */
+    return FALSE;
+}
+
 gboolean
-nautilus_query_editor_handle_event (NautilusQueryEditor *self,
-                                    GdkEvent            *event)
+nautilus_query_editor_handle_event (NautilusQueryEditor   *self,
+                                    GtkEventControllerKey *controller,
+                                    guint                  keyval,
+                                    GdkModifierType        state)
 {
     g_return_val_if_fail (NAUTILUS_IS_QUERY_EDITOR (self), GDK_EVENT_PROPAGATE);
-    g_return_val_if_fail (event != NULL, GDK_EVENT_PROPAGATE);
+    g_return_val_if_fail (controller != NULL, GDK_EVENT_PROPAGATE);
+
+    /* Conditions are copied straight from GTK. */
+    if (nautilus_gtk_search_entry_is_keynav_event (keyval, state) ||
+        keyval == GDK_KEY_space ||
+        keyval == GDK_KEY_Menu)
+    {
+        return GDK_EVENT_PROPAGATE;
+    }
 
-    return gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (self->entry), event);
+    return gtk_event_controller_key_forward (controller, GTK_WIDGET (self->entry));
 }
diff --git a/src/nautilus-query-editor.h b/src/nautilus-query-editor.h
index c09de18a0..ec9db128e 100644
--- a/src/nautilus-query-editor.h
+++ b/src/nautilus-query-editor.h
@@ -73,5 +73,7 @@ void           nautilus_query_editor_set_text     (NautilusQueryEditor *editor,
                                                    const gchar         *text);
 
 gboolean
-nautilus_query_editor_handle_event (NautilusQueryEditor *self,
-                                    GdkEvent            *event);
+nautilus_query_editor_handle_event (NautilusQueryEditor   *self,
+                                    GtkEventControllerKey *controller,
+                                    guint                  keyval,
+                                    GdkModifierType        state);
diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c
index 5d783c0f1..2fa7c9557 100644
--- a/src/nautilus-window-slot.c
+++ b/src/nautilus-window-slot.c
@@ -608,34 +608,25 @@ nautilus_window_slot_search (NautilusWindowSlot *self,
 }
 
 gboolean
-nautilus_window_slot_handle_event (NautilusWindowSlot *self,
-                                   GdkEvent           *event)
+nautilus_window_slot_handle_event (NautilusWindowSlot    *self,
+                                   GtkEventControllerKey *controller,
+                                   guint                  keyval,
+                                   GdkModifierType        state)
 {
     gboolean retval;
     GAction *action;
-    guint keyval;
 
     retval = FALSE;
     action = g_action_map_lookup_action (G_ACTION_MAP (self->slot_action_group),
                                          "search-visible");
 
-    if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
-    {
-        return GDK_EVENT_PROPAGATE;
-    }
-
-    if (G_UNLIKELY (!gdk_event_get_keyval (event, &keyval)))
-    {
-        g_return_val_if_reached (GDK_EVENT_PROPAGATE);
-    }
-
     if (keyval == GDK_KEY_Escape)
     {
-        g_autoptr (GVariant) state = NULL;
+        g_autoptr (GVariant) action_state = NULL;
 
-        state = g_action_get_state (action);
+        action_state = g_action_get_state (action);
 
-        if (g_variant_get_boolean (state))
+        if (g_variant_get_boolean (action_state))
         {
             nautilus_window_slot_set_search_visible (self, FALSE);
         }
@@ -644,7 +635,10 @@ nautilus_window_slot_handle_event (NautilusWindowSlot *self,
     /* If the action is not enabled, don't try to handle search */
     if (g_action_get_enabled (action))
     {
-        retval = nautilus_query_editor_handle_event (self->query_editor, event);
+        retval = nautilus_query_editor_handle_event (self->query_editor,
+                                                     controller,
+                                                     keyval,
+                                                     state);
     }
 
     if (retval)
diff --git a/src/nautilus-window-slot.h b/src/nautilus-window-slot.h
index 268578789..0a6f1a1d0 100644
--- a/src/nautilus-window-slot.h
+++ b/src/nautilus-window-slot.h
@@ -73,8 +73,10 @@ void     nautilus_window_slot_stop_loading                 (NautilusWindowSlot *
 const gchar *nautilus_window_slot_get_title                (NautilusWindowSlot *slot);
 void         nautilus_window_slot_update_title            (NautilusWindowSlot *slot);
 
-gboolean nautilus_window_slot_handle_event                (NautilusWindowSlot *slot,
-                                                           GdkEvent           *event);
+gboolean nautilus_window_slot_handle_event                (NautilusWindowSlot    *slot,
+                                                           GtkEventControllerKey *controller,
+                                                           guint                  keyval,
+                                                           GdkModifierType        state);
 
 void    nautilus_window_slot_queue_reload                 (NautilusWindowSlot *slot);
 
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index ee2ef8602..f052de77b 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -2399,15 +2399,13 @@ nautilus_window_key_bubble (GtkEventControllerKey *controller,
                             GdkModifierType        state,
                             gpointer               user_data)
 {
-    g_autoptr (GdkEvent) event = NULL;
     GtkWidget *widget;
     NautilusWindow *window;
 
-    event = gtk_get_current_event ();
     widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (controller));
     window = NAUTILUS_WINDOW (widget);
     if (window->active_slot != NULL &&
-        nautilus_window_slot_handle_event (window->active_slot, (GdkEvent *) event))
+        nautilus_window_slot_handle_event (window->active_slot, controller, keyval, state))
     {
         return GDK_EVENT_STOP;
     }


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