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

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: Use display_text.length() as caret pos for KEYBOARD_SHORTCUT 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
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 user_input_in_progress_ ? view_->GetText() : base::string16();
548 size_t caret_pos = display_text.length();
549 if (entry_method == KeywordModeEntryMethod::QUESTION_MARK) {
550 display_text.erase(0, 1);
551 caret_pos = 0;
552 }
553
554 InternalSetUserText(display_text);
555 view_->SetWindowTextAndCaretPos(display_text, caret_pos, true, false);
556 if (entry_method == KeywordModeEntryMethod::KEYBOARD_SHORTCUT)
557 view_->SelectAll(false);
570 558
571 UMA_HISTOGRAM_ENUMERATION( 559 UMA_HISTOGRAM_ENUMERATION(
572 kEnteredKeywordModeHistogram, static_cast<int>(entry_method), 560 kEnteredKeywordModeHistogram, static_cast<int>(entry_method),
573 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS)); 561 static_cast<int>(KeywordModeEntryMethod::NUM_ITEMS));
574 } 562 }
575 563
576 void OmniboxEditModel::OpenMatch(AutocompleteMatch match, 564 void OmniboxEditModel::OpenMatch(AutocompleteMatch match,
577 WindowOpenDisposition disposition, 565 WindowOpenDisposition disposition,
578 const GURL& alternate_nav_url, 566 const GURL& alternate_nav_url,
579 const base::string16& pasted_text, 567 const base::string16& pasted_text,
580 size_t index) { 568 size_t index) {
581 const base::TimeTicks& now(base::TimeTicks::Now()); 569 const base::TimeTicks& now(base::TimeTicks::Now());
582 base::TimeDelta elapsed_time_since_user_first_modified_omnibox( 570 base::TimeDelta elapsed_time_since_user_first_modified_omnibox(
583 now - time_user_first_modified_omnibox_); 571 now - time_user_first_modified_omnibox_);
584 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime( 572 autocomplete_controller()->UpdateMatchDestinationURLWithQueryFormulationTime(
585 elapsed_time_since_user_first_modified_omnibox, &match); 573 elapsed_time_since_user_first_modified_omnibox, &match);
586 574
587 base::string16 input_text(pasted_text); 575 base::string16 input_text(pasted_text);
588 if (input_text.empty()) 576 if (input_text.empty())
589 input_text = user_input_in_progress_ ? user_text_ : permanent_text_; 577 input_text = user_input_in_progress_ ? user_text_ : permanent_text_;
590 // Create a dummy AutocompleteInput for use in calling SuggestExactInput() 578 // Create a dummy AutocompleteInput for use in calling SuggestExactInput()
591 // to create an alternate navigational match. 579 // to create an alternate navigational match.
592 AutocompleteInput alternate_input( 580 AutocompleteInput alternate_input(
593 input_text, base::string16::npos, std::string(), 581 input_text, base::string16::npos, std::string(),
594 // Somehow we can occasionally get here with no active tab. It's not 582 // Somehow we can occasionally get here with no active tab. It's not
595 // clear why this happens. 583 // clear why this happens.
596 client_->CurrentPageExists() ? client_->GetURL() : GURL(), ClassifyPage(), 584 client_->GetURL(), ClassifyPage(), false, false, true, true, false,
597 false, false, true, true, false, client_->GetSchemeClassifier()); 585 client_->GetSchemeClassifier());
598 std::unique_ptr<OmniboxNavigationObserver> observer( 586 std::unique_ptr<OmniboxNavigationObserver> observer(
599 client_->CreateOmniboxNavigationObserver( 587 client_->CreateOmniboxNavigationObserver(
600 input_text, match, 588 input_text, match,
601 autocomplete_controller()->history_url_provider()->SuggestExactInput( 589 autocomplete_controller()->history_url_provider()->SuggestExactInput(
602 alternate_input, alternate_nav_url, false))); 590 alternate_input, alternate_nav_url, false)));
603 591
604 base::TimeDelta elapsed_time_since_last_change_to_default_match( 592 base::TimeDelta elapsed_time_since_last_change_to_default_match(
605 now - autocomplete_controller()->last_time_default_match_changed()); 593 now - autocomplete_controller()->last_time_default_match_changed());
606 DCHECK(match.provider); 594 DCHECK(match.provider);
607 // These elapsed times don't really make sense for matches that come from 595 // 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 } 1197 }
1210 1198
1211 if (!state_changes.text_differs || !allow_keyword_ui_change || 1199 if (!state_changes.text_differs || !allow_keyword_ui_change ||
1212 (state_changes.just_deleted_text && no_selection) || 1200 (state_changes.just_deleted_text && no_selection) ||
1213 is_keyword_selected() || (paste_state_ != NONE)) 1201 is_keyword_selected() || (paste_state_ != NONE))
1214 return true; 1202 return true;
1215 1203
1216 // If the user input a "?" at the beginning of the text, put them into 1204 // If the user input a "?" at the beginning of the text, put them into
1217 // keyword mode for their default search provider. 1205 // keyword mode for their default search provider.
1218 if ((state_changes.new_sel_start == 1) && (user_text_[0] == '?')) { 1206 if ((state_changes.new_sel_start == 1) && (user_text_[0] == '?')) {
1219 view_->SetUserText(user_text_.substr(1));
1220 EnterKeywordModeForDefaultSearchProvider( 1207 EnterKeywordModeForDefaultSearchProvider(
1221 KeywordModeEntryMethod::QUESTION_MARK); 1208 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; 1209 return false;
1225 } 1210 }
1226 1211
1227 // Change to keyword mode if the user is now pressing space after a keyword 1212 // 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 1213 // 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 1214 // 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 1215 // 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 1216 // 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_| 1217 // MaybeAcceptKeywordBySpace() to look at |keyword_| and |is_keyword_hint_|
1233 // to determine what keyword, if any, is applicable. 1218 // 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 1416 // Update state and notify view if the omnibox has focus and the caret
1432 // visibility changed. 1417 // visibility changed.
1433 const bool was_caret_visible = is_caret_visible(); 1418 const bool was_caret_visible = is_caret_visible();
1434 focus_state_ = state; 1419 focus_state_ = state;
1435 if (focus_state_ != OMNIBOX_FOCUS_NONE && 1420 if (focus_state_ != OMNIBOX_FOCUS_NONE &&
1436 is_caret_visible() != was_caret_visible) 1421 is_caret_visible() != was_caret_visible)
1437 view_->ApplyCaretVisibility(); 1422 view_->ApplyCaretVisibility();
1438 1423
1439 client_->OnFocusChanged(focus_state_, reason); 1424 client_->OnFocusChanged(focus_state_, reason);
1440 } 1425 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/omnibox_edit_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698