Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/image_view.h" | 5 #include "ui/views/controls/image_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" | 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "ui/base/accessibility/accessible_view_state.h" | 10 #include "ui/base/accessibility/accessible_view_state.h" |
| 11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/insets.h" | 12 #include "ui/gfx/insets.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 | 15 |
| 16 ImageView::ImageView() | 16 ImageView::ImageView() |
| 17 : image_size_set_(false), | 17 : image_size_set_(false), |
| 18 horiz_alignment_(CENTER), | 18 horiz_alignment_(CENTER), |
| 19 vert_alignment_(CENTER), | 19 vert_alignment_(CENTER), |
| 20 interactive_(true) { | 20 interactive_(true), |
| 21 last_paint_scale_(0.f), | |
| 22 last_painted_image_(NULL) { | |
| 21 } | 23 } |
| 22 | 24 |
| 23 ImageView::~ImageView() { | 25 ImageView::~ImageView() { |
| 24 } | 26 } |
| 25 | 27 |
| 26 void ImageView::SetImage(const gfx::ImageSkia& img) { | 28 void ImageView::SetImage(const gfx::ImageSkia& img) { |
| 27 if (image_.BackedBySameObjectAs(img)) | 29 if (IsImageEqual(img)) |
|
pkotwicz
2013/11/15 20:57:09
Can you please add a comment (in the code) as to w
sky
2013/11/15 22:19:13
I placed the comment in IsImageEqual.
| |
| 28 return; | 30 return; |
| 29 | 31 |
| 32 last_painted_image_ = NULL; | |
| 30 gfx::Size pref_size(GetPreferredSize()); | 33 gfx::Size pref_size(GetPreferredSize()); |
| 31 image_ = img; | 34 image_ = img; |
| 32 if (pref_size != GetPreferredSize()) | 35 if (pref_size != GetPreferredSize()) |
| 33 PreferredSizeChanged(); | 36 PreferredSizeChanged(); |
| 34 SchedulePaint(); | 37 SchedulePaint(); |
| 35 } | 38 } |
| 36 | 39 |
| 37 void ImageView::SetImage(const gfx::ImageSkia* image_skia) { | 40 void ImageView::SetImage(const gfx::ImageSkia* image_skia) { |
| 38 if (image_skia) { | 41 if (image_skia) { |
| 39 SetImage(*image_skia); | 42 SetImage(*image_skia); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 if (image_size_set_) { | 78 if (image_size_set_) { |
| 76 gfx::Size image_size; | 79 gfx::Size image_size; |
| 77 GetImageSize(&image_size); | 80 GetImageSize(&image_size); |
| 78 image_size.Enlarge(insets.width(), insets.height()); | 81 image_size.Enlarge(insets.width(), insets.height()); |
| 79 return image_size; | 82 return image_size; |
| 80 } | 83 } |
| 81 return gfx::Size(image_.width() + insets.width(), | 84 return gfx::Size(image_.width() + insets.width(), |
| 82 image_.height() + insets.height()); | 85 image_.height() + insets.height()); |
| 83 } | 86 } |
| 84 | 87 |
| 88 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { | |
| 89 return image_.BackedBySameObjectAs(img) && | |
| 90 last_paint_scale_ != 0.0f && | |
| 91 last_painted_image_ == | |
| 92 img.GetRepresentation(last_paint_scale_).sk_bitmap().getPixels(); | |
| 93 } | |
| 94 | |
| 85 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { | 95 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { |
| 86 gfx::Insets insets = GetInsets(); | 96 gfx::Insets insets = GetInsets(); |
| 87 | 97 |
| 88 int x; | 98 int x; |
| 89 // In order to properly handle alignment of images in RTL locales, we need | 99 // In order to properly handle alignment of images in RTL locales, we need |
| 90 // to flip the meaning of trailing and leading. For example, if the | 100 // to flip the meaning of trailing and leading. For example, if the |
| 91 // horizontal alignment is set to trailing, then we'll use left alignment for | 101 // horizontal alignment is set to trailing, then we'll use left alignment for |
| 92 // the image instead of right alignment if the UI layout is RTL. | 102 // the image instead of right alignment if the UI layout is RTL. |
| 93 Alignment actual_horiz_alignment = horiz_alignment_; | 103 Alignment actual_horiz_alignment = horiz_alignment_; |
| 94 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER)) | 104 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER)) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 107 case CENTER: y = (height() - image_size.height()) / 2; break; | 117 case CENTER: y = (height() - image_size.height()) / 2; break; |
| 108 default: NOTREACHED(); y = 0; break; | 118 default: NOTREACHED(); y = 0; break; |
| 109 } | 119 } |
| 110 | 120 |
| 111 return gfx::Point(x, y); | 121 return gfx::Point(x, y); |
| 112 } | 122 } |
| 113 | 123 |
| 114 void ImageView::OnPaint(gfx::Canvas* canvas) { | 124 void ImageView::OnPaint(gfx::Canvas* canvas) { |
| 115 View::OnPaint(canvas); | 125 View::OnPaint(canvas); |
| 116 | 126 |
| 127 last_paint_scale_ = canvas->image_scale(); | |
| 128 last_painted_image_ = NULL; | |
| 129 | |
| 117 if (image_.isNull()) | 130 if (image_.isNull()) |
| 118 return; | 131 return; |
| 119 | 132 |
| 120 gfx::Rect image_bounds(GetImageBounds()); | 133 gfx::Rect image_bounds(GetImageBounds()); |
| 121 if (image_bounds.IsEmpty()) | 134 if (image_bounds.IsEmpty()) |
| 122 return; | 135 return; |
| 123 | 136 |
| 124 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { | 137 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { |
| 125 // Resize case | 138 // Resize case |
| 126 SkPaint paint; | 139 SkPaint paint; |
| 127 paint.setFilterBitmap(true); | 140 paint.setFilterBitmap(true); |
| 128 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), | 141 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), |
| 129 image_bounds.x(), image_bounds.y(), image_bounds.width(), | 142 image_bounds.x(), image_bounds.y(), image_bounds.width(), |
| 130 image_bounds.height(), true, paint); | 143 image_bounds.height(), true, paint); |
| 131 } else { | 144 } else { |
| 132 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); | 145 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); |
| 133 } | 146 } |
| 147 last_painted_image_ = | |
| 148 image_.GetRepresentation(last_paint_scale_).sk_bitmap().getPixels(); | |
| 134 } | 149 } |
| 135 | 150 |
| 136 void ImageView::GetAccessibleState(ui::AccessibleViewState* state) { | 151 void ImageView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 137 state->role = ui::AccessibilityTypes::ROLE_GRAPHIC; | 152 state->role = ui::AccessibilityTypes::ROLE_GRAPHIC; |
| 138 state->name = tooltip_text_; | 153 state->name = tooltip_text_; |
| 139 } | 154 } |
| 140 | 155 |
| 141 void ImageView::SetHorizontalAlignment(Alignment ha) { | 156 void ImageView::SetHorizontalAlignment(Alignment ha) { |
| 142 if (ha != horiz_alignment_) { | 157 if (ha != horiz_alignment_) { |
| 143 horiz_alignment_ = ha; | 158 horiz_alignment_ = ha; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 174 | 189 |
| 175 *tooltip = GetTooltipText(); | 190 *tooltip = GetTooltipText(); |
| 176 return true; | 191 return true; |
| 177 } | 192 } |
| 178 | 193 |
| 179 bool ImageView::HitTestRect(const gfx::Rect& rect) const { | 194 bool ImageView::HitTestRect(const gfx::Rect& rect) const { |
| 180 return interactive_ ? View::HitTestRect(rect) : false; | 195 return interactive_ ? View::HitTestRect(rect) : false; |
| 181 } | 196 } |
| 182 | 197 |
| 183 } // namespace views | 198 } // namespace views |
| OLD | NEW |