Add function-chain contructor for TOOL_ACTION

A function-chain constructor method allows for the long list of
parameters to the constructor to be shrunk to only the ones needed by
each action, and allow self-documenting code for what each part of a
TOOL_ACTION constructor does.

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/7617
This commit is contained in:
Ian McInerney 2022-09-26 23:44:34 +01:00
parent 1d26b454f4
commit b63bbdea5f
3 changed files with 140 additions and 0 deletions

View File

@ -44,6 +44,10 @@ ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) :
if( action->m_id == -1 )
action->m_id = MakeActionId( action->m_name );
wxLogTrace( kicadTraceToolStack,
"ACTION_MANAGER::ACTION_MANAGER: Registering action %s with ID %d and UI ID %d",
action->m_name, action->m_id, action->GetUIId() );
RegisterAction( action );
}
}

View File

@ -29,6 +29,7 @@
#include <algorithm>
#include <bitmaps.h>
#include <hotkeys_basic.h>
#include <wx/stringimpl.h>
#include <wx/translation.h>
TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
@ -63,6 +64,26 @@ TOOL_ACTION::TOOL_ACTION() :
}
TOOL_ACTION::TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs ) :
m_name( aArgs.m_name.value_or( "" ) ),
m_scope( aArgs.m_scope.value_or( AS_CONTEXT ) ),
m_defaultHotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
m_hotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
m_legacyName( aArgs.m_legacyName.value_or( "" ) ),
m_label( aArgs.m_menuText.value_or( wxEmptyString ) ),
m_tooltip( aArgs.m_tooltip.value_or( wxEmptyString ) ),
m_icon( aArgs.m_icon.value_or( BITMAPS::INVALID_BITMAP) ),
m_id( -1 ),
m_flags( aArgs.m_flags.value_or( AF_NONE ) ),
m_param( aArgs.m_param.value_or( nullptr ) )
{
// Action name is the only mandatory part
assert( !m_name.empty() );
ACTION_MANAGER::GetActionList().push_back( this );
}
TOOL_ACTION::~TOOL_ACTION()
{
ACTION_MANAGER::GetActionList().remove( this );

View File

@ -30,6 +30,7 @@
#include <string>
#include <cassert>
#include <optional>
#include <wx/string.h>
@ -53,6 +54,119 @@ enum TOOL_ACTION_FLAGS
AF_NOTIFY = 2 ///< Action is a notification (it is by default passed to all tools)
};
/**
* Build up the properties of a TOOL_ACTION in an incremental manner that is static-construction
* safe.
*/
class TOOL_ACTION_ARGS
{
public:
TOOL_ACTION_ARGS() = default;
/**
* The name of the action, the convention is "app.tool.actionName".
*
* This is a required property.
*/
TOOL_ACTION_ARGS& Name( std::string aName )
{
m_name = aName;
return *this;
}
/**
* The scope of the action.
*/
TOOL_ACTION_ARGS& Scope( TOOL_ACTION_SCOPE aScope )
{
m_scope = aScope;
return *this;
}
/**
* The default hotkey to assign to the action.
*/
TOOL_ACTION_ARGS& DefaultHotkey( int aDefaultHotkey )
{
m_defaultHotKey = aDefaultHotkey;
return *this;
}
/**
* The legacy hotkey name from the old system.
*
* This property is only needed for existing actions and shouldn't be used in new actions.
*/
TOOL_ACTION_ARGS& LegacyHotkeyName( std::string aLegacyName )
{
m_legacyName = aLegacyName;
return *this;
}
/**
*The string to use when displaying the action in a menu.
*/
TOOL_ACTION_ARGS& MenuText( wxString aMenuText )
{
m_menuText = aMenuText;
return *this;
}
/**
* The string to use as a tooltip for the action in menus and toolbars.
*/
TOOL_ACTION_ARGS& Tooltip( wxString aTooltip )
{
m_tooltip = aTooltip;
return *this;
}
/**
* The bitmap to use as the icon for the action in toolbars and menus.
*/
TOOL_ACTION_ARGS& Icon( BITMAPS aIcon )
{
m_icon = aIcon;
return *this;
}
/**
* Flags describing the type of the action.
*/
TOOL_ACTION_ARGS& Flags( TOOL_ACTION_FLAGS aFlags )
{
m_flags = aFlags;
return *this;
}
/**
* Custom parameter to pass information to the tool.
*/
TOOL_ACTION_ARGS& Parameter( void* aParam )
{
m_param = aParam;
return *this;
}
protected:
// Let the TOOL_ACTION constructor have direct access to the members here
friend class TOOL_ACTION;
std::optional<std::string> m_name;
std::optional<TOOL_ACTION_SCOPE> m_scope;
std::optional<TOOL_ACTION_FLAGS> m_flags;
std::optional<int> m_defaultHotKey;
std::optional<std::string> m_legacyName;
std::optional<wxString> m_menuText;
std::optional<wxString> m_tooltip;
std::optional<BITMAPS> m_icon;
std::optional<void*> m_param;
};
/**
* Represent a single user action.
*
@ -67,6 +181,7 @@ enum TOOL_ACTION_FLAGS
class TOOL_ACTION
{
public:
TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs );
TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
const wxString& aMenuText = wxEmptyString,