From c2f661171af1e87a742d4efed9020e42b461d30f Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 19 Oct 2021 20:14:17 +0100 Subject: [PATCH] PopupMenu: Avoid giving focus back to previous component on dismiss This change is designed to fix issues with views stealing focus and being brought to the front when popup menus are dismissed. To test this behaviour: - Open two instances "a" and "b" of a plugin editor containing a ComboBox - Click the ComboBox in editor "a", opening its PopupMenu - Drag the titlebar of editor "b" to move it - The PopupMenu should be dismissed, but editor "a" should *not* steal keyboard focus or be brought to the front --- .../juce_gui_basics/menus/juce_PopupMenu.cpp | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index 61c75ce1cb..276f1203af 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -2013,38 +2013,29 @@ struct PopupMenuCompletionCallback : public ModalComponentManager::Callback if (PopupMenuSettings::menuWasHiddenBecauseOfAppChange) return; - auto* focusComponent = getComponentToPassFocusTo(); - - const auto focusedIsNotMinimised = [focusComponent] + if (auto* focusComponent = Component::getCurrentlyFocusedComponent()) { - if (focusComponent != nullptr) + const auto focusedIsNotMinimised = [focusComponent] + { if (auto* peer = focusComponent->getPeer()) return ! peer->isMinimised(); - return false; - }(); + return false; + }(); - if (focusedIsNotMinimised) - { - if (auto* topLevel = focusComponent->getTopLevelComponent()) - topLevel->toFront (true); + if (focusedIsNotMinimised) + { + if (auto* topLevel = focusComponent->getTopLevelComponent()) + topLevel->toFront (true); - if (focusComponent->isShowing() && ! focusComponent->hasKeyboardFocus (true)) - focusComponent->grabKeyboardFocus(); + if (focusComponent->isShowing() && ! focusComponent->hasKeyboardFocus (true)) + focusComponent->grabKeyboardFocus(); + } } } - Component* getComponentToPassFocusTo() const - { - if (auto* current = Component::getCurrentlyFocusedComponent()) - return current; - - return prevFocused.get(); - } - ApplicationCommandManager* managerOfChosenCommand = nullptr; std::unique_ptr component; - WeakReference prevFocused { Component::getCurrentlyFocusedComponent() }; JUCE_DECLARE_NON_COPYABLE (PopupMenuCompletionCallback) };