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 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 IconLabelBubbleView::IconLabelBubbleView(int contained_image, | 34 IconLabelBubbleView::IconLabelBubbleView(int contained_image, |
35 const gfx::FontList& font_list, | 35 const gfx::FontList& font_list, |
36 SkColor parent_background_color, | 36 SkColor parent_background_color, |
37 bool elide_in_middle) | 37 bool elide_in_middle) |
38 : background_painter_(nullptr), | 38 : background_painter_(nullptr), |
39 image_(new views::ImageView()), | 39 image_(new views::ImageView()), |
40 label_(new views::Label(base::string16(), font_list)), | 40 label_(new views::Label(base::string16(), font_list)), |
41 builtin_leading_padding_(0), | |
42 builtin_trailing_padding_(0), | |
43 is_extension_icon_(false), | 41 is_extension_icon_(false), |
44 parent_background_color_(parent_background_color) { | 42 parent_background_color_(parent_background_color) { |
45 if (contained_image) { | 43 if (contained_image) { |
46 image_->SetImage( | 44 image_->SetImage( |
47 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 45 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
48 contained_image)); | 46 contained_image)); |
49 } | 47 } |
50 | 48 |
51 // Disable separate hit testing for |image_|. This prevents views treating | 49 // Disable separate hit testing for |image_|. This prevents views treating |
52 // |image_| as a separate mouse hover region from |this|. | 50 // |image_| as a separate mouse hover region from |this|. |
53 image_->set_interactive(false); | 51 image_->set_interactive(false); |
54 AddChildView(image_); | 52 AddChildView(image_); |
55 | 53 |
56 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 54 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
57 | 55 |
58 if (elide_in_middle) | 56 if (elide_in_middle) |
59 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); | 57 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); |
60 AddChildView(label_); | 58 AddChildView(label_); |
61 | 59 |
62 // Bubbles are given the full internal height of the location bar so that all | 60 // 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 | 61 // 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 | 62 // the bubble should be smaller, so use an empty border to shrink down the |
65 // content bounds so the background gets painted correctly. | 63 // content bounds so the background gets painted correctly. |
66 const int padding = GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING); | 64 SetBorder(views::Border::CreateEmptyBorder( |
67 SetBorder( | 65 gfx::Insets(GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING), 0))); |
68 views::Border::CreateEmptyBorder(gfx::Insets(padding, 0, padding, 0))); | |
69 } | 66 } |
70 | 67 |
71 IconLabelBubbleView::~IconLabelBubbleView() { | 68 IconLabelBubbleView::~IconLabelBubbleView() { |
72 } | 69 } |
73 | 70 |
74 void IconLabelBubbleView::SetBackgroundImageGrid( | 71 void IconLabelBubbleView::SetBackgroundImageGrid( |
75 const int background_images[]) { | 72 const int background_images[]) { |
76 should_show_background_ = true; | 73 should_show_background_ = true; |
77 if (!ui::MaterialDesignController::IsModeMaterial()) { | 74 if (!ui::MaterialDesignController::IsModeMaterial()) { |
78 background_painter_.reset( | 75 background_painter_.reset( |
(...skipping 12 matching lines...) Expand all Loading... | |
91 should_show_background_ = false; | 88 should_show_background_ = false; |
92 SetLabelBackgroundColor(SK_ColorTRANSPARENT); | 89 SetLabelBackgroundColor(SK_ColorTRANSPARENT); |
93 } | 90 } |
94 | 91 |
95 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 92 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
96 label_->SetText(label); | 93 label_->SetText(label); |
97 } | 94 } |
98 | 95 |
99 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 96 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
100 image_->SetImage(image_skia); | 97 image_->SetImage(image_skia); |
101 | |
102 if (ui::MaterialDesignController::IsModeMaterial()) { | |
103 gfx::GetVisibleMargins(image_skia, &builtin_leading_padding_, | |
104 &builtin_trailing_padding_); | |
Peter Kasting
2016/05/20 20:54:11
If this call is removed, GetVisibleMargins() can b
Evan Stade
2016/05/20 22:33:53
yes, as noted above. The only reason I haven't don
| |
105 if (base::i18n::IsRTL()) | |
106 std::swap(builtin_leading_padding_, builtin_trailing_padding_); | |
107 } | |
108 } | 98 } |
109 | 99 |
110 bool IconLabelBubbleView::ShouldShowBackground() const { | 100 bool IconLabelBubbleView::ShouldShowBackground() const { |
111 return should_show_background_; | 101 return should_show_background_; |
112 } | 102 } |
113 | 103 |
114 double IconLabelBubbleView::WidthMultiplier() const { | 104 double IconLabelBubbleView::WidthMultiplier() const { |
115 return 1.0; | 105 return 1.0; |
116 } | 106 } |
117 | 107 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 const int label_x = image_x + image_width + GetInternalSpacing(); | 172 const int label_x = image_x + image_width + GetInternalSpacing(); |
183 const int label_width = | 173 const int label_width = |
184 std::max(0, width() - label_x - bubble_trailing_padding); | 174 std::max(0, width() - label_x - bubble_trailing_padding); |
185 label_->SetBounds(label_x, 0, label_width, height()); | 175 label_->SetBounds(label_x, 0, label_width, height()); |
186 } | 176 } |
187 | 177 |
188 void IconLabelBubbleView::OnNativeThemeChanged( | 178 void IconLabelBubbleView::OnNativeThemeChanged( |
189 const ui::NativeTheme* native_theme) { | 179 const ui::NativeTheme* native_theme) { |
190 label_->SetEnabledColor(GetTextColor()); | 180 label_->SetEnabledColor(GetTextColor()); |
191 | 181 |
192 if (!ui::MaterialDesignController::IsModeMaterial()) | 182 if (ui::MaterialDesignController::IsModeMaterial()) { |
193 return; | 183 label_->SetBackgroundColor(GetParentBackgroundColor()); |
194 | 184 SchedulePaint(); |
195 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); | 185 } |
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 } | 186 } |
202 | 187 |
203 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 188 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
204 image()->SetPaintToLayer(true); | 189 image()->SetPaintToLayer(true); |
205 image()->layer()->SetFillsBoundsOpaquely(false); | 190 image()->layer()->SetFillsBoundsOpaquely(false); |
206 InkDropHostView::AddInkDropLayer(ink_drop_layer); | 191 InkDropHostView::AddInkDropLayer(ink_drop_layer); |
207 } | 192 } |
208 | 193 |
209 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 194 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
210 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 195 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); |
(...skipping 18 matching lines...) Expand all Loading... | |
229 } | 214 } |
230 | 215 |
231 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 216 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
232 gfx::Size size(image_->GetPreferredSize()); | 217 gfx::Size size(image_->GetPreferredSize()); |
233 const bool shrinking = IsShrinking(); | 218 const bool shrinking = IsShrinking(); |
234 // Animation continues for the last few pixels even after the label is not | 219 // 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 | 220 // 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 | 221 // is necessary to animate |total_width| even when the background is hidden |
237 // as long as the animation is still shrinking. | 222 // as long as the animation is still shrinking. |
238 if (ShouldShowBackground() || shrinking) { | 223 if (ShouldShowBackground() || shrinking) { |
224 // Extra padding to place at the end of the label (right side in LTR). | |
225 const int extra_trailing_padding = | |
226 ui::MaterialDesignController::IsModeMaterial() ? 7 : 0; | |
Evan Stade
2016/05/19 21:20:45
these constants are basically trial and error. The
Peter Kasting
2016/05/20 20:54:11
The mock is 8 dp between text and divider line, ri
Evan Stade
2016/05/20 22:33:53
The way the mocks measure the padding abuts the re
Peter Kasting
2016/05/20 22:44:56
Is that because that same constant is used as the
Evan Stade
2016/05/23 21:24:13
OK, I tried that, but it messed up the blocked pop
| |
239 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 227 // |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 | 228 // 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 | 229 // 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 | 230 // 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 | 231 // zero, since this would mean the view would completely disappear and then |
244 // pop back to an icon after the animation finishes. | 232 // pop back to an icon after the animation finishes. |
245 const int max_width = MinimumWidthForImageWithBackgroundShown() + | 233 const int max_width = MinimumWidthForImageWithBackgroundShown() + |
246 GetInternalSpacing() + label_width; | 234 GetInternalSpacing() + label_width + |
235 extra_trailing_padding; | |
247 const int current_width = WidthMultiplier() * max_width; | 236 const int current_width = WidthMultiplier() * max_width; |
248 size.set_width(shrinking ? std::max(current_width, size.width()) | 237 size.set_width(shrinking ? std::max(current_width, size.width()) |
249 : current_width); | 238 : current_width); |
250 } | 239 } |
251 return size; | 240 return size; |
252 } | 241 } |
253 | 242 |
254 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { | 243 int IconLabelBubbleView::MinimumWidthForImageWithBackgroundShown() const { |
255 return GetOuterPadding(true) + image_->GetPreferredSize().width() + | 244 return GetOuterPadding(true) + image_->GetPreferredSize().width() + |
256 GetOuterPadding(false); | 245 GetOuterPadding(false); |
(...skipping 12 matching lines...) Expand all Loading... | |
269 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), | 258 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
270 SkColorGetA(chip_background_color))); | 259 SkColorGetA(chip_background_color))); |
271 } | 260 } |
272 | 261 |
273 int IconLabelBubbleView::GetOuterPadding(bool leading) const { | 262 int IconLabelBubbleView::GetOuterPadding(bool leading) const { |
274 if (ui::MaterialDesignController::IsModeMaterial()) { | 263 if (ui::MaterialDesignController::IsModeMaterial()) { |
275 // The apparent leading and trailing padding should be equal, so we need to | 264 // The apparent leading and trailing padding should be equal, so we need to |
276 // subtract the amount of built-in padding in the image. This will mean | 265 // 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 | 266 // 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. | 267 // amount of padding as on the trailing edge of the bubble. |
279 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING) - | 268 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
280 (leading ? builtin_leading_padding_ : 0); | |
281 } | 269 } |
282 | 270 |
283 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - | 271 return GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) - |
284 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + | 272 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) + |
285 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); | 273 (leading ? 0 : GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING)); |
286 } | 274 } |
287 | 275 |
288 int IconLabelBubbleView::GetInternalSpacing() const { | 276 int IconLabelBubbleView::GetInternalSpacing() const { |
289 return image_->GetPreferredSize().IsEmpty() | 277 return image_->GetPreferredSize().IsEmpty() |
290 ? 0 | 278 ? 0 |
291 : (GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING) - | 279 : GetLayoutConstant(ICON_LABEL_VIEW_INTERNAL_SPACING); |
292 builtin_trailing_padding_); | |
293 } | 280 } |
294 | 281 |
295 const char* IconLabelBubbleView::GetClassName() const { | 282 const char* IconLabelBubbleView::GetClassName() const { |
296 return "IconLabelBubbleView"; | 283 return "IconLabelBubbleView"; |
297 } | 284 } |
298 | 285 |
299 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 286 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
300 if (!ShouldShowBackground()) | 287 if (!ShouldShowBackground()) |
301 return; | 288 return; |
302 if (background_painter_) { | 289 if (background_painter_) { |
303 views::Painter::PaintPainterAt(canvas, background_painter_.get(), | 290 views::Painter::PaintPainterAt(canvas, background_painter_.get(), |
304 GetContentsBounds()); | 291 GetContentsBounds()); |
305 } | 292 } |
306 if (background()) | 293 if (ui::MaterialDesignController::IsModeMaterial()) { |
307 background()->Paint(canvas, this); | 294 gfx::Rect bounds(GetLocalBounds()); |
295 // Amount of space to leave after the separator (dp). | |
296 const int kPostSeparatorSpacing = 4; | |
Peter Kasting
2016/05/20 20:54:11
This is calculated so that with the additional spa
Evan Stade
2016/05/20 22:33:53
well, not quite. The mocks seem to assume less int
Peter Kasting
2016/05/20 22:44:56
Totally agree that calculations can be confusing a
Evan Stade
2016/05/23 21:24:13
made this mathy, ptal?
| |
297 // Height of the separator (dp). | |
298 const int kSeparatorHeight = 16; | |
Peter Kasting
2016/05/20 20:54:11
I wonder if this should be calculated based on the
Evan Stade
2016/05/20 22:33:53
Not sure what you want me to do at the moment. Sha
Peter Kasting
2016/05/20 22:44:57
I'd just ask Max, and assume until he answers that
Evan Stade
2016/05/23 21:24:13
he seems to want exactly 16
| |
299 bounds.Inset(kPostSeparatorSpacing, | |
300 (bounds.height() - kSeparatorHeight) / 2); | |
301 SkColor separator_color = | |
302 SkColorSetA(GetNativeTheme()->GetSystemColor( | |
303 ui::NativeTheme::kColorId_TextfieldDefaultColor), | |
304 0x26); | |
305 canvas->DrawLine(bounds.top_right(), bounds.bottom_right(), | |
306 separator_color); | |
Peter Kasting
2016/05/20 20:54:11
Will this draw a 2 px-thick line on scale factor 2
Evan Stade
2016/05/20 22:33:53
I think we want 1dp, i.e. 2px on scale factor 2. T
Evan Stade
2016/05/23 21:24:13
ok, 1px it is
| |
307 } | |
308 } | 308 } |
OLD | NEW |