Clear DocumentMedia in sticker panel.

This commit is contained in:
John Preston 2020-05-21 21:39:46 +04:00
parent eb75859dc0
commit 9725d4272e
2 changed files with 28 additions and 35 deletions

View File

@ -189,7 +189,7 @@ auto StickersListWidget::PrepareStickers(const Stickers::Pack &pack)
return ranges::view::all( return ranges::view::all(
pack pack
) | ranges::view::transform([](DocumentData *document) { ) | ranges::view::transform([](DocumentData *document) {
return Sticker{ document, document->createMediaView() }; return Sticker{ document };
}) | ranges::to_vector; }) | ranges::to_vector;
} }
@ -217,6 +217,14 @@ StickersListWidget::Set &StickersListWidget::Set::operator=(
Set &&other) = default; Set &&other) = default;
StickersListWidget::Set::~Set() = default; StickersListWidget::Set::~Set() = default;
void StickersListWidget::Sticker::ensureMediaCreated() {
if (documentMedia) {
return;
}
documentMedia = document->createMediaView();
}
StickersListWidget::Footer::Footer(not_null<StickersListWidget*> parent) StickersListWidget::Footer::Footer(not_null<StickersListWidget*> parent)
: InnerFooter(parent) : InnerFooter(parent)
, _pan(parent) , _pan(parent)
@ -932,10 +940,10 @@ void StickersListWidget::checkVisibleFeatured(
const auto destroyAbove = floorclamp(visibleTop - visibleHeight, rowHeight, 0, _officialSets.size()); const auto destroyAbove = floorclamp(visibleTop - visibleHeight, rowHeight, 0, _officialSets.size());
const auto destroyBelow = ceilclamp(visibleBottom + visibleHeight, rowHeight, 0, _officialSets.size()); const auto destroyBelow = ceilclamp(visibleBottom + visibleHeight, rowHeight, 0, _officialSets.size());
for (auto i = 0; i != destroyAbove; ++i) { for (auto i = 0; i != destroyAbove; ++i) {
destroyLottieIn(_officialSets[i]); clearHeavyIn(_officialSets[i]);
} }
for (auto i = destroyBelow; i != _officialSets.size(); ++i) { for (auto i = destroyBelow; i != _officialSets.size(); ++i) {
destroyLottieIn(_officialSets[i]); clearHeavyIn(_officialSets[i]);
} }
} }
@ -1014,7 +1022,8 @@ void StickersListWidget::readVisibleFeatured(
for (int j = 0; j < count; ++j) { for (int j = 0; j < count; ++j) {
if (!set.stickers[j].document->hasThumbnail() if (!set.stickers[j].document->hasThumbnail()
|| !set.stickers[j].document->thumbnailLoading() || !set.stickers[j].document->thumbnailLoading()
|| set.stickers[j].documentMedia->loaded()) { || (set.stickers[j].documentMedia
&& set.stickers[j].documentMedia->loaded())) {
++loaded; ++loaded;
} }
} }
@ -1578,7 +1587,7 @@ void StickersListWidget::checkVisibleLottie() {
enumerateSections([&](const SectionInfo &info) { enumerateSections([&](const SectionInfo &info) {
if (destroyBelow <= info.rowsTop if (destroyBelow <= info.rowsTop
|| destroyAbove >= info.rowsBottom) { || destroyAbove >= info.rowsBottom) {
destroyLottieIn(shownSets()[info.section]); clearHeavyIn(shownSets()[info.section]);
} else if ((visibleTop > info.rowsTop && visibleTop < info.rowsBottom) } else if ((visibleTop > info.rowsTop && visibleTop < info.rowsBottom)
|| (visibleBottom > info.rowsTop || (visibleBottom > info.rowsTop
&& visibleBottom < info.rowsBottom)) { && visibleBottom < info.rowsBottom)) {
@ -1588,13 +1597,11 @@ void StickersListWidget::checkVisibleLottie() {
}); });
} }
void StickersListWidget::destroyLottieIn(Set &set) { void StickersListWidget::clearHeavyIn(Set &set) {
if (!set.lottiePlayer) {
return;
}
set.lottiePlayer = nullptr; set.lottiePlayer = nullptr;
for (auto &sticker : set.stickers) { for (auto &sticker : set.stickers) {
sticker.animated = nullptr; sticker.animated = nullptr;
sticker.documentMedia = nullptr;
} }
_lottieData.remove(set.id); _lottieData.remove(set.id);
} }
@ -1719,6 +1726,7 @@ void StickersListWidget::setupLottie(Set &set, int section, int index) {
const auto document = sticker.document; const auto document = sticker.document;
ensureLottiePlayer(set); ensureLottiePlayer(set);
sticker.ensureMediaCreated();
sticker.animated = Stickers::LottieAnimationFromDocument( sticker.animated = Stickers::LottieAnimationFromDocument(
set.lottiePlayer, set.lottiePlayer,
sticker.documentMedia.get(), sticker.documentMedia.get(),
@ -1737,6 +1745,7 @@ QSize StickersListWidget::boundingBoxSize() const {
void StickersListWidget::paintSticker(Painter &p, Set &set, int y, int section, int index, bool selected, bool deleteSelected) { void StickersListWidget::paintSticker(Painter &p, Set &set, int y, int section, int index, bool selected, bool deleteSelected) {
auto &sticker = set.stickers[index]; auto &sticker = set.stickers[index];
sticker.ensureMediaCreated();
const auto document = sticker.document; const auto document = sticker.document;
const auto &media = sticker.documentMedia; const auto &media = sticker.documentMedia;
if (!document->sticker()) { if (!document->sticker()) {
@ -2130,7 +2139,7 @@ TabbedSelector::InnerFooter *StickersListWidget::getFooter() const {
void StickersListWidget::processHideFinished() { void StickersListWidget::processHideFinished() {
clearSelection(); clearSelection();
clearLottieData(); clearHeavyData();
if (_footer) { if (_footer) {
_footer->clearHeavyData(); _footer->clearHeavyData();
} }
@ -2138,7 +2147,7 @@ void StickersListWidget::processHideFinished() {
void StickersListWidget::processPanelHideFinished() { void StickersListWidget::processPanelHideFinished() {
clearInstalledLocally(); clearInstalledLocally();
clearLottieData(); clearHeavyData();
if (_footer) { if (_footer) {
_footer->clearHeavyData(); _footer->clearHeavyData();
} }
@ -2154,13 +2163,13 @@ void StickersListWidget::setSection(Section section) {
if (_section == section) { if (_section == section) {
return; return;
} }
clearLottieData(); clearHeavyData();
_section = section; _section = section;
} }
void StickersListWidget::clearLottieData() { void StickersListWidget::clearHeavyData() {
for (auto &set : shownSets()) { for (auto &set : shownSets()) {
destroyLottieIn(set); clearHeavyIn(set);
} }
_lottieData.clear(); _lottieData.clear();
} }
@ -2322,23 +2331,6 @@ void StickersListWidget::refreshFooterIcons() {
} }
void StickersListWidget::preloadImages() { void StickersListWidget::preloadImages() {
auto &sets = shownSets();
for (int i = 0, l = sets.size(), k = 0; i < l; ++i) {
int count = sets[i].stickers.size();
if (sets[i].externalLayout) {
accumulate_min(count, _columnCount);
}
for (int j = 0; j != count; ++j) {
if (++k > _columnCount * (_columnCount + 1)) break;
const auto document = sets[i].stickers[j].document;
const auto &media = sets[i].stickers[j].documentMedia;
if (!document || !document->sticker()) continue;
media->checkStickerSmall();
}
if (k > _columnCount * (_columnCount + 1)) break;
}
if (_footer) { if (_footer) {
_footer->preloadImages(); _footer->preloadImages();
} }
@ -2428,8 +2420,7 @@ auto StickersListWidget::collectRecentStickers() -> std::vector<Sticker> {
} }
} else if (!_favedStickersMap.contains(document)) { } else if (!_favedStickersMap.contains(document)) {
result.push_back(Sticker{ result.push_back(Sticker{
document, document
document->createMediaView()
}); });
_custom.push_back(custom); _custom.push_back(custom);
} }

View File

@ -157,6 +157,8 @@ private:
not_null<DocumentData*> document; not_null<DocumentData*> document;
std::shared_ptr<Data::DocumentMedia> documentMedia; std::shared_ptr<Data::DocumentMedia> documentMedia;
Lottie::Animation *animated = nullptr; Lottie::Animation *animated = nullptr;
void ensureMediaCreated();
}; };
struct Set { struct Set {
@ -265,10 +267,10 @@ private:
void markLottieFrameShown(Set &set); void markLottieFrameShown(Set &set);
void checkVisibleLottie(); void checkVisibleLottie();
void pauseInvisibleLottieIn(const SectionInfo &info); void pauseInvisibleLottieIn(const SectionInfo &info);
void destroyLottieIn(Set &set); void clearHeavyIn(Set &set);
void refillLottieData(); void refillLottieData();
void refillLottieData(Set &set); void refillLottieData(Set &set);
void clearLottieData(); void clearHeavyData();
int stickersRight() const; int stickersRight() const;
bool featuredHasAddButton(int index) const; bool featuredHasAddButton(int index) const;