mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-02-02 03:20:06 +00:00
MainMenu: Allow components to directly handle shortcut keys which trigger menu items
This fixes a regression introduced by
6e9261ea66 which meant that components
were not given a chance to respond to shortcut keypresses if those same
keypresses were registered for a menu item. This resulted in behaviour
where shortcuts such as 'cmd+c' would not be passed to a focused
TextEditor if a different command with the same shortcut was registered
in the main menu.
With this change in place, we now check whether the menu item's shortcut
keys match the current event's pressed keys. If the keypresses match, we
can assume that the event was triggered by the keyboard, and dispatch
the keypresses to the ComponentPeer. If the keypresses do not match,
then the menu item was likely selected using space/return, or by
clicking, in which case the event is dispatched directly to the
ApplicationCommandManager.
This commit is contained in:
parent
75ed712b19
commit
1e606ddb32
1 changed files with 33 additions and 2 deletions
|
|
@ -537,12 +537,43 @@ private:
|
|||
}
|
||||
|
||||
private:
|
||||
/* Returns true if and only if the peer handles the event. */
|
||||
static bool tryPassingKeyEventToPeer (NSMenuItem* item)
|
||||
{
|
||||
auto* e = [NSApp currentEvent];
|
||||
|
||||
if ([e type] != NSEventTypeKeyDown && [e type] != NSEventTypeKeyUp)
|
||||
return false;
|
||||
|
||||
const auto triggeredByShortcut = [[e charactersIgnoringModifiers] isEqualToString: [item keyEquivalent]]
|
||||
&& ([e modifierFlags] & ~(NSUInteger) 0xFFFF) == [item keyEquivalentModifierMask];
|
||||
|
||||
if (! triggeredByShortcut)
|
||||
return false;
|
||||
|
||||
if (auto* focused = juce::Component::getCurrentlyFocusedComponent())
|
||||
{
|
||||
if (auto* peer = dynamic_cast<juce::NSViewComponentPeer*> (focused->getPeer()))
|
||||
{
|
||||
if ([e type] == NSEventTypeKeyDown)
|
||||
peer->redirectKeyDown (e);
|
||||
else
|
||||
peer->redirectKeyUp (e);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void menuItemInvoked (id self, SEL, NSMenuItem* item)
|
||||
{
|
||||
auto owner = getIvar<JuceMainMenuHandler*> (self, "owner");
|
||||
if (tryPassingKeyEventToPeer (item))
|
||||
return;
|
||||
|
||||
if (auto* juceItem = getJuceClassFromNSObject<PopupMenu::Item> ([item representedObject]))
|
||||
owner->invoke (*juceItem, static_cast<int> ([item tag]));
|
||||
getIvar<JuceMainMenuHandler*> (self, "owner")->invoke (*juceItem, static_cast<int> ([item tag]));
|
||||
}
|
||||
|
||||
static void menuNeedsUpdate (id self, SEL, NSMenu* menu)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue