feat: main views retain focus on ui interactions (#2650)

Currently, interactions with the user interface, like changing providers, opening menus or even resizing windows, take the focus away from the main views. This PR resets focus to the child  (if view has no children then the view's window itself is used)  of the view that had focus before the interaction took place. 

It was tested by interacting with menus,  changing providers, using toolbar icons, using command palette, resizing windows or widgets of the view itself that are not children windows of the main view (e.g.  running a pattern and having focus return to the pattern editor when evaluation ends. or using the icons in the hex editor)  and also by selecting the main view itself. To clarify this last item, if you click on the view tab (not the provider tab but the view tab itself) the focus will be restored to the child that had focused before the tab was clicked 

There is no attempt to recover the active status of widgets within the window but it can be easily recovered by clicking the Tab key once. Some views, like the pattern data view, can set other views to focus depending on where they are clicked.

The implementation saves the the child sub-window in a pointer of the view and is only changed if another child is given focus. Then various UI interactions are detected with care not to change focus while the interaction occurs. The end of the interaction is detected by checking if undesired items are the ones that have focus (these undesired values only occur when the UI interaction ends) and if they are, then the focus is restored to the window that is stored in the view pointer.
This commit is contained in:
paxcut
2026-02-27 23:46:31 -07:00
committed by GitHub
parent 5333a33775
commit d20d6736b3
2 changed files with 66 additions and 3 deletions

View File

@@ -145,8 +145,9 @@ namespace hex {
* @brief A view that draws a regular window. This should be the default for most views * @brief A view that draws a regular window. This should be the default for most views
*/ */
class View::Window : public View { class View::Window : public View {
ImGuiWindow *m_focusedSubWindow;
public: public:
explicit Window(UnlocalizedString unlocalizedName, const char *icon) : View(std::move(unlocalizedName), icon) {} explicit Window(UnlocalizedString unlocalizedName, const char *icon) : View(std::move(unlocalizedName), icon), m_focusedSubWindow(nullptr) {}
/** /**
* @brief Draws help text for the view * @brief Draws help text for the view

View File

@@ -5,7 +5,7 @@
#include <hex/api/task_manager.hpp> #include <hex/api/task_manager.hpp>
#include <hex/api/tutorial_manager.hpp> #include <hex/api/tutorial_manager.hpp>
#include <hex/providers/provider.hpp> #include <imgui_internal.h>
#include <imgui.h> #include <imgui.h>
@@ -120,11 +120,73 @@ namespace hex {
void View::Window::draw(ImGuiWindowFlags extraFlags) { void View::Window::draw(ImGuiWindowFlags extraFlags) {
if (this->shouldDraw()) { if (this->shouldDraw()) {
const auto title = fmt::format("{} {}", this->getIcon(), View::toWindowName(this->getUnlocalizedName()));
const ImGuiContext& g = *ImGui::GetCurrentContext();
bool foundTopFocused = false;
ImGuiWindow *imguiFocusedWindow = nullptr;
ImGuiWindow *focusedSubWindow = nullptr;
if (g.NavWindow != nullptr) {
imguiFocusedWindow = g.NavWindow;
foundTopFocused = true;
}
for (auto focusedWindow: g.WindowsFocusOrder | std::views::reverse) {
if (focusedWindow == nullptr || !focusedWindow->WasActive)
continue;
std::string focusedWindowName = focusedWindow->Name;
if (!foundTopFocused) {
imguiFocusedWindow = focusedWindow;
foundTopFocused = true;
}
if (imguiFocusedWindow == nullptr || !focusedWindowName.contains("###hex.builtin.view."))
continue;
if (auto focusedChild = focusedWindow->NavLastChildNavWindow; focusedChild != nullptr)
focusedSubWindow = focusedChild;
else if (focusedWindow == focusedWindow->RootWindow)
focusedSubWindow = focusedWindow;
break;
}
std::string imguiFocusedWindowName = "NULL";
if (imguiFocusedWindow != nullptr)
imguiFocusedWindowName.assign(imguiFocusedWindow->Name);
std::string focusedSubWindowName;
if (focusedSubWindow != nullptr || m_focusedSubWindow != nullptr) {
focusedSubWindowName = focusedSubWindow != nullptr ? focusedSubWindow->Name : m_focusedSubWindow->Name;
if (focusedSubWindow != nullptr && m_focusedSubWindow != nullptr) {
std::string_view windowName = m_focusedSubWindow->Name;
auto stringsVector = wolv::util::splitString(focusedSubWindowName, "/");
if (stringsVector.back().contains("resize") || (focusedSubWindow == focusedSubWindow->RootWindow && windowName.starts_with(focusedSubWindowName)))
focusedSubWindowName = windowName;
else
m_focusedSubWindow = focusedSubWindow;
} else if (focusedSubWindow != nullptr)
m_focusedSubWindow = focusedSubWindow;
bool windowAlreadyFocused = focusedSubWindowName == imguiFocusedWindowName;
bool titleFocused = focusedSubWindowName.starts_with(title);
if (titleFocused && !windowAlreadyFocused) {
bool windowMayNeedFocus = focusedSubWindowName.starts_with(imguiFocusedWindowName);
std::string activeName = g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL";
if ((activeName == "NULL" || windowMayNeedFocus) && (imguiFocusedWindowName == "##MainMenuBar" || imguiFocusedWindowName.starts_with("ImHexDockSpace") || imguiFocusedWindowName.contains("###hex.builtin.view."))) {
if (m_focusedSubWindow == m_focusedSubWindow->RootWindow)
ImGui::FocusWindow(m_focusedSubWindow, ImGuiFocusRequestFlags_RestoreFocusedChild);
else
ImGui::FocusWindow(m_focusedSubWindow, ImGuiFocusRequestFlags_None);
}
}
}
if (!allowScroll()) if (!allowScroll())
extraFlags |= ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; extraFlags |= ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;
ImGui::SetNextWindowSizeConstraints(this->getMinSize(), this->getMaxSize()); ImGui::SetNextWindowSizeConstraints(this->getMinSize(), this->getMaxSize());
const auto title = fmt::format("{} {}", this->getIcon(), View::toWindowName(this->getUnlocalizedName()));
if (ImGui::Begin(title.c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse | extraFlags | this->getWindowFlags())) { if (ImGui::Begin(title.c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse | extraFlags | this->getWindowFlags())) {
TutorialManager::setLastItemInteractiveHelpPopup([this]{ this->drawHelpText(); }); TutorialManager::setLastItemInteractiveHelpPopup([this]{ this->drawHelpText(); });
this->drawContent(); this->drawContent();