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

Unified Diff: ui/views/controls/textfield/textfield.cc

Issue 2345183002: Views: Draw Textfield selected text in gray when top-level Widget loses focus.
Patch Set: Review comments. 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/textfield/textfield.cc
diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc
index 2ab27564ee4b4354f377bcfe825d7b0d4949d4b7..27753e174f24928ee0bfaf9edc108984a4117bc2 100644
--- a/ui/views/controls/textfield/textfield.cc
+++ b/ui/views/controls/textfield/textfield.cc
@@ -238,6 +238,7 @@ Textfield::Textfield()
background_color_(SK_ColorWHITE),
selection_text_color_(SK_ColorWHITE),
selection_background_color_(SK_ColorBLUE),
+ focus_manager_(nullptr),
placeholder_text_color_(kDefaultPlaceholderTextColor),
invalid_(false),
text_input_type_(ui::TEXT_INPUT_TYPE_TEXT),
@@ -275,6 +276,7 @@ Textfield::~Textfield() {
// The textfield should have been blurred before destroy.
DCHECK(this != GetInputMethod()->GetTextInputClient());
}
+ DCHECK(!focus_manager_);
}
void Textfield::SetReadOnly(bool read_only) {
@@ -422,18 +424,23 @@ SkColor Textfield::GetSelectionBackgroundColor() const {
void Textfield::SetSelectionBackgroundColor(SkColor color) {
selection_background_color_ = color;
use_default_selection_background_color_ = false;
- GetRenderText()->set_selection_background_focused_color(
+ GetRenderText()->set_selection_background_color(
GetSelectionBackgroundColor());
SchedulePaint();
}
void Textfield::UseDefaultSelectionBackgroundColor() {
use_default_selection_background_color_ = true;
- GetRenderText()->set_selection_background_focused_color(
+ GetRenderText()->set_selection_background_color(
GetSelectionBackgroundColor());
SchedulePaint();
}
+SkColor Textfield::GetUnfocusedSelectionBackgroundColor() const {
+ return GetNativeTheme()->GetSystemColor(
+ ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused);
+}
+
bool Textfield::GetCursorEnabled() const {
return GetRenderText()->cursor_enabled();
}
@@ -927,6 +934,21 @@ void Textfield::OnEnabledChanged() {
SchedulePaint();
}
+void Textfield::ViewHierarchyChanged(
+ const ViewHierarchyChangedDetails& details) {
+ // Textfields only care about focus changes if the entire Widget has lost
+ // focus, so don't bother listening if there is no Widget.
+ if (!GetWidget())
+ return;
+
+ if (details.parent->Contains(this) && details.move_view == nullptr)
+ ObserveWidgetFocusChanges(details.is_add);
+}
+
+void Textfield::NativeViewHierarchyChanged() {
+ ObserveWidgetFocusChanges(true);
+}
+
void Textfield::OnPaint(gfx::Canvas* canvas) {
OnPaintBackground(canvas);
PaintTextAndCursor(canvas);
@@ -980,8 +1002,12 @@ void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) {
UpdateBackgroundColor();
render_text->set_cursor_color(GetTextColor());
render_text->set_selection_color(GetSelectionTextColor());
- render_text->set_selection_background_focused_color(
- GetSelectionBackgroundColor());
+ if (HasFocus()) {
+ render_text->set_selection_background_color(GetSelectionBackgroundColor());
+ } else {
+ render_text->set_selection_background_color(
+ GetUnfocusedSelectionBackgroundColor());
+ }
}
////////////////////////////////////////////////////////////////////////////////
@@ -1058,6 +1084,25 @@ bool Textfield::CanStartDragForView(View* sender,
}
////////////////////////////////////////////////////////////////////////////////
+// Textfield, FocusChangeListener overrides:
+
+void Textfield::OnWillChangeFocus(View* focus_before, View* focus_after) {
+ if (focus_before != this && focus_after != this)
+ return;
+
+ SkColor selection_bg_color = focus_after
+ ? GetSelectionBackgroundColor()
+ : GetUnfocusedSelectionBackgroundColor();
+
+ // Selection is drawn if |this| has focus, or the Widget loses activation, but
+ // not if another View in this Widget is gaining focus.
+ GetRenderText()->set_draw_text_selection(focus_after == this || !focus_after);
+ GetRenderText()->set_selection_background_color(selection_bg_color);
+}
+
+void Textfield::OnDidChangeFocus(View* focused_before, View* focused_now) {}
+
+////////////////////////////////////////////////////////////////////////////////
// Textfield, WordLookupClient overrides:
bool Textfield::GetDecoratedWordAtPoint(const gfx::Point& point,
@@ -1824,6 +1869,25 @@ void Textfield::UpdateSelectionClipboard() {
#endif
}
+void Textfield::ObserveWidgetFocusChanges(bool add_as_listener) {
+ if (focus_manager_) {
+ focus_manager_->RemoveFocusChangeListener(this);
+ focus_manager_ = nullptr;
+ }
+
+ if (add_as_listener) {
+ focus_manager_ = GetFocusManager();
+ if (!focus_manager_) {
+ // If the Widget this Textfield is attached to has started deleting
+ // itself, GetWidget() and thus GetFocusManager() will both return null.
+ // NativeViewHierarchyChanged() will be called after this, which should
+ // make this Textfield start listening to the new FocusManager.
tapted 2016/11/07 06:14:50 This isn't quite right. I think we're after someth
Patti Lor 2016/11/08 01:47:31 Done.
+ return;
+ }
+ focus_manager_->AddFocusChangeListener(this);
+ }
+}
+
void Textfield::AccessibilitySetValue(const base::string16& new_value,
bool clear_first) {
if (read_only())

Powered by Google App Engine
This is Rietveld 408576698