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

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

Issue 1998493002: Update omnibox chips in MD (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove extra include Created 4 years, 7 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"
11 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/color_utils.h" 13 #include "ui/gfx/color_utils.h"
14 #include "ui/gfx/image/image_util.h" 14 #include "ui/gfx/image/image_util.h"
15 #include "ui/gfx/scoped_canvas.h"
15 #include "ui/native_theme/native_theme.h" 16 #include "ui/native_theme/native_theme.h"
16 #include "ui/views/animation/ink_drop_hover.h" 17 #include "ui/views/animation/ink_drop_hover.h"
17 #include "ui/views/border.h" 18 #include "ui/views/border.h"
18 #include "ui/views/controls/image_view.h" 19 #include "ui/views/controls/image_view.h"
20 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/painter.h" 21 #include "ui/views/painter.h"
20 22
21 namespace { 23 namespace {
22 24
25 // Extra padding to place at the end of the chip when the label is showing.
26 const int kExtraTrailingPadding = 7;
27
23 SkColor CalculateImageColor(gfx::ImageSkia* image) { 28 SkColor CalculateImageColor(gfx::ImageSkia* image) {
24 // We grab the color of the middle pixel of the image, which we treat as 29 // We grab the color of the middle pixel of the image, which we treat as
25 // the representative color of the entire image (reasonable, given the current 30 // the representative color of the entire image (reasonable, given the current
26 // appearance of these assets). 31 // appearance of these assets).
27 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); 32 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap());
28 SkAutoLockPixels pixel_lock(bitmap); 33 SkAutoLockPixels pixel_lock(bitmap);
29 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); 34 return bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2);
30 } 35 }
31 36
32 } // namespace 37 } // namespace
33 38
34 IconLabelBubbleView::IconLabelBubbleView(int contained_image, 39 IconLabelBubbleView::IconLabelBubbleView(int contained_image,
35 const gfx::FontList& font_list, 40 const gfx::FontList& font_list,
36 SkColor parent_background_color, 41 SkColor parent_background_color,
37 bool elide_in_middle) 42 bool elide_in_middle)
38 : background_painter_(nullptr), 43 : background_painter_(nullptr),
39 image_(new views::ImageView()), 44 image_(new views::ImageView()),
40 label_(new views::Label(base::string16(), font_list)), 45 label_(new views::Label(base::string16(), font_list)),
41 builtin_leading_padding_(0),
42 builtin_trailing_padding_(0),
43 is_extension_icon_(false), 46 is_extension_icon_(false),
44 parent_background_color_(parent_background_color) { 47 parent_background_color_(parent_background_color) {
45 if (contained_image) { 48 if (contained_image) {
46 image_->SetImage( 49 image_->SetImage(
47 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 50 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
48 contained_image)); 51 contained_image));
49 } 52 }
50 53
51 // Disable separate hit testing for |image_|. This prevents views treating 54 // Disable separate hit testing for |image_|. This prevents views treating
52 // |image_| as a separate mouse hover region from |this|. 55 // |image_| as a separate mouse hover region from |this|.
53 image_->set_interactive(false); 56 image_->set_interactive(false);
54 AddChildView(image_); 57 AddChildView(image_);
55 58
56 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 59 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
57 60
58 if (elide_in_middle) 61 if (elide_in_middle)
59 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); 62 label_->SetElideBehavior(gfx::ELIDE_MIDDLE);
60 AddChildView(label_); 63 AddChildView(label_);
61 64
62 // Bubbles are given the full internal height of the location bar so that all 65 // Bubbles are given the full internal height of the location bar so that all
63 // child views in the location bar have the same height. The visible height of 66 // child views in the location bar have the same height. The visible height of
64 // the bubble should be smaller, so use an empty border to shrink down the 67 // the bubble should be smaller, so use an empty border to shrink down the
65 // content bounds so the background gets painted correctly. 68 // content bounds so the background gets painted correctly.
66 const int padding = GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING); 69 SetBorder(views::Border::CreateEmptyBorder(
67 SetBorder( 70 gfx::Insets(GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING), 0)));
68 views::Border::CreateEmptyBorder(gfx::Insets(padding, 0, padding, 0))); 71
72 // Flip the canvas in MD RTL so the separator is drawn on the correct side.
73 EnableCanvasFlippingForRTLUI(ui::MaterialDesignController::IsModeMaterial());
69 } 74 }
70 75
71 IconLabelBubbleView::~IconLabelBubbleView() { 76 IconLabelBubbleView::~IconLabelBubbleView() {
72 } 77 }
73 78
74 void IconLabelBubbleView::SetBackgroundImageGrid( 79 void IconLabelBubbleView::SetBackgroundImageGrid(
75 const int background_images[]) { 80 const int background_images[]) {
76 should_show_background_ = true; 81 should_show_background_ = true;
77 if (!ui::MaterialDesignController::IsModeMaterial()) { 82 if (!ui::MaterialDesignController::IsModeMaterial()) {
78 background_painter_.reset( 83 background_painter_.reset(
(...skipping 12 matching lines...) Expand all
91 should_show_background_ = false; 96 should_show_background_ = false;
92 SetLabelBackgroundColor(SK_ColorTRANSPARENT); 97 SetLabelBackgroundColor(SK_ColorTRANSPARENT);
93 } 98 }
94 99
95 void IconLabelBubbleView::SetLabel(const base::string16& label) { 100 void IconLabelBubbleView::SetLabel(const base::string16& label) {
96 label_->SetText(label); 101 label_->SetText(label);
97 } 102 }
98 103
99 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { 104 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) {
100 image_->SetImage(image_skia); 105 image_->SetImage(image_skia);
101
102 if (ui::MaterialDesignController::IsModeMaterial()) {
103 gfx::GetVisibleMargins(image_skia, &builtin_leading_padding_,
104 &builtin_trailing_padding_);
105 if (base::i18n::IsRTL())
106 std::swap(builtin_leading_padding_, builtin_trailing_padding_);
107 }
108 } 106 }
109 107
110 bool IconLabelBubbleView::ShouldShowBackground() const { 108 bool IconLabelBubbleView::ShouldShowBackground() const {
111 return should_show_background_; 109 return should_show_background_;
112 } 110 }
113 111
114 double IconLabelBubbleView::WidthMultiplier() const { 112 double IconLabelBubbleView::WidthMultiplier() const {
115 return 1.0; 113 return 1.0;
116 } 114 }
117 115
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 const int label_x = image_x + image_width + GetInternalSpacing(); 180 const int label_x = image_x + image_width + GetInternalSpacing();
183 const int label_width = 181 const int label_width =
184 std::max(0, width() - label_x - bubble_trailing_padding); 182 std::max(0, width() - label_x - bubble_trailing_padding);
185 label_->SetBounds(label_x, 0, label_width, height()); 183 label_->SetBounds(label_x, 0, label_width, height());
186 } 184 }
187 185
188 void IconLabelBubbleView::OnNativeThemeChanged( 186 void IconLabelBubbleView::OnNativeThemeChanged(
189 const ui::NativeTheme* native_theme) { 187 const ui::NativeTheme* native_theme) {
190 label_->SetEnabledColor(GetTextColor()); 188 label_->SetEnabledColor(GetTextColor());
191 189
192 if (!ui::MaterialDesignController::IsModeMaterial()) 190 if (ui::MaterialDesignController::IsModeMaterial()) {
193 return; 191 label_->SetBackgroundColor(GetParentBackgroundColor());
194 192 SchedulePaint();
195 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); 193 }
196 SkColor border_color = inverted ? SK_ColorWHITE : GetBorderColor();
197 SkColor background_color =
198 inverted ? SK_ColorWHITE : SkColorSetA(border_color, 0x13);
199 set_background(new BackgroundWith1PxBorder(background_color, border_color));
200 SetLabelBackgroundColor(background_color);
201 } 194 }
202 195
203 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { 196 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) {
204 image()->SetPaintToLayer(true); 197 image()->SetPaintToLayer(true);
205 image()->layer()->SetFillsBoundsOpaquely(false); 198 image()->layer()->SetFillsBoundsOpaquely(false);
206 InkDropHostView::AddInkDropLayer(ink_drop_layer); 199 InkDropHostView::AddInkDropLayer(ink_drop_layer);
207 } 200 }
208 201
209 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { 202 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
210 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); 203 InkDropHostView::RemoveInkDropLayer(ink_drop_layer);
(...skipping 18 matching lines...) Expand all
229 } 222 }
230 223
231 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { 224 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const {
232 gfx::Size size(image_->GetPreferredSize()); 225 gfx::Size size(image_->GetPreferredSize());
233 const bool shrinking = IsShrinking(); 226 const bool shrinking = IsShrinking();
234 // Animation continues for the last few pixels even after the label is not 227 // Animation continues for the last few pixels even after the label is not
235 // visible in order to slide the icon into its final position. Therefore it 228 // visible in order to slide the icon into its final position. Therefore it
236 // is necessary to animate |total_width| even when the background is hidden 229 // is necessary to animate |total_width| even when the background is hidden
237 // as long as the animation is still shrinking. 230 // as long as the animation is still shrinking.
238 if (ShouldShowBackground() || shrinking) { 231 if (ShouldShowBackground() || shrinking) {
232 const int extra_trailing_padding =
233 ui::MaterialDesignController::IsModeMaterial() ? kExtraTrailingPadding
234 : 0;
239 // |multiplier| grows from zero to one, stays equal to one and then shrinks 235 // |multiplier| grows from zero to one, stays equal to one and then shrinks
240 // to zero again. The view width should correspondingly grow from zero to 236 // to zero again. The view width should correspondingly grow from zero to
241 // fully showing both label and icon, stay there, then shrink to just large 237 // fully showing both label and icon, stay there, then shrink to just large
242 // enough to show the icon. We don't want to shrink all the way back to 238 // enough to show the icon. We don't want to shrink all the way back to
243 // zero, since this would mean the view would completely disappear and then 239 // zero, since this would mean the view would completely disappear and then
244 // pop back to an icon after the animation finishes. 240 // pop back to an icon after the animation finishes.
245 const int max_width = MinimumWidthForImageWithBackgroundShown() + 241 const int max_width = MinimumWidthForImageWithBackgroundShown() +
246 GetInternalSpacing() + label_width; 242 GetInternalSpacing() + label_width +
243 extra_trailing_padding;
247 const int current_width = WidthMultiplier() * max_width; 244 const int current_width = WidthMultiplier() * max_width;
248 size.set_width(shrinking ? std::max(current_width, size.width()) 245 size.set_width(shrinking ? std::max(current_width, size.width())
249 : current_width); 246 : current_width);
250 } 247 }
251 return size; 248 return size;
252 } 249 }
253 250
254 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { 251 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const {
255 return GetOuterPadding(true) + image_->GetPreferredSize().width() + 252 return GetOuterPadding(true) + image_->GetPreferredSize().width() +
256 GetOuterPadding(false); 253 GetOuterPadding(false);
257 } 254 }
258 255
259 void IconLabelBubbleView::SetLabelBackgroundColor( 256 void IconLabelBubbleView::SetLabelBackgroundColor(
260 SkColor chip_background_color) { 257 SkColor chip_background_color) {
261 // The background images are painted atop |parent_background_color_|. 258 // The background images are painted atop |parent_background_color_|.
262 // Alpha-blend |chip_background_color| with |parent_background_color_| to 259 // Alpha-blend |chip_background_color| with |parent_background_color_| to
263 // determine the actual color the label text will sit atop. 260 // determine the actual color the label text will sit atop.
264 // Tricky bit: We alpha blend an opaque version of |chip_background_color| 261 // Tricky bit: We alpha blend an opaque version of |chip_background_color|
265 // against |parent_background_color_| using the original image grid color's 262 // against |parent_background_color_| using the original image grid color's
266 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged 263 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged
267 // even if |a| is a color with non-255 alpha. 264 // even if |a| is a color with non-255 alpha.
268 label_->SetBackgroundColor(color_utils::AlphaBlend( 265 label_->SetBackgroundColor(color_utils::AlphaBlend(
269 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), 266 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(),
270 SkColorGetA(chip_background_color))); 267 SkColorGetA(chip_background_color)));
271 } 268 }
272 269
273 int IconLabelBubbleView::GetOuterPadding(bool leading) const { 270 int IconLabelBubbleView::GetOuterPadding(bool leading) const {
274 if (ui::MaterialDesignController::IsModeMaterial()) { 271 if (ui::MaterialDesignController::IsModeMaterial())
275 // The apparent leading and trailing padding should be equal, so we need to 272 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING);
276 // subtract the amount of built-in padding in the image. This will mean
277 // that the actual padding + the padding inside the image add up to the same
278 // amount of padding as on the trailing edge of the bubble.
279 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) -
280 (leading ? builtin_leading_padding_ : 0);
281 }
282 273
283 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - 274 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) -
284 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + 275 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) +
285 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); 276 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING));
286 } 277 }
287 278
288 int IconLabelBubbleView::GetInternalSpacing() const { 279 int IconLabelBubbleView::GetInternalSpacing() const {
289 return image_->GetPreferredSize().IsEmpty() 280 return image_->GetPreferredSize().IsEmpty()
290 ? 0 281 ? 0
291 : (GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING) - 282 : GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING);
292 builtin_trailing_padding_);
293 } 283 }
294 284
295 const char* IconLabelBubbleView::GetClassName() const { 285 const char* IconLabelBubbleView::GetClassName() const {
296 return "IconLabelBubbleView"; 286 return "IconLabelBubbleView";
297 } 287 }
298 288
299 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { 289 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) {
300 if (!ShouldShowBackground()) 290 if (!ShouldShowBackground())
301 return; 291 return;
302 if (background_painter_) { 292 if (background_painter_) {
303 views::Painter::PaintPainterAt(canvas, background_painter_.get(), 293 views::Painter::PaintPainterAt(canvas, background_painter_.get(),
304 GetContentsBounds()); 294 GetContentsBounds());
305 } 295 }
306 if (background()) 296
307 background()->Paint(canvas, this); 297 // In MD, draw a separator and not a background.
298 if (ui::MaterialDesignController::IsModeMaterial()) {
299 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor(
300 ui::NativeTheme::kColorId_TextfieldDefaultColor);
301 const SkColor separator_color = SkColorSetA(
302 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC);
303
304 // Amount of padding that is built into the whatever view comes after this
305 // one in the location bar (e.g. omnibox textfield).
306 int post_chip_spacing =
307 GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING);
308 // Amount of space to leave after the separator (dp).
309 const float kPostSeparatorSpacing =
310 (GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) +
311 kExtraTrailingPadding + post_chip_spacing) /
312 2.0f -
313 post_chip_spacing;
314 // Height of the separator (dp).
315 const int kSeparatorHeight = 16;
316 gfx::RectF bounds(GetLocalBounds());
Peter Kasting 2016/05/25 20:23:03 Can using float-based bounds here result in drawin
Evan Stade 2016/05/25 21:03:26 no because there's no anti aliasing, also it's hai
Peter Kasting 2016/05/25 21:24:17 Since there's no code here that explicitly disable
Evan Stade 2016/05/25 21:43:08 Don't you mean explicitly compute non-integer coor
Peter Kasting 2016/05/25 21:54:12 Does that apply to Canvas::DrawLine()? I thought
Evan Stade 2016/05/25 22:32:43 I don't think there's any explicit handling, DrawL
Peter Kasting 2016/05/25 22:44:33 I'm kind of lost by this point as to what snapping
317 bounds.Inset(kPostSeparatorSpacing,
318 (bounds.height() - kSeparatorHeight) / 2);
319
320 // 1px at all scale factors.
321 gfx::ScopedCanvas scoped_canvas(canvas);
322 const float scale = canvas->UndoDeviceScaleFactor();
323 const gfx::RectF scaled_bounds = gfx::ScaleRect(bounds, scale);
324 canvas->DrawLine(scaled_bounds.top_right(), scaled_bounds.bottom_right(),
325 separator_color);
326 }
308 } 327 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698