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

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

Issue 2435103002: Omnibox: Preserve display text and select all on a focus search (Closed)
Patch Set: Remove printfs Created 4 years, 1 month 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 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 view_->SetWindowTextAndCaretPos(permanent_text_, 403 view_->SetWindowTextAndCaretPos(permanent_text_,
404 has_focus() ? permanent_text_.length() : 0, 404 has_focus() ? permanent_text_.length() : 0,
405 false, true); 405 false, true);
406 client_->OnRevert(); 406 client_->OnRevert();
407 } 407 }
408 408
409 void OmniboxEditModel::StartAutocomplete(bool has_selected_text, 409 void OmniboxEditModel::StartAutocomplete(bool has_selected_text,
410 bool prevent_inline_autocomplete) { 410 bool prevent_inline_autocomplete) {
411 const base::string16 input_text = MaybePrependKeyword(user_text_); 411 const base::string16 input_text = MaybePrependKeyword(user_text_);
412 412
413 // Compute the cursor position. There are three cases: 413 size_t start, cursor_position;
414 // 1. The user is in the midst of typing; there is no inline autocompletion. 414 view_->GetSelectionBounds(&start, &cursor_position);
415 // 2. The user is in the midst of typing; there is inline autocompletion.
416 // 3. The user is not in the midst of typing, but is triggering this some
417 // other way, e.g. hitting ctrl-K while the view is showing the permanent
418 // or temporary text.
419 size_t cursor_position;
420 if (!has_temporary_text_ && (user_text_ == view_->GetText())) {
421 // Case 1 above. In this case there's a meaningful current cursor position,
422 // so we read it from the view. (Note that if there is a selected range,
423 // the "cursor position" is considered to be the selection's end.)
424 size_t start;
425 view_->GetSelectionBounds(&start, &cursor_position);
426 415
427 // For keyword searches, the text that AutocompleteInput expects is of the 416 // For keyword searches, the text that AutocompleteInput expects is
428 // form "<keyword> <query>", where our query is |user_text_|. So we need to 417 // of the form "<keyword> <query>", where our query is |user_text_|.
429 // adjust the cursor position forward by the length of any keyword added by 418 // So we need to adjust the cursor position forward by the length of
430 // MaybePrependKeyword() above. 419 // any keyword added by MaybePrependKeyword() above.
420 if (is_keyword_selected())
431 cursor_position += input_text.length() - user_text_.length(); 421 cursor_position += input_text.length() - user_text_.length();
432 } else {
433 // Case 2 or 3 above. In case 2, the existing inline autocompletion will be
434 // ignored for this next autocomplete run. The current cursor position is
435 // always effectively "the end of the input text". (If the user changes
436 // this cursor position by arrowing, it will accept the inline
437 // autocompletion, which would put us in case 1.) In case 3, there is no
438 // meaningful current cursor position; the correct default behavior is to
439 // simply claim the cursor is at the end of the input.
440 cursor_position = input_text.length();
441 }
442 422
443 GURL current_url;
444 if (client_->CurrentPageExists())
445 current_url = client_->GetURL();
446 input_ = AutocompleteInput( 423 input_ = AutocompleteInput(
447 input_text, cursor_position, std::string(), current_url, ClassifyPage(), 424 input_text, cursor_position, std::string(),
425 client_->CurrentPageExists() ? client_->GetURL() : GURL(), ClassifyPage(),
Peter Kasting 2016/10/25 02:46:33 Seems like this isn't the only place that does thi
Tom (Use chromium acct) 2016/10/25 18:50:31 Done.
448 prevent_inline_autocomplete || just_deleted_text_ || 426 prevent_inline_autocomplete || just_deleted_text_ ||
449 (has_selected_text && inline_autocomplete_text_.empty()) || 427 (has_selected_text && inline_autocomplete_text_.empty()) ||
450 (paste_state_ != NONE), 428 (paste_state_ != NONE),
451 is_keyword_selected(), 429 is_keyword_selected(),
452 is_keyword_selected() || allow_exact_keyword_match_, true, false, 430 is_keyword_selected() || allow_exact_keyword_match_, true, false,
453 client_->GetSchemeClassifier()); 431 client_->GetSchemeClassifier());
454 432
455 omnibox_controller_->StartAutocomplete(input_); 433 omnibox_controller_->StartAutocomplete(input_);
456 } 434 }
457 435
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 531
554 DCHECK(popup_model()); 532 DCHECK(popup_model());
555 view_->OpenMatch(match, disposition, alternate_nav_url, base::string16(), 533 view_->OpenMatch(match, disposition, alternate_nav_url, base::string16(),
556 popup_model()->selected_line()); 534 popup_model()->selected_line());
557 } 535 }
558 536
559 void OmniboxEditModel::EnterKeywordModeForDefaultSearchProvider( 537 void OmniboxEditModel::EnterKeywordModeForDefaultSearchProvider(
560 KeywordModeEntryMethod entry_method) { 538 KeywordModeEntryMethod entry_method) {
561 autocomplete_controller()->Stop(false); 539 autocomplete_controller()->Stop(false);
562 540
563 user_input_in_progress_ = true; 541 switch (entry_method) {
564 keyword_ = client_->GetTemplateURLService()-> 542 case KeywordModeEntryMethod::KEYBOARD_SHORTCUT:
Tom (Use chromium acct) 2016/10/25 02:11:08 I don't know if it's better for EnterKeywordModeFo
565 GetDefaultSearchProvider()->keyword(); 543 view_->SetUserText(
544 user_input_in_progress_ ? view_->GetText() : base::string16(), false);
545 view_->SelectAll(false);
Peter Kasting 2016/10/25 02:46:33 Nit: Personally I'd write this as a call to the Mo
Tom (Use chromium acct) 2016/10/25 18:50:31 The doc on SetUserText says only the view should c
546 break;
547 case KeywordModeEntryMethod::QUESTION_MARK:
548 DCHECK_EQ(base::ASCIIToUTF16("?"), view_->GetText().substr(0, 1));
549 view_->SetUserText(view_->GetText().substr(1), false);
Peter Kasting 2016/10/25 02:46:33 No reason for "view_->" on this, that's just a wra
Tom (Use chromium acct) 2016/10/25 18:50:31 Ok, done. I've also made the changes above. I us
550 view_->SetWindowTextAndCaretPos(view_->GetText(), 0, false, false);
Tom (Use chromium acct) 2016/10/25 02:11:08 It would be useful if OmniboxView had a SetUserTex
Peter Kasting 2016/10/25 02:46:33 I don't think so, but we can revisit after respond
551 break;
552 default:
553 NOTREACHED();
554 }
555
556 keyword_ =
557 client_->GetTemplateURLService()->GetDefaultSearchProvider()->keyword();
566 is_keyword_hint_ = false; 558 is_keyword_hint_ = false;
567 keyword_mode_entry_method_ = entry_method; 559 keyword_mode_entry_method_ = entry_method;
Peter Kasting 2016/10/25 02:46:33 Nit: I would move this block above the switch. Se
568 560
569 StartAutocomplete(false, false); 561 size_t sel_start, sel_end;
562 view_->GetSelectionBounds(&sel_start, &sel_end);
563 StartAutocomplete(sel_start != sel_end, false);
Peter Kasting 2016/10/25 02:46:33 The primary difference between this and just calli
Tom (Use chromium acct) 2016/10/25 18:50:31 I'd argue we want the UpdatePopup() behavior becau
570 564
571 UMA_HISTOGRAM_ENUMERATION( 565 UMA_HISTOGRAM_ENUMERATION(
572 kEnteredKeywordModeHistogram, static_cast<int>(entry_method), 566 kEnteredKeywordModeHistogram, static_cast<int>(entry_method),
573 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS)); 567 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS));
574 } 568 }
575 569
576 void OmniboxEditModel::OpenMatch(AutocompleteMatch match, 570 void OmniboxEditModel::OpenMatch(AutocompleteMatch match,
577 WindowOpenDisposition disposition, 571 WindowOpenDisposition disposition,
578 const GURL& alternate_nav_url, 572 const GURL& alternate_nav_url,
579 const base::string16& pasted_text, 573 const base::string16& pasted_text,
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after
1209 } 1203 }
1210 1204
1211 if (!state_changes.text_differs || !allow_keyword_ui_change || 1205 if (!state_changes.text_differs || !allow_keyword_ui_change ||
1212 (state_changes.just_deleted_text && no_selection) || 1206 (state_changes.just_deleted_text && no_selection) ||
1213 is_keyword_selected() || (paste_state_ != NONE)) 1207 is_keyword_selected() || (paste_state_ != NONE))
1214 return true; 1208 return true;
1215 1209
1216 // If the user input a "?" at the beginning of the text, put them into 1210 // If the user input a "?" at the beginning of the text, put them into
1217 // keyword mode for their default search provider. 1211 // keyword mode for their default search provider.
1218 if ((state_changes.new_sel_start == 1) && (user_text_[0] == '?')) { 1212 if ((state_changes.new_sel_start == 1) && (user_text_[0] == '?')) {
1219 view_->SetUserText(user_text_.substr(1));
1220 EnterKeywordModeForDefaultSearchProvider( 1213 EnterKeywordModeForDefaultSearchProvider(
1221 KeywordModeEntryMethod::QUESTION_MARK); 1214 KeywordModeEntryMethod::QUESTION_MARK);
1222 // Set the caret position to 0 without changing the user text.
1223 view_->SetWindowTextAndCaretPos(view_->GetText(), 0, false, false);
1224 return false; 1215 return false;
1225 } 1216 }
1226 1217
1227 // Change to keyword mode if the user is now pressing space after a keyword 1218 // Change to keyword mode if the user is now pressing space after a keyword
1228 // name. Note that if this is the case, then even if there was no keyword 1219 // name. Note that if this is the case, then even if there was no keyword
1229 // hint when we entered this function (e.g. if the user has used space to 1220 // hint when we entered this function (e.g. if the user has used space to
1230 // replace some selected text that was adjoined to this keyword), there will 1221 // replace some selected text that was adjoined to this keyword), there will
1231 // be one now because of the call to UpdatePopup() above; so it's safe for 1222 // be one now because of the call to UpdatePopup() above; so it's safe for
1232 // MaybeAcceptKeywordBySpace() to look at |keyword_| and |is_keyword_hint_| 1223 // MaybeAcceptKeywordBySpace() to look at |keyword_| and |is_keyword_hint_|
1233 // to determine what keyword, if any, is applicable. 1224 // to determine what keyword, if any, is applicable.
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1431 // Update state and notify view if the omnibox has focus and the caret 1422 // Update state and notify view if the omnibox has focus and the caret
1432 // visibility changed. 1423 // visibility changed.
1433 const bool was_caret_visible = is_caret_visible(); 1424 const bool was_caret_visible = is_caret_visible();
1434 focus_state_ = state; 1425 focus_state_ = state;
1435 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1426 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1436 is_caret_visible() != was_caret_visible) 1427 is_caret_visible() != was_caret_visible)
1437 view_->ApplyCaretVisibility(); 1428 view_->ApplyCaretVisibility();
1438 1429
1439 client_->OnFocusChanged(focus_state_, reason); 1430 client_->OnFocusChanged(focus_state_, reason);
1440 } 1431 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698