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.
This commit is contained in:
António Fernandes 2022-09-15 01:52:22 +01:00
parent 1b4ed00509
commit 4fc4298920

View 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;