ecode: Added command palette.

This commit is contained in:
Martín Lucas Golini
2023-02-08 02:37:37 -03:00
parent 7bccac1c06
commit a6ea966348
16 changed files with 409 additions and 39 deletions

View File

@@ -838,6 +838,12 @@ class EE_API String {
const StringBaseType& back() const;
String& trim( char character = ' ' );
String& lTrim( char character = ' ' );
String& rTrim( char character = ' ' );
private:
friend EE_API bool operator==( const String& left, const String& right );
friend EE_API bool operator<( const String& left, const String& right );

View File

@@ -73,6 +73,8 @@ class EE_API KeyBindings {
const ShortcutMap& getShortcutMap() const;
const std::map<std::string, Uint64> getKeybindings() const;
std::string getShortcutString( Shortcut shortcut ) const;
protected:

View File

@@ -9,10 +9,12 @@ namespace EE { namespace UI { namespace Models {
template <typename T> class ItemListModel final : public Model {
public:
static std::shared_ptr<ItemListModel> create( std::vector<T>& data ) {
return std::make_shared( *new ItemListModel<T>( data ) );
static std::shared_ptr<ItemListModel> create( const std::vector<T>& data ) {
return std::make_shared<ItemListModel<T>>( data );
}
explicit ItemListModel( const std::vector<T>& data ) : mData( data ) {}
virtual ~ItemListModel() {}
virtual size_t rowCount( const ModelIndex& ) const { return mData.size(); }
@@ -30,17 +32,17 @@ template <typename T> class ItemListModel final : public Model {
virtual void update() { onModelUpdate(); }
private:
explicit ItemListModel( std::vector<T>& data ) : mData( data ) {}
std::vector<T>& mData;
const std::vector<T>& mData;
};
template <typename K, typename V> class ItemPairListModel final : public Model {
public:
static std::shared_ptr<ItemPairListModel> create( std::vector<std::pair<K, V>>& data ) {
return std::make_shared( *new ItemPairListModel<K, V>( data ) );
static std::shared_ptr<ItemPairListModel> create( const std::vector<std::pair<K, V>>& data ) {
return std::make_shared<ItemPairListModel<K, V>>( data );
}
explicit ItemPairListModel( const std::vector<std::pair<K, V>>& data ) : mData( data ) {}
virtual ~ItemPairListModel() {}
virtual size_t rowCount( const ModelIndex& ) const { return mData.size(); }
@@ -73,12 +75,132 @@ template <typename K, typename V> class ItemPairListModel final : public Model {
virtual void update() { onModelUpdate(); }
private:
explicit ItemPairListModel( std::vector<K, V>& data ) : mData( data ) {}
std::vector<std::pair<K, V>>& mData;
const std::vector<std::pair<K, V>>& mData;
std::vector<std::string> mColumnNames{ "Title", "Description" };
};
template <typename T> class ItemListOwnerModel final : public Model {
public:
static std::shared_ptr<ItemListOwnerModel> create( const std::vector<T>& data ) {
return std::make_shared<ItemListOwnerModel<T>>( data );
}
explicit ItemListOwnerModel( const std::vector<T>& data ) : mData( data ) {}
virtual ~ItemListOwnerModel() {}
virtual size_t rowCount( const ModelIndex& ) const { return mData.size(); }
virtual size_t columnCount( const ModelIndex& ) const { return 1; }
virtual std::string columnName( const size_t& ) const { return "Data"; }
virtual Variant data( const ModelIndex& index, ModelRole role = ModelRole::Display ) const {
if ( role == ModelRole::Display )
return Variant( mData[index.row()] );
return {};
}
virtual void update() { onModelUpdate(); }
private:
std::vector<T> mData;
};
template <typename K, typename V> class ItemPairListOwnerModel final : public Model {
public:
static std::shared_ptr<ItemPairListOwnerModel>
create( const std::vector<std::pair<K, V>>& data ) {
return std::make_shared<ItemPairListOwnerModel<K, V>>( data );
}
explicit ItemPairListOwnerModel( const std::vector<std::pair<K, V>>& data ) : mData( data ) {}
virtual ~ItemPairListOwnerModel() {}
virtual size_t rowCount( const ModelIndex& ) const { return mData.size(); }
virtual size_t columnCount( const ModelIndex& ) const { return 2; }
virtual std::string columnName( const size_t& index ) const {
eeASSERT( index < 2 );
return mColumnNames[index];
}
virtual void setColumnName( const size_t& index, const std::string& name ) {
eeASSERT( index < 2 );
mColumnNames[index] = name;
}
virtual Variant data( const ModelIndex& index, ModelRole role = ModelRole::Display ) const {
if ( role == ModelRole::Display ) {
switch ( index.column() ) {
case 0:
return Variant( mData[index.row()].first );
case 1:
default:
return Variant( mData[index.row()].second );
}
}
return {};
}
virtual void update() { onModelUpdate(); }
private:
std::vector<std::pair<K, V>> mData;
std::vector<std::string> mColumnNames{ "Title", "Description" };
};
template <typename V> class ItemVectorListOwnerModel final : public Model {
public:
static std::shared_ptr<ItemVectorListOwnerModel>
create( const size_t& columnCount, const std::vector<std::vector<V>>& data ) {
return std::make_shared<ItemVectorListOwnerModel<V>>( columnCount, data );
}
explicit ItemVectorListOwnerModel( const size_t& columnCount,
const std::vector<std::vector<V>>& data ) :
mData( data ) {
mColumnNames.resize( columnCount );
}
virtual ~ItemVectorListOwnerModel() {}
virtual size_t rowCount( const ModelIndex& ) const { return mData.size(); }
virtual size_t columnCount( const ModelIndex& ) const { return mColumnNames.size(); }
virtual std::string columnName( const size_t& index ) const {
eeASSERT( index < mColumnNames.size() );
return mColumnNames[index];
}
virtual void setColumnName( const size_t& index, const std::string& name ) {
eeASSERT( index < mColumnNames.size() );
mColumnNames[index] = name;
}
virtual Variant data( const ModelIndex& index, ModelRole role = ModelRole::Display ) const {
eeASSERT( index.row() < (Int64)mData.size() );
eeASSERT( index.column() < (Int64)mData[index.row()].size() );
if ( index.row() >= (Int64)mData.size() ||
index.column() >= (Int64)mData[index.row()].size() )
return {};
if ( role == ModelRole::Display )
return Variant( mData[index.row()][index.column()] );
return {};
}
virtual void update() { onModelUpdate(); }
private:
std::vector<std::vector<V>> mData;
std::vector<std::string> mColumnNames;
};
}}} // namespace EE::UI::Models
#endif // EE_UI_MODELS_ITEMLISTMODEL_HPP

View File

@@ -16,7 +16,15 @@ class EE_API WidgetCommandExecuter {
WidgetCommandExecuter( const KeyBindings& keybindings ) : mKeyBindings( keybindings ) {}
void setCommand( const std::string& name, const CommandCallback& cb ) { mCommands[name] = cb; }
void setCommand( const std::string& name, const CommandCallback& cb ) {
auto cmdIt = mCommands.find( name );
if ( cmdIt == mCommands.end() ) {
mCommands[name] = cb;
mCommandList.emplace_back( name );
} else {
cmdIt->second = cb;
}
}
bool hasCommand( const std::string& name ) const {
return mCommands.find( name ) != mCommands.end();
@@ -32,9 +40,12 @@ class EE_API WidgetCommandExecuter {
KeyBindings& getKeyBindings() { return mKeyBindings; }
const std::vector<std::string>& getCommandList() const { return mCommandList; }
protected:
KeyBindings mKeyBindings;
std::unordered_map<std::string, std::function<void()>> mCommands;
std::vector<std::string> mCommandList;
Uint32 onKeyDown( const KeyEvent& event ) {
std::string cmd =