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.
This commit is contained in:
redsky17 2019-01-20 04:43:48 +00:00
parent b3f7c13e2f
commit 237c7ad114
8 changed files with 75 additions and 7 deletions

View File

@ -148,6 +148,7 @@ signals:
const QImage &icon);
void updateGroupsInfo(const mtx::responses::JoinedGroups &groups);
void themeChanged();
private slots:
void showUnreadMessageNotification(int count);

View File

@ -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,

View File

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

View File

@ -132,6 +132,7 @@ protected:
signals:
void moveBack();
void trayOptionChanged(bool value);
void themeChanged();
private slots:
void importSessionKeys();

View File

@ -15,6 +15,8 @@
using TimelineEvent = mtx::events::collections::TimelineEvents;
QHash<QString, QString> authorColors_;
QString
utils::localUser()
{
@ -511,6 +513,28 @@ utils::luminance(const QColor &col)
return lum;
}
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)
{

View File

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

View File

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

View File

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