From 2ae69e8e7235dbd067f938cd5fded4e6961ff97a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 23 Dec 2024 23:42:45 +0100 Subject: [PATCH] feat: Allow command palette to work with previous results --- .../include/hex/api/content_registry.hpp | 4 +-- .../content/views/view_command_palette.hpp | 5 ++-- .../content/command_palette_commands.cpp | 13 +++++++++ .../content/views/view_command_palette.cpp | 27 ++++++++++++++----- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index ff219d354..9323c93e5 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -380,7 +380,7 @@ namespace hex { }; using DisplayCallback = std::function; - using ExecuteCallback = std::function; + using ExecuteCallback = std::function(std::string)>; using QueryCallback = std::function(std::string)>; struct Entry { @@ -416,7 +416,7 @@ namespace hex { const std::string &command, const UnlocalizedString &unlocalizedDescription, const impl::DisplayCallback &displayCallback, - const impl::ExecuteCallback &executeCallback = [](auto) {}); + const impl::ExecuteCallback &executeCallback = [](auto) { return std::nullopt; }); /** * @brief Adds a new command handler to the command palette diff --git a/plugins/builtin/include/content/views/view_command_palette.hpp b/plugins/builtin/include/content/views/view_command_palette.hpp index 57a15346c..1975b0fd1 100644 --- a/plugins/builtin/include/content/views/view_command_palette.hpp +++ b/plugins/builtin/include/content/views/view_command_palette.hpp @@ -5,6 +5,7 @@ #include #include +#include namespace hex::plugin::builtin { @@ -35,7 +36,7 @@ namespace hex::plugin::builtin { struct CommandResult { std::string displayResult; std::string matchedCommand; - std::function executeCallback; + ContentRegistry::CommandPaletteCommands::impl::ExecuteCallback executeCallback; }; bool m_commandPaletteOpen = false; @@ -54,4 +55,4 @@ namespace hex::plugin::builtin { std::vector getCommandResults(const std::string &input); }; -} \ No newline at end of file +} diff --git a/plugins/builtin/source/content/command_palette_commands.cpp b/plugins/builtin/source/content/command_palette_commands.cpp index 5f2f4207a..8058afbd5 100644 --- a/plugins/builtin/source/content/command_palette_commands.cpp +++ b/plugins/builtin/source/content/command_palette_commands.cpp @@ -264,6 +264,17 @@ namespace hex::plugin::builtin { return hex::format("Error: {}", *evaluator.getLastError()); else return std::string("???"); + }, [](auto input) -> std::optional { + wolv::math_eval::MathEvaluator evaluator; + evaluator.registerStandardVariables(); + evaluator.registerStandardFunctions(); + + std::optional result = evaluator.evaluate(input); + if (result.has_value()) { + return hex::format("= {}", result.value()); + } else { + return std::nullopt; + } }); ContentRegistry::CommandPaletteCommands::add( @@ -275,6 +286,7 @@ namespace hex::plugin::builtin { }, [](auto input) { hex::openWebpage(input); + return std::nullopt; }); ContentRegistry::CommandPaletteCommands::add( @@ -286,6 +298,7 @@ namespace hex::plugin::builtin { }, [](auto input) { hex::executeCommand(input); + return std::nullopt; }); ContentRegistry::CommandPaletteCommands::addHandler( diff --git a/plugins/builtin/source/content/views/view_command_palette.cpp b/plugins/builtin/source/content/views/view_command_palette.cpp index e26229315..4df5df2cb 100644 --- a/plugins/builtin/source/content/views/view_command_palette.cpp +++ b/plugins/builtin/source/content/views/view_command_palette.cpp @@ -86,11 +86,20 @@ namespace hex::plugin::builtin { // Execute the currently selected command when pressing enter if (ImGui::IsItemFocused() && (ImGui::IsKeyPressed(ImGuiKey_Enter, false) || ImGui::IsKeyPressed(ImGuiKey_KeypadEnter, false))) { + bool closePalette = true; if (!m_lastResults.empty()) { auto &[displayResult, matchedCommand, callback] = m_lastResults.front(); - callback(matchedCommand); + + if (auto result = callback(matchedCommand); result.has_value()) { + m_commandBuffer = result.value(); + closePalette = false; + m_focusInputTextBox = true; + } + } + + if (closePalette) { + ImGui::CloseCurrentPopup(); } - ImGui::CloseCurrentPopup(); } // Focus the input text box when the popup is opened @@ -113,11 +122,15 @@ namespace hex::plugin::builtin { // Allow executing a command by clicking on it or selecting it with the keyboard and pressing enter if (ImGui::Selectable(displayResult.c_str(), false, ImGuiSelectableFlags_NoAutoClosePopups)) { - callback(matchedCommand); + if (auto result = callback(matchedCommand); result.has_value()) + m_commandBuffer = result.value(); + break; } if (ImGui::IsItemFocused() && (ImGui::IsKeyDown(ImGuiKey_Enter) || ImGui::IsKeyDown(ImGuiKey_KeypadEnter))) { - callback(matchedCommand); + if (auto result = callback(matchedCommand); result.has_value()) + m_commandBuffer = result.value(); + break; } } @@ -161,6 +174,8 @@ namespace hex::plugin::builtin { this->focusInputTextBox(); m_commandBuffer = currCommand + " "; m_lastResults = this->getCommandResults(currCommand); + + return std::nullopt; }; if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) { @@ -201,11 +216,11 @@ namespace hex::plugin::builtin { for (const auto &[description, callback] : queryCallback(processedInput)) { if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) { if (auto [match, value] = MatchCommand(input, command); match != MatchType::NoMatch) { - results.push_back({ hex::format("{} ({})", command, description), "", callback }); + results.push_back({ hex::format("{} ({})", command, description), "", [callback](auto ... args){ callback(args...); return std::nullopt; } }); } } else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) { if (auto [match, value] = MatchCommand(input, command + " "); match != MatchType::NoMatch) { - results.push_back({ hex::format("{} ({})", command, description), "", callback }); + results.push_back({ hex::format("{} ({})", command, description), "", [callback](auto ... args){ callback(args...); return std::nullopt; } }); } } }