Chromium Code Reviews| Index: ui/views/controls/textfield/textfield.cc |
| diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc |
| index 14736697771bda755ab2e7e08f2ad70cc1fee919..09cf937993b10417260611f29547350826c1de3b 100644 |
| --- a/ui/views/controls/textfield/textfield.cc |
| +++ b/ui/views/controls/textfield/textfield.cc |
| @@ -255,7 +255,7 @@ Textfield::Textfield() |
| text_input_flags_(0), |
| performing_user_action_(false), |
| skip_input_method_cancel_composition_(false), |
| - cursor_visible_(false), |
| + editing_cursor_visible_(false), |
|
Peter Kasting
2016/09/16 18:52:27
I still don't really know what "editing cursor vis
|
| drop_cursor_visible_(false), |
| initiating_drag_(false), |
| aggregated_clicks_(0), |
| @@ -974,14 +974,14 @@ void Textfield::OnPaint(gfx::Canvas* canvas) { |
| void Textfield::OnFocus() { |
| GetRenderText()->set_focused(true); |
| - cursor_visible_ = true; |
| + editing_cursor_visible_ = true; |
|
Peter Kasting
2016/09/16 18:52:27
Why not make this mutually exclusive with HasSelec
|
| SchedulePaint(); |
| if (GetInputMethod()) |
| GetInputMethod()->SetFocusedTextInputClient(this); |
| OnCaretBoundsChanged(); |
| const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); |
| - if (caret_blink_ms != 0) { |
| + if (caret_blink_ms != 0 && !HasSelection()) { |
| cursor_repaint_timer_.Start(FROM_HERE, |
| base::TimeDelta::FromMilliseconds(caret_blink_ms), this, |
| &Textfield::UpdateCursor); |
| @@ -996,8 +996,8 @@ void Textfield::OnBlur() { |
| if (GetInputMethod()) |
| GetInputMethod()->DetachTextInputClient(this); |
| cursor_repaint_timer_.Stop(); |
| - if (cursor_visible_) { |
| - cursor_visible_ = false; |
| + if (editing_cursor_visible_) { |
| + editing_cursor_visible_ = false; |
| RepaintCursor(); |
| } |
| @@ -1787,16 +1787,33 @@ void Textfield::UpdateBackgroundColor() { |
| } |
| void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { |
| + DCHECK(HasFocus()); |
| if (text_changed) { |
| if (controller_) |
| controller_->ContentsChanged(this, text()); |
| NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); |
| } |
| if (cursor_changed) { |
| - cursor_visible_ = true; |
| + editing_cursor_visible_ = false; |
| RepaintCursor(); |
|
Peter Kasting
2016/09/16 18:52:27
Nit: Since RepaintCursor() merely schedules a pain
|
| - if (cursor_repaint_timer_.IsRunning()) |
| + |
| + // If this textfield is not focused, the timer must have been stopped in |
| + // |Textfield::OnBlur|, and this method needs to avoid accidentally |
| + // restarting it. |
| + if (!HasFocus()) |
| + DCHECK(!cursor_repaint_timer_.IsRunning()); |
|
Peter Kasting
2016/09/16 18:52:27
Nit: Just:
DCHECK(HasFocus() || !cursor_repai
|
| + |
| + // Cursor repaints are suppressed whenever there is a selection or this view |
| + // does not have focus. When the selection changes, the cursor moves, or |
| + // this view gains/loses focus, this method will get called with |
| + // |cursor_changed| set to true, and the timer will be restarted here. |
| + if (HasSelection()) { |
|
Peter Kasting
2016/09/16 18:52:27
Nit: Inspired by my suggestion above to move the R
|
| + cursor_repaint_timer_.Stop(); |
| + } else if (HasFocus() && enabled() && !read_only()) { |
| + editing_cursor_visible_ = true; |
| cursor_repaint_timer_.Reset(); |
| + } |
| + |
| if (!text_changed) { |
| // TEXT_CHANGED implies TEXT_SELECTION_CHANGED, so we only need to fire |
| // this if only the selection changed. |
| @@ -1811,7 +1828,7 @@ void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { |
| void Textfield::UpdateCursor() { |
| const size_t caret_blink_ms = Textfield::GetCaretBlinkMs(); |
| - cursor_visible_ = !cursor_visible_ || (caret_blink_ms == 0); |
| + editing_cursor_visible_ = !editing_cursor_visible_ || (caret_blink_ms == 0); |
| RepaintCursor(); |
| } |
| @@ -1836,7 +1853,8 @@ void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { |
| } |
| // Draw the text, cursor, and selection. |
| - render_text->set_cursor_visible(cursor_visible_ && !drop_cursor_visible_ && |
| + render_text->set_cursor_visible(editing_cursor_visible_ && |
| + !drop_cursor_visible_ && |
| !HasSelection()); |
| render_text->Draw(canvas); |