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; |
128 RecalculateColors(); | 118 RecalculateColors(); |
129 } | 119 } |
130 | 120 |
131 void Label::SetSelectionTextColor(SkColor color) { | 121 void Label::SetSelectionTextColor(SkColor color) { |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 | 417 |
428 if (ShouldShowDefaultTooltip()) { | 418 if (ShouldShowDefaultTooltip()) { |
429 // 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). |
430 tooltip->assign(render_text_->GetDisplayText()); | 420 tooltip->assign(render_text_->GetDisplayText()); |
431 return true; | 421 return true; |
432 } | 422 } |
433 | 423 |
434 return false; | 424 return false; |
435 } | 425 } |
436 | 426 |
437 void Label::OnEnabledChanged() { | |
438 ApplyTextColors(); | |
439 View::OnEnabledChanged(); | |
440 } | |
441 | |
442 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( | 427 std::unique_ptr<gfx::RenderText> Label::CreateRenderText( |
443 const base::string16& text, | 428 const base::string16& text, |
444 gfx::HorizontalAlignment alignment, | 429 gfx::HorizontalAlignment alignment, |
445 gfx::DirectionalityMode directionality, | 430 gfx::DirectionalityMode directionality, |
446 gfx::ElideBehavior elide_behavior) const { | 431 gfx::ElideBehavior elide_behavior) const { |
447 std::unique_ptr<gfx::RenderText> render_text( | 432 std::unique_ptr<gfx::RenderText> render_text( |
448 render_text_->CreateInstanceOfSameType()); | 433 render_text_->CreateInstanceOfSameType()); |
449 render_text->SetHorizontalAlignment(alignment); | 434 render_text->SetHorizontalAlignment(alignment); |
450 render_text->SetDirectionalityMode(directionality); | 435 render_text->SetDirectionalityMode(directionality); |
451 render_text->SetElideBehavior(elide_behavior); | 436 render_text->SetElideBehavior(elide_behavior); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
823 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); | 808 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT); |
824 // NOTE: |render_text_| should not be elided at all. This is used to keep some | 809 // NOTE: |render_text_| should not be elided at all. This is used to keep some |
825 // properties and to compute the size of the string. | 810 // properties and to compute the size of the string. |
826 render_text_->SetElideBehavior(gfx::NO_ELIDE); | 811 render_text_->SetElideBehavior(gfx::NO_ELIDE); |
827 render_text_->SetFontList(font_list); | 812 render_text_->SetFontList(font_list); |
828 render_text_->SetCursorEnabled(false); | 813 render_text_->SetCursorEnabled(false); |
829 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); | 814 render_text_->SetWordWrapBehavior(gfx::TRUNCATE_LONG_WORDS); |
830 | 815 |
831 elide_behavior_ = gfx::ELIDE_TAIL; | 816 elide_behavior_ = gfx::ELIDE_TAIL; |
832 stored_selection_range_ = gfx::Range::InvalidRange(); | 817 stored_selection_range_ = gfx::Range::InvalidRange(); |
833 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; | 818 enabled_color_set_ = background_color_set_ = false; |
834 selection_text_color_set_ = selection_background_color_set_ = false; | 819 selection_text_color_set_ = selection_background_color_set_ = false; |
835 subpixel_rendering_enabled_ = true; | 820 subpixel_rendering_enabled_ = true; |
836 auto_color_readability_ = true; | 821 auto_color_readability_ = true; |
837 multi_line_ = false; | 822 multi_line_ = false; |
838 UpdateColorsFromTheme(GetNativeTheme()); | 823 UpdateColorsFromTheme(GetNativeTheme()); |
839 handles_tooltips_ = true; | 824 handles_tooltips_ = true; |
840 collapse_when_hidden_ = false; | 825 collapse_when_hidden_ = false; |
841 fixed_width_ = 0; | 826 fixed_width_ = 0; |
842 max_width_ = 0; | 827 max_width_ = 0; |
843 is_first_paint_text_ = true; | 828 is_first_paint_text_ = true; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows()); | 954 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows()); |
970 size.Enlarge(shadow_margin.width(), shadow_margin.height()); | 955 size.Enlarge(shadow_margin.width(), shadow_margin.height()); |
971 return size; | 956 return size; |
972 } | 957 } |
973 | 958 |
974 void Label::RecalculateColors() { | 959 void Label::RecalculateColors() { |
975 actual_enabled_color_ = auto_color_readability_ ? | 960 actual_enabled_color_ = auto_color_readability_ ? |
976 color_utils::GetReadableColor(requested_enabled_color_, | 961 color_utils::GetReadableColor(requested_enabled_color_, |
977 background_color_) : | 962 background_color_) : |
978 requested_enabled_color_; | 963 requested_enabled_color_; |
979 actual_disabled_color_ = auto_color_readability_ ? | |
980 color_utils::GetReadableColor(requested_disabled_color_, | |
981 background_color_) : | |
982 requested_disabled_color_; | |
983 actual_selection_text_color_ = | 964 actual_selection_text_color_ = |
984 auto_color_readability_ | 965 auto_color_readability_ |
985 ? color_utils::GetReadableColor(requested_selection_text_color_, | 966 ? color_utils::GetReadableColor(requested_selection_text_color_, |
986 selection_background_color_) | 967 selection_background_color_) |
987 : requested_selection_text_color_; | 968 : requested_selection_text_color_; |
988 | 969 |
989 ApplyTextColors(); | 970 ApplyTextColors(); |
990 SchedulePaint(); | 971 SchedulePaint(); |
991 } | 972 } |
992 | 973 |
993 void Label::ApplyTextColors() const { | 974 void Label::ApplyTextColors() const { |
994 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; | |
995 bool subpixel_rendering_suppressed = | 975 bool subpixel_rendering_suppressed = |
996 SkColorGetA(background_color_) != SK_AlphaOPAQUE || | 976 SkColorGetA(background_color_) != SK_AlphaOPAQUE || |
997 !subpixel_rendering_enabled_; | 977 !subpixel_rendering_enabled_; |
998 for (size_t i = 0; i < lines_.size(); ++i) { | 978 for (size_t i = 0; i < lines_.size(); ++i) { |
999 lines_[i]->SetColor(color); | 979 lines_[i]->SetColor(actual_enabled_color_); |
1000 lines_[i]->set_selection_color(actual_selection_text_color_); | 980 lines_[i]->set_selection_color(actual_selection_text_color_); |
1001 lines_[i]->set_selection_background_focused_color( | 981 lines_[i]->set_selection_background_focused_color( |
1002 selection_background_color_); | 982 selection_background_color_); |
1003 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); | 983 lines_[i]->set_subpixel_rendering_suppressed(subpixel_rendering_suppressed); |
1004 } | 984 } |
1005 } | 985 } |
1006 | 986 |
1007 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { | 987 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { |
1008 if (!enabled_color_set_) { | 988 if (!enabled_color_set_) { |
1009 requested_enabled_color_ = | 989 requested_enabled_color_ = |
1010 style::GetColor(text_context_, style::STYLE_PRIMARY, theme); | 990 style::GetColor(text_context_, style::STYLE_PRIMARY, theme); |
1011 } | 991 } |
1012 if (!disabled_color_set_) { | |
1013 requested_disabled_color_ = | |
1014 style::GetColor(text_context_, style::STYLE_DISABLED, theme); | |
1015 } | |
1016 if (!background_color_set_) { | 992 if (!background_color_set_) { |
1017 background_color_ = | 993 background_color_ = |
1018 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); | 994 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); |
1019 } | 995 } |
1020 if (!selection_text_color_set_) { | 996 if (!selection_text_color_set_) { |
1021 requested_selection_text_color_ = theme->GetSystemColor( | 997 requested_selection_text_color_ = theme->GetSystemColor( |
1022 ui::NativeTheme::kColorId_LabelTextSelectionColor); | 998 ui::NativeTheme::kColorId_LabelTextSelectionColor); |
1023 } | 999 } |
1024 if (!selection_background_color_set_) { | 1000 if (!selection_background_color_set_) { |
1025 selection_background_color_ = theme->GetSystemColor( | 1001 selection_background_color_ = theme->GetSystemColor( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 .WriteText(GetSelectedText()); | 1038 .WriteText(GetSelectedText()); |
1063 } | 1039 } |
1064 | 1040 |
1065 void Label::BuildContextMenuContents() { | 1041 void Label::BuildContextMenuContents() { |
1066 context_menu_contents_.AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); | 1042 context_menu_contents_.AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); |
1067 context_menu_contents_.AddItemWithStringId(IDS_APP_SELECT_ALL, | 1043 context_menu_contents_.AddItemWithStringId(IDS_APP_SELECT_ALL, |
1068 IDS_APP_SELECT_ALL); | 1044 IDS_APP_SELECT_ALL); |
1069 } | 1045 } |
1070 | 1046 |
1071 } // namespace views | 1047 } // namespace views |
OLD | NEW |