From eafc01e02bb70b8da7002054c01a87fc0e28be81 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 3 Jun 2024 14:14:42 +0400
Subject: [PATCH] Fix small photo display in wide enough chat.

Fixes #28004.
---
 .../history/view/media/history_view_extended_preview.cpp  | 3 +++
 .../SourceFiles/history/view/media/history_view_photo.cpp | 8 +++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/Telegram/SourceFiles/history/view/media/history_view_extended_preview.cpp b/Telegram/SourceFiles/history/view/media/history_view_extended_preview.cpp
index 88c405d2e..582e95265 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_extended_preview.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_extended_preview.cpp
@@ -163,6 +163,9 @@ QSize ExtendedPreview::countCurrentSize(int newWidth) {
 			_parent->textualMaxWidth());
 		newWidth = qMin(qMax(newWidth, maxWithCaption), thumbMaxWidth);
 	}
+	if (newWidth >= maxWidth()) {
+		newHeight = qMin(newHeight, minHeight());
+	}
 	return { newWidth, newHeight };
 }
 
diff --git a/Telegram/SourceFiles/history/view/media/history_view_photo.cpp b/Telegram/SourceFiles/history/view/media/history_view_photo.cpp
index ed0ded0fe..9c5ccd302 100644
--- a/Telegram/SourceFiles/history/view/media/history_view_photo.cpp
+++ b/Telegram/SourceFiles/history/view/media/history_view_photo.cpp
@@ -218,7 +218,6 @@ QSize Photo::countCurrentSize(int newWidth) {
 		maxWidth());
 	newWidth = qMax(pix.width(), minWidth);
 	auto newHeight = qMax(pix.height(), st::minPhotoSize);
-	auto imageHeight = newHeight;
 	if (_parent->hasBubble()) {
 		auto captionMaxWidth = _parent->textualMaxWidth();
 		const auto botTop = _parent->Get<FakeBotAboutTop>();
@@ -227,10 +226,13 @@ QSize Photo::countCurrentSize(int newWidth) {
 		}
 		const auto maxWithCaption = qMin(st::msgMaxWidth, captionMaxWidth);
 		newWidth = qMin(qMax(newWidth, maxWithCaption), thumbMaxWidth);
-		imageHeight = newHeight = adjustHeightForLessCrop(
+		newHeight = adjustHeightForLessCrop(
 			dimensions,
 			{ newWidth, newHeight });
 	}
+	if (newWidth >= maxWidth()) {
+		newHeight = qMin(newHeight, minHeight());
+	}
 	const auto enlargeInner = st::historyPageEnlargeSize;
 	const auto enlargeOuter = 2 * st::historyPageEnlargeSkip + enlargeInner;
 	const auto showEnlarge = (_parent->media() != this)
@@ -238,7 +240,7 @@ QSize Photo::countCurrentSize(int newWidth) {
 		&& _parent->data()->media()->webpage()
 		&& _parent->data()->media()->webpage()->suggestEnlargePhoto()
 		&& (newWidth >= enlargeOuter)
-		&& (imageHeight >= enlargeOuter);
+		&& (newHeight >= enlargeOuter);
 	_showEnlarge = showEnlarge ? 1 : 0;
 	return { newWidth, newHeight };
 }