diff --git a/src/Cache.cpp b/src/Cache.cpp
index a9094e2d..d6a7b509 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -2059,6 +2059,7 @@ Cache::roomMembers(const std::string &room_id)
QHash<QString, QString> Cache::DisplayNames;
QHash<QString, QString> Cache::AvatarUrls;
+QHash<QString, QString> Cache::UserColors;
QString
Cache::displayName(const QString &room_id, const QString &user_id)
@@ -2090,6 +2091,16 @@ Cache::avatarUrl(const QString &room_id, const QString &user_id)
return QString();
}
+QString
+Cache::userColor(const QString &user_id)
+{
+ if (UserColors.contains(user_id)) {
+ return UserColors[user_id];
+ }
+
+ return QString();
+}
+
void
Cache::insertDisplayName(const QString &room_id,
const QString &user_id,
@@ -2119,3 +2130,21 @@ Cache::removeAvatarUrl(const QString &room_id, const QString &user_id)
auto fmt = QString("%1 %2").arg(room_id).arg(user_id);
AvatarUrls.remove(fmt);
}
+
+void
+Cache::insertUserColor(const QString &user_id, const QString &color_name)
+{
+ UserColors.insert(user_id, color_name);
+}
+
+void
+Cache::removeUserColor(const QString &user_id)
+{
+ UserColors.remove(user_id);
+}
+
+void
+Cache::clearUserColors()
+{
+ UserColors.clear();
+}
\ No newline at end of file
diff --git a/src/Cache.h b/src/Cache.h
index b730d6fc..1a133618 100644
--- a/src/Cache.h
+++ b/src/Cache.h
@@ -282,13 +282,16 @@ public:
static QHash<QString, QString> DisplayNames;
static QHash<QString, QString> AvatarUrls;
+ static QHash<QString, QString> UserColors;
static std::string displayName(const std::string &room_id, const std::string &user_id);
static QString displayName(const QString &room_id, const QString &user_id);
static QString avatarUrl(const QString &room_id, const QString &user_id);
+ static QString userColor(const QString &user_id);
static void removeDisplayName(const QString &room_id, const QString &user_id);
static void removeAvatarUrl(const QString &room_id, const QString &user_id);
+ static void removeUserColor(const QString &user_id);
static void insertDisplayName(const QString &room_id,
const QString &user_id,
@@ -296,6 +299,9 @@ public:
static void insertAvatarUrl(const QString &room_id,
const QString &user_id,
const QString &avatar_url);
+ static void insertUserColor(const QString &user_id, const QString &color_name);
+
+ static void clearUserColors();
//! Load saved data for the display names & avatars.
void populateMembers();
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index 51b23fe2..2e062c0c 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -113,7 +113,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(
userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool)));
connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, []() {
- utils::clearAuthorColors();
+ Cache::clearUserColors();
});
connect(
userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged);
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 66bc86f6..1d1dbeb2 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -514,28 +514,6 @@ utils::luminance(const QColor &col)
}
void
-utils::clearAuthorColors()
-{
- authorColors_.clear();
-}
-
-QString
-utils::getAuthorColor(const QString &author)
-{
- if (authorColors_.contains(author)) {
- return authorColors_[author];
- } else {
- return "";
- }
-}
-
-void
-utils::addAuthorColor(const QString &author, const QString &color)
-{
- authorColors_[author] = color;
-}
-
-void
utils::centerWidget(QWidget *widget, QWidget *parent)
{
auto findCenter = [childRect = widget->rect()](QRect hostRect) -> QPoint {
diff --git a/src/Utils.h b/src/Utils.h
index b6f89f73..8672e7d4 100644
--- a/src/Utils.h
+++ b/src/Utils.h
@@ -246,18 +246,6 @@ computeContrast(const qreal &one, const qreal &two);
qreal
luminance(const QColor &col);
-//! Clear the author color hashmap
-void
-clearAuthorColors();
-
-//! Get the given QString from the authorColors hash
-QString
-getAuthorColor(const QString &author);
-
-//! Put the given QString into the authorColor hash
-void
-addAuthorColor(const QString &author, const QString &color);
-
//! Center a widget in relation to another widget.
void
centerWidget(QWidget *widget, QWidget *parent);
diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp
index 103bd6d6..11341b92 100644
--- a/src/timeline/TimelineItem.cpp
+++ b/src/timeline/TimelineItem.cpp
@@ -608,15 +608,17 @@ void
TimelineItem::refreshAuthorColor()
{
if (userName_) {
- QString userColor = utils::getAuthorColor(userName_->text());
+ QString userColor = Cache::userColor(userName_->text());
if (userColor.isEmpty()) {
+ // This attempts to refresh this item since it's not drawn
+ // which allows us to get the background color accurately.
qApp->style()->polish(this);
// generate user's unique color.
auto backCol = backgroundColor().name();
userColor = utils::generateContrastingHexColor(userName_->text(), backCol);
- utils::addAuthorColor(userName_->text(), userColor);
+ Cache::insertUserColor(userName_->text(), userColor);
+ userName_->setStyleSheet("QLabel { color : " + userColor + "; }");
}
- userName_->setStyleSheet("QLabel { color : " + userColor + "; }");
}
}
// The username/timestamp is displayed along with the message body.
@@ -655,13 +657,13 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam
// TimelineItem isn't displayed. This forces the QSS to get
// loaded.
- QString userColor = utils::getAuthorColor(user_id);
+ QString userColor = Cache::userColor(user_id);
if (userColor.isEmpty()) {
qApp->style()->polish(this);
// generate user's unique color.
auto backCol = backgroundColor().name();
userColor = utils::generateContrastingHexColor(user_id, backCol);
- utils::addAuthorColor(user_id, userColor);
+ Cache::insertUserColor(user_id, userColor);
}
userName_->setStyleSheet("QLabel { color : " + userColor + "; }");
|