From 98ce91df3945db86e8662c14b721b3c1baf01d2b Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Wed, 20 Mar 2024 11:59:05 +0400
Subject: [PATCH] Fix intro hello sticker playback in editing.

---
 .../history/view/history_view_about_view.cpp  |  9 ++---
 .../history/view/history_view_element.cpp     | 14 ++++---
 .../view/media/history_view_custom_emoji.cpp  |  4 +-
 .../view/media/history_view_giveaway.cpp      | 13 ++++---
 .../view/media/history_view_premium_gift.cpp  |  7 ++--
 .../view/media/history_view_sticker.cpp       | 37 +++++++++----------
 .../history/view/media/history_view_sticker.h | 14 ++++---
 Telegram/SourceFiles/ui/chat/chat.style       |  2 +-
 8 files changed, 52 insertions(+), 48 deletions(-)

diff --git a/Telegram/SourceFiles/history/view/history_view_about_view.cpp b/Telegram/SourceFiles/history/view/history_view_about_view.cpp
index 0252ccc68..d26a8818a 100644
--- a/Telegram/SourceFiles/history/view/history_view_about_view.cpp
+++ b/Telegram/SourceFiles/history/view/history_view_about_view.cpp
@@ -180,13 +180,10 @@ ChatIntroBox::ChatIntroBox(not_null<Element*> parent, Data::ChatIntro data)
 , _data(data) {
 	if (const auto document = data.sticker) {
 		if (const auto sticker = document->sticker()) {
-			const auto skipPremiumEffect = false;
+			const auto skipPremiumEffect = true;
 			_sticker.emplace(_parent, document, skipPremiumEffect, _parent);
-			_sticker->setDiceIndex(sticker->alt, 0);
-			_sticker->setGiftBoxSticker(true);
-			_sticker->initSize();
-			_sticker->setCustomEmojiPart(
-				st::chatIntroStickerSize,
+			_sticker->initSize(st::chatIntroStickerSize);
+			_sticker->setCustomCachingTag(
 				ChatHelpers::StickerLottieSize::ChatIntroHelloSticker);
 		}
 	}
diff --git a/Telegram/SourceFiles/history/view/history_view_element.cpp b/Telegram/SourceFiles/history/view/history_view_element.cpp
index 1f99b66c7..ce25da4dd 100644
--- a/Telegram/SourceFiles/history/view/history_view_element.cpp
+++ b/Telegram/SourceFiles/history/view/history_view_element.cpp
@@ -753,14 +753,16 @@ void Element::refreshMedia(Element *replacing) {
 		const auto emojiStickers = &history()->session().emojiStickersPack();
 		const auto skipPremiumEffect = false;
 		if (const auto sticker = emojiStickers->stickerForEmoji(emoji)) {
+			auto content = std::make_unique<Sticker>(
+				this,
+				sticker.document,
+				skipPremiumEffect,
+				replacing,
+				sticker.replacements);
+			content->setEmojiSticker();
 			_media = std::make_unique<UnwrappedMedia>(
 				this,
-				std::make_unique<Sticker>(
-					this,
-					sticker.document,
-					skipPremiumEffect,
-					replacing,
-					sticker.replacements));
+				std::move(content));
 		} else {
 			_media = std::make_unique<UnwrappedMedia>(
 				this,
diff --git a/Telegram/SourceFiles/history/view/media/history_view_custom_emoji.cpp b/Telegram/SourceFiles/history/view/media/history_view_custom_emoji.cpp
index acd836fc1..51c69c89b 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_custom_emoji.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_custom_emoji.cpp
@@ -128,7 +128,9 @@ std::unique_ptr<Sticker> CustomEmoji::createStickerPart(
 		_parent,
 		document,
 		skipPremiumEffect);
-	result->setCustomEmojiPart(_singleSize, _cachingTag);
+	result->initSize(_singleSize);
+	result->setCustomCachingTag(_cachingTag);
+	result->setCustomEmojiPart();
 	return result;
 }
 
diff --git a/Telegram/SourceFiles/history/view/media/history_view_giveaway.cpp b/Telegram/SourceFiles/history/view/media/history_view_giveaway.cpp
index 859ab201e..663d3a04e 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_giveaway.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_giveaway.cpp
@@ -310,10 +310,10 @@ void StickerWithBadgePart::draw(
 		int outerWidth) const {
 	const auto stickerSize = st::msgServiceGiftBoxStickerSize;
 	const auto sticker = QRect(
-		(outerWidth - stickerSize.width()) / 2,
+		(outerWidth - stickerSize) / 2,
 		st::chatGiveawayStickerTop + _skipTop,
-		stickerSize.width(),
-		stickerSize.height());
+		stickerSize,
+		stickerSize);
 
 	if (_sticker) {
 		_sticker->draw(p, context, sticker);
@@ -335,7 +335,7 @@ void StickerWithBadgePart::unloadHeavyPart() {
 
 QSize StickerWithBadgePart::countOptimalSize() {
 	const auto size = st::msgServiceGiftBoxStickerSize;
-	return { size.width(), st::chatGiveawayStickerTop + size.height() };
+	return { size, st::chatGiveawayStickerTop + size };
 }
 
 QSize StickerWithBadgePart::countCurrentSize(int newWidth) {
@@ -352,8 +352,9 @@ void StickerWithBadgePart::ensureCreated() const {
 			_skipTop = data.skipTop;
 			_sticker.emplace(_parent, document, skipPremiumEffect, _parent);
 			_sticker->setDiceIndex(sticker->alt, 1);
-			_sticker->setGiftBoxSticker(data.isGiftBoxSticker);
-			_sticker->initSize();
+			_sticker->initSize(data.isGiftBoxSticker
+				? st::msgServiceGiftBoxStickerSize
+				: 0);
 		}
 	}
 }
diff --git a/Telegram/SourceFiles/history/view/media/history_view_premium_gift.cpp b/Telegram/SourceFiles/history/view/media/history_view_premium_gift.cpp
index 7701f6db8..b832a6dc3 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_premium_gift.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_premium_gift.cpp
@@ -39,7 +39,9 @@ int PremiumGift::top() {
 }
 
 QSize PremiumGift::size() {
-	return st::msgServiceGiftBoxStickerSize;
+	return QSize(
+		st::msgServiceGiftBoxStickerSize,
+		st::msgServiceGiftBoxStickerSize);
 }
 
 QString PremiumGift::title() {
@@ -172,8 +174,7 @@ void PremiumGift::ensureStickerCreated() const {
 			const auto skipPremiumEffect = false;
 			_sticker.emplace(_parent, document, skipPremiumEffect, _parent);
 			_sticker->setDiceIndex(sticker->alt, 1);
-			_sticker->setGiftBoxSticker(true);
-			_sticker->initSize();
+			_sticker->initSize(st::msgServiceGiftBoxStickerSize);
 		}
 	}
 }
diff --git a/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp b/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp
index cefaad0c2..338c163fa 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_sticker.cpp
@@ -130,20 +130,18 @@ bool Sticker::hasPremiumEffect() const {
 }
 
 bool Sticker::customEmojiPart() const {
-	return (_cachingTag != ChatHelpers::StickerLottieSize::MessageHistory);
+	return _customEmojiPart;
 }
 
-bool Sticker::isEmojiSticker() const {
-	return (_parent->data()->media() == nullptr);
+bool Sticker::emojiSticker() const {
+	return _emojiSticker;
 }
 
-void Sticker::initSize() {
-	if (isEmojiSticker() || _diceIndex >= 0) {
-		if (_giftBoxSticker) {
-			_size = st::msgServiceGiftBoxStickerSize;
-		} else {
-			_size = EmojiSize();
-		}
+void Sticker::initSize(int customSize) {
+	if (customSize > 0) {
+		_size = { customSize, customSize };
+	} else if (emojiSticker() || _diceIndex >= 0) {
+		_size = EmojiSize();
 		if (_diceIndex > 0) {
 			[[maybe_unused]] bool result = readyToDrawAnimationFrame();
 		}
@@ -252,7 +250,7 @@ void Sticker::paintAnimationFrame(
 		: (context.selected() && !_nextLastDiceFrame)
 		? context.st->msgStickerOverlay()->c
 		: QColor(0, 0, 0, 0);
-	const auto powerSavingFlag = (isEmojiSticker() || _diceIndex >= 0)
+	const auto powerSavingFlag = (emojiSticker() || _diceIndex >= 0)
 		? PowerSaving::kEmojiChat
 		: PowerSaving::kStickersChat;
 	const auto paused = context.paused || On(powerSavingFlag);
@@ -298,7 +296,7 @@ void Sticker::paintAnimationFrame(
 		? true
 		: (_diceIndex == 0)
 		? false
-		: ((!customEmojiPart() && isEmojiSticker())
+		: ((!customEmojiPart() && emojiSticker())
 			|| !Core::App().settings().loopAnimatedStickers());
 	const auto lastDiceFrame = (_diceIndex > 0) && atTheEnd();
 	const auto switchToNext = !playOnce
@@ -421,7 +419,7 @@ void Sticker::refreshLink() {
 		return;
 	}
 	const auto sticker = _data->sticker();
-	if (isEmojiSticker()) {
+	if (emojiSticker()) {
 		const auto weak = base::make_weak(this);
 		_link = std::make_shared<LambdaClickHandler>([weak] {
 			if (const auto that = weak.get()) {
@@ -499,15 +497,16 @@ void Sticker::setDiceIndex(const QString &emoji, int index) {
 	_diceIndex = index;
 }
 
-void Sticker::setCustomEmojiPart(
-		int size,
-		ChatHelpers::StickerLottieSize tag) {
-	_size = { size, size };
+void Sticker::setCustomCachingTag(ChatHelpers::StickerLottieSize tag) {
 	_cachingTag = tag;
 }
 
-void Sticker::setGiftBoxSticker(bool giftBoxSticker) {
-	_giftBoxSticker = giftBoxSticker;
+void Sticker::setCustomEmojiPart() {
+	_customEmojiPart = true;
+}
+
+void Sticker::setEmojiSticker() {
+	_emojiSticker = true;
 }
 
 void Sticker::setupPlayer() {
diff --git a/Telegram/SourceFiles/history/view/media/history_view_sticker.h b/Telegram/SourceFiles/history/view/media/history_view_sticker.h
index 86c637390..1cbd7ea82 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_sticker.h
+++ b/Telegram/SourceFiles/history/view/media/history_view_sticker.h
@@ -42,7 +42,7 @@ public:
 		const Lottie::ColorReplacements *replacements = nullptr);
 	~Sticker();
 
-	void initSize();
+	void initSize(int customSize = 0);
 	QSize countOptimalSize() override;
 	void draw(
 		Painter &p,
@@ -62,12 +62,13 @@ public:
 
 	void refreshLink() override;
 	bool hasTextForCopy() const override {
-		return isEmojiSticker();
+		return emojiSticker();
 	}
 
 	void setDiceIndex(const QString &emoji, int index);
-	void setCustomEmojiPart(int size, ChatHelpers::StickerLottieSize tag);
-	void setGiftBoxSticker(bool giftBoxSticker);
+	void setCustomCachingTag(ChatHelpers::StickerLottieSize tag);
+	void setCustomEmojiPart();
+	void setEmojiSticker();
 	[[nodiscard]] bool atTheEnd() const {
 		return 	(_frameIndex >= 0) && (_frameIndex + 1 == _framesCount);
 	}
@@ -96,7 +97,7 @@ public:
 private:
 	[[nodiscard]] bool hasPremiumEffect() const;
 	[[nodiscard]] bool customEmojiPart() const;
-	[[nodiscard]] bool isEmojiSticker() const;
+	[[nodiscard]] bool emojiSticker() const;
 	void paintAnimationFrame(
 		Painter &p,
 		const PaintContext &context,
@@ -134,7 +135,8 @@ private:
 	mutable bool _premiumEffectSkipped : 1 = false;
 	mutable bool _nextLastDiceFrame : 1 = false;
 	bool _skipPremiumEffect : 1 = false;
-	bool _giftBoxSticker : 1 = false;
+	bool _customEmojiPart : 1 = false;
+	bool _emojiSticker : 1 = false;
 
 };
 
diff --git a/Telegram/SourceFiles/ui/chat/chat.style b/Telegram/SourceFiles/ui/chat/chat.style
index a18af8cd4..92b68860f 100644
--- a/Telegram/SourceFiles/ui/chat/chat.style
+++ b/Telegram/SourceFiles/ui/chat/chat.style
@@ -923,7 +923,7 @@ msgServiceGiftBoxButtonPadding: margins(2px, 0px, 2px, 0px);
 msgServiceGiftBoxButtonMargins: margins(0px, 13px, 0px, 17px);
 msgServiceGiftBoxTitlePadding: margins(0px, 5px, 0px, 2px);
 msgServiceGiftBoxStickerTop: -19px;
-msgServiceGiftBoxStickerSize: size(140px, 140px);
+msgServiceGiftBoxStickerSize: 140px;
 
 historySponsorInfoItem: FlatLabel(defaultFlatLabel) {
 	style: TextStyle(defaultTextStyle) {