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

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

Issue 2322303002: Textfield: suppress cursor repaints when there's a selection (Closed)
Patch Set: remove spurious DCHECK Created 4 years, 3 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
« no previous file with comments | « ui/views/controls/textfield/textfield.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/textfield/textfield.cc
diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc
index e6d8012fcf490fad838bc0c52b9b7ef96cb7cb7a..4642d23fc89a89633cc84ff72fbb5011959a7b9d 100644
--- a/ui/views/controls/textfield/textfield.cc
+++ b/ui/views/controls/textfield/textfield.cc
@@ -255,7 +255,6 @@ Textfield::Textfield()
text_input_flags_(0),
performing_user_action_(false),
skip_input_method_cancel_composition_(false),
- cursor_visible_(false),
drop_cursor_visible_(false),
initiating_drag_(false),
aggregated_clicks_(0),
@@ -855,6 +854,8 @@ int Textfield::OnDragUpdated(const ui::DropTargetEvent& event) {
OnCaretBoundsChanged();
SchedulePaint();
+ StopBlinkingCursor();
+
if (initiating_drag_) {
if (in_selection)
return ui::DragDropTypes::DRAG_NONE;
@@ -866,6 +867,8 @@ int Textfield::OnDragUpdated(const ui::DropTargetEvent& event) {
void Textfield::OnDragExited() {
drop_cursor_visible_ = false;
+ if (ShouldBlinkCursor())
+ StartBlinkingCursor();
SchedulePaint();
}
@@ -977,30 +980,26 @@ void Textfield::OnPaint(gfx::Canvas* canvas) {
void Textfield::OnFocus() {
GetRenderText()->set_focused(true);
- cursor_visible_ = true;
+ if (ShouldShowCursor())
+ GetRenderText()->set_cursor_visible(true);
SchedulePaint();
if (GetInputMethod())
GetInputMethod()->SetFocusedTextInputClient(this);
OnCaretBoundsChanged();
-
- const size_t caret_blink_ms = Textfield::GetCaretBlinkMs();
- if (caret_blink_ms != 0) {
- cursor_repaint_timer_.Start(FROM_HERE,
- base::TimeDelta::FromMilliseconds(caret_blink_ms), this,
- &Textfield::UpdateCursor);
- }
-
+ if (ShouldBlinkCursor())
+ StartBlinkingCursor();
View::OnFocus();
SchedulePaint();
}
void Textfield::OnBlur() {
- GetRenderText()->set_focused(false);
+ gfx::RenderText* render_text = GetRenderText();
+ render_text->set_focused(false);
if (GetInputMethod())
GetInputMethod()->DetachTextInputClient(this);
- cursor_repaint_timer_.Stop();
- if (cursor_visible_) {
- cursor_visible_ = false;
+ StopBlinkingCursor();
+ if (render_text->cursor_visible()) {
+ render_text->set_cursor_visible(false);
RepaintCursor();
}
@@ -1801,10 +1800,12 @@ void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) {
NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true);
}
if (cursor_changed) {
- cursor_visible_ = true;
+ GetRenderText()->set_cursor_visible(ShouldShowCursor());
RepaintCursor();
- if (cursor_repaint_timer_.IsRunning())
- cursor_repaint_timer_.Reset();
+ if (ShouldBlinkCursor())
+ StartBlinkingCursor();
+ else
+ StopBlinkingCursor();
if (!text_changed) {
// TEXT_CHANGED implies TEXT_SELECTION_CHANGED, so we only need to fire
// this if only the selection changed.
@@ -1817,12 +1818,6 @@ 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);
- RepaintCursor();
-}
-
void Textfield::RepaintCursor() {
gfx::Rect r(GetRenderText()->GetUpdatedCursorBounds());
r.Inset(-1, -1, -1, -1);
@@ -1843,9 +1838,6 @@ void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) {
render_text->display_rect());
}
- // Draw the text, cursor, and selection.
- render_text->set_cursor_visible(cursor_visible_ && !drop_cursor_visible_ &&
- !HasSelection());
render_text->Draw(canvas);
// Draw the detached drop cursor that marks where the text will be dropped.
@@ -2046,4 +2038,31 @@ void Textfield::OnKeypressUnhandled() {
PlatformStyle::OnTextfieldKeypressUnhandled();
}
+bool Textfield::ShouldShowCursor() const {
+ return HasFocus() && !HasSelection() && enabled() && !read_only() &&
+ !drop_cursor_visible_;
+}
+
+bool Textfield::ShouldBlinkCursor() const {
+ return ShouldShowCursor() && Textfield::GetCaretBlinkMs() != 0;
+}
+
+void Textfield::StartBlinkingCursor() {
+ DCHECK(ShouldBlinkCursor());
+ cursor_blink_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
+ Textfield::GetCaretBlinkMs()),
+ this, &Textfield::OnCursorBlinkTimerFired);
+}
+
+void Textfield::StopBlinkingCursor() {
+ cursor_blink_timer_.Stop();
+}
+
+void Textfield::OnCursorBlinkTimerFired() {
+ DCHECK(ShouldBlinkCursor());
+ gfx::RenderText* render_text = GetRenderText();
+ render_text->set_cursor_visible(!render_text->cursor_visible());
+ RepaintCursor();
+}
+
} // namespace views
« no previous file with comments | « ui/views/controls/textfield/textfield.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698