From 3dae83e749ade62d4a4c8716283365e02d43c6a7 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 1 Apr 2018 16:16:54 +0200 Subject: [PATCH] Add an EmojiMap which is like an EmoteMap except it contains data for Emojis Fix emote popup not inserting the correct emoji value on click. It no inserts the shortcode (i.e. :ok_hand:) Fix #299 --- src/emojis.hpp | 10 ++++++++++ src/singletons/emotemanager.cpp | 26 +++++++++----------------- src/singletons/emotemanager.hpp | 5 ++--- src/widgets/emotepopup.cpp | 8 ++++---- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/emojis.hpp b/src/emojis.hpp index b845003dc..39f2be24c 100644 --- a/src/emojis.hpp +++ b/src/emojis.hpp @@ -1,5 +1,7 @@ #pragma once +#include "util/emotemap.hpp" + #include namespace chatterino { @@ -13,6 +15,14 @@ struct EmojiData { // i.e. thinking QString shortCode; + + util::EmoteData emoteData; }; +namespace util { + +using EmojiMap = ConcurrentMap; + +} // namespace util + } // namespace chatterino diff --git a/src/singletons/emotemanager.cpp b/src/singletons/emotemanager.cpp index 482697703..1c25d2339 100644 --- a/src/singletons/emotemanager.cpp +++ b/src/singletons/emotemanager.cpp @@ -226,7 +226,7 @@ util::EmoteMap &EmoteManager::getBTTVChannelEmoteFromCaches() return _bttvChannelEmoteFromCaches; } -util::EmoteMap &EmoteManager::getEmojis() +util::EmojiMap &EmoteManager::getEmojis() { return this->emojis; } @@ -277,10 +277,15 @@ void EmoteManager::loadEmojis() unicodeBytes[numUnicodeBytes++] = QString(unicodeCharacter).toUInt(nullptr, 16); } + QString url = "https://cdnjs.cloudflare.com/ajax/libs/" + "emojione/2.2.6/assets/png/" + + code + ".png"; + EmojiData emojiData{ QString::fromUcs4(unicodeBytes, numUnicodeBytes), // code, // shortCode, // + {new Image(url, 0.35, ":" + shortCode + ":", ":" + shortCode + ":
Emoji")}, }; this->emojiShortCodeToEmoji.insert(shortCode, emojiData); @@ -288,12 +293,7 @@ void EmoteManager::loadEmojis() this->emojiFirstByte[emojiData.value.at(0)].append(emojiData); - QString url = "https://cdnjs.cloudflare.com/ajax/libs/" - "emojione/2.2.6/assets/png/" + - code + ".png"; - - this->emojis.insert(code, util::EmoteData(new Image(url, 0.35, ":" + shortCode + ":", - ":" + shortCode + ":
Emoji"))); + this->emojis.insert(code, emojiData); } for (auto &p : this->emojiFirstByte) { @@ -369,17 +369,9 @@ void EmoteManager::parseEmojis(std::vector> charactersFromLastParsedEmoji)); } - QString url = "https://cdnjs.cloudflare.com/ajax/libs/" - "emojione/2.2.6/assets/png/" + - matchedEmoji.code + ".png"; - - // Create or fetch cached emoji image - auto emojiImage = this->emojis.getOrAdd(matchedEmoji.code, [&url] { - return util::EmoteData(new Image(url, 0.35, "?????????", "???????????????")); // - }); - // Push the emoji as a word to parsedWords - parsedWords.push_back(std::tuple(emojiImage, QString())); + parsedWords.push_back( + std::tuple(matchedEmoji.emoteData, QString())); lastParsedEmojiEndIndex = currentParsedEmojiEndIndex; diff --git a/src/singletons/emotemanager.hpp b/src/singletons/emotemanager.hpp index 75ef6744a..2a9365f38 100644 --- a/src/singletons/emotemanager.hpp +++ b/src/singletons/emotemanager.hpp @@ -42,7 +42,7 @@ public: util::EmoteMap &getFFZEmotes(); util::EmoteMap &getChatterinoEmotes(); util::EmoteMap &getBTTVChannelEmoteFromCaches(); - util::EmoteMap &getEmojis(); + util::EmojiMap &getEmojis(); util::ConcurrentMap &getFFZChannelEmoteFromCaches(); util::ConcurrentMap &getTwitchEmoteFromCache(); @@ -78,8 +78,7 @@ private: // Maps the first character of the emoji unicode string to a vector of possible emojis QMap> emojiFirstByte; - // url Emoji-one image - util::EmoteMap emojis; + util::EmojiMap emojis; void loadEmojis(); diff --git a/src/widgets/emotepopup.cpp b/src/widgets/emotepopup.cpp index 5cd092d5d..c4b5537a1 100644 --- a/src/widgets/emotepopup.cpp +++ b/src/widgets/emotepopup.cpp @@ -89,7 +89,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel) void EmotePopup::loadEmojis() { - util::EmoteMap &emojis = singletons::EmoteManager::getInstance().getEmojis(); + auto &emojis = singletons::EmoteManager::getInstance().getEmojis(); ChannelPtr emojiChannel(new Channel("")); @@ -105,9 +105,9 @@ void EmotePopup::loadEmojis() builder.getMessage()->flags &= Message::Centered; builder.getMessage()->flags &= Message::DisableCompactEmotes; - emojis.each([this, &builder](const QString &key, const util::EmoteData &value) { - builder.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow)) - ->setLink(Link(Link::Type::InsertText, key))); + emojis.each([this, &builder](const QString &key, const auto &value) { + builder.append((new EmoteElement(value.emoteData, MessageElement::Flags::AlwaysShow)) + ->setLink(Link(Link::Type::InsertText, ":" + value.shortCode + ":"))); }); emojiChannel->addMessage(builder.getMessage());