Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: chrome/browser/ui/views/location_bar/icon_label_bubble_view.cc

Issue 1763713004: Adjusts content bubble animation with respect to icon size (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adjusts content bubble animation with respect to icon size (review comments) Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 const int image_x = (label_->visible() || width() > image_preferred_width)
136 ? GetBubbleOuterPadding(icon_has_enough_padding)
Peter Kasting 2016/03/08 22:07:06 Can we just always do this? Is it ever actually n
varkha 2016/03/10 22:25:56 Done. Still need to make sure the image is right a
Peter Kasting 2016/03/11 01:14:42 How can we overshoot given the current definition
varkha 2016/03/11 03:08:40 I meant this as an explanation of what I have trie
137 : (width() - image_preferred_width) / 2;
138 const int image_padding =
139 label_->visible() ? GetBubbleOuterPadding(false) : 0;
140 const int image_width =
141 std::min(width() - image_x - image_padding, image_preferred_width);
142 image_->SetBounds(image_x, 0, image_width, height());
130 143
131 int pre_label_width = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth(); 144 int label_x = GetBubbleOuterPadding(true) + GetImageAndPaddingWidth();
132 label_->SetBounds(pre_label_width, 0, 145 const int label_width =
133 width() - pre_label_width - GetBubbleOuterPadding(false), 146 std::max(0, width() - label_x - GetBubbleOuterPadding(false));
134 height()); 147
148 // Hiding the label when it no longer fits and when the leading edge of the
149 // image plus padding crosses the right edge of the bubble allows the final
150 // frames of the animation to smoothly transition from showing a bubble to
151 // showing just the image.
152 if (IsShrinking() && label_->visible() && label_width == 0 &&
153 (image_x + image_padding + image_preferred_width >= bounds().width())) {
Peter Kasting 2016/03/08 22:07:06 Why is the last conditional clause here necessary?
varkha 2016/03/10 22:25:56 I wanted to hide the boundary only when the image
Peter Kasting 2016/03/11 01:14:42 That sounds like the (label_width == 0) check is u
varkha 2016/03/11 03:08:40 Done.
154 label_->SetVisible(false);
155 }
156
157 label_->SetBounds(label_x, 0, label_width, height());
135 } 158 }
136 159
137 void IconLabelBubbleView::OnNativeThemeChanged( 160 void IconLabelBubbleView::OnNativeThemeChanged(
138 const ui::NativeTheme* native_theme) { 161 const ui::NativeTheme* native_theme) {
139 label_->SetEnabledColor(GetTextColor()); 162 label_->SetEnabledColor(GetTextColor());
140 163
141 if (!ui::MaterialDesignController::IsModeMaterial()) 164 if (!ui::MaterialDesignController::IsModeMaterial())
142 return; 165 return;
143 166
144 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); 167 bool inverted = color_utils::IsDark(GetParentBackgroundColor());
(...skipping 27 matching lines...) Expand all
172 195
173 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { 196 SkColor IconLabelBubbleView::GetParentBackgroundColor() const {
174 return ui::MaterialDesignController::IsModeMaterial() 197 return ui::MaterialDesignController::IsModeMaterial()
175 ? GetNativeTheme()->GetSystemColor( 198 ? GetNativeTheme()->GetSystemColor(
176 ui::NativeTheme::kColorId_TextfieldDefaultBackground) 199 ui::NativeTheme::kColorId_TextfieldDefaultBackground)
177 : parent_background_color_; 200 : parent_background_color_;
178 } 201 }
179 202
180 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { 203 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const {
181 gfx::Size size(image_->GetPreferredSize()); 204 gfx::Size size(image_->GetPreferredSize());
182 if (ShouldShowBackground()) { 205 bool shrinking = IsShrinking();
183 const int non_label_width = GetBubbleOuterPadding(true) + 206 if (ShouldShowBackground() || shrinking) {
Peter Kasting 2016/03/08 22:07:06 Add comment on why it's necessary to check for |sh
varkha 2016/03/10 22:25:57 Done.
184 GetImageAndPaddingWidth() + 207 const int leading_padding = GetBubbleOuterPadding(true);
185 GetBubbleOuterPadding(false); 208 const int padding_rest =
Peter Kasting 2016/03/08 22:07:06 Nit: |remaining_padding| seems better ("rest" soun
varkha 2016/03/10 22:25:57 Done.
186 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); 209 GetBubbleOuterPadding(false) +
187 if (!ui::MaterialDesignController::IsModeMaterial()) 210 GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_PADDING);
188 size.SetToMax(background_painter_->GetMinimumSize()); 211 const int image_width = size.width();
212 const double multiplier = WidthMultiplier();
213 // |multiplier| grows from zero to one, stays equal to one and then shrinks
214 // to zero again. This view width should grow from zero to its maximum
215 // width, then stay at its maximum width and then shrink to such width that
216 // has enough room for the image and its leading padding.
217 const int total_width = shrinking
218 ? multiplier * (width + padding_rest) + leading_padding + image_width
219 : multiplier * (width + padding_rest + leading_padding + image_width);
Peter Kasting 2016/03/08 22:07:06 This means the view will shrink more slowly (in te
varkha 2016/03/10 22:25:57 Sure. That makes the animation continue near the e
220 size.set_width(total_width);
189 } 221 }
222 return size;
223 }
190 224
191 return size; 225 int IconLabelBubbleView::GetBubbleOuterPadding(bool leading) const {
226 if (ui::MaterialDesignController::IsModeMaterial())
227 return GetBubbleOuterPaddingMd(leading);
228
229 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) -
230 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) +
231 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING));
192 } 232 }
193 233
194 void IconLabelBubbleView::SetLabelBackgroundColor( 234 void IconLabelBubbleView::SetLabelBackgroundColor(
195 SkColor chip_background_color) { 235 SkColor chip_background_color) {
196 // The background images are painted atop |parent_background_color_|. 236 // The background images are painted atop |parent_background_color_|.
197 // Alpha-blend |chip_background_color| with |parent_background_color_| to 237 // Alpha-blend |chip_background_color| with |parent_background_color_| to
198 // determine the actual color the label text will sit atop. 238 // determine the actual color the label text will sit atop.
199 // Tricky bit: We alpha blend an opaque version of |chip_background_color| 239 // Tricky bit: We alpha blend an opaque version of |chip_background_color|
200 // against |parent_background_color_| using the original image grid color's 240 // against |parent_background_color_| using the original image grid color's
201 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged 241 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged
202 // even if |a| is a color with non-255 alpha. 242 // even if |a| is a color with non-255 alpha.
203 label_->SetBackgroundColor(color_utils::AlphaBlend( 243 label_->SetBackgroundColor(color_utils::AlphaBlend(
204 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), 244 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(),
205 SkColorGetA(chip_background_color))); 245 SkColorGetA(chip_background_color)));
206 } 246 }
207 247
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 { 248 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const {
218 // When the image is empty, leading and trailing padding are equal. 249 // When the image is empty, leading and trailing padding are equal.
219 if (image_->GetPreferredSize().IsEmpty() || !leading) 250 if (image_->GetPreferredSize().IsEmpty() || !leading)
220 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); 251 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING);
221 252
222 // Leading padding is 2dp. 253 // Leading padding is 2dp.
223 return 2; 254 return 2;
224 } 255 }
225 256
226 const char* IconLabelBubbleView::GetClassName() const { 257 const char* IconLabelBubbleView::GetClassName() const {
227 return "IconLabelBubbleView"; 258 return "IconLabelBubbleView";
228 } 259 }
229 260
230 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { 261 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) {
231 if (!ShouldShowBackground()) 262 if (!ShouldShowBackground())
232 return; 263 return;
233 if (background_painter_) { 264 if (background_painter_) {
234 views::Painter::PaintPainterAt(canvas, background_painter_.get(), 265 views::Painter::PaintPainterAt(canvas, background_painter_.get(),
235 GetContentsBounds()); 266 GetContentsBounds());
236 } 267 }
237 if (background()) 268 if (background())
238 background()->Paint(canvas, this); 269 background()->Paint(canvas, this);
239 } 270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698