Fixed command palette and added /web command

This commit is contained in:
WerWolv
2021-02-08 19:56:04 +01:00
parent 1b98afe37d
commit 179e222919
8 changed files with 87 additions and 42 deletions

View File

@@ -6,7 +6,6 @@ namespace hex {
ViewCommandPalette::ViewCommandPalette() : View("Command Palette") {
this->m_commandBuffer.resize(1024, 0x00);
this->m_lastResults = this->getCommandResults("");
}
ViewCommandPalette::~ViewCommandPalette() {
@@ -15,17 +14,14 @@ namespace hex {
void ViewCommandPalette::drawContent() {
if (!this->getWindowOpenState()) return;
if (!this->m_commandPaletteOpen) return;
auto windowPos = SharedData::windowPos;
auto windowSize = SharedData::windowSize;
auto paletteSize = this->getMinSize();
ImGui::SetNextWindowPos(ImVec2(windowPos.x + (windowSize.x - paletteSize.x) / 2.0F, windowPos.y), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(SharedData::windowPos.x + SharedData::windowSize.x * 0.5F, SharedData::windowPos.y), ImGuiCond_Always, ImVec2(0.5F,0.0F));
if (ImGui::BeginPopup("Command Palette")) {
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
ImGui::CloseCurrentPopup();
ImGui::PushItemWidth(paletteSize.x - ImGui::GetStyle().WindowPadding.x * 2);
ImGui::PushItemWidth(-1);
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);
@@ -33,24 +29,36 @@ namespace hex {
return 0;
}, this)) {
if (!this->m_lastResults.empty()) {
auto &[displayResult, matchedCommand, callback] = this->m_lastResults.front();
callback(matchedCommand);
}
ImGui::CloseCurrentPopup();
}
ImGui::PopItemWidth();
if (this->m_justOpened) {
ImGui::SetKeyboardFocusHere(0);
focusInputTextBox();
this->m_lastResults = this->getCommandResults("");
std::memset(this->m_commandBuffer.data(), 0x00, this->m_commandBuffer.size());
this->m_justOpened = false;
}
if (this->m_focusInputTextBox) {
ImGui::SetKeyboardFocusHere(0);
this->m_focusInputTextBox = false;
}
ImGui::Separator();
for (const auto &result : this->m_lastResults) {
ImGui::TextUnformatted(result.c_str());
for (const auto &[displayResult, matchedCommand, callback] : this->m_lastResults) {
if (ImGui::Selectable(displayResult.c_str(), false, ImGuiSelectableFlags_DontClosePopups))
callback(matchedCommand);
}
ImGui::EndPopup();
} else {
this->getWindowOpenState() = false;
this->m_commandPaletteOpen = false;
}
}
@@ -62,9 +70,9 @@ namespace hex {
bool ViewCommandPalette::handleShortcut(int key, int mods) {
if (key == GLFW_KEY_P && mods == (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL)) {
View::doLater([this] {
this->m_justOpened = true;
ImGui::OpenPopup("Command Palette");
this->getWindowOpenState() = true;
this->m_commandPaletteOpen = true;
this->m_justOpened = true;
});
return true;
}
@@ -72,15 +80,8 @@ namespace hex {
return false;
}
enum class MatchType {
NoMatch,
InfoMatch,
PartialMatch,
PerfectMatch
};
std::vector<std::string> ViewCommandPalette::getCommandResults(std::string_view input) {
constexpr auto matchCommand = [](std::string_view currCommand, std::string_view commandToMatch) -> std::pair<MatchType, std::string_view> {
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> {
if (currCommand.empty()) {
return { MatchType::InfoMatch, "" };
}
@@ -98,23 +99,33 @@ namespace hex {
}
};
std::vector<std::string> results;
std::vector<CommandResult> results;
for (const auto &[type, command, description, callback] : ContentRegistry::CommandPaletteCommands::getEntries()) {
for (const auto &[type, command, description, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::getEntries()) {
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);
};
if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) {
if (auto [match, value] = matchCommand(input, command); match != MatchType::NoMatch) {
if (auto [match, value] = MatchCommand(input, command); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back(command + " (" + description + ")");
else
results.emplace_back(callback(input.substr(command.length()).data()));
results.push_back({ command + " (" + description + ")", "", AutoComplete });
else {
auto matchedCommand = input.substr(command.length()).data();
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
}
}
} else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) {
if (auto [match, value] = matchCommand(input, command + " "); match != MatchType::NoMatch) {
if (auto [match, value] = MatchCommand(input, command + " "); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back(command + " (" + description + ")");
else
results.emplace_back(callback(input.substr(command.length() + 1).data()));
results.push_back({ command + " (" + description + ")", "", AutoComplete });
else {
auto matchedCommand = input.substr(command.length() + 1).data();
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
}
}
}