From 8455b9ac74d5337429a1a14ebbc792d760651699 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 17 Oct 2022 12:29:44 -0400 Subject: [PATCH] window: Fix focus updates This partially undoes changes from 3dbf5038fab8eb0. That commit did two things: 1) Move the focus update to after-paint time 2) Change from grabbing focus to the visible parent to calling move_focus (TAB) The second part did have the unintended consequence of moving focus laterally. Fixes: #4903 --- gtk/gtkwindow.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c index 21e0ac3036..ff16a68104 100644 --- a/gtk/gtkwindow.c +++ b/gtk/gtkwindow.c @@ -184,6 +184,7 @@ typedef struct GtkWidget *default_widget; GtkWidget *focus_widget; + GtkWidget *move_focus_widget; GtkWindow *transient_parent; GtkWindowGeometryInfo *geometry_info; GtkWindowGroup *group; @@ -4675,7 +4676,22 @@ maybe_unset_focus_and_default (GtkWindow *window) GtkWindowPrivate *priv = gtk_window_get_instance_private (window); if (priv->move_focus) - gtk_widget_child_focus (GTK_WIDGET (window), GTK_DIR_TAB_FORWARD); + { + GtkWidget *parent; + + parent = _gtk_widget_get_parent (priv->move_focus_widget); + + while (parent) + { + if (_gtk_widget_get_visible (parent)) + { + if (gtk_widget_grab_focus (parent)) + break; + } + + parent = _gtk_widget_get_parent (parent); + } + } if (priv->unset_default) gtk_window_set_default_widget (window, NULL); @@ -5134,7 +5150,10 @@ _gtk_window_unset_focus_and_default (GtkWindow *window, child = priv->focus_widget; if (child && (child == widget || gtk_widget_is_ancestor (child, widget))) - priv->move_focus = TRUE; + { + priv->move_focus_widget = widget; + priv->move_focus = TRUE; + } child = priv->default_widget; if (child && (child == widget || gtk_widget_is_ancestor (child, widget)))