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 "ui/views/controls/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "ui/gfx/animation/throb_animation.h" | 9 #include "ui/gfx/animation/throb_animation.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 image_(new ImageView()), | 63 image_(new ImageView()), |
| 64 label_(new Label()), | 64 label_(new Label()), |
| 65 cached_normal_font_list_(GetDefaultNormalFontList()), | 65 cached_normal_font_list_(GetDefaultNormalFontList()), |
| 66 cached_bold_font_list_(GetDefaultBoldFontList()), | 66 cached_bold_font_list_(GetDefaultBoldFontList()), |
| 67 button_state_images_(), | 67 button_state_images_(), |
| 68 button_state_colors_(), | 68 button_state_colors_(), |
| 69 explicitly_set_colors_(), | 69 explicitly_set_colors_(), |
| 70 is_default_(false), | 70 is_default_(false), |
| 71 style_(STYLE_TEXTBUTTON), | 71 style_(STYLE_TEXTBUTTON), |
| 72 border_is_themed_border_(true), | 72 border_is_themed_border_(true), |
| 73 image_label_spacing_(kSpacing) { | 73 image_label_spacing_(kSpacing), |
| 74 ignore_vertical_overlap_(true) { | |
| 74 SetAnimationDuration(kHoverAnimationDurationMs); | 75 SetAnimationDuration(kHoverAnimationDurationMs); |
| 75 SetText(text); | 76 SetText(text); |
| 76 | 77 |
| 77 AddChildView(image_); | 78 AddChildView(image_); |
| 78 image_->set_interactive(false); | 79 image_->set_interactive(false); |
| 79 | 80 |
| 80 AddChildView(label_); | 81 AddChildView(label_); |
| 81 label_->SetFontList(cached_normal_font_list_); | 82 label_->SetFontList(cached_normal_font_list_); |
| 82 label_->SetAutoColorReadabilityEnabled(false); | 83 label_->SetAutoColorReadabilityEnabled(false); |
| 83 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 84 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 gfx::Rect child_area(GetChildAreaBounds()); | 291 gfx::Rect child_area(GetChildAreaBounds()); |
| 291 child_area.Inset(GetInsets()); | 292 child_area.Inset(GetInsets()); |
| 292 | 293 |
| 293 gfx::Size image_size(image_->GetPreferredSize()); | 294 gfx::Size image_size(image_->GetPreferredSize()); |
| 294 image_size.SetToMin(child_area.size()); | 295 image_size.SetToMin(child_area.size()); |
| 295 | 296 |
| 296 // The label takes any remaining width after sizing the image, unless both | 297 // The label takes any remaining width after sizing the image, unless both |
| 297 // views are centered. In that case, using the tighter preferred label width | 298 // views are centered. In that case, using the tighter preferred label width |
| 298 // avoids wasted space within the label that would look like awkward padding. | 299 // avoids wasted space within the label that would look like awkward padding. |
| 299 // Labels can paint over the full button height, including the border height. | 300 // Labels can paint over the full button height, including the border height. |
| 300 gfx::Size label_size(child_area.width(), height()); | 301 gfx::Size label_size(child_area.width(), ignore_vertical_overlap_ |
| 302 ? height() | |
| 303 : child_area.height()); | |
| 301 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { | 304 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
| 302 label_size.set_width(std::max(child_area.width() - | 305 label_size.set_width(std::max(child_area.width() - |
| 303 image_size.width() - image_label_spacing_, 0)); | 306 image_size.width() - image_label_spacing_, 0)); |
| 304 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 307 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 305 // Ensure multi-line labels paired with images use their available width. | 308 // Ensure multi-line labels paired with images use their available width. |
| 306 label_size.set_width( | 309 label_size.set_width( |
| 307 std::min(label_size.width(), label_->GetPreferredSize().width())); | 310 std::min(label_size.width(), label_->GetPreferredSize().width())); |
| 308 } | 311 } |
| 309 } | 312 } |
| 310 | 313 |
| 311 gfx::Point image_origin(child_area.origin()); | 314 gfx::Point image_origin(child_area.origin()); |
| 312 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); | 315 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
| 313 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 316 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 314 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? | 317 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? |
| 315 image_label_spacing_ : 0; | 318 image_label_spacing_ : 0; |
| 316 const int total_width = image_size.width() + label_size.width() + | 319 const int total_width = image_size.width() + label_size.width() + |
| 317 spacing; | 320 spacing; |
| 318 image_origin.Offset((child_area.width() - total_width) / 2, 0); | 321 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
| 319 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { | 322 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { |
| 320 image_origin.Offset(child_area.width() - image_size.width(), 0); | 323 image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 321 } | 324 } |
| 322 | 325 |
| 323 gfx::Point label_origin(child_area.x(), 0); | 326 gfx::Point label_origin(child_area.x(), |
| 327 ignore_vertical_overlap_ ? 0 : child_area.y()); | |
|
flackr
2015/01/30 15:34:04
Out of curiosity, can you tell from the git histor
Nina
2015/01/30 15:44:32
It's required by the bookmark bar:
https://coderev
sky
2015/02/03 21:40:02
This seems suspicious to me. If BookmarkBar wants
msw
2015/02/03 23:50:19
Ignoring the vertical insets for text rendering wa
Nina
2015/02/04 16:20:11
Acknowledged.
flackr
2015/02/04 16:39:08
So it sounds like if we ignore borders for the pur
| |
| 324 if (!image_size.IsEmpty() && adjusted_alignment != gfx::ALIGN_RIGHT) { | 328 if (!image_size.IsEmpty() && adjusted_alignment != gfx::ALIGN_RIGHT) { |
| 325 label_origin.set_x(image_origin.x() + image_size.width() + | 329 label_origin.set_x(image_origin.x() + image_size.width() + |
| 326 image_label_spacing_); | 330 image_label_spacing_); |
| 327 } | 331 } |
| 328 | 332 |
| 329 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); | 333 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
| 330 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); | 334 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
| 331 } | 335 } |
| 332 | 336 |
| 333 const char* LabelButton::GetClassName() const { | 337 const char* LabelButton::GetClassName() const { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 GetExtraParams(params); | 513 GetExtraParams(params); |
| 510 return ui::NativeTheme::kHovered; | 514 return ui::NativeTheme::kHovered; |
| 511 } | 515 } |
| 512 | 516 |
| 513 void LabelButton::ResetCachedPreferredSize() { | 517 void LabelButton::ResetCachedPreferredSize() { |
| 514 cached_preferred_size_valid_ = false; | 518 cached_preferred_size_valid_ = false; |
| 515 cached_preferred_size_= gfx::Size(); | 519 cached_preferred_size_= gfx::Size(); |
| 516 } | 520 } |
| 517 | 521 |
| 518 } // namespace views | 522 } // namespace views |
| OLD | NEW |