From d6e1a6a45b41f7055f57c8ce1749b42c5d27cc4a Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 25 Nov 2020 21:21:12 +0300 Subject: [PATCH] Improve color-coding for user disambiguation See also: https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal --- src/neochatuser.cpp | 28 +++++++++++++++++++++++++++- src/neochatuser.h | 23 ++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/neochatuser.cpp b/src/neochatuser.cpp index c07484ca1..0d8d0863b 100644 --- a/src/neochatuser.cpp +++ b/src/neochatuser.cpp @@ -5,9 +5,35 @@ */ #include "neochatuser.h" +#include // Kirigami + #include "csapi/profile.h" +NeoChatUser::NeoChatUser(QString userId, Connection *connection) + : User(userId, connection) +{ + m_theme = static_cast(qmlAttachedPropertiesObject(this, true)); + Q_ASSERT(m_theme); + + connect(m_theme, &Kirigami::PlatformTheme::colorsChanged, this, &NeoChatUser::polishColor); +} + QColor NeoChatUser::color() { - return QColor::fromHslF(hueF(), 0.7, 0.5, 1); + return m_color; +} + +void NeoChatUser::setColor(QColor color) +{ + if (m_color == color) + return; + + m_color = color; + emit colorChanged(m_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())); } diff --git a/src/neochatuser.h b/src/neochatuser.h index 1773d6f01..d5ead65a3 100644 --- a/src/neochatuser.h +++ b/src/neochatuser.h @@ -10,17 +10,30 @@ #include "room.h" #include "user.h" +namespace Kirigami +{ +class PlatformTheme; +} + using namespace Quotient; class NeoChatUser : public User { Q_OBJECT - Q_PROPERTY(QColor color READ color CONSTANT) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) public: - NeoChatUser(QString userId, Connection *connection) - : User(userId, connection) - { - } + NeoChatUser(QString userId, Connection *connection); +public Q_SLOTS: QColor color(); + void setColor(QColor color); + +Q_SIGNALS: + void colorChanged(QColor color); + +private: + Kirigami::PlatformTheme *m_theme = nullptr; + QColor m_color; + + void polishColor(); };