[gtk/antoniof-main-patch-03677: 138/139] listbase: Grab focus on items instead of container




commit 9804bbcb329f363c5b1fc0f6dfa11bed12253f1c
Author: António Fernandes <antoniof gnome org>
Date:   Thu Sep 15 01:52:22 2022 +0100

    listbase: Grab focus on items instead of container
    
    The container view itself being focusable makes keyboard navigation
    slower by adding a useless focus step.
    
    It also means if an item gets removed, the focus jumps back to the view,
    instead of jumping to the next item, as seen in nautilus bug report:
    https://gitlab.gnome.org/GNOME/nautilus/-/issues/2489
    
    Instead of making the GtkListBase container itself focusable, override
    the .grab_focus() vfunc. This way, calling gtk_widget_grab_focus() on
    the view container keeps working sucessfully, but focuses the focus
    item directly instead.
    
    This is particularly useful to have because applicaiton authors do
    not have direct acess to this class's children, so they can't call
    gtk_widget_grab_focus() on them directly.

 gtk/gtklistbase.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtklistbase.c b/gtk/gtklistbase.c
index 0d09c8d26c..64cbe56723 100644
--- a/gtk/gtklistbase.c
+++ b/gtk/gtklistbase.c
@@ -569,6 +569,20 @@ gtk_list_base_focus (GtkWidget        *widget,
   return gtk_widget_child_focus (item->widget, direction);
 }
 
+static gboolean
+gtk_list_base_grab_focus (GtkWidget *widget)
+{
+  GtkListBase *self = GTK_LIST_BASE (widget);
+  GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
+  guint pos;
+
+  pos = gtk_list_item_tracker_get_position (priv->item_manager, priv->focus);
+  if (gtk_list_base_grab_focus_on_item (self, pos, FALSE, FALSE, FALSE))
+    return TRUE;
+
+  return GTK_WIDGET_GET_CLASS (widget)->grab_focus (widget);
+}
+
 static void
 gtk_list_base_dispose (GObject *object)
 {
@@ -1120,6 +1134,7 @@ gtk_list_base_class_init (GtkListBaseClass *klass)
   gpointer iface;
 
   widget_class->focus = gtk_list_base_focus;
+  widget_class->grab_focus = gtk_list_base_grab_focus;
 
   gobject_class->dispose = gtk_list_base_dispose;
   gobject_class->get_property = gtk_list_base_get_property;
@@ -1833,7 +1848,6 @@ gtk_list_base_init_real (GtkListBase      *self,
   priv->orientation = GTK_ORIENTATION_VERTICAL;
 
   gtk_widget_set_overflow (GTK_WIDGET (self), GTK_OVERFLOW_HIDDEN);
-  gtk_widget_set_focusable (GTK_WIDGET (self), TRUE);
 
   controller = gtk_drop_controller_motion_new ();
   g_signal_connect (controller, "motion", G_CALLBACK (gtk_list_base_drag_motion), NULL);


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