Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / icon_label_bubble_view.cc
blobb8eca339a1b15ebda314f5ac381c88fb3ab20f6d
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"
16 namespace {
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);
27 } // namespace
29 IconLabelBubbleView::IconLabelBubbleView(int contained_image,
30 const gfx::FontList& font_list,
31 SkColor text_color,
32 SkColor parent_background_color,
33 bool elide_in_middle)
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) {
40 image_->SetImage(
41 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
42 contained_image));
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);
48 AddChildView(image_);
50 label_->SetEnabledColor(text_color);
52 if (elide_in_middle)
53 label_->SetElideBehavior(gfx::ELIDE_MIDDLE);
54 AddChildView(label_);
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
65 // background.
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(
76 background_image_id);
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 {
91 return true;
94 double IconLabelBubbleView::WidthMultiplier() const {
95 return 1.0;
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),
116 height());
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());
133 return size;
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())
168 return;
169 if (background_painter_)
170 background_painter_->Paint(canvas, size());