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 |
| 291 // want the image to respect it. |
290 gfx::Rect child_area(GetChildAreaBounds()); | 292 gfx::Rect child_area(GetChildAreaBounds()); |
291 child_area.Inset(GetInsets()); | 293 gfx::Rect label_area(child_area); |
| 294 |
| 295 gfx::Insets insets(GetInsets()); |
| 296 child_area.Inset(insets); |
| 297 // Labels can paint over the vertical component of the border insets. |
| 298 label_area.Inset(insets.left(), 0, insets.right(), 0); |
292 | 299 |
293 gfx::Size image_size(image_->GetPreferredSize()); | 300 gfx::Size image_size(image_->GetPreferredSize()); |
294 image_size.SetToMin(child_area.size()); | 301 image_size.SetToMin(child_area.size()); |
295 | 302 |
296 // The label takes any remaining width after sizing the image, unless both | 303 // 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 | 304 // 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. | 305 // 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. | 306 gfx::Size label_size(label_area.size()); |
300 gfx::Size label_size(child_area.width(), height()); | |
301 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { | 307 if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
302 label_size.set_width(std::max(child_area.width() - | 308 label_size.set_width(std::max(child_area.width() - |
303 image_size.width() - image_label_spacing_, 0)); | 309 image_size.width() - image_label_spacing_, 0)); |
304 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 310 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
305 // Ensure multi-line labels paired with images use their available width. | 311 // Ensure multi-line labels paired with images use their available width. |
306 label_size.set_width( | 312 label_size.set_width( |
307 std::min(label_size.width(), label_->GetPreferredSize().width())); | 313 std::min(label_size.width(), label_->GetPreferredSize().width())); |
308 } | 314 } |
309 } | 315 } |
310 | 316 |
311 gfx::Point image_origin(child_area.origin()); | 317 gfx::Point image_origin(child_area.origin()); |
312 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); | 318 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
313 if (adjusted_alignment == gfx::ALIGN_CENTER) { | 319 if (adjusted_alignment == gfx::ALIGN_CENTER) { |
314 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? | 320 const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? |
315 image_label_spacing_ : 0; | 321 image_label_spacing_ : 0; |
316 const int total_width = image_size.width() + label_size.width() + | 322 const int total_width = image_size.width() + label_size.width() + |
317 spacing; | 323 spacing; |
318 image_origin.Offset((child_area.width() - total_width) / 2, 0); | 324 image_origin.Offset((child_area.width() - total_width) / 2, 0); |
319 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { | 325 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { |
320 image_origin.Offset(child_area.width() - image_size.width(), 0); | 326 image_origin.Offset(child_area.width() - image_size.width(), 0); |
321 } | 327 } |
322 | 328 |
323 gfx::Point label_origin(child_area.x(), 0); | 329 gfx::Point label_origin(label_area.origin()); |
324 if (!image_size.IsEmpty() && adjusted_alignment != gfx::ALIGN_RIGHT) { | 330 if (!image_size.IsEmpty() && adjusted_alignment != gfx::ALIGN_RIGHT) { |
325 label_origin.set_x(image_origin.x() + image_size.width() + | 331 label_origin.set_x(image_origin.x() + image_size.width() + |
326 image_label_spacing_); | 332 image_label_spacing_); |
327 } | 333 } |
328 | 334 |
329 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); | 335 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
330 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); | 336 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
331 } | 337 } |
332 | 338 |
333 const char* LabelButton::GetClassName() const { | 339 const char* LabelButton::GetClassName() const { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 } | 511 } |
506 | 512 |
507 ui::NativeTheme::State LabelButton::GetForegroundThemeState( | 513 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
508 ui::NativeTheme::ExtraParams* params) const { | 514 ui::NativeTheme::ExtraParams* params) const { |
509 GetExtraParams(params); | 515 GetExtraParams(params); |
510 return ui::NativeTheme::kHovered; | 516 return ui::NativeTheme::kHovered; |
511 } | 517 } |
512 | 518 |
513 void LabelButton::ResetCachedPreferredSize() { | 519 void LabelButton::ResetCachedPreferredSize() { |
514 cached_preferred_size_valid_ = false; | 520 cached_preferred_size_valid_ = false; |
515 cached_preferred_size_= gfx::Size(); | 521 cached_preferred_size_ = gfx::Size(); |
516 } | 522 } |
517 | 523 |
518 } // namespace views | 524 } // namespace views |
OLD | NEW |