| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/autocomplete/autocomplete_edit_view_mac.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
| 6 | 6 |
| 7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 8 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
| 9 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 9 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" | 10 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 // Thin Obj-C bridge class that is the delegate of the omnibox field. | 91 // Thin Obj-C bridge class that is the delegate of the omnibox field. |
| 92 // It intercepts various control delegate methods and vectors them to | 92 // It intercepts various control delegate methods and vectors them to |
| 93 // the edit view. | 93 // the edit view. |
| 94 | 94 |
| 95 @interface AutocompleteFieldDelegate : NSObject { | 95 @interface AutocompleteFieldDelegate : NSObject { |
| 96 @private | 96 @private |
| 97 AutocompleteEditViewMac* edit_view_; // weak, owns us. | 97 AutocompleteEditViewMac* edit_view_; // weak, owns us. |
| 98 } | 98 } |
| 99 - initWithEditView:(AutocompleteEditViewMac*)view; | 99 - initWithEditView:(AutocompleteEditViewMac*)view; |
| 100 - (void)windowDidResignKey:(NSNotification*)notification; |
| 101 - (void)windowDidBecomeKey:(NSNotification*)notification; |
| 100 @end | 102 @end |
| 101 | 103 |
| 102 AutocompleteEditViewMac::AutocompleteEditViewMac( | 104 AutocompleteEditViewMac::AutocompleteEditViewMac( |
| 103 AutocompleteEditController* controller, | 105 AutocompleteEditController* controller, |
| 104 ToolbarModel* toolbar_model, | 106 ToolbarModel* toolbar_model, |
| 105 Profile* profile, | 107 Profile* profile, |
| 106 CommandUpdater* command_updater, | 108 CommandUpdater* command_updater, |
| 107 NSTextField* field) | 109 NSTextField* field) |
| 108 : model_(new AutocompleteEditModel(this, controller, profile)), | 110 : model_(new AutocompleteEditModel(this, controller, profile)), |
| 109 popup_view_(new AutocompletePopupViewMac(this, model_.get(), profile, | 111 popup_view_(new AutocompletePopupViewMac(this, model_.get(), profile, |
| 110 field)), | 112 field)), |
| 111 controller_(controller), | 113 controller_(controller), |
| 112 toolbar_model_(toolbar_model), | 114 toolbar_model_(toolbar_model), |
| 113 command_updater_(command_updater), | 115 command_updater_(command_updater), |
| 114 field_(field), | 116 field_(field), |
| 115 edit_helper_([[AutocompleteFieldDelegate alloc] initWithEditView:this]) { | 117 edit_helper_([[AutocompleteFieldDelegate alloc] initWithEditView:this]) { |
| 116 DCHECK(controller); | 118 DCHECK(controller); |
| 117 DCHECK(toolbar_model); | 119 DCHECK(toolbar_model); |
| 118 DCHECK(profile); | 120 DCHECK(profile); |
| 119 DCHECK(command_updater); | 121 DCHECK(command_updater); |
| 120 DCHECK(field); | 122 DCHECK(field); |
| 121 [field_ setDelegate:edit_helper_]; | 123 [field_ setDelegate:edit_helper_]; |
| 122 | 124 |
| 123 // Needed so that editing doesn't lose the styling. | 125 // Needed so that editing doesn't lose the styling. |
| 124 [field_ setAllowsEditingTextAttributes:YES]; | 126 [field_ setAllowsEditingTextAttributes:YES]; |
| 127 |
| 128 // Track the window's key status for signalling focus changes to |
| 129 // |model_|. |
| 130 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; |
| 131 [nc addObserver:edit_helper_ |
| 132 selector:@selector(windowDidResignKey:) |
| 133 name:NSWindowDidResignKeyNotification |
| 134 object:[field_ window]]; |
| 135 [nc addObserver:edit_helper_ |
| 136 selector:@selector(windowDidBecomeKey:) |
| 137 name:NSWindowDidBecomeKeyNotification |
| 138 object:[field_ window]]; |
| 125 } | 139 } |
| 126 | 140 |
| 127 AutocompleteEditViewMac::~AutocompleteEditViewMac() { | 141 AutocompleteEditViewMac::~AutocompleteEditViewMac() { |
| 128 // TODO(shess): Having to be aware of destructor ordering in this | 142 // TODO(shess): Having to be aware of destructor ordering in this |
| 129 // way seems brittle. There must be a better way. | 143 // way seems brittle. There must be a better way. |
| 130 | 144 |
| 131 // Destroy popup view before this object in case it tries to call us | 145 // Destroy popup view before this object in case it tries to call us |
| 132 // back in the destructor. Likewise for destroying the model before | 146 // back in the destructor. Likewise for destroying the model before |
| 133 // this object. | 147 // this object. |
| 134 popup_view_.reset(); | 148 popup_view_.reset(); |
| 135 model_.reset(); | 149 model_.reset(); |
| 136 | 150 |
| 137 // Disconnect field_ from edit_helper_ so that we don't get calls | 151 // Disconnect field_ from edit_helper_ so that we don't get calls |
| 138 // after destruction. | 152 // after destruction. |
| 139 [field_ setDelegate:nil]; | 153 [field_ setDelegate:nil]; |
| 154 |
| 155 // Disconnect notifications so they don't signal a dead object. |
| 156 [[NSNotificationCenter defaultCenter] removeObserver:edit_helper_]; |
| 140 } | 157 } |
| 141 | 158 |
| 142 void AutocompleteEditViewMac::SaveStateToTab(TabContents* tab) { | 159 void AutocompleteEditViewMac::SaveStateToTab(TabContents* tab) { |
| 143 DCHECK(tab); | 160 DCHECK(tab); |
| 144 | 161 |
| 145 NSRange range; | 162 NSRange range; |
| 146 if (model_->has_focus()) { | 163 if (model_->has_focus()) { |
| 147 range = GetSelectedRange(); | 164 range = GetSelectedRange(); |
| 148 } else { | 165 } else { |
| 149 // If we are not focussed, there is no selection. Manufacture | 166 // If we are not focussed, there is no selection. Manufacture |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 } | 450 } |
| 434 | 451 |
| 435 void AutocompleteEditViewMac::OnUpOrDownKeyPressed(bool up, bool by_page) { | 452 void AutocompleteEditViewMac::OnUpOrDownKeyPressed(bool up, bool by_page) { |
| 436 const int count = by_page ? model_->result().size() : 1; | 453 const int count = by_page ? model_->result().size() : 1; |
| 437 model_->OnUpOrDownKeyPressed(up ? -count : count); | 454 model_->OnUpOrDownKeyPressed(up ? -count : count); |
| 438 } | 455 } |
| 439 void AutocompleteEditViewMac::OnEscapeKeyPressed() { | 456 void AutocompleteEditViewMac::OnEscapeKeyPressed() { |
| 440 model_->OnEscapeKeyPressed(); | 457 model_->OnEscapeKeyPressed(); |
| 441 } | 458 } |
| 442 void AutocompleteEditViewMac::OnSetFocus(bool f) { | 459 void AutocompleteEditViewMac::OnSetFocus(bool f) { |
| 443 model_->OnSetFocus(f); | 460 // Only forward if we actually do have the focus. |
| 461 if ([field_ currentEditor]) { |
| 462 model_->OnSetFocus(f); |
| 463 } |
| 444 } | 464 } |
| 445 void AutocompleteEditViewMac::OnKillFocus() { | 465 void AutocompleteEditViewMac::OnKillFocus() { |
| 446 // TODO(shess): This would seem to be a job for |model_|. | 466 // TODO(shess): This would seem to be a job for |model_|. |
| 447 ClosePopup(); | 467 ClosePopup(); |
| 448 | 468 |
| 449 // Tell the model to reset itself. | 469 // Tell the model to reset itself. |
| 450 model_->OnKillFocus(); | 470 model_->OnKillFocus(); |
| 451 } | 471 } |
| 452 void AutocompleteEditViewMac::AcceptInput( | 472 void AutocompleteEditViewMac::AcceptInput( |
| 453 WindowOpenDisposition disposition, bool for_drop) { | 473 WindowOpenDisposition disposition, bool for_drop) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 550 |
| 531 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { | 551 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { |
| 532 edit_view_->OnKillFocus(); | 552 edit_view_->OnKillFocus(); |
| 533 | 553 |
| 534 return YES; | 554 return YES; |
| 535 | 555 |
| 536 // TODO(shess): Figure out where the selection belongs. On GTK, | 556 // TODO(shess): Figure out where the selection belongs. On GTK, |
| 537 // it's set to the start of the text. | 557 // it's set to the start of the text. |
| 538 } | 558 } |
| 539 | 559 |
| 560 // Signal that we've lost focus when the window resigns key. |
| 561 - (void)windowDidResignKey:(NSNotification*)notification { |
| 562 edit_view_->OnKillFocus(); |
| 563 } |
| 564 |
| 565 // Signal that we may have regained focus. |
| 566 - (void)windowDidBecomeKey:(NSNotification*)notification { |
| 567 edit_view_->OnSetFocus(false); |
| 568 } |
| 569 |
| 540 @end | 570 @end |
| OLD | NEW |