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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool IconLabelBubbleView::ShouldShowBackground() const { | 100 bool IconLabelBubbleView::ShouldShowBackground() const { |
| 101 return should_show_background_; | 101 return should_show_background_; |
| 102 } | 102 } |
| 103 | 103 |
| 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 { | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 108 int IconLabelBubbleView::GetImageAndPaddingWidth() const { | 112 int IconLabelBubbleView::GetImageAndPaddingWidth() const { |
| 109 const int image_width = image_->GetPreferredSize().width(); | 113 const int image_width = image_->GetPreferredSize().width(); |
| 110 return image_width | 114 return image_width |
| 111 ? image_width + GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) | 115 ? image_width + GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) |
| 112 : 0; | 116 : 0; |
| 113 } | 117 } |
| 114 | 118 |
| 115 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 119 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 116 // Height will be ignored by the LocationBarView. | 120 // Height will be ignored by the LocationBarView. |
| 117 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 121 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 118 } | 122 } |
| 119 | 123 |
| 120 void IconLabelBubbleView::Layout() { | 124 void IconLabelBubbleView::Layout() { |
| 121 // In MD mode, both extension icons and Chrome-provided icons are 16px, | 125 // In MD mode, both extension icons and Chrome-provided icons are 16px, |
| 122 // so it's not necessary to handle them differently. TODO(estade): clean | 126 // so it's not necessary to handle them differently. TODO(estade): clean |
| 123 // this up when MD is on by default. | 127 // this up when MD is on by default. |
| 124 bool icon_has_enough_padding = | 128 bool icon_has_enough_padding = |
| 125 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); | 129 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); |
| 126 const int image_width = image_->GetPreferredSize().width(); | 130 const int image_preferred_width = image_->GetPreferredSize().width(); |
| 127 image_->SetBounds(std::min((width() - image_width) / 2, | 131 // image is positioned at a constant offset when a label is visible or when |
| 128 GetBubbleOuterPadding(icon_has_enough_padding)), | 132 // there is enough room to fit the whole image. This is done so that the image |
| 129 0, image_->GetPreferredSize().width(), height()); | 133 // slides in as a part of the sliding surface and doesn't shift relative to |
| 134 // the background. | |
| 135 int image_padding = GetBubbleOuterPadding(false); | |
|
Peter Kasting
2016/03/11 01:14:42
Nit: image_trailing_padding?
varkha
2016/03/11 03:08:40
Done.
| |
| 136 int image_x = GetBubbleOuterPadding(icon_has_enough_padding); | |
| 137 if (!label_->visible() || image_x + image_preferred_width > width()) { | |
| 138 image_padding = 0; | |
| 139 image_x = std::min(image_x, width() - image_preferred_width); | |
|
Peter Kasting
2016/03/11 01:14:42
Doesn't this line result in pinning the trailing e
varkha
2016/03/11 03:08:40
Yes, it was a bad regression.
| |
| 140 } | |
| 141 const int image_width = | |
| 142 std::min(image_preferred_width, width() - image_x - image_padding); | |
| 143 image_->SetBounds(image_x, 0, image_width, height()); | |
| 130 | 144 |
| 131 int pre_label_width = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); | 145 int label_x = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); |
| 132 label_->SetBounds(pre_label_width, 0, | 146 const int label_width = |
| 133 width() - pre_label_width - GetBubbleOuterPadding(false), | 147 std::max(0, width() - label_x - GetBubbleOuterPadding(false)); |
| 134 height()); | 148 |
| 149 // Hiding the label when it no longer fits and when the leading edge of the | |
| 150 // image plus padding crosses the right edge of the bubble allows the final | |
| 151 // frames of the animation to smoothly transition from showing a bubble to | |
| 152 // showing just the image. | |
| 153 if (IsShrinking() && label_->visible() && label_width == 0 && | |
| 154 (image_x + image_preferred_width + image_padding >= bounds().width())) { | |
| 155 label_->SetVisible(false); | |
| 156 } | |
| 157 | |
| 158 label_->SetBounds(label_x, 0, label_width, height()); | |
| 135 } | 159 } |
| 136 | 160 |
| 137 void IconLabelBubbleView::OnNativeThemeChanged( | 161 void IconLabelBubbleView::OnNativeThemeChanged( |
| 138 const ui::NativeTheme* native_theme) { | 162 const ui::NativeTheme* native_theme) { |
| 139 label_->SetEnabledColor(GetTextColor()); | 163 label_->SetEnabledColor(GetTextColor()); |
| 140 | 164 |
| 141 if (!ui::MaterialDesignController::IsModeMaterial()) | 165 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 142 return; | 166 return; |
| 143 | 167 |
| 144 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); | 168 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 170 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 194 return color_utils::DeriveDefaultIconColor(GetTextColor()); |
| 171 } | 195 } |
| 172 | 196 |
| 173 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 197 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 174 return ui::MaterialDesignController::IsModeMaterial() | 198 return ui::MaterialDesignController::IsModeMaterial() |
| 175 ? GetNativeTheme()->GetSystemColor( | 199 ? GetNativeTheme()->GetSystemColor( |
| 176 ui::NativeTheme::kColorId_TextfieldDefaultBackground) | 200 ui::NativeTheme::kColorId_TextfieldDefaultBackground) |
| 177 : parent_background_color_; | 201 : parent_background_color_; |
| 178 } | 202 } |
| 179 | 203 |
| 180 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { | 204 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 181 gfx::Size size(image_->GetPreferredSize()); | 205 gfx::Size size(image_->GetPreferredSize()); |
| 182 if (ShouldShowBackground()) { | 206 bool shrinking = IsShrinking(); |
| 183 const int non_label_width = GetBubbleOuterPadding(true) + | 207 // Animation continues for the last few pixels even after the label is not |
| 184 GetImageAndPaddingWidth() + | 208 // visible in order to slide the icon into its final position. Therefore it |
| 185 GetBubbleOuterPadding(false); | 209 // is necessary to animate |total_width| even when the background is hidden |
| 186 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); | 210 // as long as the animation is still shrinking. |
| 187 if (!ui::MaterialDesignController::IsModeMaterial()) | 211 if (ShouldShowBackground() || shrinking) { |
| 188 size.SetToMax(background_painter_->GetMinimumSize()); | 212 const int image_width = size.width(); |
| 213 const int leading_padding = GetBubbleOuterPadding(true); | |
| 214 const int remaining_padding = | |
| 215 GetBubbleOuterPadding(false) + | |
| 216 GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING); | |
|
Peter Kasting
2016/03/11 01:14:42
Nit: Given the code below, you could now collapse
varkha
2016/03/11 03:08:40
Done.
| |
| 217 // |multiplier| grows from zero to one, stays equal to one and then shrinks | |
| 218 // to zero again. This view width should grow from zero to its maximum | |
| 219 // width, then stay at its maximum width and then shrink to preferred | |
| 220 // image width. | |
|
Peter Kasting
2016/03/11 01:14:42
Nit: How about:
The view width should correspondi
varkha
2016/03/11 03:08:40
Done.
| |
| 221 int total_width = WidthMultiplier() * | |
| 222 (label_width + leading_padding + image_width + remaining_padding); | |
| 223 if (shrinking) | |
| 224 total_width = std::max(total_width, image_width); | |
| 225 size.set_width(total_width); | |
| 189 } | 226 } |
| 227 return size; | |
| 228 } | |
| 190 | 229 |
| 191 return size; | 230 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const { |
| 231 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 232 return GetBubbleOuterPaddingMd(leading); | |
| 233 | |
| 234 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | |
| 235 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | |
| 236 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | |
| 192 } | 237 } |
| 193 | 238 |
| 194 void IconLabelBubbleView::SetLabelBackgroundColor( | 239 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 195 SkColor chip_background_color) { | 240 SkColor chip_background_color) { |
| 196 // The background images are painted atop |parent_background_color_|. | 241 // The background images are painted atop |parent_background_color_|. |
| 197 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 242 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 198 // determine the actual color the label text will sit atop. | 243 // determine the actual color the label text will sit atop. |
| 199 // Tricky bit: We alpha blend an opaque version of |chip_background_color| | 244 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
| 200 // against |parent_background_color_| using the original image grid color's | 245 // against |parent_background_color_| using the original image grid color's |
| 201 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 246 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 202 // even if |a| is a color with non-255 alpha. | 247 // even if |a| is a color with non-255 alpha. |
| 203 label_->SetBackgroundColor(color_utils::AlphaBlend( | 248 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 204 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 249 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 205 SkColorGetA(chip_background_color))); | 250 SkColorGetA(chip_background_color))); |
| 206 } | 251 } |
| 207 | 252 |
| 208 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const { | |
| 209 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 210 return GetBubbleOuterPaddingMd(leading); | |
| 211 | |
| 212 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | |
| 213 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | |
| 214 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | |
| 215 } | |
| 216 | |
| 217 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { | 253 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { |
| 218 // When the image is empty, leading and trailing padding are equal. | 254 // When the image is empty, leading and trailing padding are equal. |
| 219 if (image_->GetPreferredSize().IsEmpty() || !leading) | 255 if (image_->GetPreferredSize().IsEmpty() || !leading) |
| 220 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 256 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
| 221 | 257 |
| 222 // Leading padding is 2dp. | 258 // Leading padding is 2dp. |
| 223 return 2; | 259 return 2; |
| 224 } | 260 } |
| 225 | 261 |
| 226 const char* IconLabelBubbleView::GetClassName() const { | 262 const char* IconLabelBubbleView::GetClassName() const { |
| 227 return "IconLabelBubbleView"; | 263 return "IconLabelBubbleView"; |
| 228 } | 264 } |
| 229 | 265 |
| 230 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 266 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 231 if (!ShouldShowBackground()) | 267 if (!ShouldShowBackground()) |
| 232 return; | 268 return; |
| 233 if (background_painter_) { | 269 if (background_painter_) { |
| 234 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 270 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 235 GetContentsBounds()); | 271 GetContentsBounds()); |
| 236 } | 272 } |
| 237 if (background()) | 273 if (background()) |
| 238 background()->Paint(canvas, this); | 274 background()->Paint(canvas, this); |
| 239 } | 275 } |
| OLD | NEW |