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

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: Rebase. Created 4 years, 2 months 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 835b10509babbaf1fe5147d13903fde02867f278..f3c8ef3894bc6dafece83b25c1b1a908ee7bb567 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),
@@ -422,18 +423,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 +933,29 @@ 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.
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
+ if (!GetWidget())
+ return;
+
+ if (details.parent->Contains(this) && details.move_view == nullptr) {
+ if (details.is_add) {
+ FocusManager* focus_manager = GetFocusManager();
+ // There should never be two different FocusManagers for the same Widget
+ // at the same time.
+ DCHECK(!focus_manager_ && focus_manager);
+ focus_manager_ = focus_manager;
+ focus_manager_->AddFocusChangeListener(this);
+ } else {
+ DCHECK(focus_manager_);
+ focus_manager_->RemoveFocusChangeListener(this);
+ focus_manager_ = nullptr;
+ }
+ }
+}
+
void Textfield::OnPaint(gfx::Canvas* canvas) {
OnPaintBackground(canvas);
PaintTextAndCursor(canvas);
@@ -980,8 +1009,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 +1091,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,

Powered by Google App Engine
This is Rietveld 408576698