[nautilus/wip/antoniof/gtk4-preparation-step-event-controllers: 12/22] location-entry: Use event controller for key events




commit 6e994cc60166a2a9f0c699508152df6ff3318ad0
Author: António Fernandes <antoniof gnome org>
Date:   Sat Aug 7 13:09:31 2021 +0100

    location-entry: Use event controller for key events
    
    Preparing for GTK4, where the ::event signal will be gone.

 src/nautilus-location-entry.c | 65 ++++++++++++++-----------------------------
 1 file changed, 21 insertions(+), 44 deletions(-)
---
diff --git a/src/nautilus-location-entry.c b/src/nautilus-location-entry.c
index 3d1bbe997..cce5c3d3e 100644
--- a/src/nautilus-location-entry.c
+++ b/src/nautilus-location-entry.c
@@ -75,6 +75,8 @@ typedef struct _NautilusLocationEntryPrivate
     gchar *special_text;
     NautilusLocationEntryAction secondary_action;
 
+    GtkEventController *controller;
+
     GtkEntryCompletion *completion;
     GtkListStore *completions_store;
     GtkCellRenderer *completion_cell;
@@ -527,6 +529,8 @@ finalize (GObject *object)
     g_clear_object (&priv->completion);
     g_clear_object (&priv->completions_store);
 
+    g_clear_object (&priv->controller);
+
     G_OBJECT_CLASS (nautilus_location_entry_parent_class)->finalize (object);
 }
 
@@ -626,27 +630,18 @@ nautilus_location_entry_icon_release (GtkEntry             *gentry,
 }
 
 static gboolean
-nautilus_location_entry_on_event (GtkWidget *widget,
-                                  GdkEvent  *event)
+nautilus_location_entry_key_pressed (GtkEventControllerKey *controller,
+                                     unsigned int           keyval,
+                                     unsigned int           keycode,
+                                     GdkModifierType        state,
+                                     gpointer               user_data)
 {
-    GtkWidgetClass *parent_widget_class;
-    NautilusLocationEntry *entry;
-    NautilusLocationEntryPrivate *priv;
+    GtkWidget *widget;
     GtkEditable *editable;
     gboolean selected;
-    guint keyval;
-    GdkModifierType state;
-    gboolean handled;
 
-    parent_widget_class = GTK_WIDGET_CLASS (nautilus_location_entry_parent_class);
 
-    if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
-    {
-        return parent_widget_class->event (widget, event);
-    }
-
-    entry = NAUTILUS_LOCATION_ENTRY (widget);
-    priv = nautilus_location_entry_get_instance_private (entry);
+    widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (controller));
     editable = GTK_EDITABLE (widget);
     selected = gtk_editable_get_selection_bounds (editable, NULL, NULL);
 
@@ -655,15 +650,6 @@ nautilus_location_entry_on_event (GtkWidget *widget,
         return GDK_EVENT_PROPAGATE;
     }
 
-    if (G_UNLIKELY (!gdk_event_get_keyval (event, &keyval)))
-    {
-        g_return_val_if_reached (GDK_EVENT_PROPAGATE);
-    }
-    if (G_UNLIKELY (!gdk_event_get_state (event, &state)))
-    {
-        g_return_val_if_reached (GDK_EVENT_PROPAGATE);
-    }
-
     /* The location bar entry wants TAB to work kind of
      * like it does in the shell for command completion,
      * so if we get a tab and there's a selection, we
@@ -681,7 +667,7 @@ nautilus_location_entry_on_event (GtkWidget *widget,
         }
         else
         {
-            gtk_widget_error_bell (GTK_WIDGET (entry));
+            gtk_widget_error_bell (widget);
         }
 
         return GDK_EVENT_STOP;
@@ -693,23 +679,6 @@ nautilus_location_entry_on_event (GtkWidget *widget,
         set_position_and_selection_to_end (editable);
     }
 
-    /* GTK+ 4 TODO: Calling the event vfunc is not enough, we need the entry
-     *              to handle the key press and insert the text first.
-     *
-     * Chaining up here is required either way, since the code below
-     * used to be in the handler for ::event-after, which is no longer a thing.
-     */
-    handled = parent_widget_class->key_press_event (widget, (GdkEventKey *) event);
-
-
-    if (keyval == GDK_KEY_Down || keyval == GDK_KEY_Up)
-    {
-        /* Ignore up/down arrow keys. These are used by the entry completion,
-         * and if we modify the completion store, navigation through the list
-         * will be interrupted. */
-        return GDK_EVENT_PROPAGATE;
-    }
-
     return GDK_EVENT_PROPAGATE;
 }
 
@@ -809,7 +778,6 @@ nautilus_location_entry_class_init (NautilusLocationEntryClass *class)
 
     widget_class = GTK_WIDGET_CLASS (class);
     widget_class->destroy = destroy;
-    widget_class->event = nautilus_location_entry_on_event;
 
     gobject_class = G_OBJECT_CLASS (class);
     gobject_class->finalize = finalize;
@@ -949,6 +917,15 @@ nautilus_location_entry_init (NautilusLocationEntry *entry)
     g_signal_connect_object (entry, "changed",
                              G_CALLBACK (editable_changed_callback), entry, 0);
 
+    priv->controller = gtk_event_controller_key_new (GTK_WIDGET (entry));
+    /* In GTK3, the Tab key binding (for focus change) happens in the bubble
+     * phase, and we want to stop that from happening. After porting to GTK4
+     * we need to check whether this is still correct. */
+    gtk_event_controller_set_propagation_phase (priv->controller, GTK_PHASE_BUBBLE);
+    g_signal_connect (priv->controller,
+                      "key-pressed",
+                      G_CALLBACK (nautilus_location_entry_key_pressed),
+                      NULL);
     g_signal_connect_after (entry,
                             "insert-text",
                             G_CALLBACK (on_after_insert_text),


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