From e826894632f59c214c309d934843c49d73103612 Mon Sep 17 00:00:00 2001 From: hnOsmium0001 Date: Mon, 25 Apr 2022 20:32:25 -0700 Subject: Replace EditorCommandPalette.hpp/cpp with the copy from P6503 --- source/EditorCommandPalette.hpp | 109 ++++++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 32 deletions(-) (limited to 'source/EditorCommandPalette.hpp') 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 #include -#include #include #include -#include +#include -enum ImCmdTextType { - ImCmdTextType_Regular, - ImCmdTextType_Highlight, - ImCmdTextType_COUNT, +class EditorCommandExecuteContext; +class EditorCommand { +public: + std::string name; + std::function callback; + std::function subsequentCallback; + std::function terminate; }; -namespace ImCmd { -struct Command { - const char* Name; - std::function InitialCallback; - std::function SubsequentCallback; - std::function TerminatingCallback; +class EditorCommandExecuteContext { + friend class EditorCommandPalette; + +private: + const EditorCommand* mCommand = nullptr; + std::vector mCurrentOptions; + int mDepth = 0; + +public: + bool IsInitiated() const; + const EditorCommand* GetCurrentCommand() const; + void Initiate(const EditorCommand& command); + + void Prompt(std::vector 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 mCommands; + std::vector mItems; + std::vector 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 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(); +}; -- cgit v1.2.3-70-g09d2