From dbe5537fa920d61cf894f203aca9fa696d24964c Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Fri, 14 Aug 2020 02:33:25 +0100 Subject: [PATCH] Don't run the hotkey if the action isn't enabled CHANGED: Don't run the hotkey if the action's UI elements aren't enabled This makes it so the action can't be run from either the hotkey or the UI if its enabling conditions are not met. --- common/tool/action_manager.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/common/tool/action_manager.cpp b/common/tool/action_manager.cpp index e4d875f80d..0310726eca 100644 --- a/common/tool/action_manager.cpp +++ b/common/tool/action_manager.cpp @@ -74,6 +74,10 @@ void ACTION_MANAGER::SetConditions( const TOOL_ACTION& aAction, const ACTION_CON m_uiConditions[aAction.GetId()] = aConditions; + wxLogTrace( kicadTraceToolStack, + "ACTION_MANAGER::SetConditions: Registering conditions for ID %d - %s", + aAction.GetId(), aAction.GetName() ); + // Register a new handler with the new conditions if( m_toolMgr ) m_toolMgr->GetToolHolder()->RegisterUIUpdateHandler( aAction, aConditions ); @@ -82,7 +86,7 @@ void ACTION_MANAGER::SetConditions( const TOOL_ACTION& aAction, const ACTION_CON const ACTION_CONDITIONS* ACTION_MANAGER::GetCondition( const TOOL_ACTION& aAction ) const { - const auto it = m_uiConditions.find( aAction.GetUIId() ); + const auto it = m_uiConditions.find( aAction.GetId() ); // If the action doesn't have something registered, then return null if( it == m_uiConditions.end() ) @@ -174,25 +178,41 @@ bool ACTION_MANAGER::RunHotKey( int aHotKey ) const } } + // Get the selection to use to test if the action is enabled + SELECTION& sel = m_toolMgr->GetToolHolder()->GetCurrentSelection(); + if( context ) { + bool runAction = true; + + if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) ) + runAction = aCond->enableCondition( sel ); + wxLogTrace( kicadTraceToolStack, - "ACTION_MANAGER::RunHotKey Running context action %s for hotkey %s", + "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s", + runAction ? "Running" : "Not running", context->GetName(), KeyNameFromKeyCode( aHotKey ) ); - return m_toolMgr->RunAction( *context, true ); + if( runAction ) + return m_toolMgr->RunAction( *context, true ); } else if( !global.empty() ) { for( auto act : global ) { + bool runAction = true; + + if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) ) + runAction = aCond->enableCondition( sel ); + wxLogTrace( kicadTraceToolStack, - "ACTION_MANAGER::RunHotKey Running global action: %s for hotkey %s", + "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s", + runAction ? "Running" : "Not running", act->GetName(), KeyNameFromKeyCode( aHotKey ) ); - if( m_toolMgr->RunAction( *act, true ) ) + if( runAction && m_toolMgr->RunAction( *act, true ) ) return true; } }