2020-12-16 22:43:07 +01:00
|
|
|
#include "views/view_command_palette.hpp"
|
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-03-03 22:26:17 +01:00
|
|
|
ViewCommandPalette::ViewCommandPalette() : View("hex.view.command_palette.name") {
|
2020-12-16 22:43:07 +01:00
|
|
|
this->m_commandBuffer.resize(1024, 0x00);
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewCommandPalette::~ViewCommandPalette() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewCommandPalette::drawContent() {
|
2020-12-16 22:43:07 +01:00
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
if (!this->m_commandPaletteOpen) return;
|
2021-01-27 14:26:24 +01:00
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
ImGui::SetNextWindowPos(ImVec2(SharedData::windowPos.x + SharedData::windowSize.x * 0.5F, SharedData::windowPos.y), ImGuiCond_Always, ImVec2(0.5F,0.0F));
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::BeginPopup("hex.view.command_palette.name"_lang)) {
|
2020-12-16 22:43:07 +01:00
|
|
|
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
|
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
ImGui::PushItemWidth(-1);
|
2020-12-16 22:43:07 +01:00
|
|
|
if (ImGui::InputText("##nolabel", 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)) {
|
2021-02-08 19:56:04 +01:00
|
|
|
if (!this->m_lastResults.empty()) {
|
|
|
|
auto &[displayResult, matchedCommand, callback] = this->m_lastResults.front();
|
|
|
|
callback(matchedCommand);
|
|
|
|
}
|
2020-12-16 22:43:07 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
|
|
|
if (this->m_justOpened) {
|
2021-02-08 19:56:04 +01:00
|
|
|
focusInputTextBox();
|
|
|
|
this->m_lastResults = this->getCommandResults("");
|
|
|
|
std::memset(this->m_commandBuffer.data(), 0x00, this->m_commandBuffer.size());
|
2020-12-16 22:43:07 +01:00
|
|
|
this->m_justOpened = false;
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
if (this->m_focusInputTextBox) {
|
|
|
|
ImGui::SetKeyboardFocusHere(0);
|
|
|
|
this->m_focusInputTextBox = false;
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
for (const auto &[displayResult, matchedCommand, callback] : this->m_lastResults) {
|
|
|
|
if (ImGui::Selectable(displayResult.c_str(), false, ImGuiSelectableFlags_DontClosePopups))
|
|
|
|
callback(matchedCommand);
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
2021-01-27 14:26:24 +01:00
|
|
|
} else {
|
2021-02-08 19:56:04 +01:00
|
|
|
this->m_commandPaletteOpen = false;
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
2021-01-27 14:26:24 +01:00
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewCommandPalette::drawMenu() {
|
2020-12-16 22:43:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewCommandPalette::handleShortcut(int key, int mods) {
|
2021-03-26 21:40:35 +01:00
|
|
|
if (key == 'P' && mods == (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL)) {
|
2020-12-16 22:43:07 +01:00
|
|
|
View::doLater([this] {
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::OpenPopup("hex.view.command_palette.name"_lang);
|
2021-02-08 19:56:04 +01:00
|
|
|
this->m_commandPaletteOpen = true;
|
|
|
|
this->m_justOpened = true;
|
2020-12-16 22:43:07 +01:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
std::vector<ViewCommandPalette::CommandResult> ViewCommandPalette::getCommandResults(std::string_view input) {
|
|
|
|
constexpr auto MatchCommand = [](std::string_view currCommand, std::string_view commandToMatch) -> std::pair<MatchType, std::string_view> {
|
2020-12-16 22:43:07 +01:00
|
|
|
if (currCommand.empty()) {
|
|
|
|
return { MatchType::InfoMatch, "" };
|
|
|
|
}
|
|
|
|
else if (currCommand.size() <= commandToMatch.size()) {
|
|
|
|
if (commandToMatch.starts_with(currCommand))
|
|
|
|
return { MatchType::PartialMatch, currCommand };
|
|
|
|
else
|
|
|
|
return { MatchType::NoMatch, { } };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (currCommand.starts_with(commandToMatch))
|
|
|
|
return { MatchType::PerfectMatch, currCommand.substr(commandToMatch.length()) };
|
|
|
|
else
|
|
|
|
return { MatchType::NoMatch, { } };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
std::vector<CommandResult> results;
|
|
|
|
|
2021-02-13 15:15:32 +01:00
|
|
|
for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::getEntries()) {
|
2020-12-16 22:43:07 +01:00
|
|
|
|
2021-02-08 19:56:04 +01:00
|
|
|
auto AutoComplete = [this, &currCommand = command](auto) {
|
|
|
|
focusInputTextBox();
|
|
|
|
std::strncpy(this->m_commandBuffer.data(), currCommand.data(), this->m_commandBuffer.size());
|
|
|
|
this->m_lastResults = this->getCommandResults(currCommand);
|
|
|
|
};
|
2021-01-11 23:02:55 +01:00
|
|
|
|
|
|
|
if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) {
|
2021-02-08 19:56:04 +01:00
|
|
|
if (auto [match, value] = MatchCommand(input, command); match != MatchType::NoMatch) {
|
2021-01-11 23:02:55 +01:00
|
|
|
if (match != MatchType::PerfectMatch)
|
2021-02-13 15:15:32 +01:00
|
|
|
results.push_back({ command + " (" + LangEntry(unlocalizedDescription) + ")", "", AutoComplete });
|
2021-02-08 19:56:04 +01:00
|
|
|
else {
|
|
|
|
auto matchedCommand = input.substr(command.length()).data();
|
|
|
|
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
|
|
|
}
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
|
|
|
} else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) {
|
2021-02-08 19:56:04 +01:00
|
|
|
if (auto [match, value] = MatchCommand(input, command + " "); match != MatchType::NoMatch) {
|
2021-01-11 23:02:55 +01:00
|
|
|
if (match != MatchType::PerfectMatch)
|
2021-02-13 15:15:32 +01:00
|
|
|
results.push_back({ command + " (" + LangEntry(unlocalizedDescription) + ")", "", AutoComplete });
|
2021-02-08 19:56:04 +01:00
|
|
|
else {
|
|
|
|
auto matchedCommand = input.substr(command.length() + 1).data();
|
|
|
|
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
|
|
|
}
|
2021-01-11 23:02:55 +01:00
|
|
|
}
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
2021-01-11 23:02:55 +01:00
|
|
|
|
2020-12-16 22:43:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|