Chromium Code Reviews| 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/gfx/image/image_util.h" |
| 15 #include "ui/gfx/scoped_canvas.h" | |
| 15 #include "ui/native_theme/native_theme.h" | 16 #include "ui/native_theme/native_theme.h" |
| 16 #include "ui/views/animation/ink_drop_hover.h" | 17 #include "ui/views/animation/ink_drop_hover.h" |
| 17 #include "ui/views/border.h" | 18 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
| 20 #include "ui/views/controls/textfield/textfield.h" | |
| 19 #include "ui/views/painter.h" | 21 #include "ui/views/painter.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 25 // Extra padding to place at the end of the chip when the label is showing. | |
| 26 const int kExtraTrailingPadding = 7; | |
| 27 | |
| 23 SkColor CalculateImageColor(gfx::ImageSkia* image) { | 28 SkColor CalculateImageColor(gfx::ImageSkia* image) { |
| 24 // We grab the color of the middle pixel of the image, which we treat as | 29 // We grab the color of the middle pixel of the image, which we treat as |
| 25 // the representative color of the entire image (reasonable, given the current | 30 // the representative color of the entire image (reasonable, given the current |
| 26 // appearance of these assets). | 31 // appearance of these assets). |
| 27 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); | 32 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); |
| 28 SkAutoLockPixels pixel_lock(bitmap); | 33 SkAutoLockPixels pixel_lock(bitmap); |
| 29 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); | 34 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); |
| 30 } | 35 } |
| 31 | 36 |
| 32 } // namespace | 37 } // namespace |
| 33 | 38 |
| 34 IconLabelBubbleView::IconLabelBubbleView(int contained_image, | 39 IconLabelBubbleView::IconLabelBubbleView(int contained_image, |
| 35 const gfx::FontList& font_list, | 40 const gfx::FontList& font_list, |
| 36 SkColor parent_background_color, | 41 SkColor parent_background_color, |
| 37 bool elide_in_middle) | 42 bool elide_in_middle) |
| 38 : background_painter_(nullptr), | 43 : background_painter_(nullptr), |
| 39 image_(new views::ImageView()), | 44 image_(new views::ImageView()), |
| 40 label_(new views::Label(base::string16(), font_list)), | 45 label_(new views::Label(base::string16(), font_list)), |
| 41 builtin_leading_padding_(0), | |
| 42 builtin_trailing_padding_(0), | |
| 43 is_extension_icon_(false), | 46 is_extension_icon_(false), |
| 44 parent_background_color_(parent_background_color) { | 47 parent_background_color_(parent_background_color) { |
| 45 if (contained_image) { | 48 if (contained_image) { |
| 46 image_->SetImage( | 49 image_->SetImage( |
| 47 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 50 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 48 contained_image)); | 51 contained_image)); |
| 49 } | 52 } |
| 50 | 53 |
| 51 // Disable separate hit testing for |image_|. This prevents views treating | 54 // Disable separate hit testing for |image_|. This prevents views treating |
| 52 // |image_| as a separate mouse hover region from |this|. | 55 // |image_| as a separate mouse hover region from |this|. |
| 53 image_->set_interactive(false); | 56 image_->set_interactive(false); |
| 54 AddChildView(image_); | 57 AddChildView(image_); |
| 55 | 58 |
| 56 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 59 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 57 | 60 |
| 58 if (elide_in_middle) | 61 if (elide_in_middle) |
| 59 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); | 62 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); |
| 60 AddChildView(label_); | 63 AddChildView(label_); |
| 61 | 64 |
| 62 // Bubbles are given the full internal height of the location bar so that all | 65 // Bubbles are given the full internal height of the location bar so that all |
| 63 // child views in the location bar have the same height. The visible height of | 66 // child views in the location bar have the same height. The visible height of |
| 64 // the bubble should be smaller, so use an empty border to shrink down the | 67 // the bubble should be smaller, so use an empty border to shrink down the |
| 65 // content bounds so the background gets painted correctly. | 68 // content bounds so the background gets painted correctly. |
| 66 const int padding = GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING); | 69 SetBorder(views::Border::CreateEmptyBorder( |
| 67 SetBorder( | 70 gfx::Insets(GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING), 0))); |
| 68 views::Border::CreateEmptyBorder(gfx::Insets(padding, 0, padding, 0))); | |
| 69 } | 71 } |
| 70 | 72 |
| 71 IconLabelBubbleView::~IconLabelBubbleView() { | 73 IconLabelBubbleView::~IconLabelBubbleView() { |
| 72 } | 74 } |
| 73 | 75 |
| 74 void IconLabelBubbleView::SetBackgroundImageGrid( | 76 void IconLabelBubbleView::SetBackgroundImageGrid( |
| 75 const int background_images[]) { | 77 const int background_images[]) { |
| 76 should_show_background_ = true; | 78 should_show_background_ = true; |
| 77 if (!ui::MaterialDesignController::IsModeMaterial()) { | 79 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 78 background_painter_.reset( | 80 background_painter_.reset( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 91 should_show_background_ = false; | 93 should_show_background_ = false; |
| 92 SetLabelBackgroundColor(SK_ColorTRANSPARENT); | 94 SetLabelBackgroundColor(SK_ColorTRANSPARENT); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 97 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
| 96 label_->SetText(label); | 98 label_->SetText(label); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 101 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
| 100 image_->SetImage(image_skia); | 102 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 } | |
| 108 } | 103 } |
| 109 | 104 |
| 110 bool IconLabelBubbleView::ShouldShowBackground() const { | 105 bool IconLabelBubbleView::ShouldShowBackground() const { |
| 111 return should_show_background_; | 106 return should_show_background_; |
| 112 } | 107 } |
| 113 | 108 |
| 114 double IconLabelBubbleView::WidthMultiplier() const { | 109 double IconLabelBubbleView::WidthMultiplier() const { |
| 115 return 1.0; | 110 return 1.0; |
| 116 } | 111 } |
| 117 | 112 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 const int label_x = image_x + image_width + GetInternalSpacing(); | 177 const int label_x = image_x + image_width + GetInternalSpacing(); |
| 183 const int label_width = | 178 const int label_width = |
| 184 std::max(0, width() - label_x - bubble_trailing_padding); | 179 std::max(0, width() - label_x - bubble_trailing_padding); |
| 185 label_->SetBounds(label_x, 0, label_width, height()); | 180 label_->SetBounds(label_x, 0, label_width, height()); |
| 186 } | 181 } |
| 187 | 182 |
| 188 void IconLabelBubbleView::OnNativeThemeChanged( | 183 void IconLabelBubbleView::OnNativeThemeChanged( |
| 189 const ui::NativeTheme* native_theme) { | 184 const ui::NativeTheme* native_theme) { |
| 190 label_->SetEnabledColor(GetTextColor()); | 185 label_->SetEnabledColor(GetTextColor()); |
| 191 | 186 |
| 192 if (!ui::MaterialDesignController::IsModeMaterial()) | 187 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 193 return; | 188 label_->SetBackgroundColor(GetParentBackgroundColor()); |
| 194 | 189 SchedulePaint(); |
| 195 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); | 190 } |
| 196 SkColor border_color = inverted ? SK_ColorWHITE : GetBorderColor(); | |
| 197 SkColor background_color = | |
| 198 inverted ? SK_ColorWHITE : SkColorSetA(border_color, 0x13); | |
| 199 set_background(new BackgroundWith1PxBorder(background_color, border_color)); | |
| 200 SetLabelBackgroundColor(background_color); | |
| 201 } | 191 } |
| 202 | 192 |
| 203 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 193 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
| 204 image()->SetPaintToLayer(true); | 194 image()->SetPaintToLayer(true); |
| 205 image()->layer()->SetFillsBoundsOpaquely(false); | 195 image()->layer()->SetFillsBoundsOpaquely(false); |
| 206 InkDropHostView::AddInkDropLayer(ink_drop_layer); | 196 InkDropHostView::AddInkDropLayer(ink_drop_layer); |
| 207 } | 197 } |
| 208 | 198 |
| 209 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 199 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
| 210 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 200 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 229 } | 219 } |
| 230 | 220 |
| 231 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 221 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 232 gfx::Size size(image_->GetPreferredSize()); | 222 gfx::Size size(image_->GetPreferredSize()); |
| 233 const bool shrinking = IsShrinking(); | 223 const bool shrinking = IsShrinking(); |
| 234 // Animation continues for the last few pixels even after the label is not | 224 // Animation continues for the last few pixels even after the label is not |
| 235 // visible in order to slide the icon into its final position. Therefore it | 225 // visible in order to slide the icon into its final position. Therefore it |
| 236 // is necessary to animate |total_width| even when the background is hidden | 226 // is necessary to animate |total_width| even when the background is hidden |
| 237 // as long as the animation is still shrinking. | 227 // as long as the animation is still shrinking. |
| 238 if (ShouldShowBackground() || shrinking) { | 228 if (ShouldShowBackground() || shrinking) { |
| 229 const int extra_trailing_padding = | |
| 230 ui::MaterialDesignController::IsModeMaterial() ? kExtraTrailingPadding | |
| 231 : 0; | |
| 239 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 232 // |multiplier| grows from zero to one, stays equal to one and then shrinks |
| 240 // to zero again. The view width should correspondingly grow from zero to | 233 // to zero again. The view width should correspondingly grow from zero to |
| 241 // fully showing both label and icon, stay there, then shrink to just large | 234 // fully showing both label and icon, stay there, then shrink to just large |
| 242 // enough to show the icon. We don't want to shrink all the way back to | 235 // enough to show the icon. We don't want to shrink all the way back to |
| 243 // zero, since this would mean the view would completely disappear and then | 236 // zero, since this would mean the view would completely disappear and then |
| 244 // pop back to an icon after the animation finishes. | 237 // pop back to an icon after the animation finishes. |
| 245 const int max_width = MinimumWidthForImageWithBackgroundShown() + | 238 const int max_width = MinimumWidthForImageWithBackgroundShown() + |
| 246 GetInternalSpacing() + label_width; | 239 GetInternalSpacing() + label_width + |
| 240 extra_trailing_padding; | |
| 247 const int current_width = WidthMultiplier() * max_width; | 241 const int current_width = WidthMultiplier() * max_width; |
| 248 size.set_width(shrinking ? std::max(current_width, size.width()) | 242 size.set_width(shrinking ? std::max(current_width, size.width()) |
| 249 : current_width); | 243 : current_width); |
| 250 } | 244 } |
| 251 return size; | 245 return size; |
| 252 } | 246 } |
| 253 | 247 |
| 254 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { | 248 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { |
| 255 return GetOuterPadding(true) + image_->GetPreferredSize().width() + | 249 return GetOuterPadding(true) + image_->GetPreferredSize().width() + |
| 256 GetOuterPadding(false); | 250 GetOuterPadding(false); |
| 257 } | 251 } |
| 258 | 252 |
| 259 void IconLabelBubbleView::SetLabelBackgroundColor( | 253 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 260 SkColor chip_background_color) { | 254 SkColor chip_background_color) { |
| 261 // The background images are painted atop |parent_background_color_|. | 255 // The background images are painted atop |parent_background_color_|. |
| 262 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 256 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 263 // determine the actual color the label text will sit atop. | 257 // determine the actual color the label text will sit atop. |
| 264 // 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| |
| 265 // against |parent_background_color_| using the original image grid color's | 259 // against |parent_background_color_| using the original image grid color's |
| 266 // 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 |
| 267 // even if |a| is a color with non-255 alpha. | 261 // even if |a| is a color with non-255 alpha. |
| 268 label_->SetBackgroundColor(color_utils::AlphaBlend( | 262 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 269 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 263 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 270 SkColorGetA(chip_background_color))); | 264 SkColorGetA(chip_background_color))); |
| 271 } | 265 } |
| 272 | 266 |
| 273 int IconLabelBubbleView::GetOuterPadding(bool leading) const { | 267 int IconLabelBubbleView::GetOuterPadding(bool leading) const { |
| 274 if (ui::MaterialDesignController::IsModeMaterial()) { | 268 if (ui::MaterialDesignController::IsModeMaterial()) |
| 275 // The apparent leading and trailing padding should be equal, so we need to | 269 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
| 276 // subtract the amount of built-in padding in the image. This will mean | |
| 277 // that the actual padding + the padding inside the image add up to the same | |
| 278 // amount of padding as on the trailing edge of the bubble. | |
| 279 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) - | |
| 280 (leading ? builtin_leading_padding_ : 0); | |
| 281 } | |
| 282 | 270 |
| 283 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | 271 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
| 284 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | 272 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
| 285 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | 273 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); |
| 286 } | 274 } |
| 287 | 275 |
| 288 int IconLabelBubbleView::GetInternalSpacing() const { | 276 int IconLabelBubbleView::GetInternalSpacing() const { |
| 289 return image_->GetPreferredSize().IsEmpty() | 277 return image_->GetPreferredSize().IsEmpty() |
| 290 ? 0 | 278 ? 0 |
| 291 : (GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING) - | 279 : GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING); |
| 292 builtin_trailing_padding_); | |
| 293 } | 280 } |
| 294 | 281 |
| 295 const char* IconLabelBubbleView::GetClassName() const { | 282 const char* IconLabelBubbleView::GetClassName() const { |
| 296 return "IconLabelBubbleView"; | 283 return "IconLabelBubbleView"; |
| 297 } | 284 } |
| 298 | 285 |
| 299 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 286 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 300 if (!ShouldShowBackground()) | 287 if (!ShouldShowBackground()) |
| 301 return; | 288 return; |
| 302 if (background_painter_) { | 289 if (background_painter_) { |
| 303 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 290 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 304 GetContentsBounds()); | 291 GetContentsBounds()); |
| 305 } | 292 } |
| 306 if (background()) | 293 |
| 307 background()->Paint(canvas, this); | 294 // In MD, draw a separator and not a background. |
| 295 if (ui::MaterialDesignController::IsModeMaterial()) { | |
| 296 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | |
| 297 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
| 298 const SkColor separator_color = SkColorSetA( | |
| 299 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
| 300 | |
| 301 // Amount of padding that is built into the whatever view comes after this | |
| 302 // one in the location bar (e.g. omnibox textfield). | |
| 303 const int post_chip_spacing = | |
| 304 GetLayoutConstant(LOCATION_BAR_PRE_EDIT_SPACING) + | |
| 305 views::Textfield::kTextPadding; | |
|
Peter Kasting
2016/05/25 00:03:27
Is this calculation correct for the blocked popup
Evan Stade
2016/05/25 20:03:44
I found a different way of doing this that is perh
| |
| 306 // Amount of space to leave after the separator (dp). | |
| 307 const int kPostSeparatorSpacing = | |
| 308 (GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) + | |
| 309 kExtraTrailingPadding + post_chip_spacing) / | |
| 310 2 - | |
| 311 post_chip_spacing; | |
| 312 // Height of the separator (dp). | |
| 313 const int kSeparatorHeight = 16; | |
| 314 gfx::Rect bounds(GetLocalBounds()); | |
| 315 bounds.Inset(kPostSeparatorSpacing, | |
| 316 (bounds.height() - kSeparatorHeight) / 2); | |
| 317 | |
| 318 // 1px at all scale factors. | |
| 319 gfx::ScopedCanvas scoped_canvas(canvas); | |
| 320 const float scale = canvas->UndoDeviceScaleFactor(); | |
| 321 const gfx::RectF scaled_bounds = gfx::ScaleRect(gfx::RectF(bounds), scale); | |
| 322 canvas->DrawLine(scaled_bounds.top_right(), scaled_bounds.bottom_right(), | |
| 323 separator_color); | |
| 324 } | |
| 308 } | 325 } |
| OLD | NEW |