[gtk/gtk4_fix_focus_jump_general] Move focus to parent when hiding a focused child




commit c591ca3e82a15981dc9e284c88aabc383095fe8a
Author: Nelson Benítez León <nbenitezl gmail com>
Date:   Tue Aug 30 20:31:17 2022 -0400

    Move focus to parent when hiding a focused child
    
    because otherwise the focus will jump forward
    to next widget, while expected behaviour is
    for focus to come back to parent widget.
    
    As we now do this for all widgets in gtk_widget_hide(),
    there's no need for individual widgets to do that
    explicitly, so we remove such explicit calls by the
    GtkText and GtkTextView widgets.
    
    Fixes issue #4903

 gtk/gtktext.c     |  1 -
 gtk/gtktextview.c |  1 -
 gtk/gtkwidget.c   | 21 ++++++++++++++++++---
 3 files changed, 18 insertions(+), 5 deletions(-)
---
diff --git a/gtk/gtktext.c b/gtk/gtktext.c
index b95d6d8c51..5658a08cb8 100644
--- a/gtk/gtktext.c
+++ b/gtk/gtktext.c
@@ -6919,7 +6919,6 @@ gtk_text_insert_emoji (GtkText *self)
 
       gtk_widget_set_parent (chooser, GTK_WIDGET (self));
       g_signal_connect (chooser, "emoji-picked", G_CALLBACK (emoji_picked), self);
-      g_signal_connect_swapped (chooser, "hide", G_CALLBACK (gtk_text_grab_focus_without_selecting), self);
     }
 
   gtk_popover_popup (GTK_POPOVER (chooser));
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index bfd3616530..0f8854e224 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -10070,7 +10070,6 @@ gtk_text_view_insert_emoji (GtkTextView *text_view)
 
       gtk_widget_set_parent (chooser, GTK_WIDGET (text_view));
       g_signal_connect (chooser, "emoji-picked", G_CALLBACK (emoji_picked), text_view);
-      g_signal_connect_swapped (chooser, "hide", G_CALLBACK (gtk_widget_grab_focus), text_view);
     }
 
   buffer = get_buffer (text_view);
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index dda5a1f042..57760e261f 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -67,6 +67,7 @@
 #include "gtksizegroup-private.h"
 #include "gtksnapshotprivate.h"
 #include "gtkstylecontextprivate.h"
+#include "gtktext.h"
 #include "gtktooltipprivate.h"
 #include "gsktransformprivate.h"
 #include "gtktypebuiltins.h"
@@ -2734,15 +2735,20 @@ gtk_widget_hide (GtkWidget *widget)
 
   if (_gtk_widget_get_visible (widget))
     {
-      GtkWidget *parent;
+      GtkWidget *parent, *focused_widget;
       GtkRoot *root;
+      gboolean widget_is_focused = FALSE;
 
       g_object_ref (widget);
       gtk_widget_push_verify_invariants (widget);
 
       root = _gtk_widget_get_root (widget);
       if (GTK_WIDGET (root) != widget && GTK_IS_WINDOW (root))
-        _gtk_window_unset_focus_and_default (GTK_WINDOW (root), widget);
+        {
+          focused_widget = gtk_window_get_focus (GTK_WINDOW (root));
+          widget_is_focused = focused_widget && (focused_widget == widget || gtk_widget_is_ancestor 
(focused_widget, widget));
+          _gtk_window_unset_focus_and_default (GTK_WINDOW (root), widget);
+        }
 
       /* a parent may now be expand=FALSE since we're hidden. */
       if (priv->need_compute_expand ||
@@ -2763,7 +2769,16 @@ gtk_widget_hide (GtkWidget *widget)
 
       parent = gtk_widget_get_parent (widget);
       if (parent)
-        gtk_widget_queue_resize (parent);
+        {
+          gtk_widget_queue_resize (parent);
+          if (widget_is_focused)
+            {
+              if (GTK_IS_TEXT (parent))
+                gtk_text_grab_focus_without_selecting (GTK_TEXT (parent));
+              else
+                gtk_widget_grab_focus (parent);
+            }
+        }
 
       gtk_widget_queue_allocate (widget);
 


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