aboutsummaryrefslogtreecommitdiff
path: root/source/EditorCommandPalette.hpp
diff options
context:
space:
mode:
authorhnOsmium0001 <[email protected]>2022-04-25 20:32:25 -0700
committerhnOsmium0001 <[email protected]>2022-04-25 20:32:25 -0700
commite826894632f59c214c309d934843c49d73103612 (patch)
tree5ece9f0fb9f06576c6f0f4c5838c6655bce99e84 /source/EditorCommandPalette.hpp
parentf54370de7e4214cb7813d26b1a39a8f6e42b7b56 (diff)
Replace EditorCommandPalette.hpp/cpp with the copy from P6503
Diffstat (limited to 'source/EditorCommandPalette.hpp')
-rw-r--r--source/EditorCommandPalette.hpp109
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();
+};