Focus gain for lastFocusedComponent does not check if it still wants keyboard focus


#1

We’re doing some fancy keyboard focus stuff at the request of users who need special handling for their particular DAW’s. To do this, we have to call setWantsKeyboardFocus based on a number of factors that might change over time.

Mostly this works great except for JUCE’s handling of lastFocusedComponent. In particular, the ComponentPeer tries to give back focus to the lastFocusedComponent even if wantsFocusFlag is false. Is it possible to add a check that lastFocusedComponent actually wants keyboard focus before giving it back?


#2

Sure, maybe suggest a change that does what you need and I can review that?


#3

Cool. I think this should work:

--- a/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp
+++ b/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp
@@ -345,9 +345,7 @@ void ComponentPeer::handleFocusGain()
 
     if (component.isParentOf (lastFocusedComponent))
     {
-        Component::currentlyFocusedComponent = lastFocusedComponent;
-        Desktop::getInstance().triggerFocusCallback();
-        lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
+        lastFocusedComponent->grabFocusInternal (Component::focusChangedDirectly, false);^M

This would require that it the lastFocusedComponent be showing, which seems like an improvement too?


#4

Hmm, looks to me like that could end up being recursive in some situations - I think that’s why the code was written the way it was, and why it set the pointer directly rather than invoking another function to change it.


#5

Hmm, I see. Maybe these checks could just be added instead?

--- a/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp
+++ b/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp
@@ -343,7 +343,9 @@ void ComponentPeer::handleFocusGain()
 {
     ModifierKeys::updateCurrentModifiers();
 
-    if (component.isParentOf (lastFocusedComponent))
+    if (component.isParentOf (lastFocusedComponent)
+        && lastFocusedComponent->isShowing()
+        && lastFocusedComponent->getWantsKeyboardFocus()))^M

#6

Sure, sounds like a sensible addition - will add it now…