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

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_view_views.cc

Issue 1855423003: Interpret '?' and Ctrl-K or Ctrl-E as putting omnibox in keyword search mode for Default Search Pro… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compilation on Mac and removed forced query unit tests Created 4 years, 8 months 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 (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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 return text(); 269 return text();
270 } 270 }
271 271
272 void OmniboxViewViews::SetUserText(const base::string16& text, 272 void OmniboxViewViews::SetUserText(const base::string16& text,
273 const base::string16& display_text, 273 const base::string16& display_text,
274 bool update_popup) { 274 bool update_popup) {
275 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); 275 saved_selection_for_focus_change_ = gfx::Range::InvalidRange();
276 OmniboxView::SetUserText(text, display_text, update_popup); 276 OmniboxView::SetUserText(text, display_text, update_popup);
277 } 277 }
278 278
279 void OmniboxViewViews::SetForcedQuery() { 279 void OmniboxViewViews::EnterKeywordModeForDefaultSearchProvider() {
280 const base::string16 current_text(text()); 280 // Transition the user into keyword mode using their default search provider.
281 const size_t start = current_text.find_first_not_of(base::kWhitespaceUTF16); 281 // Select their query if they typed one.
282 if (start == base::string16::npos || (current_text[start] != '?')) 282 model()->EnterKeywordModeForDefaultSearchProvider(
283 OmniboxView::SetUserText(base::ASCIIToUTF16("?")); 283 METHOD_KEYBOARD_SHORTCUT);
284 else 284 SelectRange(gfx::Range(text().size(), 0));
285 SelectRange(gfx::Range(current_text.size(), start + 1));
286 } 285 }
287 286
288 void OmniboxViewViews::GetSelectionBounds( 287 void OmniboxViewViews::GetSelectionBounds(
289 base::string16::size_type* start, 288 base::string16::size_type* start,
290 base::string16::size_type* end) const { 289 base::string16::size_type* end) const {
291 const gfx::Range range = GetSelectedRange(); 290 const gfx::Range range = GetSelectedRange();
292 *start = static_cast<size_t>(range.start()); 291 *start = static_cast<size_t>(range.start());
293 *end = static_cast<size_t>(range.end()); 292 *end = static_cast<size_t>(range.end());
294 } 293 }
295 294
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 } 410 }
412 } 411 }
413 412
414 bool OmniboxViewViews::HandleEarlyTabActions(const ui::KeyEvent& event) { 413 bool OmniboxViewViews::HandleEarlyTabActions(const ui::KeyEvent& event) {
415 // This must run before accelerator handling invokes a focus change on tab. 414 // This must run before accelerator handling invokes a focus change on tab.
416 // Note the parallel with SkipDefaultKeyEventProcessing above. 415 // Note the parallel with SkipDefaultKeyEventProcessing above.
417 if (!views::FocusManager::IsTabTraversalKeyEvent(event)) 416 if (!views::FocusManager::IsTabTraversalKeyEvent(event))
418 return false; 417 return false;
419 418
420 if (model()->is_keyword_hint() && !event.IsShiftDown()) 419 if (model()->is_keyword_hint() && !event.IsShiftDown())
421 return model()->AcceptKeyword(ENTERED_KEYWORD_MODE_VIA_TAB); 420 return model()->AcceptKeyword(METHOD_TAB);
422 421
423 if (!model()->popup_model()->IsOpen()) 422 if (!model()->popup_model()->IsOpen())
424 return false; 423 return false;
425 424
426 if (event.IsShiftDown() && 425 if (event.IsShiftDown() &&
427 (model()->popup_model()->selected_line_state() == 426 (model()->popup_model()->selected_line_state() ==
428 OmniboxPopupModel::KEYWORD)) 427 OmniboxPopupModel::KEYWORD))
429 model()->ClearKeyword(); 428 model()->ClearKeyword();
430 else 429 else
431 model()->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1); 430 model()->OnUpOrDownKeyPressed(event.IsShiftDown() ? -1 : 1);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 SelectRange(saved_temporary_selection_); 516 SelectRange(saved_temporary_selection_);
518 // We got here because the user hit the Escape key. We explicitly don't call 517 // We got here because the user hit the Escape key. We explicitly don't call
519 // TextChanged(), since OmniboxPopupModel::ResetToDefaultMatch() has already 518 // TextChanged(), since OmniboxPopupModel::ResetToDefaultMatch() has already
520 // been called by now, and it would've called TextChanged() if it was 519 // been called by now, and it would've called TextChanged() if it was
521 // warranted. 520 // warranted.
522 } 521 }
523 522
524 void OmniboxViewViews::OnBeforePossibleChange() { 523 void OmniboxViewViews::OnBeforePossibleChange() {
525 // Record our state. 524 // Record our state.
526 text_before_change_ = text(); 525 text_before_change_ = text();
526 keyword_before_change_ = model()->keyword();
527 is_keyword_selected_before_change_ = model()->is_keyword_selected();
527 sel_before_change_ = GetSelectedRange(); 528 sel_before_change_ = GetSelectedRange();
528 ime_composing_before_change_ = IsIMEComposing(); 529 ime_composing_before_change_ = IsIMEComposing();
529 } 530 }
530 531
531 bool OmniboxViewViews::OnAfterPossibleChange(bool allow_keyword_ui_change) { 532 bool OmniboxViewViews::OnAfterPossibleChange(bool allow_keyword_ui_change) {
532 // See if the text or selection have changed since OnBeforePossibleChange(). 533 // See if the text or selection have changed since OnBeforePossibleChange().
533 const base::string16 new_text = text(); 534 const base::string16 new_text = text();
535 const base::string16 new_keyword = model()->keyword();
536 const bool new_is_keyword_selected = model()->is_keyword_selected();
534 const gfx::Range new_sel = GetSelectedRange(); 537 const gfx::Range new_sel = GetSelectedRange();
535 const bool text_changed = (new_text != text_before_change_) || 538 const bool text_changed = (new_text != text_before_change_) ||
536 (ime_composing_before_change_ != IsIMEComposing()); 539 (ime_composing_before_change_ != IsIMEComposing());
540 const bool keyword_changed =
541 (new_is_keyword_selected != is_keyword_selected_before_change_) ||
542 (new_is_keyword_selected && is_keyword_selected_before_change_ &&
543 new_keyword != keyword_before_change_);
537 const bool selection_differs = 544 const bool selection_differs =
538 !((sel_before_change_.is_empty() && new_sel.is_empty()) || 545 !((sel_before_change_.is_empty() && new_sel.is_empty()) ||
539 sel_before_change_.EqualsIgnoringDirection(new_sel)); 546 sel_before_change_.EqualsIgnoringDirection(new_sel));
540 547
541 // When the user has deleted text, we don't allow inline autocomplete. Make 548 // When the user has deleted text, we don't allow inline autocomplete. Make
542 // sure to not flag cases like selecting part of the text and then pasting 549 // sure to not flag cases like selecting part of the text and then pasting
543 // (or typing) the prefix of that selection. (We detect these by making 550 // (or typing) the prefix of that selection. (We detect these by making
544 // sure the caret, which should be after any insertion, hasn't moved 551 // sure the caret, which should be after any insertion, hasn't moved
545 // forward of the old selection start.) 552 // forward of the old selection start.)
546 const bool just_deleted_text = 553 const bool just_deleted_text =
547 (text_before_change_.length() > new_text.length()) && 554 (text_before_change_.length() > new_text.length()) &&
548 (new_sel.start() <= sel_before_change_.GetMin()); 555 (new_sel.start() <= sel_before_change_.GetMin());
549 556
550 const bool something_changed = model()->OnAfterPossibleChange( 557 const bool something_changed = model()->OnAfterPossibleChange(
551 text_before_change_, new_text, new_sel.start(), new_sel.end(), 558 text_before_change_, new_text, new_sel.start(), new_sel.end(),
552 selection_differs, text_changed, just_deleted_text, 559 selection_differs, text_changed, keyword_changed, just_deleted_text,
553 allow_keyword_ui_change && !IsIMEComposing()); 560 allow_keyword_ui_change && !IsIMEComposing());
554 561
555 // If only selection was changed, we don't need to call model()'s 562 // If only selection was changed, we don't need to call model()'s
556 // OnChanged() method, which is called in TextChanged(). 563 // OnChanged() method, which is called in TextChanged().
557 // But we still need to call EmphasizeURLComponents() to make sure the text 564 // But we still need to call EmphasizeURLComponents() to make sure the text
558 // attributes are updated correctly. 565 // attributes are updated correctly.
559 if (something_changed && text_changed) 566 if (something_changed && (text_changed || keyword_changed))
560 TextChanged(); 567 TextChanged();
561 else if (selection_differs) 568 else if (selection_differs)
562 EmphasizeURLComponents(); 569 EmphasizeURLComponents();
563 else if (delete_at_end_pressed_) 570 else if (delete_at_end_pressed_)
564 model()->OnChanged(); 571 model()->OnChanged();
565 572
566 return something_changed; 573 return something_changed;
567 } 574 }
568 575
569 gfx::NativeView OmniboxViewViews::GetNativeView() const { 576 gfx::NativeView OmniboxViewViews::GetNativeView() const {
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 menu_contents->InsertItemWithStringIdAt( 1081 menu_contents->InsertItemWithStringIdAt(
1075 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); 1082 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL);
1076 } 1083 }
1077 1084
1078 // Minor note: We use IDC_ for command id here while the underlying textfield 1085 // Minor note: We use IDC_ for command id here while the underlying textfield
1079 // is using IDS_ for all its command ids. This is because views cannot depend 1086 // is using IDS_ for all its command ids. This is because views cannot depend
1080 // on IDC_ for now. 1087 // on IDC_ for now.
1081 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, 1088 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES,
1082 IDS_EDIT_SEARCH_ENGINES); 1089 IDS_EDIT_SEARCH_ENGINES);
1083 } 1090 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698