feat: Allow command palette to work with previous results

This commit is contained in:
WerWolv
2024-12-23 23:42:45 +01:00
parent 9274fac8e1
commit 2ae69e8e72
4 changed files with 39 additions and 10 deletions

View File

@@ -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(

View File

@@ -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; } });
}
}
}