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 void IconLabelBubbleView::StopAnimation() {} | |
| 109 | |
| 110 bool IconLabelBubbleView::IsShrinking() const { | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 108 int IconLabelBubbleView::GetImageAndPaddingWidth() const { | 114 int IconLabelBubbleView::GetImageAndPaddingWidth() const { |
| 109 const int image_width = image_->GetPreferredSize().width(); | 115 const int image_width = image_->GetPreferredSize().width(); |
| 110 return image_width | 116 return image_width |
| 111 ? image_width + GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) | 117 ? image_width + GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING) |
| 112 : 0; | 118 : 0; |
| 113 } | 119 } |
| 114 | 120 |
| 115 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 121 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 116 // Height will be ignored by the LocationBarView. | 122 // Height will be ignored by the LocationBarView. |
| 117 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 123 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 118 } | 124 } |
| 119 | 125 |
| 120 void IconLabelBubbleView::Layout() { | 126 void IconLabelBubbleView::Layout() { |
| 121 // In MD mode, both extension icons and Chrome-provided icons are 16px, | 127 // 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 | 128 // so it's not necessary to handle them differently. TODO(estade): clean |
| 123 // this up when MD is on by default. | 129 // this up when MD is on by default. |
| 124 bool icon_has_enough_padding = | 130 bool icon_has_enough_padding = |
| 125 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); | 131 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); |
| 126 const int image_width = image_->GetPreferredSize().width(); | 132 const int image_min_width = image_->GetPreferredSize().width(); |
|
Peter Kasting
2016/03/07 20:24:27
Nit: |image_min_width| seems misleading here since
varkha
2016/03/08 18:01:03
Done. Renamed to |image_preferred_width|.
| |
| 127 image_->SetBounds(std::min((width() - image_width) / 2, | 133 // image is positioned at a constant offset when a label is visible or when |
| 128 GetBubbleOuterPadding(icon_has_enough_padding)), | 134 // there is enough room to fit the whole image. This is done so that the image |
| 129 0, image_->GetPreferredSize().width(), height()); | 135 // slides in as a part of the sliding surface and doesn't shift relative to |
| 136 // the background. | |
| 137 const int image_x = (label_->visible() || width() > image_min_width) | |
| 138 ? GetBubbleOuterPadding(icon_has_enough_padding) | |
| 139 : (width() - image_min_width) / 2; | |
| 140 const int image_padding = | |
| 141 label_->visible() ? GetBubbleOuterPadding(false) : 0; | |
| 142 const int image_width = | |
| 143 std::min(width() - image_x - image_padding, image_min_width); | |
| 144 image_->SetBounds(image_x, 0, image_width, height()); | |
| 130 | 145 |
| 131 int pre_label_width = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); | 146 int label_x = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); |
| 132 label_->SetBounds(pre_label_width, 0, | 147 const int label_width = |
| 133 width() - pre_label_width - GetBubbleOuterPadding(false), | 148 std::max(0, width() - label_x - GetBubbleOuterPadding(false)); |
| 134 height()); | 149 |
| 150 // Hiding the label when it no longer fits and when the leading edge of the | |
| 151 // image plus padding crosses the right edge of the bubble allows the final | |
| 152 // frames of the animation to smoothly transition from showing a bubble to | |
| 153 // showing just the image. | |
| 154 if (IsShrinking() && label_->visible() && label_width == 0 && | |
| 155 (image_x + image_padding + image_min_width >= bounds().width())) { | |
| 156 label_->SetVisible(false); | |
| 157 } | |
| 158 // Animation is finished when the image is in its target position which is | |
| 159 // the position with no padding. | |
| 160 if (IsShrinking() && (image_x + image_min_width >= bounds().width())) | |
| 161 StopAnimation(); | |
|
Peter Kasting
2016/03/07 20:24:27
I don't think this is the right way to do this.
I
varkha
2016/03/08 18:01:03
I have put the correction in IconLabelBubbleView::
| |
| 162 | |
| 163 label_->SetBounds(label_x, 0, label_width, height()); | |
| 135 } | 164 } |
| 136 | 165 |
| 137 void IconLabelBubbleView::OnNativeThemeChanged( | 166 void IconLabelBubbleView::OnNativeThemeChanged( |
| 138 const ui::NativeTheme* native_theme) { | 167 const ui::NativeTheme* native_theme) { |
| 139 label_->SetEnabledColor(GetTextColor()); | 168 label_->SetEnabledColor(GetTextColor()); |
| 140 | 169 |
| 141 if (!ui::MaterialDesignController::IsModeMaterial()) | 170 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 142 return; | 171 return; |
| 143 | 172 |
| 144 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); | 173 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 172 | 201 |
| 173 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 202 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 174 return ui::MaterialDesignController::IsModeMaterial() | 203 return ui::MaterialDesignController::IsModeMaterial() |
| 175 ? GetNativeTheme()->GetSystemColor( | 204 ? GetNativeTheme()->GetSystemColor( |
| 176 ui::NativeTheme::kColorId_TextfieldDefaultBackground) | 205 ui::NativeTheme::kColorId_TextfieldDefaultBackground) |
| 177 : parent_background_color_; | 206 : parent_background_color_; |
| 178 } | 207 } |
| 179 | 208 |
| 180 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { | 209 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { |
| 181 gfx::Size size(image_->GetPreferredSize()); | 210 gfx::Size size(image_->GetPreferredSize()); |
| 182 if (ShouldShowBackground()) { | 211 if (ShouldShowBackground() || IsShrinking()) { |
|
Peter Kasting
2016/03/07 20:24:27
Won't ShouldShowBackground() always be true if we
varkha
2016/03/08 18:01:03
There is also label()->visible() condition (in the
| |
| 183 const int non_label_width = GetBubbleOuterPadding(true) + | 212 const int non_label_width = GetBubbleOuterPadding(true) + |
| 184 GetImageAndPaddingWidth() + | 213 GetImageAndPaddingWidth() + |
| 185 GetBubbleOuterPadding(false); | 214 GetBubbleOuterPadding(false); |
| 186 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); | 215 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); |
| 187 if (!ui::MaterialDesignController::IsModeMaterial()) | |
| 188 size.SetToMax(background_painter_->GetMinimumSize()); | |
| 189 } | 216 } |
| 190 | 217 |
| 191 return size; | 218 return size; |
| 192 } | 219 } |
| 193 | 220 |
| 221 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const { | |
| 222 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 223 return GetBubbleOuterPaddingMd(leading); | |
|
Peter Kasting
2016/03/07 20:24:27
Nit: We might as well just scrap GetBubbleOuterPad
varkha
2016/03/08 18:01:03
I think keeping it separate makes it a bit easier
| |
| 224 | |
| 225 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | |
| 226 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | |
| 227 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | |
| 228 } | |
| 229 | |
| 194 void IconLabelBubbleView::SetLabelBackgroundColor( | 230 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 195 SkColor chip_background_color) { | 231 SkColor chip_background_color) { |
| 196 // The background images are painted atop |parent_background_color_|. | 232 // The background images are painted atop |parent_background_color_|. |
| 197 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 233 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 198 // determine the actual color the label text will sit atop. | 234 // determine the actual color the label text will sit atop. |
| 199 // Tricky bit: We alpha blend an opaque version of |chip_background_color| | 235 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
| 200 // against |parent_background_color_| using the original image grid color's | 236 // against |parent_background_color_| using the original image grid color's |
| 201 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 237 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 202 // even if |a| is a color with non-255 alpha. | 238 // even if |a| is a color with non-255 alpha. |
| 203 label_->SetBackgroundColor(color_utils::AlphaBlend( | 239 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 204 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 240 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 205 SkColorGetA(chip_background_color))); | 241 SkColorGetA(chip_background_color))); |
| 206 } | 242 } |
| 207 | 243 |
| 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 { | 244 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { |
| 218 // When the image is empty, leading and trailing padding are equal. | 245 // When the image is empty, leading and trailing padding are equal. |
| 219 if (image_->GetPreferredSize().IsEmpty() || !leading) | 246 if (image_->GetPreferredSize().IsEmpty() || !leading) |
| 220 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 247 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
| 221 | 248 |
| 222 // Leading padding is 2dp. | 249 // Leading padding is 2dp. |
| 223 return 2; | 250 return 2; |
| 224 } | 251 } |
| 225 | 252 |
| 226 const char* IconLabelBubbleView::GetClassName() const { | 253 const char* IconLabelBubbleView::GetClassName() const { |
| 227 return "IconLabelBubbleView"; | 254 return "IconLabelBubbleView"; |
| 228 } | 255 } |
| 229 | 256 |
| 230 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 257 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 231 if (!ShouldShowBackground()) | 258 if (!ShouldShowBackground()) |
| 232 return; | 259 return; |
| 233 if (background_painter_) { | 260 if (background_painter_) { |
| 234 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 261 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 235 GetContentsBounds()); | 262 GetContentsBounds()); |
| 236 } | 263 } |
| 237 if (background()) | 264 if (background()) |
| 238 background()->Paint(canvas, this); | 265 background()->Paint(canvas, this); |
| 239 } | 266 } |
| OLD | NEW |