From 4e770803c63417791cf2e38e27ec85127056b7a5 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Mon, 18 Nov 2024 13:02:52 +0100 Subject: [PATCH] tdf#163792 gtk3: Don't always focus combobox when its popup closes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since commit 405cf00e4818886b0d3053d03cfb2e3f3a5e8eb8 Author: Michael Weghorn Date: Tue May 28 11:50:12 2024 +0200 related tdf#160971 gtk3 a11y: Keep new combobox value , closing an (editable) combobox like the font size one in the Writer formatting toolbar resulted in the combobox's entry having focus, while the focus was set to the document before that commit. Make this work as expected (again), by making grabbing the focus conditional in 2 places: 1) In GtkInstanceComboBox::signal_popup_toggled, only set the focus to the GtkEntry of the custom combobox implementation if the child focus is already inside of the combobox. As I understand it, the intended logic is "move focus from the combo box's button to its entry", not "unconditionally grab the focus". This also matches what the gtk4 implementation does. Sync the comment from the gtk4 implementation, which explicitly mentions the case of the font combobox in the toolbar (which behaves the same as the font size one in that regard) where focus should not be grabbed. 2) In GtkComboBox::menu_toggled, only explicitly set focus to the toggle button if the combo box is located inside of a popup. This is the same condition used to grab all keyboard events. The code to do both was introduced in commit 131c1c7da8c567636ca55751e49d24cb6d6c9b9e Date: Sun Nov 21 19:53:47 2021 +0000 Related: tdf#145786 cooperate between our own grabs whose commit message only explicitly mentions grabs for popups. The scenarios described in the commit message still work as expected with GDK_BACKEND=x11. With this in place, focus now moves to the document as expected when clicking on a combobox entry in the popup for the font size combobox in Writer's formatting toolbar, while clicking an entry in any of the comboboxes in e.g. Writer's "Format" -> "Character" dialog's "Font" tab still results in the entry of the combobox getting focus. Change-Id: Ib07b034f8327dab19a2264ae3ed8e20ea918dd89 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176713 Reviewed-by: Caolán McNamara Tested-by: Jenkins Reviewed-by: Michael Weghorn --- vcl/unx/gtk3/gtkinst.cxx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/vcl/unx/gtk3/gtkinst.cxx b/vcl/unx/gtk3/gtkinst.cxx index 8ae81f64da1b..c3205805cada 100644 --- a/vcl/unx/gtk3/gtkinst.cxx +++ b/vcl/unx/gtk3/gtkinst.cxx @@ -21395,8 +21395,10 @@ private: GdkSurface* pParentSurface = pParent ? widget_get_surface(pParent) : nullptr; void* pParentIsPopover = pParentSurface ? g_object_get_data(G_OBJECT(pParentSurface), "g-lo-InstancePopup") : nullptr; if (pParentIsPopover) + { do_grab(m_pToggleButton); - gtk_widget_grab_focus(m_pToggleButton); + gtk_widget_grab_focus(m_pToggleButton); + } } } else @@ -21442,11 +21444,18 @@ private: ComboBox::signal_popup_toggled(); if (!m_bPopupActive && m_pEntry) { - disable_notify_events(); - //restore focus to the GtkEntry when the popup is gone, which - //is what the vcl case does, to ease the transition a little - gtk_widget_grab_focus(m_pEntry); - enable_notify_events(); + if (has_child_focus()) + { + // restore focus to the GtkEntry when the popup is gone, which + // is what the vcl case does, to ease the transition a little, + // but don't do it if the focus was moved out of togglebutton + // by something else already (e.g. font combobox in toolbar + // on a "direct pick" from the menu which moves focus into + // the main document + disable_notify_events(); + gtk_widget_grab_focus(m_pEntry); + enable_notify_events(); + } // tdf#160971: For some reason, the tree view in the no longer visible // popup still incorrectly assumes it has focus in addition to the now