window: Fix focus updates
authorMatthias Clasen <mclasen@redhat.com>
Mon, 17 Oct 2022 16:29:44 +0000 (12:29 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 17 Oct 2022 19:10:41 +0000 (15:10 -0400)
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

index 21e0ac30369c0b9969c2bcd122d7ee811029956f..ff16a681041f9b8842560cf8dacb2adfed3cdf3a 100644 (file)
@@ -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)))