diff --git a/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp b/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp index b9ebe4bb5f..96bd885f34 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp @@ -33,6 +33,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/cached_round_corners.h" #include "ui/painter.h" #include "ui/ui_utility.h" +#include "window/section_widget.h" #include "window/window_session_controller.h" #include "window/themes/window_theme.h" #include "styles/style_chat.h" @@ -471,13 +472,28 @@ ThemeDocumentBox::ThemeDocumentBox( not_null parent, const Data::WallPaper &paper) : _parent(parent) -, _preview( - parent, +, _emojiId(paper.emojiId()) { + Window::WallPaperResolved( + &_parent->history()->owner(), + &paper + ) | rpl::start_with_next([=](const Data::WallPaper *paper) { + _parent->repaint(); + if (!paper) { + _preview.reset(); + } else { + createPreview(*paper); + } + }, _lifetime); +} + +void ThemeDocumentBox::createPreview(const Data::WallPaper &paper) { + _preview.emplace( + _parent, paper.document(), paper, - st::msgServicePhotoWidth) { - _preview.initDimensions(); - _preview.resizeGetHeight(_preview.maxWidth()); + st::msgServicePhotoWidth); + _preview->initDimensions(); + _preview->resizeGetHeight(_preview->maxWidth()); } ThemeDocumentBox::~ThemeDocumentBox() = default; @@ -487,7 +503,9 @@ int ThemeDocumentBox::top() { } QSize ThemeDocumentBox::size() { - return { _preview.maxWidth(), _preview.minHeight() }; + return _preview + ? QSize(_preview->maxWidth(), _preview->minHeight()) + : QSize(st::msgServicePhotoWidth, st::msgServicePhotoWidth); } QString ThemeDocumentBox::title() { @@ -560,9 +578,11 @@ void ThemeDocumentBox::draw( Painter &p, const PaintContext &context, const QRect &geometry) { - p.translate(geometry.topLeft()); - _preview.draw(p, context); - p.translate(-geometry.topLeft()); + if (_preview) { + p.translate(geometry.topLeft()); + _preview->draw(p, context); + p.translate(-geometry.topLeft()); + } } void ThemeDocumentBox::stickerClearLoopPlayed() { @@ -575,11 +595,13 @@ std::unique_ptr ThemeDocumentBox::stickerTakePlayer( } bool ThemeDocumentBox::hasHeavyPart() { - return _preview.hasHeavyPart(); + return _preview && _preview->hasHeavyPart(); } void ThemeDocumentBox::unloadHeavyPart() { - _preview.unloadHeavyPart(); + if (_preview) { + _preview->unloadHeavyPart(); + } } } // namespace HistoryView diff --git a/Telegram/SourceFiles/history/view/media/history_view_theme_document.h b/Telegram/SourceFiles/history/view/media/history_view_theme_document.h index a9e2e6eccd..3e1d34ecfc 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_theme_document.h +++ b/Telegram/SourceFiles/history/view/media/history_view_theme_document.h @@ -119,8 +119,12 @@ public: void unloadHeavyPart() override; private: + void createPreview(const Data::WallPaper &paper); + const not_null _parent; - ThemeDocument _preview; + QString _emojiId; + std::optional _preview; + rpl::lifetime _lifetime; }; diff --git a/Telegram/SourceFiles/window/section_widget.cpp b/Telegram/SourceFiles/window/section_widget.cpp index 24032e00a3..4f69791b09 100644 --- a/Telegram/SourceFiles/window/section_widget.cpp +++ b/Telegram/SourceFiles/window/section_widget.cpp @@ -56,43 +56,7 @@ struct ResolvedPaper { peer, Data::PeerUpdate::Flag::ChatWallPaper ) | rpl::map([=]() -> rpl::producer { - const auto paper = peer->wallPaper(); - const auto id = paper ? paper->emojiId() : QString(); - if (id.isEmpty()) { - return rpl::single(paper); - } - const auto themes = &peer->owner().cloudThemes(); - auto fromThemes = [=](bool force) - -> rpl::producer { - if (themes->chatThemes().empty() && !force) { - return nullptr; - } - return Window::Theme::IsNightModeValue( - ) | rpl::map([=](bool dark) -> const Data::WallPaper* { - const auto &list = themes->chatThemes(); - const auto i = ranges::find( - list, - id, - &Data::CloudTheme::emoticon); - if (i != end(list)) { - using Type = Data::CloudThemeType; - const auto type = dark ? Type::Dark : Type::Light; - const auto j = i->settings.find(type); - if (j != end(i->settings) && j->second.paper) { - return &*j->second.paper; - } - } - return nullptr; - }); - }; - if (auto result = fromThemes(false)) { - return result; - } - themes->refreshChatThemes(); - return themes->chatThemesUpdated( - ) | rpl::take(1) | rpl::map([=] { - return fromThemes(true); - }) | rpl::flatten_latest(); + return WallPaperResolved(&peer->owner(), peer->wallPaper()); }) | rpl::flatten_latest(); } @@ -202,6 +166,47 @@ struct ResolvedTheme { } // namespace +rpl::producer WallPaperResolved( + not_null owner, + const Data::WallPaper *paper) { + const auto id = paper ? paper->emojiId() : QString(); + if (id.isEmpty()) { + return rpl::single(paper); + } + const auto themes = &owner->cloudThemes(); + auto fromThemes = [=](bool force) + -> rpl::producer { + if (themes->chatThemes().empty() && !force) { + return nullptr; + } + return Window::Theme::IsNightModeValue( + ) | rpl::map([=](bool dark) -> const Data::WallPaper* { + const auto &list = themes->chatThemes(); + const auto i = ranges::find( + list, + id, + &Data::CloudTheme::emoticon); + if (i != end(list)) { + using Type = Data::CloudThemeType; + const auto type = dark ? Type::Dark : Type::Light; + const auto j = i->settings.find(type); + if (j != end(i->settings) && j->second.paper) { + return &*j->second.paper; + } + } + return nullptr; + }); + }; + if (auto result = fromThemes(false)) { + return result; + } + themes->refreshChatThemes(); + return themes->chatThemesUpdated( + ) | rpl::take(1) | rpl::map([=] { + return fromThemes(true); + }) | rpl::flatten_latest(); +} + AbstractSectionWidget::AbstractSectionWidget( QWidget *parent, not_null controller, diff --git a/Telegram/SourceFiles/window/section_widget.h b/Telegram/SourceFiles/window/section_widget.h index d8cfda8147..52b94f7025 100644 --- a/Telegram/SourceFiles/window/section_widget.h +++ b/Telegram/SourceFiles/window/section_widget.h @@ -23,6 +23,8 @@ class Show; namespace Data { struct ReactionId; class ForumTopic; +class WallPaper; +class Session; } // namespace Data namespace Main { @@ -251,4 +253,8 @@ private: not_null item, const Data::ReactionId &id); +[[nodiscard]] rpl::producer WallPaperResolved( + not_null owner, + const Data::WallPaper *paper); + } // namespace Window