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

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: Fix 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.
940 if (!GetWidget()) {
941 focus_manager_ = nullptr;
tapted 2016/10/31 00:30:25 Ah, so this doesn't look right without the other c
Patti Lor 2016/11/02 03:19:48 Oops, sorry for the misunderstanding - I thought y
tapted 2016/11/02 03:45:06 This looks OK. It might be nice to have GetWidget(
942 return;
943 }
944
945 if (details.parent->Contains(this) && details.move_view == nullptr) {
946 if (details.is_add) {
947 FocusManager* focus_manager = GetFocusManager();
948 // There should never be two different FocusManagers for the same Widget
949 // at the same time.
950 DCHECK(!focus_manager_ && focus_manager);
951 focus_manager_ = focus_manager;
952 focus_manager_->AddFocusChangeListener(this);
953 } else {
954 DCHECK(focus_manager_);
955 focus_manager_->RemoveFocusChangeListener(this);
956 focus_manager_ = nullptr;
957 }
958 }
959 }
960
930 void Textfield::OnPaint(gfx::Canvas* canvas) { 961 void Textfield::OnPaint(gfx::Canvas* canvas) {
931 OnPaintBackground(canvas); 962 OnPaintBackground(canvas);
932 PaintTextAndCursor(canvas); 963 PaintTextAndCursor(canvas);
933 OnPaintBorder(canvas); 964 OnPaintBorder(canvas);
934 } 965 }
935 966
936 void Textfield::OnFocus() { 967 void Textfield::OnFocus() {
937 GetRenderText()->set_focused(true); 968 GetRenderText()->set_focused(true);
938 if (ShouldShowCursor()) 969 if (ShouldShowCursor())
939 GetRenderText()->set_cursor_visible(true); 970 GetRenderText()->set_cursor_visible(true);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 gfx::Point Textfield::GetKeyboardContextMenuLocation() { 1004 gfx::Point Textfield::GetKeyboardContextMenuLocation() {
974 return GetCaretBounds().bottom_right(); 1005 return GetCaretBounds().bottom_right();
975 } 1006 }
976 1007
977 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { 1008 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) {
978 gfx::RenderText* render_text = GetRenderText(); 1009 gfx::RenderText* render_text = GetRenderText();
979 render_text->SetColor(GetTextColor()); 1010 render_text->SetColor(GetTextColor());
980 UpdateBackgroundColor(); 1011 UpdateBackgroundColor();
981 render_text->set_cursor_color(GetTextColor()); 1012 render_text->set_cursor_color(GetTextColor());
982 render_text->set_selection_color(GetSelectionTextColor()); 1013 render_text->set_selection_color(GetSelectionTextColor());
983 render_text->set_selection_background_focused_color( 1014 if (HasFocus()) {
984 GetSelectionBackgroundColor()); 1015 render_text->set_selection_background_color(GetSelectionBackgroundColor());
1016 } else {
1017 render_text->set_selection_background_color(
1018 GetUnfocusedSelectionBackgroundColor());
1019 }
985 } 1020 }
986 1021
987 //////////////////////////////////////////////////////////////////////////////// 1022 ////////////////////////////////////////////////////////////////////////////////
988 // Textfield, TextfieldModel::Delegate overrides: 1023 // Textfield, TextfieldModel::Delegate overrides:
989 1024
990 void Textfield::OnCompositionTextConfirmedOrCleared() { 1025 void Textfield::OnCompositionTextConfirmedOrCleared() {
991 if (!skip_input_method_cancel_composition_) 1026 if (!skip_input_method_cancel_composition_)
992 GetInputMethod()->CancelComposition(this); 1027 GetInputMethod()->CancelComposition(this);
993 } 1028 }
994 1029
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 return drag_operations; 1086 return drag_operations;
1052 } 1087 }
1053 1088
1054 bool Textfield::CanStartDragForView(View* sender, 1089 bool Textfield::CanStartDragForView(View* sender,
1055 const gfx::Point& press_pt, 1090 const gfx::Point& press_pt,
1056 const gfx::Point& p) { 1091 const gfx::Point& p) {
1057 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt); 1092 return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt);
1058 } 1093 }
1059 1094
1060 //////////////////////////////////////////////////////////////////////////////// 1095 ////////////////////////////////////////////////////////////////////////////////
1096 // Textfield, FocusChangeListener overrides:
1097
1098 void Textfield::OnWillChangeFocus(View* focus_before, View* focus_after) {
1099 if (focus_before != this && focus_after != this)
1100 return;
1101
1102 SkColor selection_bg_color = focus_after
1103 ? GetSelectionBackgroundColor()
1104 : GetUnfocusedSelectionBackgroundColor();
1105
1106 // Selection is drawn if |this| has focus, or the Widget loses activation, but
1107 // not if another View in this Widget is gaining focus.
1108 GetRenderText()->set_draw_text_selection(focus_after == this || !focus_after);
1109 GetRenderText()->set_selection_background_color(selection_bg_color);
1110 }
1111
1112 void Textfield::OnDidChangeFocus(View* focused_before, View* focused_now) {}
1113
1114 ////////////////////////////////////////////////////////////////////////////////
1061 // Textfield, WordLookupClient overrides: 1115 // Textfield, WordLookupClient overrides:
1062 1116
1063 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point, 1117 bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point,
1064 gfx::DecoratedText* decorated_word, 1118 gfx::DecoratedText* decorated_word,
1065 gfx::Point* baseline_point) { 1119 gfx::Point* baseline_point) {
1066 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word, 1120 return GetRenderText()->GetDecoratedWordAtPoint(point, decorated_word,
1067 baseline_point); 1121 baseline_point);
1068 } 1122 }
1069 1123
1070 //////////////////////////////////////////////////////////////////////////////// 1124 ////////////////////////////////////////////////////////////////////////////////
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 } 2095 }
2042 2096
2043 void Textfield::OnCursorBlinkTimerFired() { 2097 void Textfield::OnCursorBlinkTimerFired() {
2044 DCHECK(ShouldBlinkCursor()); 2098 DCHECK(ShouldBlinkCursor());
2045 gfx::RenderText* render_text = GetRenderText(); 2099 gfx::RenderText* render_text = GetRenderText();
2046 render_text->set_cursor_visible(!render_text->cursor_visible()); 2100 render_text->set_cursor_visible(!render_text->cursor_visible());
2047 RepaintCursor(); 2101 RepaintCursor();
2048 } 2102 }
2049 2103
2050 } // namespace views 2104 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698