File: data_reply_preview.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (146 lines) | stat: -rw-r--r-- 4,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
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"
#include "data/data_photo_media.h"
#include "ui/image/image.h"
#include "styles/style_chat.h"

namespace Data {

ReplyPreview::ReplyPreview(not_null<DocumentData*> document)
: _document(document) {
}

ReplyPreview::ReplyPreview(not_null<PhotoData*> photo)
: _photo(photo) {
}

ReplyPreview::~ReplyPreview() = default;

void ReplyPreview::prepare(
		not_null<Image*> image,
		Images::Options options,
		bool spoiler) {
	using namespace Images;
	if (image->isNull()) {
		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);
	thumbSize *= style::DevicePixelRatio();
	options |= Option::TransparentBackground;
	auto outerSize = st::msgReplyBarSize.height();
	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);
}

Image *ReplyPreview::image(
		Data::FileOrigin origin,
		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())) {
			if (!_documentMedia) {
				_documentMedia = _document->createMediaView();
				_documentMedia->thumbnailWanted(origin);
			}
			const auto thumbnail = _documentMedia->thumbnail();
			const auto option = _document->isVideoMessage()
				? Images::Option::RoundCircle
				: Images::Option::None;
			if (spoiler) {
				if (const auto image = _documentMedia->thumbnailInline()) {
					prepare(image, option, true);
				} else if (thumbnail) {
					prepare(thumbnail, option, true);
				}
			} else if (thumbnail) {
				prepare(thumbnail, option);
			} else if (!image) {
				if (const auto image = _documentMedia->thumbnailInline()) {
					prepare(image, option | Images::Option::Blur);
				}
			}
			if (_good || !_document->hasThumbnail()) {
				checked = true;
				_documentMedia = nullptr;
			}
		}
	} else {
		Assert(_photo != nullptr);
		if (!image || !_good) {
			const auto inlineThumbnailBytes = _photo->inlineThumbnailBytes();
			if (!_photoMedia) {
				_photoMedia = _photo->createMediaView();
			}
			using Size = PhotoSize;
			const auto loadThumbnail = inlineThumbnailBytes.isEmpty()
				|| (!spoiler
					&& _photoMedia->autoLoadThumbnailAllowed(context));
			if (loadThumbnail) {
				_photoMedia->wanted(Size::Small, origin);
			}
			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) {
				if (const auto blurred = _photoMedia->thumbnailInline()) {
					prepare(blurred, Images::Option::Blur);
				}
			}
			if (_good) {
				checked = true;
				_photoMedia = nullptr;
			}
		}
	}
	return image.get();
}

bool ReplyPreview::loaded(bool spoiler) const {
	return spoiler ? _checkedSpoilered : _checkedRegular;
}

} // namespace Data