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/label.h" | 5 #include "ui/views/controls/label.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <vector> | 10 #include <vector> |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 return true; | 320 return true; |
321 } | 321 } |
322 | 322 |
323 return false; | 323 return false; |
324 } | 324 } |
325 | 325 |
326 void Label::OnEnabledChanged() { | 326 void Label::OnEnabledChanged() { |
327 RecalculateColors(); | 327 RecalculateColors(); |
328 } | 328 } |
329 | 329 |
330 void Label::PaintText(gfx::Canvas* canvas) { | 330 gfx::Rect Label::PaintText(gfx::Canvas* canvas) { |
331 MaybeBuildRenderTextLines(); | 331 MaybeBuildRenderTextLines(); |
332 for (size_t i = 0; i < lines_.size(); ++i) | 332 gfx::Rect focus_bounds; |
333 for (size_t i = 0; i < lines_.size(); ++i) { | |
334 gfx::Point origin; | |
335 origin += lines_[i]->GetLineOffset(0); | |
336 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize())); | |
333 lines_[i]->Draw(canvas); | 337 lines_[i]->Draw(canvas); |
338 } | |
339 | |
340 if (HasFocus()) { | |
sky
2015/03/16 21:29:57
What if the label has focus, but is empty? What wi
Jun Mukai
2015/03/16 22:05:38
Good point, I believe a thin rectangle (line-heigh
| |
341 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | |
sky
2015/03/16 21:29:57
What if kFocusBorderPadding is > current size?
Jun Mukai
2015/03/16 22:05:37
Fixed to be capped inside of the size.
| |
342 canvas->DrawFocusRect(focus_bounds); | |
343 return focus_bounds; | |
344 } | |
345 return gfx::Rect(); | |
sky
2015/03/16 21:29:57
Is there a reason not to always return focus_rect?
Jun Mukai
2015/03/16 22:05:37
Changed to keep the focus bounds as a field |focus
sky
2015/03/16 22:56:09
Why use a field at all for this? The focus rect is
Jun Mukai
2015/03/17 00:03:47
I see. separated as GetFocusBounds().
| |
334 } | 346 } |
335 | 347 |
336 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 348 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
337 if (previous_bounds.size() != size()) | 349 if (previous_bounds.size() != size()) |
338 InvalidateLayout(); | 350 InvalidateLayout(); |
339 } | 351 } |
340 | 352 |
341 void Label::OnPaint(gfx::Canvas* canvas) { | 353 void Label::OnPaint(gfx::Canvas* canvas) { |
342 View::OnPaint(canvas); | 354 View::OnPaint(canvas); |
343 if (is_first_paint_text_) { | 355 if (is_first_paint_text_) { |
344 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. | 356 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
345 tracked_objects::ScopedTracker tracking_profile( | 357 tracked_objects::ScopedTracker tracking_profile( |
346 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText first")); | 358 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText first")); |
347 | 359 |
348 is_first_paint_text_ = false; | 360 is_first_paint_text_ = false; |
349 PaintText(canvas); | 361 PaintText(canvas); |
350 } else { | 362 } else { |
351 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. | 363 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
352 tracked_objects::ScopedTracker tracking_profile( | 364 tracked_objects::ScopedTracker tracking_profile( |
353 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); | 365 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); |
354 | 366 |
355 PaintText(canvas); | 367 PaintText(canvas); |
356 } | 368 } |
357 if (HasFocus()) { | |
358 gfx::Rect focus_bounds = GetLocalBounds(); | |
359 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | |
360 canvas->DrawFocusRect(focus_bounds); | |
361 } | |
362 } | 369 } |
363 | 370 |
364 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 371 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
365 UpdateColorsFromTheme(theme); | 372 UpdateColorsFromTheme(theme); |
366 } | 373 } |
367 | 374 |
368 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { | 375 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { |
369 View::OnDeviceScaleFactorChanged(device_scale_factor); | 376 View::OnDeviceScaleFactorChanged(device_scale_factor); |
370 // When the device scale factor is changed, some font rendering parameters is | 377 // When the device scale factor is changed, some font rendering parameters is |
371 // changed (especially, hinting). The bounding box of the text has to be | 378 // changed (especially, hinting). The bounding box of the text has to be |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 render_text->SetCursorEnabled(false); | 433 render_text->SetCursorEnabled(false); |
427 render_text->SetText(text); | 434 render_text->SetText(text); |
428 return render_text.Pass(); | 435 return render_text.Pass(); |
429 } | 436 } |
430 | 437 |
431 void Label::MaybeBuildRenderTextLines() { | 438 void Label::MaybeBuildRenderTextLines() { |
432 if (!lines_.empty()) | 439 if (!lines_.empty()) |
433 return; | 440 return; |
434 | 441 |
435 gfx::Rect rect = GetContentsBounds(); | 442 gfx::Rect rect = GetContentsBounds(); |
443 if (focusable()) | |
444 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); | |
436 if (rect.IsEmpty()) | 445 if (rect.IsEmpty()) |
437 return; | 446 return; |
438 | 447 |
439 gfx::HorizontalAlignment alignment = horizontal_alignment(); | 448 gfx::HorizontalAlignment alignment = horizontal_alignment(); |
440 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); | 449 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); |
441 if (multi_line()) { | 450 if (multi_line()) { |
442 // Force the directionality and alignment of the first line on other lines. | 451 // Force the directionality and alignment of the first line on other lines. |
443 bool rtl = | 452 bool rtl = |
444 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; | 453 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; |
445 if (alignment == gfx::ALIGN_TO_HEAD) | 454 if (alignment == gfx::ALIGN_TO_HEAD) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
554 } | 563 } |
555 | 564 |
556 bool Label::ShouldShowDefaultTooltip() const { | 565 bool Label::ShouldShowDefaultTooltip() const { |
557 const gfx::Size text_size = GetTextSize(); | 566 const gfx::Size text_size = GetTextSize(); |
558 const gfx::Size size = GetContentsBounds().size(); | 567 const gfx::Size size = GetContentsBounds().size(); |
559 return !obscured() && (text_size.width() > size.width() || | 568 return !obscured() && (text_size.width() > size.width() || |
560 (multi_line() && text_size.height() > size.height())); | 569 (multi_line() && text_size.height() > size.height())); |
561 } | 570 } |
562 | 571 |
563 } // namespace views | 572 } // namespace views |
OLD | NEW |