1
0
mirror of synced 2025-01-11 05:42:15 +01:00

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
This commit is contained in:
paxcut 2024-12-05 11:34:51 -07:00 committed by GitHub
parent 22252d9044
commit 8acdc19be4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 10 deletions

View File

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

View File

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

View File

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