From 4b7b1c35e1d544c0a3950ee3b55143f1f9344ce1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 1 Aug 2019 13:55:14 +0100 Subject: [PATCH] Improve storing of played animated stickers. --- .../admin_log/history_admin_log_inner.cpp | 10 +----- .../admin_log/history_admin_log_inner.h | 4 +-- .../history/history_inner_widget.cpp | 32 ++++++++----------- .../history/history_inner_widget.h | 5 ++- .../SourceFiles/history/media/history_media.h | 2 ++ .../history/media/history_media_sticker.cpp | 11 ++++--- .../history/media/history_media_sticker.h | 5 +++ .../history/view/history_view_element.cpp | 7 +--- .../history/view/history_view_element.h | 8 ++--- .../history/view/history_view_list_widget.cpp | 10 +----- .../history/view/history_view_list_widget.h | 3 +- 11 files changed, 37 insertions(+), 60 deletions(-) diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp index e217bf970..0be9a5414 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp @@ -548,15 +548,7 @@ bool InnerWidget::elementIntersectsRange( return (top < till && bottom > from); } -bool InnerWidget::elementStartStickerLoop(not_null view) { - if (_controller->session().settings().loopAnimatedStickers()) { - return true; - } - return !_animatedStickersPlayed.contains(view->data()->fullId()); -} - -void InnerWidget::elementStickerLoopStarted(not_null view) { - _animatedStickersPlayed.emplace(view->data()->fullId()); +void InnerWidget::elementStartStickerLoop(not_null view) { } void InnerWidget::saveState(not_null memento) { diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h index 85c55eee1..3eb1524f3 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h @@ -99,9 +99,7 @@ public: not_null view, int from, int till) override; - bool elementStartStickerLoop( - not_null view) override; - void elementStickerLoopStarted( + void elementStartStickerLoop( not_null view) override; ~InnerWidget(); diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 5163635a0..e2ad6742f 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1245,6 +1245,8 @@ void HistoryInner::itemRemoved(not_null item) { return; } + _animatedStickersPlayed.remove(item); + auto i = _selected.find(item); if (i != _selected.cend()) { _selected.erase(i); @@ -2267,6 +2269,13 @@ void HistoryInner::leaveEventHook(QEvent *e) { } HistoryInner::~HistoryInner() { + for (const auto &item : _animatedStickersPlayed) { + if (const auto view = item->mainView()) { + if (const auto media = view->media()) { + media->clearStickerLoopPlayed(); + } + } + } if (Instance == this) { Instance = nullptr; } @@ -2384,16 +2393,9 @@ bool HistoryInner::elementIntersectsRange( return (top < till && bottom > from); } -bool HistoryInner::elementStartStickerLoop( - not_null view) const { - return _controller->session().settings().loopAnimatedStickers() - || !_animatedStickersPlayed.contains(view->data()->fullId()); -} - -void HistoryInner::elementStickerLoopStarted(not_null view) { - if (!_controller->session().settings().loopAnimatedStickers()) { - _animatedStickersPlayed.emplace(view->data()->fullId()); - } +void HistoryInner::elementStartStickerLoop( + not_null view) { + _animatedStickersPlayed.emplace(view->data()); } auto HistoryInner::getSelectionState() const @@ -3246,16 +3248,10 @@ not_null HistoryInner::ElementDelegate() { ? Instance->elementIntersectsRange(view, from, till) : false; } - bool elementStartStickerLoop( - not_null view) override { - return Instance - ? Instance->elementStartStickerLoop(view) - : true; - } - void elementStickerLoopStarted( + void elementStartStickerLoop( not_null view) override { if (Instance) { - Instance->elementStickerLoopStarted(view); + Instance->elementStartStickerLoop(view); } } diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index 5e43bff60..91ccef009 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -79,8 +79,7 @@ public: not_null view, int from, int till) const; - bool elementStartStickerLoop(not_null view) const; - void elementStickerLoopStarted(not_null view); + void elementStartStickerLoop(not_null view); void updateBotInfo(bool recount = true); @@ -332,7 +331,7 @@ private: style::cursor _cursor = style::cur_default; SelectedItems _selected; - base::flat_set _animatedStickersPlayed; + base::flat_set> _animatedStickersPlayed; MouseAction _mouseAction = MouseAction::None; TextSelectType _mouseSelectType = TextSelectType::Letters; diff --git a/Telegram/SourceFiles/history/media/history_media.h b/Telegram/SourceFiles/history/media/history_media.h index 4bb718dc9..4198d57a5 100644 --- a/Telegram/SourceFiles/history/media/history_media.h +++ b/Telegram/SourceFiles/history/media/history_media.h @@ -133,6 +133,8 @@ public: } virtual void stopAnimation() { } + virtual void clearStickerLoopPlayed() { + } [[nodiscard]] virtual QSize sizeForGrouping() const { Unexpected("Grouping method call."); diff --git a/Telegram/SourceFiles/history/media/history_media_sticker.cpp b/Telegram/SourceFiles/history/media/history_media_sticker.cpp index 6afb111b6..302e87b5d 100644 --- a/Telegram/SourceFiles/history/media/history_media_sticker.cpp +++ b/Telegram/SourceFiles/history/media/history_media_sticker.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_cursor_state.h" #include "ui/image/image.h" #include "ui/emoji_config.h" +#include "main/main_session.h" #include "mainwindow.h" // App::wnd()->sessionController. #include "window/window_session_controller.h" // isGifPausedAtLeastFor. #include "data/data_session.h" @@ -212,12 +213,14 @@ void HistorySticker::draw( frame.image); const auto paused = App::wnd()->sessionController()->isGifPausedAtLeastFor(Window::GifPauseReason::Any); + const auto playOnce = !_data->session().settings().loopAnimatedStickers(); if (!paused - && (frame.index != 0 - || _parent->delegate()->elementStartStickerLoop(_parent)) + && (!playOnce || frame.index != 0 || !_lottieOncePlayed) && _lottie->markFrameShown() - && !frame.index) { - _parent->delegate()->elementStickerLoopStarted(_parent); + && playOnce + && !_lottieOncePlayed) { + _lottieOncePlayed = true; + _parent->delegate()->elementStartStickerLoop(_parent); } } if (!inWebPage) { diff --git a/Telegram/SourceFiles/history/media/history_media_sticker.h b/Telegram/SourceFiles/history/media/history_media_sticker.h index e92919b64..eca113397 100644 --- a/Telegram/SourceFiles/history/media/history_media_sticker.h +++ b/Telegram/SourceFiles/history/media/history_media_sticker.h @@ -54,6 +54,9 @@ public: bool hidesForwardedInfo() const override { return true; } + void clearStickerLoopPlayed() override { + _lottieOncePlayed = false; + } void unloadHeavyPart() override { unloadLottie(); @@ -76,6 +79,8 @@ private: not_null _data; QString _emoji; std::unique_ptr _lottie; + mutable bool _lottieOncePlayed = false; + rpl::lifetime _lifetime; }; diff --git a/Telegram/SourceFiles/history/view/history_view_element.cpp b/Telegram/SourceFiles/history/view/history_view_element.cpp index b156476b1..fb5f1cf1b 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.cpp +++ b/Telegram/SourceFiles/history/view/history_view_element.cpp @@ -84,12 +84,7 @@ bool SimpleElementDelegate::elementIntersectsRange( return true; } -bool SimpleElementDelegate::elementStartStickerLoop( - not_null view) { - return true; -} - -void SimpleElementDelegate::elementStickerLoopStarted( +void SimpleElementDelegate::elementStartStickerLoop( not_null view) { } diff --git a/Telegram/SourceFiles/history/view/history_view_element.h b/Telegram/SourceFiles/history/view/history_view_element.h index 2d4c2d5ab..a69471c13 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.h +++ b/Telegram/SourceFiles/history/view/history_view_element.h @@ -50,9 +50,7 @@ public: not_null view, int from, int till) = 0; - virtual bool elementStartStickerLoop(not_null view) = 0; - virtual void elementStickerLoopStarted( - not_null view) = 0; + virtual void elementStartStickerLoop(not_null view) = 0; }; @@ -72,9 +70,7 @@ public: not_null view, int from, int till) override; - bool elementStartStickerLoop( - not_null view) override; - void elementStickerLoopStarted(not_null view) override; + void elementStartStickerLoop(not_null view) override; }; diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index 30b373d69..ae5f6e218 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -1141,15 +1141,7 @@ bool ListWidget::elementIntersectsRange( return (top < till && bottom > from); } -bool ListWidget::elementStartStickerLoop(not_null view) { - return _controller->session().settings().loopAnimatedStickers() - || !_animatedStickersPlayed.contains(view->data()->fullId()); -} - -void ListWidget::elementStickerLoopStarted(not_null view) { - if (!_controller->session().settings().loopAnimatedStickers()) { - _animatedStickersPlayed.emplace(view->data()->fullId()); - } +void ListWidget::elementStartStickerLoop(not_null view) { } void ListWidget::saveState(not_null memento) { diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.h b/Telegram/SourceFiles/history/view/history_view_list_widget.h index 128137a7a..47125454a 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.h @@ -191,8 +191,7 @@ public: not_null view, int from, int till) override; - bool elementStartStickerLoop(not_null view) override; - void elementStickerLoopStarted(not_null view) override; + void elementStartStickerLoop(not_null view) override; ~ListWidget();