Use lottie instead of webp in Edit Chat Reactions.

This commit is contained in:
John Preston 2022-01-05 15:10:05 +03:00
parent 718ba2d0e3
commit 82523978c9

View file

@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h" #include "data/data_session.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "apiwrap.h" #include "apiwrap.h"
#include "lottie/lottie_icon.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "ui/widgets/buttons.h" #include "ui/widgets/buttons.h"
#include "info/profile/info_profile_icon.h" #include "info/profile/info_profile_icon.h"
@ -32,7 +33,9 @@ void AddReactionIcon(
not_null<DocumentData*> document) { not_null<DocumentData*> document) {
struct State { struct State {
std::shared_ptr<Data::DocumentMedia> media; std::shared_ptr<Data::DocumentMedia> media;
std::unique_ptr<Lottie::Icon> icon;
QImage image; QImage image;
rpl::lifetime downloadLifetime;
}; };
const auto size = st::editPeerReactionsPreview; const auto size = st::editPeerReactionsPreview;
@ -50,32 +53,36 @@ void AddReactionIcon(
size.width()); size.width());
}, icon->lifetime()); }, icon->lifetime());
const auto setImage = [=](not_null<Image*> image) { const auto initLottie = [=] {
state->image = Images::prepare( state->icon = std::make_unique<Lottie::Icon>(Lottie::IconDescriptor{
image->original(), .path = state->media->owner()->filepath(true),
size * style::DevicePixelRatio(), .json = state->media->bytes(),
size * style::DevicePixelRatio(), .sizeOverride = QSize(size, size),
Images::Option::Smooth | Images::Option::TransparentBackground, .frame = -1,
size, });
size); state->downloadLifetime.destroy();
icon->update(); state->media = nullptr;
}; };
if (const auto image = state->media->getStickerLarge()) { state->media->checkStickerLarge();
setImage(image); if (state->media->loaded()) {
initLottie();
} else { } else {
document->session().downloaderTaskFinished( document->session().downloaderTaskFinished(
) | rpl::map([=] { ) | rpl::filter([=] {
return state->media->getStickerLarge(); return state->media->loaded();
}) | rpl::filter_nullptr() | rpl::take( }) | rpl::start_with_next([=] {
1 initLottie();
) | rpl::start_with_next([=](not_null<Image*> image) { icon->update();
setImage(image); }, state->downloadLifetime);
}, button->lifetime());
} }
icon->paintRequest( icon->paintRequest(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
Painter p(icon); QPainter p(icon);
if (state->image.isNull() && state->icon) {
state->image = state->icon->frame();
crl::async([icon = std::move(state->icon)]{});
}
if (!state->image.isNull()) { if (!state->image.isNull()) {
p.drawImage(0, 0, state->image); p.drawImage(0, 0, state->image);
} }
@ -148,7 +155,7 @@ void EditAllowedReactionsBox(
container, container,
rpl::single(entry.title), rpl::single(entry.title),
st::manageGroupButton.button); st::manageGroupButton.button);
AddReactionIcon(button, entry.staticIcon); AddReactionIcon(button, entry.centerIcon);
state->toggles.emplace(entry.emoji, button); state->toggles.emplace(entry.emoji, button);
button->toggleOn(rpl::single( button->toggleOn(rpl::single(
active(entry) active(entry)