Commit d8cbea31 authored by Dana Fried's avatar Dana Fried Committed by Commit Bot

Don't stretch previews with extreme aspect ratio

This fixes the attached bug. When the preview image served to the hover
card system by the thumbnail system is extremely narrow or wide (which
can happen with extreme web viewport shapes) the image will be centered
rather than stretched in the hover card.

This can really only be reproduced by opening developer tools and
widening it until it fills most of the browser window. Otherwise the
preview capture system handles scaling and cropping appropriately.

Bug: 1146389
Change-Id: I1e3943ffa02c1c72bc5105ebbeb757fac6c10323
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2528910
Commit-Queue: Dana Fried <dfried@chromium.org>
Reviewed-by: default avatarCaroline Rising <corising@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826046}
parent 4347b610
......@@ -133,6 +133,33 @@ std::unique_ptr<views::View> CreateAlertView(const TabAlertState& state) {
return alert_state_label;
}
// Calculates an appropriate size to display a preview image in the hover card.
// For the vast majority of images, the |preferred_size| is used, but extremely
// tall or wide images use the image size instead, centering in the available
// space.
gfx::Size GetPreviewImageSize(gfx::Size preview_size,
gfx::Size preferred_size) {
DCHECK(!preferred_size.IsEmpty());
if (preview_size.IsEmpty())
return preview_size;
const float preview_aspect_ratio =
float{preview_size.width()} / preview_size.height();
const float preferred_aspect_ratio =
float{preferred_size.width()} / preferred_size.height();
const float ratio = preview_aspect_ratio / preferred_aspect_ratio;
// Images between 2/3 and 3/2 of the target aspect ratio use the preferred
// size, stretching the image. Only images outside this range get centered.
// Since this is a corner case most users will never see, the specific cutoffs
// just need to be reasonable and don't need to be precise values (that is,
// there is no "correct" value; if the results are not aesthetic they can be
// tuned).
constexpr float kMinStretchRatio = 0.667f;
constexpr float kMaxStretchRatio = 1.5f;
if (ratio >= kMinStretchRatio && ratio <= kMaxStretchRatio)
return preferred_size;
return preview_size;
}
} // namespace
// static
......@@ -849,7 +876,8 @@ void TabHoverCardBubbleView::OnThumbnailImageAvailable(
gfx::ImageSkia preview_image) {
const gfx::Size preview_size = TabStyle::GetPreviewImageSize();
preview_image_->SetImage(preview_image);
preview_image_->SetImageSize(preview_size);
preview_image_->SetImageSize(
GetPreviewImageSize(preview_image.size(), preview_size));
preview_image_->SetPreferredSize(preview_size);
preview_image_->SetBackground(nullptr);
waiting_for_decompress_ = false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment