From 5f5f6ac5397e31d7d1a4a956f4a80541adc18f40 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 29 Dec 2024 21:08:08 +0100 Subject: [PATCH] feat: Added option to move selection back to hex editor footer Closes #2024 --- plugins/builtin/romfs/lang/en_US.json | 1 + .../source/content/settings_entries.cpp | 1 + plugins/builtin/source/content/ui_items.cpp | 10 -------- .../source/content/views/view_hex_editor.cpp | 20 ++++++++++++++++ plugins/builtin/source/plugin_builtin.cpp | 10 ++++---- plugins/ui/include/ui/hex_editor.hpp | 5 ++++ plugins/ui/source/ui/hex_editor.cpp | 24 +++++++++++++++---- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 019e36ba9..b39bd4a52 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -493,6 +493,7 @@ "hex.builtin.setting.hex_editor.pattern_parent_highlighting": "Highlight pattern parents on hover", "hex.builtin.setting.hex_editor.paste_behaviour": "Single-Byte Paste behaviour", "hex.builtin.setting.hex_editor.sync_scrolling": "Synchronize editor scroll position", + "hex.builtin.setting.hex_editor.show_selection": "Move selection display to hex editor footer", "hex.builtin.setting.imhex": "ImHex", "hex.builtin.setting.imhex.recent_files": "Recent Files", "hex.builtin.setting.interface": "Interface", diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index b978f77c0..d79f163b1 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -834,6 +834,7 @@ namespace hex::plugin::builtin { ContentRegistry::Settings::add("hex.builtin.setting.hex_editor", "", "hex.builtin.setting.hex_editor.highlight_color", ImColor(0x80, 0x80, 0xC0, 0x60)); ContentRegistry::Settings::add("hex.builtin.setting.hex_editor", "", "hex.builtin.setting.hex_editor.sync_scrolling", false); + ContentRegistry::Settings::add("hex.builtin.setting.hex_editor", "", "hex.builtin.setting.hex_editor.show_selection", false); ContentRegistry::Settings::add("hex.builtin.setting.hex_editor", "", "hex.builtin.setting.hex_editor.byte_padding", 0, 0, 50); ContentRegistry::Settings::add("hex.builtin.setting.hex_editor", "", "hex.builtin.setting.hex_editor.char_padding", 0, 0, 50); diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index b20c6bd9c..31122b8e2 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -313,16 +313,6 @@ namespace hex::plugin::builtin { } } }); - - ContentRegistry::Interface::addFooterItem([] { - if (auto selection = ImHexApi::HexEditor::getSelection(); selection.has_value()) { - ImGuiExt::TextFormatted("0x{0:02X} - 0x{1:02X} (0x{2:02X} | {2} bytes)", - selection->getStartAddress(), - selection->getEndAddress(), - selection->getSize() - ); - } - }); } static void drawProviderContextMenu(prv::Provider *provider) { diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 5fdc56777..ba3042782 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1110,6 +1110,26 @@ namespace hex::plugin::builtin { ContentRegistry::Settings::onChange("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.char_padding", [this](const ContentRegistry::Settings::SettingsValue &value) { m_hexEditor.setCharacterCellPadding(value.get(0)); }); + + static bool showSelectionInWindowFooter = true; + ContentRegistry::Settings::onChange("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.show_selection", [this](const ContentRegistry::Settings::SettingsValue &value) { + const auto show = value.get(false); + + m_hexEditor.setShowSelectionInFooter(show); + showSelectionInWindowFooter = !show; + }); + + ContentRegistry::Interface::addFooterItem([] { + if (!showSelectionInWindowFooter) return; + + if (auto selection = ImHexApi::HexEditor::getSelection(); selection.has_value()) { + ImGuiExt::TextFormatted("0x{0:02X} - 0x{1:02X} (0x{2:02X} | {2} bytes)", + selection->getStartAddress(), + selection->getEndAddress(), + selection->getSize() + ); + } + }); } void ViewHexEditor::registerMenuItems() { diff --git a/plugins/builtin/source/plugin_builtin.cpp b/plugins/builtin/source/plugin_builtin.cpp index 10cb412c4..84ab23253 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -90,6 +90,11 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") { registerMainMenuEntries(); + addFooterItems(); + addTitleBarButtons(); + addToolbarItems(); + addGlobalUIItems(); + registerEventHandlers(); registerDataVisualizers(); registerMiniMapVisualizers(); @@ -122,10 +127,5 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") { addWindowDecoration(); createWelcomeScreen(); - addFooterItems(); - addTitleBarButtons(); - addToolbarItems(); - addGlobalUIItems(); - setupOutOfBoxExperience(); } diff --git a/plugins/ui/include/ui/hex_editor.hpp b/plugins/ui/include/ui/hex_editor.hpp index 70d8d4887..a28458e4e 100644 --- a/plugins/ui/include/ui/hex_editor.hpp +++ b/plugins/ui/include/ui/hex_editor.hpp @@ -279,6 +279,10 @@ namespace hex::ui { m_tooltipCallback = callback; } + void setShowSelectionInFooter(bool showSelection) { + m_showSelectionInFooter = showSelection; + } + [[nodiscard]] i64 getScrollPosition() { return m_scrollPosition.get(); } @@ -367,6 +371,7 @@ namespace hex::ui { bool m_showAscii = true; bool m_showCustomEncoding = true; bool m_showMiniMap = false; + bool m_showSelectionInFooter = false; int m_miniMapWidth = 5; u32 m_byteCellPadding = 0, m_characterCellPadding = 0; bool m_footerCollapsed = true; diff --git a/plugins/ui/source/ui/hex_editor.cpp b/plugins/ui/source/ui/hex_editor.cpp index 85592e3fd..fa0db4b09 100644 --- a/plugins/ui/source/ui/hex_editor.cpp +++ b/plugins/ui/source/ui/hex_editor.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -12,6 +11,8 @@ #include #include +#include + namespace hex::ui { /* Data Visualizer */ @@ -1075,6 +1076,14 @@ namespace hex::ui { } } + ImGui::SameLine(0, 15_scaled); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() - 2_scaled); + if (m_mode == Mode::Insert) { + ImGui::TextUnformatted("[ INSERT ]"); + } else { + ImGui::Dummy({}); + } + // Collapse button ImGui::TableNextColumn(); { @@ -1084,9 +1093,16 @@ namespace hex::ui { ImGui::TableNextColumn(); - ImGui::SameLine(0, 20_scaled); - if (m_mode == Mode::Insert) { - ImGui::TextUnformatted("[ INSERT ]"); + if (m_showSelectionInFooter && this->isSelectionValid()) { + const auto selection = this->getSelection(); + + ImGui::SameLine(0, 15_scaled); + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 2_scaled); + ImGuiExt::TextFormattedSelectable("0x{0:02X} - 0x{1:02X} (0x{2:02X} | {2} bytes)", + selection.getStartAddress(), + selection.getEndAddress(), + selection.getSize() + ); } if (!m_footerCollapsed) {