OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 // TODO(oshima): IME support | 272 // TODO(oshima): IME support |
273 return text(); | 273 return text(); |
274 } | 274 } |
275 | 275 |
276 void OmniboxViewViews::SetUserText(const base::string16& text, | 276 void OmniboxViewViews::SetUserText(const base::string16& text, |
277 bool update_popup) { | 277 bool update_popup) { |
278 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); | 278 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); |
279 OmniboxView::SetUserText(text, update_popup); | 279 OmniboxView::SetUserText(text, update_popup); |
280 } | 280 } |
281 | 281 |
282 void OmniboxViewViews::SetForcedQuery() { | 282 void OmniboxViewViews::EnterKeywordModeForDefaultSearchProvider() { |
283 const base::string16 current_text(text()); | 283 // Transition the user into keyword mode using their default search provider. |
284 const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16); | 284 // Select their query if they typed one. |
285 if (start == base::string16::npos || (current_text[start] != '?')) | 285 model()->EnterKeywordModeForDefaultSearchProvider( |
286 OmniboxView::SetUserText(base::ASCIIToUTF16("?")); | 286 KeywordModeEntryMethod::KEYBOARD_SHORTCUT); |
287 else | 287 SelectRange(gfx::Range(text().size(), 0)); |
288 SelectRange(gfx::Range(current_text.size(), start + 1)); | |
289 } | 288 } |
290 | 289 |
291 void OmniboxViewViews::GetSelectionBounds( | 290 void OmniboxViewViews::GetSelectionBounds( |
292 base::string16::size_type* start, | 291 base::string16::size_type* start, |
293 base::string16::size_type* end) const { | 292 base::string16::size_type* end) const { |
294 const gfx::Range range = GetSelectedRange(); | 293 const gfx::Range range = GetSelectedRange(); |
295 *start = static_cast<size_t>(range.start()); | 294 *start = static_cast<size_t>(range.start()); |
296 *end = static_cast<size_t>(range.end()); | 295 *end = static_cast<size_t>(range.end()); |
297 } | 296 } |
298 | 297 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 } | 400 } |
402 | 401 |
403 void OmniboxViewViews::OnPaste() { | 402 void OmniboxViewViews::OnPaste() { |
404 const base::string16 text(GetClipboardText()); | 403 const base::string16 text(GetClipboardText()); |
405 if (!text.empty()) { | 404 if (!text.empty()) { |
406 OnBeforePossibleChange(); | 405 OnBeforePossibleChange(); |
407 // Record this paste, so we can do different behavior. | 406 // Record this paste, so we can do different behavior. |
408 model()->OnPaste(); | 407 model()->OnPaste(); |
409 // Force a Paste operation to trigger the text_changed code in | 408 // Force a Paste operation to trigger the text_changed code in |
410 // OnAfterPossibleChange(), even if identical contents are pasted. | 409 // OnAfterPossibleChange(), even if identical contents are pasted. |
411 text_before_change_.clear(); | 410 state_before_change_.text.clear(); |
412 InsertOrReplaceText(text); | 411 InsertOrReplaceText(text); |
413 OnAfterPossibleChange(true); | 412 OnAfterPossibleChange(true); |
414 } | 413 } |
415 } | 414 } |
416 | 415 |
417 bool OmniboxViewViews::HandleEarlyTabActions(const ui::KeyEvent& event) { | 416 bool OmniboxViewViews::HandleEarlyTabActions(const ui::KeyEvent& event) { |
418 // This must run before accelerator handling invokes a focus change on tab. | 417 // This must run before accelerator handling invokes a focus change on tab. |
419 // Note the parallel with SkipDefaultKeyEventProcessing above. | 418 // Note the parallel with SkipDefaultKeyEventProcessing above. |
420 if (!views::FocusManager::IsTabTraversalKeyEvent(event)) | 419 if (!views::FocusManager::IsTabTraversalKeyEvent(event)) |
421 return false; | 420 return false; |
422 | 421 |
423 if (model()->is_keyword_hint() && !event.IsShiftDown()) | 422 if (model()->is_keyword_hint() && !event.IsShiftDown()) |
424 return model()->AcceptKeyword(ENTERED_KEYWORD_MODE_VIA_TAB); | 423 return model()->AcceptKeyword(KeywordModeEntryMethod::TAB); |
425 | 424 |
426 if (!model()->popup_model()->IsOpen()) | 425 if (!model()->popup_model()->IsOpen()) |
427 return false; | 426 return false; |
428 | 427 |
429 if (event.IsShiftDown() && | 428 if (event.IsShiftDown() && |
430 (model()->popup_model()->selected_line_state() == | 429 (model()->popup_model()->selected_line_state() == |
431 OmniboxPopupModel::KEYWORD)) | 430 OmniboxPopupModel::KEYWORD)) |
432 model()->ClearKeyword(); | 431 model()->ClearKeyword(); |
433 else | 432 else |
434 model()->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1); | 433 model()->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 void OmniboxViewViews::OnRevertTemporaryText() { | 518 void OmniboxViewViews::OnRevertTemporaryText() { |
520 SelectRange(saved_temporary_selection_); | 519 SelectRange(saved_temporary_selection_); |
521 // We got here because the user hit the Escape key. We explicitly don't call | 520 // We got here because the user hit the Escape key. We explicitly don't call |
522 // TextChanged(), since OmniboxPopupModel::ResetToDefaultMatch() has already | 521 // TextChanged(), since OmniboxPopupModel::ResetToDefaultMatch() has already |
523 // been called by now, and it would've called TextChanged() if it was | 522 // been called by now, and it would've called TextChanged() if it was |
524 // warranted. | 523 // warranted. |
525 } | 524 } |
526 | 525 |
527 void OmniboxViewViews::OnBeforePossibleChange() { | 526 void OmniboxViewViews::OnBeforePossibleChange() { |
528 // Record our state. | 527 // Record our state. |
529 text_before_change_ = text(); | 528 GetState(&state_before_change_); |
530 sel_before_change_ = GetSelectedRange(); | |
531 ime_composing_before_change_ = IsIMEComposing(); | 529 ime_composing_before_change_ = IsIMEComposing(); |
532 } | 530 } |
533 | 531 |
534 bool OmniboxViewViews::OnAfterPossibleChange(bool allow_keyword_ui_change) { | 532 bool OmniboxViewViews::OnAfterPossibleChange(bool allow_keyword_ui_change) { |
535 // See if the text or selection have changed since OnBeforePossibleChange(). | 533 // See if the text or selection have changed since OnBeforePossibleChange(). |
536 const base::string16 new_text = text(); | 534 State new_state; |
537 const gfx::Range new_sel = GetSelectedRange(); | 535 GetState(&new_state); |
538 const bool text_changed = (new_text != text_before_change_) || | 536 OmniboxView::StateChanges state_changes = |
| 537 GetStateChanges(state_before_change_, new_state); |
| 538 |
| 539 state_changes.text_differs = |
| 540 state_changes.text_differs || |
539 (ime_composing_before_change_ != IsIMEComposing()); | 541 (ime_composing_before_change_ != IsIMEComposing()); |
540 const bool selection_differs = | |
541 !((sel_before_change_.is_empty() && new_sel.is_empty()) || | |
542 sel_before_change_.EqualsIgnoringDirection(new_sel)); | |
543 | |
544 // When the user has deleted text, we don't allow inline autocomplete. Make | |
545 // sure to not flag cases like selecting part of the text and then pasting | |
546 // (or typing) the prefix of that selection. (We detect these by making | |
547 // sure the caret, which should be after any insertion, hasn't moved | |
548 // forward of the old selection start.) | |
549 const bool just_deleted_text = | |
550 (text_before_change_.length() > new_text.length()) && | |
551 (new_sel.start() <= sel_before_change_.GetMin()); | |
552 | 542 |
553 const bool something_changed = model()->OnAfterPossibleChange( | 543 const bool something_changed = model()->OnAfterPossibleChange( |
554 text_before_change_, new_text, new_sel.start(), new_sel.end(), | 544 state_changes, allow_keyword_ui_change && !IsIMEComposing()); |
555 selection_differs, text_changed, just_deleted_text, | |
556 allow_keyword_ui_change && !IsIMEComposing()); | |
557 | 545 |
558 // If only selection was changed, we don't need to call model()'s | 546 // If only selection was changed, we don't need to call model()'s |
559 // OnChanged() method, which is called in TextChanged(). | 547 // OnChanged() method, which is called in TextChanged(). |
560 // But we still need to call EmphasizeURLComponents() to make sure the text | 548 // But we still need to call EmphasizeURLComponents() to make sure the text |
561 // attributes are updated correctly. | 549 // attributes are updated correctly. |
562 if (something_changed && text_changed) | 550 if (something_changed && |
| 551 (state_changes.text_differs || state_changes.keyword_differs)) |
563 TextChanged(); | 552 TextChanged(); |
564 else if (selection_differs) | 553 else if (state_changes.selection_differs) |
565 EmphasizeURLComponents(); | 554 EmphasizeURLComponents(); |
566 else if (delete_at_end_pressed_) | 555 else if (delete_at_end_pressed_) |
567 model()->OnChanged(); | 556 model()->OnChanged(); |
568 | 557 |
569 return something_changed; | 558 return something_changed; |
570 } | 559 } |
571 | 560 |
572 gfx::NativeView OmniboxViewViews::GetNativeView() const { | 561 gfx::NativeView OmniboxViewViews::GetNativeView() const { |
573 return GetWidget()->GetNativeView(); | 562 return GetWidget()->GetNativeView(); |
574 } | 563 } |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 menu_contents->InsertItemWithStringIdAt( | 1072 menu_contents->InsertItemWithStringIdAt( |
1084 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); | 1073 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); |
1085 } | 1074 } |
1086 | 1075 |
1087 // Minor note: We use IDC_ for command id here while the underlying textfield | 1076 // Minor note: We use IDC_ for command id here while the underlying textfield |
1088 // is using IDS_ for all its command ids. This is because views cannot depend | 1077 // is using IDS_ for all its command ids. This is because views cannot depend |
1089 // on IDC_ for now. | 1078 // on IDC_ for now. |
1090 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 1079 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
1091 IDS_EDIT_SEARCH_ENGINES); | 1080 IDS_EDIT_SEARCH_ENGINES); |
1092 } | 1081 } |
OLD | NEW |