tdesktop/Telegram/SourceFiles/chat_helpers/stickers_emoji_pack.cpp

322 lines
7.4 KiB
C++
Raw Normal View History

2019-08-01 14:13:02 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "chat_helpers/stickers_emoji_pack.h"
#include "chat_helpers/stickers_emoji_image_loader.h"
2019-08-01 14:13:02 +00:00
#include "history/history_item.h"
2019-08-07 14:22:51 +00:00
#include "lottie/lottie_common.h"
2019-08-01 14:13:02 +00:00
#include "ui/emoji_config.h"
2019-08-02 18:19:14 +00:00
#include "ui/text/text_isolated_emoji.h"
2020-05-29 15:10:25 +00:00
#include "ui/image/image.h"
2019-08-01 14:13:02 +00:00
#include "main/main_session.h"
2019-08-02 18:19:14 +00:00
#include "data/data_file_origin.h"
2019-08-01 14:13:02 +00:00
#include "data/data_session.h"
#include "data/data_document.h"
#include "core/core_settings.h"
#include "core/application.h"
2019-09-26 10:55:35 +00:00
#include "base/call_delayed.h"
2019-08-01 14:13:02 +00:00
#include "apiwrap.h"
2020-10-10 09:15:37 +00:00
#include "styles/style_chat.h"
2019-08-01 14:13:02 +00:00
2019-09-04 07:19:15 +00:00
#include <QtCore/QBuffer>
2019-08-01 14:13:02 +00:00
namespace Stickers {
namespace {
2019-09-11 10:49:27 +00:00
constexpr auto kRefreshTimeout = 7200 * crl::time(1000);
2019-08-02 18:19:14 +00:00
2019-08-05 11:44:40 +00:00
[[nodiscard]] QSize SingleSize() {
2019-08-02 18:19:14 +00:00
const auto single = st::largeEmojiSize;
const auto outline = st::largeEmojiOutline;
return QSize(
2019-08-05 11:44:40 +00:00
2 * outline + single,
2019-08-02 18:19:14 +00:00
2 * outline + single
) * cIntRetinaFactor();
}
2019-08-07 14:22:51 +00:00
[[nodiscard]] const Lottie::ColorReplacements *ColorReplacements(int index) {
Expects(index >= 1 && index <= 5);
static const auto color1 = Lottie::ColorReplacements{
{
2021-02-12 11:03:53 +00:00
{ 0xf77e41U, 0xcb7b55U },
{ 0xffb139U, 0xf6b689U },
{ 0xffd140U, 0xffcda7U },
{ 0xffdf79U, 0xffdfc5U },
2019-08-07 14:22:51 +00:00
},
1,
};
static const auto color2 = Lottie::ColorReplacements{
{
2021-02-12 11:03:53 +00:00
{ 0xf77e41U, 0xa45a38U },
{ 0xffb139U, 0xdf986bU },
{ 0xffd140U, 0xedb183U },
{ 0xffdf79U, 0xf4c3a0U },
2019-08-07 14:22:51 +00:00
},
2,
};
static const auto color3 = Lottie::ColorReplacements{
{
2021-02-12 11:03:53 +00:00
{ 0xf77e41U, 0x703a17U },
{ 0xffb139U, 0xab673dU },
{ 0xffd140U, 0xc37f4eU },
{ 0xffdf79U, 0xd89667U },
2019-08-07 14:22:51 +00:00
},
3,
};
static const auto color4 = Lottie::ColorReplacements{
{
2021-02-12 11:03:53 +00:00
{ 0xf77e41U, 0x4a2409U },
{ 0xffb139U, 0x7d3e0eU },
{ 0xffd140U, 0x965529U },
{ 0xffdf79U, 0xa96337U },
2019-08-07 14:22:51 +00:00
},
4,
};
static const auto color5 = Lottie::ColorReplacements{
{
2021-02-12 11:03:53 +00:00
{ 0xf77e41U, 0x200f0aU },
{ 0xffb139U, 0x412924U },
{ 0xffd140U, 0x593d37U },
{ 0xffdf79U, 0x63453fU },
2019-08-07 14:22:51 +00:00
},
5,
};
static const auto list = std::array{
&color1,
&color2,
&color3,
&color4,
&color5,
};
return list[index - 1];
}
2019-08-01 14:13:02 +00:00
} // namespace
2020-05-29 14:29:21 +00:00
QSize LargeEmojiImage::Size() {
return SingleSize();
2020-05-29 14:29:21 +00:00
}
2019-08-02 18:19:14 +00:00
EmojiPack::EmojiPack(not_null<Main::Session*> session)
: _session(session) {
2019-08-01 14:13:02 +00:00
refresh();
2019-08-01 21:14:19 +00:00
session->data().itemRemoved(
) | rpl::filter([](not_null<const HistoryItem*> item) {
2019-08-02 18:19:14 +00:00
return item->isIsolatedEmoji();
2019-08-01 21:14:19 +00:00
}) | rpl::start_with_next([=](not_null<const HistoryItem*> item) {
remove(item);
}, _lifetime);
2019-08-02 10:52:35 +00:00
Core::App().settings().largeEmojiChanges(
2019-08-05 14:08:20 +00:00
) | rpl::start_with_next([=](bool large) {
2019-08-02 18:19:14 +00:00
refreshAll();
}, _lifetime);
Ui::Emoji::Updated(
) | rpl::start_with_next([=] {
_images.clear();
refreshAll();
2019-08-02 10:52:35 +00:00
}, _lifetime);
2019-08-01 14:13:02 +00:00
}
2019-08-02 18:19:14 +00:00
EmojiPack::~EmojiPack() = default;
bool EmojiPack::add(not_null<HistoryItem*> item) {
if (const auto emoji = item->isolatedEmoji()) {
_items[emoji].emplace(item);
return true;
2019-08-01 21:14:19 +00:00
}
return false;
}
2019-08-02 18:19:14 +00:00
void EmojiPack::remove(not_null<const HistoryItem*> item) {
Expects(item->isIsolatedEmoji());
const auto emoji = item->isolatedEmoji();
2019-08-01 21:14:19 +00:00
const auto i = _items.find(emoji);
Assert(i != end(_items));
const auto j = i->second.find(item);
Assert(j != end(i->second));
i->second.erase(j);
if (i->second.empty()) {
_items.erase(i);
}
}
2019-08-07 14:22:51 +00:00
auto EmojiPack::stickerForEmoji(const IsolatedEmoji &emoji) -> Sticker {
2019-08-02 18:19:14 +00:00
Expects(!emoji.empty());
if (emoji.items[1] != nullptr) {
2019-08-07 14:22:51 +00:00
return Sticker();
}
const auto first = emoji.items[0];
const auto i = _map.find(first);
if (i != end(_map)) {
return { i->second.get(), nullptr };
}
if (!first->colored()) {
return Sticker();
}
const auto j = _map.find(first->original());
if (j != end(_map)) {
const auto index = first->variantIndex(first);
return { j->second.get(), ColorReplacements(index) };
2019-08-01 14:13:02 +00:00
}
2019-08-07 14:22:51 +00:00
return Sticker();
2019-08-01 14:13:02 +00:00
}
2020-05-29 14:29:21 +00:00
std::shared_ptr<LargeEmojiImage> EmojiPack::image(EmojiPtr emoji) {
const auto i = _images.emplace(
emoji,
std::weak_ptr<LargeEmojiImage>()).first;
2019-08-02 18:19:14 +00:00
if (const auto result = i->second.lock()) {
return result;
}
2020-05-29 14:29:21 +00:00
auto result = std::make_shared<LargeEmojiImage>();
const auto raw = result.get();
const auto weak = base::make_weak(_session.get());
raw->load = [=] {
Core::App().emojiImageLoader().with([=](
const EmojiImageLoader &loader) {
2020-05-29 14:29:21 +00:00
crl::on_main(weak, [
=,
image = loader.prepare(emoji)
]() mutable {
const auto i = _images.find(emoji);
if (i != end(_images)) {
if (const auto strong = i->second.lock()) {
if (!strong->image) {
strong->load = nullptr;
2020-05-29 15:10:25 +00:00
strong->image.emplace(std::move(image));
_session->notifyDownloaderTaskFinished();
2020-05-29 14:29:21 +00:00
}
}
}
});
});
raw->load = nullptr;
};
2019-08-02 18:19:14 +00:00
i->second = result;
return result;
}
2019-08-01 14:13:02 +00:00
void EmojiPack::refresh() {
if (_requestId) {
return;
}
_requestId = _session->api().request(MTPmessages_GetStickerSet(
MTP_inputStickerSetAnimatedEmoji()
)).done([=](const MTPmessages_StickerSet &result) {
2019-08-01 21:14:19 +00:00
_requestId = 0;
2019-08-01 14:13:02 +00:00
refreshDelayed();
result.match([&](const MTPDmessages_stickerSet &data) {
2019-08-01 21:14:19 +00:00
applySet(data);
2019-08-01 14:13:02 +00:00
});
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2019-08-01 21:14:19 +00:00
_requestId = 0;
2019-08-01 14:13:02 +00:00
refreshDelayed();
}).send();
}
2019-08-01 21:14:19 +00:00
void EmojiPack::applySet(const MTPDmessages_stickerSet &data) {
const auto stickers = collectStickers(data.vdocuments().v);
auto was = base::take(_map);
for (const auto &pack : data.vpacks().v) {
pack.match([&](const MTPDstickerPack &data) {
applyPack(data, stickers);
});
}
for (const auto &[emoji, document] : _map) {
const auto i = was.find(emoji);
if (i == end(was)) {
refreshItems(emoji);
} else {
if (i->second != document) {
refreshItems(i->first);
}
was.erase(i);
}
}
for (const auto &[emoji, document] : was) {
2019-08-01 21:14:19 +00:00
refreshItems(emoji);
}
}
2019-08-02 18:19:14 +00:00
void EmojiPack::refreshAll() {
for (const auto &[emoji, list] : _items) {
refreshItems(list);
}
}
2019-08-01 21:14:19 +00:00
void EmojiPack::refreshItems(EmojiPtr emoji) {
2019-08-02 18:19:14 +00:00
const auto i = _items.find(IsolatedEmoji{ { emoji } });
if (!emoji->colored()) {
if (const auto count = emoji->variantsCount()) {
for (auto i = 0; i != count; ++i) {
refreshItems(emoji->variant(i + 1));
}
}
}
2019-08-01 21:14:19 +00:00
if (i == end(_items)) {
return;
}
2019-08-02 18:19:14 +00:00
refreshItems(i->second);
}
void EmojiPack::refreshItems(
const base::flat_set<not_null<HistoryItem*>> &list) {
for (const auto &item : list) {
2019-08-01 21:14:19 +00:00
_session->data().requestItemViewRefresh(item);
}
}
void EmojiPack::applyPack(
const MTPDstickerPack &data,
const base::flat_map<uint64, not_null<DocumentData*>> &map) {
const auto emoji = [&] {
return Ui::Emoji::Find(qs(data.vemoticon()));
}();
const auto document = [&]() -> DocumentData * {
for (const auto &id : data.vdocuments().v) {
const auto i = map.find(id.v);
if (i != end(map)) {
return i->second.get();
}
}
return nullptr;
}();
if (emoji && document) {
_map.emplace_or_assign(emoji, document);
}
}
base::flat_map<uint64, not_null<DocumentData*>> EmojiPack::collectStickers(
const QVector<MTPDocument> &list) const {
auto result = base::flat_map<uint64, not_null<DocumentData*>>();
for (const auto &sticker : list) {
const auto document = _session->data().processDocument(
sticker);
if (document->sticker()) {
result.emplace(document->id, document);
}
}
return result;
}
2019-08-01 14:13:02 +00:00
void EmojiPack::refreshDelayed() {
base::call_delayed(kRefreshTimeout, _session, [=] {
2019-08-01 14:13:02 +00:00
refresh();
});
}
} // namespace Stickers