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 "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" | 5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/layout_constants.h" | 8 #include "chrome/browser/ui/layout_constants.h" |
9 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" | 9 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" |
10 #include "ui/base/material_design/material_design_controller.h" | 10 #include "ui/base/material_design/material_design_controller.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/color_utils.h" | 13 #include "ui/gfx/color_utils.h" |
14 #include "ui/gfx/image/image_util.h" | |
14 #include "ui/native_theme/native_theme.h" | 15 #include "ui/native_theme/native_theme.h" |
15 #include "ui/views/animation/ink_drop_hover.h" | 16 #include "ui/views/animation/ink_drop_hover.h" |
16 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
17 #include "ui/views/controls/image_view.h" | 18 #include "ui/views/controls/image_view.h" |
18 #include "ui/views/painter.h" | 19 #include "ui/views/painter.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 SkColor CalculateImageColor(gfx::ImageSkia* image) { | 23 SkColor CalculateImageColor(gfx::ImageSkia* image) { |
23 // We grab the color of the middle pixel of the image, which we treat as | 24 // We grab the color of the middle pixel of the image, which we treat as |
24 // the representative color of the entire image (reasonable, given the current | 25 // the representative color of the entire image (reasonable, given the current |
25 // appearance of these assets). | 26 // appearance of these assets). |
26 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); | 27 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); |
27 SkAutoLockPixels pixel_lock(bitmap); | 28 SkAutoLockPixels pixel_lock(bitmap); |
28 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); | 29 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); |
29 } | 30 } |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 IconLabelBubbleView::IconLabelBubbleView(int contained_image, | 34 IconLabelBubbleView::IconLabelBubbleView(int contained_image, |
34 const gfx::FontList& font_list, | 35 const gfx::FontList& font_list, |
35 SkColor parent_background_color, | 36 SkColor parent_background_color, |
36 bool elide_in_middle) | 37 bool elide_in_middle) |
37 : background_painter_(nullptr), | 38 : background_painter_(nullptr), |
38 image_(new views::ImageView()), | 39 image_(new views::ImageView()), |
39 label_(new views::Label(base::string16(), font_list)), | 40 label_(new views::Label(base::string16(), font_list)), |
41 builtin_leading_padding_(0), | |
42 builtin_trailing_padding_(0), | |
40 is_extension_icon_(false), | 43 is_extension_icon_(false), |
41 parent_background_color_(parent_background_color) { | 44 parent_background_color_(parent_background_color) { |
42 if (contained_image) { | 45 if (contained_image) { |
43 image_->SetImage( | 46 image_->SetImage( |
44 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 47 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
45 contained_image)); | 48 contained_image)); |
46 } | 49 } |
47 | 50 |
48 // Disable separate hit testing for |image_|. This prevents views treating | 51 // Disable separate hit testing for |image_|. This prevents views treating |
49 // |image_| as a separate mouse hover region from |this|. | 52 // |image_| as a separate mouse hover region from |this|. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 should_show_background_ = false; | 91 should_show_background_ = false; |
89 SetLabelBackgroundColor(SK_ColorTRANSPARENT); | 92 SetLabelBackgroundColor(SK_ColorTRANSPARENT); |
90 } | 93 } |
91 | 94 |
92 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 95 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
93 label_->SetText(label); | 96 label_->SetText(label); |
94 } | 97 } |
95 | 98 |
96 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 99 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
97 image_->SetImage(image_skia); | 100 image_->SetImage(image_skia); |
101 | |
102 if (ui::MaterialDesignController::IsModeMaterial()) { | |
103 gfx::GetVisibleMargins(image_skia, &builtin_leading_padding_, | |
104 &builtin_trailing_padding_); | |
105 if (base::i18n::IsRTL()) | |
106 std::swap(builtin_leading_padding_, builtin_trailing_padding_); | |
107 } | |
98 } | 108 } |
99 | 109 |
100 bool IconLabelBubbleView::ShouldShowBackground() const { | 110 bool IconLabelBubbleView::ShouldShowBackground() const { |
101 return should_show_background_; | 111 return should_show_background_; |
102 } | 112 } |
103 | 113 |
104 double IconLabelBubbleView::WidthMultiplier() const { | 114 double IconLabelBubbleView::WidthMultiplier() const { |
105 return 1.0; | 115 return 1.0; |
106 } | 116 } |
107 | 117 |
108 bool IconLabelBubbleView::IsShrinking() const { | 118 bool IconLabelBubbleView::IsShrinking() const { |
109 return false; | 119 return false; |
110 } | 120 } |
111 | 121 |
112 int IconLabelBubbleView::GetImageAndPaddingWidth() const { | |
113 const int image_width = image_->GetPreferredSize().width(); | |
114 return image_width ? (image_width + GetInternalSpacing()) : 0; | |
115 } | |
116 | |
117 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 122 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
118 // Height will be ignored by the LocationBarView. | 123 // Height will be ignored by the LocationBarView. |
119 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 124 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
120 } | 125 } |
121 | 126 |
122 void IconLabelBubbleView::Layout() { | 127 void IconLabelBubbleView::Layout() { |
123 // Compute the image bounds. In non-MD, the leading padding depends on | 128 // Compute the image bounds. In non-MD, the leading padding depends on |
124 // whether this is an extension icon, since extension icons and | 129 // whether this is an extension icon, since extension icons and |
125 // Chrome-provided icons are different sizes. In MD, these sizes are the | 130 // Chrome-provided icons are different sizes. In MD, these sizes are the |
126 // same, so it's not necessary to handle the two types differently. | 131 // same, so it's not necessary to handle the two types differently. |
(...skipping 24 matching lines...) Expand all Loading... | |
151 // be enough room to show the image at all. | 156 // be enough room to show the image at all. |
152 const int image_width = | 157 const int image_width = |
153 std::min(image_preferred_width, | 158 std::min(image_preferred_width, |
154 std::max(0, width() - image_x - bubble_trailing_padding)); | 159 std::max(0, width() - image_x - bubble_trailing_padding)); |
155 image_->SetBounds(image_x, 0, image_width, height()); | 160 image_->SetBounds(image_x, 0, image_width, height()); |
156 | 161 |
157 // Compute the label bounds. The label gets whatever size is left over after | 162 // Compute the label bounds. The label gets whatever size is left over after |
158 // accounting for the preferred image width and padding amounts. Note that if | 163 // accounting for the preferred image width and padding amounts. Note that if |
159 // the label has zero size it doesn't actually matter what we compute its X | 164 // the label has zero size it doesn't actually matter what we compute its X |
160 // value to be, since it won't be visible. | 165 // value to be, since it won't be visible. |
161 const int label_x = image_x + GetImageAndPaddingWidth(); | 166 const int label_x = |
167 image_x + image_width + (image_width ? GetInternalSpacing() : 0); | |
162 const int label_width = | 168 const int label_width = |
163 std::max(0, width() - label_x - bubble_trailing_padding); | 169 std::max(0, width() - label_x - bubble_trailing_padding); |
164 label_->SetBounds(label_x, 0, label_width, height()); | 170 label_->SetBounds(label_x, 0, label_width, height()); |
165 } | 171 } |
166 | 172 |
167 void IconLabelBubbleView::OnNativeThemeChanged( | 173 void IconLabelBubbleView::OnNativeThemeChanged( |
168 const ui::NativeTheme* native_theme) { | 174 const ui::NativeTheme* native_theme) { |
169 label_->SetEnabledColor(GetTextColor()); | 175 label_->SetEnabledColor(GetTextColor()); |
170 | 176 |
171 if (!ui::MaterialDesignController::IsModeMaterial()) | 177 if (!ui::MaterialDesignController::IsModeMaterial()) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 if (shrinking) | 234 if (shrinking) |
229 total_width = std::max(total_width, image_width); | 235 total_width = std::max(total_width, image_width); |
230 size.set_width(total_width); | 236 size.set_width(total_width); |
231 } | 237 } |
232 return size; | 238 return size; |
233 } | 239 } |
234 | 240 |
235 int IconLabelBubbleView::GetOuterPadding(bool leading) const { | 241 int IconLabelBubbleView::GetOuterPadding(bool leading) const { |
236 if (ui::MaterialDesignController::IsModeMaterial()) { | 242 if (ui::MaterialDesignController::IsModeMaterial()) { |
237 // Leading and trailing padding are equal. | 243 // Leading and trailing padding are equal. |
238 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 244 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) - |
245 (leading ? builtin_leading_padding_ : 0); | |
varkha
2016/03/30 04:04:07
Wouldn't that increase the visible trailing paddin
Peter Kasting
2016/03/30 04:10:55
In some cases, yes; this looks more consistent (th
varkha
2016/03/30 04:35:10
So in the popup animated bubble the visible left p
Peter Kasting
2016/03/30 04:44:57
No. The visible padding will be equal. The point
varkha
2016/03/30 05:21:58
OK, but in general case if builtin_*_padding_ valu
Peter Kasting
2016/03/30 05:33:37
No.
ICON_LABEL_VIEW_TRAILING_PADDING is the numbe
varkha
2016/03/30 06:17:29
I see now, thanks for the explanation. I guess I w
| |
239 } | 246 } |
240 | 247 |
241 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | 248 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
242 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | 249 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
243 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | 250 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); |
244 } | 251 } |
245 | 252 |
246 void IconLabelBubbleView::SetLabelBackgroundColor( | 253 void IconLabelBubbleView::SetLabelBackgroundColor( |
247 SkColor chip_background_color) { | 254 SkColor chip_background_color) { |
248 // The background images are painted atop |parent_background_color_|. | 255 // The background images are painted atop |parent_background_color_|. |
249 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 256 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
250 // determine the actual color the label text will sit atop. | 257 // determine the actual color the label text will sit atop. |
251 // Tricky bit: We alpha blend an opaque version of |chip_background_color| | 258 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
252 // against |parent_background_color_| using the original image grid color's | 259 // against |parent_background_color_| using the original image grid color's |
253 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 260 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
254 // even if |a| is a color with non-255 alpha. | 261 // even if |a| is a color with non-255 alpha. |
255 label_->SetBackgroundColor(color_utils::AlphaBlend( | 262 label_->SetBackgroundColor(color_utils::AlphaBlend( |
256 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 263 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
257 SkColorGetA(chip_background_color))); | 264 SkColorGetA(chip_background_color))); |
258 } | 265 } |
259 | 266 |
260 int IconLabelBubbleView::GetInternalSpacing() const { | 267 int IconLabelBubbleView::GetInternalSpacing() const { |
261 return GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING); | 268 return GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING) - |
269 builtin_trailing_padding_; | |
262 } | 270 } |
263 | 271 |
264 const char* IconLabelBubbleView::GetClassName() const { | 272 const char* IconLabelBubbleView::GetClassName() const { |
265 return "IconLabelBubbleView"; | 273 return "IconLabelBubbleView"; |
266 } | 274 } |
267 | 275 |
268 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 276 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
269 if (!ShouldShowBackground()) | 277 if (!ShouldShowBackground()) |
270 return; | 278 return; |
271 if (background_painter_) { | 279 if (background_painter_) { |
272 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 280 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
273 GetContentsBounds()); | 281 GetContentsBounds()); |
274 } | 282 } |
275 if (background()) | 283 if (background()) |
276 background()->Paint(canvas, this); | 284 background()->Paint(canvas, this); |
277 } | 285 } |
OLD | NEW |