diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_mac.h | 4 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_mac.mm | 32 |
2 files changed, 34 insertions, 2 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index ff394de..e8d00fb 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -80,9 +80,11 @@ class AutocompleteEditViewMac : public AutocompleteEditView { virtual void OnBeforePossibleChange(); virtual bool OnAfterPossibleChange(); - // Helper functions which forward to our private: model_. + // Helper functions which forward to |model_|, for use from + // AutocompleteEditHelper Objective-C class. void OnUpOrDownKeyPressed(bool up, bool by_page); void OnEscapeKeyPressed(); + // Only forwards to |model_| if the field_ has focus. void OnSetFocus(bool f); void OnKillFocus(); void AcceptInput(WindowOpenDisposition disposition, bool for_drop); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index d0d1db4..c86fa29 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -97,6 +97,8 @@ NSRange ComponentToNSRange(const url_parse::Component& component) { AutocompleteEditViewMac* edit_view_; // weak, owns us. } - initWithEditView:(AutocompleteEditViewMac*)view; +- (void)windowDidResignKey:(NSNotification*)notification; +- (void)windowDidBecomeKey:(NSNotification*)notification; @end AutocompleteEditViewMac::AutocompleteEditViewMac( @@ -122,6 +124,18 @@ AutocompleteEditViewMac::AutocompleteEditViewMac( // Needed so that editing doesn't lose the styling. [field_ setAllowsEditingTextAttributes:YES]; + + // Track the window's key status for signalling focus changes to + // |model_|. + NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; + [nc addObserver:edit_helper_ + selector:@selector(windowDidResignKey:) + name:NSWindowDidResignKeyNotification + object:[field_ window]]; + [nc addObserver:edit_helper_ + selector:@selector(windowDidBecomeKey:) + name:NSWindowDidBecomeKeyNotification + object:[field_ window]]; } AutocompleteEditViewMac::~AutocompleteEditViewMac() { @@ -137,6 +151,9 @@ AutocompleteEditViewMac::~AutocompleteEditViewMac() { // Disconnect field_ from edit_helper_ so that we don't get calls // after destruction. [field_ setDelegate:nil]; + + // Disconnect notifications so they don't signal a dead object. + [[NSNotificationCenter defaultCenter] removeObserver:edit_helper_]; } void AutocompleteEditViewMac::SaveStateToTab(TabContents* tab) { @@ -440,7 +457,10 @@ void AutocompleteEditViewMac::OnEscapeKeyPressed() { model_->OnEscapeKeyPressed(); } void AutocompleteEditViewMac::OnSetFocus(bool f) { - model_->OnSetFocus(f); + // Only forward if we actually do have the focus. + if ([field_ currentEditor]) { + model_->OnSetFocus(f); + } } void AutocompleteEditViewMac::OnKillFocus() { // TODO(shess): This would seem to be a job for |model_|. @@ -537,4 +557,14 @@ void AutocompleteEditViewMac::FocusLocation() { // it's set to the start of the text. } +// Signal that we've lost focus when the window resigns key. +- (void)windowDidResignKey:(NSNotification*)notification { + edit_view_->OnKillFocus(); +} + +// Signal that we may have regained focus. +- (void)windowDidBecomeKey:(NSNotification*)notification { + edit_view_->OnSetFocus(false); +} + @end |