OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/omnibox/omnibox_edit_model.h" | 5 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } else { | 129 } else { |
130 InternalSetUserText(user_text); | 130 InternalSetUserText(user_text); |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_, | 134 return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_, |
135 is_caret_visible_); | 135 is_caret_visible_); |
136 } | 136 } |
137 | 137 |
138 void OmniboxEditModel::RestoreState(const State& state) { | 138 void OmniboxEditModel::RestoreState(const State& state) { |
139 SetCaretVisibility(state.is_caret_visible); | 139 SetCaretVisibilityInternal(state.is_caret_visible, FOCUS_CHANGE_TAB_SWITCH); |
140 // Restore any user editing. | 140 // Restore any user editing. |
141 if (state.user_input_in_progress) { | 141 if (state.user_input_in_progress) { |
142 // NOTE: Be sure and set keyword-related state BEFORE invoking | 142 // NOTE: Be sure and set keyword-related state BEFORE invoking |
143 // DisplayTextFromUserText(), as its result depends upon this state. | 143 // DisplayTextFromUserText(), as its result depends upon this state. |
144 keyword_ = state.keyword; | 144 keyword_ = state.keyword; |
145 is_keyword_hint_ = state.is_keyword_hint; | 145 is_keyword_hint_ = state.is_keyword_hint; |
146 view_->SetUserText(state.user_text, | 146 view_->SetUserText(state.user_text, |
147 DisplayTextFromUserText(state.user_text), false); | 147 DisplayTextFromUserText(state.user_text), false); |
148 } | 148 } |
149 } | 149 } |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 } | 708 } |
709 } | 709 } |
710 | 710 |
711 const AutocompleteResult& OmniboxEditModel::result() const { | 711 const AutocompleteResult& OmniboxEditModel::result() const { |
712 return autocomplete_controller_->result(); | 712 return autocomplete_controller_->result(); |
713 } | 713 } |
714 | 714 |
715 void OmniboxEditModel::OnSetFocus(bool control_down) { | 715 void OmniboxEditModel::OnSetFocus(bool control_down) { |
716 has_focus_ = true; | 716 has_focus_ = true; |
717 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; | 717 control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; |
718 // Restore caret visibility whenever the user focuses back into the omnibox. | 718 // If the omnibox lost focus while the caret was hidden and then regained |
| 719 // focus, OnSetFocus() is called and should restore visibility. Note that |
| 720 // focus can be regained without an accompanying call to |
| 721 // OmniboxView::SetFocus(), e.g. by tabbing in. |
719 SetCaretVisibility(true); | 722 SetCaretVisibility(true); |
720 | 723 |
721 if (InstantController* instant = controller_->GetInstant()) | 724 // The SetCaretVisibility call above will already send an update to the |
722 instant->OmniboxGotFocus(); | 725 // InstantController in some cases but we still need this here since it won't |
| 726 // send an update if the caret was already visible. |
| 727 InstantController* instant = controller_->GetInstant(); |
| 728 if (instant) |
| 729 instant->OmniboxFocusChanged(FOCUS_VISIBLE, FOCUS_CHANGE_EXPLICIT, NULL); |
723 | 730 |
724 content::WebContents* web_contents = controller_->GetWebContents(); | 731 content::WebContents* web_contents = controller_->GetWebContents(); |
725 if (web_contents) { | 732 if (web_contents) { |
726 // TODO(jered): We may want to merge this into Start() and just call that | 733 // TODO(jered): We may want to merge this into Start() and just call that |
727 // here rather than having a special entry point for zero-suggest. Note | 734 // here rather than having a special entry point for zero-suggest. Note |
728 // that we avoid PermanentURL() here because it's not guaranteed to give us | 735 // that we avoid PermanentURL() here because it's not guaranteed to give us |
729 // the actual underlying current URL, e.g. if we're on the NTP and the | 736 // the actual underlying current URL, e.g. if we're on the NTP and the |
730 // |permanent_text_| is empty. | 737 // |permanent_text_| is empty. |
731 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), | 738 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), |
732 user_text_); | 739 user_text_); |
733 } | 740 } |
734 | 741 |
735 NotifySearchTabHelper(); | 742 NotifySearchTabHelper(); |
736 } | 743 } |
737 | 744 |
738 void OmniboxEditModel::SetCaretVisibility(bool visible) { | 745 void OmniboxEditModel::SetCaretVisibility(bool visible) { |
739 if (has_focus_ && visible != is_caret_visible_) { | 746 SetCaretVisibilityInternal(visible, FOCUS_CHANGE_EXPLICIT); |
740 is_caret_visible_ = visible; | |
741 view_->ApplyCaretVisibility(); | |
742 } | |
743 } | 747 } |
744 | 748 |
745 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { | 749 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { |
746 SetInstantSuggestion(InstantSuggestion()); | 750 SetInstantSuggestion(InstantSuggestion()); |
747 | 751 |
748 if (InstantController* instant = controller_->GetInstant()) | 752 InstantController* instant = controller_->GetInstant(); |
749 instant->OmniboxLostFocus(view_gaining_focus); | 753 if (instant) { |
| 754 instant->OmniboxFocusChanged(FOCUS_NONE, FOCUS_CHANGE_EXPLICIT, |
| 755 view_gaining_focus); |
| 756 } |
750 | 757 |
751 // TODO(jered): Rip this out along with StartZeroSuggest. | 758 // TODO(jered): Rip this out along with StartZeroSuggest. |
752 autocomplete_controller_->StopZeroSuggest(); | 759 autocomplete_controller_->StopZeroSuggest(); |
753 NotifySearchTabHelper(); | 760 NotifySearchTabHelper(); |
754 } | 761 } |
755 | 762 |
756 void OmniboxEditModel::OnKillFocus() { | 763 void OmniboxEditModel::OnKillFocus() { |
757 has_focus_ = false; | 764 has_focus_ = false; |
758 control_key_state_ = UP; | 765 control_key_state_ = UP; |
759 paste_state_ = NONE; | 766 paste_state_ = NONE; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 // that replaced all the text, preserve that information; otherwise, if we've | 952 // that replaced all the text, preserve that information; otherwise, if we've |
946 // made some other edit, clear paste tracking. | 953 // made some other edit, clear paste tracking. |
947 if (paste_state_ == PASTING) | 954 if (paste_state_ == PASTING) |
948 paste_state_ = PASTED; | 955 paste_state_ = PASTED; |
949 else if (text_differs) | 956 else if (text_differs) |
950 paste_state_ = NONE; | 957 paste_state_ = NONE; |
951 | 958 |
952 // Restore caret visibility whenever the user changes text or selection in the | 959 // Restore caret visibility whenever the user changes text or selection in the |
953 // omnibox. | 960 // omnibox. |
954 if (text_differs || selection_differs) | 961 if (text_differs || selection_differs) |
955 SetCaretVisibility(true); | 962 SetCaretVisibilityInternal(true, FOCUS_CHANGE_TYPING); |
956 | 963 |
957 // Modifying the selection counts as accepting the autocompleted text. | 964 // Modifying the selection counts as accepting the autocompleted text. |
958 const bool user_text_changed = | 965 const bool user_text_changed = |
959 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); | 966 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); |
960 | 967 |
961 // If something has changed while the control key is down, prevent | 968 // If something has changed while the control key is down, prevent |
962 // "ctrl-enter" until the control key is released. When we do this, we need | 969 // "ctrl-enter" until the control key is released. When we do this, we need |
963 // to update the popup if it's open, since the desired_tld will have changed. | 970 // to update the popup if it's open, since the desired_tld will have changed. |
964 if ((text_differs || selection_differs) && | 971 if ((text_differs || selection_differs) && |
965 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { | 972 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 // | 1014 // |
1008 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that | 1015 // If MaybeAcceptKeywordBySpace() accepts the keyword and returns true, that |
1009 // will have updated our state already, so in that case we don't also return | 1016 // will have updated our state already, so in that case we don't also return |
1010 // true from this function. | 1017 // true from this function. |
1011 return !(text_differs && allow_keyword_ui_change && !just_deleted_text && | 1018 return !(text_differs && allow_keyword_ui_change && !just_deleted_text && |
1012 no_selection && (selection_start == user_text_.length()) && | 1019 no_selection && (selection_start == user_text_.length()) && |
1013 MaybeAcceptKeywordBySpace(user_text_)); | 1020 MaybeAcceptKeywordBySpace(user_text_)); |
1014 } | 1021 } |
1015 | 1022 |
1016 void OmniboxEditModel::PopupBoundsChangedTo(const gfx::Rect& bounds) { | 1023 void OmniboxEditModel::PopupBoundsChangedTo(const gfx::Rect& bounds) { |
1017 if (InstantController* instant = controller_->GetInstant()) | 1024 InstantController* instant = controller_->GetInstant(); |
| 1025 if (instant) |
1018 instant->SetOmniboxBounds(bounds); | 1026 instant->SetOmniboxBounds(bounds); |
1019 } | 1027 } |
1020 | 1028 |
1021 void OmniboxEditModel::OnResultChanged(bool default_match_changed) { | 1029 void OmniboxEditModel::OnResultChanged(bool default_match_changed) { |
1022 const bool was_open = popup_->IsOpen(); | 1030 const bool was_open = popup_->IsOpen(); |
1023 if (default_match_changed) { | 1031 if (default_match_changed) { |
1024 string16 inline_autocomplete_text; | 1032 string16 inline_autocomplete_text; |
1025 string16 keyword; | 1033 string16 keyword; |
1026 bool is_keyword_hint = false; | 1034 bool is_keyword_hint = false; |
1027 const AutocompleteResult& result = this->result(); | 1035 const AutocompleteResult& result = this->result(); |
(...skipping 28 matching lines...) Expand all Loading... |
1056 } else if (was_open) { | 1064 } else if (was_open) { |
1057 // Accepts the temporary text as the user text, because it makes little | 1065 // Accepts the temporary text as the user text, because it makes little |
1058 // sense to have temporary text when the popup is closed. | 1066 // sense to have temporary text when the popup is closed. |
1059 InternalSetUserText(UserTextFromDisplayText(view_->GetText())); | 1067 InternalSetUserText(UserTextFromDisplayText(view_->GetText())); |
1060 has_temporary_text_ = false; | 1068 has_temporary_text_ = false; |
1061 is_temporary_text_set_by_instant_ = false; | 1069 is_temporary_text_set_by_instant_ = false; |
1062 PopupBoundsChangedTo(gfx::Rect()); | 1070 PopupBoundsChangedTo(gfx::Rect()); |
1063 NotifySearchTabHelper(); | 1071 NotifySearchTabHelper(); |
1064 } | 1072 } |
1065 | 1073 |
1066 if (InstantController* instant = controller_->GetInstant()) | 1074 InstantController* instant = controller_->GetInstant(); |
| 1075 if (instant) |
1067 instant->HandleAutocompleteResults(*autocomplete_controller_->providers()); | 1076 instant->HandleAutocompleteResults(*autocomplete_controller_->providers()); |
1068 } | 1077 } |
1069 | 1078 |
1070 bool OmniboxEditModel::query_in_progress() const { | 1079 bool OmniboxEditModel::query_in_progress() const { |
1071 return !autocomplete_controller_->done(); | 1080 return !autocomplete_controller_->done(); |
1072 } | 1081 } |
1073 | 1082 |
1074 void OmniboxEditModel::InternalSetUserText(const string16& text) { | 1083 void OmniboxEditModel::InternalSetUserText(const string16& text) { |
1075 user_text_ = text; | 1084 user_text_ = text; |
1076 just_deleted_text_ = false; | 1085 just_deleted_text_ = false; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 } | 1290 } |
1282 | 1291 |
1283 void OmniboxEditModel::ClassifyStringForPasteAndGo( | 1292 void OmniboxEditModel::ClassifyStringForPasteAndGo( |
1284 const string16& text, | 1293 const string16& text, |
1285 AutocompleteMatch* match, | 1294 AutocompleteMatch* match, |
1286 GURL* alternate_nav_url) const { | 1295 GURL* alternate_nav_url) const { |
1287 DCHECK(match); | 1296 DCHECK(match); |
1288 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, | 1297 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, |
1289 string16(), false, false, match, alternate_nav_url); | 1298 string16(), false, false, match, alternate_nav_url); |
1290 } | 1299 } |
| 1300 |
| 1301 void OmniboxEditModel::SetCaretVisibilityInternal( |
| 1302 bool visible, |
| 1303 OmniboxFocusChangeReason reason) { |
| 1304 if (has_focus_ && visible != is_caret_visible_) { |
| 1305 InstantController* instant = controller_->GetInstant(); |
| 1306 if (instant) { |
| 1307 instant->OmniboxFocusChanged(visible ? FOCUS_VISIBLE : FOCUS_INVISIBLE, |
| 1308 reason, NULL); |
| 1309 } |
| 1310 is_caret_visible_ = visible; |
| 1311 view_->ApplyCaretVisibility(); |
| 1312 } |
| 1313 } |
OLD | NEW |