fix: update color of usernames & boldness of usernames on the fly (#5300)

This commit is contained in:
pajlada
2024-05-12 14:37:47 +02:00
committed by GitHub
parent febcf464fe
commit 3d5acff907
7 changed files with 120 additions and 96 deletions
+1 -4
View File
@@ -763,10 +763,7 @@ void MessageBuilder::addTextOrEmoji(const QString &string_)
auto &&textColor = this->textColor_;
if (string.startsWith('@'))
{
this->emplace<TextElement>(string, MessageElementFlag::BoldUsername,
textColor, FontStyle::ChatMediumBold);
this->emplace<TextElement>(string, MessageElementFlag::NonBoldUsername,
textColor);
this->emplace<MentionElement>(string, textColor, textColor);
}
else
{
+32
View File
@@ -703,6 +703,38 @@ Link LinkElement::getLink() const
return {Link::Url, this->linkInfo_.url()};
}
MentionElement::MentionElement(const QString &name, MessageColor fallbackColor_,
MessageColor userColor_)
: TextElement(name, {MessageElementFlag::Text, MessageElementFlag::Mention})
, fallbackColor(fallbackColor_)
, userColor(userColor_)
{
}
void MentionElement::addToContainer(MessageLayoutContainer &container,
MessageElementFlags flags)
{
if (getSettings()->colorUsernames)
{
this->color_ = this->userColor;
}
else
{
this->color_ = this->fallbackColor;
}
if (getSettings()->boldUsernames)
{
this->style_ = FontStyle::ChatMediumBold;
}
else
{
this->style_ = FontStyle::ChatMedium;
}
TextElement::addToContainer(container, flags);
}
// TIMESTAMP
TimestampElement::TimestampElement(QTime time)
: MessageElement(MessageElementFlag::Timestamp)
+40 -4
View File
@@ -133,9 +133,10 @@ enum class MessageElementFlag : int64_t {
// needed
Collapsed = (1LL << 26),
// used for dynamic bold usernames
BoldUsername = (1LL << 27),
NonBoldUsername = (1LL << 28),
// A mention of a username that isn't the author of the message
Mention = (1LL << 27),
// Unused = (1LL << 28),
// used to check if links should be lowercased
LowercaseLinks = (1LL << 29),
@@ -236,7 +237,6 @@ public:
protected:
QStringList words_;
private:
MessageColor color_;
FontStyle style_;
};
@@ -301,6 +301,42 @@ private:
QStringList original_;
};
/**
* @brief Contains a username mention.
*
* Examples of mentions:
* V
* 13:37 pajlada: hello @forsen
*
* V V
* 13:37 The moderators of this channel are: forsen, nuuls
*/
class MentionElement : public TextElement
{
public:
MentionElement(const QString &name, MessageColor fallbackColor_,
MessageColor userColor_);
~MentionElement() override = default;
MentionElement(const MentionElement &) = delete;
MentionElement(MentionElement &&) = delete;
MentionElement &operator=(const MentionElement &) = delete;
MentionElement &operator=(MentionElement &&) = delete;
void addToContainer(MessageLayoutContainer &container,
MessageElementFlags flags) override;
private:
/**
* The color of the element in case the "Colorize @usernames" is disabled
**/
MessageColor fallbackColor;
/**
* The color of the element in case the "Colorize @usernames" is enabled
**/
MessageColor userColor;
};
// contains emote data and will pick the emote based on :
// a) are images for the emote type enabled
// b) which size it wants
@@ -756,9 +756,7 @@ void MessageLayoutContainer::reorderRTL(int firstTextIndex)
const auto neutral = isNeutral(element->getText());
const auto neutralOrUsername =
neutral ||
element->getFlags().hasAny({MessageElementFlag::BoldUsername,
MessageElementFlag::NonBoldUsername});
neutral || element->getFlags().has(MessageElementFlag::Mention);
if (neutral &&
((this->first == FirstWord::RTL && !this->wasPrevReversed_) ||