From 9b9c040d2da91a04010888bb623c371e520a50a9 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 20 Feb 2021 22:38:31 +0100 Subject: [PATCH] ui: Run pattern language runtime asynchronously, added compile button --- .../ImGui/include/imgui_imhex_extensions.h | 1 + .../ImGui/source/imgui_imhex_extensions.cpp | 12 ++++ include/views/view_pattern.hpp | 2 + source/views/view_pattern.cpp | 55 ++++++++++++++----- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/external/ImGui/include/imgui_imhex_extensions.h b/external/ImGui/include/imgui_imhex_extensions.h index e7db14a26..23d598cd8 100644 --- a/external/ImGui/include/imgui_imhex_extensions.h +++ b/external/ImGui/include/imgui_imhex_extensions.h @@ -12,4 +12,5 @@ namespace ImGui { void UnderlinedText(const char* label, ImColor color, const ImVec2& size_arg = ImVec2(0, 0)); + void Disabled(std::function widgets, bool disabled); } \ No newline at end of file diff --git a/external/ImGui/source/imgui_imhex_extensions.cpp b/external/ImGui/source/imgui_imhex_extensions.cpp index 62caf5b12..750953e9b 100644 --- a/external/ImGui/source/imgui_imhex_extensions.cpp +++ b/external/ImGui/source/imgui_imhex_extensions.cpp @@ -141,4 +141,16 @@ namespace ImGui { PopStyleColor(); } + void Disabled(std::function widgets, bool disabled) { + if (disabled) { + ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); + ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5F); + widgets(); + ImGui::PopStyleVar(); + ImGui::PopItemFlag(); + } else { + widgets(); + } + } + } \ No newline at end of file diff --git a/include/views/view_pattern.hpp b/include/views/view_pattern.hpp index 8331caef7..36782c5cc 100644 --- a/include/views/view_pattern.hpp +++ b/include/views/view_pattern.hpp @@ -31,6 +31,8 @@ namespace hex { std::vector &m_patternData; std::vector m_possiblePatternFiles; int m_selectedPatternFile = 0; + bool m_runAutomatically = false; + bool m_compilerRunning = false; TextEditor m_textEditor; std::vector> m_console; diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index ad0553a53..734a4a57d 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -6,6 +6,8 @@ #include +#include + namespace hex { static const TextEditor::LanguageDefinition& PatternLanguage() { @@ -213,17 +215,19 @@ namespace hex { } void ViewPattern::drawContent() { - if (ImGui::Begin("hex.view.pattern.title"_lang, &this->getWindowOpenState(), ImGuiWindowFlags_None | ImGuiWindowFlags_NoCollapse)) { + if (ImGui::Begin("hex.view.pattern.title"_lang, &this->getWindowOpenState(), ImGuiWindowFlags_None | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) { auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isAvailable()) { auto textEditorSize = ImGui::GetContentRegionAvail(); textEditorSize.y *= 4.0/5.0; + textEditorSize.y -= ImGui::GetTextLineHeightWithSpacing(); this->m_textEditor.Render("hex.view.pattern.title"_lang, textEditorSize, true); auto consoleSize = ImGui::GetContentRegionAvail(); - ImGui::PushStyleColor(ImGuiCol_ChildBg, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Background)]); + consoleSize.y -= ImGui::GetTextLineHeightWithSpacing(); + ImGui::PushStyleColor(ImGuiCol_ChildBg, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Background)]); if (ImGui::BeginChild("##console", consoleSize, true, ImGuiWindowFlags_AlwaysVerticalScrollbar)) { for (auto &[level, message] : this->m_console) { switch (level) { @@ -248,10 +252,26 @@ namespace hex { } } ImGui::EndChild(); - ImGui::PopStyleColor(1); - if (this->m_textEditor.IsTextChanged()) { + ImGui::Disabled([this]{ + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(ImColor(0x20, 0x85, 0x20))); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1); + + if (ImGui::ArrowButton("Compile", ImGuiDir_Right)) + this->parsePattern(this->m_textEditor.GetText().data()); + + ImGui::PopStyleVar(); + ImGui::PopStyleColor(); + }, this->m_compilerRunning); + + ImGui::SameLine(); + if (this->m_compilerRunning) + ImGui::Text("[%c] Compiling...", "|/-\\"[u8(ImGui::GetTime() * 20) % 4]); + else + ImGui::Checkbox("Run automatically", &this->m_runAutomatically); + + if (this->m_textEditor.IsTextChanged() && this->m_runAutomatically) { this->parsePattern(this->m_textEditor.GetText().data()); } } @@ -324,24 +344,31 @@ namespace hex { } void ViewPattern::parsePattern(char *buffer) { + this->m_compilerRunning = true; + this->clearPatternData(); this->m_textEditor.SetErrorMarkers({ }); this->m_console.clear(); View::postEvent(Events::PatternChanged); - auto result = this->m_patternLanguageRuntime->executeString(SharedData::currentProvider, buffer); + std::thread([this, buffer = std::string(buffer)] { + auto result = this->m_patternLanguageRuntime->executeString(SharedData::currentProvider, buffer); - auto error = this->m_patternLanguageRuntime->getError(); - if (error.has_value()) { - this->m_textEditor.SetErrorMarkers({ error.value() }); - } + auto error = this->m_patternLanguageRuntime->getError(); + if (error.has_value()) { + this->m_textEditor.SetErrorMarkers({ error.value() }); + } - this->m_console = this->m_patternLanguageRuntime->getConsoleLog(); + this->m_console = this->m_patternLanguageRuntime->getConsoleLog(); + + if (result.has_value()) { + this->m_patternData = std::move(result.value()); + View::doLater([]{ View::postEvent(Events::PatternChanged); }); + } + + this->m_compilerRunning = false; + }).detach(); - if (result.has_value()) { - this->m_patternData = std::move(result.value()); - View::postEvent(Events::PatternChanged); - } } } \ No newline at end of file