1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/message_center/views/proportional_image_view.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/message_center/message_center_style.h"
10 namespace message_center
{
12 const char ProportionalImageView::kViewClassName
[] = "ProportionalImageView";
14 ProportionalImageView::ProportionalImageView(const gfx::Size
& view_size
)
15 : view_size_(view_size
) {}
17 ProportionalImageView::~ProportionalImageView() {}
19 void ProportionalImageView::SetImage(const gfx::ImageSkia
& image
,
20 const gfx::Size
& max_image_size
) {
22 max_image_size_
= max_image_size
;
26 gfx::Size
ProportionalImageView::GetPreferredSize() const { return view_size_
; }
28 int ProportionalImageView::GetHeightForWidth(int width
) const {
29 return view_size_
.height();
32 void ProportionalImageView::OnPaint(gfx::Canvas
* canvas
) {
33 views::View::OnPaint(canvas
);
35 gfx::Size draw_size
= GetImageDrawingSize();
37 if (draw_size
.IsEmpty())
40 gfx::Rect draw_bounds
= GetContentsBounds();
41 draw_bounds
.ClampToCenteredSize(draw_size
);
43 gfx::Size
image_size(image_
.size());
45 if (image_size
== draw_size
) {
46 canvas
->DrawImageInt(image_
, draw_bounds
.x(), draw_bounds
.y());
49 paint
.setFilterQuality(kLow_SkFilterQuality
);
51 // This call resizes the image while drawing into the canvas.
54 0, 0, image_size
.width(), image_size
.height(),
55 draw_bounds
.x(), draw_bounds
.y(), draw_size
.width(), draw_size
.height(),
61 const char* ProportionalImageView::GetClassName() const {
62 return kViewClassName
;
65 gfx::Size
ProportionalImageView::GetImageDrawingSize() {
69 gfx::Size max_size
= max_image_size_
;
70 max_size
.SetToMin(GetContentsBounds().size());
71 return message_center::GetImageSizeForContainerSize(max_size
, image_
.size());
74 } // namespace message_center