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




commit 015adbd351e8c6e930bef0ed361aed5f800c140c
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, after GTK4, we can just ust that method and drop our bubble phase
    event controller along with the nautilus_window_slot_handle_event()
    and nautilus_query_editor_handle_event() functions.

 src/nautilus-query-editor.c | 43 +++++++++++++++++++++++++++++++++++++++----
 src/nautilus-query-editor.h |  6 ++++--
 src/nautilus-window-slot.c  | 28 +++++++++++-----------------
 src/nautilus-window-slot.h  |  6 ++++--
 src/nautilus-window.c       |  6 +++---
 5 files changed, 61 insertions(+), 28 deletions(-)
---
diff --git a/src/nautilus-query-editor.c b/src/nautilus-query-editor.c
index 1eb074927..04ebf2378 100644
--- a/src/nautilus-query-editor.c
+++ b/src/nautilus-query-editor.c
@@ -744,12 +744,47 @@ 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 GDK_EVENT_PROPAGATE;
+    }
+
+    /* 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. After porting to GTK4, use
+     * gtk_search_entry_set_key_capture_widget() and remove all of this. */
+    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 8d6a4cde3..2a1c79e40 100644
--- a/src/nautilus-window-slot.c
+++ b/src/nautilus-window-slot.c
@@ -607,34 +607,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);
         }
@@ -643,7 +634,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 d1f1d5679..4a127fbe7 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -2452,15 +2452,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;
     }
@@ -2764,6 +2762,8 @@ nautilus_window_init (NautilusWindow *window)
                       "key-pressed", G_CALLBACK (nautilus_window_key_capture),
                       NULL);
 
+    /* After porting to GTK4, use gtk_search_entry_set_key_capture_widget() and
+     * remove this bubble phase controller. */
     window->key_bubble_controller = gtk_event_controller_key_new (GTK_WIDGET (window));
     gtk_event_controller_set_propagation_phase (window->key_bubble_controller,
                                                 GTK_PHASE_BUBBLE);


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