Skip to content

Commit

Permalink
refactor: Remove Emoji's EmojiMap with a vector (#4980)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Nov 26, 2023
1 parent 9f97398 commit 5b741a8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
- Dev: Replace `boost::optional` with `std::optional`. (#4877)
- Dev: Improve performance of selecting text. (#4889, #4911)
- Dev: Removed direct dependency on Qt 5 compatibility module. (#4906)
- Dev: Refactor `Emoji`'s EmojiMap into a vector. (#4980)
- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Changed lifetime of context menus. (#4924)
- Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926)
Expand Down
17 changes: 14 additions & 3 deletions benchmarks/src/Emojis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,20 @@ static void BM_EmojiParsing(benchmark::State &state)
};

const auto &emojiMap = emojis.getEmojis();
std::shared_ptr<EmojiData> penguin;
emojiMap.tryGet("1F427", penguin);
auto penguinEmoji = penguin->emote;
auto getEmoji = [&](auto code) {
std::shared_ptr<EmojiData> emoji;
for (const auto &e : emojis.getEmojis())
{
if (e->unifiedCode == code)
{
emoji = e;
break;
}
}
return emoji->emote;
};
auto penguinEmoji = getEmoji("1F427");
assert(penguinEmoji.get() != nullptr);

std::vector<TestCase> tests{
{
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/completion/sources/EmoteSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ namespace {
}
}

void addEmojis(std::vector<EmoteItem> &out, const EmojiMap &map)
void addEmojis(std::vector<EmoteItem> &out,
const std::vector<EmojiPtr> &map)
{
map.each([&](const QString &, const std::shared_ptr<EmojiData> &emoji) {
for (const auto &emoji : map)
{
for (auto &&shortCode : emoji->shortCodes)
{
out.push_back(
Expand All @@ -40,7 +42,7 @@ namespace {
.providerName = "Emoji",
.isEmoji = true});
}
});
};
}

} // namespace
Expand Down
15 changes: 6 additions & 9 deletions src/providers/emoji/Emojis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void Emojis::loadEmojis()

this->emojiFirstByte_[emojiData->value.at(0)].append(emojiData);

this->emojis.insert(emojiData->unifiedCode, emojiData);
this->emojis.push_back(emojiData);

if (unparsedEmoji.HasMember("skin_variations"))
{
Expand All @@ -218,8 +218,7 @@ void Emojis::loadEmojis()
this->emojiFirstByte_[variationEmojiData->value.at(0)].append(
variationEmojiData);

this->emojis.insert(variationEmojiData->unifiedCode,
variationEmojiData);
this->emojis.push_back(variationEmojiData);
}
}
}
Expand All @@ -244,10 +243,8 @@ void Emojis::sortEmojis()
void Emojis::loadEmojiSet()
{
getSettings()->emojiSet.connect([this](const auto &emojiSet) {
this->emojis.each([=](const auto &name,
std::shared_ptr<EmojiData> &emoji) {
(void)name;

for (const auto &emoji : this->emojis)
{
QString emojiSetToUse = emojiSet;
// clang-format off
static std::map<QString, QString> emojiSets = {
Expand Down Expand Up @@ -289,7 +286,7 @@ void Emojis::loadEmojiSet()
emoji->emote = std::make_shared<Emote>(Emote{
EmoteName{emoji->value}, ImageSet{Image::fromUrl({url}, 0.35)},
Tooltip{":" + emoji->shortCodes[0] + ":<br/>Emoji"}, Url{}});
});
}
});
}

Expand Down Expand Up @@ -430,7 +427,7 @@ QString Emojis::replaceShortCodes(const QString &text) const
return ret;
}

const EmojiMap &Emojis::getEmojis() const
const std::vector<EmojiPtr> &Emojis::getEmojis() const
{
return this->emojis;
}
Expand Down
12 changes: 6 additions & 6 deletions src/providers/emoji/Emojis.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include "util/ConcurrentMap.hpp"

#include <boost/variant.hpp>
#include <QMap>
#include <QRegularExpression>
#include <QVector>

#include <memory>
#include <set>
#include <vector>

Expand Down Expand Up @@ -37,7 +36,7 @@ struct EmojiData {
EmotePtr emote;
};

using EmojiMap = ConcurrentMap<QString, std::shared_ptr<EmojiData>>;
using EmojiPtr = std::shared_ptr<EmojiData>;

class IEmojis
{
Expand All @@ -46,7 +45,7 @@ class IEmojis

virtual std::vector<boost::variant<EmotePtr, QString>> parse(
const QString &text) const = 0;
virtual const EmojiMap &getEmojis() const = 0;
virtual const std::vector<EmojiPtr> &getEmojis() const = 0;
virtual const std::vector<QString> &getShortCodes() const = 0;
virtual QString replaceShortCodes(const QString &text) const = 0;
};
Expand All @@ -59,18 +58,19 @@ class Emojis : public IEmojis
std::vector<boost::variant<EmotePtr, QString>> parse(
const QString &text) const override;

EmojiMap emojis;
std::vector<QString> shortCodes;
QString replaceShortCodes(const QString &text) const override;

const EmojiMap &getEmojis() const override;
const std::vector<EmojiPtr> &getEmojis() const override;
const std::vector<QString> &getShortCodes() const override;

private:
void loadEmojis();
void sortEmojis();
void loadEmojiSet();

std::vector<EmojiPtr> emojis;

/// Emojis
QRegularExpression findShortCodesRegex_{":([-+\\w]+):"};

Expand Down
36 changes: 19 additions & 17 deletions src/widgets/dialogs/EmotePopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,22 @@ auto makeEmoteMessage(const EmoteMap &map, const MessageElementFlag &emoteFlag)
return builder.release();
}

auto makeEmojiMessage(EmojiMap &emojiMap)
auto makeEmojiMessage(const std::vector<EmojiPtr> &emojiMap)
{
MessageBuilder builder;
builder->flags.set(MessageFlag::Centered);
builder->flags.set(MessageFlag::DisableCompactEmotes);

emojiMap.each([&builder](const auto &key, const auto &value) {
(void)key; // unused

for (const auto &value : emojiMap)
{
builder
.emplace<EmoteElement>(
value->emote,
MessageElementFlags{MessageElementFlag::AlwaysShow,
MessageElementFlag::EmojiAll})
->setLink(
Link(Link::Type::InsertText, ":" + value->shortCodes[0] + ":"));
});
}

return builder.release();
}
Expand Down Expand Up @@ -165,15 +164,16 @@ void addEmotes(Channel &channel, const EmoteMap &map, const QString &title,
channel.addMessage(makeEmoteMessage(map, emoteFlag));
}

void loadEmojis(ChannelView &view, EmojiMap &emojiMap)
void loadEmojis(ChannelView &view, const std::vector<EmojiPtr> &emojiMap)
{
ChannelPtr emojiChannel(new Channel("", Channel::Type::None));
emojiChannel->addMessage(makeEmojiMessage(emojiMap));

view.setChannel(emojiChannel);
}

void loadEmojis(Channel &channel, EmojiMap &emojiMap, const QString &title)
void loadEmojis(Channel &channel, const std::vector<EmojiPtr> &emojiMap,
const QString &title)
{
channel.addMessage(makeTitleMessage(title));
channel.addMessage(makeEmojiMessage(emojiMap));
Expand Down Expand Up @@ -269,7 +269,8 @@ EmotePopup::EmotePopup(QWidget *parent)
this->globalEmotesView_ = makeView("Global");
this->viewEmojis_ = makeView("Emojis");

loadEmojis(*this->viewEmojis_, getApp()->emotes->emojis.emojis);
loadEmojis(*this->viewEmojis_,
getApp()->getEmotes()->getEmojis()->getEmojis());
this->addShortcuts();
this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated,
[this]() {
Expand Down Expand Up @@ -546,17 +547,18 @@ void EmotePopup::filterEmotes(const QString &searchText)
this->filterTwitchEmotes(searchChannel, searchText);
}

EmojiMap filteredEmojis{};
std::vector<EmojiPtr> filteredEmojis{};
int emojiCount = 0;

getApp()->emotes->emojis.emojis.each(
[&, searchText](const auto &name, std::shared_ptr<EmojiData> &emoji) {
if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive))
{
filteredEmojis.insert(name, emoji);
emojiCount++;
}
});
const auto &emojis = getIApp()->getEmotes()->getEmojis()->getEmojis();
for (const auto &emoji : emojis)
{
if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive))
{
filteredEmojis.push_back(emoji);
emojiCount++;
}
}
// emojis
if (emojiCount > 0)
{
Expand Down
9 changes: 8 additions & 1 deletion tests/src/Emojis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ TEST(Emojis, Parse)

auto getEmoji = [&](auto code) {
std::shared_ptr<EmojiData> emoji;
emojis.getEmojis().tryGet(code, emoji);
for (const auto &e : emojis.getEmojis())
{
if (e->unifiedCode == code)
{
emoji = e;
break;
}
}
return emoji->emote;
};

Expand Down

0 comments on commit 5b741a8

Please sign in to comment.