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" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 double IconLabelBubbleView::WidthMultiplier() const { | 104 double IconLabelBubbleView::WidthMultiplier() const { |
| 105 return 1.0; | 105 return 1.0; |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool IconLabelBubbleView::IsShrinking() const { | 108 bool IconLabelBubbleView::IsShrinking() const { |
| 109 return false; | 109 return false; |
| 110 } | 110 } |
| 111 | 111 |
| 112 int IconLabelBubbleView::GetImageAndPaddingWidth() const { | 112 int IconLabelBubbleView::GetImageAndPaddingWidth() const { |
| 113 const int image_width = image_->GetPreferredSize().width(); | 113 const int image_width = image_->GetPreferredSize().width(); |
| 114 return image_width | 114 return image_width ? (image_width + GetInternalSpacing()) : 0; |
| 115 ? image_width + GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) | |
| 116 : 0; | |
| 117 } | 115 } |
| 118 | 116 |
| 119 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 117 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 120 // Height will be ignored by the LocationBarView. | 118 // Height will be ignored by the LocationBarView. |
| 121 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 119 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 122 } | 120 } |
| 123 | 121 |
| 124 void IconLabelBubbleView::Layout() { | 122 void IconLabelBubbleView::Layout() { |
| 125 // Compute the label bounds. The label gets whatever size is left over after | 123 // Compute the label bounds. The label gets whatever size is left over after |
| 126 // accounting for the preferred image width and padding amounts. Note that if | 124 // accounting for the preferred image width and padding amounts. Note that if |
| 127 // the label has zero size it doesn't actually matter what we compute its X | 125 // the label has zero size it doesn't actually matter what we compute its X |
| 128 // value to be, since it won't be visible, so the X value can be "wrong" | 126 // value to be, since it won't be visible, so the X value can be "wrong" |
| 129 // compared to where the right edge of the image is computed to be below. | 127 // compared to where the right edge of the image is computed to be below. |
| 130 // This means doing this layout doesn't doesn't depend on any of the layout | 128 // This means doing this layout doesn't doesn't depend on any of the layout |
| 131 // below. That layout, however, may need for this layout to have already | 129 // below. That layout, however, may need for this layout to have already |
| 132 // happened, since the value of ShouldShowBackground() we read below may | 130 // happened, since the value of ShouldShowBackground() we read below may |
| 133 // depend on whether the label has nonzero size. Therefore, we do this first. | 131 // depend on whether the label has nonzero size. Therefore, we do this first. |
| 134 const int label_x = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); | 132 const int label_x = GetOuterPadding(true) + GetImageAndPaddingWidth(); |
| 135 const int label_width = | 133 const int label_width = |
| 136 std::max(0, width() - label_x - GetBubbleOuterPadding(false)); | 134 std::max(0, width() - label_x - GetOuterPadding(false)); |
| 137 label_->SetBounds(label_x, 0, label_width, height()); | 135 label_->SetBounds(label_x, 0, label_width, height()); |
| 138 | 136 |
| 139 // Now compute the image bounds. In non-MD, the leading padding depends on | 137 // Now compute the image bounds. In non-MD, the leading padding depends on |
| 140 // whether this is an extension icon, since extension icons and | 138 // whether this is an extension icon, since extension icons and |
| 141 // Chrome-provided icons are different sizes. In MD, these sizes are the | 139 // Chrome-provided icons are different sizes. In MD, these sizes are the |
| 142 // same, so it's not necessary to handle the two types differently. | 140 // same, so it's not necessary to handle the two types differently. |
| 143 const bool icon_has_enough_padding = | 141 const bool icon_has_enough_padding = |
| 144 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); | 142 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); |
| 145 int image_x = GetBubbleOuterPadding(icon_has_enough_padding); | 143 int image_x = GetOuterPadding(icon_has_enough_padding); |
| 146 const int image_preferred_width = image_->GetPreferredSize().width(); | 144 int bubble_trailing_padding = GetOuterPadding(false); |
| 147 int bubble_trailing_padding = GetBubbleOuterPadding(false); | |
| 148 | 145 |
| 149 // If ShouldShowBackground() is true, then either we show a background in the | 146 // If ShouldShowBackground() is true, then either we show a background in the |
| 150 // steady state, or we're not yet in the last portion of the animation. In | 147 // steady state, or we're not yet in the last portion of the animation. In |
| 151 // these cases, we leave the leading and trailing padding alone; we don't want | 148 // these cases, we leave the leading and trailing padding alone; we don't want |
| 152 // to let the image overlap the edge of the background, as this looks glitchy. | 149 // to let the image overlap the edge of the background, as this looks glitchy. |
| 153 // If this is false, however, then we're only showing the image, and either | 150 // If this is false, however, then we're only showing the image, and either |
| 154 // the view width is the image width, or it's animating downwards and getting | 151 // the view width is the image width, or it's animating downwards and getting |
| 155 // close to it. In these cases, we want to shrink the trailing padding first, | 152 // close to it. In these cases, we want to shrink the trailing padding first, |
| 156 // so the image slides all the way to the trailing edge before slowing or | 153 // so the image slides all the way to the trailing edge before slowing or |
| 157 // stopping; then we want to shrink the leading padding down to zero. | 154 // stopping; then we want to shrink the leading padding down to zero. |
| 155 const int image_preferred_width = image_->GetPreferredSize().width(); | |
| 158 if (!ShouldShowBackground()) { | 156 if (!ShouldShowBackground()) { |
| 159 image_x = std::min(image_x, width() - image_preferred_width); | 157 image_x = std::min(image_x, width() - image_preferred_width); |
| 160 bubble_trailing_padding = std::min( | 158 bubble_trailing_padding = std::min( |
| 161 bubble_trailing_padding, width() - image_preferred_width - image_x); | 159 bubble_trailing_padding, width() - image_preferred_width - image_x); |
| 162 } | 160 } |
| 163 | 161 |
| 164 // Now that we've computed the padding values, give the image all the | 162 // Now that we've computed the padding values, give the image all the |
| 165 // remaining width. This will be less than the image's preferred width during | 163 // remaining width. This will be less than the image's preferred width during |
| 166 // the first portion of the animation; during the very beginning there may not | 164 // the first portion of the animation; during the very beginning there may not |
| 167 // be enough room to show the image at all. | 165 // be enough room to show the image at all. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 | 207 |
| 210 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 208 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 211 return ui::MaterialDesignController::IsModeMaterial() | 209 return ui::MaterialDesignController::IsModeMaterial() |
| 212 ? GetNativeTheme()->GetSystemColor( | 210 ? GetNativeTheme()->GetSystemColor( |
| 213 ui::NativeTheme::kColorId_TextfieldDefaultBackground) | 211 ui::NativeTheme::kColorId_TextfieldDefaultBackground) |
| 214 : parent_background_color_; | 212 : parent_background_color_; |
| 215 } | 213 } |
| 216 | 214 |
| 217 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 215 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 218 gfx::Size size(image_->GetPreferredSize()); | 216 gfx::Size size(image_->GetPreferredSize()); |
| 219 bool shrinking = IsShrinking(); | 217 const bool shrinking = IsShrinking(); |
| 220 // Animation continues for the last few pixels even after the label is not | 218 // Animation continues for the last few pixels even after the label is not |
| 221 // visible in order to slide the icon into its final position. Therefore it | 219 // visible in order to slide the icon into its final position. Therefore it |
| 222 // 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 |
| 223 // as long as the animation is still shrinking. | 221 // as long as the animation is still shrinking. |
| 224 if (ShouldShowBackground() || shrinking) { | 222 if (ShouldShowBackground() || shrinking) { |
| 225 const int image_width = size.width(); | |
| 226 const int padding = GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) + | |
| 227 GetBubbleOuterPadding(true) + | |
| 228 GetBubbleOuterPadding(false); | |
| 229 // |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 |
| 230 // 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 |
| 231 // 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 |
| 232 // 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 |
| 233 // 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 |
| 234 // pop back to an icon after the animation finishes. | 228 // pop back to an icon after the animation finishes. |
| 235 int total_width = WidthMultiplier() * (label_width + image_width + padding); | 229 const int max_width = |
| 236 if (shrinking) | 230 label_width + MinimumWidthForImageWithBackgroundShown(); |
|
varkha
2016/03/30 03:03:42
Don't you need to include GetLayoutConstant(ICON_L
Peter Kasting
2016/03/30 03:56:54
Yes. Not sure how I didn't notice this.
| |
| 237 total_width = std::max(total_width, image_width); | 231 const int current_width = WidthMultiplier() * max_width; |
| 238 size.set_width(total_width); | 232 size.set_width( |
| 233 shrinking ? std::max(current_width, size.width()) : current_width); | |
| 239 } | 234 } |
| 240 return size; | 235 return size; |
| 241 } | 236 } |
| 242 | 237 |
| 243 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const { | 238 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { |
| 244 if (ui::MaterialDesignController::IsModeMaterial()) | 239 return GetOuterPadding(true) + image_->GetPreferredSize().width() + |
| 245 return GetBubbleOuterPaddingMd(leading); | 240 GetOuterPadding(false); |
| 246 | |
| 247 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | |
| 248 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | |
| 249 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | |
| 250 } | 241 } |
| 251 | 242 |
| 252 void IconLabelBubbleView::SetLabelBackgroundColor( | 243 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 253 SkColor chip_background_color) { | 244 SkColor chip_background_color) { |
| 254 // The background images are painted atop |parent_background_color_|. | 245 // The background images are painted atop |parent_background_color_|. |
| 255 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 246 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 256 // determine the actual color the label text will sit atop. | 247 // determine the actual color the label text will sit atop. |
| 257 // 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| |
| 258 // against |parent_background_color_| using the original image grid color's | 249 // against |parent_background_color_| using the original image grid color's |
| 259 // 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 |
| 260 // even if |a| is a color with non-255 alpha. | 251 // even if |a| is a color with non-255 alpha. |
| 261 label_->SetBackgroundColor(color_utils::AlphaBlend( | 252 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 262 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 253 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 263 SkColorGetA(chip_background_color))); | 254 SkColorGetA(chip_background_color))); |
| 264 } | 255 } |
| 265 | 256 |
| 266 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { | 257 int IconLabelBubbleView::GetOuterPadding(bool leading) const { |
| 267 // When the image is empty, leading and trailing padding are equal. | 258 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 268 if (image_->GetPreferredSize().IsEmpty() || !leading) | 259 // Leading and trailing padding are equal. |
| 269 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 260 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
| 261 } | |
| 270 | 262 |
| 271 // Leading padding is 2dp. | 263 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
| 272 return 2; | 264 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
| 265 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | |
| 266 } | |
| 267 | |
| 268 int IconLabelBubbleView::GetInternalSpacing() const { | |
| 269 return GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING); | |
| 273 } | 270 } |
| 274 | 271 |
| 275 const char* IconLabelBubbleView::GetClassName() const { | 272 const char* IconLabelBubbleView::GetClassName() const { |
| 276 return "IconLabelBubbleView"; | 273 return "IconLabelBubbleView"; |
| 277 } | 274 } |
| 278 | 275 |
| 279 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 276 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 280 if (!ShouldShowBackground()) | 277 if (!ShouldShowBackground()) |
| 281 return; | 278 return; |
| 282 if (background_painter_) { | 279 if (background_painter_) { |
| 283 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 280 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 284 GetContentsBounds()); | 281 GetContentsBounds()); |
| 285 } | 282 } |
| 286 if (background()) | 283 if (background()) |
| 287 background()->Paint(canvas, this); | 284 background()->Paint(canvas, this); |
| 288 } | 285 } |
| OLD | NEW |