From a63c0d7ceefc034152eb161fdbbe653fcef4949e Mon Sep 17 00:00:00 2001 From: John McKisson Date: Wed, 2 Oct 2024 14:34:02 -0400 Subject: [PATCH] Add: Quick clear input-line functionality (#7450) #### Brief overview of PR changes/additions This adds functionality to handle input line clearing as described in Issue https://github.com/Mudlet/Mudlet/issues/7177 A user may press DOWN (with no modifiers) to quickly clear what they have typed in the command line. The clearing functionality will only trigger if the user is in the middle of typing a command (nothing *selected* on the command line), and while not navigating through their command history. This allows history navigation and autocompletion to function as normal while allowing an additional feature of quickly clearing the command line. #### Motivation for adding to Mudlet Occasionally a player may find themself in the middle of typing a command when they wish to quickly switch to another command. Previously the options to clear the command line and start typing again involved either sending the unwanted command which could cause unwanted game lag set for the player (for example a backstab command on someone not there may result in a combat round of lag on a *miss*), or using multiple keystrokes to select all and clear with backspace. #### Other info (issues closed, discussion etc) --- src/TCommandLine.cpp | 27 ++++++++++++++++++++++----- src/TCommandLine.h | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/TCommandLine.cpp b/src/TCommandLine.cpp index 83f3843454d..6347b161816 100644 --- a/src/TCommandLine.cpp +++ b/src/TCommandLine.cpp @@ -382,7 +382,13 @@ bool TCommandLine::event(QEvent* event) #endif // If EXACTLY Down is pressed without modifiers (special case // for macOs - also sets KeyPad modifier) - historyMove(MOVE_DOWN); + bool shouldClearInput = historyMove(MOVE_DOWN); + + // If the user has pressed DOWN while in the middle of typing a command + // the command line should be cleared + if (shouldClearInput) { + clear(); + } ke->accept(); return true; } @@ -1117,11 +1123,16 @@ void TCommandLine::handleAutoCompletion() // cursor up/down: turns on autocompletion mode and cycles through all possible matches // In case nothing has been typed it cycles through the command history in // reverse order compared to cursor down. +// If the user is currently typing in the command line, a DOWN key will indicate +// that the input line should be cleared -void TCommandLine::historyMove(MoveDirection direction) +bool TCommandLine::historyMove(MoveDirection direction) { + bool shouldClearInput = false; + if (mHistoryList.empty()) { - return; + // If the history is empty, we may still want to clear the input line... + return true; } const int shift = (direction == MOVE_UP ? 1 : -1); if ((textCursor().selectedText().size() == toPlainText().size()) || (toPlainText().isEmpty()) || !mpHost->mHighlightHistory) { @@ -1140,10 +1151,16 @@ void TCommandLine::historyMove(MoveDirection direction) moveCursor(QTextCursor::End); } } else { - mAutoCompletionCount += shift; - handleAutoCompletion(); + if (direction == MOVE_DOWN && !toPlainText().isEmpty()) { + shouldClearInput = true; + } else { + mAutoCompletionCount += shift; + handleAutoCompletion(); + } } adjustHeight(); + + return shouldClearInput; } void TCommandLine::slot_clearSelection(bool yes) diff --git a/src/TCommandLine.h b/src/TCommandLine.h index 569badbd875..ea26b9cc88d 100644 --- a/src/TCommandLine.h +++ b/src/TCommandLine.h @@ -105,7 +105,7 @@ public slots: void spellCheck(); void fillSpellCheckList(QMouseEvent*, QMenu*); void handleTabCompletion(bool); - void historyMove(MoveDirection); + bool historyMove(MoveDirection); void enterCommand(QKeyEvent*); void processNormalKey(QEvent*); bool keybindingMatched(QKeyEvent*);