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...) 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...) 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. | |
Peter Kasting
2012/12/08 01:12:21
But in that case why do we need to send an update?
samarth
2012/12/08 01:51:34
It's because InstantController also needs to know
Peter Kasting
2012/12/08 02:07:52
Perhaps we should move this to the top of the func
samarth
2012/12/10 23:45:00
Done.
| |
727 if (InstantController* instant = controller_->GetInstant()) { | |
728 instant->OmniboxFocusChanged(FOCUS_VISIBLE, | |
729 FOCUS_CHANGE_EXPLICIT, NULL); | |
730 } | |
723 | 731 |
724 content::WebContents* web_contents = controller_->GetWebContents(); | 732 content::WebContents* web_contents = controller_->GetWebContents(); |
725 if (web_contents) { | 733 if (web_contents) { |
726 // TODO(jered): We may want to merge this into Start() and just call that | 734 // 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 | 735 // 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 | 736 // 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 | 737 // the actual underlying current URL, e.g. if we're on the NTP and the |
730 // |permanent_text_| is empty. | 738 // |permanent_text_| is empty. |
731 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), | 739 autocomplete_controller_->StartZeroSuggest(web_contents->GetURL(), |
732 user_text_); | 740 user_text_); |
733 } | 741 } |
734 | 742 |
735 NotifySearchTabHelper(); | 743 NotifySearchTabHelper(); |
736 } | 744 } |
737 | 745 |
738 void OmniboxEditModel::SetCaretVisibility(bool visible) { | 746 void OmniboxEditModel::SetCaretVisibility(bool visible) { |
739 if (has_focus_ && visible != is_caret_visible_) { | 747 SetCaretVisibilityInternal(visible, FOCUS_CHANGE_EXPLICIT); |
740 is_caret_visible_ = visible; | |
741 view_->ApplyCaretVisibility(); | |
742 } | |
743 } | 748 } |
744 | 749 |
745 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { | 750 void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { |
746 SetInstantSuggestion(InstantSuggestion()); | 751 SetInstantSuggestion(InstantSuggestion()); |
747 | 752 |
748 if (InstantController* instant = controller_->GetInstant()) | 753 if (InstantController* instant = controller_->GetInstant()) { |
749 instant->OmniboxLostFocus(view_gaining_focus); | 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...) 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 315 matching lines...) Loading... | |
1281 } | 1288 } |
1282 | 1289 |
1283 void OmniboxEditModel::ClassifyStringForPasteAndGo( | 1290 void OmniboxEditModel::ClassifyStringForPasteAndGo( |
1284 const string16& text, | 1291 const string16& text, |
1285 AutocompleteMatch* match, | 1292 AutocompleteMatch* match, |
1286 GURL* alternate_nav_url) const { | 1293 GURL* alternate_nav_url) const { |
1287 DCHECK(match); | 1294 DCHECK(match); |
1288 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, | 1295 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(text, |
1289 string16(), false, false, match, alternate_nav_url); | 1296 string16(), false, false, match, alternate_nav_url); |
1290 } | 1297 } |
1298 | |
1299 void OmniboxEditModel::SetCaretVisibilityInternal( | |
1300 bool visible, | |
1301 OmniboxFocusChangeReason reason) { | |
1302 if (has_focus_ && visible != is_caret_visible_) { | |
1303 if (InstantController* instant = controller_->GetInstant()) { | |
1304 instant->OmniboxFocusChanged(visible ? FOCUS_VISIBLE : FOCUS_INVISIBLE, | |
1305 reason, NULL); | |
1306 } | |
1307 is_caret_visible_ = visible; | |
1308 view_->ApplyCaretVisibility(); | |
1309 } | |
1310 } | |
OLD | NEW |