From 768fd74361217ccee6658c63df5de4570745e9c1 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 17 Jun 2021 00:09:16 +0200 Subject: [PATCH] Keep completion behavior similar to before * Use tab to jump to the next completion item * Space to accept change --- imports/NeoChat/Component/ChatBox/ChatBar.qml | 22 ++++++++++++++----- src/chatdocumenthandler.cpp | 22 +++++++++++-------- src/chatdocumenthandler.h | 2 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/imports/NeoChat/Component/ChatBox/ChatBar.qml b/imports/NeoChat/Component/ChatBox/ChatBar.qml index 3fdc77f55..add0dc6e5 100644 --- a/imports/NeoChat/Component/ChatBox/ChatBar.qml +++ b/imports/NeoChat/Component/ChatBox/ChatBar.qml @@ -287,6 +287,7 @@ ToolBar { if (chatBar.isCompleting) { event.accepted = true completionMenu.listView.decrementCurrentIndex() + autoAppeared = true; } event.accepted = false } @@ -295,6 +296,7 @@ ToolBar { if (chatBar.isCompleting) { event.accepted = true completionMenu.listView.incrementCurrentIndex() + autoAppeared = true; } event.accepted = false } @@ -336,11 +338,18 @@ ToolBar { currentRoom.cachedInput = text autoAppeared = false; - const completionInfo = documentHandler.getAutocompletionInfo(); + const completionInfo = documentHandler.getAutocompletionInfo(isCompleting); if (completionInfo.type === ChatDocumentHandler.Ignore) { + if (completionInfo.keyword) { + // custom emojis + const idx = completionMenu.currentIndex; + completionMenu.model = Array.from(chatBar.customEmojiModel.filterModel(completionInfo.keyword)).concat(EmojiModel.filterModel(completionInfo.keyword)) + completionMenu.currentIndex = idx; + } return; } + if (completionInfo.type === ChatDocumentHandler.None) { isCompleting = false; return; @@ -359,9 +368,12 @@ ToolBar { isCompleting = false; return; } - isCompleting = true - autoAppeared = true; - completionMenu.endPosition = cursorPosition + + if (!isCompleting) { + isCompleting = true + autoAppeared = true; + completionMenu.endPosition = cursorPosition + } } } } @@ -503,8 +515,6 @@ ToolBar { function complete() { documentHandler.replaceAutoComplete(completionMenu.currentDisplayText); - completionMenu.model = null - chatBar.isCompleting = false if (completionMenu.completionType === ChatDocumentHandler.User && completionMenu.currentDisplayText.length > 0 && completionMenu.currentItem.userId.length > 0) { diff --git a/src/chatdocumenthandler.cpp b/src/chatdocumenthandler.cpp index 878faffef..8d489984a 100644 --- a/src/chatdocumenthandler.cpp +++ b/src/chatdocumenthandler.cpp @@ -123,7 +123,7 @@ void ChatDocumentHandler::setRoom(NeoChatRoom *room) Q_EMIT roomChanged(); } -QVariantMap ChatDocumentHandler::getAutocompletionInfo() +QVariantMap ChatDocumentHandler::getAutocompletionInfo(bool isAutocompleting) { QTextCursor cursor = textCursor(); @@ -133,9 +133,6 @@ QVariantMap ChatDocumentHandler::getAutocompletionInfo() {"type", AutoCompletionType::Ignore}, }; } - if (m_cursorPosition != m_autoCompleteBeginPosition && m_cursorPosition != m_autoCompleteEndPosition) { - // we moved our cursor, so cancel autocompletion - } QString text = cursor.block().text(); QString textBeforeCursor = text; @@ -167,10 +164,17 @@ QVariantMap ChatDocumentHandler::getAutocompletionInfo() }; } - return QVariantMap{ - {"keyword", autoCompletePrefix}, - {"type", AutoCompletionType::Emoji}, - }; + if (!isAutocompleting) { + return QVariantMap{ + {"keyword", autoCompletePrefix}, + {"type", AutoCompletionType::Emoji}, + }; + } else { + return QVariantMap{ + {"type", AutoCompletionType::Ignore}, + {"keyword", autoCompletePrefix}, + }; + } } return QVariantMap{ @@ -197,7 +201,7 @@ void ChatDocumentHandler::replaceAutoComplete(const QString &word) } } - cursor.insertHtml(word + " "); + cursor.insertHtml(word); m_lastState = cursor.block().text(); cursor.endEditBlock(); } diff --git a/src/chatdocumenthandler.h b/src/chatdocumenthandler.h index 215f96570..2ac926a6c 100644 --- a/src/chatdocumenthandler.h +++ b/src/chatdocumenthandler.h @@ -52,7 +52,7 @@ public: /// This function will look at the current QTextCursor and determine if there /// is the possibility to autocomplete it. - Q_INVOKABLE QVariantMap getAutocompletionInfo(); + Q_INVOKABLE QVariantMap getAutocompletionInfo(bool isAutocompleting); Q_INVOKABLE void replaceAutoComplete(const QString &word); Q_SIGNALS: