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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 // Assume CJK + Thai users are using an IME. | 124 // Assume CJK + Thai users are using an IME. |
| 125 if (language == "ja" || | 125 if (language == "ja" || |
| 126 language == "ko" || | 126 language == "ko" || |
| 127 language == "th" || | 127 language == "th" || |
| 128 language == "zh") | 128 language == "zh") |
| 129 return ui::TEXT_INPUT_TYPE_SEARCH; | 129 return ui::TEXT_INPUT_TYPE_SEARCH; |
| 130 #endif | 130 #endif |
| 131 return ui::TEXT_INPUT_TYPE_URL; | 131 return ui::TEXT_INPUT_TYPE_URL; |
| 132 } | 132 } |
| 133 | 133 |
| 134 ui::TextEditCommand GetCommandForKeyEvent(const ui::KeyEvent& event) { | |
| 135 const bool shift = event.IsShiftDown(); | |
| 136 const bool control = event.IsControlDown(); | |
| 137 const bool alt = event.IsAltDown() || event.IsAltGrDown(); | |
| 138 switch (event.key_code()) { | |
| 139 case ui::VKEY_UP: | |
| 140 return ui::TextEditCommand::MOVE_UP; | |
| 141 case ui::VKEY_DOWN: | |
| 142 return ui::TextEditCommand::MOVE_DOWN; | |
| 143 case ui::VKEY_PRIOR: | |
| 144 if (!(control || alt || shift)) | |
| 145 return ui::TextEditCommand::MOVE_PAGE_UP; | |
| 146 break; | |
| 147 case ui::VKEY_NEXT: | |
| 148 if (!(control || alt || shift)) | |
| 149 return ui::TextEditCommand::MOVE_PAGE_DOWN; | |
| 150 break; | |
| 151 case ui::VKEY_V: | |
| 152 if (control && !alt) | |
| 153 return ui::TextEditCommand::PASTE; | |
| 154 break; | |
| 155 case ui::VKEY_INSERT: | |
| 156 if (shift && !control) | |
| 157 return ui::TextEditCommand::PASTE; | |
| 158 break; | |
| 159 default: | |
| 160 break; | |
| 161 } | |
| 162 return ui::TextEditCommand::INVALID_COMMAND; | |
| 163 } | |
| 164 | |
| 134 } // namespace | 165 } // namespace |
| 135 | 166 |
| 136 | 167 |
| 137 // OmniboxViewViews ----------------------------------------------------------- | 168 // OmniboxViewViews ----------------------------------------------------------- |
| 138 | 169 |
| 139 // static | 170 // static |
| 140 const char OmniboxViewViews::kViewClassName[] = "OmniboxViewViews"; | 171 const char OmniboxViewViews::kViewClassName[] = "OmniboxViewViews"; |
| 141 | 172 |
| 142 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, | 173 OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, |
| 143 Profile* profile, | 174 Profile* profile, |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 // These commands don't invoke the popup via OnBefore/AfterPossibleChange(). | 391 // These commands don't invoke the popup via OnBefore/AfterPossibleChange(). |
| 361 case IDS_PASTE_AND_GO: | 392 case IDS_PASTE_AND_GO: |
| 362 model()->PasteAndGo(GetClipboardText()); | 393 model()->PasteAndGo(GetClipboardText()); |
| 363 return; | 394 return; |
| 364 case IDS_SHOW_URL: | 395 case IDS_SHOW_URL: |
| 365 controller()->ShowURL(); | 396 controller()->ShowURL(); |
| 366 return; | 397 return; |
| 367 case IDC_EDIT_SEARCH_ENGINES: | 398 case IDC_EDIT_SEARCH_ENGINES: |
| 368 location_bar_view_->command_updater()->ExecuteCommand(command_id); | 399 location_bar_view_->command_updater()->ExecuteCommand(command_id); |
| 369 return; | 400 return; |
| 370 case IDS_MOVE_DOWN: | |
| 371 case IDS_MOVE_UP: | |
| 372 model()->OnUpOrDownKeyPressed(command_id == IDS_MOVE_DOWN ? 1 : -1); | |
| 373 return; | |
| 374 | |
| 375 // These commands do invoke the popup. | 401 // These commands do invoke the popup. |
| 376 case IDS_APP_PASTE: | 402 case IDS_APP_PASTE: |
| 377 OnPaste(); | 403 ExecuteEditCommand(ui::TextEditCommand::PASTE); |
| 378 return; | 404 return; |
| 379 default: | 405 default: |
| 380 if (Textfield::IsCommandIdEnabled(command_id)) { | 406 if (Textfield::IsCommandIdEnabled(command_id)) { |
| 381 // The Textfield code will invoke OnBefore/AfterPossibleChange() itself | 407 // The Textfield code will invoke OnBefore/AfterPossibleChange() itself |
| 382 // as necessary. | 408 // as necessary. |
| 383 Textfield::ExecuteCommand(command_id, event_flags); | 409 Textfield::ExecuteCommand(command_id, event_flags); |
| 384 return; | 410 return; |
| 385 } | 411 } |
| 386 OnBeforePossibleChange(); | 412 OnBeforePossibleChange(); |
| 387 location_bar_view_->command_updater()->ExecuteCommand(command_id); | 413 location_bar_view_->command_updater()->ExecuteCommand(command_id); |
| 388 OnAfterPossibleChange(true); | 414 OnAfterPossibleChange(true); |
| 389 return; | 415 return; |
| 390 } | 416 } |
| 391 } | 417 } |
| 392 | 418 |
| 419 void OmniboxViewViews::ExecuteEditCommand(ui::TextEditCommand command) { | |
| 420 if (!IsEditCommandEnabled(command)) | |
| 421 return; | |
| 422 | |
| 423 DestroyTouchSelection(); | |
| 424 switch (command) { | |
| 425 case ui::TextEditCommand::MOVE_UP: | |
| 426 model()->OnUpOrDownKeyPressed(-1); | |
| 427 break; | |
| 428 case ui::TextEditCommand::MOVE_DOWN: | |
| 429 model()->OnUpOrDownKeyPressed(1); | |
| 430 break; | |
| 431 case ui::TextEditCommand::PASTE: | |
| 432 OnPaste(); | |
| 433 break; | |
| 434 case ui::TextEditCommand::MOVE_PAGE_UP: | |
| 435 model()->OnUpOrDownKeyPressed(-1 * model()->result().size()); | |
| 436 break; | |
| 437 case ui::TextEditCommand::MOVE_PAGE_DOWN: | |
| 438 model()->OnUpOrDownKeyPressed(model()->result().size()); | |
| 439 break; | |
| 440 default: | |
| 441 Textfield::ExecuteEditCommand(command); | |
| 442 break; | |
| 443 } | |
| 444 } | |
| 445 | |
| 393 void OmniboxViewViews::SetTextAndSelectedRange(const base::string16& text, | 446 void OmniboxViewViews::SetTextAndSelectedRange(const base::string16& text, |
| 394 const gfx::Range& range) { | 447 const gfx::Range& range) { |
| 395 SetText(text); | 448 SetText(text); |
| 396 SelectRange(range); | 449 SelectRange(range); |
| 397 } | 450 } |
| 398 | 451 |
| 399 base::string16 OmniboxViewViews::GetSelectedText() const { | 452 base::string16 OmniboxViewViews::GetSelectedText() const { |
| 400 // TODO(oshima): Support IME. | 453 // TODO(oshima): Support IME. |
| 401 return views::Textfield::GetSelectedText(); | 454 return views::Textfield::GetSelectedText(); |
| 402 } | 455 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 } | 491 } |
| 439 | 492 |
| 440 void OmniboxViewViews::AccessibilitySetValue(const base::string16& new_value) { | 493 void OmniboxViewViews::AccessibilitySetValue(const base::string16& new_value) { |
| 441 SetUserText(new_value, true); | 494 SetUserText(new_value, true); |
| 442 } | 495 } |
| 443 | 496 |
| 444 void OmniboxViewViews::UpdateSecurityLevel() { | 497 void OmniboxViewViews::UpdateSecurityLevel() { |
| 445 security_level_ = controller()->GetToolbarModel()->GetSecurityLevel(false); | 498 security_level_ = controller()->GetToolbarModel()->GetSecurityLevel(false); |
| 446 } | 499 } |
| 447 | 500 |
| 501 bool OmniboxViewViews::IsEditCommandEnabledInternal( | |
| 502 ui::TextEditCommand command) const { | |
| 503 switch (command) { | |
| 504 case ui::TextEditCommand::MOVE_UP: | |
| 505 case ui::TextEditCommand::MOVE_DOWN: | |
| 506 case ui::TextEditCommand::PASTE: | |
| 507 return !read_only(); | |
| 508 // Todo shouldn't this also have read_only? | |
| 509 case ui::TextEditCommand::MOVE_PAGE_UP: | |
| 510 case ui::TextEditCommand::MOVE_PAGE_DOWN: | |
| 511 return true; | |
| 512 default: | |
| 513 break; | |
| 514 } | |
| 515 return false; | |
| 516 } | |
| 517 | |
| 448 void OmniboxViewViews::SetWindowTextAndCaretPos(const base::string16& text, | 518 void OmniboxViewViews::SetWindowTextAndCaretPos(const base::string16& text, |
| 449 size_t caret_pos, | 519 size_t caret_pos, |
| 450 bool update_popup, | 520 bool update_popup, |
| 451 bool notify_text_changed) { | 521 bool notify_text_changed) { |
| 452 const gfx::Range range(caret_pos, caret_pos); | 522 const gfx::Range range(caret_pos, caret_pos); |
| 453 SetTextAndSelectedRange(text, range); | 523 SetTextAndSelectedRange(text, range); |
| 454 | 524 |
| 455 if (update_popup) | 525 if (update_popup) |
| 456 UpdatePopup(); | 526 UpdatePopup(); |
| 457 | 527 |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 736 select_all_on_mouse_release_ = false; | 806 select_all_on_mouse_release_ = false; |
| 737 } | 807 } |
| 738 | 808 |
| 739 bool OmniboxViewViews::OnKeyPressed(const ui::KeyEvent& event) { | 809 bool OmniboxViewViews::OnKeyPressed(const ui::KeyEvent& event) { |
| 740 // Skip processing of [Alt]+<num-pad digit> Unicode alt key codes. | 810 // Skip processing of [Alt]+<num-pad digit> Unicode alt key codes. |
| 741 // Otherwise, if num-lock is off, the events are handled as [Up], [Down], etc. | 811 // Otherwise, if num-lock is off, the events are handled as [Up], [Down], etc. |
| 742 if (event.IsUnicodeKeyCode()) | 812 if (event.IsUnicodeKeyCode()) |
| 743 return views::Textfield::OnKeyPressed(event); | 813 return views::Textfield::OnKeyPressed(event); |
| 744 | 814 |
| 745 const bool shift = event.IsShiftDown(); | 815 const bool shift = event.IsShiftDown(); |
| 746 const bool control = event.IsControlDown(); | |
| 747 const bool alt = event.IsAltDown() || event.IsAltGrDown(); | 816 const bool alt = event.IsAltDown() || event.IsAltGrDown(); |
| 748 switch (event.key_code()) { | 817 switch (event.key_code()) { |
| 749 case ui::VKEY_RETURN: | 818 case ui::VKEY_RETURN: |
| 750 model()->AcceptInput(alt ? NEW_FOREGROUND_TAB : CURRENT_TAB, false); | 819 model()->AcceptInput(alt ? NEW_FOREGROUND_TAB : CURRENT_TAB, false); |
| 751 return true; | 820 return true; |
| 752 case ui::VKEY_ESCAPE: | 821 case ui::VKEY_ESCAPE: |
| 753 return model()->OnEscapeKeyPressed(); | 822 return model()->OnEscapeKeyPressed(); |
| 754 case ui::VKEY_CONTROL: | 823 case ui::VKEY_CONTROL: |
| 755 model()->OnControlKeyChanged(true); | 824 model()->OnControlKeyChanged(true); |
| 756 break; | 825 break; |
| 757 case ui::VKEY_DELETE: | 826 case ui::VKEY_DELETE: |
| 758 if (shift && model()->popup_model()->IsOpen()) | 827 if (shift && model()->popup_model()->IsOpen()) |
| 759 model()->popup_model()->TryDeletingCurrentItem(); | 828 model()->popup_model()->TryDeletingCurrentItem(); |
| 760 break; | 829 break; |
| 761 case ui::VKEY_UP: | |
| 762 if (!read_only()) { | |
| 763 model()->OnUpOrDownKeyPressed(-1); | |
| 764 return true; | |
| 765 } | |
| 766 break; | |
| 767 case ui::VKEY_DOWN: | |
| 768 if (!read_only()) { | |
| 769 model()->OnUpOrDownKeyPressed(1); | |
| 770 return true; | |
| 771 } | |
| 772 break; | |
| 773 case ui::VKEY_PRIOR: | |
| 774 if (control || alt || shift) | |
| 775 return false; | |
| 776 model()->OnUpOrDownKeyPressed(-1 * model()->result().size()); | |
| 777 return true; | |
| 778 case ui::VKEY_NEXT: | |
| 779 if (control || alt || shift) | |
| 780 return false; | |
| 781 model()->OnUpOrDownKeyPressed(model()->result().size()); | |
| 782 return true; | |
| 783 case ui::VKEY_V: | |
| 784 if (control && !alt && !read_only()) { | |
| 785 ExecuteCommand(IDS_APP_PASTE, 0); | |
| 786 return true; | |
| 787 } | |
| 788 break; | |
| 789 case ui::VKEY_INSERT: | |
| 790 if (shift && !control && !read_only()) { | |
| 791 ExecuteCommand(IDS_APP_PASTE, 0); | |
| 792 return true; | |
| 793 } | |
| 794 break; | |
| 795 default: | 830 default: |
| 796 break; | 831 break; |
| 797 } | 832 } |
| 798 | 833 |
| 834 ui::TextEditCommand command = scheduled_edit_command(); | |
| 835 if (command == ui::TextEditCommand::INVALID_COMMAND) | |
| 836 command = GetCommandForKeyEvent(event); | |
| 837 | |
| 838 if (IsEditCommandEnabledInternal(command)) { | |
| 839 reset_scheduled_edit_command(); | |
| 840 ExecuteEditCommand(command); | |
| 841 return true; | |
| 842 } | |
| 843 | |
| 799 return views::Textfield::OnKeyPressed(event) || HandleEarlyTabActions(event); | 844 return views::Textfield::OnKeyPressed(event) || HandleEarlyTabActions(event); |
| 800 } | 845 } |
| 801 | 846 |
| 802 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { | 847 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { |
| 803 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { | 848 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { |
| 804 select_all_on_gesture_tap_ = true; | 849 select_all_on_gesture_tap_ = true; |
| 805 | 850 |
| 806 // If we're trying to select all on tap, invalidate any saved selection lest | 851 // If we're trying to select all on tap, invalidate any saved selection lest |
| 807 // restoring it fights with the "select all" action. | 852 // restoring it fights with the "select all" action. |
| 808 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); | 853 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 902 location_bar_view_->SchedulePaint(); | 947 location_bar_view_->SchedulePaint(); |
| 903 } | 948 } |
| 904 | 949 |
| 905 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { | 950 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { |
| 906 if (command_id == IDS_APP_PASTE) | 951 if (command_id == IDS_APP_PASTE) |
| 907 return !read_only() && !GetClipboardText().empty(); | 952 return !read_only() && !GetClipboardText().empty(); |
| 908 if (command_id == IDS_PASTE_AND_GO) | 953 if (command_id == IDS_PASTE_AND_GO) |
| 909 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); | 954 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); |
| 910 if (command_id == IDS_SHOW_URL) | 955 if (command_id == IDS_SHOW_URL) |
| 911 return controller()->GetToolbarModel()->WouldReplaceURL(); | 956 return controller()->GetToolbarModel()->WouldReplaceURL(); |
| 912 return command_id == IDS_MOVE_DOWN || command_id == IDS_MOVE_UP || | 957 // What about IDC_EDIT_SEARCH_ENGINES? |
|
tapted
2016/05/31 04:18:16
What's the context for this? I think this is depre
karandeepb
2016/05/31 05:12:32
Edit Search Engines still remains as a context men
| |
| 913 Textfield::IsCommandIdEnabled(command_id) || | 958 return Textfield::IsCommandIdEnabled(command_id) || |
| 914 location_bar_view_->command_updater()->IsCommandEnabled(command_id); | 959 location_bar_view_->command_updater()->IsCommandEnabled(command_id); |
| 915 } | 960 } |
| 916 | 961 |
| 917 base::string16 OmniboxViewViews::GetSelectionClipboardText() const { | 962 base::string16 OmniboxViewViews::GetSelectionClipboardText() const { |
| 918 return SanitizeTextForPaste(Textfield::GetSelectionClipboardText()); | 963 return SanitizeTextForPaste(Textfield::GetSelectionClipboardText()); |
| 919 } | 964 } |
| 920 | 965 |
| 921 void OmniboxViewViews::DoInsertChar(base::char16 ch) { | 966 void OmniboxViewViews::DoInsertChar(base::char16 ch) { |
| 922 // If |insert_char_time_| is not null, there's a pending insert char operation | 967 // If |insert_char_time_| is not null, there's a pending insert char operation |
| 923 // that hasn't been painted yet. Keep the earlier time. | 968 // that hasn't been painted yet. Keep the earlier time. |
| 924 if (insert_char_time_.is_null()) | 969 if (insert_char_time_.is_null()) |
| 925 insert_char_time_ = base::TimeTicks::Now(); | 970 insert_char_time_ = base::TimeTicks::Now(); |
| 926 Textfield::DoInsertChar(ch); | 971 Textfield::DoInsertChar(ch); |
| 927 } | 972 } |
| 928 | 973 |
| 974 bool OmniboxViewViews::IsEditCommandEnabled(ui::TextEditCommand command) const { | |
| 975 return IsEditCommandEnabledInternal(command) || | |
| 976 Textfield::IsEditCommandEnabled(command); | |
| 977 } | |
| 978 | |
| 929 #if defined(OS_CHROMEOS) | 979 #if defined(OS_CHROMEOS) |
| 930 void OmniboxViewViews::CandidateWindowOpened( | 980 void OmniboxViewViews::CandidateWindowOpened( |
| 931 chromeos::input_method::InputMethodManager* manager) { | 981 chromeos::input_method::InputMethodManager* manager) { |
| 932 ime_candidate_window_open_ = true; | 982 ime_candidate_window_open_ = true; |
| 933 } | 983 } |
| 934 | 984 |
| 935 void OmniboxViewViews::CandidateWindowClosed( | 985 void OmniboxViewViews::CandidateWindowClosed( |
| 936 chromeos::input_method::InputMethodManager* manager) { | 986 chromeos::input_method::InputMethodManager* manager) { |
| 937 ime_candidate_window_open_ = false; | 987 ime_candidate_window_open_ = false; |
| 938 } | 988 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1084 menu_contents->InsertItemWithStringIdAt( | 1134 menu_contents->InsertItemWithStringIdAt( |
| 1085 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); | 1135 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); |
| 1086 } | 1136 } |
| 1087 | 1137 |
| 1088 // Minor note: We use IDC_ for command id here while the underlying textfield | 1138 // Minor note: We use IDC_ for command id here while the underlying textfield |
| 1089 // is using IDS_ for all its command ids. This is because views cannot depend | 1139 // is using IDS_ for all its command ids. This is because views cannot depend |
| 1090 // on IDC_ for now. | 1140 // on IDC_ for now. |
| 1091 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 1141 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
| 1092 IDS_EDIT_SEARCH_ENGINES); | 1142 IDS_EDIT_SEARCH_ENGINES); |
| 1093 } | 1143 } |
| OLD | NEW |