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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 | 102 |
103 void Label::SetEnabledColor(SkColor color) { | 103 void Label::SetEnabledColor(SkColor color) { |
104 if (enabled_color_set_ && requested_enabled_color_ == color) | 104 if (enabled_color_set_ && requested_enabled_color_ == color) |
105 return; | 105 return; |
106 is_first_paint_text_ = true; | 106 is_first_paint_text_ = true; |
107 requested_enabled_color_ = color; | 107 requested_enabled_color_ = color; |
108 enabled_color_set_ = true; | 108 enabled_color_set_ = true; |
109 RecalculateColors(); | 109 RecalculateColors(); |
110 } | 110 } |
111 | 111 |
112 // TODO(tapted): Move this into a subclass used only by LabelButton. | |
113 void Label::SetDisabledColorForLabelButton(SkColor color) { | |
114 if (disabled_color_set_ && requested_disabled_color_ == color) | |
115 return; | |
116 is_first_paint_text_ = true; | |
117 requested_disabled_color_ = color; | |
118 disabled_color_set_ = true; | |
119 RecalculateColors(); | |
120 } | |
121 | |
122 void Label::SetBackgroundColor(SkColor color) { | 112 void Label::SetBackgroundColor(SkColor color) { |
123 if (background_color_set_ && background_color_ == color) | 113 if (background_color_set_ && background_color_ == color) |
124 return; | 114 return; |
125 is_first_paint_text_ = true; | 115 is_first_paint_text_ = true; |
126 background_color_ = color; | 116 background_color_ = color; |
127 background_color_set_ = true; | 117 background_color_set_ = true; |
118 OnBackgroundColorChanged(); | |
128 RecalculateColors(); | 119 RecalculateColors(); |
129 } | 120 } |
130 | 121 |
131 void Label::SetSelectionTextColor(SkColor color) { | 122 void Label::SetSelectionTextColor(SkColor color) { |
132 if (selection_text_color_set_ && requested_selection_text_color_ == color) | 123 if (selection_text_color_set_ && requested_selection_text_color_ == color) |
133 return; | 124 return; |
134 is_first_paint_text_ = true; | 125 is_first_paint_text_ = true; |
135 requested_selection_text_color_ = color; | 126 requested_selection_text_color_ = color; |
136 selection_text_color_set_ = true; | 127 selection_text_color_set_ = true; |
137 RecalculateColors(); | 128 RecalculateColors(); |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 | 417 |
427 if (ShouldShowDefaultTooltip()) { | 418 if (ShouldShowDefaultTooltip()) { |
428 // Note that |render_text_| is never elided (see the comment in Init() too). | 419 // Note that |render_text_| is never elided (see the comment in Init() too). |
429 tooltip->assign(render_text_->GetDisplayText()); | 420 tooltip->assign(render_text_->GetDisplayText()); |
430 return true; | 421 return true; |
431 } | 422 } |
432 | 423 |
433 return false; | 424 return false; |
434 } | 425 } |
435 | 426 |
436 void Label::OnEnabledChanged() { | |
437 ApplyTextColors(); | |
tapted
2017/06/06 12:30:24
I tested and ran jobs in earlier patchsets with th
| |
438 View::OnEnabledChanged(); | |
439 } | |
440 | |
441 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( | 427 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( |
442 const base::string16& text, | 428 const base::string16& text, |
443 gfx::HorizontalAlignment alignment, | 429 gfx::HorizontalAlignment alignment, |
444 gfx::DirectionalityMode directionality, | 430 gfx::DirectionalityMode directionality, |
445 gfx::ElideBehavior elide_behavior) const { | 431 gfx::ElideBehavior elide_behavior) const { |
446 std::unique_ptr<gfx::RenderText> render_text( | 432 std::unique_ptr<gfx::RenderText> render_text( |
447 render_text_->CreateInstanceOfSameType()); | 433 render_text_->CreateInstanceOfSameType()); |
448 render_text->SetHorizontalAlignment(alignment); | 434 render_text->SetHorizontalAlignment(alignment); |
449 render_text->SetDirectionalityMode(directionality); | 435 render_text->SetDirectionalityMode(directionality); |
450 render_text->SetElideBehavior(elide_behavior); | 436 render_text->SetElideBehavior(elide_behavior); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
500 | 486 |
501 if (view->layer() && view->layer()->fills_bounds_opaquely()) { | 487 if (view->layer() && view->layer()->fills_bounds_opaquely()) { |
502 DLOG(WARNING) << "Ancestor view has a non-opaque layer: " | 488 DLOG(WARNING) << "Ancestor view has a non-opaque layer: " |
503 << view->GetClassName() << " with ID " << view->id(); | 489 << view->GetClassName() << " with ID " << view->id(); |
504 break; | 490 break; |
505 } | 491 } |
506 } | 492 } |
507 #endif | 493 #endif |
508 } | 494 } |
509 | 495 |
496 SkColor Label::EnsureTextColorReadable(SkColor requested) const { | |
497 return auto_color_readability_ | |
498 ? color_utils::GetReadableColor(requested, background_color_) | |
499 : requested; | |
500 } | |
501 | |
502 SkColor Label::GetForegroundColor() const { | |
503 return enabled_color(); | |
504 } | |
505 | |
506 void Label::OnBackgroundColorChanged() {} | |
507 | |
508 void Label::ApplyTextColors() const { | |
509 bool subpixel_rendering_suppressed = | |
510 SkColorGetA(background_color_) != SK_AlphaOPAQUE || | |
511 !subpixel_rendering_enabled_; | |
512 for (size_t i = 0; i < lines_.size(); ++i) { | |
513 lines_[i]->SetColor(GetForegroundColor()); | |
514 lines_[i]->set_selection_color(actual_selection_text_color_); | |
515 lines_[i]->set_selection_background_focused_color( | |
516 selection_background_color_); | |
517 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); | |
518 } | |
519 } | |
520 | |
510 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 521 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
511 if (previous_bounds.size() != size()) | 522 if (previous_bounds.size() != size()) |
512 InvalidateLayout(); | 523 InvalidateLayout(); |
513 } | 524 } |
514 | 525 |
515 void Label::OnPaint(gfx::Canvas* canvas) { | 526 void Label::OnPaint(gfx::Canvas* canvas) { |
516 View::OnPaint(canvas); | 527 View::OnPaint(canvas); |
517 if (is_first_paint_text_) { | 528 if (is_first_paint_text_) { |
518 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is | 529 // TODO(ckocagil): Remove ScopedTracker below once crbug.com/441028 is |
519 // fixed. | 530 // fixed. |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
822 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); | 833 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); |
823 // NOTE: |render_text_| should not be elided at all. This is used to keep some | 834 // NOTE: |render_text_| should not be elided at all. This is used to keep some |
824 // properties and to compute the size of the string. | 835 // properties and to compute the size of the string. |
825 render_text_->SetElideBehavior(gfx::NO_ELIDE); | 836 render_text_->SetElideBehavior(gfx::NO_ELIDE); |
826 render_text_->SetFontList(font_list); | 837 render_text_->SetFontList(font_list); |
827 render_text_->SetCursorEnabled(false); | 838 render_text_->SetCursorEnabled(false); |
828 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); | 839 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); |
829 | 840 |
830 elide_behavior_ = gfx::ELIDE_TAIL; | 841 elide_behavior_ = gfx::ELIDE_TAIL; |
831 stored_selection_range_ = gfx::Range::InvalidRange(); | 842 stored_selection_range_ = gfx::Range::InvalidRange(); |
832 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; | 843 enabled_color_set_ = background_color_set_ = false; |
833 selection_text_color_set_ = selection_background_color_set_ = false; | 844 selection_text_color_set_ = selection_background_color_set_ = false; |
834 subpixel_rendering_enabled_ = true; | 845 subpixel_rendering_enabled_ = true; |
835 auto_color_readability_ = true; | 846 auto_color_readability_ = true; |
836 multi_line_ = false; | 847 multi_line_ = false; |
837 UpdateColorsFromTheme(GetNativeTheme()); | 848 UpdateColorsFromTheme(GetNativeTheme()); |
838 handles_tooltips_ = true; | 849 handles_tooltips_ = true; |
839 collapse_when_hidden_ = false; | 850 collapse_when_hidden_ = false; |
840 fixed_width_ = 0; | 851 fixed_width_ = 0; |
841 max_width_ = 0; | 852 max_width_ = 0; |
842 is_first_paint_text_ = true; | 853 is_first_paint_text_ = true; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
964 size.set_width(std::max(size.width(), line.width())); | 975 size.set_width(std::max(size.width(), line.width())); |
965 size.set_height(std::max(line_height(), size.height() + line.height())); | 976 size.set_height(std::max(line_height(), size.height() + line.height())); |
966 } | 977 } |
967 } | 978 } |
968 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows()); | 979 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows()); |
969 size.Enlarge(shadow_margin.width(), shadow_margin.height()); | 980 size.Enlarge(shadow_margin.width(), shadow_margin.height()); |
970 return size; | 981 return size; |
971 } | 982 } |
972 | 983 |
973 void Label::RecalculateColors() { | 984 void Label::RecalculateColors() { |
974 actual_enabled_color_ = auto_color_readability_ ? | 985 actual_enabled_color_ = EnsureTextColorReadable(requested_enabled_color_); |
975 color_utils::GetReadableColor(requested_enabled_color_, | |
976 background_color_) : | |
977 requested_enabled_color_; | |
978 actual_disabled_color_ = auto_color_readability_ ? | |
979 color_utils::GetReadableColor(requested_disabled_color_, | |
980 background_color_) : | |
981 requested_disabled_color_; | |
982 actual_selection_text_color_ = | 986 actual_selection_text_color_ = |
983 auto_color_readability_ | 987 auto_color_readability_ |
984 ? color_utils::GetReadableColor(requested_selection_text_color_, | 988 ? color_utils::GetReadableColor(requested_selection_text_color_, |
985 selection_background_color_) | 989 selection_background_color_) |
986 : requested_selection_text_color_; | 990 : requested_selection_text_color_; |
987 | 991 |
988 ApplyTextColors(); | 992 ApplyTextColors(); |
989 SchedulePaint(); | 993 SchedulePaint(); |
990 } | 994 } |
991 | 995 |
992 void Label::ApplyTextColors() const { | |
993 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; | |
994 bool subpixel_rendering_suppressed = | |
995 SkColorGetA(background_color_) != SK_AlphaOPAQUE || | |
996 !subpixel_rendering_enabled_; | |
997 for (size_t i = 0; i < lines_.size(); ++i) { | |
998 lines_[i]->SetColor(color); | |
999 lines_[i]->set_selection_color(actual_selection_text_color_); | |
1000 lines_[i]->set_selection_background_focused_color( | |
1001 selection_background_color_); | |
1002 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); | |
1003 } | |
1004 } | |
1005 | |
1006 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { | 996 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
1007 if (!enabled_color_set_) { | 997 if (!enabled_color_set_) { |
1008 requested_enabled_color_ = | 998 requested_enabled_color_ = |
1009 style::GetColor(text_context_, style::STYLE_PRIMARY, theme); | 999 style::GetColor(text_context_, style::STYLE_PRIMARY, theme); |
1010 } | 1000 } |
1011 if (!disabled_color_set_) { | |
1012 requested_disabled_color_ = | |
1013 style::GetColor(text_context_, style::STYLE_DISABLED, theme); | |
1014 } | |
1015 if (!background_color_set_) { | 1001 if (!background_color_set_) { |
1016 background_color_ = | 1002 background_color_ = |
1017 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); | 1003 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); |
1018 } | 1004 } |
1019 if (!selection_text_color_set_) { | 1005 if (!selection_text_color_set_) { |
1020 requested_selection_text_color_ = theme->GetSystemColor( | 1006 requested_selection_text_color_ = theme->GetSystemColor( |
1021 ui::NativeTheme::kColorId_LabelTextSelectionColor); | 1007 ui::NativeTheme::kColorId_LabelTextSelectionColor); |
1022 } | 1008 } |
1023 if (!selection_background_color_set_) { | 1009 if (!selection_background_color_set_) { |
1024 selection_background_color_ = theme->GetSystemColor( | 1010 selection_background_color_ = theme->GetSystemColor( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1061 .WriteText(GetSelectedText()); | 1047 .WriteText(GetSelectedText()); |
1062 } | 1048 } |
1063 | 1049 |
1064 void Label::BuildContextMenuContents() { | 1050 void Label::BuildContextMenuContents() { |
1065 context_menu_contents_.AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); | 1051 context_menu_contents_.AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); |
1066 context_menu_contents_.AddItemWithStringId(IDS_APP_SELECT_ALL, | 1052 context_menu_contents_.AddItemWithStringId(IDS_APP_SELECT_ALL, |
1067 IDS_APP_SELECT_ALL); | 1053 IDS_APP_SELECT_ALL); |
1068 } | 1054 } |
1069 | 1055 |
1070 } // namespace views | 1056 } // namespace views |
OLD | NEW |