From c95f4d82769b9eb5836f0ffbf9f87e054c393cec Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 21 Jan 2020 20:41:09 +0100 Subject: [PATCH] Fix colors in typing display, when username contains emoji --- resources/qml/Avatar.qml | 3 ++- resources/qml/TimelineView.qml | 1 + src/Utils.cpp | 5 ++++- src/timeline/TimelineModel.cpp | 31 ++++++++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/resources/qml/Avatar.qml b/resources/qml/Avatar.qml index a53f057b..25fff7f3 100644 --- a/resources/qml/Avatar.qml +++ b/resources/qml/Avatar.qml @@ -19,7 +19,8 @@ Rectangle { Text { anchors.fill: parent - text: String.fromCodePoint(displayName.codePointAt(0)) + text: chat.model.escapeEmoji(String.fromCodePoint(displayName.codePointAt(0))) + textFormat: Text.RichText color: colors.text font.pixelSize: avatar.height/2 verticalAlignment: Text.AlignVCenter diff --git a/resources/qml/TimelineView.qml b/resources/qml/TimelineView.qml index 60a8e6c1..a93f50c0 100644 --- a/resources/qml/TimelineView.qml +++ b/resources/qml/TimelineView.qml @@ -206,6 +206,7 @@ Item { id: typingDisplay text: chat.model ? chat.model.formatTypingUsers(chat.model.typingUsers, chatFooter.color) : "" + textFormat: Text.RichText color: colors.windowText } } diff --git a/src/Utils.cpp b/src/Utils.cpp index ab8631f7..911e57fc 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -39,7 +39,7 @@ utils::replaceEmoji(const QString &body) QSettings settings; QString userFontFamily = settings.value("user/emoji_font_family", "emoji").toString(); - bool insideFontBlock = true; + bool insideFontBlock = false; for (auto &code : utf32_string) { // TODO: Be more precise here. if ((code >= 0x2600 && code <= 0x27bf) || (code >= 0x1f300 && code <= 0x1f3ff) || @@ -57,6 +57,9 @@ utils::replaceEmoji(const QString &body) } fmtBody += QString::fromUcs4(&code, 1); } + if (insideFontBlock) { + fmtBody += ""; + } return fmtBody; } diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 54e054ec..65a6e470 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -1406,9 +1406,34 @@ TimelineModel::formatTypingUsers(const std::vector &users, QColor bg) QStringList uidWithoutLast; auto formatUser = [this, bg](const QString &user_id) -> QString { - return QString("%2") - .arg(userColor(user_id, bg).name()) - .arg(escapeEmoji(displayName(user_id).toHtmlEscaped())); + auto uncoloredUsername = escapeEmoji(displayName(user_id).toHtmlEscaped()); + QString prefix = QString("").arg(userColor(user_id, bg).name()); + + // color only parts that don't have a font already specified + QString coloredUsername; + int index = 0; + do { + auto startIndex = uncoloredUsername.indexOf(" 0 ? startIndex - index : -1) + + ""; + + auto endIndex = uncoloredUsername.indexOf("", startIndex); + if (endIndex > 0) + endIndex += sizeof("") - 1; + + if (endIndex - startIndex != 0) + coloredUsername += + uncoloredUsername.midRef(startIndex, endIndex - startIndex); + + index = endIndex; + } while (index > 0 && index < uncoloredUsername.size()); + + return coloredUsername; }; for (size_t i = 0; i + 1 < users.size(); i++) {