From d14a5f80676a695b3a087bd1378e8057fc7ffc13 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 17 Nov 2020 13:25:16 +0100 Subject: [PATCH] Reimplement message history --- resources/qml/MessageInput.qml | 5 ++- src/timeline/InputBar.cpp | 66 ++++++++++++++++++++++++++-------- src/timeline/InputBar.h | 5 ++- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/resources/qml/MessageInput.qml b/resources/qml/MessageInput.qml index da76c6ac..236cc009 100644 --- a/resources/qml/MessageInput.qml +++ b/resources/qml/MessageInput.qml @@ -84,7 +84,10 @@ Rectangle { TimelineManager.timeline.input.send(); textArea.clear(); event.accepted = true; - } + } else if (event.modifiers == Qt.ControlModifier && event.key == Qt.Key_P) + textArea.text = TimelineManager.timeline.input.previousText(); + else if (event.modifiers == Qt.ControlModifier && event.key == Qt.Key_N) + textArea.text = TimelineManager.timeline.input.nextText(); } Connections { diff --git a/src/timeline/InputBar.cpp b/src/timeline/InputBar.cpp index 6603287b..1eaaaa64 100644 --- a/src/timeline/InputBar.cpp +++ b/src/timeline/InputBar.cpp @@ -119,39 +119,77 @@ InputBar::updateState(int selectionStart_, int selectionEnd_, int cursorPosition else startTyping(); + if (text_ != text()) { + if (history_.empty()) + history_.push_front(text_); + else + history_.front() = text_; + history_index_ = 0; + } + selectionStart = selectionStart_; selectionEnd = selectionEnd_; cursorPosition = cursorPosition_; - text = text_; +} + +QString +InputBar::text() const +{ + if (history_index_ < history_.size()) + return history_.at(history_index_); + + return ""; +} + +QString +InputBar::previousText() +{ + history_index_++; + if (history_index_ >= INPUT_HISTORY_SIZE) + history_index_ = INPUT_HISTORY_SIZE; + else if (text().isEmpty()) + history_index_--; + + return text(); +} + +QString +InputBar::nextText() +{ + history_index_--; + if (history_index_ >= INPUT_HISTORY_SIZE) + history_index_ = 0; + + return text(); } void InputBar::send() { - if (text.trimmed().isEmpty()) + if (text().trimmed().isEmpty()) return; - if (history_.size() == INPUT_HISTORY_SIZE) - history_.pop_back(); - history_.push_front(text); - history_index_ = 0; - - if (text.startsWith('/')) { - int command_end = text.indexOf(' '); + if (text().startsWith('/')) { + int command_end = text().indexOf(' '); if (command_end == -1) - command_end = text.size(); - auto name = text.mid(1, command_end - 1); - auto args = text.mid(command_end + 1); + command_end = text().size(); + auto name = text().mid(1, command_end - 1); + auto args = text().mid(command_end + 1); if (name.isEmpty() || name == "/") { message(args); } else { command(name, args); } } else { - message(text); + message(text()); } - nhlog::ui()->debug("Send: {}", text.toStdString()); + nhlog::ui()->debug("Send: {}", text().toStdString()); + + if (history_.size() == INPUT_HISTORY_SIZE) + history_.pop_back(); + history_.push_front(""); + history_index_ = 0; } void diff --git a/src/timeline/InputBar.h b/src/timeline/InputBar.h index 0e9ef592..5e66e86f 100644 --- a/src/timeline/InputBar.h +++ b/src/timeline/InputBar.h @@ -30,6 +30,10 @@ public: } public slots: + QString text() const; + QString previousText(); + QString nextText(); + void send(); void paste(bool fromMouse); void updateState(int selectionStart, int selectionEnd, int cursorPosition, QString text); @@ -84,7 +88,6 @@ private: QTimer typingRefresh_; QTimer typingTimeout_; TimelineModel *room; - QString text; std::deque history_; std::size_t history_index_ = 0; int selectionStart = 0, selectionEnd = 0, cursorPosition = 0;