2018-06-26 14:09:39 +02:00
|
|
|
#include "EmotePopup.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2020-02-15 15:49:06 +01:00
|
|
|
#include "common/CompletionModel.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2018-08-11 14:20:53 +02:00
|
|
|
#include "debug/Benchmark.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "messages/Message.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "messages/MessageBuilder.hpp"
|
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
2021-07-13 13:23:50 +02:00
|
|
|
#include "providers/twitch/TwitchIrcServer.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
2020-04-13 13:15:51 +02:00
|
|
|
#include "singletons/WindowManager.hpp"
|
2020-01-05 09:45:10 +01:00
|
|
|
#include "util/Shortcut.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "widgets/Notebook.hpp"
|
2021-04-10 14:11:26 +02:00
|
|
|
#include "widgets/Scrollbar.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "widgets/helper/ChannelView.hpp"
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include <QHBoxLayout>
|
2018-05-23 13:54:42 +02:00
|
|
|
#include <QShortcut>
|
2018-04-27 22:11:19 +02:00
|
|
|
#include <QTabWidget>
|
|
|
|
|
2017-09-15 17:23:49 +02:00
|
|
|
namespace chatterino {
|
2018-08-11 14:20:53 +02:00
|
|
|
namespace {
|
2018-08-15 22:46:20 +02:00
|
|
|
auto makeTitleMessage(const QString &title)
|
|
|
|
{
|
|
|
|
MessageBuilder builder;
|
|
|
|
builder.emplace<TextElement>(title, MessageElementFlag::Text);
|
|
|
|
builder->flags.set(MessageFlag::Centered);
|
|
|
|
return builder.release();
|
2018-08-11 14:20:53 +02:00
|
|
|
}
|
2021-04-24 17:43:15 +02:00
|
|
|
auto makeEmoteMessage(const EmoteMap &map,
|
|
|
|
const MessageElementFlag &emoteFlag)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
MessageBuilder builder;
|
|
|
|
builder->flags.set(MessageFlag::Centered);
|
|
|
|
builder->flags.set(MessageFlag::DisableCompactEmotes);
|
|
|
|
|
2021-04-24 17:43:15 +02:00
|
|
|
if (map.empty())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-09-04 20:09:06 +02:00
|
|
|
builder.emplace<TextElement>("no emotes available",
|
|
|
|
MessageElementFlag::Text,
|
|
|
|
MessageColor::System);
|
2021-04-24 17:43:15 +02:00
|
|
|
return builder.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<EmoteName, EmotePtr>> vec(map.begin(), map.end());
|
|
|
|
std::sort(vec.begin(), vec.end(),
|
|
|
|
[](const std::pair<EmoteName, EmotePtr> &l,
|
|
|
|
const std::pair<EmoteName, EmotePtr> &r) {
|
|
|
|
return CompletionModel::compareStrings(l.first.string,
|
|
|
|
r.first.string);
|
|
|
|
});
|
|
|
|
for (const auto &emote : vec)
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.emplace<EmoteElement>(
|
|
|
|
emote.second,
|
|
|
|
MessageElementFlags{MessageElementFlag::AlwaysShow,
|
|
|
|
emoteFlag})
|
|
|
|
->setLink(Link(Link::InsertText, emote.first.string));
|
2018-08-11 14:20:53 +02:00
|
|
|
}
|
2017-12-28 00:48:21 +01:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
return builder.release();
|
|
|
|
}
|
|
|
|
void addEmoteSets(
|
|
|
|
std::vector<std::shared_ptr<TwitchAccount::EmoteSet>> sets,
|
2020-10-11 12:31:00 +02:00
|
|
|
Channel &globalChannel, Channel &subChannel, QString currentChannelName)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
2018-10-11 14:56:10 +02:00
|
|
|
QMap<QString, QPair<bool, std::vector<MessagePtr>>> mapOfSets;
|
2018-08-15 22:46:20 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &set : sets)
|
|
|
|
{
|
2021-07-11 11:12:49 +02:00
|
|
|
// Some emotes (e.g. follower ones) are only available in their origin channel
|
|
|
|
if (set->local && currentChannelName != set->channelName)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
// TITLE
|
2018-10-11 14:56:10 +02:00
|
|
|
auto channelName = set->channelName;
|
2021-06-24 23:23:31 +02:00
|
|
|
auto text = set->text.isEmpty() ? "Twitch" : set->text;
|
2018-08-15 22:46:20 +02:00
|
|
|
|
|
|
|
// EMOTES
|
|
|
|
MessageBuilder builder;
|
|
|
|
builder->flags.set(MessageFlag::Centered);
|
|
|
|
builder->flags.set(MessageFlag::DisableCompactEmotes);
|
|
|
|
|
2018-10-11 14:56:10 +02:00
|
|
|
// If value of map is empty, create init pair and add title.
|
2018-10-21 13:43:02 +02:00
|
|
|
if (mapOfSets.find(channelName) == mapOfSets.end())
|
|
|
|
{
|
2018-10-11 14:56:10 +02:00
|
|
|
std::vector<MessagePtr> b;
|
|
|
|
b.push_back(makeTitleMessage(text));
|
|
|
|
mapOfSets[channelName] = qMakePair(set->key == "0", b);
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &emote : set->emotes)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
builder
|
|
|
|
.emplace<EmoteElement>(
|
|
|
|
getApp()->emotes->twitch.getOrCreateEmote(emote.id,
|
|
|
|
emote.name),
|
2021-04-24 17:43:15 +02:00
|
|
|
MessageElementFlags{MessageElementFlag::AlwaysShow,
|
|
|
|
MessageElementFlag::TwitchEmote})
|
2018-08-15 22:46:20 +02:00
|
|
|
->setLink(Link(Link::InsertText, emote.name.string));
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:56:10 +02:00
|
|
|
mapOfSets[channelName].second.push_back(builder.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output to channel all created messages,
|
|
|
|
// That contain title or emotes.
|
2020-10-11 12:31:00 +02:00
|
|
|
// Put current channel emotes at the top
|
|
|
|
auto currentChannelPair = mapOfSets[currentChannelName];
|
|
|
|
for (auto message : currentChannelPair.second)
|
|
|
|
{
|
|
|
|
subChannel.addMessage(message);
|
|
|
|
}
|
|
|
|
mapOfSets.remove(currentChannelName);
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
foreach (auto pair, mapOfSets)
|
|
|
|
{
|
2018-10-11 14:56:10 +02:00
|
|
|
auto &channel = pair.first ? globalChannel : subChannel;
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto message : pair.second)
|
|
|
|
{
|
2018-10-11 14:56:10 +02:00
|
|
|
channel.addMessage(message);
|
|
|
|
}
|
2018-08-15 22:46:20 +02:00
|
|
|
}
|
2018-08-11 14:20:53 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-08-13 16:39:22 +02:00
|
|
|
EmotePopup::EmotePopup(QWidget *parent)
|
2019-12-14 12:57:40 +01:00
|
|
|
: BasePopup(BaseWindow::EnableCustomFrame, parent)
|
2018-08-11 14:20:53 +02:00
|
|
|
{
|
2020-07-05 14:07:03 +02:00
|
|
|
this->setStayInScreenRect(true);
|
2020-04-18 10:59:27 +02:00
|
|
|
this->moveTo(this, getApp()->windows->emotePopupPos(), false);
|
2020-04-13 13:15:51 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto layout = new QVBoxLayout(this);
|
2018-04-18 17:51:53 +02:00
|
|
|
this->getLayoutContainer()->setLayout(layout);
|
2017-12-19 03:18:00 +01:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto notebook = new Notebook(this);
|
2018-04-18 17:51:53 +02:00
|
|
|
layout->addWidget(notebook);
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
2020-11-08 12:02:19 +01:00
|
|
|
auto clicked = [this](const Link &link) {
|
|
|
|
this->linkClicked.invoke(link);
|
|
|
|
};
|
2018-08-11 17:15:17 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto makeView = [&](QString tabTitle) {
|
|
|
|
auto view = new ChannelView();
|
|
|
|
|
|
|
|
view->setOverrideFlags(MessageElementFlags{
|
|
|
|
MessageElementFlag::Default, MessageElementFlag::AlwaysShow,
|
|
|
|
MessageElementFlag::EmoteImages});
|
|
|
|
view->setEnableScrollingToBottom(false);
|
|
|
|
notebook->addPage(view, tabTitle);
|
2018-08-11 17:15:17 +02:00
|
|
|
view->linkClicked.connect(clicked);
|
2018-08-11 14:20:53 +02:00
|
|
|
|
|
|
|
return view;
|
|
|
|
};
|
|
|
|
|
|
|
|
this->subEmotesView_ = makeView("Subs");
|
|
|
|
this->channelEmotesView_ = makeView("Channel");
|
|
|
|
this->globalEmotesView_ = makeView("Global");
|
|
|
|
this->viewEmojis_ = makeView("Emojis");
|
2017-12-19 03:18:00 +01:00
|
|
|
|
|
|
|
this->loadEmojis();
|
2020-01-05 09:45:10 +01:00
|
|
|
|
2020-12-12 14:58:59 +01:00
|
|
|
// CTRL + 1-8 to open corresponding tab
|
|
|
|
for (auto i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
const auto openTab = [this, i, notebook] {
|
|
|
|
notebook->selectIndex(i);
|
|
|
|
};
|
|
|
|
createWindowShortcut(this, QString("CTRL+%1").arg(i + 1).toUtf8(),
|
|
|
|
openTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open last tab (first one from right)
|
|
|
|
createWindowShortcut(this, "CTRL+9", [=] {
|
|
|
|
notebook->selectLastTab();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Cycle through tabs
|
2020-11-08 12:02:19 +01:00
|
|
|
createWindowShortcut(this, "CTRL+Tab", [=] {
|
|
|
|
notebook->selectNextTab();
|
|
|
|
});
|
|
|
|
createWindowShortcut(this, "CTRL+Shift+Tab", [=] {
|
|
|
|
notebook->selectPreviousTab();
|
|
|
|
});
|
2021-04-10 14:11:26 +02:00
|
|
|
|
|
|
|
// Scroll with Page Up / Page Down
|
|
|
|
createWindowShortcut(this, "PgUp", [=] {
|
|
|
|
auto &scrollbar =
|
|
|
|
dynamic_cast<ChannelView *>(notebook->getSelectedPage())
|
|
|
|
->getScrollBar();
|
|
|
|
scrollbar.offset(-scrollbar.getLargeChange());
|
|
|
|
});
|
|
|
|
createWindowShortcut(this, "PgDown", [=] {
|
|
|
|
auto &scrollbar =
|
|
|
|
dynamic_cast<ChannelView *>(notebook->getSelectedPage())
|
|
|
|
->getScrollBar();
|
|
|
|
scrollbar.offset(scrollbar.getLargeChange());
|
|
|
|
});
|
2017-09-15 17:23:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-24 13:15:41 +01:00
|
|
|
void EmotePopup::loadChannel(ChannelPtr _channel)
|
2017-09-15 17:23:49 +02:00
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
BenchmarkGuard guard("loadChannel");
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-09-04 21:39:54 +02:00
|
|
|
this->setWindowTitle("Emotes in #" + _channel->getName());
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto twitchChannel = dynamic_cast<TwitchChannel *>(_channel.get());
|
2018-10-21 13:43:02 +02:00
|
|
|
if (twitchChannel == nullptr)
|
|
|
|
return;
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto addEmotes = [&](Channel &channel, const EmoteMap &map,
|
2021-04-24 17:43:15 +02:00
|
|
|
const QString &title,
|
|
|
|
const MessageElementFlag &emoteFlag) {
|
2018-08-11 14:20:53 +02:00
|
|
|
channel.addMessage(makeTitleMessage(title));
|
2021-04-24 17:43:15 +02:00
|
|
|
channel.addMessage(makeEmoteMessage(map, emoteFlag));
|
2017-09-16 00:05:06 +02:00
|
|
|
};
|
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
auto subChannel = std::make_shared<Channel>("", Channel::Type::None);
|
|
|
|
auto globalChannel = std::make_shared<Channel>("", Channel::Type::None);
|
|
|
|
auto channelChannel = std::make_shared<Channel>("", Channel::Type::None);
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
// twitch
|
|
|
|
addEmoteSets(
|
|
|
|
getApp()->accounts->twitch.getCurrent()->accessEmotes()->emoteSets,
|
2020-10-11 12:31:00 +02:00
|
|
|
*globalChannel, *subChannel, _channel->getName());
|
2018-05-31 16:20:46 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
// global
|
2021-07-13 13:23:50 +02:00
|
|
|
addEmotes(*globalChannel, *getApp()->twitch2->getBttvEmotes().emotes(),
|
2021-04-24 17:43:15 +02:00
|
|
|
"BetterTTV", MessageElementFlag::BttvEmote);
|
2021-07-13 13:23:50 +02:00
|
|
|
addEmotes(*globalChannel, *getApp()->twitch2->getFfzEmotes().emotes(),
|
2021-04-24 17:43:15 +02:00
|
|
|
"FrankerFaceZ", MessageElementFlag::FfzEmote);
|
2018-05-31 16:20:46 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
// channel
|
2021-04-24 17:43:15 +02:00
|
|
|
addEmotes(*channelChannel, *twitchChannel->bttvEmotes(), "BetterTTV",
|
|
|
|
MessageElementFlag::BttvEmote);
|
|
|
|
addEmotes(*channelChannel, *twitchChannel->ffzEmotes(), "FrankerFaceZ",
|
|
|
|
MessageElementFlag::FfzEmote);
|
2018-05-31 16:20:46 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
this->globalEmotesView_->setChannel(globalChannel);
|
|
|
|
this->subEmotesView_->setChannel(subChannel);
|
|
|
|
this->channelEmotesView_->setChannel(channelChannel);
|
2018-09-04 20:09:06 +02:00
|
|
|
|
2018-11-03 21:26:57 +01:00
|
|
|
if (subChannel->getMessageSnapshot().size() == 0)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-09-04 20:09:06 +02:00
|
|
|
MessageBuilder builder;
|
|
|
|
builder->flags.set(MessageFlag::Centered);
|
|
|
|
builder->flags.set(MessageFlag::DisableCompactEmotes);
|
|
|
|
builder.emplace<TextElement>("no subscription emotes available",
|
|
|
|
MessageElementFlag::Text,
|
|
|
|
MessageColor::System);
|
|
|
|
subChannel->addMessage(builder.release());
|
|
|
|
}
|
2017-12-19 03:18:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmotePopup::loadEmojis()
|
|
|
|
{
|
2018-06-05 18:53:49 +02:00
|
|
|
auto &emojis = getApp()->emotes->emojis.emojis;
|
2017-12-19 03:18:00 +01:00
|
|
|
|
2018-07-06 17:30:12 +02:00
|
|
|
ChannelPtr emojiChannel(new Channel("", Channel::Type::None));
|
2017-12-19 03:18:00 +01:00
|
|
|
|
|
|
|
// emojis
|
2018-06-28 19:38:57 +02:00
|
|
|
MessageBuilder builder;
|
2018-08-07 07:55:31 +02:00
|
|
|
builder->flags.set(MessageFlag::Centered);
|
|
|
|
builder->flags.set(MessageFlag::DisableCompactEmotes);
|
2018-01-11 20:16:25 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
emojis.each([&builder](const auto &key, const auto &value) {
|
2018-08-07 01:35:24 +02:00
|
|
|
builder
|
2021-04-24 17:43:15 +02:00
|
|
|
.emplace<EmoteElement>(
|
|
|
|
value->emote,
|
|
|
|
MessageElementFlags{MessageElementFlag::AlwaysShow,
|
|
|
|
MessageElementFlag::EmojiAll})
|
2018-08-07 01:35:24 +02:00
|
|
|
->setLink(
|
|
|
|
Link(Link::Type::InsertText, ":" + value->shortCodes[0] + ":"));
|
2018-01-11 20:16:25 +01:00
|
|
|
});
|
2018-08-07 01:35:24 +02:00
|
|
|
emojiChannel->addMessage(builder.release());
|
2017-09-16 16:20:10 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->viewEmojis_->setChannel(emojiChannel);
|
2017-09-15 17:23:49 +02:00
|
|
|
}
|
2017-12-17 02:10:35 +01:00
|
|
|
|
2020-04-13 13:15:51 +02:00
|
|
|
void EmotePopup::closeEvent(QCloseEvent *event)
|
|
|
|
{
|
|
|
|
getApp()->windows->setEmotePopupPos(this->pos());
|
|
|
|
QWidget::closeEvent(event);
|
|
|
|
}
|
2017-09-23 18:37:51 +02:00
|
|
|
} // namespace chatterino
|