diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index 278904f2ef..4f7c30c907 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -1015,12 +1015,12 @@ public: bool canBecomeKeyWindow() { - return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0; + return component.isVisible() && (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0; } bool canBecomeMainWindow() { - return dynamic_cast (&component) != nullptr; + return component.isVisible() && dynamic_cast (&component) != nullptr; } bool worksWhenModal() const @@ -1881,9 +1881,15 @@ private: static BOOL becomeFirstResponder (id self, SEL) { if (auto* owner = getOwner (self)) - owner->viewFocusGain(); + { + if (owner->canBecomeKeyWindow()) + { + owner->viewFocusGain(); + return YES; + } + } - return YES; + return NO; } static BOOL resignFirstResponder (id self, SEL) @@ -1999,7 +2005,17 @@ private: sendSuperclassMessage (self, @selector (becomeKeyWindow)); if (auto* owner = getOwner (self)) - owner->becomeKeyWindow(); + { + if (owner->canBecomeKeyWindow()) + { + owner->becomeKeyWindow(); + return; + } + + // this fixes a bug causing hidden windows to sometimes become visible when the app regains focus + if (! owner->getComponent().isVisible()) + [(NSWindow*) self orderOut: nil]; + } } static BOOL windowShouldClose (id self, SEL, id /*window*/)