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 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 height = std::min(height, max_size_.height()); | 280 height = std::min(height, max_size_.height()); |
| 281 return height; | 281 return height; |
| 282 } | 282 } |
| 283 | 283 |
| 284 void LabelButton::Layout() { | 284 void LabelButton::Layout() { |
| 285 gfx::HorizontalAlignment adjusted_alignment = GetHorizontalAlignment(); | 285 gfx::HorizontalAlignment adjusted_alignment = GetHorizontalAlignment(); |
| 286 if (base::i18n::IsRTL() && adjusted_alignment != gfx::ALIGN_CENTER) | 286 if (base::i18n::IsRTL() && adjusted_alignment != gfx::ALIGN_CENTER) |
| 287 adjusted_alignment = (adjusted_alignment == gfx::ALIGN_LEFT) ? | 287 adjusted_alignment = (adjusted_alignment == gfx::ALIGN_LEFT) ? |
| 288 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; | 288 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 289 | 289 |
| 290 // By default, GetChildAreaBounds() ignores the top and bottom border, but we | |
|
msw
2015/02/06 21:24:26
Flip around some of this code: GetChildAreaBounds
Nina
2015/02/09 16:35:09
Done. The idea behind the way I had originally wri
| |
| 291 // want the image to respect it. | |
| 292 gfx::Rect max_image_bounds = GetLocalBounds(); | |
| 293 max_image_bounds.Inset(GetInsets()); | |
| 290 gfx::Rect child_area(GetChildAreaBounds()); | 294 gfx::Rect child_area(GetChildAreaBounds()); |
| 291 child_area.Inset(GetInsets()); | |
| 292 | 295 |
| 293 gfx::Size image_size(image_->GetPreferredSize()); | 296 gfx::Size image_size(image_->GetPreferredSize()); |
| 294 image_size.SetToMin(child_area.size()); | 297 image_size.SetToMin(child_area.size()); |
| 298 image_size.SetToMin(max_image_bounds.size()); | |
| 295 | 299 |
| 296 // The label takes any remaining width after sizing the image, unless both | 300 // 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 | 301 // 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. | 302 // 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. | 303 // Labels can paint over the full button height, including the border height. |
| 300 gfx::Size label_size(child_area.width(), height()); | 304 gfx::Size label_size(child_area.size()); |
| 301 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { | 305 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
| 302 label_size.set_width(std::max(child_area.width() - | 306 label_size.set_width(std::max(child_area.width() - |
| 303 image_size.width() - image_label_spacing_, 0)); | 307 image_size.width() - image_label_spacing_, 0)); |
| 304 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 308 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 305 // Ensure multi-line labels paired with images use their available width. | 309 // Ensure multi-line labels paired with images use their available width. |
| 306 label_size.set_width( | 310 label_size.set_width( |
| 307 std::min(label_size.width(), label_->GetPreferredSize().width())); | 311 std::min(label_size.width(), label_->GetPreferredSize().width())); |
| 308 } | 312 } |
| 309 } | 313 } |
| 310 | 314 |
| 311 gfx::Point image_origin(child_area.origin()); | 315 gfx::Point image_origin(child_area.origin()); |
| 312 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); | 316 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
| 313 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 317 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 314 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? | 318 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? |
| 315 image_label_spacing_ : 0; | 319 image_label_spacing_ : 0; |
| 316 const int total_width = image_size.width() + label_size.width() + | 320 const int total_width = image_size.width() + label_size.width() + |
| 317 spacing; | 321 spacing; |
| 318 image_origin.Offset((child_area.width() - total_width) / 2, 0); | 322 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
| 319 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { | 323 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { |
| 320 image_origin.Offset(child_area.width() - image_size.width(), 0); | 324 image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 321 } | 325 } |
| 322 | 326 |
| 323 gfx::Point label_origin(child_area.x(), 0); | 327 gfx::Point label_origin(child_area.origin()); |
|
msw
2015/02/06 21:24:26
Then change this to: gfx::Point label_origin(label
Nina
2015/02/09 16:35:09
Done.
| |
| 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 { |
| 334 return kViewClassName; | 338 return kViewClassName; |
| 335 } | 339 } |
| 336 | 340 |
| 337 scoped_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const { | 341 scoped_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const { |
| 338 return make_scoped_ptr(new LabelButtonBorder(style_)); | 342 return make_scoped_ptr(new LabelButtonBorder(style_)); |
| 339 } | 343 } |
| 340 | 344 |
| 341 void LabelButton::SetBorder(scoped_ptr<Border> border) { | 345 void LabelButton::SetBorder(scoped_ptr<Border> border) { |
| 342 border_is_themed_border_ = false; | 346 border_is_themed_border_ = false; |
| 343 View::SetBorder(border.Pass()); | 347 View::SetBorder(border.Pass()); |
| 344 ResetCachedPreferredSize(); | 348 ResetCachedPreferredSize(); |
| 345 } | 349 } |
| 346 | 350 |
| 347 gfx::Rect LabelButton::GetChildAreaBounds() { | 351 gfx::Rect LabelButton::GetChildAreaBounds() { |
| 348 return GetLocalBounds(); | 352 gfx::Rect bounds = GetLocalBounds(); |
|
msw
2015/02/06 21:24:26
Revert this to just return GetLocalBounds().
Nina
2015/02/09 16:35:09
Done.
| |
| 353 gfx::Insets insets = GetInsets(); | |
| 354 bounds.Inset(insets.left(), 0, insets.right(), 0); | |
| 355 return bounds; | |
| 349 } | 356 } |
| 350 | 357 |
| 351 void LabelButton::OnPaint(gfx::Canvas* canvas) { | 358 void LabelButton::OnPaint(gfx::Canvas* canvas) { |
| 352 View::OnPaint(canvas); | 359 View::OnPaint(canvas); |
| 353 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 360 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 354 } | 361 } |
| 355 | 362 |
| 356 void LabelButton::OnFocus() { | 363 void LabelButton::OnFocus() { |
| 357 View::OnFocus(); | 364 View::OnFocus(); |
| 358 // Typically the border renders differently when focused. | 365 // Typically the border renders differently when focused. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 } | 512 } |
| 506 | 513 |
| 507 ui::NativeTheme::State LabelButton::GetForegroundThemeState( | 514 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 508 ui::NativeTheme::ExtraParams* params) const { | 515 ui::NativeTheme::ExtraParams* params) const { |
| 509 GetExtraParams(params); | 516 GetExtraParams(params); |
| 510 return ui::NativeTheme::kHovered; | 517 return ui::NativeTheme::kHovered; |
| 511 } | 518 } |
| 512 | 519 |
| 513 void LabelButton::ResetCachedPreferredSize() { | 520 void LabelButton::ResetCachedPreferredSize() { |
| 514 cached_preferred_size_valid_ = false; | 521 cached_preferred_size_valid_ = false; |
| 515 cached_preferred_size_= gfx::Size(); | 522 cached_preferred_size_ = gfx::Size(); |
| 516 } | 523 } |
| 517 | 524 |
| 518 } // namespace views | 525 } // namespace views |
| OLD | NEW |