added limit to cached username colors (#2515)

This commit is contained in:
fourtf
2021-03-13 15:34:11 +01:00
committed by GitHub
parent 1090524dec
commit 8a4ffc5f5b
9 changed files with 145 additions and 6 deletions
+7 -5
View File
@@ -7,6 +7,7 @@ namespace chatterino {
ChannelChatters::ChannelChatters(Channel &channel)
: channel_(channel)
, chatterColors_(ChannelChatters::maxChatterColorCount)
{
}
@@ -72,22 +73,23 @@ void ChannelChatters::setChatters(UsernameSet &&set)
const QColor ChannelChatters::getUserColor(const QString &user)
{
const auto chatterColors = this->chatterColors_.accessConst();
const auto chatterColors = this->chatterColors_.access();
const auto search = chatterColors->find(user.toLower());
if (search == chatterColors->end())
auto lowerUser = user.toLower();
if (!chatterColors->exists(lowerUser))
{
// Returns an invalid color so we can decide not to override `textColor`
return QColor();
}
return search->second;
return QColor::fromRgb(chatterColors->get(lowerUser));
}
void ChannelChatters::setUserColor(const QString &user, const QColor &color)
{
const auto chatterColors = this->chatterColors_.access();
chatterColors->insert_or_assign(user.toLower(), color);
chatterColors->put(user.toLower(), color.rgb());
}
} // namespace chatterino
+6 -1
View File
@@ -3,6 +3,9 @@
#include "common/Channel.hpp"
#include "common/UniqueAccess.hpp"
#include "common/UsernameSet.hpp"
#include "util/QStringHash.hpp"
#include "lrucache/lrucache.hpp"
namespace chatterino {
@@ -22,11 +25,13 @@ public:
void setUserColor(const QString &user, const QColor &color);
private:
static constexpr int maxChatterColorCount = 5000;
Channel &channel_;
// maps 2 char prefix to set of names
UniqueAccess<UsernameSet> chatters_;
UniqueAccess<std::map<QString, QColor>> chatterColors_;
UniqueAccess<cache::lru_cache<QString, QRgb>> chatterColors_;
// combines multiple joins/parts into one message
UniqueAccess<QStringList> joinedUsers_;
+3
View File
@@ -124,6 +124,9 @@ AboutPage::AboutPage()
addLicense(form.getElement(), "QtKeychain",
"https://github.com/frankosterfeld/qtkeychain",
":/licenses/qtkeychain.txt");
addLicense(form.getElement(), "lrucache",
"https://github.com/lamerman/cpp-lru-cache",
":/licenses/lrucache.txt");
}
auto attributions = layout.emplace<QGroupBox>("Attributions...");