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/cocoa/omnibox/omnibox_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 DCHECK_LE(caret_pos, text.size()); | 351 DCHECK_LE(caret_pos, text.size()); |
| 352 SetTextAndSelectedRange(text, NSMakeRange(caret_pos, 0)); | 352 SetTextAndSelectedRange(text, NSMakeRange(caret_pos, 0)); |
| 353 | 353 |
| 354 if (update_popup) | 354 if (update_popup) |
| 355 UpdatePopup(); | 355 UpdatePopup(); |
| 356 | 356 |
| 357 if (notify_text_changed) | 357 if (notify_text_changed) |
| 358 TextChanged(); | 358 TextChanged(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 void OmniboxViewMac::SetForcedQuery() { | 361 void OmniboxViewMac::SetCaretPos(size_t caret_pos) { |
| 362 [[field_ currentEditor] setSelectedRange:NSMakeRange(caret_pos, 0)]; | |
| 363 } | |
| 364 | |
| 365 void OmniboxViewMac::EnterKeywordModeForDefaultSearchProvider() { | |
| 362 // We need to do this first, else |SetSelectedRange()| won't work. | 366 // We need to do this first, else |SetSelectedRange()| won't work. |
| 363 FocusLocation(true); | 367 FocusLocation(true); |
| 364 | 368 |
| 365 const base::string16 current_text(GetText()); | 369 // Transition the user into keyword mode using their default search provider. |
| 366 const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16); | 370 // Select their query if they typed one. |
| 367 if (start == base::string16::npos || (current_text[start] != '?')) { | 371 model()->EnterKeywordModeForDefaultSearchProvider( |
| 368 SetUserText(base::ASCIIToUTF16("?")); | 372 KeywordModeEntryMethod::KEYBOARD_SHORTCUT); |
| 369 } else { | 373 NSRange range = NSMakeRange(0, GetText().size()); |
| 370 NSRange range = NSMakeRange(start + 1, current_text.size() - start - 1); | 374 [[field_ currentEditor] setSelectedRange:range]; |
|
Peter Kasting
2016/04/28 21:24:41
Looks like these two lines are just trying to do w
Tom (Use chromium acct)
2016/04/29 01:10:20
Done.
| |
| 371 [[field_ currentEditor] setSelectedRange:range]; | |
| 372 } | |
| 373 } | 375 } |
| 374 | 376 |
| 375 bool OmniboxViewMac::IsSelectAll() const { | 377 bool OmniboxViewMac::IsSelectAll() const { |
| 376 if (![field_ currentEditor]) | 378 if (![field_ currentEditor]) |
| 377 return true; | 379 return true; |
| 378 const NSRange all_range = NSMakeRange(0, GetTextLength()); | 380 const NSRange all_range = NSMakeRange(0, GetTextLength()); |
| 379 return NSEqualRanges(all_range, GetSelectedRange()); | 381 return NSEqualRanges(all_range, GetSelectedRange()); |
| 380 } | 382 } |
| 381 | 383 |
| 382 bool OmniboxViewMac::DeleteAtEndPressed() { | 384 bool OmniboxViewMac::DeleteAtEndPressed() { |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 bool OmniboxViewMac::IsFirstResponder() const { | 656 bool OmniboxViewMac::IsFirstResponder() const { |
| 655 return [field_ currentEditor] != nil ? true : false; | 657 return [field_ currentEditor] != nil ? true : false; |
| 656 } | 658 } |
| 657 | 659 |
| 658 void OmniboxViewMac::OnBeforePossibleChange() { | 660 void OmniboxViewMac::OnBeforePossibleChange() { |
| 659 // We should only arrive here when the field is focused. | 661 // We should only arrive here when the field is focused. |
| 660 DCHECK(IsFirstResponder()); | 662 DCHECK(IsFirstResponder()); |
| 661 | 663 |
| 662 selection_before_change_ = GetSelectedRange(); | 664 selection_before_change_ = GetSelectedRange(); |
| 663 text_before_change_ = GetText(); | 665 text_before_change_ = GetText(); |
| 666 keyword_before_change_ = model()->keyword(); | |
| 667 is_keyword_selected_before_change_ = model()->is_keyword_selected(); | |
| 664 marked_range_before_change_ = GetMarkedRange(); | 668 marked_range_before_change_ = GetMarkedRange(); |
| 665 } | 669 } |
| 666 | 670 |
| 667 bool OmniboxViewMac::OnAfterPossibleChange(bool allow_keyword_ui_change) { | 671 bool OmniboxViewMac::OnAfterPossibleChange(bool allow_keyword_ui_change) { |
| 668 // We should only arrive here when the field is focused. | 672 // We should only arrive here when the field is focused. |
| 669 DCHECK(IsFirstResponder()); | 673 DCHECK(IsFirstResponder()); |
| 670 | 674 |
| 671 const NSRange new_selection(GetSelectedRange()); | 675 const NSRange new_selection(GetSelectedRange()); |
| 672 const base::string16 new_text(GetText()); | 676 const base::string16 new_text(GetText()); |
| 677 const base::string16 new_keyword(model()->keyword()); | |
| 678 const bool new_is_keyword_selected = model()->is_keyword_selected(); | |
| 673 const size_t length = new_text.length(); | 679 const size_t length = new_text.length(); |
| 674 | 680 |
| 675 const bool selection_differs = | 681 const bool selection_differs = |
| 676 (new_selection.length || selection_before_change_.length) && | 682 (new_selection.length || selection_before_change_.length) && |
| 677 !NSEqualRanges(new_selection, selection_before_change_); | 683 !NSEqualRanges(new_selection, selection_before_change_); |
| 678 const bool at_end_of_edit = (length == new_selection.location); | 684 const bool at_end_of_edit = (length == new_selection.location); |
| 679 const bool text_differs = (new_text != text_before_change_) || | 685 const bool text_differs = (new_text != text_before_change_) || |
| 680 !NSEqualRanges(marked_range_before_change_, GetMarkedRange()); | 686 !NSEqualRanges(marked_range_before_change_, GetMarkedRange()); |
| 687 const bool keyword_differs = | |
| 688 (new_is_keyword_selected != is_keyword_selected_before_change_) || | |
| 689 (new_is_keyword_selected && is_keyword_selected_before_change_ && | |
| 690 new_keyword != keyword_before_change_); | |
| 681 | 691 |
| 682 // When the user has deleted text, we don't allow inline | 692 // When the user has deleted text, we don't allow inline |
| 683 // autocomplete. This is assumed if the text has gotten shorter AND | 693 // autocomplete. This is assumed if the text has gotten shorter AND |
| 684 // the selection has shifted towards the front of the text. During | 694 // the selection has shifted towards the front of the text. During |
| 685 // normal typing the text will almost always be shorter (as the new | 695 // normal typing the text will almost always be shorter (as the new |
| 686 // input replaces the autocomplete suggestion), but in that case the | 696 // input replaces the autocomplete suggestion), but in that case the |
| 687 // selection point will have moved towards the end of the text. | 697 // selection point will have moved towards the end of the text. |
| 688 // TODO(shess): In our implementation, we can catch -deleteBackward: | 698 // TODO(shess): In our implementation, we can catch -deleteBackward: |
| 689 // and other methods to provide positive knowledge that a delete | 699 // and other methods to provide positive knowledge that a delete |
| 690 // occurred, rather than intuiting it from context. Consider whether | 700 // occurred, rather than intuiting it from context. Consider whether |
| 691 // that would be a stronger approach. | 701 // that would be a stronger approach. |
| 692 const bool just_deleted_text = | 702 const bool just_deleted_text = |
| 693 (length < text_before_change_.length() && | 703 (length < text_before_change_.length() && |
| 694 new_selection.location <= selection_before_change_.location); | 704 new_selection.location <= selection_before_change_.location); |
| 695 | 705 |
| 696 delete_at_end_pressed_ = false; | 706 delete_at_end_pressed_ = false; |
| 697 | 707 |
| 698 const bool something_changed = model()->OnAfterPossibleChange( | 708 const bool something_changed = model()->OnAfterPossibleChange( |
| 699 text_before_change_, new_text, new_selection.location, | 709 text_before_change_, new_text, new_selection.location, |
| 700 NSMaxRange(new_selection), selection_differs, text_differs, | 710 NSMaxRange(new_selection), selection_differs, text_differs, |
| 701 just_deleted_text, allow_keyword_ui_change && !IsImeComposing()); | 711 keyword_differs, just_deleted_text, |
| 712 allow_keyword_ui_change && !IsImeComposing()); | |
| 702 | 713 |
| 703 if (delete_was_pressed_ && at_end_of_edit) | 714 if (delete_was_pressed_ && at_end_of_edit) |
| 704 delete_at_end_pressed_ = true; | 715 delete_at_end_pressed_ = true; |
| 705 | 716 |
| 706 // Restyle in case the user changed something. | 717 // Restyle in case the user changed something. |
| 707 // TODO(shess): I believe there are multiple-redraw cases, here. | 718 // TODO(shess): I believe there are multiple-redraw cases, here. |
| 708 // Linux watches for something_changed && text_differs, but that | 719 // Linux watches for something_changed && text_differs, but that |
| 709 // fails for us in case you copy the URL and paste the identical URL | 720 // fails for us in case you copy the URL and paste the identical URL |
| 710 // back (we'll lose the styling). | 721 // back (we'll lose the styling). |
| 711 TextChanged(); | 722 TextChanged(); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 839 return true; | 850 return true; |
| 840 } | 851 } |
| 841 | 852 |
| 842 if (cmd == @selector(cancelOperation:)) { | 853 if (cmd == @selector(cancelOperation:)) { |
| 843 return model()->OnEscapeKeyPressed(); | 854 return model()->OnEscapeKeyPressed(); |
| 844 } | 855 } |
| 845 | 856 |
| 846 if ((cmd == @selector(insertTab:) || | 857 if ((cmd == @selector(insertTab:) || |
| 847 cmd == @selector(insertTabIgnoringFieldEditor:)) && | 858 cmd == @selector(insertTabIgnoringFieldEditor:)) && |
| 848 model()->is_keyword_hint()) { | 859 model()->is_keyword_hint()) { |
| 849 return model()->AcceptKeyword(ENTERED_KEYWORD_MODE_VIA_TAB); | 860 return model()->AcceptKeyword(KeywordModeEntryMethod::TAB); |
| 850 } | 861 } |
| 851 | 862 |
| 852 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op | 863 // |-noop:| is sent when the user presses Cmd+Return. Override the no-op |
| 853 // behavior with the proper WindowOpenDisposition. | 864 // behavior with the proper WindowOpenDisposition. |
| 854 NSEvent* event = [NSApp currentEvent]; | 865 NSEvent* event = [NSApp currentEvent]; |
| 855 if (cmd == @selector(insertNewline:) || | 866 if (cmd == @selector(insertNewline:) || |
| 856 (cmd == @selector(noop:) && | 867 (cmd == @selector(noop:) && |
| 857 ([event type] == NSKeyDown || [event type] == NSKeyUp) && | 868 ([event type] == NSKeyDown || [event type] == NSKeyUp) && |
| 858 [event keyCode] == kVK_Return)) { | 869 [event keyCode] == kVK_Return)) { |
| 859 WindowOpenDisposition disposition = | 870 WindowOpenDisposition disposition = |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1113 | 1124 |
| 1114 NSUInteger OmniboxViewMac::GetTextLength() const { | 1125 NSUInteger OmniboxViewMac::GetTextLength() const { |
| 1115 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1126 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
| 1116 [[field_ stringValue] length]; | 1127 [[field_ stringValue] length]; |
| 1117 } | 1128 } |
| 1118 | 1129 |
| 1119 bool OmniboxViewMac::IsCaretAtEnd() const { | 1130 bool OmniboxViewMac::IsCaretAtEnd() const { |
| 1120 const NSRange selection = GetSelectedRange(); | 1131 const NSRange selection = GetSelectedRange(); |
| 1121 return NSMaxRange(selection) == GetTextLength(); | 1132 return NSMaxRange(selection) == GetTextLength(); |
| 1122 } | 1133 } |
| OLD | NEW |