| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 if (ShouldShowDefaultTooltip()) { | 325 if (ShouldShowDefaultTooltip()) { |
| 326 // Note that |render_text_| is never elided (see the comment in Init() too). | 326 // Note that |render_text_| is never elided (see the comment in Init() too). |
| 327 tooltip->assign(render_text_->GetDisplayText()); | 327 tooltip->assign(render_text_->GetDisplayText()); |
| 328 return true; | 328 return true; |
| 329 } | 329 } |
| 330 | 330 |
| 331 return false; | 331 return false; |
| 332 } | 332 } |
| 333 | 333 |
| 334 void Label::OnEnabledChanged() { | 334 void Label::OnEnabledChanged() { |
| 335 RecalculateColors(); | 335 ApplyTextColors(); |
| 336 View::OnEnabledChanged(); |
| 336 } | 337 } |
| 337 | 338 |
| 338 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( | 339 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( |
| 339 const base::string16& text, | 340 const base::string16& text, |
| 340 gfx::HorizontalAlignment alignment, | 341 gfx::HorizontalAlignment alignment, |
| 341 gfx::DirectionalityMode directionality, | 342 gfx::DirectionalityMode directionality, |
| 342 gfx::ElideBehavior elide_behavior) { | 343 gfx::ElideBehavior elide_behavior) { |
| 343 std::unique_ptr<gfx::RenderText> render_text( | 344 std::unique_ptr<gfx::RenderText> render_text( |
| 344 render_text_->CreateInstanceOfSameType()); | 345 render_text_->CreateInstanceOfSameType()); |
| 345 render_text->SetHorizontalAlignment(alignment); | 346 render_text->SetHorizontalAlignment(alignment); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 std::unique_ptr<gfx::RenderText> line = | 475 std::unique_ptr<gfx::RenderText> line = |
| 475 CreateRenderText(lines[i], alignment, directionality, elide_behavior); | 476 CreateRenderText(lines[i], alignment, directionality, elide_behavior); |
| 476 line->SetDisplayRect(rect); | 477 line->SetDisplayRect(rect); |
| 477 lines_.push_back(std::move(line)); | 478 lines_.push_back(std::move(line)); |
| 478 rect.set_y(rect.y() + rect.height()); | 479 rect.set_y(rect.y() + rect.height()); |
| 479 } | 480 } |
| 480 // Append the remaining text to the last visible line. | 481 // Append the remaining text to the last visible line. |
| 481 for (size_t i = lines_.size(); i < lines.size(); ++i) | 482 for (size_t i = lines_.size(); i < lines.size(); ++i) |
| 482 lines_.back()->SetText(lines_.back()->text() + lines[i]); | 483 lines_.back()->SetText(lines_.back()->text() + lines[i]); |
| 483 } | 484 } |
| 484 RecalculateColors(); | 485 ApplyTextColors(); |
| 485 } | 486 } |
| 486 | 487 |
| 487 gfx::Rect Label::GetFocusBounds() { | 488 gfx::Rect Label::GetFocusBounds() { |
| 488 MaybeBuildRenderTextLines(); | 489 MaybeBuildRenderTextLines(); |
| 489 | 490 |
| 490 gfx::Rect focus_bounds; | 491 gfx::Rect focus_bounds; |
| 491 if (lines_.empty()) { | 492 if (lines_.empty()) { |
| 492 focus_bounds = gfx::Rect(GetTextSize()); | 493 focus_bounds = gfx::Rect(GetTextSize()); |
| 493 } else { | 494 } else { |
| 494 for (size_t i = 0; i < lines_.size(); ++i) { | 495 for (size_t i = 0; i < lines_.size(); ++i) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 void Label::RecalculateColors() { | 554 void Label::RecalculateColors() { |
| 554 actual_enabled_color_ = auto_color_readability_ ? | 555 actual_enabled_color_ = auto_color_readability_ ? |
| 555 color_utils::GetReadableColor(requested_enabled_color_, | 556 color_utils::GetReadableColor(requested_enabled_color_, |
| 556 background_color_) : | 557 background_color_) : |
| 557 requested_enabled_color_; | 558 requested_enabled_color_; |
| 558 actual_disabled_color_ = auto_color_readability_ ? | 559 actual_disabled_color_ = auto_color_readability_ ? |
| 559 color_utils::GetReadableColor(requested_disabled_color_, | 560 color_utils::GetReadableColor(requested_disabled_color_, |
| 560 background_color_) : | 561 background_color_) : |
| 561 requested_disabled_color_; | 562 requested_disabled_color_; |
| 562 | 563 |
| 564 ApplyTextColors(); |
| 565 SchedulePaint(); |
| 566 } |
| 567 |
| 568 void Label::ApplyTextColors() { |
| 563 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; | 569 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; |
| 564 bool subpixel_rendering_suppressed = | 570 bool subpixel_rendering_suppressed = |
| 565 SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_; | 571 SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_; |
| 566 for (size_t i = 0; i < lines_.size(); ++i) { | 572 for (size_t i = 0; i < lines_.size(); ++i) { |
| 567 lines_[i]->SetColor(color); | 573 lines_[i]->SetColor(color); |
| 568 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); | 574 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); |
| 569 } | 575 } |
| 570 SchedulePaint(); | |
| 571 } | 576 } |
| 572 | 577 |
| 573 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { | 578 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
| 574 if (!enabled_color_set_) { | 579 if (!enabled_color_set_) { |
| 575 requested_enabled_color_ = theme->GetSystemColor( | 580 requested_enabled_color_ = theme->GetSystemColor( |
| 576 ui::NativeTheme::kColorId_LabelEnabledColor); | 581 ui::NativeTheme::kColorId_LabelEnabledColor); |
| 577 } | 582 } |
| 578 if (!disabled_color_set_) { | 583 if (!disabled_color_set_) { |
| 579 requested_disabled_color_ = theme->GetSystemColor( | 584 requested_disabled_color_ = theme->GetSystemColor( |
| 580 ui::NativeTheme::kColorId_LabelDisabledColor); | 585 ui::NativeTheme::kColorId_LabelDisabledColor); |
| 581 } | 586 } |
| 582 if (!background_color_set_) { | 587 if (!background_color_set_) { |
| 583 background_color_ = theme->GetSystemColor( | 588 background_color_ = theme->GetSystemColor( |
| 584 ui::NativeTheme::kColorId_LabelBackgroundColor); | 589 ui::NativeTheme::kColorId_LabelBackgroundColor); |
| 585 } | 590 } |
| 586 RecalculateColors(); | 591 RecalculateColors(); |
| 587 } | 592 } |
| 588 | 593 |
| 589 bool Label::ShouldShowDefaultTooltip() const { | 594 bool Label::ShouldShowDefaultTooltip() const { |
| 590 const gfx::Size text_size = GetTextSize(); | 595 const gfx::Size text_size = GetTextSize(); |
| 591 const gfx::Size size = GetContentsBounds().size(); | 596 const gfx::Size size = GetContentsBounds().size(); |
| 592 return !obscured() && (text_size.width() > size.width() || | 597 return !obscured() && (text_size.width() > size.width() || |
| 593 (multi_line() && text_size.height() > size.height())); | 598 (multi_line() && text_size.height() > size.height())); |
| 594 } | 599 } |
| 595 | 600 |
| 596 } // namespace views | 601 } // namespace views |
| OLD | NEW |