diff options
author | rtk0c <[email protected]> | 2022-04-25 20:32:25 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-25 20:32:25 -0700 |
commit | 7fa7840be1093f194031d2d5d9b45ada8613e72e (patch) | |
tree | 50f27a16084d652dee58e7867ffe3f7e3ec477ec /source/EditorCommandPalette.hpp | |
parent | 855da86feae1a5cc14dc2d486ccf115f484dbc2e (diff) |
Changeset: 17 Replace EditorCommandPalette.hpp/cpp with the copy from P6503
Diffstat (limited to 'source/EditorCommandPalette.hpp')
-rw-r--r-- | source/EditorCommandPalette.hpp | 109 |
1 files changed, 77 insertions, 32 deletions
diff --git a/source/EditorCommandPalette.hpp b/source/EditorCommandPalette.hpp index 1603f41..101344d 100644 --- a/source/EditorCommandPalette.hpp +++ b/source/EditorCommandPalette.hpp @@ -2,48 +2,93 @@ #include <imgui.h> #include <cstddef> -#include <cstdint> #include <functional> #include <string> -#include <vector> +#include <string_view> -enum ImCmdTextType { - ImCmdTextType_Regular, - ImCmdTextType_Highlight, - ImCmdTextType_COUNT, +class EditorCommandExecuteContext; +class EditorCommand { +public: + std::string name; + std::function<void(EditorCommandExecuteContext& ctx)> callback; + std::function<void(EditorCommandExecuteContext& ctx, size_t selectedOptionId)> subsequentCallback; + std::function<void()> terminate; }; -namespace ImCmd { -struct Command { - const char* Name; - std::function<void()> InitialCallback; - std::function<void(int selected_option)> SubsequentCallback; - std::function<void()> TerminatingCallback; +class EditorCommandExecuteContext { + friend class EditorCommandPalette; + +private: + const EditorCommand* mCommand = nullptr; + std::vector<std::string> mCurrentOptions; + int mDepth = 0; + +public: + bool IsInitiated() const; + const EditorCommand* GetCurrentCommand() const; + void Initiate(const EditorCommand& command); + + void Prompt(std::vector<std::string> options); + void Finish(); + + /// Return the number of prompts that the user is currently completing. For example, when the user opens command + /// palette fresh and selects a command, 0 is returned. If the command asks some prompt, and then the user selects + /// again, 1 is returned. + int GetExecutionDepth() const; }; -// Command management -void AddCommand(Command command); -void RemoveCommand(const char* name); +class EditorCommandPalette { +private: + struct SearchResult; + struct Item; + + std::vector<EditorCommand> mCommands; + std::vector<Item> mItems; + std::vector<SearchResult> mSearchResults; + std::string mSearchText; + EditorCommandExecuteContext mExeCtx; + int mFocusedItemId = 0; + bool mFocusSearchBox = false; + bool mShouldCloseNextFrame = false; -// Styling -void SetStyleFont(ImCmdTextType type, ImFont* font); -void SetStyleColor(ImCmdTextType type, ImU32 color); -void ClearStyleColor(ImCmdTextType type); //< Clear the style color for the given type, defaulting to ImGuiCol_Text +public: + EditorCommandPalette(); + ~EditorCommandPalette(); -// Command palette widget -void SetNextCommandPaletteSearch(const char* text); -void SetNextCommandPaletteSearchBoxFocused(); -void ShowCommandPalette(const char* name); -bool IsAnyItemSelected(); + EditorCommandPalette(const EditorCommandPalette&) = delete; + EditorCommandPalette& operator=(const EditorCommandPalette&) = delete; + EditorCommandPalette(EditorCommandPalette&&) = default; + EditorCommandPalette& operator=(EditorCommandPalette&&) = default; -void RemoveCache(const char* name); -void RemoveAllCaches(); + void AddCommand(std::string_view category, std::string_view name, EditorCommand command); + void RemoveCommand(std::string_view category, std::string_view name); + void RemoveCommand(const std::string& commandName); -// Command palette widget in a window helper -void SetNextWindowAffixedTop(ImGuiCond cond = 0); -void ShowCommandPaletteWindow(const char* name, bool* p_open); + void Show(bool* open = nullptr); -// Command responses, only call these in command callbacks (except TerminatingCallback) -void Prompt(std::vector<std::string> options); + enum ItemType { + CommandItem, + CommandOptionItem, + }; -} // namespace ImCmd + enum IndexType { + DirectIndex, + SearchResultIndex, + }; + + struct ItemInfo { + const char* text; + const EditorCommand* command; + int itemId; + ItemType itemType; + IndexType indexType; + }; + + size_t GetItemCount() const; + ItemInfo GetItem(size_t idx) const; + + void SelectFocusedItem(); + +private: + void InvalidateSearchResults(); +}; |