Commit d66a177c authored by Andrew Xu's avatar Andrew Xu Committed by Commit Bot

[Multipaste] Calculate scaling ratio depending on the image type

This CL implements a new feature of the multipaste menu:
(1) If the image to show is a bitmap image, scale the image up to fill
the contents area. For example, the size of the contents area is
128x66 while the image's size is 64x22. After scaling up, the image's
size becomes 192x66.

(2) If the image to show is rendered from HTML, scale the image up
until its size matches one edge of the contents area. Use the same
example. After scaling up, the image's size becomes 128x44.

Bug: 1139498
Change-Id: I982a17125a1b67669e7d528403010605f4a34d23
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2506669Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarDavid Black <dmblack@google.com>
Commit-Queue: Andrew Xu <andrewxu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822828}
parent f7195a6f
......@@ -280,15 +280,37 @@ class ClipboardHistoryBitmapItemView::BitmapContentsView
const float height_ratio =
image_size.height() / float(contents_bounds.height());
if (width_ratio <= 1.f || height_ratio <= 1.f) {
image_view_->SetImage(image_view_->original_image(), image_size);
return;
// Calculate `scaling_up_ratio` depending on the image type. A bitmap image
// should fill the contents bounds while an image rendered from HTML
// should meet at least one edge of the contents bounds.
float scaling_up_ratio = 0.f;
switch (*data_format_) {
case ui::ClipboardInternalFormat::kBitmap: {
if (width_ratio >= 1.f && height_ratio >= 1.f)
scaling_up_ratio = 1.f;
else
scaling_up_ratio = std::fmin(width_ratio, height_ratio);
break;
}
case ui::ClipboardInternalFormat::kHtml: {
if (width_ratio >= 1.f || height_ratio >= 1.f)
scaling_up_ratio = 1.f;
else
scaling_up_ratio =
std::fmin(std::fmax(width_ratio, height_ratio), 1.f);
break;
}
default:
NOTREACHED();
break;
}
const float resize_ratio = std::fmin(width_ratio, height_ratio);
DCHECK_LE(scaling_up_ratio, 1.f);
DCHECK_GT(scaling_up_ratio, 0.f);
image_view_->SetImage(image_view_->original_image(),
gfx::Size(image_size.width() / resize_ratio,
image_size.height() / resize_ratio));
gfx::Size(image_size.width() / scaling_up_ratio,
image_size.height() / scaling_up_ratio));
}
ClipboardHistoryBitmapItemView* const container_;
......
......@@ -21,14 +21,6 @@
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
namespace {
// Size of the ClipboardHistoryBitmapItemView shown in ClipboardHistory.
constexpr gfx::SizeF kClipboardHistoryBitmapItemViewSize =
gfx::SizeF(222.0, 66.0);
} // namespace
ClipboardImageModelRequest::Params::Params(const base::UnguessableToken& id,
const std::string& html_markup,
ImageModelCallback callback)
......@@ -156,25 +148,15 @@ void ClipboardImageModelRequest::PostCopySurfaceTask() {
void ClipboardImageModelRequest::CopySurface() {
content::RenderWidgetHostView* source_view =
web_contents()->GetRenderViewHost()->GetWidget()->GetView();
gfx::Size source_size = source_view->GetViewBounds().size();
if (source_size.IsEmpty()) {
if (source_view->GetViewBounds().size().IsEmpty()) {
Stop();
return;
}
// Scale down |source_size| until every edge fits into the 222x66 image that
// will be shown in ClipboardHistoryBitmapItemView.
const float height_ratio =
kClipboardHistoryBitmapItemViewSize.height() / source_size.height();
const float width_ratio =
kClipboardHistoryBitmapItemViewSize.width() / source_size.width();
const float scale = height_ratio < width_ratio ? height_ratio : width_ratio;
gfx::Size scaled_source_size =
gfx::ScaleToRoundedSize(source_size, scale, scale);
// There is no guarantee CopyFromSurface will call OnCopyComplete. If this
// takes too long, this will be cleaned up by |timeout_timer_|.
source_view->CopyFromSurface(
gfx::Rect(), scaled_source_size,
/*src_rect=*/gfx::Rect(), /*output_size=*/gfx::Size(),
base::BindOnce(&ClipboardImageModelRequest::OnCopyComplete,
weak_ptr_factory_.GetWeakPtr()));
}
......
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