Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 switch (ui::ScopedAnimationDurationScaleMode::duration_scale_mode()) { | 76 switch (ui::ScopedAnimationDurationScaleMode::duration_scale_mode()) { |
| 77 case ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION: return 100; | 77 case ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION: return 100; |
| 78 case ui::ScopedAnimationDurationScaleMode::FAST_DURATION: return 25; | 78 case ui::ScopedAnimationDurationScaleMode::FAST_DURATION: return 25; |
| 79 case ui::ScopedAnimationDurationScaleMode::SLOW_DURATION: return 400; | 79 case ui::ScopedAnimationDurationScaleMode::SLOW_DURATION: return 400; |
| 80 case ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION: return 1; | 80 case ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION: return 1; |
| 81 case ui::ScopedAnimationDurationScaleMode::ZERO_DURATION: return 0; | 81 case ui::ScopedAnimationDurationScaleMode::ZERO_DURATION: return 0; |
| 82 } | 82 } |
| 83 return 100; | 83 return 100; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Get the default command for a given key |event| and selection state. | 86 // Get the default command for a given key |event|. |
| 87 int GetCommandForKeyEvent(const ui::KeyEvent& event, bool has_selection) { | 87 int GetCommandForKeyEvent(const ui::KeyEvent& event) { |
| 88 if (event.type() != ui::ET_KEY_PRESSED || event.IsUnicodeKeyCode()) | 88 if (event.type() != ui::ET_KEY_PRESSED || event.IsUnicodeKeyCode()) |
| 89 return kNoCommand; | 89 return kNoCommand; |
| 90 | 90 |
| 91 const bool shift = event.IsShiftDown(); | 91 const bool shift = event.IsShiftDown(); |
| 92 const bool control = event.IsControlDown(); | 92 const bool control = event.IsControlDown(); |
| 93 const bool alt = event.IsAltDown() || event.IsAltGrDown(); | 93 const bool alt = event.IsAltDown() || event.IsAltGrDown(); |
| 94 switch (event.key_code()) { | 94 switch (event.key_code()) { |
| 95 case ui::VKEY_Z: | 95 case ui::VKEY_Z: |
| 96 if (control && !shift && !alt) | 96 if (control && !shift && !alt) |
| 97 return IDS_APP_UNDO; | 97 return IDS_APP_UNDO; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 122 return control ? IDS_MOVE_WORD_LEFT : IDS_MOVE_LEFT; | 122 return control ? IDS_MOVE_WORD_LEFT : IDS_MOVE_LEFT; |
| 123 return control ? IDS_MOVE_WORD_LEFT_AND_MODIFY_SELECTION : | 123 return control ? IDS_MOVE_WORD_LEFT_AND_MODIFY_SELECTION : |
| 124 IDS_MOVE_LEFT_AND_MODIFY_SELECTION; | 124 IDS_MOVE_LEFT_AND_MODIFY_SELECTION; |
| 125 case ui::VKEY_HOME: | 125 case ui::VKEY_HOME: |
| 126 return shift ? IDS_MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION : | 126 return shift ? IDS_MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION : |
| 127 IDS_MOVE_TO_BEGINNING_OF_LINE; | 127 IDS_MOVE_TO_BEGINNING_OF_LINE; |
| 128 case ui::VKEY_END: | 128 case ui::VKEY_END: |
| 129 return shift ? IDS_MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION : | 129 return shift ? IDS_MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION : |
| 130 IDS_MOVE_TO_END_OF_LINE; | 130 IDS_MOVE_TO_END_OF_LINE; |
| 131 case ui::VKEY_BACK: | 131 case ui::VKEY_BACK: |
| 132 if (!control || has_selection) | 132 if (!control) |
| 133 return IDS_DELETE_BACKWARD; | 133 return IDS_DELETE_BACKWARD; |
| 134 #if defined(OS_LINUX) | 134 #if defined(OS_LINUX) |
| 135 // Only erase by line break on Linux and ChromeOS. | 135 // Only erase by line break on Linux and ChromeOS. |
| 136 if (shift) | 136 if (shift) |
| 137 return IDS_DELETE_TO_BEGINNING_OF_LINE; | 137 return IDS_DELETE_TO_BEGINNING_OF_LINE; |
| 138 #endif | 138 #endif |
| 139 return IDS_DELETE_WORD_BACKWARD; | 139 return IDS_DELETE_WORD_BACKWARD; |
| 140 case ui::VKEY_DELETE: | 140 case ui::VKEY_DELETE: |
|
msw
2016/04/28 18:22:02
Consider rewriting this case a little bit for simp
karandeepb
2016/04/29 01:40:59
Done.
| |
| 141 if (!control || has_selection) | 141 if (shift) { |
| 142 return (shift && has_selection) ? IDS_APP_CUT : IDS_DELETE_FORWARD; | |
| 143 #if defined(OS_LINUX) | 142 #if defined(OS_LINUX) |
| 144 // Only erase by line break on Linux and ChromeOS. | 143 // Only erase by line break on Linux and ChromeOS. |
| 145 if (shift) | 144 if (control) |
| 146 return IDS_DELETE_TO_END_OF_LINE; | 145 return IDS_DELETE_TO_END_OF_LINE; |
| 147 #endif | 146 #endif |
| 148 return IDS_DELETE_WORD_FORWARD; | 147 return control ? IDS_DELETE_WORD_FORWARD : IDS_APP_CUT; |
|
msw
2016/04/28 18:22:02
It's weird to return IDS_APP_CUT without knowing i
Peter Kasting
2016/04/28 19:09:18
Shift-delete with no selection should no-op. This
msw
2016/04/28 19:17:43
Okay, but note that Shift-Backspace acts as simply
Peter Kasting
2016/04/28 19:21:48
Shift-delete is normally "cut" whereas shift-backs
msw
2016/04/28 19:23:41
Okay, sounds good to me!
| |
| 148 } | |
| 149 return control ? IDS_DELETE_WORD_FORWARD : IDS_DELETE_FORWARD; | |
| 149 case ui::VKEY_INSERT: | 150 case ui::VKEY_INSERT: |
| 150 if (control && !shift) | 151 if (control && !shift) |
| 151 return IDS_APP_COPY; | 152 return IDS_APP_COPY; |
| 152 return (shift && !control) ? IDS_APP_PASTE : kNoCommand; | 153 return (shift && !control) ? IDS_APP_PASTE : kNoCommand; |
| 153 default: | 154 default: |
| 154 return kNoCommand; | 155 return kNoCommand; |
| 155 } | 156 } |
| 156 } | 157 } |
| 157 | 158 |
| 158 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 159 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| (...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 if (IsCommandIdEnabled(command)) { | 724 if (IsCommandIdEnabled(command)) { |
| 724 ExecuteCommand(command); | 725 ExecuteCommand(command); |
| 725 handled = true; | 726 handled = true; |
| 726 } | 727 } |
| 727 } | 728 } |
| 728 return handled; | 729 return handled; |
| 729 } | 730 } |
| 730 #endif | 731 #endif |
| 731 | 732 |
| 732 if (edit_command == kNoCommand) | 733 if (edit_command == kNoCommand) |
| 733 edit_command = GetCommandForKeyEvent(event, HasSelection()); | 734 edit_command = GetCommandForKeyEvent(event); |
| 734 | 735 |
| 735 if (!handled && IsCommandIdEnabled(edit_command)) { | 736 if (!handled && IsCommandIdEnabled(edit_command)) { |
| 736 ExecuteCommand(edit_command); | 737 ExecuteCommand(edit_command); |
| 737 handled = true; | 738 handled = true; |
| 738 } | 739 } |
| 739 return handled; | 740 return handled; |
| 740 } | 741 } |
| 741 | 742 |
| 742 void Textfield::OnGestureEvent(ui::GestureEvent* event) { | 743 void Textfield::OnGestureEvent(ui::GestureEvent* event) { |
| 743 switch (event->type()) { | 744 switch (event->type()) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 827 break; | 828 break; |
| 828 default: | 829 default: |
| 829 return; | 830 return; |
| 830 } | 831 } |
| 831 } | 832 } |
| 832 | 833 |
| 833 // This function is called by BrowserView to execute clipboard commands. | 834 // This function is called by BrowserView to execute clipboard commands. |
| 834 bool Textfield::AcceleratorPressed(const ui::Accelerator& accelerator) { | 835 bool Textfield::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 835 ui::KeyEvent event(accelerator.type(), accelerator.key_code(), | 836 ui::KeyEvent event(accelerator.type(), accelerator.key_code(), |
| 836 accelerator.modifiers()); | 837 accelerator.modifiers()); |
| 837 ExecuteCommand(GetCommandForKeyEvent(event, HasSelection())); | 838 ExecuteCommand(GetCommandForKeyEvent(event)); |
| 838 return true; | 839 return true; |
| 839 } | 840 } |
| 840 | 841 |
| 841 bool Textfield::CanHandleAccelerators() const { | 842 bool Textfield::CanHandleAccelerators() const { |
| 842 return GetRenderText()->focused() && View::CanHandleAccelerators(); | 843 return GetRenderText()->focused() && View::CanHandleAccelerators(); |
| 843 } | 844 } |
| 844 | 845 |
| 845 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { | 846 void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) { |
| 846 SelectAll(false); | 847 SelectAll(false); |
| 847 } | 848 } |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1296 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); | 1297 *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_CONTROL_DOWN); |
| 1297 return true; | 1298 return true; |
| 1298 | 1299 |
| 1299 default: | 1300 default: |
| 1300 return false; | 1301 return false; |
| 1301 } | 1302 } |
| 1302 } | 1303 } |
| 1303 | 1304 |
| 1304 void Textfield::ExecuteCommand(int command_id, int event_flags) { | 1305 void Textfield::ExecuteCommand(int command_id, int event_flags) { |
| 1305 DestroyTouchSelection(); | 1306 DestroyTouchSelection(); |
| 1307 | |
| 1308 if (HasSelection()) { | |
|
msw
2016/04/28 18:22:02
nit: add an explanatory comment that Linux and Mac
karandeepb
2016/04/29 01:40:59
Done.
| |
| 1309 switch (command_id) { | |
| 1310 case IDS_DELETE_WORD_BACKWARD: | |
| 1311 case IDS_DELETE_TO_BEGINNING_OF_LINE: | |
| 1312 command_id = IDS_DELETE_BACKWARD; | |
|
msw
2016/04/28 18:22:02
It seems a little odd that this function would sup
karandeepb
2016/04/29 01:40:59
Yeah this should be easy to do on Mac at least. Bu
| |
| 1313 break; | |
| 1314 case IDS_DELETE_WORD_FORWARD: | |
| 1315 case IDS_DELETE_TO_END_OF_LINE: | |
| 1316 command_id = IDS_DELETE_FORWARD; | |
| 1317 break; | |
| 1318 } | |
| 1319 } | |
| 1320 | |
| 1306 if (!IsCommandIdEnabled(command_id)) | 1321 if (!IsCommandIdEnabled(command_id)) |
| 1307 return; | 1322 return; |
| 1308 | 1323 |
| 1309 bool text_changed = false; | 1324 bool text_changed = false; |
| 1310 bool cursor_changed = false; | 1325 bool cursor_changed = false; |
| 1311 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; | 1326 bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; |
| 1312 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT; | 1327 gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT; |
| 1313 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT; | 1328 gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT; |
| 1314 gfx::SelectionModel selection_model = GetSelectionModel(); | 1329 gfx::SelectionModel selection_model = GetSelectionModel(); |
| 1315 | 1330 |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1913 RequestFocus(); | 1928 RequestFocus(); |
| 1914 model_->MoveCursorTo(mouse); | 1929 model_->MoveCursorTo(mouse); |
| 1915 if (!selection_clipboard_text.empty()) { | 1930 if (!selection_clipboard_text.empty()) { |
| 1916 model_->InsertText(selection_clipboard_text); | 1931 model_->InsertText(selection_clipboard_text); |
| 1917 UpdateAfterChange(true, true); | 1932 UpdateAfterChange(true, true); |
| 1918 } | 1933 } |
| 1919 OnAfterUserAction(); | 1934 OnAfterUserAction(); |
| 1920 } | 1935 } |
| 1921 | 1936 |
| 1922 } // namespace views | 1937 } // namespace views |
| OLD | NEW |