Hi all,
When a JUCE window is inactive in Windows, clicking its child component activates the
window and correctly move keyboard focus to the clicked component during mouse
event handling. However, the postponed WM_SETFOCUS handling in juce_Windowing_windows.cpp restores
the previous keyboard focus again via handleSetFocus(), causing a different component to end up focused.
It might be better to avoid calling handleSetFocus() if doMouseEvent() has already changed the
focused component. The following patch preserves the changed focus:
diff --git a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp
index 701d8c08e6..6cea92b195 100644
--- a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp
+++ b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp
@@ -2619,6 +2619,8 @@ private:
if (GetCapture() != hwnd)
SetCapture (hwnd);
+ const auto* prevFocusedComponent = Component::getCurrentlyFocusedComponent();
+
doMouseMove (lParam, true, WindowArea::client);
if (isValidPeer (this))
@@ -2633,7 +2635,11 @@ private:
// If this is the first event after receiving both a MOUSEACTIVATE and a SETFOCUS, then
// process the postponed focus update.
if (std::exchange (mouseActivateFlags, (uint8_t) 0) == (gotMouseActivate | gotSetFocus))
- handleSetFocus();
+ {
+ // Don't update the focus if the focus has been changed by doMouseEvent.
+ if (Component::getCurrentlyFocusedComponent() == prevFocusedComponent)
+ handleSetFocus();
+ }
}
void doMouseUp (Point<float> position, const WPARAM wParam, bool adjustCapture = true)
