Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search to emote popup #3404

Merged
merged 19 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- Minor: Added autocompletion for default Twitch commands starting with the dot (e.g. `.mods` which does the same as `/mods`). (#3144)
- Minor: Sorted usernames in `Users joined/parted` messages alphabetically. (#3421)
- Minor: Mod list, VIP list, and Users joined/parted messages are now searchable. (#3426)
- Minor: Add search to emote popup. (#3404)
- Minor: Messages can now be highlighted by subscriber or founder badges. (#3445)
- Bugfix: Fix Split Input hotkeys not being available when input is hidden (#3362)
- Bugfix: Fixed colored usernames sometimes not working. (#3170)
Expand Down
225 changes: 184 additions & 41 deletions src/widgets/dialogs/EmotePopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#include "widgets/Scrollbar.hpp"
#include "widgets/helper/ChannelView.hpp"

#include <QAbstractButton>
#include <QHBoxLayout>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QTabWidget>

namespace chatterino {
Expand Down Expand Up @@ -62,6 +65,24 @@ namespace {

return builder.release();
}
auto makeEmojiMessage(EmojiMap &emojiMap)
{
MessageBuilder builder;
builder->flags.set(MessageFlag::Centered);
builder->flags.set(MessageFlag::DisableCompactEmotes);

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

return builder.release();
}
void addEmoteSets(
std::vector<std::shared_ptr<TwitchAccount::EmoteSet>> sets,
Channel &globalChannel, Channel &subChannel, QString currentChannelName)
Expand Down Expand Up @@ -126,6 +147,12 @@ namespace {
}
}
}
void addEmotes(Channel &channel, const EmoteMap &map, const QString &title,
const MessageElementFlag &emoteFlag)
{
channel.addMessage(makeTitleMessage(title));
channel.addMessage(makeEmoteMessage(map, emoteFlag));
};
} // namespace

EmotePopup::EmotePopup(QWidget *parent)
Expand All @@ -137,40 +164,66 @@ EmotePopup::EmotePopup(QWidget *parent)
auto layout = new QVBoxLayout(this);
this->getLayoutContainer()->setLayout(layout);

this->notebook_ = new Notebook(this);
layout->addWidget(this->notebook_);
layout->setMargin(0);
QRegularExpression searchRegex("\\S*");
searchRegex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QValidator *searchValidator = new QRegularExpressionValidator(searchRegex);

this->search_ = new QLineEdit();
this->search_->setPlaceholderText("Search all emotes...");
this->search_->setValidator(searchValidator);
this->search_->setClearButtonEnabled(true);
this->search_->findChild<QAbstractButton *>()->setIcon(
acdvs marked this conversation as resolved.
Show resolved Hide resolved
QPixmap(":/buttons/clearSearch.png"));
layout->addWidget(this->search_);

QObject::connect(this->search_, &QLineEdit::textChanged, this,
&EmotePopup::filterEmotes);

auto clicked = [this](const Link &link) {
this->linkClicked.invoke(link);
};

auto makeView = [&](QString tabTitle) {
auto makeView = [&](QString tabTitle, bool addToNotebook = true) {
auto view = new ChannelView();

view->setOverrideFlags(MessageElementFlags{
MessageElementFlag::Default, MessageElementFlag::AlwaysShow,
MessageElementFlag::EmoteImages});
view->setEnableScrollingToBottom(false);
this->notebook_->addPage(view, tabTitle);
view->linkClicked.connect(clicked);

if (addToNotebook)
{
this->notebook_->addPage(view, tabTitle);
}

return view;
};

this->searchView_ = makeView("", false);
this->searchView_->hide();
layout->addWidget(this->searchView_);

this->notebook_ = new Notebook(this);
layout->addWidget(this->notebook_);
layout->setMargin(0);

this->subEmotesView_ = makeView("Subs");
this->channelEmotesView_ = makeView("Channel");
this->globalEmotesView_ = makeView("Global");
this->viewEmojis_ = makeView("Emojis");

this->loadEmojis();
this->loadEmojis(*this->viewEmojis_, getApp()->emotes->emojis.emojis);
this->addShortcuts();
this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated,
[this]() {
this->clearShortcuts();
this->addShortcuts();
});

this->search_->setFocus();
}

void EmotePopup::addShortcuts()
{
HotkeyController::HotkeyMap actions{
Expand Down Expand Up @@ -252,29 +305,31 @@ void EmotePopup::addShortcuts()

{"reject", nullptr},
{"accept", nullptr},
{"search", nullptr},
{"search",
[this](std::vector<QString>) -> QString {
this->search_->setFocus();
this->search_->selectAll();
return "";
}},
};

this->shortcuts_ = getApp()->hotkeys->shortcutsForCategory(
HotkeyCategory::PopupWindow, actions, this);
}

void EmotePopup::loadChannel(ChannelPtr _channel)
void EmotePopup::loadChannel(ChannelPtr channel)
{
BenchmarkGuard guard("loadChannel");

this->setWindowTitle("Emotes in #" + _channel->getName());
this->channel_ = channel;
this->twitchChannel_ = dynamic_cast<TwitchChannel *>(this->channel_.get());

auto twitchChannel = dynamic_cast<TwitchChannel *>(_channel.get());
if (twitchChannel == nullptr)
return;
this->setWindowTitle("Emotes in #" + this->channel_->getName());

auto addEmotes = [&](Channel &channel, const EmoteMap &map,
const QString &title,
const MessageElementFlag &emoteFlag) {
channel.addMessage(makeTitleMessage(title));
channel.addMessage(makeEmoteMessage(map, emoteFlag));
};
if (this->twitchChannel_ == nullptr)
{
return;
}

auto subChannel = std::make_shared<Channel>("", Channel::Type::None);
auto globalChannel = std::make_shared<Channel>("", Channel::Type::None);
Expand All @@ -283,7 +338,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
// twitch
addEmoteSets(
getApp()->accounts->twitch.getCurrent()->accessEmotes()->emoteSets,
*globalChannel, *subChannel, _channel->getName());
*globalChannel, *subChannel, this->channel_->getName());

// global
addEmotes(*globalChannel, *getApp()->twitch2->getBttvEmotes().emotes(),
Expand All @@ -292,10 +347,10 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
"FrankerFaceZ", MessageElementFlag::FfzEmote);

// channel
addEmotes(*channelChannel, *twitchChannel->bttvEmotes(), "BetterTTV",
addEmotes(*channelChannel, *this->twitchChannel_->bttvEmotes(), "BetterTTV",
MessageElementFlag::BttvEmote);
addEmotes(*channelChannel, *twitchChannel->ffzEmotes(), "FrankerFaceZ",
MessageElementFlag::FfzEmote);
addEmotes(*channelChannel, *this->twitchChannel_->ffzEmotes(),
"FrankerFaceZ", MessageElementFlag::FfzEmote);

this->globalEmotesView_->setChannel(globalChannel);
this->subEmotesView_->setChannel(subChannel);
Expand All @@ -313,29 +368,117 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
}
}

void EmotePopup::loadEmojis()
void EmotePopup::loadEmojis(ChannelView &view, EmojiMap &emojiMap)
{
auto &emojis = getApp()->emotes->emojis.emojis;

ChannelPtr emojiChannel(new Channel("", Channel::Type::None));
emojiChannel->addMessage(makeEmojiMessage(emojiMap));

view.setChannel(emojiChannel);
}

void EmotePopup::loadEmojis(Channel &channel, EmojiMap &emojiMap,
const QString &title)
{
channel.addMessage(makeTitleMessage(title));
channel.addMessage(makeEmojiMessage(emojiMap));
}

void EmotePopup::filterEmotes(const QString &searchText)
{
if (searchText.length() == 0)
{
this->notebook_->show();
this->searchView_->hide();

return;
}

auto searchChannel = std::make_shared<Channel>("", Channel::Type::None);

auto twitchEmoteSets =
getApp()->accounts->twitch.getCurrent()->accessEmotes()->emoteSets;
std::vector<std::shared_ptr<TwitchAccount::EmoteSet>> twitchGlobalEmotes{};

for (const auto &set : twitchEmoteSets)
{
auto setCopy = std::make_shared<TwitchAccount::EmoteSet>(*set);
auto setIt =
std::remove_if(setCopy->emotes.begin(), setCopy->emotes.end(),
[searchText](auto &emote) {
return !emote.name.string.contains(
searchText, Qt::CaseInsensitive);
});
setCopy->emotes.resize(std::distance(setCopy->emotes.begin(), setIt));

if (setCopy->emotes.size() > 0)
twitchGlobalEmotes.push_back(setCopy);
}

auto bttvGlobalEmotes = this->filterEmoteMap(
searchText, getApp()->twitch2->getBttvEmotes().emotes());
auto ffzGlobalEmotes = this->filterEmoteMap(
searchText, getApp()->twitch2->getFfzEmotes().emotes());
auto bttvChannelEmotes =
this->filterEmoteMap(searchText, this->twitchChannel_->bttvEmotes());
auto ffzChannelEmotes =
this->filterEmoteMap(searchText, this->twitchChannel_->ffzEmotes());

EmojiMap 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++;
}
});

// twitch
addEmoteSets(twitchGlobalEmotes, *searchChannel, *searchChannel,
this->channel_->getName());

// global
if (bttvGlobalEmotes->size() > 0)
addEmotes(*searchChannel, *bttvGlobalEmotes, "BetterTTV (Global)",
MessageElementFlag::BttvEmote);
if (ffzGlobalEmotes->size() > 0)
addEmotes(*searchChannel, *ffzGlobalEmotes, "FrankerFaceZ (Global)",
MessageElementFlag::FfzEmote);

// channel
if (bttvChannelEmotes->size() > 0)
addEmotes(*searchChannel, *bttvChannelEmotes, "BetterTTV (Channel)",
MessageElementFlag::BttvEmote);
if (ffzChannelEmotes->size() > 0)
addEmotes(*searchChannel, *ffzChannelEmotes, "FrankerFaceZ (Channel)",
MessageElementFlag::FfzEmote);

// emojis
MessageBuilder builder;
builder->flags.set(MessageFlag::Centered);
builder->flags.set(MessageFlag::DisableCompactEmotes);

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

this->viewEmojis_->setChannel(emojiChannel);
if (emojiCount > 0)
this->loadEmojis(*searchChannel, filteredEmojis, "Emojis");

this->searchView_->setChannel(searchChannel);

this->notebook_->hide();
this->searchView_->show();
}

EmoteMap *EmotePopup::filterEmoteMap(const QString &text,
std::shared_ptr<const EmoteMap> emotes)
{
auto filteredMap = new EmoteMap();

for (const auto &emote : *emotes)
{
if (emote.first.string.contains(text, Qt::CaseInsensitive))
{
filteredMap->insert(emote);
}
}

return filteredMap;
}

void EmotePopup::closeEvent(QCloseEvent *event)
Expand Down
20 changes: 19 additions & 1 deletion src/widgets/dialogs/EmotePopup.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once

#include "providers/emoji/Emojis.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "widgets/BasePopup.hpp"
#include "widgets/Notebook.hpp"

#include <pajlada/signals/signal.hpp>

#include <QLineEdit>

namespace chatterino {

struct Link;
Expand All @@ -18,7 +22,6 @@ class EmotePopup : public BasePopup
EmotePopup(QWidget *parent = nullptr);

void loadChannel(ChannelPtr channel);
void loadEmojis();

virtual void closeEvent(QCloseEvent *event) override;

Expand All @@ -29,8 +32,23 @@ class EmotePopup : public BasePopup
ChannelView *channelEmotesView_{};
ChannelView *subEmotesView_{};
ChannelView *viewEmojis_{};
/**
* @brief Visible only when the user has specified a search query into the `search_` input.
* Otherwise the `notebook_` and all other views are visible.
*/
ChannelView *searchView_{};
acdvs marked this conversation as resolved.
Show resolved Hide resolved

ChannelPtr channel_;
TwitchChannel *twitchChannel_{};

QLineEdit *search_;
acdvs marked this conversation as resolved.
Show resolved Hide resolved
Notebook *notebook_;

void loadEmojis(ChannelView &view, EmojiMap &emojiMap);
void loadEmojis(Channel &channel, EmojiMap &emojiMap, const QString &title);
void filterEmotes(const QString &text);
EmoteMap *filterEmoteMap(const QString &text,
std::shared_ptr<const EmoteMap> emotes);
void addShortcuts() override;
};

Expand Down