listbase: Grab focus on items instead of container
authorAntónio Fernandes <antoniof@gnome.org>
Thu, 15 Sep 2022 00:52:22 +0000 (01:52 +0100)
committerAntónio Fernandes <antoniof@gnome.org>
Fri, 21 Oct 2022 08:03:14 +0000 (08:03 +0000)
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.

(cherry picked from commit 4fc429892086e8bc1c9be94d5184e7eeff518f9c)

gtk/gtklistbase.c

index 0d09c8d26cb96ee412e9e7b6eed1f25d23fc0f20..03bcb48288cec10164bb72e017b893c1845974eb 100644 (file)
@@ -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_CLASS (gtk_list_base_parent_class)->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;