From 2a4d269eca2b718520a0855769fadc7b2dd5f6ab Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 25 Jul 2022 13:56:24 +0300 Subject: [PATCH] Use large image previews more. --- .../data/stickers/data_custom_emoji.cpp | 5 +++ .../ui/text/custom_emoji_instance.cpp | 38 +++++++++++++++++++ .../ui/text/custom_emoji_instance.h | 4 ++ 3 files changed, 47 insertions(+) diff --git a/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp b/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp index 50ab929f5..d725af94b 100644 --- a/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp +++ b/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp @@ -382,6 +382,11 @@ std::unique_ptr CustomEmojiManager::create( factory(), prepareNonExactPreview(documentId, tag) }, std::move(repaint))).first; + } else if (!i->second->hasImagePreview()) { + auto preview = prepareNonExactPreview(documentId, tag); + if (preview.isImage()) { + i->second->updatePreview(std::move(preview)); + } } return std::make_unique( i->second.get(), diff --git a/Telegram/SourceFiles/ui/text/custom_emoji_instance.cpp b/Telegram/SourceFiles/ui/text/custom_emoji_instance.cpp index e96da5ebd..b6fdf18c3 100644 --- a/Telegram/SourceFiles/ui/text/custom_emoji_instance.cpp +++ b/Telegram/SourceFiles/ui/text/custom_emoji_instance.cpp @@ -536,10 +536,26 @@ void Loading::paint(QPainter &p, int x, int y, const QColor &preview) { _preview.paint(p, x, y, preview); } +bool Loading::hasImagePreview() const { +return _preview.isImage(); +} + Preview Loading::imagePreview() const { return _preview.isImage() ? _preview : Preview(); } +void Loading::updatePreview(Preview preview) { + if (!_preview.isImage() && preview.isImage()) { + _preview = std::move(preview); + } else if (!_preview) { + if (auto loaderPreview = _loader->preview()) { + _preview = std::move(loaderPreview); + } else if (preview) { + _preview = std::move(preview); + } + } +} + void Loading::cancel() { _loader->cancel(); invalidate_weak_ptrs(this); @@ -606,6 +622,17 @@ void Instance::paint( } } +bool Instance::hasImagePreview() const { + if (const auto loading = std::get_if(&_state)) { + return loading->hasImagePreview(); + } else if (const auto caching = std::get_if(&_state)) { + return caching->preview.isImage(); + } else if (const auto cached = std::get_if(&_state)) { + return true; + } + return false; +} + Preview Instance::imagePreview() const { if (const auto loading = std::get_if(&_state)) { return loading->imagePreview(); @@ -617,6 +644,17 @@ Preview Instance::imagePreview() const { return {}; } +void Instance::updatePreview(Preview preview) { + if (const auto loading = std::get_if(&_state)) { + loading->updatePreview(std::move(preview)); + } else if (const auto caching = std::get_if(&_state)) { + if ((!caching->preview.isImage() && preview.isImage()) + || (!caching->preview && preview)) { + caching->preview = std::move(preview); + } + } +} + void Instance::repaint() { for (const auto &object : _usage) { object->repaint(); diff --git a/Telegram/SourceFiles/ui/text/custom_emoji_instance.h b/Telegram/SourceFiles/ui/text/custom_emoji_instance.h index 5f840cbd0..e78f51656 100644 --- a/Telegram/SourceFiles/ui/text/custom_emoji_instance.h +++ b/Telegram/SourceFiles/ui/text/custom_emoji_instance.h @@ -200,7 +200,9 @@ public: void load(Fn done); [[nodiscard]] bool loading() const; void paint(QPainter &p, int x, int y, const QColor &preview); + [[nodiscard]] bool hasImagePreview() const; [[nodiscard]] Preview imagePreview() const; + void updatePreview(Preview preview); void cancel(); private: @@ -231,7 +233,9 @@ public: crl::time now, const QColor &preview, bool paused); + [[nodiscard]] bool hasImagePreview() const; [[nodiscard]] Preview imagePreview() const; + void updatePreview(Preview preview); void incrementUsage(not_null object); void decrementUsage(not_null object);