2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_command_palette.hpp"
|
2020-12-16 22:43:07 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-03-20 17:05:26 +01:00
|
|
|
#include <wolv/utils/guards.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-12-16 22:43:07 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ViewCommandPalette::ViewCommandPalette() : View::Special("hex.builtin.view.command_palette.name") {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Add global shortcut to open the command palette
|
2023-11-17 14:46:21 +01:00
|
|
|
ShortcutManager::addGlobalShortcut(CTRLCMD + SHIFT + Keys::P, "hex.builtin.view.command_palette.name", [this] {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenPopup::post("hex.builtin.view.command_palette.name"_lang);
|
2023-12-19 13:10:25 +01:00
|
|
|
m_commandPaletteOpen = true;
|
|
|
|
m_justOpened = true;
|
2021-12-23 15:11:38 +01:00
|
|
|
});
|
2023-11-14 00:03:22 +01:00
|
|
|
|
2023-12-14 13:50:26 +01:00
|
|
|
EventSearchBoxClicked::subscribe([this](ImGuiMouseButton button) {
|
|
|
|
if (button == ImGuiMouseButton_Left) {
|
|
|
|
RequestOpenPopup::post("hex.builtin.view.command_palette.name"_lang);
|
2023-12-19 13:10:25 +01:00
|
|
|
m_commandPaletteOpen = true;
|
|
|
|
m_justOpened = true;
|
2023-12-14 13:50:26 +01:00
|
|
|
}
|
2023-11-14 00:03:22 +01:00
|
|
|
});
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
void ViewCommandPalette::drawAlwaysVisibleContent() {
|
2023-08-29 12:14:12 +02:00
|
|
|
// If the command palette is hidden, don't draw it
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_commandPaletteOpen) return;
|
2021-01-27 14:26:24 +01:00
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
auto windowPos = ImHexApi::System::getMainWindowPosition();
|
2022-02-01 18:09:40 +01:00
|
|
|
auto windowSize = ImHexApi::System::getMainWindowSize();
|
|
|
|
|
|
|
|
ImGui::SetNextWindowPos(ImVec2(windowPos.x + windowSize.x * 0.5F, windowPos.y), ImGuiCond_Always, ImVec2(0.5F, 0.0F));
|
2023-11-23 09:33:47 +01:00
|
|
|
ImGui::SetNextWindowSizeConstraints(this->getMinSize(), this->getMaxSize());
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::BeginPopup("hex.builtin.view.command_palette.name"_lang)) {
|
2023-11-23 09:33:47 +01:00
|
|
|
ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindowRead());
|
|
|
|
ImGui::BringWindowToFocusFront(ImGui::GetCurrentWindowRead());
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Close the popup if the user presses ESC
|
2024-04-12 22:56:10 +02:00
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_Escape))
|
2020-12-16 22:43:07 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
|
2023-03-20 17:05:26 +01:00
|
|
|
|
2023-11-14 12:15:37 +01:00
|
|
|
const auto buttonColor = [](float alpha) {
|
|
|
|
return ImU32(ImColor(ImGui::GetStyleColorVec4(ImGuiCol_ModalWindowDimBg) * ImVec4(1, 1, 1, alpha)));
|
|
|
|
};
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw the main input text box
|
2023-03-20 17:05:26 +01:00
|
|
|
ImGui::PushItemWidth(-1);
|
2023-11-14 12:15:37 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_FrameBg, buttonColor(0.5F));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, buttonColor(0.7F));
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_FrameBgActive, buttonColor(0.9F));
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0_scaled);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 4_scaled);
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (ImGui::InputText("##command_input", m_commandBuffer)) {
|
|
|
|
m_lastResults = this->getCommandResults(m_commandBuffer);
|
2023-03-20 17:05:26 +01:00
|
|
|
}
|
2024-05-04 21:30:21 +02:00
|
|
|
ImGui::SetItemKeyOwner(ImGuiKey_LeftAlt, ImGuiInputFlags_CondActive);
|
2023-12-06 16:20:06 +01:00
|
|
|
|
2023-11-14 12:15:37 +01:00
|
|
|
ImGui::PopStyleVar(2);
|
|
|
|
ImGui::PopStyleColor(3);
|
2023-03-20 17:05:26 +01:00
|
|
|
ImGui::PopItemWidth();
|
|
|
|
ImGui::SetItemDefaultFocus();
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_moveCursorToEnd) {
|
2022-07-02 21:38:58 +02:00
|
|
|
auto textState = ImGui::GetInputTextState(ImGui::GetID("##command_input"));
|
|
|
|
if (textState != nullptr) {
|
2023-03-20 22:25:27 +01:00
|
|
|
textState->Stb.cursor =
|
|
|
|
textState->Stb.select_start =
|
2023-12-19 13:10:25 +01:00
|
|
|
textState->Stb.select_end = m_commandBuffer.size();
|
2022-07-02 21:38:58 +02:00
|
|
|
}
|
2023-12-19 13:10:25 +01:00
|
|
|
m_moveCursorToEnd = false;
|
2023-12-06 16:20:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle giving back focus to the input text box
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_focusInputTextBox) {
|
2023-12-06 16:20:06 +01:00
|
|
|
ImGui::SetKeyboardFocusHere(-1);
|
|
|
|
ImGui::ActivateItemByID(ImGui::GetID("##command_input"));
|
2022-07-02 21:38:58 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_focusInputTextBox = false;
|
|
|
|
m_moveCursorToEnd = true;
|
2022-07-02 21:38:58 +02:00
|
|
|
}
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Execute the currently selected command when pressing enter
|
2023-03-20 17:05:26 +01:00
|
|
|
if (ImGui::IsItemFocused() && (ImGui::IsKeyPressed(ImGuiKey_Enter, false) || ImGui::IsKeyPressed(ImGuiKey_KeypadEnter, false))) {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_lastResults.empty()) {
|
|
|
|
auto &[displayResult, matchedCommand, callback] = m_lastResults.front();
|
2021-02-08 19:56:04 +01:00
|
|
|
callback(matchedCommand);
|
|
|
|
}
|
2020-12-16 22:43:07 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Focus the input text box when the popup is opened
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_justOpened) {
|
2021-02-08 19:56:04 +01:00
|
|
|
focusInputTextBox();
|
2023-12-19 13:10:25 +01:00
|
|
|
m_lastResults = this->getCommandResults("");
|
|
|
|
m_commandBuffer.clear();
|
|
|
|
m_justOpened = false;
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 12:15:37 +01:00
|
|
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetStyle().FramePadding.y);
|
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw the results
|
2024-07-01 22:00:08 +02:00
|
|
|
if (ImGui::BeginChild("##results", ImGui::GetContentRegionAvail(), ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &[displayResult, matchedCommand, callback] : m_lastResults) {
|
2023-06-04 00:09:45 +02:00
|
|
|
ImGui::PushTabStop(true);
|
|
|
|
ON_SCOPE_EXIT { ImGui::PopTabStop(); };
|
2023-03-20 17:05:26 +01:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Allow executing a command by clicking on it or selecting it with the keyboard and pressing enter
|
2023-03-20 17:05:26 +01:00
|
|
|
if (ImGui::Selectable(displayResult.c_str(), false, ImGuiSelectableFlags_DontClosePopups)) {
|
|
|
|
callback(matchedCommand);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ImGui::IsItemFocused() && (ImGui::IsKeyDown(ImGuiKey_Enter) || ImGui::IsKeyDown(ImGuiKey_KeypadEnter))) {
|
|
|
|
callback(matchedCommand);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
2023-11-23 09:33:47 +01:00
|
|
|
ImGui::EndChild();
|
2020-12-16 22:43:07 +01:00
|
|
|
|
|
|
|
ImGui::EndPopup();
|
2021-01-27 14:26:24 +01:00
|
|
|
} else {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_commandPaletteOpen = false;
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
std::vector<ViewCommandPalette::CommandResult> ViewCommandPalette::getCommandResults(const std::string &input) {
|
2022-09-12 21:28:02 +02:00
|
|
|
constexpr static auto MatchCommand = [](const std::string &currCommand, const std::string &commandToMatch) -> std::pair<MatchType, std::string_view> {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Check if the current input matches a command
|
|
|
|
// NoMatch means that the input doesn't match the command
|
|
|
|
// PartialMatch means that the input partially matches the command but is not a perfect match
|
|
|
|
// PerfectMatch means that the input perfectly matches the command
|
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
if (currCommand.empty()) {
|
|
|
|
return { MatchType::InfoMatch, "" };
|
2022-01-24 20:53:17 +01:00
|
|
|
} else if (currCommand.size() <= commandToMatch.size()) {
|
2020-12-16 22:43:07 +01:00
|
|
|
if (commandToMatch.starts_with(currCommand))
|
|
|
|
return { MatchType::PartialMatch, currCommand };
|
|
|
|
else
|
2022-01-24 20:53:17 +01:00
|
|
|
return { MatchType::NoMatch, {} };
|
|
|
|
} else {
|
2020-12-16 22:43:07 +01:00
|
|
|
if (currCommand.starts_with(commandToMatch))
|
|
|
|
return { MatchType::PerfectMatch, currCommand.substr(commandToMatch.length()) };
|
|
|
|
else
|
2022-01-24 20:53:17 +01:00
|
|
|
return { MatchType::NoMatch, {} };
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
std::vector<CommandResult> results;
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Loop over every registered command and check if the input matches it
|
2023-03-21 15:33:43 +01:00
|
|
|
for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::impl::getEntries()) {
|
2020-12-16 22:43:07 +01:00
|
|
|
|
2023-03-20 17:05:26 +01:00
|
|
|
auto AutoComplete = [this, currCommand = command](auto) {
|
|
|
|
this->focusInputTextBox();
|
2023-12-19 13:10:25 +01:00
|
|
|
m_commandBuffer = currCommand + " ";
|
|
|
|
m_lastResults = this->getCommandResults(currCommand);
|
2021-02-08 19:56:04 +01:00
|
|
|
};
|
2021-01-11 23:02:55 +01:00
|
|
|
|
|
|
|
if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle symbol commands
|
|
|
|
// These commands are used by entering a single symbol and then any input
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
if (auto [match, value] = MatchCommand(input, command); match != MatchType::NoMatch) {
|
2023-12-27 16:33:49 +01:00
|
|
|
if (match != MatchType::PerfectMatch) {
|
2024-08-03 11:32:17 +02:00
|
|
|
results.push_back({ hex::format("{} ({})", command, Lang(unlocalizedDescription)), "", AutoComplete });
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-12-06 16:20:06 +01:00
|
|
|
auto matchedCommand = wolv::util::trim(input.substr(command.length()));
|
2021-02-08 19:56:04 +01:00
|
|
|
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
|
|
|
}
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
|
|
|
} else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle keyword commands
|
|
|
|
// These commands are used by entering a keyword followed by a space and then any input
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
if (auto [match, value] = MatchCommand(input, command + " "); match != MatchType::NoMatch) {
|
2023-12-27 16:33:49 +01:00
|
|
|
if (match != MatchType::PerfectMatch) {
|
2024-08-03 11:32:17 +02:00
|
|
|
results.push_back({ hex::format("{} ({})", command, Lang(unlocalizedDescription)), "", AutoComplete });
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-12-06 16:20:06 +01:00
|
|
|
auto matchedCommand = wolv::util::trim(input.substr(command.length() + 1));
|
2021-02-08 19:56:04 +01:00
|
|
|
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
|
|
|
}
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// WHen a command has been identified, show the query results for that command
|
2023-03-21 15:33:43 +01:00
|
|
|
for (const auto &handler : ContentRegistry::CommandPaletteCommands::impl::getHandlers()) {
|
2023-03-20 14:11:43 +01:00
|
|
|
const auto &[type, command, queryCallback, displayCallback] = handler;
|
|
|
|
|
|
|
|
auto processedInput = input;
|
|
|
|
if (processedInput.starts_with(command))
|
2023-12-06 16:20:06 +01:00
|
|
|
processedInput = wolv::util::trim(processedInput.substr(command.length()));
|
2023-03-20 14:11:43 +01:00
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
} 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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|