Browse Source

Add a flag to release the focus of a component when the user accesses the main menu bar on mac

tags/2021-05-28
hogliux 8 years ago
parent
commit
17a07a0057
3 changed files with 45 additions and 22 deletions
  1. +1
    -0
      modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp
  2. +32
    -19
      modules/juce_gui_basics/components/juce_Component.h
  3. +12
    -3
      modules/juce_gui_basics/native/juce_mac_MainMenu.mm

+ 1
- 0
modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp View File

@@ -90,6 +90,7 @@ MidiKeyboardComponent::MidiKeyboardComponent (MidiKeyboardState& s, Orientation
colourChanged(); colourChanged();
setWantsKeyboardFocus (true); setWantsKeyboardFocus (true);
setLosesFocusWhenAccessingMainMenuBar (true);
state.addListener (this); state.addListener (this);


+ 32
- 19
modules/juce_gui_basics/components/juce_Component.h View File

@@ -880,6 +880,18 @@ public:
bool& allowsClicksOnChildComponents) const noexcept; bool& allowsClicksOnChildComponents) const noexcept;
/** Changes the focus behavior when the main menu bar is accessed.
If set to true, then clicking the main menu bar on Mac will unfocus the component.
*/
void setLosesFocusWhenAccessingMainMenuBar (bool loseFocus) noexcept { flags.shouldReleaseFocusOnMainMenuBarAccess = loseFocus; }
/** Retrieves the focus behavior when the main menu bar is accessed.
Returns true if clicking the main menu bar on Mac will unfocus this component.
*/
bool getLosesFocusWhenAccessingMainMenuBar() const noexcept { return flags.shouldReleaseFocusOnMainMenuBarAccess; }
/** Returns true if a given point lies within this component or one of its children. /** Returns true if a given point lies within this component or one of its children.
Never override this method! Use hitTest to create custom hit regions. Never override this method! Use hitTest to create custom hit regions.
@@ -2266,27 +2278,28 @@ private:
struct ComponentFlags struct ComponentFlags
{ {
bool hasHeavyweightPeerFlag : 1;
bool visibleFlag : 1;
bool opaqueFlag : 1;
bool ignoresMouseClicksFlag : 1;
bool allowChildMouseClicksFlag : 1;
bool wantsFocusFlag : 1;
bool isFocusContainerFlag : 1;
bool dontFocusOnMouseClickFlag : 1;
bool alwaysOnTopFlag : 1;
bool bufferToImageFlag : 1;
bool bringToFrontOnClickFlag : 1;
bool repaintOnMouseActivityFlag : 1;
bool isDisabledFlag : 1;
bool childCompFocusedFlag : 1;
bool dontClipGraphicsFlag : 1;
bool mouseDownWasBlocked : 1;
bool isMoveCallbackPending : 1;
bool isResizeCallbackPending : 1;
bool hasHeavyweightPeerFlag : 1;
bool visibleFlag : 1;
bool opaqueFlag : 1;
bool ignoresMouseClicksFlag : 1;
bool allowChildMouseClicksFlag : 1;
bool wantsFocusFlag : 1;
bool isFocusContainerFlag : 1;
bool dontFocusOnMouseClickFlag : 1;
bool alwaysOnTopFlag : 1;
bool bufferToImageFlag : 1;
bool bringToFrontOnClickFlag : 1;
bool repaintOnMouseActivityFlag : 1;
bool isDisabledFlag : 1;
bool childCompFocusedFlag : 1;
bool dontClipGraphicsFlag : 1;
bool mouseDownWasBlocked : 1;
bool isMoveCallbackPending : 1;
bool isResizeCallbackPending : 1;
#if JUCE_DEBUG #if JUCE_DEBUG
bool isInsidePaintCall : 1;
bool isInsidePaintCall : 1;
#endif #endif
bool shouldReleaseFocusOnMainMenuBarAccess : 1;
}; };
union union


+ 12
- 3
modules/juce_gui_basics/native/juce_mac_MainMenu.mm View File

@@ -728,10 +728,19 @@ static void mainMenuTrackingChanged (bool isTracking)
{ {
menuHandler->isOpen = isTracking; menuHandler->isOpen = isTracking;
if (menuHandler->defferedUpdateRequested && ! isTracking)
if (isTracking)
{ {
menuHandler->defferedUpdateRequested = false;
menuHandler->menuBarItemsChanged (menuHandler->currentModel);
if (Component* c = Component::getCurrentlyFocusedComponent())
if (c->getLosesFocusWhenAccessingMainMenuBar())
c->unfocusAllComponents();
}
else
{
if (menuHandler->defferedUpdateRequested)
{
menuHandler->defferedUpdateRequested = false;
menuHandler->menuBarItemsChanged (menuHandler->currentModel);
}
} }
} }
} }


Loading…
Cancel
Save