From 740619529c7e9ab861f53e1e8b4eff8740cad130 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 14 Jan 2021 17:01:44 +0100 Subject: [PATCH] Allow most modal popups to be closed with escape --- plugins/libimhex/source/views/view.cpp | 4 ++-- source/views/view_bookmarks.cpp | 12 +++++++----- source/views/view_command_palette.cpp | 14 +++++++++++++- source/views/view_help.cpp | 4 ++++ source/views/view_hexeditor.cpp | 16 ++++++++++++---- source/views/view_pattern.cpp | 11 ++++++----- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/plugins/libimhex/source/views/view.cpp b/plugins/libimhex/source/views/view.cpp index 90a254ae2..40fb5138e 100644 --- a/plugins/libimhex/source/views/view.cpp +++ b/plugins/libimhex/source/views/view.cpp @@ -25,7 +25,7 @@ namespace hex { } void View::drawCommonInterfaces() { - if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_NoResize)) { + if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::NewLine(); if (ImGui::BeginChild("##scrolling", ImVec2(300, 100))) { ImGui::SetCursorPosX((300 - ImGui::CalcTextSize(SharedData::errorPopupMessage.c_str(), nullptr, false).x) / 2.0F); @@ -34,7 +34,7 @@ namespace hex { } ImGui::NewLine(); ImGui::SetCursorPosX(75); - if (ImGui::Button("Okay", ImVec2(150, 20))) + if (ImGui::Button("Okay", ImVec2(150, 20)) || ImGui::IsKeyDown(ImGuiKey_Escape)) ImGui::CloseCurrentPopup(); ImGui::EndPopup(); } diff --git a/source/views/view_bookmarks.cpp b/source/views/view_bookmarks.cpp index c2ecae5ad..500e2456b 100644 --- a/source/views/view_bookmarks.cpp +++ b/source/views/view_bookmarks.cpp @@ -10,13 +10,15 @@ namespace hex { ViewBookmarks::ViewBookmarks(std::list &bookmarks) : View("Bookmarks"), m_bookmarks(bookmarks) { View::subscribeEvent(Events::AddBookmark, [this](const void *userData) { Bookmark bookmark = *reinterpret_cast(userData); - bookmark.name.resize(64); bookmark.comment.resize(0xF'FFFF); - std::memset(bookmark.name.data(), 0x00, 64); - std::strcpy(bookmark.name.data(), hex::format("Bookmark [0x%lX - 0x%lX]", - bookmark.region.address, - bookmark.region.address + bookmark.region.size - 1).c_str()); + if (bookmark.name.empty()) { + bookmark.name.resize(64); + std::memset(bookmark.name.data(), 0x00, 64); + std::strcpy(bookmark.name.data(), hex::format("Bookmark [0x%lX - 0x%lX]", + bookmark.region.address, + bookmark.region.address + bookmark.region.size - 1).c_str()); + } if (bookmark.comment.empty()) std::memset(bookmark.comment.data(), 0x00, 0xF'FFFF); diff --git a/source/views/view_command_palette.cpp b/source/views/view_command_palette.cpp index c138dfdd4..e7be81cc0 100644 --- a/source/views/view_command_palette.cpp +++ b/source/views/view_command_palette.cpp @@ -14,7 +14,7 @@ namespace hex { ContentRegistry::CommandPaletteCommands::add( ContentRegistry::CommandPaletteCommands::Type::SymbolCommand, "#", "Calculator", - [](std::string input) { + [](auto input) { MathEvaluator evaluator; evaluator.registerStandardVariables(); evaluator.registerStandardFunctions(); @@ -31,6 +31,18 @@ namespace hex { return hex::format("#%s = ???", input.data()); }); + ContentRegistry::CommandPaletteCommands::add( + ContentRegistry::CommandPaletteCommands::Type::KeywordCommand, + "/bm", "Create Bookmark", + [](auto input) { + Bookmark bookmark; + bookmark.name.resize(64); + std::strncpy(bookmark.name.data(), input.c_str(), bookmark.name.size()); + + View::postEvent(Events::AddBookmark, &bookmark); + return ""; + }); + this->m_lastResults = this->getCommandResults(""); } diff --git a/source/views/view_help.cpp b/source/views/view_help.cpp index b6731c91f..e5ff181e9 100644 --- a/source/views/view_help.cpp +++ b/source/views/view_help.cpp @@ -56,6 +56,10 @@ namespace hex { ImGui::BulletText("FreeType"); ImGui::PopStyleColor(); + + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); } } diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 0ffc30326..73d173f4a 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -192,6 +192,10 @@ namespace hex { ImGui::NewLine(); confirmButtons("Yes", "No", [] { std::exit(0); }, [] { ImGui::CloseCurrentPopup(); }); + + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); } @@ -210,6 +214,8 @@ namespace hex { if (ImGui::Button("File")) ImGui::OpenPopup("Loader Script: Open File"); + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) + ImGui::CloseCurrentPopup(); if (this->m_fileBrowser.showFileDialog("Loader Script: Open Script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(0, 0), ".py")) { this->m_loaderScriptScriptPath = this->m_fileBrowser.selected_path; @@ -243,13 +249,15 @@ namespace hex { ImGui::InputText("Address", this->m_baseAddressBuffer, 16, ImGuiInputTextFlags_CharsHexadecimal); ImGui::NewLine(); - if (ImGui::Button("Set")) { + confirmButtons("Set", "Cancel", + [this, &provider]{ provider->setBaseAddress(strtoull(this->m_baseAddressBuffer, nullptr, 16)); ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); + }, []{ + ImGui::CloseCurrentPopup(); + }); - if (ImGui::Button("Cancel")) + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) ImGui::CloseCurrentPopup(); ImGui::EndPopup(); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index f2c98aeec..ed96f0317 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -279,14 +279,15 @@ namespace hex { ImGui::Text("Do you want to load it?"); ImGui::NewLine(); - if (ImGui::Button("Yes", ImVec2(40, 20))) { + confirmButtons("Yes", "No", [this]{ this->loadPatternFile(this->m_possiblePatternFile.string()); ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); - if (ImGui::Button("No", ImVec2(40, 20))) { + }, []{ + ImGui::CloseCurrentPopup(); + }); + + if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) ImGui::CloseCurrentPopup(); - } ImGui::EndPopup(); }