From c37c53369b6feb7e6769f4351411bfd26503b7cb Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 30 Oct 2023 21:53:44 +0100 Subject: [PATCH] impr: Dock newly opened windows by default --- lib/libimhex/include/hex/ui/view.hpp | 4 ++++ lib/libimhex/source/ui/view.cpp | 12 ++++++++++++ main/gui/source/window/window.cpp | 13 ++++++++++--- plugins/builtin/source/content/main_menu_items.cpp | 8 ++++++-- .../source/content/views/view_theme_manager.cpp | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index 9d22e04f3..201749e94 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -44,6 +44,9 @@ namespace hex { [[nodiscard]] const std::string &getUnlocalizedName() const; [[nodiscard]] std::string getName() const; + [[nodiscard]] bool didWindowJustOpen(); + void setWindowJustOpened(bool state); + static void confirmButtons(const std::string &textLeft, const std::string &textRight, const std::function &leftButtonFn, const std::function &rightButtonFn); static void discardNavigationRequests(); @@ -61,6 +64,7 @@ namespace hex { std::string m_unlocalizedViewName; bool m_windowOpen = false; std::map> m_shortcuts; + bool m_windowJustOpened = false; friend class ShortcutManager; }; diff --git a/lib/libimhex/source/ui/view.cpp b/lib/libimhex/source/ui/view.cpp index 95e0b17e7..428fea6e5 100644 --- a/lib/libimhex/source/ui/view.cpp +++ b/lib/libimhex/source/ui/view.cpp @@ -50,6 +50,18 @@ namespace hex { return View::toWindowName(this->m_unlocalizedViewName); } + bool View::didWindowJustOpen() { + bool result = this->m_windowJustOpened; + + this->m_windowJustOpened = false; + + return result; + } + + void View::setWindowJustOpened(bool state) { + this->m_windowJustOpened = state; + } + void View::discardNavigationRequests() { if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard; diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 86efc7c32..96efcccec 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -727,17 +727,24 @@ namespace hex { view->drawContent(); } - // Handle per-view shortcuts if (view->getWindowOpenState() && !popupOpen) { auto window = ImGui::FindWindowByName(view->getName().c_str()); bool hasWindow = window != nullptr; bool focused = false; // Get the currently focused view - if (hasWindow && !(window->Flags & ImGuiWindowFlags_Popup)) { - ImGui::Begin(View::toWindowName(name).c_str()); + if (hasWindow && (window->Flags & ImGuiWindowFlags_Popup) != ImGuiWindowFlags_Popup) { + auto windowName = View::toWindowName(name); + ImGui::Begin(windowName.c_str()); + // Detect if the window is focused focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_NoPopupHierarchy); + + // Dock the window if it's not already docked + if (view->didWindowJustOpen() && !ImGui::IsWindowDocked()) { + ImGui::DockBuilderDockWindow(windowName.c_str(), ImHexApi::System::getMainDockSpaceId()); + } + ImGui::End(); } diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index a8d4cd6c4..ef8557b3a 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -434,8 +434,12 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.view" }, 1000, [] { for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { - if (view->hasViewMenuItemEntry()) - ImGui::MenuItem(LangEntry(view->getUnlocalizedName()), "", &view->getWindowOpenState()); + if (view->hasViewMenuItemEntry()) { + auto &state = view->getWindowOpenState(); + + if (ImGui::MenuItem(LangEntry(view->getUnlocalizedName()), "", &state)) + view->setWindowJustOpened(state); + } } ImGui::Separator(); diff --git a/plugins/builtin/source/content/views/view_theme_manager.cpp b/plugins/builtin/source/content/views/view_theme_manager.cpp index e0bd9d0a3..675992799 100644 --- a/plugins/builtin/source/content/views/view_theme_manager.cpp +++ b/plugins/builtin/source/content/views/view_theme_manager.cpp @@ -14,7 +14,7 @@ namespace hex::plugin::builtin { } void ViewThemeManager::drawContent() { - if (ImGui::Begin(View::toWindowName("hex.builtin.view.theme_manager.name").c_str(), &this->m_viewOpen, ImGuiWindowFlags_NoCollapse)) { + if (ImGui::Begin(View::toWindowName("hex.builtin.view.theme_manager.name").c_str(), &this->m_viewOpen, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking)) { ImGui::Header("hex.builtin.view.theme_manager.colors"_lang, true); // Draw theme handlers