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

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

Issue 15684004: Refine Views textfield state interaction, testing, and examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add and expand on comments. Created 7 years, 7 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 | « no previous file | ui/views/controls/textfield/native_textfield_views_unittest.cc » ('j') | 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/native_textfield_views.h" 5 #include "ui/views/controls/textfield/native_textfield_views.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 1150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 } 1161 }
1162 1162
1163 bool NativeTextfieldViews::HandleKeyEvent(const ui::KeyEvent& key_event) { 1163 bool NativeTextfieldViews::HandleKeyEvent(const ui::KeyEvent& key_event) {
1164 // TODO(oshima): Refactor and consolidate with ExecuteCommand. 1164 // TODO(oshima): Refactor and consolidate with ExecuteCommand.
1165 if (key_event.type() == ui::ET_KEY_PRESSED) { 1165 if (key_event.type() == ui::ET_KEY_PRESSED) {
1166 ui::KeyboardCode key_code = key_event.key_code(); 1166 ui::KeyboardCode key_code = key_event.key_code();
1167 if (key_code == ui::VKEY_TAB || key_event.IsUnicodeKeyCode()) 1167 if (key_code == ui::VKEY_TAB || key_event.IsUnicodeKeyCode())
1168 return false; 1168 return false;
1169 1169
1170 OnBeforeUserAction(); 1170 OnBeforeUserAction();
1171 bool editable = !textfield_->read_only(); 1171 const bool editable = !textfield_->read_only();
1172 bool readable = !textfield_->IsObscured(); 1172 const bool readable = !textfield_->IsObscured();
1173 bool shift = key_event.IsShiftDown(); 1173 const bool shift = key_event.IsShiftDown();
1174 bool control = key_event.IsControlDown(); 1174 const bool control = key_event.IsControlDown();
1175 const bool alt = key_event.IsAltDown();
1175 bool text_changed = false; 1176 bool text_changed = false;
1176 bool cursor_changed = false; 1177 bool cursor_changed = false;
1177 switch (key_code) { 1178 switch (key_code) {
1178 case ui::VKEY_Z: 1179 case ui::VKEY_Z:
1179 if (control && !shift && editable) 1180 if (control && !shift && !alt && editable)
1180 cursor_changed = text_changed = model_->Undo(); 1181 cursor_changed = text_changed = model_->Undo();
1181 else if (control && shift && editable) 1182 else if (control && shift && !alt && editable)
1182 cursor_changed = text_changed = model_->Redo(); 1183 cursor_changed = text_changed = model_->Redo();
1183 break; 1184 break;
1184 case ui::VKEY_Y: 1185 case ui::VKEY_Y:
1185 if (control && editable) 1186 if (control && !alt && editable)
1186 cursor_changed = text_changed = model_->Redo(); 1187 cursor_changed = text_changed = model_->Redo();
1187 break; 1188 break;
1188 case ui::VKEY_A: 1189 case ui::VKEY_A:
1189 if (control) { 1190 if (control && !alt) {
1190 model_->SelectAll(false); 1191 model_->SelectAll(false);
1191 cursor_changed = true; 1192 cursor_changed = true;
1192 } 1193 }
1193 break; 1194 break;
1194 case ui::VKEY_X: 1195 case ui::VKEY_X:
1195 if (control && editable && readable) 1196 if (control && !alt && editable && readable)
1196 cursor_changed = text_changed = Cut(); 1197 cursor_changed = text_changed = Cut();
1197 break; 1198 break;
1198 case ui::VKEY_C: 1199 case ui::VKEY_C:
1199 if (control && readable) 1200 if (control && !alt && readable)
1200 Copy(); 1201 Copy();
1201 break; 1202 break;
1202 case ui::VKEY_V: 1203 case ui::VKEY_V:
1203 if (control && editable) 1204 if (control && !alt && editable)
1204 cursor_changed = text_changed = Paste(); 1205 cursor_changed = text_changed = Paste();
1205 break; 1206 break;
1206 case ui::VKEY_RIGHT: 1207 case ui::VKEY_RIGHT:
1207 case ui::VKEY_LEFT: { 1208 case ui::VKEY_LEFT: {
1208 // We should ignore the alt-left/right keys because alt key doesn't make 1209 // We should ignore the alt-left/right keys because alt key doesn't make
1209 // any special effects for them and they can be shortcut keys such like 1210 // any special effects for them and they can be shortcut keys such like
1210 // forward/back of the browser history. 1211 // forward/back of the browser history.
1211 if (key_event.IsAltDown()) 1212 if (alt)
1212 break; 1213 break;
1213 size_t cursor_position = model_->GetCursorPosition(); 1214 size_t cursor_position = model_->GetCursorPosition();
1214 model_->MoveCursor( 1215 model_->MoveCursor(
1215 control ? gfx::WORD_BREAK : gfx::CHARACTER_BREAK, 1216 control ? gfx::WORD_BREAK : gfx::CHARACTER_BREAK,
1216 (key_code == ui::VKEY_RIGHT) ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT, 1217 (key_code == ui::VKEY_RIGHT) ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT,
1217 shift); 1218 shift);
1218 cursor_changed = model_->GetCursorPosition() != cursor_position; 1219 cursor_changed = model_->GetCursorPosition() != cursor_position;
1219 break; 1220 break;
1220 } 1221 }
1221 case ui::VKEY_END: 1222 case ui::VKEY_END:
(...skipping 29 matching lines...) Expand all
1251 model_->Backspace(); 1252 model_->Backspace();
1252 else 1253 else
1253 model_->Delete(); 1254 model_->Delete();
1254 1255
1255 // We have to consume the backspace/delete keys here even if the edit 1256 // We have to consume the backspace/delete keys here even if the edit
1256 // did not make effects. This is to prevent further handling of the key 1257 // did not make effects. This is to prevent further handling of the key
1257 // event that might have unintended side-effects. 1258 // event that might have unintended side-effects.
1258 text_changed = true; 1259 text_changed = true;
1259 break; 1260 break;
1260 case ui::VKEY_INSERT: 1261 case ui::VKEY_INSERT:
1261 if (control && !shift) 1262 if (control && !shift && readable)
1262 Copy(); 1263 Copy();
1263 else if (shift && !control) 1264 else if (shift && !control && editable)
1264 cursor_changed = text_changed = Paste(); 1265 cursor_changed = text_changed = Paste();
1265 break; 1266 break;
1266 default: 1267 default:
1267 break; 1268 break;
1268 } 1269 }
1269 1270
1270 // We must have input method in order to support text input. 1271 // We must have input method in order to support text input.
1271 DCHECK(textfield_->GetInputMethod()); 1272 DCHECK(textfield_->GetInputMethod());
1272 1273
1273 UpdateAfterChange(text_changed, cursor_changed); 1274 UpdateAfterChange(text_changed, cursor_changed);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1356 controller->OnBeforeUserAction(textfield_); 1357 controller->OnBeforeUserAction(textfield_);
1357 } 1358 }
1358 1359
1359 void NativeTextfieldViews::OnAfterUserAction() { 1360 void NativeTextfieldViews::OnAfterUserAction() {
1360 TextfieldController* controller = textfield_->GetController(); 1361 TextfieldController* controller = textfield_->GetController();
1361 if (controller) 1362 if (controller)
1362 controller->OnAfterUserAction(textfield_); 1363 controller->OnAfterUserAction(textfield_);
1363 } 1364 }
1364 1365
1365 bool NativeTextfieldViews::Cut() { 1366 bool NativeTextfieldViews::Cut() {
1366 if (model_->Cut()) { 1367 if (!textfield_->read_only() && !textfield_->IsObscured() && model_->Cut()) {
1367 TextfieldController* controller = textfield_->GetController(); 1368 TextfieldController* controller = textfield_->GetController();
1368 if (controller) 1369 if (controller)
1369 controller->OnAfterCutOrCopy(); 1370 controller->OnAfterCutOrCopy();
1370 return true; 1371 return true;
1371 } 1372 }
1372 return false; 1373 return false;
1373 } 1374 }
1374 1375
1375 bool NativeTextfieldViews::Copy() { 1376 bool NativeTextfieldViews::Copy() {
1376 if (model_->Copy()) { 1377 if (!textfield_->IsObscured() && model_->Copy()) {
1377 TextfieldController* controller = textfield_->GetController(); 1378 TextfieldController* controller = textfield_->GetController();
1378 if (controller) 1379 if (controller)
1379 controller->OnAfterCutOrCopy(); 1380 controller->OnAfterCutOrCopy();
1380 return true; 1381 return true;
1381 } 1382 }
1382 return false; 1383 return false;
1383 } 1384 }
1384 1385
1385 bool NativeTextfieldViews::Paste() { 1386 bool NativeTextfieldViews::Paste() {
1387 if (textfield_->read_only())
1388 return false;
1389
1386 const string16 original_text = GetText(); 1390 const string16 original_text = GetText();
1387 const bool success = model_->Paste(); 1391 const bool success = model_->Paste();
1388 1392
1389 if (success) { 1393 if (success) {
1390 // As Paste is handled in model_->Paste(), the RenderText may contain 1394 // As Paste is handled in model_->Paste(), the RenderText may contain
1391 // upper case characters. This is not consistent with other places 1395 // upper case characters. This is not consistent with other places
1392 // which keeps RenderText only containing lower case characters. 1396 // which keeps RenderText only containing lower case characters.
1393 string16 new_text = GetTextForDisplay(GetText()); 1397 string16 new_text = GetTextForDisplay(GetText());
1394 model_->SetText(new_text); 1398 model_->SetText(new_text);
1395 1399
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1478 1482
1479 void NativeTextfieldViews::PlatformGestureEventHandling( 1483 void NativeTextfieldViews::PlatformGestureEventHandling(
1480 const ui::GestureEvent* event) { 1484 const ui::GestureEvent* event) {
1481 #if defined(OS_WIN) && defined(USE_AURA) 1485 #if defined(OS_WIN) && defined(USE_AURA)
1482 if (event->type() == ui::ET_GESTURE_TAP_DOWN && !textfield_->read_only()) 1486 if (event->type() == ui::ET_GESTURE_TAP_DOWN && !textfield_->read_only())
1483 base::win::DisplayVirtualKeyboard(); 1487 base::win::DisplayVirtualKeyboard();
1484 #endif 1488 #endif
1485 } 1489 }
1486 1490
1487 } // namespace views 1491 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/textfield/native_textfield_views_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698