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

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: Refactor to use SelectionController(Delegate). Unfinished! Created 4 years 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/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 background_color_ = color; 377 background_color_ = color;
378 use_default_background_color_ = false; 378 use_default_background_color_ = false;
379 UpdateBackgroundColor(); 379 UpdateBackgroundColor();
380 } 380 }
381 381
382 void Textfield::UseDefaultBackgroundColor() { 382 void Textfield::UseDefaultBackgroundColor() {
383 use_default_background_color_ = true; 383 use_default_background_color_ = true;
384 UpdateBackgroundColor(); 384 UpdateBackgroundColor();
385 } 385 }
386 386
387 SkColor Textfield::GetSelectionTextColor() const {
388 return use_default_selection_text_color_ ?
389 GetNativeTheme()->GetSystemColor(
390 ui::NativeTheme::kColorId_TextfieldSelectionColor) :
391 selection_text_color_;
392 }
393
394 void Textfield::SetSelectionTextColor(SkColor color) { 387 void Textfield::SetSelectionTextColor(SkColor color) {
395 selection_text_color_ = color; 388 selection_text_color_ = color;
396 use_default_selection_text_color_ = false; 389 use_default_selection_text_color_ = false;
397 GetRenderText()->set_selection_color(GetSelectionTextColor()); 390 GetRenderText()->set_selection_color(GetSelectionTextColor());
398 SchedulePaint(); 391 SchedulePaint();
399 } 392 }
400 393
401 void Textfield::UseDefaultSelectionTextColor() { 394 void Textfield::UseDefaultSelectionTextColor() {
402 use_default_selection_text_color_ = true; 395 use_default_selection_text_color_ = true;
403 GetRenderText()->set_selection_color(GetSelectionTextColor()); 396 GetRenderText()->set_selection_color(GetSelectionTextColor());
404 SchedulePaint(); 397 SchedulePaint();
405 } 398 }
406 399
407 void Textfield::SetShadows(const gfx::ShadowValues& shadows) { 400 void Textfield::SetShadows(const gfx::ShadowValues& shadows) {
408 GetRenderText()->set_shadows(shadows); 401 GetRenderText()->set_shadows(shadows);
409 SchedulePaint(); 402 SchedulePaint();
410 } 403 }
411 404
412 SkColor Textfield::GetSelectionBackgroundColor() const {
413 return use_default_selection_background_color_ ?
414 GetNativeTheme()->GetSystemColor(
415 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused) :
416 selection_background_color_;
417 }
418
419 void Textfield::SetSelectionBackgroundColor(SkColor color) { 405 void Textfield::SetSelectionBackgroundColor(SkColor color) {
420 selection_background_color_ = color; 406 selection_background_color_ = color;
421 use_default_selection_background_color_ = false; 407 use_default_selection_background_color_ = false;
422 GetRenderText()->set_selection_background_focused_color( 408 GetRenderText()->set_selection_background_color(
423 GetSelectionBackgroundColor()); 409 GetSelectionBackgroundColor());
424 SchedulePaint(); 410 SchedulePaint();
425 } 411 }
426 412
427 void Textfield::UseDefaultSelectionBackgroundColor() { 413 void Textfield::UseDefaultSelectionBackgroundColor() {
428 use_default_selection_background_color_ = true; 414 use_default_selection_background_color_ = true;
429 GetRenderText()->set_selection_background_focused_color( 415 GetRenderText()->set_selection_background_color(
430 GetSelectionBackgroundColor()); 416 GetSelectionBackgroundColor());
431 SchedulePaint(); 417 SchedulePaint();
432 } 418 }
433 419
434 bool Textfield::GetCursorEnabled() const { 420 bool Textfield::GetCursorEnabled() const {
435 return GetRenderText()->cursor_enabled(); 421 return GetRenderText()->cursor_enabled();
436 } 422 }
437 423
438 void Textfield::SetCursorEnabled(bool enabled) { 424 void Textfield::SetCursorEnabled(bool enabled) {
439 GetRenderText()->SetCursorEnabled(enabled); 425 GetRenderText()->SetCursorEnabled(enabled);
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 touch_selection_controller_->SelectionChanged(); 915 touch_selection_controller_->SelectionChanged();
930 } 916 }
931 917
932 void Textfield::OnEnabledChanged() { 918 void Textfield::OnEnabledChanged() {
933 View::OnEnabledChanged(); 919 View::OnEnabledChanged();
934 if (GetInputMethod()) 920 if (GetInputMethod())
935 GetInputMethod()->OnTextInputTypeChanged(this); 921 GetInputMethod()->OnTextInputTypeChanged(this);
936 SchedulePaint(); 922 SchedulePaint();
937 } 923 }
938 924
925 void Textfield::ViewHierarchyChanged(
926 const ViewHierarchyChangedDetails& details) {
927 // Update the focus manager being listened to.
928 if (details.parent->Contains(this) && details.move_view == nullptr)
929 ObserveWidgetFocusChanges(GetFocusManager(), details.is_add);
930 }
931
932 void Textfield::NativeViewHierarchyChanged() {
933 ObserveWidgetFocusChanges(GetFocusManager(), true);
934 }
935
939 void Textfield::OnPaint(gfx::Canvas* canvas) { 936 void Textfield::OnPaint(gfx::Canvas* canvas) {
940 OnPaintBackground(canvas); 937 OnPaintBackground(canvas);
941 PaintTextAndCursor(canvas); 938 PaintTextAndCursor(canvas);
942 OnPaintBorder(canvas); 939 OnPaintBorder(canvas);
943 } 940 }
944 941
945 void Textfield::OnFocus() { 942 void Textfield::OnFocus() {
946 GetRenderText()->set_focused(true); 943 GetRenderText()->set_focused(true);
947 if (ShouldShowCursor()) 944 if (ShouldShowCursor())
948 GetRenderText()->set_cursor_visible(true); 945 GetRenderText()->set_cursor_visible(true);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
981 978
982 gfx::Point Textfield::GetKeyboardContextMenuLocation() { 979 gfx::Point Textfield::GetKeyboardContextMenuLocation() {
983 return GetCaretBounds().bottom_right(); 980 return GetCaretBounds().bottom_right();
984 } 981 }
985 982
986 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { 983 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) {
987 gfx::RenderText* render_text = GetRenderText(); 984 gfx::RenderText* render_text = GetRenderText();
988 render_text->SetColor(GetTextColor()); 985 render_text->SetColor(GetTextColor());
989 UpdateBackgroundColor(); 986 UpdateBackgroundColor();
990 render_text->set_cursor_color(GetTextColor()); 987 render_text->set_cursor_color(GetTextColor());
991 render_text->set_selection_color(GetSelectionTextColor()); 988 // TODO(patricialor): Does this actually work?
992 render_text->set_selection_background_focused_color( 989 UpdateTextSelectionDrawState(this, GetFocusManager()->GetStoredFocusView(),
993 GetSelectionBackgroundColor()); 990 GetFocusManager()->GetFocusedView());
994 } 991 }
995 992
996 //////////////////////////////////////////////////////////////////////////////// 993 ////////////////////////////////////////////////////////////////////////////////
997 // Textfield, TextfieldModel::Delegate overrides: 994 // Textfield, TextfieldModel::Delegate overrides:
998 995
999 void Textfield::OnCompositionTextConfirmedOrCleared() { 996 void Textfield::OnCompositionTextConfirmedOrCleared() {
1000 if (!skip_input_method_cancel_composition_) 997 if (!skip_input_method_cancel_composition_)
1001 GetInputMethod()->CancelComposition(this); 998 GetInputMethod()->CancelComposition(this);
1002 } 999 }
1003 1000
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1076 baseline_point); 1073 baseline_point);
1077 } 1074 }
1078 1075
1079 //////////////////////////////////////////////////////////////////////////////// 1076 ////////////////////////////////////////////////////////////////////////////////
1080 // Textfield, SelectionControllerDelegate overrides: 1077 // Textfield, SelectionControllerDelegate overrides:
1081 1078
1082 bool Textfield::HasTextBeingDragged() const { 1079 bool Textfield::HasTextBeingDragged() const {
1083 return initiating_drag_; 1080 return initiating_drag_;
1084 } 1081 }
1085 1082
1083 SkColor Textfield::GetSelectionTextColor() const {
1084 return use_default_selection_text_color_
1085 ? GetNativeTheme()->GetSystemColor(
1086 ui::NativeTheme::kColorId_TextfieldSelectionColor)
1087 : selection_text_color_;
1088 }
1089
1090 SkColor Textfield::GetSelectionBackgroundColor() const {
1091 return use_default_selection_background_color_
1092 ? GetNativeTheme()->GetSystemColor(
1093 ui::NativeTheme::
1094 kColorId_TextfieldSelectionBackgroundFocused)
1095 : selection_background_color_;
1096 }
1097
1098 SkColor Textfield::GetSelectionBackgroundUnfocusedColor() const {
1099 // TODO(patricialor): Put a proper colour here.
1100 return SK_ColorBLUE;
1101 }
1102
1086 //////////////////////////////////////////////////////////////////////////////// 1103 ////////////////////////////////////////////////////////////////////////////////
1087 // Textfield, ui::TouchEditable overrides: 1104 // Textfield, ui::TouchEditable overrides:
1088 1105
1089 void Textfield::SelectRect(const gfx::Point& start, const gfx::Point& end) { 1106 void Textfield::SelectRect(const gfx::Point& start, const gfx::Point& end) {
1090 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) 1107 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
1091 return; 1108 return;
1092 1109
1093 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start); 1110 gfx::SelectionModel start_caret = GetRenderText()->FindCursorPosition(start);
1094 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end); 1111 gfx::SelectionModel end_caret = GetRenderText()->FindCursorPosition(end);
1095 gfx::SelectionModel selection( 1112 gfx::SelectionModel selection(
(...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after
1830 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 1847 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1831 if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD) { 1848 if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD) {
1832 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_SELECTION) 1849 ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_SELECTION)
1833 .WriteText(GetSelectedText()); 1850 .WriteText(GetSelectedText());
1834 if (controller_) 1851 if (controller_)
1835 controller_->OnAfterCutOrCopy(ui::CLIPBOARD_TYPE_SELECTION); 1852 controller_->OnAfterCutOrCopy(ui::CLIPBOARD_TYPE_SELECTION);
1836 } 1853 }
1837 #endif 1854 #endif
1838 } 1855 }
1839 1856
1857 void Textfield::OnWillChangeFocus(View* focused_before, View* focused_now) {
1858 UpdateTextSelectionDrawState(this, focused_before, focused_now);
1859 SchedulePaint();
1860 }
1861
1840 void Textfield::UpdateBackgroundColor() { 1862 void Textfield::UpdateBackgroundColor() {
1841 const SkColor color = GetBackgroundColor(); 1863 const SkColor color = GetBackgroundColor();
1842 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { 1864 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) {
1843 set_background(Background::CreateBackgroundPainter( 1865 set_background(Background::CreateBackgroundPainter(
1844 true, Painter::CreateSolidRoundRectPainter( 1866 true, Painter::CreateSolidRoundRectPainter(
1845 color, FocusableBorder::kCornerRadiusDp))); 1867 color, FocusableBorder::kCornerRadiusDp)));
1846 } else { 1868 } else {
1847 set_background(Background::CreateSolidBackground(color)); 1869 set_background(Background::CreateSolidBackground(color));
1848 } 1870 }
1849 // Disable subpixel rendering when the background color is transparent 1871 // Disable subpixel rendering when the background color is transparent
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
2047 } 2069 }
2048 2070
2049 void Textfield::OnCursorBlinkTimerFired() { 2071 void Textfield::OnCursorBlinkTimerFired() {
2050 DCHECK(ShouldBlinkCursor()); 2072 DCHECK(ShouldBlinkCursor());
2051 gfx::RenderText* render_text = GetRenderText(); 2073 gfx::RenderText* render_text = GetRenderText();
2052 render_text->set_cursor_visible(!render_text->cursor_visible()); 2074 render_text->set_cursor_visible(!render_text->cursor_visible());
2053 RepaintCursor(); 2075 RepaintCursor();
2054 } 2076 }
2055 2077
2056 } // namespace views 2078 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/textfield/textfield.h ('k') | ui/views/controls/textfield/textfield_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698