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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 } | 218 } |
219 | 219 |
220 void OmniboxViewViews::Update() { | 220 void OmniboxViewViews::Update() { |
221 UpdatePlaceholderText(); | 221 UpdatePlaceholderText(); |
222 | 222 |
223 const ToolbarModel::SecurityLevel old_security_level = security_level_; | 223 const ToolbarModel::SecurityLevel old_security_level = security_level_; |
224 security_level_ = controller()->GetToolbarModel()->GetSecurityLevel(false); | 224 security_level_ = controller()->GetToolbarModel()->GetSecurityLevel(false); |
225 if (model()->UpdatePermanentText()) { | 225 if (model()->UpdatePermanentText()) { |
226 // Something visibly changed. Re-enable URL replacement. | 226 // Something visibly changed. Re-enable URL replacement. |
227 controller()->GetToolbarModel()->set_url_replacement_enabled(true); | 227 controller()->GetToolbarModel()->set_url_replacement_enabled(true); |
228 controller()->GetToolbarModel()->set_origin_chip_enabled(true); | |
229 model()->UpdatePermanentText(); | 228 model()->UpdatePermanentText(); |
230 | 229 |
231 // Select all the new text if the user had all the old text selected, or if | 230 // Select all the new text if the user had all the old text selected, or if |
232 // there was no previous text (for new tab page URL replacement extensions). | 231 // there was no previous text (for new tab page URL replacement extensions). |
233 // This makes one particular case better: the user clicks in the box to | 232 // This makes one particular case better: the user clicks in the box to |
234 // change it right before the permanent URL is changed. Since the new URL | 233 // change it right before the permanent URL is changed. Since the new URL |
235 // is still fully selected, the user's typing will replace the edit contents | 234 // is still fully selected, the user's typing will replace the edit contents |
236 // as they'd intended. | 235 // as they'd intended. |
237 const bool was_select_all = IsSelectAll(); | 236 const bool was_select_all = IsSelectAll(); |
238 const bool was_reversed = GetSelectedRange().is_reversed(); | 237 const bool was_reversed = GetSelectedRange().is_reversed(); |
239 | 238 |
240 RevertAll(); | 239 RevertAll(); |
241 | 240 |
242 // Only select all when we have focus. If we don't have focus, selecting | 241 // Only select all when we have focus. If we don't have focus, selecting |
243 // all is unnecessary since the selection will change on regaining focus, | 242 // all is unnecessary since the selection will change on regaining focus, |
244 // and can in fact cause artifacts, e.g. if the user is on the NTP and | 243 // and can in fact cause artifacts, e.g. if the user is on the NTP and |
245 // clicks a link to navigate, causing |was_select_all| to be vacuously true | 244 // clicks a link to navigate, causing |was_select_all| to be vacuously true |
246 // for the empty omnibox, and we then select all here, leading to the | 245 // for the empty omnibox, and we then select all here, leading to the |
247 // trailing portion of a long URL being scrolled into view. We could try | 246 // trailing portion of a long URL being scrolled into view. We could try |
248 // and address cases like this, but it seems better to just not muck with | 247 // and address cases like this, but it seems better to just not muck with |
249 // things when the omnibox isn't focused to begin with. | 248 // things when the omnibox isn't focused to begin with. |
250 if (was_select_all && model()->has_focus()) | 249 if (was_select_all && model()->has_focus()) |
251 SelectAll(was_reversed); | 250 SelectAll(was_reversed); |
252 } else if (old_security_level != security_level_) { | 251 } else if (old_security_level != security_level_) { |
253 EmphasizeURLComponents(); | 252 EmphasizeURLComponents(); |
254 } | 253 } |
255 } | 254 } |
256 | 255 |
257 void OmniboxViewViews::UpdatePlaceholderText() { | 256 void OmniboxViewViews::UpdatePlaceholderText() { |
258 if (chrome::ShouldDisplayOriginChip() || | 257 if (OmniboxFieldTrial::DisplayHintTextWhenPossible()) |
259 OmniboxFieldTrial::DisplayHintTextWhenPossible()) | |
260 set_placeholder_text(GetHintText()); | 258 set_placeholder_text(GetHintText()); |
261 } | 259 } |
262 | 260 |
263 base::string16 OmniboxViewViews::GetText() const { | 261 base::string16 OmniboxViewViews::GetText() const { |
264 // TODO(oshima): IME support | 262 // TODO(oshima): IME support |
265 return text(); | 263 return text(); |
266 } | 264 } |
267 | 265 |
268 void OmniboxViewViews::SetUserText(const base::string16& text, | 266 void OmniboxViewViews::SetUserText(const base::string16& text, |
269 const base::string16& display_text, | 267 const base::string16& display_text, |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 if (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) { | 679 if (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) { |
682 // When the user has clicked and released to give us focus, select all | 680 // When the user has clicked and released to give us focus, select all |
683 // unless we're omitting the URL (in which case refining an existing query | 681 // unless we're omitting the URL (in which case refining an existing query |
684 // is common enough that we do click-to-place-cursor). | 682 // is common enough that we do click-to-place-cursor). |
685 if (select_all_on_mouse_release_ && | 683 if (select_all_on_mouse_release_ && |
686 !controller()->GetToolbarModel()->WouldReplaceURL()) { | 684 !controller()->GetToolbarModel()->WouldReplaceURL()) { |
687 // Select all in the reverse direction so as not to scroll the caret | 685 // Select all in the reverse direction so as not to scroll the caret |
688 // into view and shift the contents jarringly. | 686 // into view and shift the contents jarringly. |
689 SelectAll(true); | 687 SelectAll(true); |
690 } | 688 } |
691 | |
692 HandleOriginChipMouseRelease(); | |
693 } | 689 } |
694 select_all_on_mouse_release_ = false; | 690 select_all_on_mouse_release_ = false; |
695 } | 691 } |
696 | 692 |
697 bool OmniboxViewViews::OnKeyPressed(const ui::KeyEvent& event) { | 693 bool OmniboxViewViews::OnKeyPressed(const ui::KeyEvent& event) { |
698 // Skip processing of [Alt]+<num-pad digit> Unicode alt key codes. | 694 // Skip processing of [Alt]+<num-pad digit> Unicode alt key codes. |
699 // Otherwise, if num-lock is off, the events are handled as [Up], [Down], etc. | 695 // Otherwise, if num-lock is off, the events are handled as [Up], [Down], etc. |
700 if (event.IsUnicodeKeyCode()) | 696 if (event.IsUnicodeKeyCode()) |
701 return views::Textfield::OnKeyPressed(event); | 697 return views::Textfield::OnKeyPressed(event); |
702 | 698 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | 773 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || |
778 event->type() == ui::ET_GESTURE_PINCH_BEGIN || | 774 event->type() == ui::ET_GESTURE_PINCH_BEGIN || |
779 event->type() == ui::ET_GESTURE_LONG_PRESS || | 775 event->type() == ui::ET_GESTURE_LONG_PRESS || |
780 event->type() == ui::ET_GESTURE_LONG_TAP) { | 776 event->type() == ui::ET_GESTURE_LONG_TAP) { |
781 select_all_on_gesture_tap_ = false; | 777 select_all_on_gesture_tap_ = false; |
782 } | 778 } |
783 } | 779 } |
784 | 780 |
785 void OmniboxViewViews::AboutToRequestFocusFromTabTraversal(bool reverse) { | 781 void OmniboxViewViews::AboutToRequestFocusFromTabTraversal(bool reverse) { |
786 views::Textfield::AboutToRequestFocusFromTabTraversal(reverse); | 782 views::Textfield::AboutToRequestFocusFromTabTraversal(reverse); |
787 // Tabbing into the omnibox should affect the origin chip in the same way | |
788 // clicking it should. | |
789 HandleOriginChipMouseRelease(); | |
790 } | 783 } |
791 | 784 |
792 bool OmniboxViewViews::SkipDefaultKeyEventProcessing( | 785 bool OmniboxViewViews::SkipDefaultKeyEventProcessing( |
793 const ui::KeyEvent& event) { | 786 const ui::KeyEvent& event) { |
794 if (views::FocusManager::IsTabTraversalKeyEvent(event) && | 787 if (views::FocusManager::IsTabTraversalKeyEvent(event) && |
795 ((model()->is_keyword_hint() && !event.IsShiftDown()) || | 788 ((model()->is_keyword_hint() && !event.IsShiftDown()) || |
796 model()->popup_model()->IsOpen())) { | 789 model()->popup_model()->IsOpen())) { |
797 return true; | 790 return true; |
798 } | 791 } |
799 return Textfield::SkipDefaultKeyEventProcessing(event); | 792 return Textfield::SkipDefaultKeyEventProcessing(event); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 // Save the user's existing selection to restore it later. | 829 // Save the user's existing selection to restore it later. |
837 saved_selection_for_focus_change_ = GetSelectedRange(); | 830 saved_selection_for_focus_change_ = GetSelectedRange(); |
838 | 831 |
839 views::Textfield::OnBlur(); | 832 views::Textfield::OnBlur(); |
840 model()->OnWillKillFocus(); | 833 model()->OnWillKillFocus(); |
841 CloseOmniboxPopup(); | 834 CloseOmniboxPopup(); |
842 | 835 |
843 // Tell the model to reset itself. | 836 // Tell the model to reset itself. |
844 model()->OnKillFocus(); | 837 model()->OnKillFocus(); |
845 | 838 |
846 // Ignore loss of focus if we lost focus because the website settings popup | |
847 // is open. When the popup is destroyed, focus will return to the Omnibox. | |
848 if (!WebsiteSettingsPopupView::IsPopupShowing()) | |
849 OnDidKillFocus(); | |
850 | |
851 // Make sure the beginning of the text is visible. | 839 // Make sure the beginning of the text is visible. |
852 SelectRange(gfx::Range(0)); | 840 SelectRange(gfx::Range(0)); |
853 } | 841 } |
854 | 842 |
855 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { | 843 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { |
856 if (command_id == IDS_APP_PASTE) | 844 if (command_id == IDS_APP_PASTE) |
857 return !read_only() && !GetClipboardText().empty(); | 845 return !read_only() && !GetClipboardText().empty(); |
858 if (command_id == IDS_PASTE_AND_GO) | 846 if (command_id == IDS_PASTE_AND_GO) |
859 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); | 847 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); |
860 if (command_id == IDS_SHOW_URL) | 848 if (command_id == IDS_SHOW_URL) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 } | 1000 } |
1013 | 1001 |
1014 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { | 1002 void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { |
1015 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); | 1003 int paste_position = menu_contents->GetIndexOfCommandId(IDS_APP_PASTE); |
1016 DCHECK_GE(paste_position, 0); | 1004 DCHECK_GE(paste_position, 0); |
1017 menu_contents->InsertItemWithStringIdAt( | 1005 menu_contents->InsertItemWithStringIdAt( |
1018 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); | 1006 paste_position + 1, IDS_PASTE_AND_GO, IDS_PASTE_AND_GO); |
1019 | 1007 |
1020 menu_contents->AddSeparator(ui::NORMAL_SEPARATOR); | 1008 menu_contents->AddSeparator(ui::NORMAL_SEPARATOR); |
1021 | 1009 |
1022 if (chrome::IsQueryExtractionEnabled() || chrome::ShouldDisplayOriginChip()) { | 1010 if (chrome::IsQueryExtractionEnabled()) { |
1023 int select_all_position = menu_contents->GetIndexOfCommandId( | 1011 int select_all_position = menu_contents->GetIndexOfCommandId( |
1024 IDS_APP_SELECT_ALL); | 1012 IDS_APP_SELECT_ALL); |
1025 DCHECK_GE(select_all_position, 0); | 1013 DCHECK_GE(select_all_position, 0); |
1026 menu_contents->InsertItemWithStringIdAt( | 1014 menu_contents->InsertItemWithStringIdAt( |
1027 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); | 1015 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); |
1028 } | 1016 } |
1029 | 1017 |
1030 // Minor note: We use IDC_ for command id here while the underlying textfield | 1018 // Minor note: We use IDC_ for command id here while the underlying textfield |
1031 // is using IDS_ for all its command ids. This is because views cannot depend | 1019 // is using IDS_ for all its command ids. This is because views cannot depend |
1032 // on IDC_ for now. | 1020 // on IDC_ for now. |
1033 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 1021 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
1034 IDS_EDIT_SEARCH_ENGINES); | 1022 IDS_EDIT_SEARCH_ENGINES); |
1035 } | 1023 } |
OLD | NEW |