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/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 } // namespace | 135 } // namespace |
136 | 136 |
137 /////////////////////////////////////////////////////////////////////////////// | 137 /////////////////////////////////////////////////////////////////////////////// |
138 // OmniboxEditModel::State | 138 // OmniboxEditModel::State |
139 | 139 |
140 OmniboxEditModel::State::State(bool user_input_in_progress, | 140 OmniboxEditModel::State::State(bool user_input_in_progress, |
141 const string16& user_text, | 141 const string16& user_text, |
142 const string16& gray_text, | 142 const string16& gray_text, |
143 const string16& keyword, | 143 const string16& keyword, |
144 bool is_keyword_hint, | 144 bool is_keyword_hint, |
145 OmniboxFocusState focus_state) | 145 OmniboxFocusState focus_state, |
146 FocusSource focus_source) | |
146 : user_input_in_progress(user_input_in_progress), | 147 : user_input_in_progress(user_input_in_progress), |
147 user_text(user_text), | 148 user_text(user_text), |
148 gray_text(gray_text), | 149 gray_text(gray_text), |
149 keyword(keyword), | 150 keyword(keyword), |
150 is_keyword_hint(is_keyword_hint), | 151 is_keyword_hint(is_keyword_hint), |
151 focus_state(focus_state) { | 152 focus_state(focus_state), |
153 focus_source(focus_source) { | |
152 } | 154 } |
153 | 155 |
154 OmniboxEditModel::State::~State() { | 156 OmniboxEditModel::State::~State() { |
155 } | 157 } |
156 | 158 |
157 /////////////////////////////////////////////////////////////////////////////// | 159 /////////////////////////////////////////////////////////////////////////////// |
158 // OmniboxEditModel | 160 // OmniboxEditModel |
159 | 161 |
160 OmniboxEditModel::OmniboxEditModel(OmniboxView* view, | 162 OmniboxEditModel::OmniboxEditModel(OmniboxView* view, |
161 OmniboxEditController* controller, | 163 OmniboxEditController* controller, |
162 Profile* profile) | 164 Profile* profile) |
163 : view_(view), | 165 : view_(view), |
164 controller_(controller), | 166 controller_(controller), |
165 focus_state_(OMNIBOX_FOCUS_NONE), | 167 focus_state_(OMNIBOX_FOCUS_NONE), |
168 focus_source_(INVALID), | |
166 user_input_in_progress_(false), | 169 user_input_in_progress_(false), |
167 just_deleted_text_(false), | 170 just_deleted_text_(false), |
168 has_temporary_text_(false), | 171 has_temporary_text_(false), |
169 paste_state_(NONE), | 172 paste_state_(NONE), |
170 control_key_state_(UP), | 173 control_key_state_(UP), |
171 is_keyword_hint_(false), | 174 is_keyword_hint_(false), |
172 profile_(profile), | 175 profile_(profile), |
173 in_revert_(false), | 176 in_revert_(false), |
174 allow_exact_keyword_match_(false) { | 177 allow_exact_keyword_match_(false) { |
175 omnibox_controller_.reset(new OmniboxController(this, profile)); | 178 omnibox_controller_.reset(new OmniboxController(this, profile)); |
(...skipping 19 matching lines...) Expand all Loading... | |
195 } else { | 198 } else { |
196 InternalSetUserText(user_text); | 199 InternalSetUserText(user_text); |
197 } | 200 } |
198 } | 201 } |
199 | 202 |
200 return State(user_input_in_progress_, | 203 return State(user_input_in_progress_, |
201 user_text_, | 204 user_text_, |
202 view_->GetGrayTextAutocompletion(), | 205 view_->GetGrayTextAutocompletion(), |
203 keyword_, | 206 keyword_, |
204 is_keyword_hint_, | 207 is_keyword_hint_, |
205 focus_state_); | 208 focus_state_, |
209 focus_source_); | |
206 } | 210 } |
207 | 211 |
208 void OmniboxEditModel::RestoreState(const State& state) { | 212 void OmniboxEditModel::RestoreState(const State& state) { |
209 SetFocusState(state.focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); | 213 SetFocusState(state.focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); |
214 focus_source_ = state.focus_source; | |
210 // Restore any user editing. | 215 // Restore any user editing. |
211 if (state.user_input_in_progress) { | 216 if (state.user_input_in_progress) { |
212 // NOTE: Be sure and set keyword-related state BEFORE invoking | 217 // NOTE: Be sure and set keyword-related state BEFORE invoking |
213 // DisplayTextFromUserText(), as its result depends upon this state. | 218 // DisplayTextFromUserText(), as its result depends upon this state. |
214 keyword_ = state.keyword; | 219 keyword_ = state.keyword; |
215 is_keyword_hint_ = state.is_keyword_hint; | 220 is_keyword_hint_ = state.is_keyword_hint; |
216 view_->SetUserText(state.user_text, | 221 view_->SetUserText(state.user_text, |
217 DisplayTextFromUserText(state.user_text), false); | 222 DisplayTextFromUserText(state.user_text), false); |
218 view_->SetGrayTextAutocompletion(state.gray_text); | 223 view_->SetGrayTextAutocompletion(state.gray_text); |
219 } | 224 } |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 // TODO(jered): Rip this out along with StartZeroSuggest. | 841 // TODO(jered): Rip this out along with StartZeroSuggest. |
837 autocomplete_controller()->StopZeroSuggest(); | 842 autocomplete_controller()->StopZeroSuggest(); |
838 delegate_->NotifySearchTabHelper(user_input_in_progress_, !in_revert_); | 843 delegate_->NotifySearchTabHelper(user_input_in_progress_, !in_revert_); |
839 } | 844 } |
840 | 845 |
841 void OmniboxEditModel::OnKillFocus() { | 846 void OmniboxEditModel::OnKillFocus() { |
842 // TODO(samarth): determine if it is safe to move the call to | 847 // TODO(samarth): determine if it is safe to move the call to |
843 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us | 848 // OmniboxFocusChanged() from OnWillKillFocus() to here, which would let us |
844 // just call SetFocusState() to handle the state change. | 849 // just call SetFocusState() to handle the state change. |
845 focus_state_ = OMNIBOX_FOCUS_NONE; | 850 focus_state_ = OMNIBOX_FOCUS_NONE; |
851 focus_source_ = INVALID; | |
846 control_key_state_ = UP; | 852 control_key_state_ = UP; |
847 paste_state_ = NONE; | 853 paste_state_ = NONE; |
848 } | 854 } |
849 | 855 |
850 bool OmniboxEditModel::OnEscapeKeyPressed() { | 856 bool OmniboxEditModel::OnEscapeKeyPressed() { |
851 if (has_temporary_text_) { | 857 if (has_temporary_text_) { |
852 if (CurrentMatch(NULL).destination_url != original_url_) { | 858 if (CurrentMatch(NULL).destination_url != original_url_) { |
853 RevertTemporaryText(true); | 859 RevertTemporaryText(true); |
854 return true; | 860 return true; |
855 } | 861 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1009 bool just_deleted_text, | 1015 bool just_deleted_text, |
1010 bool allow_keyword_ui_change) { | 1016 bool allow_keyword_ui_change) { |
1011 // Update the paste state as appropriate: if we're just finishing a paste | 1017 // Update the paste state as appropriate: if we're just finishing a paste |
1012 // that replaced all the text, preserve that information; otherwise, if we've | 1018 // that replaced all the text, preserve that information; otherwise, if we've |
1013 // made some other edit, clear paste tracking. | 1019 // made some other edit, clear paste tracking. |
1014 if (paste_state_ == PASTING) | 1020 if (paste_state_ == PASTING) |
1015 paste_state_ = PASTED; | 1021 paste_state_ = PASTED; |
1016 else if (text_differs) | 1022 else if (text_differs) |
1017 paste_state_ = NONE; | 1023 paste_state_ = NONE; |
1018 | 1024 |
1019 // Restore caret visibility whenever the user changes text or selection in the | 1025 if (text_differs || selection_differs) { |
1020 // omnibox. | 1026 // Record current focus state for this input if we haven't already. |
1021 if (text_differs || selection_differs) | 1027 DCHECK_NE(OMNIBOX_FOCUS_NONE, focus_state_); |
1028 if (focus_source_ == INVALID) | |
1029 focus_source_ = focus_state_ == OMNIBOX_FOCUS_VISIBLE ? OMNIBOX : FAKEBOX; | |
Peter Kasting
2013/08/02 17:59:38
Nit: Parens around binary subexpr
samarth
2013/08/06 16:29:25
Done.
| |
1030 | |
1031 // Restore caret visibility whenever the user changes text or selection in | |
1032 // the omnibox. | |
1022 SetFocusState(OMNIBOX_FOCUS_VISIBLE, OMNIBOX_FOCUS_CHANGE_TYPING); | 1033 SetFocusState(OMNIBOX_FOCUS_VISIBLE, OMNIBOX_FOCUS_CHANGE_TYPING); |
1034 } | |
1023 | 1035 |
1024 // Modifying the selection counts as accepting the autocompleted text. | 1036 // Modifying the selection counts as accepting the autocompleted text. |
1025 const bool user_text_changed = | 1037 const bool user_text_changed = |
1026 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); | 1038 text_differs || (selection_differs && !inline_autocomplete_text_.empty()); |
1027 | 1039 |
1028 // If something has changed while the control key is down, prevent | 1040 // If something has changed while the control key is down, prevent |
1029 // "ctrl-enter" until the control key is released. When we do this, we need | 1041 // "ctrl-enter" until the control key is released. When we do this, we need |
1030 // to update the popup if it's open, since the desired_tld will have changed. | 1042 // to update the popup if it's open, since the desired_tld will have changed. |
1031 if ((text_differs || selection_differs) && | 1043 if ((text_differs || selection_differs) && |
1032 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { | 1044 (control_key_state_ == DOWN_WITHOUT_CHANGE)) { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1252 case 0x3000: // Ideographic Space | 1264 case 0x3000: // Ideographic Space |
1253 return true; | 1265 return true; |
1254 default: | 1266 default: |
1255 return false; | 1267 return false; |
1256 } | 1268 } |
1257 } | 1269 } |
1258 | 1270 |
1259 AutocompleteInput::PageClassification OmniboxEditModel::ClassifyPage() const { | 1271 AutocompleteInput::PageClassification OmniboxEditModel::ClassifyPage() const { |
1260 if (!delegate_->CurrentPageExists()) | 1272 if (!delegate_->CurrentPageExists()) |
1261 return AutocompleteInput::OTHER; | 1273 return AutocompleteInput::OTHER; |
1262 if (delegate_->IsInstantNTP()) | 1274 if (delegate_->IsInstantNTP()) { |
1263 return AutocompleteInput::INSTANT_NEW_TAB_PAGE; | 1275 // Note that we treat OMNIBOX as the source if focus_source_ is INVALID, |
1276 // i.e., if input isn't actually in progress. | |
1277 return focus_source_ == FAKEBOX ? | |
Peter Kasting
2013/08/02 17:59:38
Nit: Parens here too
samarth
2013/08/06 16:29:25
Done.
| |
1278 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_FAKEBOX_AS_STARTING_FOCUS : | |
1279 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_OMNIBOX_AS_STARTING_FOCUS; | |
1280 } | |
1264 const GURL& gurl = delegate_->GetURL(); | 1281 const GURL& gurl = delegate_->GetURL(); |
1265 if (!gurl.is_valid()) | 1282 if (!gurl.is_valid()) |
1266 return AutocompleteInput::INVALID_SPEC; | 1283 return AutocompleteInput::INVALID_SPEC; |
1267 const std::string& url = gurl.spec(); | 1284 const std::string& url = gurl.spec(); |
1268 if (url == chrome::kChromeUINewTabURL) | 1285 if (url == chrome::kChromeUINewTabURL) |
1269 return AutocompleteInput::NEW_TAB_PAGE; | 1286 return AutocompleteInput::NEW_TAB_PAGE; |
1270 if (url == content::kAboutBlankURL) | 1287 if (url == content::kAboutBlankURL) |
1271 return AutocompleteInput::BLANK; | 1288 return AutocompleteInput::BLANK; |
1272 if (url == profile()->GetPrefs()->GetString(prefs::kHomePage)) | 1289 if (url == profile()->GetPrefs()->GetString(prefs::kHomePage)) |
1273 return AutocompleteInput::HOMEPAGE; | 1290 return AutocompleteInput::HOMEPAGE; |
(...skipping 22 matching lines...) Expand all Loading... | |
1296 instant->OmniboxFocusChanged(state, reason, NULL); | 1313 instant->OmniboxFocusChanged(state, reason, NULL); |
1297 | 1314 |
1298 // Update state and notify view if the omnibox has focus and the caret | 1315 // Update state and notify view if the omnibox has focus and the caret |
1299 // visibility changed. | 1316 // visibility changed. |
1300 const bool was_caret_visible = is_caret_visible(); | 1317 const bool was_caret_visible = is_caret_visible(); |
1301 focus_state_ = state; | 1318 focus_state_ = state; |
1302 if (focus_state_ != OMNIBOX_FOCUS_NONE && | 1319 if (focus_state_ != OMNIBOX_FOCUS_NONE && |
1303 is_caret_visible() != was_caret_visible) | 1320 is_caret_visible() != was_caret_visible) |
1304 view_->ApplyCaretVisibility(); | 1321 view_->ApplyCaretVisibility(); |
1305 } | 1322 } |
OLD | NEW |