Skip to content

Commit 432e16e

Browse files
authored
fix: pattern editor find/replace. (#2686)
A recent commit broke the pattern editor popups for fin/replace and goto line. The problem was cause by changes to the function that returns the name of the currently focused subwindow using a function that only updates when ImHex main window losses focus. The commit was aimed at fixing evaluation of shortcuts in pattern data view and pattern editor simultaneously but missed to fix some shortcuts like cut and paste. The fix substitutes how the subwindow is first selected by using the result of the subwindow selection used by imhex to insure that menus and other ui components don't steal focus from views. The function that returns the name of the current focused subwindow was changed to use this value. This fixes both the window popups of pattern editor and all the shortcut duplications.
1 parent 8d691b2 commit 432e16e

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

lib/libimhex/include/hex/ui/view.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ namespace hex {
146146
*/
147147
class View::Window : public View {
148148
public:
149-
explicit Window(UnlocalizedString unlocalizedName, const char *icon) : View(std::move(unlocalizedName), icon), m_focusedSubWindow(nullptr) {}
149+
explicit Window(UnlocalizedString unlocalizedName, const char *icon) : View(std::move(unlocalizedName), icon) {}
150+
[[nodiscard]] ImGuiWindow *getFocusedSubWindow() const { return m_focusedSubWindow; }
150151

151152
/**
152153
* @brief Draws help text for the view
@@ -155,15 +156,15 @@ namespace hex {
155156

156157
void draw(ImGuiWindowFlags extraFlags = ImGuiWindowFlags_None) override;
157158

158-
virtual bool allowScroll() const {
159+
[[nodiscard]] virtual bool allowScroll() const {
159160
return false;
160161
}
161162

162163
private:
163164
void handleFocusRestoration();
164165

165166
private:
166-
ImGuiWindow *m_focusedSubWindow;
167+
ImGuiWindow *m_focusedSubWindow{nullptr};
167168
};
168169

169170
/**
@@ -198,7 +199,7 @@ namespace hex {
198199

199200
void draw(ImGuiWindowFlags extraFlags = ImGuiWindowFlags_None) final;
200201

201-
bool allowScroll() const final {
202+
[[nodiscard]] bool allowScroll() const final {
202203
return true;
203204
}
204205
};

plugins/builtin/source/content/views/view_pattern_editor.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ namespace hex::plugin::builtin {
6767

6868
constexpr static auto TextEditorView = "/##pattern_editor_";
6969
constexpr static auto ConsoleView = "/##console_";
70-
constexpr static auto VariablesView = "/##env_vars_";
71-
constexpr static auto SettingsView = "/##settings_";
72-
constexpr static auto VirtualFilesView = "/##virtual_file_tree_";
73-
constexpr static auto DebuggerView = "/##debugger_";
7470

7571
class ViewPatternEditor::PopupAcceptPattern : public Popup<PopupAcceptPattern> {
7672
public:
@@ -451,11 +447,9 @@ namespace hex::plugin::builtin {
451447
if (g.CurrentWindow->Appearing)
452448
return;
453449

454-
if (g.NavWindow != nullptr) {
455-
std::string name = g.NavWindow->Name;
456-
if (name.contains(TextEditorView) || name.contains(ConsoleView) || name.contains(VariablesView) || name.contains(SettingsView) || name.contains(VirtualFilesView) || name.contains(DebuggerView))
457-
m_focusedSubWindowName = name;
458-
}
450+
auto *focusedSubWindow = getFocusedSubWindow();
451+
if (focusedSubWindow != nullptr)
452+
m_focusedSubWindowName = focusedSubWindow->Name;
459453

460454
auto defaultEditorSize = ImGui::GetContentRegionAvail();
461455
defaultEditorSize.y *= 0.66F;
@@ -690,10 +684,7 @@ namespace hex::plugin::builtin {
690684
findReplaceHandler->setFindWord(textEditor, findWord);
691685
requestFocus = true;
692686
updateCount = true;
693-
if (m_focusedSubWindowName.contains(ConsoleView))
694-
canReplace = false;
695-
else if (m_focusedSubWindowName.contains(TextEditorView))
696-
canReplace = true;
687+
canReplace = m_focusedSubWindowName.contains(TextEditorView);
697688
}
698689
bool enter = ImGui::IsKeyPressed(ImGuiKey_Enter, false) || ImGui::IsKeyPressed(ImGuiKey_KeypadEnter, false);
699690
bool upArrow = ImGui::IsKeyPressed(ImGuiKey_UpArrow, false) || ImGui::IsKeyPressed(ImGuiKey_Keypad8, false);
@@ -1944,17 +1935,15 @@ namespace hex::plugin::builtin {
19441935
}
19451936

19461937
ui::TextEditor *ViewPatternEditor::getEditorFromFocusedWindow() {
1947-
if (!this->isFocused())
1948-
return nullptr;
19491938

1950-
auto provider = ImHexApi::Provider::get();
1951-
if (provider != nullptr) {
1939+
if (auto provider = ImHexApi::Provider::get(); provider != nullptr) {
19521940
if (m_focusedSubWindowName.contains(ConsoleView)) {
19531941
return &m_consoleEditor.get(provider);
19541942
}
19551943
if (m_focusedSubWindowName.contains(TextEditorView)) {
19561944
return &m_textEditor.get(provider);
19571945
}
1946+
return nullptr;
19581947
}
19591948

19601949
return nullptr;

0 commit comments

Comments
 (0)