diff options
author | samarth@chromium.org <samarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 04:42:12 +0000 |
---|---|---|
committer | samarth@chromium.org <samarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 04:42:12 +0000 |
commit | c18cb67d0d9e18b15dd5a8903e93d91c6c63a484 (patch) | |
tree | f159e15f4412b7640155e00436c4943ea07511cb /chrome/browser/ui | |
parent | a06b5b63d01fd6b84a3edc4b422af2d533261347 (diff) | |
download | chromium_src-c18cb67d0d9e18b15dd5a8903e93d91c6c63a484.zip chromium_src-c18cb67d0d9e18b15dd5a8903e93d91c6c63a484.tar.gz chromium_src-c18cb67d0d9e18b15dd5a8903e93d91c6c63a484.tar.bz2 |
Implement {Start,Stop}CapturingKeyStrokes for Instant.
Implement {Start,Stop}CapturingKeyStrokes for Instant by transferring
focus invisibly to the omnibox. This patch implements invisible focus for
Aura. Other platforms will follow.
BUG=
Review URL: https://chromiumcodereview.appspot.com/11369137
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171160 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui')
-rw-r--r-- | chrome/browser/ui/browser_instant_controller.cc | 11 | ||||
-rw-r--r-- | chrome/browser/ui/browser_instant_controller.h | 3 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h | 1 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm | 5 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc | 5 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h | 1 | ||||
-rw-r--r-- | chrome/browser/ui/omnibox/omnibox_edit_model.cc | 25 | ||||
-rw-r--r-- | chrome/browser/ui/omnibox/omnibox_edit_model.h | 19 | ||||
-rw-r--r-- | chrome/browser/ui/omnibox/omnibox_edit_unittest.cc | 1 | ||||
-rw-r--r-- | chrome/browser/ui/omnibox/omnibox_view.h | 4 | ||||
-rw-r--r-- | chrome/browser/ui/views/omnibox/omnibox_view_views.cc | 24 | ||||
-rw-r--r-- | chrome/browser/ui/views/omnibox/omnibox_view_views.h | 5 | ||||
-rw-r--r-- | chrome/browser/ui/views/omnibox/omnibox_view_win.cc | 5 | ||||
-rw-r--r-- | chrome/browser/ui/views/omnibox/omnibox_view_win.h | 1 |
14 files changed, 102 insertions, 8 deletions
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc index b9ae096..9b82049 100644 --- a/chrome/browser/ui/browser_instant_controller.cc +++ b/chrome/browser/ui/browser_instant_controller.cc @@ -12,6 +12,7 @@ #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/omnibox/location_bar.h" +#include "chrome/browser/ui/omnibox/omnibox_view.h" #include "chrome/browser/ui/search/search.h" #include "chrome/browser/ui/search/search_tab_helper.h" #include "chrome/browser/ui/tab_contents/tab_contents.h" @@ -112,8 +113,7 @@ void BrowserInstantController::CommitInstant(content::WebContents* preview, void BrowserInstantController::SetInstantSuggestion( const InstantSuggestion& suggestion) { - if (browser_->window()->GetLocationBar()) - browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); + browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); } gfx::Rect BrowserInstantController::GetInstantBounds() { @@ -125,6 +125,13 @@ void BrowserInstantController::InstantPreviewFocused() { browser_->window()->WebContentsFocused(instant_.GetPreviewContents()); } +void BrowserInstantController::FocusOmniboxInvisibly() { + OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> + GetLocationEntry(); + omnibox_view->SetFocus(); + omnibox_view->model()->SetCaretVisibility(false); +} + content::WebContents* BrowserInstantController::GetActiveWebContents() const { return browser_->tab_strip_model()->GetActiveWebContents(); } diff --git a/chrome/browser/ui/browser_instant_controller.h b/chrome/browser/ui/browser_instant_controller.h index 44d21e7..0cbaa8e 100644 --- a/chrome/browser/ui/browser_instant_controller.h +++ b/chrome/browser/ui/browser_instant_controller.h @@ -66,6 +66,9 @@ class BrowserInstantController : public content::NotificationObserver, // to the user clicking on it. void InstantPreviewFocused(); + // Invoked by |instant_| to give the omnibox focus invisibly. + void FocusOmniboxInvisibly(); + // Invoked by |instant_| to get the currently active tab, over which the // preview would be shown. content::WebContents* GetActiveWebContents() const; diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h index 642c513..9dfdcb8 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h @@ -48,6 +48,7 @@ class OmniboxViewMac : public OmniboxView, virtual void UpdatePopup() OVERRIDE; virtual void CloseOmniboxPopup() OVERRIDE; virtual void SetFocus() OVERRIDE; + virtual void ApplyCaretVisibility() OVERRIDE; virtual void OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) OVERRIDE; diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm index 9cf11b3..84e2e17 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm @@ -362,6 +362,11 @@ void OmniboxViewMac::CloseOmniboxPopup() { void OmniboxViewMac::SetFocus() { } +void OmniboxViewMac::ApplyCaretVisibility() { + // TODO(mathp): implement for Mac. + NOTIMPLEMENTED(); +} + void OmniboxViewMac::SetText(const string16& display_text) { // If we are setting the text directly, there cannot be any suggest text. suggest_text_length_ = 0; diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc index beedfb8..9394ba7 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc @@ -426,6 +426,11 @@ void OmniboxViewGtk::SetFocus() { gtk_widget_grab_focus(text_view_); } +void OmniboxViewGtk::ApplyCaretVisibility() { + // TODO(mathp): implement for Linux. + NOTIMPLEMENTED(); +} + int OmniboxViewGtk::WidthOfTextAfterCursor() { // Not used. return -1; diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h index 6b1d60b..7c2b134 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h @@ -84,6 +84,7 @@ class OmniboxViewGtk : public OmniboxView, virtual void SelectAll(bool reversed) OVERRIDE; virtual void UpdatePopup() OVERRIDE; virtual void SetFocus() OVERRIDE; + virtual void ApplyCaretVisibility() OVERRIDE; virtual void OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) OVERRIDE; diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.cc b/chrome/browser/ui/omnibox/omnibox_edit_model.cc index 5a89651..668d9a8 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.cc +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.cc @@ -70,11 +70,13 @@ using predictors::AutocompleteActionPredictorFactory; OmniboxEditModel::State::State(bool user_input_in_progress, const string16& user_text, const string16& keyword, - bool is_keyword_hint) + bool is_keyword_hint, + bool is_caret_visible) : user_input_in_progress(user_input_in_progress), user_text(user_text), keyword(keyword), - is_keyword_hint(is_keyword_hint) { + is_keyword_hint(is_keyword_hint), + is_caret_visible(is_caret_visible) { } OmniboxEditModel::State::~State() { @@ -90,6 +92,7 @@ OmniboxEditModel::OmniboxEditModel(OmniboxView* view, popup_(NULL), controller_(controller), has_focus_(false), + is_caret_visible_(true), user_input_in_progress_(false), just_deleted_text_(false), has_temporary_text_(false), @@ -128,10 +131,12 @@ const OmniboxEditModel::State OmniboxEditModel::GetStateForTabSwitch() { } } - return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_); + return State(user_input_in_progress_, user_text_, keyword_, is_keyword_hint_, + is_caret_visible_); } void OmniboxEditModel::RestoreState(const State& state) { + SetCaretVisibility(state.is_caret_visible); // Restore any user editing. if (state.user_input_in_progress) { // NOTE: Be sure and set keyword-related state BEFORE invoking @@ -710,6 +715,8 @@ const AutocompleteResult& OmniboxEditModel::result() const { void OmniboxEditModel::OnSetFocus(bool control_down) { has_focus_ = true; control_key_state_ = control_down ? DOWN_WITHOUT_CHANGE : UP; + // Restore caret visibility whenever the user focuses back into the omnibox. + SetCaretVisibility(true); if (InstantController* instant = controller_->GetInstant()) instant->OmniboxGotFocus(); @@ -728,6 +735,13 @@ void OmniboxEditModel::OnSetFocus(bool control_down) { NotifySearchTabHelper(); } +void OmniboxEditModel::SetCaretVisibility(bool visible) { + if (has_focus_ && visible != is_caret_visible_) { + is_caret_visible_ = visible; + view_->ApplyCaretVisibility(); + } +} + void OmniboxEditModel::OnWillKillFocus(gfx::NativeView view_gaining_focus) { SetInstantSuggestion(InstantSuggestion()); @@ -935,6 +949,11 @@ bool OmniboxEditModel::OnAfterPossibleChange(const string16& old_text, else if (text_differs) paste_state_ = NONE; + // Restore caret visibility whenever the user changes text or selection in the + // omnibox. + if (text_differs || selection_differs) + SetCaretVisibility(true); + // Modifying the selection counts as accepting the autocompleted text. const bool user_text_changed = text_differs || (selection_differs && !inline_autocomplete_text_.empty()); diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.h b/chrome/browser/ui/omnibox/omnibox_edit_model.h index 6caf62f..f8444f6 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.h +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.h @@ -37,13 +37,15 @@ class OmniboxEditModel : public AutocompleteControllerDelegate { State(bool user_input_in_progress, const string16& user_text, const string16& keyword, - bool is_keyword_hint); + bool is_keyword_hint, + bool is_caret_visible); ~State(); bool user_input_in_progress; const string16 user_text; const string16 keyword; const bool is_keyword_hint; + const bool is_caret_visible; }; OmniboxEditModel(OmniboxView* view, @@ -190,6 +192,7 @@ class OmniboxEditModel : public AutocompleteControllerDelegate { size_t index); bool has_focus() const { return has_focus_; } + bool is_caret_visible() const { return is_caret_visible_; } // Accessors for keyword-related state (see comments on keyword_ and // is_keyword_hint_). @@ -213,6 +216,17 @@ class OmniboxEditModel : public AutocompleteControllerDelegate { // control key is down (at the time we're gaining focus). void OnSetFocus(bool control_down); + // Sets the visibility of the caret in the omnibox, if it has focus. The + // visibility of the caret is reset to visible if any of the following + // happens: + // - User starts typing in the omnibox + // - User clicks in the omnibox + // - Omnibox loses and then regains focus + // - SetFocus() is explicitly called again + // Caret visibility is tracked per-tab and updates automatically upon + // switching tabs. + void SetCaretVisibility(bool visible); + // Sent before |OnKillFocus| and before the popup is closed. void OnWillKillFocus(gfx::NativeView view_gaining_focus); @@ -404,6 +418,9 @@ class OmniboxEditModel : public AutocompleteControllerDelegate { // Whether the edit has focus. bool has_focus_; + // Is the caret visible? Only meaningful if has_focus_ is true. + bool is_caret_visible_; + // The URL of the currently displayed page. string16 permanent_text_; diff --git a/chrome/browser/ui/omnibox/omnibox_edit_unittest.cc b/chrome/browser/ui/omnibox/omnibox_edit_unittest.cc index 09ceaaf..47d7b32 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_unittest.cc +++ b/chrome/browser/ui/omnibox/omnibox_edit_unittest.cc @@ -44,6 +44,7 @@ class TestingOmniboxView : public OmniboxView { virtual void RevertAll() OVERRIDE {} virtual void UpdatePopup() OVERRIDE {} virtual void SetFocus() OVERRIDE {} + virtual void ApplyCaretVisibility() OVERRIDE {} virtual void OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) OVERRIDE {} diff --git a/chrome/browser/ui/omnibox/omnibox_view.h b/chrome/browser/ui/omnibox/omnibox_view.h index 692144f..7f21b56 100644 --- a/chrome/browser/ui/omnibox/omnibox_view.h +++ b/chrome/browser/ui/omnibox/omnibox_view.h @@ -152,6 +152,10 @@ class OmniboxView { // Sets the focus to the autocomplete view. virtual void SetFocus() = 0; + // Shows or hides the caret based on whether the model's is_caret_visible() is + // true. + virtual void ApplyCaretVisibility() = 0; + // Called when the temporary text in the model may have changed. // |display_text| is the new text to show; |save_original_selection| is true // when there wasn't previously a temporary text and thus we need to save off diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc index 091b9f7..bda1e48 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc @@ -224,7 +224,8 @@ OmniboxViewViews::OmniboxViewViews(OmniboxEditController* controller, delete_at_end_pressed_(false), location_bar_view_(location_bar), ime_candidate_window_open_(false), - select_all_on_mouse_release_(false) { + select_all_on_mouse_release_(false), + visible_caret_color_(SK_ColorBLACK) { } OmniboxViewViews::~OmniboxViewViews() { @@ -368,6 +369,10 @@ void OmniboxViewViews::HandleMousePressEvent(const ui::MouseEvent& event) { select_all_on_mouse_release_ = (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && !textfield_->HasFocus(); + // Restore caret visibility whenever the user clicks in the the omnibox. This + // is not always covered by OnSetFocus() because when clicking while the + // omnibox has invisible focus does not trigger a new OnSetFocus() call. + model()->SetCaretVisibility(true); } void OmniboxViewViews::HandleMouseDragEvent(const ui::MouseEvent& event) { @@ -445,7 +450,7 @@ void OmniboxViewViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { } //////////////////////////////////////////////////////////////////////////////// -// OmniboxViewViews, AutocopmleteEditView implementation: +// OmniboxViewViews, OmniboxView implementation: void OmniboxViewViews::SaveStateToTab(WebContents* tab) { DCHECK(tab); @@ -574,10 +579,25 @@ void OmniboxViewViews::UpdatePopup() { } void OmniboxViewViews::SetFocus() { + // Restore caret visibility if focused explicitly. We need to do this here + // because if we already have invisible focus, the RequestFocus() call below + // will short-circuit, preventing us from reaching + // OmniboxEditModel::OnSetFocus(), which handles restoring visibility when we + // didn't previously have focus. + model()->SetCaretVisibility(true); // In views-implementation, the focus is on textfield rather than OmniboxView. textfield_->RequestFocus(); } +void OmniboxViewViews::ApplyCaretVisibility() { + if (textfield_->cursor_color() != textfield_->background_color()) + visible_caret_color_ = textfield_->cursor_color(); + // Setting the color of the text cursor (caret) to the background color + // effectively hides it. + textfield_->SetCursorColor(model()->is_caret_visible() ? + visible_caret_color_ : textfield_->background_color()); +} + void OmniboxViewViews::OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) { diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_views.h b/chrome/browser/ui/views/omnibox/omnibox_view_views.h index 23a0487..73a6116 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_views.h +++ b/chrome/browser/ui/views/omnibox/omnibox_view_views.h @@ -111,6 +111,7 @@ class OmniboxViewViews virtual void SelectAll(bool reversed) OVERRIDE; virtual void UpdatePopup() OVERRIDE; virtual void SetFocus() OVERRIDE; + virtual void ApplyCaretVisibility() OVERRIDE; virtual void OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) OVERRIDE; @@ -221,6 +222,10 @@ class OmniboxViewViews // allow the user to select just a portion of the text. bool select_all_on_mouse_release_; + // Used to keep track of the visible caret color so that it can be restored + // after the caret has been hidden. + SkColor visible_caret_color_; + DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews); }; diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc index 9e18298..a881053 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc @@ -762,6 +762,11 @@ void OmniboxViewWin::SetFocus() { ::SetFocus(m_hWnd); } +void OmniboxViewWin::ApplyCaretVisibility() { + // TODO(mathp): implement for Windows. + NOTIMPLEMENTED(); +} + void OmniboxViewWin::SetDropHighlightPosition(int position) { if (drop_highlight_position_ != position) { RepaintDropHighlight(drop_highlight_position_); diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.h b/chrome/browser/ui/views/omnibox/omnibox_view_win.h index d3e7a6f..8ae1684 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_win.h +++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.h @@ -97,6 +97,7 @@ class OmniboxViewWin virtual void RevertAll() OVERRIDE; virtual void UpdatePopup() OVERRIDE; virtual void SetFocus() OVERRIDE; + virtual void ApplyCaretVisibility() OVERRIDE; virtual void OnTemporaryTextMaybeChanged( const string16& display_text, bool save_original_selection) OVERRIDE; |