Chromium Code Reviews| 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 "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 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 false, true); | 405 false, true); |
| 406 client_->OnRevert(); | 406 client_->OnRevert(); |
| 407 } | 407 } |
| 408 | 408 |
| 409 void OmniboxEditModel::StartAutocomplete( | 409 void OmniboxEditModel::StartAutocomplete( |
| 410 bool has_selected_text, | 410 bool has_selected_text, |
| 411 bool prevent_inline_autocomplete, | 411 bool prevent_inline_autocomplete, |
| 412 bool entering_keyword_mode) { | 412 bool entering_keyword_mode) { |
| 413 size_t cursor_position; | 413 size_t cursor_position; |
| 414 const base::string16 input_text = MaybePrependKeyword(user_text_); | 414 const base::string16 input_text = MaybePrependKeyword(user_text_); |
| 415 if (inline_autocomplete_text_.empty()) { | 415 if (user_text_ == view_->GetText()) { |
|
Peter Kasting
2016/10/19 05:32:06
I spent some time tonight thinking about this and
| |
| 416 // Cursor position is equivalent to the current selection's end. | 416 // Cursor position is equivalent to the current selection's end. |
| 417 size_t start; | 417 size_t start; |
| 418 view_->GetSelectionBounds(&start, &cursor_position); | 418 view_->GetSelectionBounds(&start, &cursor_position); |
| 419 // For keyword searches, the text that AutocompleteInput expects is of the | 419 // For keyword searches, the text that AutocompleteInput expects is of the |
| 420 // form "<keyword> <query>", where our query is |user_text_|. So if we're | 420 // form "<keyword> <query>", where our query is |user_text_|. So if we're |
| 421 // in keyword mode, we need to adjust the cursor position forward by the | 421 // in keyword mode, we need to adjust the cursor position forward by the |
| 422 // length of "<keyword> ". If we're just entering keyword mode, though, we | 422 // length of "<keyword> ". If we're just entering keyword mode, though, we |
| 423 // have to avoid making this adjustment, because we haven't actually updated | 423 // have to avoid making this adjustment, because we haven't actually updated |
| 424 // |user_text_| yet, but the caller has already cleared |is_keyword_hint_|, | 424 // |user_text_| yet, but the caller has already cleared |is_keyword_hint_|, |
| 425 // so MaybePrependKeyword() will believe we are already in keyword mode, and | 425 // so MaybePrependKeyword() will believe we are already in keyword mode, and |
| 426 // will thus mis-adjust the cursor position. | 426 // will thus mis-adjust the cursor position. |
| 427 if (!entering_keyword_mode) | 427 if (!entering_keyword_mode) |
| 428 cursor_position += input_text.length() - user_text_.length(); | 428 cursor_position += input_text.length() - user_text_.length(); |
| 429 } else { | 429 } else { |
| 430 // There are some cases where StartAutocomplete() may be called | 430 // There are some cases where StartAutocomplete() may be called |
| 431 // with non-empty |inline_autocomplete_text_|. In such cases, we cannot | 431 // when |inline_autocomplete_text_| is nonempty, or when |
| 432 // use the current selection, because it could result with the cursor | 432 // view_->GetText().length() > input_text.length(). In such |
| 433 // position past the last character from the user text. Instead, | 433 // cases, we cannot use the current selection, because it could |
| 434 // we assume that the cursor is simply at the end of input. | 434 // result with the cursor position past the last character from |
| 435 // One example is when user presses Ctrl key while having a highlighted | 435 // the user text. Instead, we assume that the cursor is simply at |
| 436 // inline autocomplete text. | 436 // the end of input. One example is when user presses Ctrl key |
| 437 // while having a highlighted inline autocomplete text. | |
| 437 // TODO: Rethink how we are going to handle this case to avoid | 438 // TODO: Rethink how we are going to handle this case to avoid |
| 438 // inconsistent behavior when user presses Ctrl key. | 439 // inconsistent behavior when user presses Ctrl key. See |
| 439 // See http://crbug.com/165961 and http://crbug.com/165968 for more details. | 440 // http://crbug.com/165961 and http://crbug.com/165968 for more |
| 441 // details. | |
| 440 cursor_position = input_text.length(); | 442 cursor_position = input_text.length(); |
| 441 } | 443 } |
| 442 | 444 |
| 443 GURL current_url; | 445 GURL current_url; |
| 444 if (client_->CurrentPageExists()) | 446 if (client_->CurrentPageExists()) |
| 445 current_url = client_->GetURL(); | 447 current_url = client_->GetURL(); |
| 446 input_ = AutocompleteInput( | 448 input_ = AutocompleteInput( |
| 447 input_text, cursor_position, std::string(), current_url, ClassifyPage(), | 449 input_text, cursor_position, std::string(), current_url, ClassifyPage(), |
| 448 prevent_inline_autocomplete || just_deleted_text_ || | 450 prevent_inline_autocomplete || just_deleted_text_ || |
| 449 (has_selected_text && inline_autocomplete_text_.empty()) || | 451 (has_selected_text && inline_autocomplete_text_.empty()) || |
| (...skipping 989 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1439 // Update state and notify view if the omnibox has focus and the caret | 1441 // Update state and notify view if the omnibox has focus and the caret |
| 1440 // visibility changed. | 1442 // visibility changed. |
| 1441 const bool was_caret_visible = is_caret_visible(); | 1443 const bool was_caret_visible = is_caret_visible(); |
| 1442 focus_state_ = state; | 1444 focus_state_ = state; |
| 1443 if (focus_state_ != OMNIBOX_FOCUS_NONE && | 1445 if (focus_state_ != OMNIBOX_FOCUS_NONE && |
| 1444 is_caret_visible() != was_caret_visible) | 1446 is_caret_visible() != was_caret_visible) |
| 1445 view_->ApplyCaretVisibility(); | 1447 view_->ApplyCaretVisibility(); |
| 1446 | 1448 |
| 1447 client_->OnFocusChanged(focus_state_, reason); | 1449 client_->OnFocusChanged(focus_state_, reason); |
| 1448 } | 1450 } |
| OLD | NEW |