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

Side by Side Diff: ui/views/controls/textfield/textfield.cc

Issue 2729133005: Fix: Cursor missing in omnibox after entering a alphabet in NTP 'Search box' (Closed)
Patch Set: address comments Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/textfield/textfield.h" 5 #include "ui/views/controls/textfield/textfield.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 865 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 // Adjust the drop destination if it is on or after the current selection. 876 // Adjust the drop destination if it is on or after the current selection.
877 size_t pos = drop_destination_model.caret_pos(); 877 size_t pos = drop_destination_model.caret_pos();
878 pos -= render_text->selection().Intersect(gfx::Range(0, pos)).length(); 878 pos -= render_text->selection().Intersect(gfx::Range(0, pos)).length();
879 model_->DeleteSelectionAndInsertTextAt(new_text, pos); 879 model_->DeleteSelectionAndInsertTextAt(new_text, pos);
880 } else { 880 } else {
881 model_->MoveCursorTo(drop_destination_model); 881 model_->MoveCursorTo(drop_destination_model);
882 // Drop always inserts text even if the textfield is not in insert mode. 882 // Drop always inserts text even if the textfield is not in insert mode.
883 model_->InsertText(new_text); 883 model_->InsertText(new_text);
884 } 884 }
885 skip_input_method_cancel_composition_ = false; 885 skip_input_method_cancel_composition_ = false;
886 OnAfterUserAction();
886 UpdateAfterChange(true, true); 887 UpdateAfterChange(true, true);
887 OnAfterUserAction();
888 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY; 888 return move ? ui::DragDropTypes::DRAG_MOVE : ui::DragDropTypes::DRAG_COPY;
889 } 889 }
890 890
891 void Textfield::OnDragDone() { 891 void Textfield::OnDragDone() {
892 initiating_drag_ = false; 892 initiating_drag_ = false;
893 drop_cursor_visible_ = false; 893 drop_cursor_visible_ = false;
894 } 894 }
895 895
896 void Textfield::GetAccessibleNodeData(ui::AXNodeData* node_data) { 896 void Textfield::GetAccessibleNodeData(ui::AXNodeData* node_data) {
897 node_data->role = ui::AX_ROLE_TEXT_FIELD; 897 node_data->role = ui::AX_ROLE_TEXT_FIELD;
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 // Textfield, ui::TextInputClient overrides: 1256 // Textfield, ui::TextInputClient overrides:
1257 1257
1258 void Textfield::SetCompositionText(const ui::CompositionText& composition) { 1258 void Textfield::SetCompositionText(const ui::CompositionText& composition) {
1259 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) 1259 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
1260 return; 1260 return;
1261 1261
1262 OnBeforeUserAction(); 1262 OnBeforeUserAction();
1263 skip_input_method_cancel_composition_ = true; 1263 skip_input_method_cancel_composition_ = true;
1264 model_->SetCompositionText(composition); 1264 model_->SetCompositionText(composition);
1265 skip_input_method_cancel_composition_ = false; 1265 skip_input_method_cancel_composition_ = false;
1266 OnAfterUserAction();
1266 UpdateAfterChange(true, true); 1267 UpdateAfterChange(true, true);
1267 OnAfterUserAction();
1268 } 1268 }
1269 1269
1270 void Textfield::ConfirmCompositionText() { 1270 void Textfield::ConfirmCompositionText() {
1271 if (!model_->HasCompositionText()) 1271 if (!model_->HasCompositionText())
1272 return; 1272 return;
1273 1273
1274 OnBeforeUserAction(); 1274 OnBeforeUserAction();
1275 skip_input_method_cancel_composition_ = true; 1275 skip_input_method_cancel_composition_ = true;
1276 model_->ConfirmCompositionText(); 1276 model_->ConfirmCompositionText();
1277 skip_input_method_cancel_composition_ = false; 1277 skip_input_method_cancel_composition_ = false;
1278 OnAfterUserAction();
1278 UpdateAfterChange(true, true); 1279 UpdateAfterChange(true, true);
1279 OnAfterUserAction();
1280 } 1280 }
1281 1281
1282 void Textfield::ClearCompositionText() { 1282 void Textfield::ClearCompositionText() {
1283 if (!model_->HasCompositionText()) 1283 if (!model_->HasCompositionText())
1284 return; 1284 return;
1285 1285
1286 OnBeforeUserAction(); 1286 OnBeforeUserAction();
1287 skip_input_method_cancel_composition_ = true; 1287 skip_input_method_cancel_composition_ = true;
1288 model_->CancelCompositionText(); 1288 model_->CancelCompositionText();
1289 skip_input_method_cancel_composition_ = false; 1289 skip_input_method_cancel_composition_ = false;
1290 OnAfterUserAction();
1290 UpdateAfterChange(true, true); 1291 UpdateAfterChange(true, true);
1291 OnAfterUserAction();
1292 } 1292 }
1293 1293
1294 void Textfield::InsertText(const base::string16& new_text) { 1294 void Textfield::InsertText(const base::string16& new_text) {
1295 // TODO(suzhe): Filter invalid characters. 1295 // TODO(suzhe): Filter invalid characters.
1296 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || new_text.empty()) 1296 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || new_text.empty())
1297 return; 1297 return;
1298 1298
1299 OnBeforeUserAction(); 1299 OnBeforeUserAction();
1300 skip_input_method_cancel_composition_ = true; 1300 skip_input_method_cancel_composition_ = true;
1301 model_->InsertText(new_text); 1301 model_->InsertText(new_text);
1302 skip_input_method_cancel_composition_ = false; 1302 skip_input_method_cancel_composition_ = false;
1303 OnAfterUserAction();
1303 UpdateAfterChange(true, true); 1304 UpdateAfterChange(true, true);
1304 OnAfterUserAction();
1305 } 1305 }
1306 1306
1307 void Textfield::InsertChar(const ui::KeyEvent& event) { 1307 void Textfield::InsertChar(const ui::KeyEvent& event) {
1308 if (read_only()) { 1308 if (read_only()) {
1309 OnEditFailed(); 1309 OnEditFailed();
1310 return; 1310 return;
1311 } 1311 }
1312 1312
1313 // Filter out all control characters, including tab and new line characters, 1313 // Filter out all control characters, including tab and new line characters,
1314 // and all characters with Alt modifier (and Search on ChromeOS, Ctrl on 1314 // and all characters with Alt modifier (and Search on ChromeOS, Ctrl on
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 1576
1577 //////////////////////////////////////////////////////////////////////////////// 1577 ////////////////////////////////////////////////////////////////////////////////
1578 // Textfield, protected: 1578 // Textfield, protected:
1579 1579
1580 void Textfield::DoInsertChar(base::char16 ch) { 1580 void Textfield::DoInsertChar(base::char16 ch) {
1581 OnBeforeUserAction(); 1581 OnBeforeUserAction();
1582 skip_input_method_cancel_composition_ = true; 1582 skip_input_method_cancel_composition_ = true;
1583 model_->InsertChar(ch); 1583 model_->InsertChar(ch);
1584 skip_input_method_cancel_composition_ = false; 1584 skip_input_method_cancel_composition_ = false;
1585 1585
1586 OnAfterUserAction();
1586 UpdateAfterChange(true, true); 1587 UpdateAfterChange(true, true);
1587 OnAfterUserAction();
1588 } 1588 }
1589 1589
1590 gfx::RenderText* Textfield::GetRenderText() const { 1590 gfx::RenderText* Textfield::GetRenderText() const {
1591 return model_->render_text(); 1591 return model_->render_text();
1592 } 1592 }
1593 1593
1594 gfx::Point Textfield::GetLastClickLocation() const { 1594 gfx::Point Textfield::GetLastClickLocation() const {
1595 return selection_controller_.last_click_location(); 1595 return selection_controller_.last_click_location();
1596 } 1596 }
1597 1597
1598 base::string16 Textfield::GetSelectionClipboardText() const { 1598 base::string16 Textfield::GetSelectionClipboardText() const {
1599 base::string16 selection_clipboard_text; 1599 base::string16 selection_clipboard_text;
1600 ui::Clipboard::GetForCurrentThread()->ReadText( 1600 ui::Clipboard::GetForCurrentThread()->ReadText(
1601 ui::CLIPBOARD_TYPE_SELECTION, &selection_clipboard_text); 1601 ui::CLIPBOARD_TYPE_SELECTION, &selection_clipboard_text);
1602 return selection_clipboard_text; 1602 return selection_clipboard_text;
1603 } 1603 }
1604 1604
1605 void Textfield::ExecuteTextEditCommand(ui::TextEditCommand command) { 1605 void Textfield::ExecuteTextEditCommand(ui::TextEditCommand command) {
1606 DestroyTouchSelection(); 1606 DestroyTouchSelection();
1607 1607
1608 // We only execute the commands enabled in Textfield::IsTextEditCommandEnabled
1609 // below. Hence don't do a virtual IsTextEditCommandEnabled call.
1610 if (!Textfield::IsTextEditCommandEnabled(command))
1611 return;
1612
1613 gfx::SelectionModel selection_model = GetSelectionModel();
1614 OnBeforeUserAction();
1615
1616 bool text_changed = ExecuteTextEditCommandImpl(command);
1617 bool cursor_changed = text_changed;
1618
1619 cursor_changed |= GetSelectionModel() != selection_model;
1620 if (cursor_changed && HasSelection())
1621 UpdateSelectionClipboard();
1622 OnAfterUserAction();
1623 UpdateAfterChange(text_changed, cursor_changed);
1624 }
1625
1626 bool Textfield::ExecuteTextEditCommandImpl(ui::TextEditCommand command) {
1608 bool add_to_kill_buffer = false; 1627 bool add_to_kill_buffer = false;
1609 1628
1610 // Some codepaths may bypass GetCommandForKeyEvent, so any selection-dependent 1629 // Some codepaths may bypass GetCommandForKeyEvent, so any selection-dependent
1611 // modifications of the command should happen here. 1630 // modifications of the command should happen here.
1612 switch (command) { 1631 switch (command) {
1613 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: 1632 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE:
1614 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH: 1633 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH:
1615 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: 1634 case ui::TextEditCommand::DELETE_TO_END_OF_LINE:
1616 case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH: 1635 case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH:
1617 add_to_kill_buffer = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD; 1636 add_to_kill_buffer = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD;
1618 // Fall through. 1637 // Fall through.
1619 case ui::TextEditCommand::DELETE_WORD_BACKWARD: 1638 case ui::TextEditCommand::DELETE_WORD_BACKWARD:
1620 case ui::TextEditCommand::DELETE_WORD_FORWARD: 1639 case ui::TextEditCommand::DELETE_WORD_FORWARD:
1621 if (HasSelection()) 1640 if (HasSelection())
1622 command = ui::TextEditCommand::DELETE_FORWARD; 1641 command = ui::TextEditCommand::DELETE_FORWARD;
1623 break; 1642 break;
1624 default: 1643 default:
1625 break; 1644 break;
1626 } 1645 }
sadrul 2017/03/09 01:47:38 This part where we may change |command| should mov
yiyix 2017/03/09 02:45:33 Done. It doesn't change the current implementation
1627 1646
1628 // We only execute the commands enabled in Textfield::IsTextEditCommandEnabled
1629 // below. Hence don't do a virtual IsTextEditCommandEnabled call.
1630 if (!Textfield::IsTextEditCommandEnabled(command))
1631 return;
1632
1633 bool text_changed = false;
1634 bool cursor_changed = false;
1635 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; 1647 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
1636 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT; 1648 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT;
1637 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT; 1649 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT;
1638 gfx::SelectionModel selection_model = GetSelectionModel(); 1650 bool is_text_changed = false;
1639 1651
1640 OnBeforeUserAction();
1641 switch (command) { 1652 switch (command) {
1642 case ui::TextEditCommand::DELETE_BACKWARD: 1653 case ui::TextEditCommand::DELETE_BACKWARD:
1643 text_changed = cursor_changed = model_->Backspace(add_to_kill_buffer); 1654 is_text_changed = model_->Backspace(add_to_kill_buffer);
1644 break; 1655 break;
1645 case ui::TextEditCommand::DELETE_FORWARD: 1656 case ui::TextEditCommand::DELETE_FORWARD:
1646 text_changed = cursor_changed = model_->Delete(add_to_kill_buffer); 1657 is_text_changed = model_->Delete(add_to_kill_buffer);
1647 break; 1658 break;
1648 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: 1659 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE:
1649 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH: 1660 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH:
1650 model_->MoveCursor(gfx::LINE_BREAK, begin, gfx::SELECTION_RETAIN); 1661 model_->MoveCursor(gfx::LINE_BREAK, begin, gfx::SELECTION_RETAIN);
1651 text_changed = cursor_changed = model_->Backspace(add_to_kill_buffer); 1662 is_text_changed = model_->Backspace(add_to_kill_buffer);
1652 break; 1663 break;
1653 case ui::TextEditCommand::DELETE_TO_END_OF_LINE: 1664 case ui::TextEditCommand::DELETE_TO_END_OF_LINE:
1654 case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH: 1665 case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH:
1655 model_->MoveCursor(gfx::LINE_BREAK, end, gfx::SELECTION_RETAIN); 1666 model_->MoveCursor(gfx::LINE_BREAK, end, gfx::SELECTION_RETAIN);
1656 text_changed = cursor_changed = model_->Delete(add_to_kill_buffer); 1667 is_text_changed = model_->Delete(add_to_kill_buffer);
1657 break; 1668 break;
1658 case ui::TextEditCommand::DELETE_WORD_BACKWARD: 1669 case ui::TextEditCommand::DELETE_WORD_BACKWARD:
1659 model_->MoveCursor(gfx::WORD_BREAK, begin, gfx::SELECTION_RETAIN); 1670 model_->MoveCursor(gfx::WORD_BREAK, begin, gfx::SELECTION_RETAIN);
1660 text_changed = cursor_changed = model_->Backspace(add_to_kill_buffer); 1671 is_text_changed = model_->Backspace(add_to_kill_buffer);
1661 break; 1672 break;
1662 case ui::TextEditCommand::DELETE_WORD_FORWARD: 1673 case ui::TextEditCommand::DELETE_WORD_FORWARD:
1663 model_->MoveCursor(gfx::WORD_BREAK, end, gfx::SELECTION_RETAIN); 1674 model_->MoveCursor(gfx::WORD_BREAK, end, gfx::SELECTION_RETAIN);
1664 text_changed = cursor_changed = model_->Delete(add_to_kill_buffer); 1675 is_text_changed = model_->Delete(add_to_kill_buffer);
1665 break; 1676 break;
1666 case ui::TextEditCommand::MOVE_BACKWARD: 1677 case ui::TextEditCommand::MOVE_BACKWARD:
1667 model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_NONE); 1678 model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_NONE);
1668 break; 1679 break;
1669 case ui::TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION: 1680 case ui::TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION:
1670 model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_RETAIN); 1681 model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_RETAIN);
1671 break; 1682 break;
1672 case ui::TextEditCommand::MOVE_FORWARD: 1683 case ui::TextEditCommand::MOVE_FORWARD:
1673 model_->MoveCursor(gfx::CHARACTER_BREAK, end, gfx::SELECTION_NONE); 1684 model_->MoveCursor(gfx::CHARACTER_BREAK, end, gfx::SELECTION_NONE);
1674 break; 1685 break;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1754 break; 1765 break;
1755 case ui::TextEditCommand::MOVE_WORD_RIGHT: 1766 case ui::TextEditCommand::MOVE_WORD_RIGHT:
1756 model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT, 1767 model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT,
1757 gfx::SELECTION_NONE); 1768 gfx::SELECTION_NONE);
1758 break; 1769 break;
1759 case ui::TextEditCommand::MOVE_WORD_RIGHT_AND_MODIFY_SELECTION: 1770 case ui::TextEditCommand::MOVE_WORD_RIGHT_AND_MODIFY_SELECTION:
1760 model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT, 1771 model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT,
1761 kWordSelectionBehavior); 1772 kWordSelectionBehavior);
1762 break; 1773 break;
1763 case ui::TextEditCommand::UNDO: 1774 case ui::TextEditCommand::UNDO:
1764 text_changed = cursor_changed = model_->Undo(); 1775 is_text_changed = model_->Undo();
1765 break; 1776 break;
1766 case ui::TextEditCommand::REDO: 1777 case ui::TextEditCommand::REDO:
1767 text_changed = cursor_changed = model_->Redo(); 1778 is_text_changed = model_->Redo();
1768 break; 1779 break;
1769 case ui::TextEditCommand::CUT: 1780 case ui::TextEditCommand::CUT:
1770 text_changed = cursor_changed = Cut(); 1781 is_text_changed = Cut();
1771 break; 1782 break;
1772 case ui::TextEditCommand::COPY: 1783 case ui::TextEditCommand::COPY:
1773 Copy(); 1784 Copy();
1774 break; 1785 break;
1775 case ui::TextEditCommand::PASTE: 1786 case ui::TextEditCommand::PASTE:
1776 text_changed = cursor_changed = Paste(); 1787 is_text_changed = Paste();
1777 break; 1788 break;
1778 case ui::TextEditCommand::SELECT_ALL: 1789 case ui::TextEditCommand::SELECT_ALL:
1779 SelectAll(false); 1790 SelectAll(false);
1780 break; 1791 break;
1781 case ui::TextEditCommand::TRANSPOSE: 1792 case ui::TextEditCommand::TRANSPOSE:
1782 text_changed = cursor_changed = model_->Transpose(); 1793 is_text_changed = model_->Transpose();
1783 break; 1794 break;
1784 case ui::TextEditCommand::YANK: 1795 case ui::TextEditCommand::YANK:
1785 text_changed = cursor_changed = model_->Yank(); 1796 is_text_changed = model_->Yank();
1786 break; 1797 break;
1787 case ui::TextEditCommand::INSERT_TEXT: 1798 case ui::TextEditCommand::INSERT_TEXT:
1788 case ui::TextEditCommand::SET_MARK: 1799 case ui::TextEditCommand::SET_MARK:
1789 case ui::TextEditCommand::UNSELECT: 1800 case ui::TextEditCommand::UNSELECT:
1790 case ui::TextEditCommand::INVALID_COMMAND: 1801 case ui::TextEditCommand::INVALID_COMMAND:
1791 NOTREACHED(); 1802 NOTREACHED();
1792 break; 1803 break;
1793 } 1804 }
1794 1805 return is_text_changed;
1795 cursor_changed |= GetSelectionModel() != selection_model;
1796 if (cursor_changed && HasSelection())
1797 UpdateSelectionClipboard();
1798 UpdateAfterChange(text_changed, cursor_changed);
1799 OnAfterUserAction();
1800 } 1806 }
1801 1807
1802 //////////////////////////////////////////////////////////////////////////////// 1808 ////////////////////////////////////////////////////////////////////////////////
1803 // Textfield, private: 1809 // Textfield, private:
1804 1810
1805 //////////////////////////////////////////////////////////////////////////////// 1811 ////////////////////////////////////////////////////////////////////////////////
1806 // Textfield, SelectionControllerDelegate overrides: 1812 // Textfield, SelectionControllerDelegate overrides:
1807 1813
1808 gfx::RenderText* Textfield::GetRenderTextForSelectionController() { 1814 gfx::RenderText* Textfield::GetRenderTextForSelectionController() {
1809 return GetRenderText(); 1815 return GetRenderText();
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
2086 cursor_blink_timer_.Stop(); 2092 cursor_blink_timer_.Stop();
2087 } 2093 }
2088 2094
2089 void Textfield::OnCursorBlinkTimerFired() { 2095 void Textfield::OnCursorBlinkTimerFired() {
2090 DCHECK(ShouldBlinkCursor()); 2096 DCHECK(ShouldBlinkCursor());
2091 cursor_view_.SetVisible(!cursor_view_.visible()); 2097 cursor_view_.SetVisible(!cursor_view_.visible());
2092 UpdateCursorView(); 2098 UpdateCursorView();
2093 } 2099 }
2094 2100
2095 } // namespace views 2101 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698