| 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/themes/theme_properties.h" | 8 #include "chrome/browser/ui/views/layout_constants.h" |
| 9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/base/theme_provider.h" | |
| 11 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/color_utils.h" | 11 #include "ui/gfx/color_utils.h" |
| 13 #include "ui/views/controls/image_view.h" | 12 #include "ui/views/controls/image_view.h" |
| 14 #include "ui/views/painter.h" | 13 #include "ui/views/painter.h" |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 SkColor CalculateImageColor(gfx::ImageSkia* image) { | 17 SkColor CalculateImageColor(gfx::ImageSkia* image) { |
| 19 // We grab the color of the middle pixel of the image, which we treat as | 18 // 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 | 19 // the representative color of the entire image (reasonable, given the current |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 97 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 99 // Height will be ignored by the LocationBarView. | 98 // Height will be ignored by the LocationBarView. |
| 100 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 99 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 101 } | 100 } |
| 102 | 101 |
| 103 void IconLabelBubbleView::Layout() { | 102 void IconLabelBubbleView::Layout() { |
| 104 const int image_width = image()->GetPreferredSize().width(); | 103 const int image_width = image()->GetPreferredSize().width(); |
| 105 image_->SetBounds(std::min((width() - image_width) / 2, | 104 image_->SetBounds(std::min((width() - image_width) / 2, |
| 106 GetBubbleOuterPadding(!is_extension_icon_)), | 105 GetBubbleOuterPadding(!is_extension_icon_)), |
| 107 0, image_->GetPreferredSize().width(), height()); | 106 0, image_->GetPreferredSize().width(), height()); |
| 108 const int horizontal_item_padding = GetThemeProvider()->GetDisplayProperty( | 107 |
| 109 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING); | 108 const int padding = GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING); |
| 110 int pre_label_width = | 109 int pre_label_width = |
| 111 GetBubbleOuterPadding(true) + | 110 GetBubbleOuterPadding(true) + (image_width ? (image_width + padding) : 0); |
| 112 (image_width ? (image_width + horizontal_item_padding) : 0); | |
| 113 | |
| 114 label_->SetBounds(pre_label_width, 0, | 111 label_->SetBounds(pre_label_width, 0, |
| 115 width() - pre_label_width - GetBubbleOuterPadding(false), | 112 width() - pre_label_width - GetBubbleOuterPadding(false), |
| 116 height()); | 113 height()); |
| 117 } | 114 } |
| 118 | 115 |
| 119 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { | 116 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { |
| 120 gfx::Size size(image_->GetPreferredSize()); | 117 gfx::Size size(image_->GetPreferredSize()); |
| 121 if (ShouldShowBackground()) { | 118 if (ShouldShowBackground()) { |
| 122 const int image_width = image_->GetPreferredSize().width(); | 119 const int image_width = image_->GetPreferredSize().width(); |
| 123 const int horizontal_item_padding = GetThemeProvider()->GetDisplayProperty( | 120 const int padding = GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING); |
| 124 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING); | |
| 125 const int non_label_width = | 121 const int non_label_width = |
| 126 GetBubbleOuterPadding(true) + | 122 GetBubbleOuterPadding(true) + |
| 127 (image_width ? (image_width + horizontal_item_padding) : 0) + | 123 (image_width ? (image_width + padding) : 0) + |
| 128 GetBubbleOuterPadding(false); | 124 GetBubbleOuterPadding(false); |
| 129 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); | 125 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); |
| 130 size.SetToMax(background_painter_->GetMinimumSize()); | 126 size.SetToMax(background_painter_->GetMinimumSize()); |
| 131 } | 127 } |
| 132 | 128 |
| 133 return size; | 129 return size; |
| 134 } | 130 } |
| 135 | 131 |
| 136 int IconLabelBubbleView::GetBubbleOuterPadding(bool by_icon) const { | 132 int IconLabelBubbleView::GetBubbleOuterPadding(bool by_icon) const { |
| 137 ui::ThemeProvider* theme_provider = GetThemeProvider(); | 133 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
| 138 const int bubble_horizontal_padding = theme_provider->GetDisplayProperty( | 134 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
| 139 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING); | 135 (by_icon ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_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); | |
| 146 } | 136 } |
| 147 | 137 |
| 148 void IconLabelBubbleView::SetLabelBackgroundColor( | 138 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 149 SkColor background_image_color) { | 139 SkColor background_image_color) { |
| 150 // The background images are painted atop |parent_background_color_|. | 140 // The background images are painted atop |parent_background_color_|. |
| 151 // Alpha-blend |background_image_color| with |parent_background_color_| to | 141 // Alpha-blend |background_image_color| with |parent_background_color_| to |
| 152 // determine the actual color the label text will sit atop. | 142 // determine the actual color the label text will sit atop. |
| 153 // Tricky bit: We alpha blend an opaque version of |background_image_color| | 143 // Tricky bit: We alpha blend an opaque version of |background_image_color| |
| 154 // against |parent_background_color_| using the original image grid color's | 144 // against |parent_background_color_| using the original image grid color's |
| 155 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 145 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 156 // even if |a| is a color with non-255 alpha. | 146 // even if |a| is a color with non-255 alpha. |
| 157 label_->SetBackgroundColor(color_utils::AlphaBlend( | 147 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 158 SkColorSetA(background_image_color, 255), parent_background_color_, | 148 SkColorSetA(background_image_color, 255), parent_background_color_, |
| 159 SkColorGetA(background_image_color))); | 149 SkColorGetA(background_image_color))); |
| 160 } | 150 } |
| 161 | 151 |
| 162 const char* IconLabelBubbleView::GetClassName() const { | 152 const char* IconLabelBubbleView::GetClassName() const { |
| 163 return "IconLabelBubbleView"; | 153 return "IconLabelBubbleView"; |
| 164 } | 154 } |
| 165 | 155 |
| 166 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 156 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 167 if (!ShouldShowBackground()) | 157 if (!ShouldShowBackground()) |
| 168 return; | 158 return; |
| 169 if (background_painter_) | 159 if (background_painter_) |
| 170 background_painter_->Paint(canvas, size()); | 160 background_painter_->Paint(canvas, size()); |
| 171 } | 161 } |
| OLD | NEW |