Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/autocomplete/autocomplete_edit.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 keyword_.clear(); | 390 keyword_.clear(); |
| 391 is_keyword_hint_ = false; | 391 is_keyword_hint_ = false; |
| 392 has_temporary_text_ = false; | 392 has_temporary_text_ = false; |
| 393 view_->SetWindowTextAndCaretPos(permanent_text_, | 393 view_->SetWindowTextAndCaretPos(permanent_text_, |
| 394 has_focus_ ? permanent_text_.length() : 0); | 394 has_focus_ ? permanent_text_.length() : 0); |
| 395 } | 395 } |
| 396 | 396 |
| 397 void AutocompleteEditModel::StartAutocomplete( | 397 void AutocompleteEditModel::StartAutocomplete( |
| 398 bool has_selected_text, | 398 bool has_selected_text, |
| 399 bool prevent_inline_autocomplete) const { | 399 bool prevent_inline_autocomplete) const { |
| 400 ResetCurrentMatchKeywordMode(); | |
| 401 | |
| 400 bool keyword_is_selected = KeywordIsSelected(); | 402 bool keyword_is_selected = KeywordIsSelected(); |
| 401 popup_->SetHoveredLine(AutocompletePopupModel::kNoMatch); | 403 popup_->SetHoveredLine(AutocompletePopupModel::kNoMatch); |
| 402 // We don't explicitly clear AutocompletePopupModel::manually_selected_match, | 404 // We don't explicitly clear AutocompletePopupModel::manually_selected_match, |
| 403 // as Start ends up invoking AutocompletePopupModel::OnResultChanged which | 405 // as Start ends up invoking AutocompletePopupModel::OnResultChanged which |
| 404 // clears it. | 406 // clears it. |
| 405 autocomplete_controller_->Start( | 407 autocomplete_controller_->Start( |
| 406 user_text_, GetDesiredTLD(), | 408 user_text_, GetDesiredTLD(), |
| 407 prevent_inline_autocomplete || just_deleted_text_ || | 409 prevent_inline_autocomplete || just_deleted_text_ || |
| 408 (has_selected_text && inline_autocomplete_text_.empty()) || | 410 (has_selected_text && inline_autocomplete_text_.empty()) || |
| 409 (paste_state_ != NONE), keyword_is_selected, | 411 (paste_state_ != NONE), keyword_is_selected, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 | 555 |
| 554 InstantController* instant = controller_->GetInstant(); | 556 InstantController* instant = controller_->GetInstant(); |
| 555 if (instant && !popup_->IsOpen()) | 557 if (instant && !popup_->IsOpen()) |
| 556 instant->DestroyPreviewContents(); | 558 instant->DestroyPreviewContents(); |
| 557 in_revert_ = false; | 559 in_revert_ = false; |
| 558 } | 560 } |
| 559 | 561 |
| 560 bool AutocompleteEditModel::AcceptKeyword() { | 562 bool AutocompleteEditModel::AcceptKeyword() { |
| 561 DCHECK(is_keyword_hint_ && !keyword_.empty()); | 563 DCHECK(is_keyword_hint_ && !keyword_.empty()); |
| 562 | 564 |
| 563 view_->OnBeforePossibleChange(); | 565 autocomplete_controller_->Stop(false); |
|
sky
2011/08/01 16:02:15
I'm pretty sure this results in not running the qu
| |
| 564 view_->SetWindowTextAndCaretPos(string16(), 0); | |
| 565 is_keyword_hint_ = false; | 566 is_keyword_hint_ = false; |
| 566 view_->OnAfterPossibleChange(); | 567 |
| 567 just_deleted_text_ = false; // OnAfterPossibleChange() erroneously sets this | 568 if (popup_->IsOpen()) |
| 568 // since the edit contents have disappeared. It | 569 popup_->SelectKeyword(true); |
| 569 // doesn't really matter, but we clear it to be | 570 |
| 570 // consistent. | 571 view_->SetUserText(string16(), string16(), false); |
| 572 | |
| 571 UserMetrics::RecordAction(UserMetricsAction("AcceptedKeywordHint")); | 573 UserMetrics::RecordAction(UserMetricsAction("AcceptedKeywordHint")); |
| 572 return true; | 574 return true; |
| 573 } | 575 } |
| 574 | 576 |
| 575 void AutocompleteEditModel::ClearKeyword(const string16& visible_text) { | 577 void AutocompleteEditModel::ClearKeyword(const string16& visible_text) { |
| 576 view_->OnBeforePossibleChange(); | 578 autocomplete_controller_->Stop(false); |
| 579 ResetCurrentMatchKeywordMode(); | |
| 580 | |
| 577 const string16 window_text(keyword_ + visible_text); | 581 const string16 window_text(keyword_ + visible_text); |
| 578 view_->SetWindowTextAndCaretPos(window_text.c_str(), keyword_.length()); | 582 |
| 579 keyword_.clear(); | 583 // Only reset the result if the edit text has changed since the |
| 580 is_keyword_hint_ = false; | 584 // keyword was accepted. |
| 581 view_->OnAfterPossibleChange(); | 585 if (just_deleted_text_) { |
| 582 just_deleted_text_ = true; // OnAfterPossibleChange() fails to clear this | 586 view_->OnBeforePossibleChange(); |
| 583 // since the edit contents have actually grown | 587 view_->SetWindowTextAndCaretPos(window_text.c_str(), keyword_.length()); |
| 584 // longer. | 588 keyword_.clear(); |
| 589 is_keyword_hint_ = false; | |
| 590 view_->OnAfterPossibleChange(); | |
| 591 just_deleted_text_ = true; // OnAfterPossibleChange() fails to clear this | |
| 592 // since the edit contents have actually grown | |
| 593 // longer. | |
| 594 } else { | |
| 595 view_->SetUserText(window_text, window_text, false); | |
| 596 | |
| 597 is_keyword_hint_ = visible_text.empty(); | |
| 598 if (!is_keyword_hint_) | |
| 599 keyword_.clear(); | |
| 600 | |
| 601 OnChanged(); | |
| 602 } | |
| 603 } | |
| 604 | |
| 605 void AutocompleteEditModel::ResetCurrentMatchKeywordMode() const { | |
|
sky
2011/08/01 16:02:15
Doesn't match position in header.
sky
2011/08/01 16:02:15
Maybe this should be called ExitKeywordMode(), or
| |
| 606 if (popup_->IsOpen() && popup_->keyword_selected()) | |
| 607 popup_->SelectKeyword(false); | |
| 585 } | 608 } |
| 586 | 609 |
| 587 const AutocompleteResult& AutocompleteEditModel::result() const { | 610 const AutocompleteResult& AutocompleteEditModel::result() const { |
| 588 return autocomplete_controller_->result(); | 611 return autocomplete_controller_->result(); |
| 589 } | 612 } |
| 590 | 613 |
| 591 void AutocompleteEditModel::OnSetFocus(bool control_down) { | 614 void AutocompleteEditModel::OnSetFocus(bool control_down) { |
| 592 has_focus_ = true; | 615 has_focus_ = true; |
| 593 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; | 616 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; |
| 594 NotificationService::current()->Notify(chrome::NOTIFICATION_OMNIBOX_FOCUSED, | 617 NotificationService::current()->Notify(chrome::NOTIFICATION_OMNIBOX_FOCUSED, |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 861 if (!match->destination_url.SchemeIs(chrome::kExtensionScheme)) { | 884 if (!match->destination_url.SchemeIs(chrome::kExtensionScheme)) { |
| 862 // Warm up DNS Prefetch cache, or preconnect to a search service. | 885 // Warm up DNS Prefetch cache, or preconnect to a search service. |
| 863 chrome_browser_net::AnticipateOmniboxUrl(match->destination_url, | 886 chrome_browser_net::AnticipateOmniboxUrl(match->destination_url, |
| 864 IsPreconnectable(match->type)); | 887 IsPreconnectable(match->type)); |
| 865 } | 888 } |
| 866 | 889 |
| 867 // We could prefetch the alternate nav URL, if any, but because there | 890 // We could prefetch the alternate nav URL, if any, but because there |
| 868 // can be many of these as a user types an initial series of characters, | 891 // can be many of these as a user types an initial series of characters, |
| 869 // the OS DNS cache could suffer eviction problems for minimal gain. | 892 // the OS DNS cache could suffer eviction problems for minimal gain. |
| 870 | 893 |
| 871 is_keyword_hint = popup_->GetKeywordForMatch(*match, &keyword); | 894 keyword = match->keyword; |
| 895 is_keyword_hint = match->associated_keyword.get() != NULL; | |
| 896 if (is_keyword_hint) | |
| 897 keyword = match->associated_keyword->keyword; | |
| 872 } | 898 } |
| 899 | |
| 873 popup_->OnResultChanged(); | 900 popup_->OnResultChanged(); |
| 874 OnPopupDataChanged(inline_autocomplete_text, NULL, keyword, | 901 OnPopupDataChanged(inline_autocomplete_text, NULL, keyword, |
| 875 is_keyword_hint); | 902 is_keyword_hint); |
| 876 } else { | 903 } else { |
| 877 popup_->OnResultChanged(); | 904 popup_->OnResultChanged(); |
| 878 } | 905 } |
| 879 | 906 |
| 880 if (popup_->IsOpen()) { | 907 if (popup_->IsOpen()) { |
| 881 PopupBoundsChangedTo(popup_->view()->GetTargetBounds()); | 908 PopupBoundsChangedTo(popup_->view()->GetTargetBounds()); |
| 882 } else if (was_open) { | 909 } else if (was_open) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1006 old_user_text.length() - caret_position + 1)) { | 1033 old_user_text.length() - caret_position + 1)) { |
| 1007 return false; | 1034 return false; |
| 1008 } | 1035 } |
| 1009 | 1036 |
| 1010 // Then check if the text before the inserted space matches a keyword. | 1037 // Then check if the text before the inserted space matches a keyword. |
| 1011 string16 keyword; | 1038 string16 keyword; |
| 1012 TrimWhitespace(new_user_text.substr(0, caret_position - 1), | 1039 TrimWhitespace(new_user_text.substr(0, caret_position - 1), |
| 1013 TRIM_LEADING, &keyword); | 1040 TRIM_LEADING, &keyword); |
| 1014 | 1041 |
| 1015 // Only allow exact keyword match if |keyword| represents a keyword hint. | 1042 // Only allow exact keyword match if |keyword| represents a keyword hint. |
| 1016 return keyword.length() && popup_->GetKeywordForText(keyword, &keyword); | 1043 return keyword.length() && |
| 1044 !autocomplete_controller_->keyword_provider()-> | |
| 1045 GetKeywordForText(keyword).empty(); | |
| 1017 } | 1046 } |
| 1018 | 1047 |
| 1019 // static | 1048 // static |
| 1020 bool AutocompleteEditModel::IsSpaceCharForAcceptingKeyword(wchar_t c) { | 1049 bool AutocompleteEditModel::IsSpaceCharForAcceptingKeyword(wchar_t c) { |
| 1021 switch (c) { | 1050 switch (c) { |
| 1022 case 0x0020: // Space | 1051 case 0x0020: // Space |
| 1023 case 0x3000: // Ideographic Space | 1052 case 0x3000: // Ideographic Space |
| 1024 return true; | 1053 return true; |
| 1025 default: | 1054 default: |
| 1026 return false; | 1055 return false; |
| 1027 } | 1056 } |
| 1028 } | 1057 } |
| OLD | NEW |