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

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

Issue 5972008: views: Improve cursor movements on word boundaries. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add a test with leading whitespace. Created 9 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « views/controls/textfield/native_textfield_views_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "views/controls/textfield/textfield_views_model.h" 5 #include "views/controls/textfield/textfield_views_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/i18n/break_iterator.h" 9 #include "base/i18n/break_iterator.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 void TextfieldViewsModel::MoveCursorToPreviousWord(bool select) { 125 void TextfieldViewsModel::MoveCursorToPreviousWord(bool select) {
126 // Notes: We always iterate words from the begining. 126 // Notes: We always iterate words from the begining.
127 // This is probably fast enough for our usage, but we may 127 // This is probably fast enough for our usage, but we may
128 // want to modify WordIterator so that it can start from the 128 // want to modify WordIterator so that it can start from the
129 // middle of string and advance backwards. 129 // middle of string and advance backwards.
130 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); 130 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD);
131 bool success = iter.Init(); 131 bool success = iter.Init();
132 DCHECK(success); 132 DCHECK(success);
133 if (!success) 133 if (!success)
134 return; 134 return;
135 int prev = 0; 135 int last = 0;
136 while (iter.Advance()) { 136 while (iter.Advance()) {
137 if (iter.IsWord()) { 137 if (iter.IsWord()) {
138 size_t begin = iter.pos() - iter.GetString().length(); 138 size_t begin = iter.pos() - iter.GetString().length();
139 if (begin == cursor_pos_) { 139 if (begin == cursor_pos_) {
140 // The cursor is at the beginning of a word. 140 // The cursor is at the beginning of a word.
141 // Move to previous word. 141 // Move to previous word.
142 cursor_pos_ = prev; 142 break;
143 } else if(iter.pos() >= cursor_pos_) { 143 } else if(iter.pos() >= cursor_pos_) {
144 // The cursor is in the middle or at the end of a word. 144 // The cursor is in the middle or at the end of a word.
145 // Move to the top of current word. 145 // Move to the top of current word.
146 cursor_pos_ = begin; 146 last = begin;
147 break;
147 } else { 148 } else {
148 prev = iter.pos() - iter.GetString().length(); 149 last = iter.pos() - iter.GetString().length();
149 continue;
150 } 150 }
151 if (!select)
152 ClearSelection();
153 break;
154 } 151 }
155 } 152 }
153
154 cursor_pos_ = last;
155 if (!select)
156 ClearSelection();
156 } 157 }
157 158
158 void TextfieldViewsModel::MoveCursorToNextWord(bool select) { 159 void TextfieldViewsModel::MoveCursorToNextWord(bool select) {
159 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); 160 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD);
160 bool success = iter.Init(); 161 bool success = iter.Init();
161 DCHECK(success); 162 DCHECK(success);
162 if (!success) 163 if (!success)
163 return; 164 return;
165 size_t pos = 0;
164 while (iter.Advance()) { 166 while (iter.Advance()) {
165 if (iter.IsWord() && iter.pos() > cursor_pos_) { 167 pos = iter.pos();
166 cursor_pos_ = iter.pos(); 168 if (iter.IsWord() && pos > cursor_pos_) {
167 if (!select)
168 ClearSelection();
169 break; 169 break;
170 } 170 }
171 } 171 }
172 cursor_pos_ = pos;
173 if (!select)
174 ClearSelection();
172 } 175 }
173 176
174 void TextfieldViewsModel::MoveCursorToStart(bool select) { 177 void TextfieldViewsModel::MoveCursorToStart(bool select) {
175 cursor_pos_ = 0; 178 cursor_pos_ = 0;
176 if (!select) 179 if (!select)
177 ClearSelection(); 180 ClearSelection();
178 } 181 }
179 182
180 void TextfieldViewsModel::MoveCursorToEnd(bool select) { 183 void TextfieldViewsModel::MoveCursorToEnd(bool select) {
181 cursor_pos_ = text_.length(); 184 cursor_pos_ = text_.length();
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 240
238 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { 241 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const {
239 DCHECK(end >= begin); 242 DCHECK(end >= begin);
240 if (is_password_) { 243 if (is_password_) {
241 return string16(end - begin, '*'); 244 return string16(end - begin, '*');
242 } 245 }
243 return text_.substr(begin, end - begin); 246 return text_.substr(begin, end - begin);
244 } 247 }
245 248
246 } // namespace views 249 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/textfield/native_textfield_views_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698