From 8acdc19be48debe7f72f0f2336891e7678896ed5 Mon Sep 17 00:00:00 2001 From: paxcut <53811119+paxcut@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:34:51 -0700 Subject: [PATCH] fix: Pattern Editor context menus being entries greyed out when they shouldn't be (#1983) Some context menu entries that were available as shortcuts were greyed out. This PR aims to fix them and improve how context menus work for the text editor and the console. The improvements include: - automatic focus on right click - automatic selection on right click. If selected text is right-clicked then copy, cut and find will use the selection, if no selection is clicked but there is text were right-clicked, then the word will be selected and used. If right-clicking empty space copy and cut will be greyed out and find will start empty. - similar functionality now exists for the console as well except the menu has fewer options due to it being read-only. - added esc to close console context menu --- .../ColorTextEditor/include/TextEditor.h | 4 +++- .../ColorTextEditor/source/TextEditor.cpp | 19 +++++++++++++++---- .../content/views/view_pattern_editor.cpp | 16 +++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/third_party/imgui/ColorTextEditor/include/TextEditor.h b/lib/third_party/imgui/ColorTextEditor/include/TextEditor.h index ec20a4840..3b8ebf7fb 100644 --- a/lib/third_party/imgui/ColorTextEditor/include/TextEditor.h +++ b/lib/third_party/imgui/ColorTextEditor/include/TextEditor.h @@ -244,7 +244,8 @@ public: Coordinates GetCursorPosition() const { return GetActualCursorCoordinates(); } void SetCursorPosition(const Coordinates& aPosition); - + bool RaiseContextMenu() { return mRaiseContextMenu; } + void ClearRaiseContextMenu() { mRaiseContextMenu = false; } inline void SetHandleMouseInputs (bool aValue){ mHandleMouseInputs = aValue;} inline bool IsHandleMouseInputsEnabled() const { return mHandleKeyboardInputs; } @@ -502,6 +503,7 @@ private: float mLastClick; bool mShowCursor; bool mShowLineNumbers; + bool mRaiseContextMenu = false; static const int sCursorBlinkInterval; static const int sCursorBlinkOnTime; diff --git a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp index 9eae2a61a..33cbe5236 100644 --- a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp +++ b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp @@ -764,10 +764,11 @@ void TextEditor::HandleMouseInputs() { if (ImGui::IsWindowHovered()) { if (!alt) { - auto click = ImGui::IsMouseClicked(0); - auto doubleClick = ImGui::IsMouseDoubleClicked(0); - auto t = ImGui::GetTime(); - auto tripleClick = click && !doubleClick && (mLastClick != -1.0f && (t - mLastClick) < io.MouseDoubleClickTime); + auto click = ImGui::IsMouseClicked(0); + auto doubleClick = ImGui::IsMouseDoubleClicked(0); + auto rightClick = ImGui::IsMouseClicked(1); + auto t = ImGui::GetTime(); + auto tripleClick = click && !doubleClick && (mLastClick != -1.0f && (t - mLastClick) < io.MouseDoubleClickTime); bool resetBlinking = false; /* Left mouse button triple click @@ -821,6 +822,16 @@ void TextEditor::HandleMouseInputs() { EnsureCursorVisible(); mLastClick = (float)ImGui::GetTime(); + } else if (rightClick) { + auto cursorPosition = ScreenPosToCoordinates(ImGui::GetMousePos()); + + if (!HasSelection() || mState.mSelectionStart > cursorPosition || cursorPosition > mState.mSelectionEnd) { + mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = cursorPosition; + mSelectionMode = SelectionMode::Normal; + SetSelection(mInteractiveStart, mInteractiveEnd, mSelectionMode); + } + ResetCursorBlinkTime(); + mRaiseContextMenu = true; } // Mouse left button dragging (=> update selection) else if (ImGui::IsMouseDragging(0) && ImGui::IsMouseDown(0)) { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index e74859f84..72d86a7db 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -351,8 +351,9 @@ namespace hex::plugin::builtin { m_textEditorHoverBox = ImRect(windowPosition,windowPosition+textEditorSize); m_consoleHoverBox = ImRect(ImVec2(windowPosition.x,windowPosition.y+textEditorSize.y),windowPosition+availableSize); TextEditor::FindReplaceHandler *findReplaceHandler = m_textEditor.GetFindReplaceHandler(); - if (ImGui::IsMouseDown(ImGuiMouseButton_Right) && ImGui::IsMouseHoveringRect(m_textEditorHoverBox.Min,m_textEditorHoverBox.Max) && !ImGui::IsMouseDragging(ImGuiMouseButton_Right)) { + if (m_textEditor.RaiseContextMenu()) { ImGui::OpenPopup("##pattern_editor_context_menu"); + m_textEditor.ClearRaiseContextMenu(); } if (ImGui::BeginPopup("##pattern_editor_context_menu")) { @@ -364,6 +365,8 @@ namespace hex::plugin::builtin { ImGui::Separator(); + if (!m_textEditor.HasSelection()) + m_textEditor.SelectWordUnderCursor(); const bool hasSelection = m_textEditor.HasSelection(); if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.edit.cut"_lang, Shortcut(CTRLCMD + Keys::X).toString().c_str(), false, hasSelection)) { m_textEditor.Cut(); @@ -386,7 +389,7 @@ namespace hex::plugin::builtin { ImGui::Separator(); // Search and replace entries - if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.find"_lang, Shortcut(CTRLCMD + Keys::F).toString().c_str(),false, m_textEditor.HasSelection())){ + if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.find"_lang, Shortcut(CTRLCMD + Keys::F).toString().c_str())){ m_replaceMode = false; m_openFindReplacePopUp = true; } @@ -398,7 +401,7 @@ namespace hex::plugin::builtin { if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.find_previous"_lang, Shortcut(SHIFT + Keys::F3).toString().c_str(),false,!findReplaceHandler->GetFindWord().empty())) findReplaceHandler->FindMatch(&m_textEditor,false); - if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.replace"_lang, Shortcut(CTRLCMD + Keys::H).toString().c_str(),false,!findReplaceHandler->GetReplaceWord().empty())) { + if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.replace"_lang, Shortcut(CTRLCMD + Keys::H).toString().c_str())) { m_replaceMode = true; m_openFindReplacePopUp = true; } @@ -921,9 +924,12 @@ namespace hex::plugin::builtin { void ViewPatternEditor::drawConsole(ImVec2 size) { auto findReplaceHandler = m_consoleEditor.GetFindReplaceHandler(); - if (ImGui::IsMouseDown(ImGuiMouseButton_Right) && ImGui::IsMouseHoveringRect(m_consoleHoverBox.Min,m_consoleHoverBox.Max) && !ImGui::IsMouseDragging(ImGuiMouseButton_Right)) { + if (m_consoleEditor.RaiseContextMenu()) { ImGui::OpenPopup("##console_context_menu"); + m_consoleEditor.ClearRaiseContextMenu(); } + if (!m_consoleEditor.HasSelection()) + m_consoleEditor.SelectWordUnderCursor(); const bool hasSelection = m_consoleEditor.HasSelection(); if (ImGui::BeginPopup("##console_context_menu")) { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.edit.copy"_lang, Shortcut(CTRLCMD + Keys::C).toString().c_str(), false, hasSelection)) { @@ -934,7 +940,7 @@ namespace hex::plugin::builtin { } ImGui::Separator(); // Search and replace entries - if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.find"_lang, Shortcut(CTRLCMD + Keys::F).toString().c_str(),false, hasSelection)) { + if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.find"_lang, Shortcut(CTRLCMD + Keys::F).toString().c_str())) { m_openFindReplacePopUp = true; m_replaceMode = false; }