1
0
mirror of synced 2024-11-28 17:40:51 +01:00

impr: Update the command palette for the modern ages

This commit is contained in:
WerWolv 2023-03-20 17:05:26 +01:00
parent 53afa6ea43
commit 17c4e405a6
2 changed files with 36 additions and 21 deletions

View File

@ -44,7 +44,7 @@ namespace hex::plugin::builtin {
bool m_justOpened = false; bool m_justOpened = false;
bool m_focusInputTextBox = false; bool m_focusInputTextBox = false;
std::vector<char> m_commandBuffer; std::string m_commandBuffer;
std::vector<CommandResult> m_lastResults; std::vector<CommandResult> m_lastResults;
std::string m_exactResult; std::string m_exactResult;

View File

@ -1,14 +1,13 @@
#include "content/views/view_command_palette.hpp" #include "content/views/view_command_palette.hpp"
#include <hex/api/content_registry.hpp> #include <hex/api/content_registry.hpp>
#include <wolv/utils/guards.hpp>
#include <cstring> #include <cstring>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
ViewCommandPalette::ViewCommandPalette() : View("hex.builtin.view.command_palette.name") { ViewCommandPalette::ViewCommandPalette() : View("hex.builtin.view.command_palette.name") {
this->m_commandBuffer = std::vector<char>(1024, 0x00);
ShortcutManager::addGlobalShortcut(CTRLCMD + SHIFT + Keys::P, [this] { ShortcutManager::addGlobalShortcut(CTRLCMD + SHIFT + Keys::P, [this] {
EventManager::post<RequestOpenPopup>("hex.builtin.view.command_palette.name"_lang); EventManager::post<RequestOpenPopup>("hex.builtin.view.command_palette.name"_lang);
this->m_commandPaletteOpen = true; this->m_commandPaletteOpen = true;
@ -28,44 +27,60 @@ namespace hex::plugin::builtin {
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
ImGui::PushItemWidth(-1);
if (ImGui::InputText("##command_input", this->m_commandBuffer)) {
this->m_lastResults = this->getCommandResults(this->m_commandBuffer);
}
ImGui::PopItemWidth();
ImGui::SetItemDefaultFocus();
if (this->m_focusInputTextBox) { if (this->m_focusInputTextBox) {
ImGui::SetKeyboardFocusHere(-1);
ImGui::ActivateItem(ImGui::GetID("##command_input"));
auto textState = ImGui::GetInputTextState(ImGui::GetID("##command_input")); auto textState = ImGui::GetInputTextState(ImGui::GetID("##command_input"));
if (textState != nullptr) { if (textState != nullptr) {
textState->Stb.cursor = strlen(this->m_commandBuffer.data()); textState->Stb.cursor = this->m_commandBuffer.size();
textState->Stb.select_start = this->m_commandBuffer.size();
textState->Stb.select_end = this->m_commandBuffer.size();
} }
ImGui::SetKeyboardFocusHere(0);
this->m_focusInputTextBox = false; this->m_focusInputTextBox = false;
} }
ImGui::PushItemWidth(-1); if (ImGui::IsItemFocused() && (ImGui::IsKeyPressed(ImGuiKey_Enter, false) || ImGui::IsKeyPressed(ImGuiKey_KeypadEnter, false))) {
if (ImGui::InputText("##command_input", this->m_commandBuffer.data(), this->m_commandBuffer.size(), ImGuiInputTextFlags_CallbackEdit | ImGuiInputTextFlags_EnterReturnsTrue,
[](ImGuiInputTextCallbackData *callbackData) -> int {
auto _this = static_cast<ViewCommandPalette *>(callbackData->UserData);
_this->m_lastResults = _this->getCommandResults(callbackData->Buf);
return 0;
}, this)) {
if (!this->m_lastResults.empty()) { if (!this->m_lastResults.empty()) {
auto &[displayResult, matchedCommand, callback] = this->m_lastResults.front(); auto &[displayResult, matchedCommand, callback] = this->m_lastResults.front();
callback(matchedCommand); callback(matchedCommand);
} }
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
} }
ImGui::PopItemWidth();
if (this->m_justOpened) { if (this->m_justOpened) {
focusInputTextBox(); focusInputTextBox();
this->m_lastResults = this->getCommandResults(""); this->m_lastResults = this->getCommandResults("");
std::memset(this->m_commandBuffer.data(), 0x00, this->m_commandBuffer.size()); this->m_commandBuffer.clear();
this->m_justOpened = false; this->m_justOpened = false;
} }
ImGui::Separator(); ImGui::Separator();
for (const auto &[displayResult, matchedCommand, callback] : this->m_lastResults) { if (ImGui::BeginChild("##results", ImGui::GetContentRegionAvail(), false, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_NavFlattened)) {
if (ImGui::Selectable(displayResult.c_str(), false, ImGuiSelectableFlags_DontClosePopups)) for (const auto &[displayResult, matchedCommand, callback] : this->m_lastResults) {;
callback(matchedCommand); ImGui::PushAllowKeyboardFocus(true);
ON_SCOPE_EXIT { ImGui::PopAllowKeyboardFocus(); };
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;
}
}
ImGui::EndChild();
} }
ImGui::EndPopup(); ImGui::EndPopup();
@ -95,9 +110,9 @@ namespace hex::plugin::builtin {
for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::getEntries()) { for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::getEntries()) {
auto AutoComplete = [this, &currCommand = command](auto) { auto AutoComplete = [this, currCommand = command](auto) {
focusInputTextBox(); this->focusInputTextBox();
std::strncpy(this->m_commandBuffer.data(), currCommand.data(), this->m_commandBuffer.size()); this->m_commandBuffer = currCommand;
this->m_lastResults = this->getCommandResults(currCommand); this->m_lastResults = this->getCommandResults(currCommand);
}; };