From ca2b5fde8e16e90e60980db2f42171d367cffb18 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Sat, 25 Dec 2021 18:31:22 +0100 Subject: [PATCH] Remove lag when starting user autocompletion We realistically don't need to interate over every user when typing '@', since this is not usefull for the user and create some lag. --- imports/NeoChat/Component/ChatBox/ChatBar.qml | 2 +- src/neochatroom.cpp | 7 ++++++- src/neochatroom.h | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/imports/NeoChat/Component/ChatBox/ChatBar.qml b/imports/NeoChat/Component/ChatBox/ChatBar.qml index a76a8a0d2..62e0df1e1 100644 --- a/imports/NeoChat/Component/ChatBox/ChatBar.qml +++ b/imports/NeoChat/Component/ChatBox/ChatBar.qml @@ -268,7 +268,7 @@ ToolBar { completionMenu.completionType = completionInfo.type if (completionInfo.type === ChatDocumentHandler.User) { - completionMenu.model = currentRoom.getUsers(completionInfo.keyword); + completionMenu.model = currentRoom.getUsers(completionInfo.keyword, 10); } else if (completionInfo.type === ChatDocumentHandler.Command) { completionMenu.model = CommandModel.filterModel(completionInfo.keyword); } else { diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index af4b1d61b..c3e7978ae 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -276,10 +276,11 @@ void NeoChatRoom::saveViewport(int topIndex, int bottomIndex) setLastDisplayedEvent(maxTimelineIndex() - bottomIndex); } -QVariantList NeoChatRoom::getUsers(const QString &keyword) const +QVariantList NeoChatRoom::getUsers(const QString &keyword, int limit) const { const auto userList = users(); QVariantList matchedList; + int count = 0; for (const auto u : userList) { if (u->displayname(this).contains(keyword, Qt::CaseInsensitive)) { NeoChatUser user(u->id(), u->connection()); @@ -289,6 +290,10 @@ QVariantList NeoChatRoom::getUsers(const QString &keyword) const {QStringLiteral("color"), user.color()}}; matchedList.append(QVariant::fromValue(userVariant)); + count++; + if (count == limit) { // -1 is infinite + break; + } } } diff --git a/src/neochatroom.h b/src/neochatroom.h index 9464ea9fc..6bbc00696 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -102,7 +102,7 @@ public: Q_INVOKABLE [[nodiscard]] int savedBottomVisibleIndex() const; Q_INVOKABLE void saveViewport(int topIndex, int bottomIndex); - Q_INVOKABLE [[nodiscard]] QVariantList getUsers(const QString &keyword) const; + Q_INVOKABLE [[nodiscard]] QVariantList getUsers(const QString &keyword, int limit = -1) const; Q_INVOKABLE [[nodiscard]] QVariantMap getUser(const QString &userID) const; Q_INVOKABLE QUrl urlToMxcUrl(const QUrl &mxcUrl);