Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: ui/views/controls/textfield/textfield.cc

Issue 2345183002: Views: Draw Textfield selected text in gray when top-level Widget loses focus.
Patch Set: Rebase. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/textfield/textfield.h" 5 #include "ui/views/controls/textfield/textfield.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 read_only_(false), 231 read_only_(false),
232 default_width_in_chars_(0), 232 default_width_in_chars_(0),
233 use_default_text_color_(true), 233 use_default_text_color_(true),
234 use_default_background_color_(true), 234 use_default_background_color_(true),
235 use_default_selection_text_color_(true), 235 use_default_selection_text_color_(true),
236 use_default_selection_background_color_(true), 236 use_default_selection_background_color_(true),
237 text_color_(SK_ColorBLACK), 237 text_color_(SK_ColorBLACK),
238 background_color_(SK_ColorWHITE), 238 background_color_(SK_ColorWHITE),
239 selection_text_color_(SK_ColorWHITE), 239 selection_text_color_(SK_ColorWHITE),
240 selection_background_color_(SK_ColorBLUE), 240 selection_background_color_(SK_ColorBLUE),
241 focus_manager_(nullptr),
241 placeholder_text_color_(kDefaultPlaceholderTextColor), 242 placeholder_text_color_(kDefaultPlaceholderTextColor),
242 invalid_(false), 243 invalid_(false),
243 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT), 244 text_input_type_(ui::TEXT_INPUT_TYPE_TEXT),
244 text_input_flags_(0), 245 text_input_flags_(0),
245 performing_user_action_(false), 246 performing_user_action_(false),
246 skip_input_method_cancel_composition_(false), 247 skip_input_method_cancel_composition_(false),
247 drop_cursor_visible_(false), 248 drop_cursor_visible_(false),
248 initiating_drag_(false), 249 initiating_drag_(false),
249 selection_controller_(this), 250 selection_controller_(this),
250 drag_start_display_offset_(0), 251 drag_start_display_offset_(0),
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 SkColor Textfield::GetSelectionBackgroundColor() const { 416 SkColor Textfield::GetSelectionBackgroundColor() const {
416 return use_default_selection_background_color_ ? 417 return use_default_selection_background_color_ ?
417 GetNativeTheme()->GetSystemColor( 418 GetNativeTheme()->GetSystemColor(
418 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused) : 419 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused) :
419 selection_background_color_; 420 selection_background_color_;
420 } 421 }
421 422
422 void Textfield::SetSelectionBackgroundColor(SkColor color) { 423 void Textfield::SetSelectionBackgroundColor(SkColor color) {
423 selection_background_color_ = color; 424 selection_background_color_ = color;
424 use_default_selection_background_color_ = false; 425 use_default_selection_background_color_ = false;
425 GetRenderText()->set_selection_background_focused_color( 426 GetRenderText()->set_selection_background_color(
426 GetSelectionBackgroundColor()); 427 GetSelectionBackgroundColor());
427 SchedulePaint(); 428 SchedulePaint();
428 } 429 }
429 430
430 void Textfield::UseDefaultSelectionBackgroundColor() { 431 void Textfield::UseDefaultSelectionBackgroundColor() {
431 use_default_selection_background_color_ = true; 432 use_default_selection_background_color_ = true;
432 GetRenderText()->set_selection_background_focused_color( 433 GetRenderText()->set_selection_background_color(
433 GetSelectionBackgroundColor()); 434 GetSelectionBackgroundColor());
434 SchedulePaint(); 435 SchedulePaint();
435 } 436 }
436 437
438 SkColor Textfield::GetUnfocusedSelectionBackgroundColor() const {
439 return GetNativeTheme()->GetSystemColor(
440 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused);
441 }
442
437 bool Textfield::GetCursorEnabled() const { 443 bool Textfield::GetCursorEnabled() const {
438 return GetRenderText()->cursor_enabled(); 444 return GetRenderText()->cursor_enabled();
439 } 445 }
440 446
441 void Textfield::SetCursorEnabled(bool enabled) { 447 void Textfield::SetCursorEnabled(bool enabled) {
442 GetRenderText()->SetCursorEnabled(enabled); 448 GetRenderText()->SetCursorEnabled(enabled);
443 } 449 }
444 450
445 const gfx::FontList& Textfield::GetFontList() const { 451 const gfx::FontList& Textfield::GetFontList() const {
446 return GetRenderText()->font_list(); 452 return GetRenderText()->font_list();
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
920 touch_selection_controller_->SelectionChanged(); 926 touch_selection_controller_->SelectionChanged();
921 } 927 }
922 928
923 void Textfield::OnEnabledChanged() { 929 void Textfield::OnEnabledChanged() {
924 View::OnEnabledChanged(); 930 View::OnEnabledChanged();
925 if (GetInputMethod()) 931 if (GetInputMethod())
926 GetInputMethod()->OnTextInputTypeChanged(this); 932 GetInputMethod()->OnTextInputTypeChanged(this);
927 SchedulePaint(); 933 SchedulePaint();
928 } 934 }
929 935
936 void Textfield::ViewHierarchyChanged(
937 const ViewHierarchyChangedDetails& details) {
938 // Textfields only care about focus changes if the entire Widget has lost
939 // focus, so don't bother listening if there is no Widget.
tapted 2016/10/27 00:53:42 This should still set focus_manager_ to nullptr, s
Patti Lor 2016/10/30 23:26:04 Done (for setting focus_manager_ to nullptr). I t
940 if (!GetWidget())
941 return;
942
943 if (details.parent->Contains(this) && details.move_view == nullptr) {
944 if (details.is_add) {
945 FocusManager* focus_manager = GetFocusManager();
946 // There should never be two different FocusManagers for the same Widget
947 // at the same time.
948 DCHECK(!focus_manager_ && focus_manager);
949 focus_manager_ = focus_manager;
950 focus_manager_->AddFocusChangeListener(this);
951 } else {
952 DCHECK(focus_manager_);
953 focus_manager_->RemoveFocusChangeListener(this);
954 focus_manager_ = nullptr;
955 }
956 }
957 }
958
930 void Textfield::OnPaint(gfx::Canvas* canvas) { 959 void Textfield::OnPaint(gfx::Canvas* canvas) {
931 OnPaintBackground(canvas); 960 OnPaintBackground(canvas);
932 PaintTextAndCursor(canvas); 961 PaintTextAndCursor(canvas);
933 OnPaintBorder(canvas); 962 OnPaintBorder(canvas);
934 } 963 }
935 964
936 void Textfield::OnFocus() { 965 void Textfield::OnFocus() {
937 GetRenderText()->set_focused(true); 966 GetRenderText()->set_focused(true);
938 if (ShouldShowCursor()) 967 if (ShouldShowCursor())
939 GetRenderText()->set_cursor_visible(true); 968 GetRenderText()->set_cursor_visible(true);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 gfx::Point Textfield::GetKeyboardContextMenuLocation() { 1002 gfx::Point Textfield::GetKeyboardContextMenuLocation() {
974 return GetCaretBounds().bottom_right(); 1003 return GetCaretBounds().bottom_right();
975 } 1004 }
976 1005
977 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { 1006 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) {
978 gfx::RenderText* render_text = GetRenderText(); 1007 gfx::RenderText* render_text = GetRenderText();
979 render_text->SetColor(GetTextColor()); 1008 render_text->SetColor(GetTextColor());
980 UpdateBackgroundColor(); 1009 UpdateBackgroundColor();
981 render_text->set_cursor_color(GetTextColor()); 1010 render_text->set_cursor_color(GetTextColor());
982 render_text->set_selection_color(GetSelectionTextColor()); 1011 render_text->set_selection_color(GetSelectionTextColor());
983 render_text->set_selection_background_focused_color( 1012 if (HasFocus()) {
984 GetSelectionBackgroundColor()); 1013 render_text->set_selection_background_color(GetSelectionBackgroundColor());
1014 } else {
1015 render_text->set_selection_background_color(
1016 GetUnfocusedSelectionBackgroundColor());
1017 }
985 } 1018 }
986 1019
987 //////////////////////////////////////////////////////////////////////////////// 1020 ////////////////////////////////////////////////////////////////////////////////
988 // Textfield, TextfieldModel::Delegate overrides: 1021 // Textfield, TextfieldModel::Delegate overrides:
989 1022
990 void Textfield::OnCompositionTextConfirmedOrCleared() { 1023 void Textfield::OnCompositionTextConfirmedOrCleared() {
991 if (!skip_input_method_cancel_composition_) 1024 if (!skip_input_method_cancel_composition_)
992 GetInputMethod()->CancelComposition(this); 1025 GetInputMethod()->CancelComposition(this);
993 } 1026 }
994 1027
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 return drag_operations; 1084 return drag_operations;
1052 } 1085 }
1053 1086
1054 bool Textfield::CanStartDragForView(View* sender, 1087 bool Textfield::CanStartDragForView(View* sender,
1055 const gfx::Point& press_pt, 1088 const gfx::Point& press_pt,
1056 const gfx::Point& p) { 1089 const gfx::Point& p) {
1057 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); 1090 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt);
1058 } 1091 }
1059 1092
1060 //////////////////////////////////////////////////////////////////////////////// 1093 ////////////////////////////////////////////////////////////////////////////////
1094 // Textfield, FocusChangeListener overrides:
1095
1096 void Textfield::OnWillChangeFocus(View* focus_before, View* focus_after) {
1097 if (focus_before != this && focus_after != this)
1098 return;
1099
1100 SkColor selection_bg_color = focus_after
1101 ? GetSelectionBackgroundColor()
1102 : GetUnfocusedSelectionBackgroundColor();
1103
1104 // Selection is drawn if |this| has focus, or the Widget loses activation, but
1105 // not if another View in this Widget is gaining focus.
1106 GetRenderText()->set_draw_text_selection(focus_after == this || !focus_after);
1107 GetRenderText()->set_selection_background_color(selection_bg_color);
1108 }
1109
1110 void Textfield::OnDidChangeFocus(View* focused_before, View* focused_now) {}
1111
1112 ////////////////////////////////////////////////////////////////////////////////
1061 // Textfield, WordLookupClient overrides: 1113 // Textfield, WordLookupClient overrides:
1062 1114
1063 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point, 1115 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point,
1064 gfx::DecoratedText* decorated_word, 1116 gfx::DecoratedText* decorated_word,
1065 gfx::Point* baseline_point) { 1117 gfx::Point* baseline_point) {
1066 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word, 1118 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word,
1067 baseline_point); 1119 baseline_point);
1068 } 1120 }
1069 1121
1070 //////////////////////////////////////////////////////////////////////////////// 1122 ////////////////////////////////////////////////////////////////////////////////
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 } 2093 }
2042 2094
2043 void Textfield::OnCursorBlinkTimerFired() { 2095 void Textfield::OnCursorBlinkTimerFired() {
2044 DCHECK(ShouldBlinkCursor()); 2096 DCHECK(ShouldBlinkCursor());
2045 gfx::RenderText* render_text = GetRenderText(); 2097 gfx::RenderText* render_text = GetRenderText();
2046 render_text->set_cursor_visible(!render_text->cursor_visible()); 2098 render_text->set_cursor_visible(!render_text->cursor_visible());
2047 RepaintCursor(); 2099 RepaintCursor();
2048 } 2100 }
2049 2101
2050 } // namespace views 2102 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698