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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/themes/theme_properties.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/base/theme_provider.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/color_utils.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/painter.h"
18 SkColor
CalculateImageColor(gfx::ImageSkia
* image
) {
19 // We grab the color of the middle pixel of the image, which we treat as
20 // the representative color of the entire image (reasonable, given the current
21 // appearance of these assets).
22 const SkBitmap
& bitmap(image
->GetRepresentation(1.0f
).sk_bitmap());
23 SkAutoLockPixels
pixel_lock(bitmap
);
24 return bitmap
.getColor(bitmap
.width() / 2, bitmap
.height() / 2);
29 IconLabelBubbleView::IconLabelBubbleView(int contained_image
,
30 const gfx::FontList
& font_list
,
32 SkColor parent_background_color
,
34 : background_painter_(nullptr),
35 image_(new views::ImageView()),
36 label_(new views::Label(base::string16(), font_list
)),
37 is_extension_icon_(false),
38 parent_background_color_(parent_background_color
) {
39 if (contained_image
) {
41 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
45 // Disable separate hit testing for |image_|. This prevents views treating
46 // |image_| as a separate mouse hover region from |this|.
47 image_
->set_interactive(false);
50 label_
->SetEnabledColor(text_color
);
53 label_
->SetElideBehavior(gfx::ELIDE_MIDDLE
);
57 IconLabelBubbleView::~IconLabelBubbleView() {
60 void IconLabelBubbleView::SetBackgroundImageGrid(
61 const int background_images
[]) {
62 background_painter_
.reset(
63 views::Painter::CreateImageGridPainter(background_images
));
64 // Use the middle image of the background to represent the color of the entire
66 gfx::ImageSkia
* background_image
=
67 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
68 background_images
[4]);
69 SetLabelBackgroundColor(CalculateImageColor(background_image
));
72 void IconLabelBubbleView::SetBackgroundImageWithInsets(int background_image_id
,
73 gfx::Insets
& insets
) {
74 gfx::ImageSkia
* background_image
=
75 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
77 background_painter_
.reset(
78 views::Painter::CreateImagePainter(*background_image
, insets
));
79 SetLabelBackgroundColor(CalculateImageColor(background_image
));
82 void IconLabelBubbleView::SetLabel(const base::string16
& label
) {
83 label_
->SetText(label
);
86 void IconLabelBubbleView::SetImage(const gfx::ImageSkia
& image_skia
) {
87 image_
->SetImage(image_skia
);
90 bool IconLabelBubbleView::ShouldShowBackground() const {
94 double IconLabelBubbleView::WidthMultiplier() const {
98 gfx::Size
IconLabelBubbleView::GetPreferredSize() const {
99 // Height will be ignored by the LocationBarView.
100 return GetSizeForLabelWidth(label_
->GetPreferredSize().width());
103 void IconLabelBubbleView::Layout() {
104 const int image_width
= image()->GetPreferredSize().width();
105 image_
->SetBounds(std::min((width() - image_width
) / 2,
106 GetBubbleOuterPadding(!is_extension_icon_
)),
107 0, image_
->GetPreferredSize().width(), height());
108 const int horizontal_item_padding
= GetThemeProvider()->GetDisplayProperty(
109 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING
);
110 int pre_label_width
=
111 GetBubbleOuterPadding(true) +
112 (image_width
? (image_width
+ horizontal_item_padding
) : 0);
114 label_
->SetBounds(pre_label_width
, 0,
115 width() - pre_label_width
- GetBubbleOuterPadding(false),
119 gfx::Size
IconLabelBubbleView::GetSizeForLabelWidth(int width
) const {
120 gfx::Size
size(image_
->GetPreferredSize());
121 if (ShouldShowBackground()) {
122 const int image_width
= image_
->width();
123 const int horizontal_item_padding
= GetThemeProvider()->GetDisplayProperty(
124 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING
);
125 const int non_label_width
=
126 GetBubbleOuterPadding(true) +
127 (image_width
? (image_width
+ horizontal_item_padding
) : 0) +
128 GetBubbleOuterPadding(false);
129 size
.Enlarge(WidthMultiplier() * (width
+ non_label_width
), 0);
130 size
.SetToMax(background_painter_
->GetMinimumSize());
136 int IconLabelBubbleView::GetBubbleOuterPadding(bool by_icon
) const {
137 ui::ThemeProvider
* theme_provider
= GetThemeProvider();
138 const int bubble_horizontal_padding
= theme_provider
->GetDisplayProperty(
139 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING
);
140 const int horizontal_item_padding
= theme_provider
->GetDisplayProperty(
141 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING
);
142 const int right_padding
= theme_provider
->GetDisplayProperty(
143 ThemeProperties::PROPERTY_ICON_LABEL_VIEW_TRAILING_PADDING
);
144 return horizontal_item_padding
- bubble_horizontal_padding
+
145 (by_icon
? 0 : right_padding
);
148 void IconLabelBubbleView::SetLabelBackgroundColor(
149 SkColor background_image_color
) {
150 // The background images are painted atop |parent_background_color_|.
151 // Alpha-blend |background_image_color| with |parent_background_color_| to
152 // determine the actual color the label text will sit atop.
153 // Tricky bit: We alpha blend an opaque version of |background_image_color|
154 // against |parent_background_color_| using the original image grid color's
155 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged
156 // even if |a| is a color with non-255 alpha.
157 label_
->SetBackgroundColor(color_utils::AlphaBlend(
158 SkColorSetA(background_image_color
, 255), parent_background_color_
,
159 SkColorGetA(background_image_color
)));
162 const char* IconLabelBubbleView::GetClassName() const {
163 return "IconLabelBubbleView";
166 void IconLabelBubbleView::OnPaint(gfx::Canvas
* canvas
) {
167 if (!ShouldShowBackground())
169 if (background_painter_
)
170 background_painter_
->Paint(canvas
, size());