File: reactions_settings_box.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 (591 lines) | stat: -rw-r--r-- 17,002 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
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 "boxes/reactions_settings_box.h"

#include "base/unixtime.h"
#include "data/data_user.h"
#include "data/data_document.h"
#include "data/data_document_media.h"
#include "data/data_message_reactions.h"
#include "data/data_session.h"
#include "history/admin_log/history_admin_log_item.h"
#include "history/history.h"
#include "history/history_item.h"
#include "history/view/history_view_element.h"
#include "history/view/reactions/history_view_reactions_strip.h"
#include "lang/lang_keys.h"
#include "boxes/premium_preview_box.h"
#include "main/main_session.h"
#include "settings/settings_common.h"
#include "settings/settings_premium.h"
#include "ui/chat/chat_style.h"
#include "ui/chat/chat_theme.h"
#include "ui/effects/scroll_content_shadow.h"
#include "ui/layers/generic_box.h"
#include "ui/toasts/common_toasts.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/scroll_area.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/animated_icon.h"
#include "ui/painter.h"
#include "window/section_widget.h"
#include "window/window_session_controller.h"
#include "styles/style_boxes.h"
#include "styles/style_chat.h"
#include "styles/style_layers.h"
#include "styles/style_media_player.h" // mediaPlayerMenuCheck
#include "styles/style_settings.h"

namespace {

PeerId GenerateUser(not_null<History*> history, const QString &name) {
	Expects(history->peer->isUser());

	const auto peerId = Data::FakePeerIdForJustName(name);
	history->owner().processUser(MTP_user(
		MTP_flags(MTPDuser::Flag::f_first_name | MTPDuser::Flag::f_min),
		peerToBareMTPInt(peerId),
		MTP_long(0),
		MTP_string(tr::lng_settings_chat_message_reply_from(tr::now)),
		MTPstring(), // last name
		MTPstring(), // username
		MTPstring(), // phone
		MTPUserProfilePhoto(), // profile photo
		MTPUserStatus(), // status
		MTP_int(0), // bot info version
		MTPVector<MTPRestrictionReason>(), // restrictions
		MTPstring(), // bot placeholder
		MTPstring(), // lang code
		MTPEmojiStatus(),
		MTPVector<MTPUsername>()));
	return peerId;
}

AdminLog::OwnedItem GenerateItem(
		not_null<HistoryView::ElementDelegate*> delegate,
		not_null<History*> history,
		PeerId from,
		MsgId replyTo,
		const QString &text) {
	Expects(history->peer->isUser());

	const auto item = history->addNewLocalMessage(
		history->nextNonHistoryEntryId(),
		MessageFlag::FakeHistoryItem
			| MessageFlag::HasFromId
			| MessageFlag::HasReplyInfo,
		UserId(), // via
		replyTo,
		base::unixtime::now(), // date
		from,
		QString(), // postAuthor
		TextWithEntities{ .text = text },
		MTP_messageMediaEmpty(),
		HistoryMessageMarkupData(),
		uint64(0)); // groupedId

	return AdminLog::OwnedItem(delegate, item);
}

void AddMessage(
		not_null<Ui::VerticalLayout*> container,
		not_null<Window::SessionController*> controller,
		rpl::producer<Data::ReactionId> &&idValue,
		int width) {

	const auto widget = container->add(
		object_ptr<Ui::RpWidget>(container),
		style::margins(
			0,
			st::settingsSectionSkip,
			0,
			st::settingsPrivacySkipTop));

	class Delegate final : public HistoryView::SimpleElementDelegate {
	public:
		using HistoryView::SimpleElementDelegate::SimpleElementDelegate;
	private:
		HistoryView::Context elementContext() override {
			return HistoryView::Context::ContactPreview;
		}
	};

	struct State {
		AdminLog::OwnedItem reply;
		AdminLog::OwnedItem item;
		std::unique_ptr<Delegate> delegate;
		std::unique_ptr<Ui::ChatStyle> style;

		struct {
			std::vector<rpl::lifetime> lifetimes;
			bool flag = false;
		} icons;
	};
	const auto state = container->lifetime().make_state<State>();
	state->delegate = std::make_unique<Delegate>(
		controller,
		crl::guard(widget, [=] { widget->update(); }));
	state->style = std::make_unique<Ui::ChatStyle>();
	state->style->apply(controller->defaultChatTheme().get());
	state->icons.lifetimes = std::vector<rpl::lifetime>(2);

	const auto history = controller->session().data().history(
		PeerData::kServiceNotificationsId);
	state->reply = GenerateItem(
		state->delegate.get(),
		history,
		GenerateUser(
			history,
			tr::lng_settings_chat_message_reply_from(tr::now)),
		0,
		tr::lng_settings_chat_message_reply(tr::now));
	auto message = GenerateItem(
		state->delegate.get(),
		history,
		history->peer->id,
		state->reply->data()->fullId().msg,
		tr::lng_settings_chat_message(tr::now));
	const auto view = message.get();
	state->item = std::move(message);

	const auto padding = st::settingsForwardPrivacyPadding;

	const auto updateWidgetSize = [=](int width) {
		const auto height = view->resizeGetHeight(width);
		const auto top = view->marginTop();
		const auto bottom = view->marginBottom();
		const auto full = padding + top + height + bottom + padding;
		widget->resize(width, full);
	};
	widget->widthValue(
	) | rpl::filter(
		rpl::mappers::_1 >= (st::historyMinimalWidth / 2)
	) | rpl::start_with_next(updateWidgetSize, widget->lifetime());
	updateWidgetSize(width);

	const auto rightSize = st::settingsReactionCornerSize;
	const auto rightRect = [=] {
		const auto viewInner = view->innerGeometry();
		return QRect(
			viewInner.x() + viewInner.width(),
			padding
				+ view->marginTop()
				+ view->resizeGetHeight(widget->width())
				- rightSize.height(),
			rightSize.width(),
			rightSize.height()).translated(st::settingsReactionCornerSkip);
	};

	widget->paintRequest(
	) | rpl::start_with_next([=](const QRect &rect) {
		Window::SectionWidget::PaintBackground(
			controller,
			controller->defaultChatTheme().get(), // #TODO themes
			widget,
			rect);

		Painter p(widget);
		auto hq = PainterHighQualityEnabler(p);
		const auto theme = controller->defaultChatTheme().get();
		auto context = theme->preparePaintContext(
			state->style.get(),
			widget->rect(),
			widget->rect(),
			controller->isGifPausedAtLeastFor(
				Window::GifPauseReason::Layer));
		context.outbg = view->hasOutLayout();

		{
			const auto radius = rightSize.height() / 2;
			const auto r = rightRect();
			const auto &st = context.st->messageStyle(
				context.outbg,
				context.selected());
			p.setPen(Qt::NoPen);
			p.setBrush(st.msgShadow);
			p.drawRoundedRect(r.translated(0, st::msgShadow), radius, radius);
			p.setBrush(st.msgBg);
			p.drawRoundedRect(r, radius, radius);
		}

		p.translate(padding / 2, padding + view->marginBottom());
		view->draw(p, context);
	}, widget->lifetime());

	auto selectedId = rpl::duplicate(idValue);
	std::move(
		selectedId
	) | rpl::start_with_next([
		=,
		idValue = std::move(idValue),
		iconSize = st::settingsReactionMessageSize
	](const Data::ReactionId &id) {
		const auto index = state->icons.flag ? 1 : 0;
		state->icons.flag = !state->icons.flag;
		state->icons.lifetimes[index] = rpl::lifetime();
		const auto &reactions = controller->session().data().reactions();
		auto iconPositionValue = widget->geometryValue(
		) | rpl::map([=](const QRect &r) {
			return widget->pos()
				+ rightRect().topLeft()
				+ QPoint(
					(rightSize.width() - iconSize) / 2,
					(rightSize.height() - iconSize) / 2);
		});
		auto destroys = rpl::duplicate(
			idValue
		) | rpl::skip(1) | rpl::to_empty;
		if (const auto customId = id.custom()) {
			AddReactionCustomIcon(
				container,
				std::move(iconPositionValue),
				iconSize,
				controller,
				customId,
				std::move(destroys),
				&state->icons.lifetimes[index]);
			return;
		}
		for (const auto &r : reactions.list(Data::Reactions::Type::Active)) {
			if (r.id != id) {
				continue;
			}
			AddReactionAnimatedIcon(
				container,
				std::move(iconPositionValue),
				iconSize,
				r,
				rpl::never<>(),
				std::move(destroys),
				&state->icons.lifetimes[index]);
			return;
		}
	}, widget->lifetime());
}

not_null<Ui::RpWidget*> AddReactionIconWrap(
		not_null<Ui::RpWidget*> parent,
		rpl::producer<QPoint> iconPositionValue,
		int iconSize,
		Fn<void(not_null<QWidget*>, QPainter&)> paintCallback,
		rpl::producer<> &&destroys,
		not_null<rpl::lifetime*> stateLifetime) {
	struct State {
		base::unique_qptr<Ui::RpWidget> widget;
		Ui::Animations::Simple finalAnimation;
	};

	const auto state = stateLifetime->make_state<State>();
	state->widget = base::make_unique_q<Ui::RpWidget>(parent);

	const auto widget = state->widget.get();
	widget->resize(iconSize, iconSize);
	widget->setAttribute(Qt::WA_TransparentForMouseEvents);

	std::move(
		iconPositionValue
	) | rpl::start_with_next([=](const QPoint &point) {
		widget->moveToLeft(point.x(), point.y());
	}, widget->lifetime());

	const auto update = crl::guard(widget, [=] { widget->update(); });

	widget->paintRequest(
	) | rpl::start_with_next([=] {
		auto p = QPainter(widget);

		if (state->finalAnimation.animating()) {
			const auto progress = 1. - state->finalAnimation.value(0.);
			const auto size = widget->size();
			const auto scaledSize = size * progress;
			const auto scaledCenter = QPoint(
				(size.width() - scaledSize.width()) / 2.,
				(size.height() - scaledSize.height()) / 2.);
			p.setOpacity(progress);
			p.translate(scaledCenter);
			p.scale(progress, progress);
		}

		paintCallback(widget, p);
	}, widget->lifetime());

	std::move(
		destroys
	) | rpl::take(1) | rpl::start_with_next([=, from = 0., to = 1.] {
		state->finalAnimation.start(
			[=](float64 value) {
				update();
				if (value == to) {
					stateLifetime->destroy();
				}
			},
			from,
			to,
			st::defaultPopupMenu.showDuration);
	}, widget->lifetime());

	widget->raise();
	widget->show();

	return widget;
}

} // namespace

void AddReactionAnimatedIcon(
		not_null<Ui::RpWidget*> parent,
		rpl::producer<QPoint> iconPositionValue,
		int iconSize,
		const Data::Reaction &reaction,
		rpl::producer<> &&selects,
		rpl::producer<> &&destroys,
		not_null<rpl::lifetime*> stateLifetime) {
	struct State {
		struct Entry {
			std::shared_ptr<Data::DocumentMedia> media;
			std::shared_ptr<Ui::AnimatedIcon> icon;
		};
		Entry appear;
		Entry select;
		bool appearAnimated = false;
		rpl::lifetime loadingLifetime;
	};
	const auto state = stateLifetime->make_state<State>();

	state->appear.media = reaction.appearAnimation->createMediaView();
	state->select.media = reaction.selectAnimation->createMediaView();
	state->appear.media->checkStickerLarge();
	state->select.media->checkStickerLarge();
	rpl::single() | rpl::then(
		reaction.appearAnimation->session().downloaderTaskFinished()
	) | rpl::start_with_next([=] {
		const auto check = [&](State::Entry &entry) {
			if (!entry.media) {
				return true;
			} else if (!entry.media->loaded()) {
				return false;
			}
			entry.icon = HistoryView::Reactions::DefaultIconFactory(
				entry.media.get(),
				iconSize);
			entry.media = nullptr;
			return true;
		};
		if (check(state->select) && check(state->appear)) {
			state->loadingLifetime.destroy();
		}
	}, state->loadingLifetime);

	const auto paintCallback = [=](not_null<QWidget*> widget, QPainter &p) {
		const auto paintFrame = [&](not_null<Ui::AnimatedIcon*> animation) {
			const auto frame = animation->frame();
			p.drawImage(
				QRect(
					(widget->width() - iconSize) / 2,
					(widget->height() - iconSize) / 2,
					iconSize,
					iconSize),
				frame);
		};

		const auto appear = state->appear.icon.get();
		if (appear && !state->appearAnimated) {
			state->appearAnimated = true;
			appear->animate(crl::guard(widget, [=] { widget->update(); }));
		}
		if (appear && appear->animating()) {
			paintFrame(appear);
		} else if (const auto select = state->select.icon.get()) {
			paintFrame(select);
		}

	};
	const auto widget = AddReactionIconWrap(
		parent,
		std::move(iconPositionValue),
		iconSize,
		paintCallback,
		std::move(destroys),
		stateLifetime);

	std::move(
		selects
	) | rpl::start_with_next([=] {
		const auto select = state->select.icon.get();
		if (select && !select->animating()) {
			select->animate(crl::guard(widget, [=] { widget->update(); }));
		}
	}, widget->lifetime());
}

void AddReactionCustomIcon(
		not_null<Ui::RpWidget*> parent,
		rpl::producer<QPoint> iconPositionValue,
		int iconSize,
		not_null<Window::SessionController*> controller,
		DocumentId customId,
		rpl::producer<> &&destroys,
		not_null<rpl::lifetime*> stateLifetime) {
	struct State {
		std::unique_ptr<Ui::Text::CustomEmoji> custom;
		Fn<void()> repaint;
	};
	const auto state = stateLifetime->make_state<State>();
	static constexpr auto tag = Data::CustomEmojiManager::SizeTag::Normal;
	state->custom = controller->session().data().customEmojiManager().create(
		customId,
		[=] { state->repaint(); },
		tag);

	const auto paintCallback = [=](not_null<QWidget*> widget, QPainter &p) {
		const auto ratio = style::DevicePixelRatio();
		const auto size = Data::FrameSizeFromTag(tag) / ratio;
		state->custom->paint(p, {
			.textColor = st::windowFg->c,
			.now = crl::now(),
			.position = QPoint(
				(widget->width() - size) / 2,
				(widget->height() - size) / 2),
			.paused = controller->isGifPausedAtLeastFor(
				Window::GifPauseReason::Layer),
		});
	};
	const auto widget = AddReactionIconWrap(
		parent,
		std::move(iconPositionValue),
		iconSize,
		paintCallback,
		std::move(destroys),
		stateLifetime);
	state->repaint = crl::guard(widget, [=] { widget->update(); });
}

void ReactionsSettingsBox(
		not_null<Ui::GenericBox*> box,
		not_null<Window::SessionController*> controller) {

	struct State {
		rpl::variable<Data::ReactionId> selectedId;
	};

	const auto &reactions = controller->session().data().reactions();
	const auto state = box->lifetime().make_state<State>();
	state->selectedId = reactions.favoriteId();

	const auto pinnedToTop = box->setPinnedToTopContent(
		object_ptr<Ui::VerticalLayout>(box));

	auto idValue = state->selectedId.value();
	AddMessage(pinnedToTop, controller, std::move(idValue), box->width());

	Settings::AddSubsectionTitle(
		pinnedToTop,
		tr::lng_settings_chat_reactions_subtitle());

	const auto container = box->verticalLayout();

	const auto check = Ui::CreateChild<Ui::RpWidget>(container.get());
	check->resize(st::settingsReactionCornerSize);
	check->setAttribute(Qt::WA_TransparentForMouseEvents);
	check->paintRequest(
	) | rpl::start_with_next([=] {
		Painter p(check);
		st::mediaPlayerMenuCheck.paintInCenter(p, check->rect());
	}, check->lifetime());
	const auto checkButton = [=](not_null<const Ui::RpWidget*> button) {
		check->moveToRight(
			st::settingsButtonRightSkip,
			button->y() + (button->height() - check->height()) / 2);
	};

	auto firstCheckedButton = (Ui::RpWidget*)(nullptr);
	const auto premiumPossible = controller->session().premiumPossible();
	auto list = reactions.list(Data::Reactions::Type::Active);
	if (const auto favorite = reactions.favorite()) {
		if (favorite->id.custom()) {
			list.insert(begin(list), *favorite);
		}
	}
	for (const auto &r : list) {
		const auto button = Settings::AddButton(
			container,
			rpl::single<QString>(base::duplicate(r.title)),
			st::settingsButton);

		const auto premium = r.premium;
		if (premium && !premiumPossible) {
			continue;
		}

		const auto iconSize = st::settingsReactionSize;
		const auto left = button->st().iconLeft;
		auto iconPositionValue = button->sizeValue(
		) | rpl::map([=](const QSize &s) {
			return QPoint(
				left + st::settingsReactionRightSkip,
				(s.height() - iconSize) / 2);
		});
		if (const auto customId = r.id.custom()) {
			AddReactionCustomIcon(
				button,
				std::move(iconPositionValue),
				iconSize,
				controller,
				customId,
				rpl::never<>(),
				&button->lifetime());
		} else {
			AddReactionAnimatedIcon(
				button,
				std::move(iconPositionValue),
				iconSize,
				r,
				button->events(
				) | rpl::filter([=](not_null<QEvent*> event) {
					return event->type() == QEvent::Enter;
				}) | rpl::to_empty,
				rpl::never<>(),
				&button->lifetime());
		}
		button->setClickedCallback([=, id = r.id] {
			if (premium && !controller->session().premium()) {
				ShowPremiumPreviewBox(
					controller,
					PremiumPreview::InfiniteReactions);
				return;
			}
			checkButton(button);
			state->selectedId = id;
		});
		if (r.id == state->selectedId.current()) {
			firstCheckedButton = button;
		}
	}
	if (firstCheckedButton) {
		firstCheckedButton->geometryValue(
		) | rpl::filter([=](const QRect &r) {
			return r.isValid();
		}) | rpl::take(1) | rpl::start_with_next([=] {
			checkButton(firstCheckedButton);
		}, firstCheckedButton->lifetime());
	}
	check->raise();

	box->setTitle(tr::lng_settings_chat_reactions_title());
	box->setWidth(st::boxWideWidth);
	box->addButton(tr::lng_settings_save(), [=] {
		const auto &data = controller->session().data();
		const auto selectedId = state->selectedId.current();
		if (data.reactions().favoriteId() != selectedId) {
			data.reactions().setFavorite(selectedId);
		}
		box->closeBox();
	});
	box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
}