diff options
author | redsky17 <joedonofry@gmail.com> | 2019-01-20 04:43:48 +0000 |
---|---|---|
committer | redsky17 <joedonofry@gmail.com> | 2019-01-20 04:43:48 +0000 |
commit | 237c7ad114b7c3d6960fdaf712b600e715eff082 (patch) | |
tree | 77f9f2f3f8eb6256a538e653bd1ed2b615fd8625 /src | |
parent | Update user id color generation (diff) | |
download | nheko-237c7ad114b7c3d6960fdaf712b600e715eff082.tar.xz |
Author Color Fixes
Author color is now cached so that it will not be re-calculated each time a new message is posted. This cache gets cleared when the theme is changed. Additionally, the author color is now automatically refreshed when the theme is changed, fixing the issue where you had to change rooms before the colors would switch.
Diffstat (limited to 'src')
-rw-r--r-- | src/ChatPage.h | 1 | ||||
-rw-r--r-- | src/MainWindow.cpp | 6 | ||||
-rw-r--r-- | src/UserSettingsPage.cpp | 5 | ||||
-rw-r--r-- | src/UserSettingsPage.h | 1 | ||||
-rw-r--r-- | src/Utils.cpp | 24 | ||||
-rw-r--r-- | src/Utils.h | 12 | ||||
-rw-r--r-- | src/timeline/TimelineItem.cpp | 30 | ||||
-rw-r--r-- | src/timeline/TimelineItem.h | 3 |
8 files changed, 75 insertions, 7 deletions
diff --git a/src/ChatPage.h b/src/ChatPage.h index 2c728c17..492613ec 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -148,6 +148,7 @@ signals: const QImage &icon); void updateGroupsInfo(const mtx::responses::JoinedGroups &groups); + void themeChanged(); private slots: void showUnreadMessageNotification(int count); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 55dbba34..450eb71a 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -112,7 +112,11 @@ MainWindow::MainWindow(QWidget *parent) connect( userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool))); - + connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, [this]() { + utils::clearAuthorColors(); + }); + connect( + userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged); connect(trayIcon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index fdb67080..e3c0d190 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -379,7 +379,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge connect(themeCombo_, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), - [this](const QString &text) { settings_->setTheme(text.toLower()); }); + [this](const QString &text) { + settings_->setTheme(text.toLower()); + emit themeChanged(); + }); connect(scaleFactorCombo_, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 94fb816f..900f57e4 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -132,6 +132,7 @@ protected: signals: void moveBack(); void trayOptionChanged(bool value); + void themeChanged(); private slots: void importSessionKeys(); diff --git a/src/Utils.cpp b/src/Utils.cpp index b7980fc3..66bc86f6 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -15,6 +15,8 @@ using TimelineEvent = mtx::events::collections::TimelineEvents; +QHash<QString, QString> authorColors_; + QString utils::localUser() { @@ -512,6 +514,28 @@ 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 8672e7d4..b6f89f73 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -246,6 +246,18 @@ 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 bd3d73bc..103bd6d6 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -192,7 +192,8 @@ TimelineItem::init() emit eventRedacted(event_id_); }); }); - + connect( + ChatPage::instance(), &ChatPage::themeChanged, this, &TimelineItem::refreshAuthorColor); connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt); connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer); @@ -603,6 +604,21 @@ TimelineItem::generateBody(const QString &body) }); } +void +TimelineItem::refreshAuthorColor() +{ + if (userName_) { + QString userColor = utils::getAuthorColor(userName_->text()); + if (userColor.isEmpty()) { + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + userColor = utils::generateContrastingHexColor(userName_->text(), backCol); + utils::addAuthorColor(userName_->text(), userColor); + } + userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); + } +} // The username/timestamp is displayed along with the message body. void TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body) @@ -639,10 +655,14 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam // TimelineItem isn't displayed. This forces the QSS to get // loaded. - qApp->style()->polish(this); - // generate user's unique color. - auto backCol = backgroundColor().name(); - auto userColor = utils::generateContrastingHexColor(user_id, backCol); + QString userColor = utils::getAuthorColor(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); + } userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); auto filter = new UserProfileFilter(user_id, userName_); diff --git a/src/timeline/TimelineItem.h b/src/timeline/TimelineItem.h index c7f320d5..6ed3325f 100644 --- a/src/timeline/TimelineItem.h +++ b/src/timeline/TimelineItem.h @@ -227,6 +227,9 @@ signals: void eventRedacted(const QString &event_id); void redactionFailed(const QString &msg); +public slots: + void refreshAuthorColor(); + protected: void paintEvent(QPaintEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; |