From d9da2edd7ccb2c2138c1a95e8f52440ee2ba95a6 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Sat, 16 Dec 2017 17:09:53 +0400
Subject: [PATCH] Improve grouped media display.

---
 Telegram/SourceFiles/boxes/send_files_box.cpp      | 11 +++++++++++
 Telegram/SourceFiles/data/data_shared_media.cpp    |  4 +---
 .../SourceFiles/history/history_media_grouped.cpp  | 14 ++++++--------
 .../SourceFiles/history/history_media_types.cpp    | 14 +++++++-------
 Telegram/SourceFiles/history/history_message.cpp   |  1 +
 Telegram/SourceFiles/history/history_message.h     |  2 +-
 Telegram/SourceFiles/history/history_service.cpp   |  3 +++
 Telegram/SourceFiles/history/history_widget.cpp    |  4 ++--
 .../info/media/info_media_list_widget.cpp          |  6 ++----
 9 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp
index 98279510e..ad1903063 100644
--- a/Telegram/SourceFiles/boxes/send_files_box.cpp
+++ b/Telegram/SourceFiles/boxes/send_files_box.cpp
@@ -492,6 +492,17 @@ EditCaptionBox::EditCaptionBox(QWidget*, HistoryMedia *media, FullMsgId msgId) :
 		image = doc->thumb;
 	} break;
 
+	case MediaTypeGrouped: {
+		if (const auto photo = media->getPhoto()) {
+			dimensions = QSize(photo->full->width(), photo->full->height());
+			image = photo->full;
+		} else if (const auto doc = media->getDocument()) {
+			dimensions = doc->dimensions;
+			image = doc->thumb;
+			_animated = true;
+		}
+	} break;
+
 	case MediaTypeFile:
 	case MediaTypeMusicFile:
 	case MediaTypeVoiceFile: {
diff --git a/Telegram/SourceFiles/data/data_shared_media.cpp b/Telegram/SourceFiles/data/data_shared_media.cpp
index 3b776b85e..f42dd97ad 100644
--- a/Telegram/SourceFiles/data/data_shared_media.cpp
+++ b/Telegram/SourceFiles/data/data_shared_media.cpp
@@ -290,9 +290,7 @@ base::optional<bool> SharedMediaWithLastSlice::IsLastIsolated(
 		| [](FullMsgId msgId) {	return App::histItemById(msgId); }
 		| [](HistoryItem *item) { return item ? item->getMedia() : nullptr; }
 		| [](HistoryMedia *media) {
-			return (media && media->type() == MediaTypePhoto)
-				? static_cast<HistoryPhoto*>(media)->getPhoto()
-				: nullptr;
+			return media ? media->getPhoto() : nullptr;
 		}
 		| [](PhotoData *photo) { return photo ? photo->id : 0; }
 		| [&](PhotoId photoId) { return *lastPeerPhotoId != photoId; };
diff --git a/Telegram/SourceFiles/history/history_media_grouped.cpp b/Telegram/SourceFiles/history/history_media_grouped.cpp
index c783664f9..0568faaf2 100644
--- a/Telegram/SourceFiles/history/history_media_grouped.cpp
+++ b/Telegram/SourceFiles/history/history_media_grouped.cpp
@@ -426,14 +426,12 @@ bool HistoryGroupedMedia::computeNeedBubble() const {
 	if (!_caption.isEmpty()) {
 		return true;
 	}
-	for (const auto &element : _elements) {
-		if (const auto message = element.item->toHistoryMessage()) {
-			if (message->viaBot()
-				|| message->Has<HistoryMessageForwarded>()
-				|| message->Has<HistoryMessageReply>()
-				|| message->displayFromName()) {
-				return true;
-			}
+	if (const auto message = _parent->toHistoryMessage()) {
+		if (message->viaBot()
+			|| message->Has<HistoryMessageReply>()
+			|| message->displayForwardedFrom()
+			|| message->displayFromName()) {
+			return true;
 		}
 	}
 	return false;
diff --git a/Telegram/SourceFiles/history/history_media_types.cpp b/Telegram/SourceFiles/history/history_media_types.cpp
index a3f19ff6f..c161b5f95 100644
--- a/Telegram/SourceFiles/history/history_media_types.cpp
+++ b/Telegram/SourceFiles/history/history_media_types.cpp
@@ -640,9 +640,9 @@ void HistoryPhoto::drawGrouped(
 		const auto roundRadius = ImageRoundRadius::Large;
 		const auto roundCorners = ImageRoundCorner::None
 			| ((corners & RectPart::TopLeft) ? ImageRoundCorner::TopLeft : ImageRoundCorner::None)
-			| ((corners & RectPart::TopRight) ? ImageRoundCorner::TopLeft : ImageRoundCorner::None)
-			| ((corners & RectPart::BottomLeft) ? ImageRoundCorner::TopLeft : ImageRoundCorner::None)
-			| ((corners & RectPart::BottomRight) ? ImageRoundCorner::TopLeft : ImageRoundCorner::None);
+			| ((corners & RectPart::TopRight) ? ImageRoundCorner::TopRight : ImageRoundCorner::None)
+			| ((corners & RectPart::BottomLeft) ? ImageRoundCorner::BottomLeft : ImageRoundCorner::None)
+			| ((corners & RectPart::BottomRight) ? ImageRoundCorner::BottomRight : ImageRoundCorner::None);
 		App::complexOverlayRect(p, geometry, roundRadius, roundCorners);
 	}
 
@@ -857,8 +857,8 @@ bool HistoryPhoto::needsBubble() const {
 	}
 	if (auto message = _parent->toHistoryMessage()) {
 		return message->viaBot()
-			|| message->Has<HistoryMessageForwarded>()
 			|| message->Has<HistoryMessageReply>()
+			|| message->displayForwardedFrom()
 			|| message->displayFromName();
 	}
 	return false;
@@ -1333,8 +1333,8 @@ bool HistoryVideo::needsBubble() const {
 	}
 	if (auto message = _parent->toHistoryMessage()) {
 		return message->viaBot()
-			|| message->Has<HistoryMessageForwarded>()
 			|| message->Has<HistoryMessageReply>()
+			|| message->displayForwardedFrom()
 			|| message->displayFromName();
 	}
 	return false;
@@ -2809,8 +2809,8 @@ bool HistoryGif::needsBubble() const {
 	}
 	if (auto message = _parent->toHistoryMessage()) {
 		return message->viaBot()
-			|| message->Has<HistoryMessageForwarded>()
 			|| message->Has<HistoryMessageReply>()
+			|| message->displayForwardedFrom()
 			|| message->displayFromName();
 	}
 	return false;
@@ -5318,8 +5318,8 @@ bool HistoryLocation::needsBubble() const {
 	}
 	if (auto message = _parent->toHistoryMessage()) {
 		return message->viaBot()
-			|| message->Has<HistoryMessageForwarded>()
 			|| message->Has<HistoryMessageReply>()
+			|| message->displayForwardedFrom()
 			|| message->displayFromName();
 	}
 	return false;
diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp
index d41739989..f01cb738a 100644
--- a/Telegram/SourceFiles/history/history_message.cpp
+++ b/Telegram/SourceFiles/history/history_message.cpp
@@ -125,6 +125,7 @@ bool HasMediaItems(const HistoryItemsList &items) {
 			switch (media->type()) {
 			case MediaTypePhoto:
 			case MediaTypeVideo:
+			case MediaTypeGrouped:
 			case MediaTypeFile:
 			case MediaTypeMusicFile:
 			case MediaTypeVoiceFile: return true;
diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h
index 143c6a41a..de4a0dbd5 100644
--- a/Telegram/SourceFiles/history/history_message.h
+++ b/Telegram/SourceFiles/history/history_message.h
@@ -163,6 +163,7 @@ public:
 		if (isAttachedToPrevious()) return false;
 		return true;
 	}
+	bool displayForwardedFrom() const;
 	bool displayEditedBadge(bool hasViaBotOrInlineMarkup) const;
 	bool uploading() const;
 	bool displayRightAction() const override;
@@ -321,7 +322,6 @@ private:
 	int performResizeGetHeight();
 	void applyEditionToEmpty();
 
-	bool displayForwardedFrom() const;
 	void paintFromName(Painter &p, QRect &trect, bool selected) const;
 	void paintForwardedInfo(Painter &p, QRect &trect, bool selected) const;
 	void paintReplyInfo(Painter &p, QRect &trect, bool selected) const;
diff --git a/Telegram/SourceFiles/history/history_service.cpp b/Telegram/SourceFiles/history/history_service.cpp
index bac90fc89..9ff401dd7 100644
--- a/Telegram/SourceFiles/history/history_service.cpp
+++ b/Telegram/SourceFiles/history/history_service.cpp
@@ -286,6 +286,9 @@ HistoryService::PreparedText HistoryService::preparePinnedText() {
 			switch (media ? media->type() : MediaTypeCount) {
 			case MediaTypePhoto: return lang(lng_action_pinned_media_photo);
 			case MediaTypeVideo: return lang(lng_action_pinned_media_video);
+			case MediaTypeGrouped: return lang(media->getPhoto()
+				? lng_action_pinned_media_photo
+				: lng_action_pinned_media_video);
 			case MediaTypeContact: return lang(lng_action_pinned_media_contact);
 			case MediaTypeFile: return lang(lng_action_pinned_media_file);
 			case MediaTypeGif: {
diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index 20c36e079..2ea3887f4 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -4513,8 +4513,8 @@ void HistoryWidget::onThumbDocumentUploaded(
 
 void HistoryWidget::onPhotoProgress(const FullMsgId &newId) {
 	if (const auto item = App::histItemById(newId)) {
-		const auto photo = (item->getMedia() && item->getMedia()->type() == MediaTypePhoto)
-			? static_cast<HistoryPhoto*>(item->getMedia())->getPhoto()
+		const auto photo = item->getMedia()
+			? item->getMedia()->getPhoto()
 			: nullptr;
 		updateSendAction(item->history(), SendAction::Type::UploadPhoto, 0);
 		Auth().data().requestItemRepaint(item);
diff --git a/Telegram/SourceFiles/info/media/info_media_list_widget.cpp b/Telegram/SourceFiles/info/media/info_media_list_widget.cpp
index 52695c9b7..65e614e3f 100644
--- a/Telegram/SourceFiles/info/media/info_media_list_widget.cpp
+++ b/Telegram/SourceFiles/info/media/info_media_list_widget.cpp
@@ -847,10 +847,8 @@ std::unique_ptr<BaseLayout> ListWidget::createLayout(
 		return nullptr;
 	}
 	auto getPhoto = [&]() -> PhotoData* {
-		if (auto media = item->getMedia()) {
-			if (media->type() == MediaTypePhoto) {
-				return static_cast<HistoryPhoto*>(media)->getPhoto();
-			}
+		if (const auto media = item->getMedia()) {
+			return media->getPhoto();
 		}
 		return nullptr;
 	};