From 06ac7eb85ff4246509c071d52ec702b3ec07d39a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 31 Aug 2025 11:22:26 +0200 Subject: [PATCH] impr: Make Hex Editor view always be focused by default --- lib/libimhex/include/hex/ui/view.hpp | 2 + main/gui/source/window/window.cpp | 24 +++++++-- .../include/content/views/view_hex_editor.hpp | 51 ++++++++++--------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index e7fe147fb..c125a271b 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -82,10 +82,12 @@ namespace hex { */ [[nodiscard]] virtual View* getMenuItemInheritView() const { return nullptr; } + [[nodiscard]] const char *getIcon() const { return m_icon; } [[nodiscard]] const UnlocalizedString& getUnlocalizedName() const; [[nodiscard]] std::string getName() const; + [[nodiscard]] virtual bool shouldDefaultFocus() const { return false; } [[nodiscard]] virtual bool shouldStoreWindowState() const { return true; } [[nodiscard]] bool &getWindowOpenState(); diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index a52ad8115..bf412aab4 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -724,11 +724,14 @@ namespace hex { if (const auto &fullScreenView = ContentRegistry::Views::impl::getFullScreenView(); fullScreenView == nullptr) { // Loop through all views and draw them - for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { + static ImGuiWindow *nextFocusWindow = nullptr; + + for (auto &[name, view] : ContentRegistry::Views::impl::getEntries() | std::views::reverse) { ImGui::GetCurrentContext()->NextWindowData.ClearFlags(); // Draw always visible views view->drawAlwaysVisibleContent(); + view->trackViewState(); // Skip views that shouldn't be processed currently if (!view->shouldProcess()) @@ -749,15 +752,29 @@ namespace hex { ImGui::SetNextWindowClass(&windowClass); - const auto window = ImGui::FindWindowByName(view->getName().c_str()); + auto window = ImGui::FindWindowByName(view->getName().c_str()); if (window != nullptr && window->DockNode == nullptr) ImGui::SetNextWindowBgAlpha(1.0F); + if (nextFocusWindow == window && !view->didWindowJustOpen() && !ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopup)) { + ImGui::SetNextWindowFocus(); + nextFocusWindow = nullptr; + } + // Draw view view->draw(); - view->trackViewState(); + + // If the window was just opened, it wasn't found above, so try to find it again + if (window == nullptr) + window = ImGui::FindWindowByName(view->getName().c_str()); if (window != nullptr) { + if (window->Appearing) { + if (view->shouldDefaultFocus()) { + nextFocusWindow = window; + } + } + if (view->getWindowOpenState()) { // Get the currently focused view auto windowName = View::toWindowName(name); @@ -795,7 +812,6 @@ namespace hex { } } - // Handle global shortcuts for (const auto &key : m_pressedKeys) { ShortcutManager::processGlobals(io.ConfigMacOSXBehaviors ? io.KeySuper : io.KeyCtrl, io.KeyAlt, io.KeyShift, io.ConfigMacOSXBehaviors ? io.KeyCtrl : io.KeySuper, key); diff --git a/plugins/builtin/include/content/views/view_hex_editor.hpp b/plugins/builtin/include/content/views/view_hex_editor.hpp index 4467bd7e2..5d4db0656 100644 --- a/plugins/builtin/include/content/views/view_hex_editor.hpp +++ b/plugins/builtin/include/content/views/view_hex_editor.hpp @@ -16,6 +16,33 @@ namespace hex::plugin::builtin { return ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; } + bool shouldDefaultFocus() const override { return true; } + + bool isSelectionValid() const { + return m_hexEditor.isSelectionValid(); + } + + Region getSelection() const { + return m_hexEditor.getSelection(); + } + + void setSelection(const Region ®ion) { + m_hexEditor.setSelection(region); + } + + void setSelection(u64 start, u64 end) { + m_hexEditor.setSelection(start, end); + } + + void jumpToSelection() { + m_hexEditor.jumpToSelection(); + } + + void jumpIfOffScreen() { + m_hexEditor.jumpIfOffScreen(); + } + + public: class Popup { public: virtual ~Popup() = default; @@ -52,30 +79,6 @@ namespace hex::plugin::builtin { m_currPopup.reset(); } - bool isSelectionValid() const { - return m_hexEditor.isSelectionValid(); - } - - Region getSelection() const { - return m_hexEditor.getSelection(); - } - - void setSelection(const Region ®ion) { - m_hexEditor.setSelection(region); - } - - void setSelection(u64 start, u64 end) { - m_hexEditor.setSelection(start, end); - } - - void jumpToSelection() { - m_hexEditor.jumpToSelection(); - } - - void jumpIfOffScreen() { - m_hexEditor.jumpIfOffScreen(); - } - private: void drawPopup();