impr: Make Hex Editor view always be focused by default

This commit is contained in:
WerWolv
2025-08-31 11:22:26 +02:00
parent 0e14ff5a3d
commit 06ac7eb85f
3 changed files with 49 additions and 28 deletions

View File

@@ -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();

View File

@@ -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);

View File

@@ -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 &region) {
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 &region) {
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();