From 1c5e91c9a87498e6e33bbc6f276ec8ec77b4a2b3 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 2 Aug 2018 17:31:54 +0300 Subject: [PATCH] Refresh EditCaptionBox thumbnail when loaded. --- .../SourceFiles/boxes/edit_caption_box.cpp | 86 ++++++++++++++++--- Telegram/SourceFiles/boxes/edit_caption_box.h | 5 +- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index 058feebed..5cd3aea38 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -20,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_controller.h" #include "mainwidget.h" #include "layout.h" +#include "auth_session.h" #include "styles/style_history.h" #include "styles/style_boxes.h" @@ -69,8 +70,22 @@ EditCaptionBox::EditCaptionBox( } else { _thumbw = st::msgFileThumbSize; } - auto options = Images::Option::Smooth | Images::Option::RoundedSmall | Images::Option::RoundedTopLeft | Images::Option::RoundedTopRight | Images::Option::RoundedBottomLeft | Images::Option::RoundedBottomRight; - _thumb = Images::pixmap(image->pix().toImage(), _thumbw * cIntRetinaFactor(), 0, options, st::msgFileThumbSize, st::msgFileThumbSize); + _thumbnailImage = image; + _refreshThumbnail = [=] { + auto options = Images::Option::Smooth + | Images::Option::RoundedSmall + | Images::Option::RoundedTopLeft + | Images::Option::RoundedTopRight + | Images::Option::RoundedBottomLeft + | Images::Option::RoundedBottomRight; + _thumb = Images::pixmap( + image->pix().toImage(), + _thumbw * cIntRetinaFactor(), + 0, + options, + st::msgFileThumbSize, + st::msgFileThumbSize); + }; } if (doc) { @@ -88,6 +103,9 @@ EditCaptionBox::EditCaptionBox( _isImage = doc->isImage(); _isAudio = (doc->isVoiceMessage() || doc->isAudioFile()); } + if (_refreshThumbnail) { + _refreshThumbnail(); + } } else { int32 maxW = 0, maxH = 0; if (_animated) { @@ -106,13 +124,33 @@ EditCaptionBox::EditCaptionBox( maxH = limitH; } } - _thumb = image->pixNoCache(maxW * cIntRetinaFactor(), maxH * cIntRetinaFactor(), Images::Option::Smooth | Images::Option::Blurred, maxW, maxH); + _thumbnailImage = image; + _refreshThumbnail = [=] { + const auto options = Images::Option::Smooth + | Images::Option::Blurred; + _thumb = image->pixNoCache( + maxW * cIntRetinaFactor(), + maxH * cIntRetinaFactor(), + options, + maxW, + maxH); + }; prepareGifPreview(doc); } else { maxW = dimensions.width(); maxH = dimensions.height(); - _thumb = image->pixNoCache(maxW * cIntRetinaFactor(), maxH * cIntRetinaFactor(), Images::Option::Smooth, maxW, maxH); + _thumbnailImage = image; + _refreshThumbnail = [=] { + _thumb = image->pixNoCache( + maxW * cIntRetinaFactor(), + maxH * cIntRetinaFactor(), + Images::Option::Smooth, + maxW, + maxH); + }; } + _refreshThumbnail(); + int32 tw = _thumb.width(), th = _thumb.height(); if (!tw || !th) { tw = th = 1; @@ -132,11 +170,37 @@ EditCaptionBox::EditCaptionBox( } _thumbx = (st::boxWideWidth - _thumbw) / 2; - _thumb = App::pixmapFromImageInPlace(_thumb.toImage().scaled(_thumbw * cIntRetinaFactor(), _thumbh * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); - _thumb.setDevicePixelRatio(cRetinaFactor()); + const auto prepareBasicThumb = _refreshThumbnail; + const auto scaleThumbDown = [=] { + _thumb = App::pixmapFromImageInPlace(_thumb.toImage().scaled( + _thumbw * cIntRetinaFactor(), + _thumbh * cIntRetinaFactor(), + Qt::IgnoreAspectRatio, + Qt::SmoothTransformation)); + _thumb.setDevicePixelRatio(cRetinaFactor()); + }; + _refreshThumbnail = [=] { + prepareBasicThumb(); + scaleThumbDown(); + }; + scaleThumbDown(); } Assert(_animated || _photo || _doc); + _thumbnailImageLoaded = _thumbnailImage + ? _thumbnailImage->loaded() + : true; + subscribe(Auth().downloaderTaskFinished(), [=] { + if (!_thumbnailImageLoaded && _thumbnailImage->loaded()) { + _thumbnailImageLoaded = true; + _refreshThumbnail(); + update(); + } + if (doc && doc->isAnimation() && doc->loaded() && !_gifPreview) { + prepareGifPreview(doc); + } + }); + _field.create( this, st::confirmCaptionArea, @@ -152,12 +216,10 @@ EditCaptionBox::EditCaptionBox( DefaultEditLinkCallback(_controller, _field)); } -void EditCaptionBox::prepareGifPreview(DocumentData *document) { - auto createGifPreview = [document] { - return (document && document->isAnimation()); - }; - auto createGifPreviewResult = createGifPreview(); // Clang freeze workaround. - if (createGifPreviewResult) { +void EditCaptionBox::prepareGifPreview(not_null document) { + if (_gifPreview) { + return; + } else if (document->isAnimation() && document->loaded()) { _gifPreview = Media::Clip::MakeReader(document, _msgId, [this](Media::Clip::Notification notification) { clipCallback(notification); }); diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.h b/Telegram/SourceFiles/boxes/edit_caption_box.h index 38d6626ff..eadb149fb 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.h +++ b/Telegram/SourceFiles/boxes/edit_caption_box.h @@ -41,7 +41,7 @@ protected: private: void updateBoxSize(); - void prepareGifPreview(DocumentData *document); + void prepareGifPreview(not_null document); void clipCallback(Media::Clip::Notification notification); void save(); @@ -54,6 +54,9 @@ private: not_null _controller; FullMsgId _msgId; + ImagePtr _thumbnailImage; + bool _thumbnailImageLoaded = false; + Fn _refreshThumbnail; bool _animated = false; bool _photo = false; bool _doc = false;