feat: Allow command palette to work with previous results
This commit is contained in:
parent
9274fac8e1
commit
2ae69e8e72
@ -380,7 +380,7 @@ namespace hex {
|
||||
};
|
||||
|
||||
using DisplayCallback = std::function<std::string(std::string)>;
|
||||
using ExecuteCallback = std::function<void(std::string)>;
|
||||
using ExecuteCallback = std::function<std::optional<std::string>(std::string)>;
|
||||
using QueryCallback = std::function<std::vector<QueryResult>(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
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <imgui.h>
|
||||
|
||||
#include <vector>
|
||||
#include <hex/api/content_registry.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
@ -35,7 +36,7 @@ namespace hex::plugin::builtin {
|
||||
struct CommandResult {
|
||||
std::string displayResult;
|
||||
std::string matchedCommand;
|
||||
std::function<void(std::string)> executeCallback;
|
||||
ContentRegistry::CommandPaletteCommands::impl::ExecuteCallback executeCallback;
|
||||
};
|
||||
|
||||
bool m_commandPaletteOpen = false;
|
||||
|
@ -264,6 +264,17 @@ namespace hex::plugin::builtin {
|
||||
return hex::format("Error: {}", *evaluator.getLastError());
|
||||
else
|
||||
return std::string("???");
|
||||
}, [](auto input) -> std::optional<std::string> {
|
||||
wolv::math_eval::MathEvaluator<long double> evaluator;
|
||||
evaluator.registerStandardVariables();
|
||||
evaluator.registerStandardFunctions();
|
||||
|
||||
std::optional<long double> 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(
|
||||
|
@ -86,12 +86,21 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Focus the input text box when the popup is opened
|
||||
if (m_justOpened) {
|
||||
@ -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; } });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user