From 76c6d324ac685fa716cf36047eb546969892e3c2 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 6 Oct 2014 10:52:28 +0100 Subject: [PATCH] Made ApplicationCommandManager::registerCommand update the command info. --- .../juce_ApplicationCommandManager.cpp | 39 ++++++++++++------- .../commands/juce_ApplicationCommandManager.h | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp b/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp index 4f4ff46c69..82ec3a248b 100644 --- a/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp +++ b/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.cpp @@ -51,7 +51,20 @@ void ApplicationCommandManager::registerCommand (const ApplicationCommandInfo& n // the name isn't optional! jassert (newCommand.shortName.isNotEmpty()); - if (getCommandForID (newCommand.commandID) == 0) + if (ApplicationCommandInfo* command = getMutableCommandForID (newCommand.commandID)) + { + // Trying to re-register the same command ID with different parameters can often indicate a typo. + // This assertion is here because I've found it useful catching some mistakes, but it may also cause + // false alarms if you're deliberately updating some flags for a command. + jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName + && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName + && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses + && (newCommand.flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor)) + == (getCommandForID (newCommand.commandID)->flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor))); + + *command = newCommand; + } + else { ApplicationCommandInfo* const newInfo = new ApplicationCommandInfo (newCommand); newInfo->flags &= ~ApplicationCommandInfo::isTicked; @@ -61,16 +74,6 @@ void ApplicationCommandManager::registerCommand (const ApplicationCommandInfo& n triggerAsyncUpdate(); } - else - { - // trying to re-register the same command ID with different parameters? - jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName - && (newCommand.description == getCommandForID (newCommand.commandID)->description || newCommand.description.isEmpty()) - && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName - && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses - && (newCommand.flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor)) - == (getCommandForID (newCommand.commandID)->flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor))); - } } void ApplicationCommandManager::registerAllCommandsForTarget (ApplicationCommandTarget* target) @@ -113,7 +116,7 @@ void ApplicationCommandManager::commandStatusChanged() } //============================================================================== -const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (const CommandID commandID) const noexcept +ApplicationCommandInfo* ApplicationCommandManager::getMutableCommandForID (CommandID commandID) const noexcept { for (int i = commands.size(); --i >= 0;) if (commands.getUnchecked(i)->commandID == commandID) @@ -122,6 +125,11 @@ const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (const return nullptr; } +const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (const CommandID commandID) const noexcept +{ + return getMutableCommandForID (commandID); +} + String ApplicationCommandManager::getNameOfCommand (const CommandID commandID) const noexcept { if (const ApplicationCommandInfo* const ci = getCommandForID (commandID)) @@ -215,7 +223,10 @@ ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const target = target->getTargetForCommand (commandID); if (target != nullptr) + { + upToDateInfo.commandID = commandID; target->getCommandInfo (commandID, upToDateInfo); + } return target; } @@ -223,7 +234,7 @@ ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const //============================================================================== ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c) { - ApplicationCommandTarget* target = dynamic_cast (c); + ApplicationCommandTarget* target = dynamic_cast (c); if (target == nullptr && c != nullptr) target = c->findParentComponentOfClass(); @@ -263,7 +274,7 @@ ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget( // component that really should get the event. And if not, the event will // still be passed up to the top level window anyway, so let's send it to the // content comp. - if (ResizableWindow* const resizableWindow = dynamic_cast (c)) + if (ResizableWindow* const resizableWindow = dynamic_cast (c)) if (Component* const content = resizableWindow->getContentComponent()) c = content; diff --git a/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.h b/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.h index 1d6158ed8d..5be135a7d4 100644 --- a/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.h +++ b/modules/juce_gui_basics/commands/juce_ApplicationCommandManager.h @@ -308,6 +308,7 @@ private: void sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo&); void handleAsyncUpdate() override; void globalFocusChanged (Component*) override; + ApplicationCommandInfo* getMutableCommandForID (CommandID) const noexcept; #if JUCE_CATCH_DEPRECATED_CODE_MISUSE // This is just here to cause a compile error in old code that hasn't been changed to use the new