Only keep one Kirigami theme instance for all users

Fetching the Kirigami Theme via attached properties is expensive. The
theme is also going to be the same for all users so it's enough to only
do it once.
This commit is contained in:
Nicolas Fella
2021-02-02 22:40:32 +01:00
parent f217bbd3c4
commit 66bcc2105a
2 changed files with 9 additions and 5 deletions

View File

@@ -10,13 +10,18 @@
#include "csapi/profile.h"
static Kirigami::PlatformTheme * s_theme = nullptr;
NeoChatUser::NeoChatUser(QString userId, Connection *connection)
: User(std::move(userId), connection)
{
m_theme = static_cast<Kirigami::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::PlatformTheme>(this, true));
Q_ASSERT(m_theme);
if (!s_theme) {
s_theme = static_cast<Kirigami::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::PlatformTheme>(this, true));
Q_ASSERT(s_theme);
}
connect(m_theme, &Kirigami::PlatformTheme::colorsChanged, this, &NeoChatUser::polishColor);
connect(s_theme, &Kirigami::PlatformTheme::colorsChanged, this, &NeoChatUser::polishColor);
polishColor();
}
QColor NeoChatUser::color()
@@ -37,5 +42,5 @@ void NeoChatUser::setColor(const QColor &color)
void NeoChatUser::polishColor()
{
// https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
setColor(QColor::fromHslF(hueF(), 1 - m_theme->alternateBackgroundColor().saturationF(), -0.7 * m_theme->alternateBackgroundColor().lightnessF() + 0.9, m_theme->textColor().alphaF()));
setColor(QColor::fromHslF(hueF(), 1 - s_theme->alternateBackgroundColor().saturationF(), -0.7 * s_theme->alternateBackgroundColor().lightnessF() + 0.9, s_theme->textColor().alphaF()));
}