| 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 // Compute the label bounds. The label gets whatever size is left over after |
| 122 // so it's not necessary to handle them differently. TODO(estade): clean | 126 // accounting for the preferred image width and padding amounts. Note that if |
| 123 // this up when MD is on by default. | 127 // the label has zero size it doesn't actually matter what we compute its X |
| 124 bool icon_has_enough_padding = | 128 // 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. |
| 130 // 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 |
| 132 // happened, since the value of ShouldShowBackground() we read below may |
| 133 // depend on whether the label has nonzero size. Therefore, we do this first. |
| 134 const int label_x = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); |
| 135 const int label_width = |
| 136 std::max(0, width() - label_x - GetBubbleOuterPadding(false)); |
| 137 label_->SetBounds(label_x, 0, label_width, height()); |
| 138 |
| 139 // Now compute the image bounds. In non-MD, the leading padding depends on |
| 140 // whether this is an extension icon, since extension icons and |
| 141 // 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. |
| 143 const bool icon_has_enough_padding = |
| 125 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); | 144 !is_extension_icon_ || ui::MaterialDesignController::IsModeMaterial(); |
| 126 const int image_width = image_->GetPreferredSize().width(); | 145 int image_x = GetBubbleOuterPadding(icon_has_enough_padding); |
| 127 image_->SetBounds(std::min((width() - image_width) / 2, | 146 const int image_preferred_width = image_->GetPreferredSize().width(); |
| 128 GetBubbleOuterPadding(icon_has_enough_padding)), | 147 int bubble_trailing_padding = GetBubbleOuterPadding(false); |
| 129 0, image_->GetPreferredSize().width(), height()); | |
| 130 | 148 |
| 131 int pre_label_width = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); | 149 // If ShouldShowBackground() is true, then either we show a background in the |
| 132 label_->SetBounds(pre_label_width, 0, | 150 // steady state, or we're not yet in the last portion of the animation. In |
| 133 width() - pre_label_width - GetBubbleOuterPadding(false), | 151 // these cases, we leave the leading and trailing padding alone; we don't want |
| 134 height()); | 152 // 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 |
| 154 // 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, |
| 156 // 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. |
| 158 if (!ShouldShowBackground()) { |
| 159 image_x = std::min(image_x, width() - image_preferred_width); |
| 160 bubble_trailing_padding = std::min( |
| 161 bubble_trailing_padding, width() - image_preferred_width - image_x); |
| 162 } |
| 163 |
| 164 // 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 |
| 166 // the first portion of the animation; during the very beginning there may not |
| 167 // be enough room to show the image at all. |
| 168 const int image_width = |
| 169 std::min(image_preferred_width, |
| 170 std::max(0, width() - image_x - bubble_trailing_padding)); |
| 171 image_->SetBounds(image_x, 0, image_width, height()); |
| 135 } | 172 } |
| 136 | 173 |
| 137 void IconLabelBubbleView::OnNativeThemeChanged( | 174 void IconLabelBubbleView::OnNativeThemeChanged( |
| 138 const ui::NativeTheme* native_theme) { | 175 const ui::NativeTheme* native_theme) { |
| 139 label_->SetEnabledColor(GetTextColor()); | 176 label_->SetEnabledColor(GetTextColor()); |
| 140 | 177 |
| 141 if (!ui::MaterialDesignController::IsModeMaterial()) | 178 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 142 return; | 179 return; |
| 143 | 180 |
| 144 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); | 181 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 170 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 207 return color_utils::DeriveDefaultIconColor(GetTextColor()); |
| 171 } | 208 } |
| 172 | 209 |
| 173 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 210 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 174 return ui::MaterialDesignController::IsModeMaterial() | 211 return ui::MaterialDesignController::IsModeMaterial() |
| 175 ? GetNativeTheme()->GetSystemColor( | 212 ? GetNativeTheme()->GetSystemColor( |
| 176 ui::NativeTheme::kColorId_TextfieldDefaultBackground) | 213 ui::NativeTheme::kColorId_TextfieldDefaultBackground) |
| 177 : parent_background_color_; | 214 : parent_background_color_; |
| 178 } | 215 } |
| 179 | 216 |
| 180 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { | 217 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 181 gfx::Size size(image_->GetPreferredSize()); | 218 gfx::Size size(image_->GetPreferredSize()); |
| 182 if (ShouldShowBackground()) { | 219 bool shrinking = IsShrinking(); |
| 183 const int non_label_width = GetBubbleOuterPadding(true) + | 220 // Animation continues for the last few pixels even after the label is not |
| 184 GetImageAndPaddingWidth() + | 221 // visible in order to slide the icon into its final position. Therefore it |
| 185 GetBubbleOuterPadding(false); | 222 // is necessary to animate |total_width| even when the background is hidden |
| 186 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); | 223 // as long as the animation is still shrinking. |
| 187 if (!ui::MaterialDesignController::IsModeMaterial()) | 224 if (ShouldShowBackground() || shrinking) { |
| 188 size.SetToMax(background_painter_->GetMinimumSize()); | 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 |
| 230 // 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 |
| 232 // 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 |
| 234 // pop back to an icon after the animation finishes. |
| 235 int total_width = WidthMultiplier() * (label_width + image_width + padding); |
| 236 if (shrinking) |
| 237 total_width = std::max(total_width, image_width); |
| 238 size.set_width(total_width); |
| 189 } | 239 } |
| 240 return size; |
| 241 } |
| 190 | 242 |
| 191 return size; | 243 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const { |
| 244 if (ui::MaterialDesignController::IsModeMaterial()) |
| 245 return GetBubbleOuterPaddingMd(leading); |
| 246 |
| 247 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
| 248 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
| 249 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); |
| 192 } | 250 } |
| 193 | 251 |
| 194 void IconLabelBubbleView::SetLabelBackgroundColor( | 252 void IconLabelBubbleView::SetLabelBackgroundColor( |
| 195 SkColor chip_background_color) { | 253 SkColor chip_background_color) { |
| 196 // The background images are painted atop |parent_background_color_|. | 254 // The background images are painted atop |parent_background_color_|. |
| 197 // Alpha-blend |chip_background_color| with |parent_background_color_| to | 255 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
| 198 // determine the actual color the label text will sit atop. | 256 // determine the actual color the label text will sit atop. |
| 199 // Tricky bit: We alpha blend an opaque version of |chip_background_color| | 257 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
| 200 // against |parent_background_color_| using the original image grid color's | 258 // against |parent_background_color_| using the original image grid color's |
| 201 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 259 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
| 202 // even if |a| is a color with non-255 alpha. | 260 // even if |a| is a color with non-255 alpha. |
| 203 label_->SetBackgroundColor(color_utils::AlphaBlend( | 261 label_->SetBackgroundColor(color_utils::AlphaBlend( |
| 204 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 262 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
| 205 SkColorGetA(chip_background_color))); | 263 SkColorGetA(chip_background_color))); |
| 206 } | 264 } |
| 207 | 265 |
| 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 { | 266 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { |
| 218 // When the image is empty, leading and trailing padding are equal. | 267 // When the image is empty, leading and trailing padding are equal. |
| 219 if (image_->GetPreferredSize().IsEmpty() || !leading) | 268 if (image_->GetPreferredSize().IsEmpty() || !leading) |
| 220 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 269 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
| 221 | 270 |
| 222 // Leading padding is 2dp. | 271 // Leading padding is 2dp. |
| 223 return 2; | 272 return 2; |
| 224 } | 273 } |
| 225 | 274 |
| 226 const char* IconLabelBubbleView::GetClassName() const { | 275 const char* IconLabelBubbleView::GetClassName() const { |
| 227 return "IconLabelBubbleView"; | 276 return "IconLabelBubbleView"; |
| 228 } | 277 } |
| 229 | 278 |
| 230 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 279 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 231 if (!ShouldShowBackground()) | 280 if (!ShouldShowBackground()) |
| 232 return; | 281 return; |
| 233 if (background_painter_) { | 282 if (background_painter_) { |
| 234 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 283 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
| 235 GetContentsBounds()); | 284 GetContentsBounds()); |
| 236 } | 285 } |
| 237 if (background()) | 286 if (background()) |
| 238 background()->Paint(canvas, this); | 287 background()->Paint(canvas, this); |
| 239 } | 288 } |
| OLD | NEW |