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.
This commit is contained in:
Carl Schwan
2021-12-25 18:31:22 +01:00
parent 26f0cd4cf4
commit ca2b5fde8e
3 changed files with 8 additions and 3 deletions

View File

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

View File

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