Refresh EditCaptionBox thumbnail when loaded.

This commit is contained in:
John Preston 2018-08-02 17:31:54 +03:00
parent 7f16675d2f
commit 1c5e91c9a8
2 changed files with 78 additions and 13 deletions

View File

@ -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<DocumentData*> document) {
if (_gifPreview) {
return;
} else if (document->isAnimation() && document->loaded()) {
_gifPreview = Media::Clip::MakeReader(document, _msgId, [this](Media::Clip::Notification notification) {
clipCallback(notification);
});

View File

@ -41,7 +41,7 @@ protected:
private:
void updateBoxSize();
void prepareGifPreview(DocumentData *document);
void prepareGifPreview(not_null<DocumentData*> document);
void clipCallback(Media::Clip::Notification notification);
void save();
@ -54,6 +54,9 @@ private:
not_null<Window::Controller*> _controller;
FullMsgId _msgId;
ImagePtr _thumbnailImage;
bool _thumbnailImageLoaded = false;
Fn<void()> _refreshThumbnail;
bool _animated = false;
bool _photo = false;
bool _doc = false;