| 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 return image_->GetPreferredSize().width() + GetInternalSpacing(); | |
| 114 } | |
| 115 | |
| 116 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 122 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 117 // Height will be ignored by the LocationBarView. | 123 // Height will be ignored by the LocationBarView. |
| 118 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 124 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 119 } | 125 } |
| 120 | 126 |
| 121 void IconLabelBubbleView::Layout() { | 127 void IconLabelBubbleView::Layout() { |
| 122 // 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 |
| 123 // whether this is an extension icon, since extension icons and | 129 // whether this is an extension icon, since extension icons and |
| 124 // 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 |
| 125 // 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... |
| 150 // be enough room to show the image at all. | 156 // be enough room to show the image at all. |
| 151 const int image_width = | 157 const int image_width = |
| 152 std::min(image_preferred_width, | 158 std::min(image_preferred_width, |
| 153 std::max(0, width() - image_x - bubble_trailing_padding)); | 159 std::max(0, width() - image_x - bubble_trailing_padding)); |
| 154 image_->SetBounds(image_x, 0, image_width, height()); | 160 image_->SetBounds(image_x, 0, image_width, height()); |
| 155 | 161 |
| 156 // 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 |
| 157 // 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 |
| 158 // 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 |
| 159 // value to be, since it won't be visible. | 165 // value to be, since it won't be visible. |
| 160 const int label_x = image_x + GetImageAndPaddingWidth(); | 166 const int label_x = image_x + image_width + GetInternalSpacing(); |
| 161 const int label_width = | 167 const int label_width = |
| 162 std::max(0, width() - label_x - bubble_trailing_padding); | 168 std::max(0, width() - label_x - bubble_trailing_padding); |
| 163 label_->SetBounds(label_x, 0, label_width, height()); | 169 label_->SetBounds(label_x, 0, label_width, height()); |
| 164 } | 170 } |
| 165 | 171 |
| 166 void IconLabelBubbleView::OnNativeThemeChanged( | 172 void IconLabelBubbleView::OnNativeThemeChanged( |
| 167 const ui::NativeTheme* native_theme) { | 173 const ui::NativeTheme* native_theme) { |
| 168 label_->SetEnabledColor(GetTextColor()); | 174 label_->SetEnabledColor(GetTextColor()); |
| 169 | 175 |
| 170 if (!ui::MaterialDesignController::IsModeMaterial()) | 176 if (!ui::MaterialDesignController::IsModeMaterial()) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 // is necessary to animate |total_width| even when the background is hidden | 220 // is necessary to animate |total_width| even when the background is hidden |
| 215 // as long as the animation is still shrinking. | 221 // as long as the animation is still shrinking. |
| 216 if (ShouldShowBackground() || shrinking) { | 222 if (ShouldShowBackground() || shrinking) { |
| 217 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 223 // |multiplier| grows from zero to one, stays equal to one and then shrinks |
| 218 // to zero again. The view width should correspondingly grow from zero to | 224 // to zero again. The view width should correspondingly grow from zero to |
| 219 // fully showing both label and icon, stay there, then shrink to just large | 225 // fully showing both label and icon, stay there, then shrink to just large |
| 220 // enough to show the icon. We don't want to shrink all the way back to | 226 // enough to show the icon. We don't want to shrink all the way back to |
| 221 // zero, since this would mean the view would completely disappear and then | 227 // zero, since this would mean the view would completely disappear and then |
| 222 // pop back to an icon after the animation finishes. | 228 // pop back to an icon after the animation finishes. |
| 223 const int max_width = MinimumWidthForImageWithBackgroundShown() + | 229 const int max_width = MinimumWidthForImageWithBackgroundShown() + |
| 224 GetInternalSpacing() + label_width; | 230 GetInternalSpacing() + label_width; |
| 225 const int current_width = WidthMultiplier() * max_width; | 231 const int current_width = WidthMultiplier() * max_width; |
| 226 size.set_width( | 232 size.set_width(shrinking ? std::max(current_width, size.width()) |
| 227 shrinking ? std::max(current_width, size.width()) : current_width); | 233 : current_width); |
| 228 } | 234 } |
| 229 return size; | 235 return size; |
| 230 } | 236 } |
| 231 | 237 |
| 232 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { | 238 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { |
| 233 return GetOuterPadding(true) + image_->GetPreferredSize().width() + | 239 return GetOuterPadding(true) + image_->GetPreferredSize().width() + |
| 234 GetOuterPadding(false); | 240 GetOuterPadding(false); |
| 235 } | 241 } |
| 236 | 242 |
| 237 void IconLabelBubbleView::SetLabelBackgroundColor( | 243 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 238 SkColor chip_background_color) { | 244 SkColor chip_background_color) { |
| 239 // The background images are painted atop |parent_background_color_|. | 245 // The background images are painted atop |parent_background_color_|. |
| 240 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 246 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 241 // determine the actual color the label text will sit atop. | 247 // determine the actual color the label text will sit atop. |
| 242 // Tricky bit: We alpha blend an opaque version of |chip_background_color| | 248 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
| 243 // against |parent_background_color_| using the original image grid color's | 249 // against |parent_background_color_| using the original image grid color's |
| 244 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 250 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 245 // even if |a| is a color with non-255 alpha. | 251 // even if |a| is a color with non-255 alpha. |
| 246 label_->SetBackgroundColor(color_utils::AlphaBlend( | 252 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 247 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 253 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 248 SkColorGetA(chip_background_color))); | 254 SkColorGetA(chip_background_color))); |
| 249 } | 255 } |
| 250 | 256 |
| 251 int IconLabelBubbleView::GetOuterPadding(bool leading) const { | 257 int IconLabelBubbleView::GetOuterPadding(bool leading) const { |
| 252 if (ui::MaterialDesignController::IsModeMaterial()) { | 258 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 253 // Leading and trailing padding are equal. | 259 // The apparent leading and trailing padding should be equal, so we need to |
| 254 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 260 // subtract the amount of built-in padding in the image. This will mean |
| 261 // that the actual padding + the padding inside the image add up to the same |
| 262 // amount of padding as on the trailing edge of the bubble. |
| 263 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) - |
| 264 (leading ? builtin_leading_padding_ : 0); |
| 255 } | 265 } |
| 256 | 266 |
| 257 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | 267 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
| 258 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | 268 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
| 259 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | 269 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); |
| 260 } | 270 } |
| 261 | 271 |
| 262 int IconLabelBubbleView::GetInternalSpacing() const { | 272 int IconLabelBubbleView::GetInternalSpacing() const { |
| 263 return image_->GetPreferredSize().IsEmpty() ? | 273 return image_->GetPreferredSize().IsEmpty() |
| 264 0 : GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING); | 274 ? 0 |
| 275 : (GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING) - |
| 276 builtin_trailing_padding_); |
| 265 } | 277 } |
| 266 | 278 |
| 267 const char* IconLabelBubbleView::GetClassName() const { | 279 const char* IconLabelBubbleView::GetClassName() const { |
| 268 return "IconLabelBubbleView"; | 280 return "IconLabelBubbleView"; |
| 269 } | 281 } |
| 270 | 282 |
| 271 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 283 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 272 if (!ShouldShowBackground()) | 284 if (!ShouldShowBackground()) |
| 273 return; | 285 return; |
| 274 if (background_painter_) { | 286 if (background_painter_) { |
| 275 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 287 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 276 GetContentsBounds()); | 288 GetContentsBounds()); |
| 277 } | 289 } |
| 278 if (background()) | 290 if (background()) |
| 279 background()->Paint(canvas, this); | 291 background()->Paint(canvas, this); |
| 280 } | 292 } |
| OLD | NEW |