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.
4
5 #include "ui/message_center/views/proportional_image_view.h"
6
7 #include "ui/gfx/canvas.h"
8 #include "ui/message_center/message_center_style.h"
9
10 namespace message_center {
11
ProportionalImageView(const gfx::ImageSkia & image,const gfx::Size & max_size)12 ProportionalImageView::ProportionalImageView(const gfx::ImageSkia& image,
13 const gfx::Size& max_size)
14 : image_(image), max_size_(max_size) {}
15
~ProportionalImageView()16 ProportionalImageView::~ProportionalImageView() {}
17
GetPreferredSize() const18 gfx::Size ProportionalImageView::GetPreferredSize() const { return max_size_; }
19
GetHeightForWidth(int width) const20 int ProportionalImageView::GetHeightForWidth(int width) const {
21 return max_size_.height();
22 }
23
OnPaint(gfx::Canvas * canvas)24 void ProportionalImageView::OnPaint(gfx::Canvas* canvas) {
25 views::View::OnPaint(canvas);
26
27 gfx::Size draw_size = GetImageDrawingSize();
28
29 if (draw_size.IsEmpty())
30 return;
31
32 gfx::Rect draw_bounds = GetContentsBounds();
33 draw_bounds.ClampToCenteredSize(draw_size);
34
35 gfx::Size image_size(image_.size());
36
37 if (image_size == draw_size) {
38 canvas->DrawImageInt(image_, draw_bounds.x(), draw_bounds.y());
39 } else {
40 SkPaint paint;
41 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
42
43 // This call resizes the image while drawing into the canvas.
44 canvas->DrawImageInt(
45 image_,
46 0, 0, image_size.width(), image_size.height(),
47 draw_bounds.x(), draw_bounds.y(), draw_size.width(), draw_size.height(),
48 true,
49 paint);
50 }
51 }
52
GetImageDrawingSize()53 gfx::Size ProportionalImageView::GetImageDrawingSize() {
54 if (!visible())
55 return gfx::Size();
56 return message_center::GetImageSizeForContainerSize(
57 GetContentsBounds().size(), image_.size());
58 }
59
60 } // namespace message_center
61