Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1292)

Side by Side Diff: components/omnibox/browser/omnibox_edit_model.cc

Issue 2510373003: Cleanup: Remove "gray text" logic from Omnibox (Closed)
Patch Set: review2 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/omnibox/browser/omnibox_edit_model.h" 5 #include "components/omnibox/browser/omnibox_edit_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 const char kFocusToOpenTimeHistogram[] = 83 const char kFocusToOpenTimeHistogram[] =
84 "Omnibox.FocusToOpenTimeAnyPopupState2"; 84 "Omnibox.FocusToOpenTimeAnyPopupState2";
85 85
86 } // namespace 86 } // namespace
87 87
88 88
89 // OmniboxEditModel::State ---------------------------------------------------- 89 // OmniboxEditModel::State ----------------------------------------------------
90 90
91 OmniboxEditModel::State::State(bool user_input_in_progress, 91 OmniboxEditModel::State::State(bool user_input_in_progress,
92 const base::string16& user_text, 92 const base::string16& user_text,
93 const base::string16& gray_text,
94 const base::string16& keyword, 93 const base::string16& keyword,
95 bool is_keyword_hint, 94 bool is_keyword_hint,
96 KeywordModeEntryMethod keyword_mode_entry_method, 95 KeywordModeEntryMethod keyword_mode_entry_method,
97 OmniboxFocusState focus_state, 96 OmniboxFocusState focus_state,
98 FocusSource focus_source, 97 FocusSource focus_source,
99 const AutocompleteInput& autocomplete_input) 98 const AutocompleteInput& autocomplete_input)
100 : user_input_in_progress(user_input_in_progress), 99 : user_input_in_progress(user_input_in_progress),
101 user_text(user_text), 100 user_text(user_text),
102 gray_text(gray_text),
103 keyword(keyword), 101 keyword(keyword),
104 is_keyword_hint(is_keyword_hint), 102 is_keyword_hint(is_keyword_hint),
105 keyword_mode_entry_method(keyword_mode_entry_method), 103 keyword_mode_entry_method(keyword_mode_entry_method),
106 focus_state(focus_state), 104 focus_state(focus_state),
107 focus_source(focus_source), 105 focus_source(focus_source),
108 autocomplete_input(autocomplete_input) { 106 autocomplete_input(autocomplete_input) {
109 } 107 }
110 108
111 OmniboxEditModel::State::State(const State& other) = default; 109 OmniboxEditModel::State::State(const State& other) = default;
112 110
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 base::AutoReset<bool> tmp(&in_revert_, true); 150 base::AutoReset<bool> tmp(&in_revert_, true);
153 view_->RevertAll(); 151 view_->RevertAll();
154 view_->SelectAll(true); 152 view_->SelectAll(true);
155 } else { 153 } else {
156 InternalSetUserText(display_text); 154 InternalSetUserText(display_text);
157 } 155 }
158 } 156 }
159 157
160 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress", 158 UMA_HISTOGRAM_BOOLEAN("Omnibox.SaveStateForTabSwitch.UserInputInProgress",
161 user_input_in_progress_); 159 user_input_in_progress_);
162 return State( 160 return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_,
163 user_input_in_progress_, user_text_, view_->GetGrayTextAutocompletion(), 161 keyword_mode_entry_method_, focus_state_, focus_source_, input_);
164 keyword_, is_keyword_hint_, keyword_mode_entry_method_,
165 focus_state_, focus_source_, input_);
166 } 162 }
167 163
168 void OmniboxEditModel::RestoreState(const State* state) { 164 void OmniboxEditModel::RestoreState(const State* state) {
169 // We need to update the permanent text correctly and revert the view 165 // We need to update the permanent text correctly and revert the view
170 // regardless of whether there is saved state. 166 // regardless of whether there is saved state.
171 permanent_text_ = controller_->GetToolbarModel()->GetFormattedURL(nullptr); 167 permanent_text_ = controller_->GetToolbarModel()->GetFormattedURL(nullptr);
172 view_->RevertAll(); 168 view_->RevertAll();
173 // Restore the autocomplete controller's input, or clear it if this is a new 169 // Restore the autocomplete controller's input, or clear it if this is a new
174 // tab. 170 // tab.
175 input_ = state ? state->autocomplete_input : AutocompleteInput(); 171 input_ = state ? state->autocomplete_input : AutocompleteInput();
176 if (!state) 172 if (!state)
177 return; 173 return;
178 174
179 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH); 175 SetFocusState(state->focus_state, OMNIBOX_FOCUS_CHANGE_TAB_SWITCH);
180 focus_source_ = state->focus_source; 176 focus_source_ = state->focus_source;
181 // Restore any user editing. 177 // Restore any user editing.
182 if (state->user_input_in_progress) { 178 if (state->user_input_in_progress) {
183 // NOTE: Be sure to set keyword-related state AFTER invoking 179 // NOTE: Be sure to set keyword-related state AFTER invoking
184 // SetUserText(), as SetUserText() clears the keyword state. 180 // SetUserText(), as SetUserText() clears the keyword state.
185 view_->SetUserText(state->user_text, false); 181 view_->SetUserText(state->user_text, false);
186 keyword_ = state->keyword; 182 keyword_ = state->keyword;
187 is_keyword_hint_ = state->is_keyword_hint; 183 is_keyword_hint_ = state->is_keyword_hint;
188 keyword_mode_entry_method_ = state->keyword_mode_entry_method; 184 keyword_mode_entry_method_ = state->keyword_mode_entry_method;
189 view_->SetGrayTextAutocompletion(state->gray_text);
190 } 185 }
191 } 186 }
192 187
193 AutocompleteMatch OmniboxEditModel::CurrentMatch( 188 AutocompleteMatch OmniboxEditModel::CurrentMatch(
194 GURL* alternate_nav_url) const { 189 GURL* alternate_nav_url) const {
195 // If we have a valid match use it. Otherwise get one for the current text. 190 // If we have a valid match use it. Otherwise get one for the current text.
196 AutocompleteMatch match = omnibox_controller_->current_match(); 191 AutocompleteMatch match = omnibox_controller_->current_match();
197 192
198 if (!match.destination_url.is_valid()) { 193 if (!match.destination_url.is_valid()) {
199 GetInfoForCurrentText(&match, alternate_nav_url); 194 GetInfoForCurrentText(&match, alternate_nav_url);
(...skipping 10 matching lines...) Expand all
210 // define "interacting" as "the omnibox has focus", but we still allow updates 205 // define "interacting" as "the omnibox has focus", but we still allow updates
211 // when the omnibox has focus as long as the user hasn't begun editing, isn't 206 // when the omnibox has focus as long as the user hasn't begun editing, isn't
212 // seeing zerosuggestions (because changing this text would require changing 207 // seeing zerosuggestions (because changing this text would require changing
213 // or hiding those suggestions), and hasn't toggled on "Show URL" (because 208 // or hiding those suggestions), and hasn't toggled on "Show URL" (because
214 // this update will re-enable search term replacement, which will be annoying 209 // this update will re-enable search term replacement, which will be annoying
215 // if the user is trying to copy the URL). When the omnibox doesn't have 210 // if the user is trying to copy the URL). When the omnibox doesn't have
216 // focus, we assume the user may have abandoned their interaction and it's 211 // focus, we assume the user may have abandoned their interaction and it's
217 // always safe to change the text; this also prevents someone toggling "Show 212 // always safe to change the text; this also prevents someone toggling "Show
218 // URL" (which sounds as if it might be persistent) from seeing just that URL 213 // URL" (which sounds as if it might be persistent) from seeing just that URL
219 // forever afterwards. 214 // forever afterwards.
220 //
221 // If the page is auto-committing gray text, however, we generally don't want
222 // to make any change to the edit. While auto-commits modify the underlying
223 // permanent URL, they're intended to have no effect on the user's editing
224 // process -- before and after the auto-commit, the omnibox should show the
225 // same user text and the same instant suggestion, even if the auto-commit
226 // happens while the edit doesn't have focus.
227 base::string16 new_permanent_text = 215 base::string16 new_permanent_text =
228 controller_->GetToolbarModel()->GetFormattedURL(nullptr); 216 controller_->GetToolbarModel()->GetFormattedURL(nullptr);
229 base::string16 gray_text = view_->GetGrayTextAutocompletion();
230 const bool visibly_changed_permanent_text = 217 const bool visibly_changed_permanent_text =
231 (permanent_text_ != new_permanent_text) && 218 (permanent_text_ != new_permanent_text) &&
232 (!has_focus() || (!user_input_in_progress_ && !PopupIsOpen())) && 219 (!has_focus() || (!user_input_in_progress_ && !PopupIsOpen()));
233 (gray_text.empty() || new_permanent_text != user_text_ + gray_text);
234 220
235 permanent_text_ = new_permanent_text; 221 permanent_text_ = new_permanent_text;
236 return visibly_changed_permanent_text; 222 return visibly_changed_permanent_text;
237 } 223 }
238 224
239 GURL OmniboxEditModel::PermanentURL() const { 225 GURL OmniboxEditModel::PermanentURL() const {
240 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_), 226 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_),
241 std::string()); 227 std::string());
242 } 228 }
243 229
244 void OmniboxEditModel::SetUserText(const base::string16& text) { 230 void OmniboxEditModel::SetUserText(const base::string16& text) {
245 SetInputInProgress(true); 231 SetInputInProgress(true);
246 keyword_.clear(); 232 keyword_.clear();
247 is_keyword_hint_ = false; 233 is_keyword_hint_ = false;
248 InternalSetUserText(text); 234 InternalSetUserText(text);
249 omnibox_controller_->InvalidateCurrentMatch(); 235 omnibox_controller_->InvalidateCurrentMatch();
250 paste_state_ = NONE; 236 paste_state_ = NONE;
251 has_temporary_text_ = false; 237 has_temporary_text_ = false;
252 } 238 }
253 239
254 bool OmniboxEditModel::CommitSuggestedText() {
255 const base::string16 suggestion = view_->GetGrayTextAutocompletion();
256 if (suggestion.empty())
257 return false;
258
259 const base::string16 final_text = view_->GetText() + suggestion;
260 view_->OnBeforePossibleChange();
261 view_->SetWindowTextAndCaretPos(final_text, final_text.length(), false,
262 false);
263 view_->OnAfterPossibleChange(true);
264 return true;
265 }
266
267 void OmniboxEditModel::OnChanged() { 240 void OmniboxEditModel::OnChanged() {
268 // Hide any suggestions we might be showing.
269 view_->SetGrayTextAutocompletion(base::string16());
270
271 // Don't call CurrentMatch() when there's no editing, as in this case we'll 241 // Don't call CurrentMatch() when there's no editing, as in this case we'll
272 // never actually use it. This avoids running the autocomplete providers (and 242 // never actually use it. This avoids running the autocomplete providers (and
273 // any systems they then spin up) during startup. 243 // any systems they then spin up) during startup.
274 const AutocompleteMatch& current_match = user_input_in_progress_ ? 244 const AutocompleteMatch& current_match = user_input_in_progress_ ?
275 CurrentMatch(nullptr) : AutocompleteMatch(); 245 CurrentMatch(nullptr) : AutocompleteMatch();
276 246
277 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_, 247 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_,
278 result(), PopupIsOpen(), has_focus()); 248 result(), PopupIsOpen(), has_focus());
279 controller_->OnChanged(); 249 controller_->OnChanged();
280 } 250 }
(...skipping 1135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 // Update state and notify view if the omnibox has focus and the caret 1386 // Update state and notify view if the omnibox has focus and the caret
1417 // visibility changed. 1387 // visibility changed.
1418 const bool was_caret_visible = is_caret_visible(); 1388 const bool was_caret_visible = is_caret_visible();
1419 focus_state_ = state; 1389 focus_state_ = state;
1420 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1390 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1421 is_caret_visible() != was_caret_visible) 1391 is_caret_visible() != was_caret_visible)
1422 view_->ApplyCaretVisibility(); 1392 view_->ApplyCaretVisibility();
1423 1393
1424 client_->OnFocusChanged(focus_state_, reason); 1394 client_->OnFocusChanged(focus_state_, reason);
1425 } 1395 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.h ('k') | components/omnibox/browser/omnibox_edit_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698