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

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: Refactor 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(), client_->GetURL(),
425 ClassifyPage(),
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 keyword_ =
564 keyword_ = client_->GetTemplateURLService()-> 542 client_->GetTemplateURLService()->GetDefaultSearchProvider()->keyword();
565 GetDefaultSearchProvider()->keyword();
566 is_keyword_hint_ = false; 543 is_keyword_hint_ = false;
567 keyword_mode_entry_method_ = entry_method; 544 keyword_mode_entry_method_ = entry_method;
568 545
569 StartAutocomplete(false, false); 546 base::string16 display_text;
547 switch (entry_method) {
548 case KeywordModeEntryMethod::KEYBOARD_SHORTCUT:
549 display_text =
550 user_input_in_progress_ ? view_->GetText() : base::string16();
551 InternalSetUserText(display_text);
Peter Kasting 2016/10/25 20:21:27 Nit: Could move these next lines (that the two cas
Tom (Use chromium acct) 2016/10/25 21:55:09 Done.
552 view_->SetWindowTextAndCaretPos(display_text, 0, true, false);
Tom (Use chromium acct) 2016/10/25 21:55:09 I wanted to clear up one thing I noticed before I
Peter Kasting 2016/10/25 22:06:11 Good question. In the case of QUESTION_MARK it sh
553 view_->SelectAll(false);
554 break;
555 case KeywordModeEntryMethod::QUESTION_MARK:
556 DCHECK_EQ(base::ASCIIToUTF16("?"), view_->GetText().substr(0, 1));
557 display_text = view_->GetText().substr(1);
558 InternalSetUserText(display_text);
559 view_->SetWindowTextAndCaretPos(display_text, 0, true, false);
560 break;
561 default:
562 NOTREACHED();
563 }
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,
580 size_t index) { 574 size_t index) {
581 const base::TimeTicks& now(base::TimeTicks::Now()); 575 const base::TimeTicks& now(base::TimeTicks::Now());
582 base::TimeDelta elapsed_time_since_user_first_modified_omnibox( 576 base::TimeDelta elapsed_time_since_user_first_modified_omnibox(
583 now - time_user_first_modified_omnibox_); 577 now - time_user_first_modified_omnibox_);
584 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime( 578 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime(
585 elapsed_time_since_user_first_modified_omnibox, &match); 579 elapsed_time_since_user_first_modified_omnibox, &match);
586 580
587 base::string16 input_text(pasted_text); 581 base::string16 input_text(pasted_text);
588 if (input_text.empty()) 582 if (input_text.empty())
589 input_text = user_input_in_progress_ ? user_text_ : permanent_text_; 583 input_text = user_input_in_progress_ ? user_text_ : permanent_text_;
590 // Create a dummy AutocompleteInput for use in calling SuggestExactInput() 584 // Create a dummy AutocompleteInput for use in calling SuggestExactInput()
591 // to create an alternate navigational match. 585 // to create an alternate navigational match.
592 AutocompleteInput alternate_input( 586 AutocompleteInput alternate_input(
593 input_text, base::string16::npos, std::string(), 587 input_text, base::string16::npos, std::string(),
594 // Somehow we can occasionally get here with no active tab. It's not 588 // Somehow we can occasionally get here with no active tab. It's not
595 // clear why this happens. 589 // clear why this happens.
596 client_->CurrentPageExists() ? client_->GetURL() : GURL(), ClassifyPage(), 590 client_->GetURL(), ClassifyPage(), false, false, true, true, false,
597 false, false, true, true, false, client_->GetSchemeClassifier()); 591 client_->GetSchemeClassifier());
598 std::unique_ptr<OmniboxNavigationObserver> observer( 592 std::unique_ptr<OmniboxNavigationObserver> observer(
599 client_->CreateOmniboxNavigationObserver( 593 client_->CreateOmniboxNavigationObserver(
600 input_text, match, 594 input_text, match,
601 autocomplete_controller()->history_url_provider()->SuggestExactInput( 595 autocomplete_controller()->history_url_provider()->SuggestExactInput(
602 alternate_input, alternate_nav_url, false))); 596 alternate_input, alternate_nav_url, false)));
603 597
604 base::TimeDelta elapsed_time_since_last_change_to_default_match( 598 base::TimeDelta elapsed_time_since_last_change_to_default_match(
605 now - autocomplete_controller()->last_time_default_match_changed()); 599 now - autocomplete_controller()->last_time_default_match_changed());
606 DCHECK(match.provider); 600 DCHECK(match.provider);
607 // These elapsed times don't really make sense for matches that come from 601 // These elapsed times don't really make sense for matches that come from
(...skipping 601 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