From 58f82620e019c56ec62bdb30996f08a67d9a83ab Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 21 May 2020 16:49:47 +0400 Subject: [PATCH] Simplify media overview layouts. --- Telegram/SourceFiles/data/data_document.cpp | 10 ++-- .../SourceFiles/overview/overview_layout.cpp | 24 -------- .../SourceFiles/overview/overview_layout.h | 59 +++---------------- .../passport/passport_form_controller.cpp | 2 +- Telegram/SourceFiles/storage/file_download.h | 8 ++- Telegram/lib_rpl | 2 +- 6 files changed, 23 insertions(+), 82 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index dd073dda9..7f46a11e8 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -689,7 +689,7 @@ void DocumentData::loadThumbnail(Data::FileOrigin origin) { _thumbnailLoader = nullptr; _flags |= Flag::ThumbnailFailed; }, [=] { - if (!_thumbnailLoader->cancelled()) { + if (_thumbnailLoader && !_thumbnailLoader->cancelled()) { if (auto image = _thumbnailLoader->imageData(); image.isNull()) { _flags |= Flag::ThumbnailFailed; } else if (const auto active = activeMediaView()) { @@ -697,7 +697,7 @@ void DocumentData::loadThumbnail(Data::FileOrigin origin) { } } _thumbnailLoader = nullptr; - }) | rpl::release(); + }, _thumbnailLoader->lifetime()); _thumbnailLoader->start(); } @@ -806,7 +806,7 @@ void DocumentData::finishLoad() { const auto guard = gsl::finally([&] { destroyLoader(); }); - if (_loader->cancelled()) { + if (!_loader || _loader->cancelled()) { _flags |= Flag::DownloadCancelled; return; } @@ -1004,7 +1004,7 @@ void DocumentData::handleLoaderUpdates() { ) | rpl::start_with_next_error_done([=] { _owner->documentLoadProgress(this); }, [=](bool started) { - if (started) { + if (started && _loader) { const auto origin = _loader->fileOrigin(); const auto failedFileName = _loader->fileName(); const auto retry = [=] { @@ -1035,7 +1035,7 @@ void DocumentData::handleLoaderUpdates() { }, [=] { finishLoad(); _owner->documentLoadDone(this); - }) | rpl::release(); + }, _loader->lifetime()); } diff --git a/Telegram/SourceFiles/overview/overview_layout.cpp b/Telegram/SourceFiles/overview/overview_layout.cpp index bea248d03..e6e4a2311 100644 --- a/Telegram/SourceFiles/overview/overview_layout.cpp +++ b/Telegram/SourceFiles/overview/overview_layout.cpp @@ -140,11 +140,6 @@ void Checkbox::startAnimation() { _pression.start(_updateCallback, showPressed ? 0. : 1., showPressed ? 1. : 0., st::overviewCheck.duration); } -MsgId AbstractItem::msgId() const { - auto item = getItem(); - return item ? item->id : 0; -} - ItemBase::ItemBase( not_null delegate, not_null parent) @@ -295,25 +290,6 @@ void StatusText::setSize(int newSize) { _size = newSize; } -Date::Date(const QDate &date, bool month) -: _date(date) -, _text(month ? langMonthFull(date) : langDayOfMonthFull(date)) { - AddComponents(Info::Bit()); -} - -void Date::initDimensions() { - _maxw = st::normalFont->width(_text); - _minh = st::linksDateMargin.top() + st::normalFont->height + st::linksDateMargin.bottom() + st::linksBorder; -} - -void Date::paint(Painter &p, const QRect &clip, TextSelection selection, const PaintContext *context) { - if (clip.intersects(QRect(0, st::linksDateMargin.top(), _width, st::normalFont->height))) { - p.setPen(st::linksDateColor); - p.setFont(st::semiboldFont); - p.drawTextLeft(0, st::linksDateMargin.top(), _width, _text); - } -} - Photo::Photo( not_null delegate, not_null parent, diff --git a/Telegram/SourceFiles/overview/overview_layout.h b/Telegram/SourceFiles/overview/overview_layout.h index d7b5a78f3..eabac1376 100644 --- a/Telegram/SourceFiles/overview/overview_layout.h +++ b/Telegram/SourceFiles/overview/overview_layout.h @@ -37,35 +37,17 @@ public: }; -class AbstractItem : public LayoutItemBase { -public: - virtual void paint(Painter &p, const QRect &clip, TextSelection selection, const PaintContext *context) = 0; - - virtual ItemBase *toMediaItem() { - return nullptr; - } - virtual const ItemBase *toMediaItem() const { - return nullptr; - } - - virtual HistoryItem *getItem() const { - return nullptr; - } - virtual DocumentData *getDocument() const { - return nullptr; - } - MsgId msgId() const; - - virtual void invalidateCache() { - } - -}; - -class ItemBase : public AbstractItem { +class ItemBase : public LayoutItemBase { public: ItemBase(not_null delegate, not_null parent); ~ItemBase(); + virtual void paint( + Painter &p, + const QRect &clip, + TextSelection selection, + const PaintContext *context) = 0; + QDateTime dateTime() const; void setPosition(int position) { @@ -75,20 +57,14 @@ public: return _position; } - ItemBase *toMediaItem() final override { - return this; - } - const ItemBase *toMediaItem() const final override { - return this; - } - HistoryItem *getItem() const final override { + HistoryItem *getItem() const { return _parent; } void clickHandlerActiveChanged(const ClickHandlerPtr &action, bool active) override; void clickHandlerPressedChanged(const ClickHandlerPtr &action, bool pressed) override; - void invalidateCache() override; + void invalidateCache(); virtual void clearHeavyPart() { } @@ -194,19 +170,6 @@ struct Info : public RuntimeComponent { int top = 0; }; -class Date : public AbstractItem { -public: - Date(const QDate &date, bool month); - - void initDimensions() override; - void paint(Painter &p, const QRect &clip, TextSelection selection, const PaintContext *context) override; - -private: - QDate _date; - QString _text; - -}; - class Photo final : public ItemBase { public: Photo( @@ -321,10 +284,6 @@ public: QPoint point, StateRequest request) const override; - DocumentData *getDocument() const override { - return _data; - } - protected: float64 dataProgress() const override; bool dataFinished() const override; diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index bbb94e8cb..f16086dd4 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -1745,7 +1745,7 @@ void FormController::loadFile(File &file) { fileLoadFail(key); }, [=] { fileLoadDone(key, loader->bytes()); - }) | rpl::release(); + }, loader->lifetime()); loader->start(); } diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h index af597046f..5eb7f0163 100644 --- a/Telegram/SourceFiles/storage/file_download.h +++ b/Telegram/SourceFiles/storage/file_download.h @@ -112,6 +112,10 @@ public: return _updates.events(); } + rpl::lifetime &lifetime() { + return _lifetime; + } + protected: enum class LocalStatus { NotTried, @@ -138,7 +142,6 @@ protected: [[nodiscard]] QByteArray readLoadedPartBack(int offset, int size); const not_null _session; - rpl::event_stream _updates; bool _autoLoading = false; uint8 _cacheTag = 0; @@ -163,6 +166,9 @@ protected: mutable QByteArray _imageFormat; mutable QImage _imageData; + rpl::lifetime _lifetime; + rpl::event_stream _updates; + }; [[nodiscard]] std::unique_ptr CreateFileLoader( diff --git a/Telegram/lib_rpl b/Telegram/lib_rpl index df918b9d3..88ae67c59 160000 --- a/Telegram/lib_rpl +++ b/Telegram/lib_rpl @@ -1 +1 @@ -Subproject commit df918b9d3e2cce729cf21de5c69a01a847e7107c +Subproject commit 88ae67c59ab791084f43163d4484c9874003ac42