2020-04-08 15:09:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_reply_preview.h"
|
|
|
|
|
|
|
|
#include "data/data_file_origin.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_document_media.h"
|
|
|
|
#include "data/data_photo.h"
|
2020-05-25 14:16:04 +00:00
|
|
|
#include "data/data_photo_media.h"
|
2020-04-08 15:09:29 +00:00
|
|
|
#include "ui/image/image.h"
|
2022-09-30 07:25:02 +00:00
|
|
|
#include "styles/style_chat.h"
|
2020-04-08 15:09:29 +00:00
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
|
|
|
ReplyPreview::ReplyPreview(not_null<DocumentData*> document)
|
2020-05-21 11:40:10 +00:00
|
|
|
: _document(document) {
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplyPreview::ReplyPreview(not_null<PhotoData*> photo)
|
|
|
|
: _photo(photo) {
|
|
|
|
}
|
|
|
|
|
2020-05-29 14:08:18 +00:00
|
|
|
ReplyPreview::~ReplyPreview() = default;
|
|
|
|
|
2022-12-30 09:37:34 +00:00
|
|
|
void ReplyPreview::prepare(
|
|
|
|
not_null<Image*> image,
|
|
|
|
Images::Options options,
|
|
|
|
bool spoiler) {
|
|
|
|
using namespace Images;
|
2020-05-29 15:10:25 +00:00
|
|
|
if (image->isNull()) {
|
2020-04-08 15:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int w = image->width(), h = image->height();
|
|
|
|
if (w <= 0) w = 1;
|
|
|
|
if (h <= 0) h = 1;
|
|
|
|
auto thumbSize = (w > h)
|
|
|
|
? QSize(
|
|
|
|
w * st::msgReplyBarSize.height() / h,
|
|
|
|
st::msgReplyBarSize.height())
|
|
|
|
: QSize(
|
|
|
|
st::msgReplyBarSize.height(),
|
|
|
|
h * st::msgReplyBarSize.height() / w);
|
2022-12-30 09:37:34 +00:00
|
|
|
thumbSize *= style::DevicePixelRatio();
|
|
|
|
options |= Option::TransparentBackground;
|
2020-04-08 15:09:29 +00:00
|
|
|
auto outerSize = st::msgReplyBarSize.height();
|
2022-12-30 09:37:34 +00:00
|
|
|
auto original = spoiler
|
|
|
|
? image->original().scaled(
|
|
|
|
{ 40, 40 },
|
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation)
|
|
|
|
: image->original();
|
|
|
|
auto prepared = Prepare(std::move(original), thumbSize, {
|
|
|
|
.options = options | (spoiler ? Option::Blur : Option()),
|
|
|
|
.outer = { outerSize, outerSize },
|
|
|
|
});
|
|
|
|
(spoiler ? _spoilered : _regular) = std::make_unique<Image>(
|
|
|
|
std::move(prepared));
|
|
|
|
_good = spoiler || ((options & Option::Blur) == 0);
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 10:59:08 +00:00
|
|
|
Image *ReplyPreview::image(
|
|
|
|
Data::FileOrigin origin,
|
2022-12-30 09:37:34 +00:00
|
|
|
not_null<PeerData*> context,
|
|
|
|
bool spoiler) {
|
|
|
|
auto &image = spoiler ? _spoilered : _regular;
|
|
|
|
auto &checked = spoiler ? _checkedSpoilered : _checkedRegular;
|
|
|
|
if (checked) {
|
|
|
|
return image.get();
|
|
|
|
} else if (_document) {
|
|
|
|
if (!image || (!_good && _document->hasThumbnail())) {
|
2020-05-25 14:16:04 +00:00
|
|
|
if (!_documentMedia) {
|
|
|
|
_documentMedia = _document->createMediaView();
|
|
|
|
_documentMedia->thumbnailWanted(origin);
|
|
|
|
}
|
|
|
|
const auto thumbnail = _documentMedia->thumbnail();
|
2020-04-08 15:09:29 +00:00
|
|
|
const auto option = _document->isVideoMessage()
|
2022-01-21 12:31:39 +00:00
|
|
|
? Images::Option::RoundCircle
|
2020-04-08 15:09:29 +00:00
|
|
|
: Images::Option::None;
|
2022-12-30 09:37:34 +00:00
|
|
|
if (spoiler) {
|
|
|
|
if (const auto image = _documentMedia->thumbnailInline()) {
|
|
|
|
prepare(image, option, true);
|
|
|
|
} else if (thumbnail) {
|
|
|
|
prepare(thumbnail, option, true);
|
|
|
|
}
|
|
|
|
} else if (thumbnail) {
|
2020-04-08 15:09:29 +00:00
|
|
|
prepare(thumbnail, option);
|
2022-12-30 09:37:34 +00:00
|
|
|
} else if (!image) {
|
2020-04-08 15:09:29 +00:00
|
|
|
if (const auto image = _documentMedia->thumbnailInline()) {
|
2022-01-21 12:31:39 +00:00
|
|
|
prepare(image, option | Images::Option::Blur);
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 14:32:38 +00:00
|
|
|
if (_good || !_document->hasThumbnail()) {
|
2022-12-30 09:37:34 +00:00
|
|
|
checked = true;
|
2020-05-21 11:40:10 +00:00
|
|
|
_documentMedia = nullptr;
|
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Assert(_photo != nullptr);
|
2022-12-30 09:37:34 +00:00
|
|
|
if (!image || !_good) {
|
2021-12-03 10:59:08 +00:00
|
|
|
const auto inlineThumbnailBytes = _photo->inlineThumbnailBytes();
|
2020-05-25 14:16:04 +00:00
|
|
|
if (!_photoMedia) {
|
|
|
|
_photoMedia = _photo->createMediaView();
|
2021-12-03 10:59:08 +00:00
|
|
|
}
|
2022-12-30 09:37:34 +00:00
|
|
|
using Size = PhotoSize;
|
2021-12-03 10:59:08 +00:00
|
|
|
const auto loadThumbnail = inlineThumbnailBytes.isEmpty()
|
2022-12-30 09:37:34 +00:00
|
|
|
|| (!spoiler
|
|
|
|
&& _photoMedia->autoLoadThumbnailAllowed(context));
|
2021-12-03 10:59:08 +00:00
|
|
|
if (loadThumbnail) {
|
2022-12-30 09:37:34 +00:00
|
|
|
_photoMedia->wanted(Size::Small, origin);
|
2020-05-25 14:16:04 +00:00
|
|
|
}
|
2022-12-30 09:37:34 +00:00
|
|
|
if (spoiler) {
|
|
|
|
if (const auto blurred = _photoMedia->thumbnailInline()) {
|
|
|
|
prepare(blurred, {}, true);
|
|
|
|
} else if (const auto small = _photoMedia->image(Size::Small)) {
|
|
|
|
prepare(small, {}, true);
|
|
|
|
} else if (const auto large = _photoMedia->image(Size::Large)) {
|
|
|
|
prepare(large, {}, true);
|
|
|
|
}
|
|
|
|
} else if (const auto small = _photoMedia->image(Size::Small)) {
|
|
|
|
prepare(small, {});
|
|
|
|
} else if (const auto large = _photoMedia->image(Size::Large)) {
|
|
|
|
prepare(large, {});
|
|
|
|
} else if (!image) {
|
2020-05-26 10:13:32 +00:00
|
|
|
if (const auto blurred = _photoMedia->thumbnailInline()) {
|
2022-01-21 12:31:39 +00:00
|
|
|
prepare(blurred, Images::Option::Blur);
|
2020-05-26 10:13:32 +00:00
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
2020-05-26 14:32:38 +00:00
|
|
|
if (_good) {
|
2022-12-30 09:37:34 +00:00
|
|
|
checked = true;
|
2020-05-26 14:32:38 +00:00
|
|
|
_photoMedia = nullptr;
|
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-30 09:37:34 +00:00
|
|
|
return image.get();
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:37:34 +00:00
|
|
|
bool ReplyPreview::loaded(bool spoiler) const {
|
|
|
|
return spoiler ? _checkedSpoilered : _checkedRegular;
|
2020-10-12 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:09:29 +00:00
|
|
|
} // namespace Data
|