diff --git a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp index f544fbb08..31038c96d 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp @@ -166,6 +166,9 @@ QSize Gif::countOptimalSize() { maxWidth = qMax(maxWidth, st::msgPadding.left() + _caption.maxWidth() + st::msgPadding.right()); + minHeight = adjustHeightForLessCrop( + scaled, + { maxWidth, minHeight }); minHeight += st::mediaCaptionSkip + _caption.minHeight(); if (isBubbleBottom()) { minHeight += st::msgPadding.bottom(); @@ -209,6 +212,9 @@ QSize Gif::countCurrentSize(int newWidth) { + _caption.maxWidth() + st::msgPadding.right())); newWidth = qMin(qMax(newWidth, maxWithCaption), thumbMaxWidth); + newHeight = adjustHeightForLessCrop( + scaled, + { newWidth, newHeight }); const auto captionw = newWidth - st::msgPadding.left() - st::msgPadding.right(); @@ -242,6 +248,19 @@ QSize Gif::countCurrentSize(int newWidth) { return { newWidth, newHeight }; } +int Gif::adjustHeightForLessCrop(QSize dimensions, QSize current) const { + if (dimensions.isEmpty()) { + return current.height(); + } + // Allow some more vertical space for less cropping, + // but not more than 1.33 * existing height. + return qMax( + current.height(), + qMin( + current.width() * dimensions.height() / dimensions.width(), + current.height() * 4 / 3)); +} + QSize Gif::videoSize() const { if (const auto streamed = activeCurrentStreamed()) { return streamed->player().videoSize(); diff --git a/Telegram/SourceFiles/history/view/media/history_view_gif.h b/Telegram/SourceFiles/history/view/media/history_view_gif.h index 19513e93a..bdd33e9e6 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_gif.h +++ b/Telegram/SourceFiles/history/view/media/history_view_gif.h @@ -120,6 +120,9 @@ private: void validateVideoThumbnail() const; [[nodiscard]] QSize countThumbSize(int &inOutWidthMax) const; + [[nodiscard]] int adjustHeightForLessCrop( + QSize dimensions, + QSize current) const; float64 dataProgress() const override; bool dataFinished() const override; diff --git a/Telegram/SourceFiles/history/view/media/history_view_photo.cpp b/Telegram/SourceFiles/history/view/media/history_view_photo.cpp index 7a682a5d3..3baa5aa76 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_photo.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_photo.cpp @@ -169,14 +169,9 @@ QSize Photo::countOptimalSize() { (st::msgPadding.left() + _caption.maxWidth() + st::msgPadding.right())); - if (!dimensions.isEmpty() - && ::Media::Streaming::FrameResizeMayExpand( - { maxWidth, minHeight }, - dimensions)) { - minHeight = qMax( - minHeight, - maxWidth * dimensions.height() / dimensions.width()); - } + minHeight = adjustHeightForLessCrop( + dimensions, + { maxWidth, minHeight }); minHeight += st::mediaCaptionSkip + _caption.minHeight(); if (isBubbleBottom()) { minHeight += st::msgPadding.bottom(); @@ -210,14 +205,9 @@ QSize Photo::countCurrentSize(int newWidth) { + _caption.maxWidth() + st::msgPadding.right())); newWidth = qMin(qMax(newWidth, maxWithCaption), thumbMaxWidth); - if (!dimensions.isEmpty() - && ::Media::Streaming::FrameResizeMayExpand( - { newWidth, newHeight }, - dimensions)) { - newHeight = qMax( - newHeight, - newWidth * dimensions.height() / dimensions.width()); - } + newHeight = adjustHeightForLessCrop( + dimensions, + { newWidth, newHeight }); const auto captionw = newWidth - st::msgPadding.left() - st::msgPadding.right(); @@ -229,6 +219,16 @@ QSize Photo::countCurrentSize(int newWidth) { return { newWidth, newHeight }; } +int Photo::adjustHeightForLessCrop(QSize dimensions, QSize current) const { + if (dimensions.isEmpty() + || !::Media::Streaming::FrameResizeMayExpand(current, dimensions)) { + return current.height(); + } + return qMax( + current.height(), + current.width() * dimensions.height() / dimensions.width()); +} + void Photo::draw(Painter &p, const PaintContext &context) const { if (width() < st::msgPadding.left() + st::msgPadding.right() + 1) return; diff --git a/Telegram/SourceFiles/history/view/media/history_view_photo.h b/Telegram/SourceFiles/history/view/media/history_view_photo.h index ec07458a3..4a350659a 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_photo.h +++ b/Telegram/SourceFiles/history/view/media/history_view_photo.h @@ -116,6 +116,9 @@ private: QSize countOptimalSize() override; QSize countCurrentSize(int newWidth) override; + [[nodiscard]] int adjustHeightForLessCrop( + QSize dimensions, + QSize current) const; bool needInfoDisplay() const; void validateGroupedCache(