 Chromium Code Reviews
 Chromium Code Reviews Issue 2660593002:
  Paint text cursor in LAYER_SOLID_COLOR  (Closed)
    
  
    Issue 2660593002:
  Paint text cursor in LAYER_SOLID_COLOR  (Closed) 
  | Index: ui/views/controls/textfield/textfield.cc | 
| diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc | 
| index c9987ca6331d2b1b83eb0dec90978aea00a7dd30..827f153b072b61f812a8a68404b6dc18d37e475e 100644 | 
| --- a/ui/views/controls/textfield/textfield.cc | 
| +++ b/ui/views/controls/textfield/textfield.cc | 
| @@ -274,6 +274,11 @@ Textfield::Textfield() | 
| weak_ptr_factory_(this) { | 
| set_context_menu_controller(this); | 
| set_drag_controller(this); | 
| + cursor_view_.SetPaintToLayer(ui::LAYER_SOLID_COLOR); | 
| + cursor_view_.layer()->SetColor(GetTextColor()); | 
| + // |cursor_view_| is owned by TextField view. | 
| 
msw
2017/02/15 22:50:04
nit: 'Textfield' to match the class name.
 | 
| + cursor_view_.set_owned_by_client(); | 
| + AddChildView(&cursor_view_); | 
| GetRenderText()->SetFontList(GetDefaultFontList()); | 
| View::SetBorder(std::unique_ptr<Border>(new FocusableBorder())); | 
| SetFocusBehavior(FocusBehavior::ALWAYS); | 
| @@ -975,8 +980,10 @@ void Textfield::OnPaint(gfx::Canvas* canvas) { | 
| void Textfield::OnFocus() { | 
| GetRenderText()->set_focused(true); | 
| - if (ShouldShowCursor()) | 
| - GetRenderText()->set_cursor_visible(true); | 
| + if (ShouldShowCursor()) { | 
| + UpdateCursorView(); | 
| + cursor_view_.SetVisible(true); | 
| + } | 
| if (GetInputMethod()) | 
| GetInputMethod()->SetFocusedTextInputClient(this); | 
| OnCaretBoundsChanged(); | 
| @@ -997,10 +1004,7 @@ void Textfield::OnBlur() { | 
| if (GetInputMethod()) | 
| GetInputMethod()->DetachTextInputClient(this); | 
| StopBlinkingCursor(); | 
| - if (render_text->cursor_visible()) { | 
| - render_text->set_cursor_visible(false); | 
| - RepaintCursor(); | 
| - } | 
| + cursor_view_.SetVisible(false); | 
| DestroyTouchSelection(); | 
| @@ -1018,10 +1022,10 @@ void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 
| gfx::RenderText* render_text = GetRenderText(); | 
| render_text->SetColor(GetTextColor()); | 
| UpdateBackgroundColor(); | 
| - render_text->set_cursor_color(GetTextColor()); | 
| render_text->set_selection_color(GetSelectionTextColor()); | 
| render_text->set_selection_background_focused_color( | 
| GetSelectionBackgroundColor()); | 
| + cursor_view_.layer()->SetColor(GetTextColor()); | 
| } | 
| //////////////////////////////////////////////////////////////////////////////// | 
| @@ -1900,8 +1904,8 @@ void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { | 
| NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); | 
| } | 
| if (cursor_changed) { | 
| - GetRenderText()->set_cursor_visible(ShouldShowCursor()); | 
| - RepaintCursor(); | 
| + cursor_view_.SetVisible(ShouldShowCursor()); | 
| + UpdateCursorView(); | 
| if (ShouldBlinkCursor()) | 
| StartBlinkingCursor(); | 
| else | 
| @@ -1914,10 +1918,8 @@ void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { | 
| } | 
| } | 
| -void Textfield::RepaintCursor() { | 
| - gfx::Rect r(GetRenderText()->GetUpdatedCursorBounds()); | 
| - r.Inset(-1, -1, -1, -1); | 
| - SchedulePaintInRect(r); | 
| +void Textfield::UpdateCursorView() { | 
| + cursor_view_.SetBoundsRect(GetRenderText()->GetUpdatedCursorBounds()); | 
| } | 
| void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { | 
| @@ -1937,8 +1939,10 @@ void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { | 
| render_text->Draw(canvas); | 
| // Draw the detached drop cursor that marks where the text will be dropped. | 
| - if (drop_cursor_visible_) | 
| - render_text->DrawCursor(canvas, drop_cursor_position_); | 
| + if (drop_cursor_visible_) { | 
| + canvas->FillRect(render_text->GetCursorBounds(drop_cursor_position_, true), | 
| + GetTextColor()); | 
| + } | 
| canvas->Restore(); | 
| } | 
| @@ -2076,9 +2080,8 @@ void Textfield::StopBlinkingCursor() { | 
| void Textfield::OnCursorBlinkTimerFired() { | 
| DCHECK(ShouldBlinkCursor()); | 
| - gfx::RenderText* render_text = GetRenderText(); | 
| - render_text->set_cursor_visible(!render_text->cursor_visible()); | 
| - RepaintCursor(); | 
| + cursor_view_.SetVisible(!cursor_view_.visible()); | 
| + UpdateCursorView(); | 
| } | 
| } // namespace views |