diff options
Diffstat (limited to 'chrome/browser')
8 files changed, 42 insertions, 12 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index d904952..4b40b9d 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -92,6 +92,8 @@ class AutocompleteEditViewMac : public AutocompleteEditView, virtual void OnDidChange(); virtual void OnDidEndEditing(); virtual bool OnDoCommandBySelector(SEL cmd); + virtual void OnSetFocus(bool control_down); + virtual void OnKillFocus(); // Helper for LocationBarViewMac. Optionally selects all in |field_|. void FocusLocation(bool select_all); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 98e7af6..e13f11c 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -632,11 +632,6 @@ void AutocompleteEditViewMac::OnDidBeginEditing() { // We should only arrive here when the field is focussed. DCHECK([field_ currentEditor]); - NSEvent* theEvent = [NSApp currentEvent]; - const bool controlDown = ([theEvent modifierFlags]&NSControlKeyMask) != 0; - model_->OnSetFocus(controlDown); - controller_->OnSetFocus(); - // Capture the current state. OnBeforePossibleChange(); } @@ -651,10 +646,6 @@ void AutocompleteEditViewMac::OnDidChange() { void AutocompleteEditViewMac::OnDidEndEditing() { ClosePopup(); - - // Tell the model to reset itself. - model_->OnKillFocus(); - controller_->OnKillFocus(); } bool AutocompleteEditViewMac::OnDoCommandBySelector(SEL cmd) { @@ -758,6 +749,17 @@ bool AutocompleteEditViewMac::OnDoCommandBySelector(SEL cmd) { return false; } +void AutocompleteEditViewMac::OnSetFocus(bool control_down) { + model_->OnSetFocus(control_down); + controller_->OnSetFocus(); +} + +void AutocompleteEditViewMac::OnKillFocus() { + // Tell the model to reset itself. + model_->OnKillFocus(); + controller_->OnKillFocus(); +} + void AutocompleteEditViewMac::OnDidResignKey() { ClosePopup(); } diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index 8e16705..f07289c 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -78,6 +78,12 @@ class AutocompleteTextFieldObserver { // clearly relevent to expose here. But consider pulling more of // the AutocompleteEditViewMac calls up to here. virtual bool OnDoCommandBySelector(SEL cmd) = 0; + + // Called whenever the autocomplete text field gets focused. + virtual void OnSetFocus(bool control_down) = 0; + + // Called whenever the autocomplete text field is losing focus. + virtual void OnKillFocus() = 0; }; @interface AutocompleteTextField : StyledTextField<NSTextViewDelegate, diff --git a/chrome/browser/cocoa/autocomplete_text_field.mm b/chrome/browser/cocoa/autocomplete_text_field.mm index a4242bb..f24cfa5 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.mm +++ b/chrome/browser/cocoa/autocomplete_text_field.mm @@ -320,6 +320,17 @@ if (doAccept) { [[BrowserWindowController browserWindowControllerForView:self] lockBarVisibilityForOwner:self withAnimation:YES delay:NO]; + + // Tells the observer that we get the focus. + // But we can't call observer_->OnKillFocus() in resignFirstResponder:, + // because the first responder will be immediately set to the field editor + // when calling [super becomeFirstResponder], thus we won't receive + // resignFirstResponder: anymore when losing focus. + if (observer_) { + NSEvent* theEvent = [NSApp currentEvent]; + const bool controlDown = ([theEvent modifierFlags]&NSControlKeyMask) != 0; + observer_->OnSetFocus(controlDown); + } } return doAccept; } diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm index 936b013..5bbc40b 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm @@ -202,6 +202,9 @@ class Extension; // Give the text field ownership of the visibility lock. [[BrowserWindowController browserWindowControllerForView:field] releaseBarVisibilityForOwner:field withAnimation:YES delay:YES]; + + if ([field observer]) + [field observer]->OnKillFocus(); } return doResign; } diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm index 1d0ae49..1f3ecd3 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm @@ -300,12 +300,12 @@ TEST_F(AutocompleteTextFieldObserverTest, FlagsChanged) { // field catches -flagsChanged: because it's on the responder chain, // the field editor doesn't implement it. TEST_F(AutocompleteTextFieldObserverTest, FieldEditorFlagsChanged) { + InSequence dummy; // Call mock in exactly the order specified. + EXPECT_CALL(field_observer_, OnSetFocus(false)); [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; NSResponder* firstResponder = [[field_ window] firstResponder]; EXPECT_EQ(firstResponder, [field_ currentEditor]); - InSequence dummy; // Call mock in exactly the order specified. - // Test without Control key down, but some other modifier down. EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; @@ -422,6 +422,7 @@ TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) { // Test that resetting the field editor bounds does not cause untoward // messages to the field's observer. TEST_F(AutocompleteTextFieldObserverTest, ResetFieldEditorContinuesEditing) { + EXPECT_CALL(field_observer_, OnSetFocus(false)); // Becoming first responder doesn't begin editing. [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); @@ -826,6 +827,7 @@ TEST_F(AutocompleteTextFieldTest, EditorGetsCorrectUndoManager) { } TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { + EXPECT_CALL(field_observer_, OnSetFocus(false)); // Becoming first responder doesn't begin editing. [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); @@ -850,6 +852,7 @@ TEST_F(AutocompleteTextFieldObserverTest, SendsEditingMessages) { [editor doCommandBySelector:cmd]; // Finished with the changes. + EXPECT_CALL(field_observer_, OnKillFocus()); EXPECT_CALL(field_observer_, OnDidEndEditing()); [test_window() clearPretendKeyWindowAndFirstResponder]; } diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h index 4997171..373ea36 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h @@ -46,6 +46,8 @@ class MockAutocompleteTextFieldObserver : public AutocompleteTextFieldObserver { MOCK_METHOD0(OnDidChange, void()); MOCK_METHOD0(OnDidEndEditing, void()); MOCK_METHOD1(OnDoCommandBySelector, bool(SEL cmd)); + MOCK_METHOD1(OnSetFocus, void(bool control_down)); + MOCK_METHOD0(OnKillFocus, void()); }; } // namespace diff --git a/chrome/browser/cocoa/location_bar_view_mac.mm b/chrome/browser/cocoa/location_bar_view_mac.mm index 26a7fc9..807c400 100644 --- a/chrome/browser/cocoa/location_bar_view_mac.mm +++ b/chrome/browser/cocoa/location_bar_view_mac.mm @@ -343,7 +343,8 @@ void LocationBarViewMac::OnInputInProgress(bool in_progress) { } void LocationBarViewMac::OnSetFocus() { - // Do nothing. + // Update the keyword and search hint states. + OnChanged(); } void LocationBarViewMac::OnKillFocus() { |