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 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" | 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
9 #include "base/mac/mac_logging.h" | 9 #include "base/mac/mac_logging.h" |
10 #include "chrome/browser/search/search.h" | 10 #include "chrome/browser/search/search.h" |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 std::reverse(decorations->begin() + left_count, decorations->end()); | 158 std::reverse(decorations->begin() + left_count, decorations->end()); |
159 std::reverse(decoration_frames->begin() + left_count, | 159 std::reverse(decoration_frames->begin() + left_count, |
160 decoration_frames->end()); | 160 decoration_frames->end()); |
161 | 161 |
162 *remaining_frame = frame; | 162 *remaining_frame = frame; |
163 return left_count; | 163 return left_count; |
164 } | 164 } |
165 | 165 |
166 } // namespace | 166 } // namespace |
167 | 167 |
| 168 @interface AutocompleteTextFieldCell () |
| 169 // Post an OnSetFocus notification to the observer of |controlView|. |
| 170 - (void)focusNotificationFor:(NSEvent*)event |
| 171 ofView:(AutocompleteTextField*)controlView; |
| 172 @end |
| 173 |
168 @implementation AutocompleteTextFieldCell | 174 @implementation AutocompleteTextFieldCell |
169 | 175 |
170 @synthesize isPopupMode = isPopupMode_; | 176 @synthesize isPopupMode = isPopupMode_; |
171 | 177 |
172 - (CGFloat)topTextFrameOffset { | 178 - (CGFloat)topTextFrameOffset { |
173 return 3.0; | 179 return 3.0; |
174 } | 180 } |
175 | 181 |
176 - (CGFloat)bottomTextFrameOffset { | 182 - (CGFloat)bottomTextFrameOffset { |
177 return 3.0; | 183 return 3.0; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 LocationBarDecoration* decoration = | 410 LocationBarDecoration* decoration = |
405 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; | 411 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; |
406 if (decoration) | 412 if (decoration) |
407 return decoration->GetMenu(); | 413 return decoration->GetMenu(); |
408 return nil; | 414 return nil; |
409 } | 415 } |
410 | 416 |
411 - (BOOL)mouseDown:(NSEvent*)theEvent | 417 - (BOOL)mouseDown:(NSEvent*)theEvent |
412 inRect:(NSRect)cellFrame | 418 inRect:(NSRect)cellFrame |
413 ofView:(AutocompleteTextField*)controlView { | 419 ofView:(AutocompleteTextField*)controlView { |
| 420 // TODO(groby): Factor this into three pieces - find target for event, handle |
| 421 // delayed focus (for any and all events), execute mouseDown for target. |
| 422 |
| 423 // Check if this mouseDown was the reason the control became firstResponder. |
| 424 // If not, discard focus event. |
| 425 base::scoped_nsobject<NSEvent> focusEvent(focusEvent_.release()); |
| 426 if (![theEvent isEqual:focusEvent]) |
| 427 focusEvent.reset(); |
| 428 |
414 LocationBarDecoration* decoration = | 429 LocationBarDecoration* decoration = |
415 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; | 430 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; |
416 if (!decoration || !decoration->AcceptsMousePress()) | 431 if (!decoration || !decoration->AcceptsMousePress()) |
417 return NO; | 432 return NO; |
418 | 433 |
419 NSRect decorationRect = | 434 NSRect decorationRect = |
420 [self frameForDecoration:decoration inFrame:cellFrame]; | 435 [self frameForDecoration:decoration inFrame:cellFrame]; |
421 | 436 |
422 // If the decoration is draggable, then initiate a drag if the user | 437 // If the decoration is draggable, then initiate a drag if the user |
423 // drags or holds the mouse down for awhile. | 438 // drags or holds the mouse down for awhile. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 | 489 |
475 button->SetButtonState(ButtonDecoration::kButtonStatePressed); | 490 button->SetButtonState(ButtonDecoration::kButtonStatePressed); |
476 [controlView setNeedsDisplay:YES]; | 491 [controlView setNeedsDisplay:YES]; |
477 | 492 |
478 // Track the mouse until the user releases the button. | 493 // Track the mouse until the user releases the button. |
479 [self trackMouse:theEvent | 494 [self trackMouse:theEvent |
480 inRect:cellFrame | 495 inRect:cellFrame |
481 ofView:controlView | 496 ofView:controlView |
482 untilMouseUp:YES]; | 497 untilMouseUp:YES]; |
483 | 498 |
| 499 // Post delayed focus notification, if necessary. |
| 500 if (focusEvent.get()) |
| 501 [self focusNotificationFor:focusEvent ofView:controlView]; |
| 502 |
484 // Set the proper state (hover or normal) once the mouse has been released, | 503 // Set the proper state (hover or normal) once the mouse has been released, |
485 // and call |OnMousePressed| if the button was released while the mouse was | 504 // and call |OnMousePressed| if the button was released while the mouse was |
486 // within the bounds of the button. | 505 // within the bounds of the button. |
487 const NSPoint mouseLocation = | 506 const NSPoint mouseLocation = |
488 [[controlView window] mouseLocationOutsideOfEventStream]; | 507 [[controlView window] mouseLocationOutsideOfEventStream]; |
489 const NSPoint point = [controlView convertPoint:mouseLocation fromView:nil]; | 508 const NSPoint point = [controlView convertPoint:mouseLocation fromView:nil]; |
490 if (NSMouseInRect(point, cellFrame, [controlView isFlipped])) { | 509 if (NSMouseInRect(point, cellFrame, [controlView isFlipped])) { |
491 button->SetButtonState(ButtonDecoration::kButtonStateHover); | 510 button->SetButtonState(ButtonDecoration::kButtonStateHover); |
492 [controlView setNeedsDisplay:YES]; | 511 [controlView setNeedsDisplay:YES]; |
493 handled = decoration->AsButtonDecoration()->OnMousePressed( | 512 handled = decoration->AsButtonDecoration()->OnMousePressed( |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 [controlView setNeedsDisplay:YES]; | 819 [controlView setNeedsDisplay:YES]; |
801 NSTextView* fieldEditor = | 820 NSTextView* fieldEditor = |
802 base::mac::ObjCCastStrict<NSTextView>([controlView currentEditor]); | 821 base::mac::ObjCCastStrict<NSTextView>([controlView currentEditor]); |
803 [fieldEditor updateInsertionPointStateAndRestartTimer:YES]; | 822 [fieldEditor updateInsertionPointStateAndRestartTimer:YES]; |
804 } | 823 } |
805 | 824 |
806 - (BOOL)showsFirstResponder { | 825 - (BOOL)showsFirstResponder { |
807 return [super showsFirstResponder] && !hideFocusState_; | 826 return [super showsFirstResponder] && !hideFocusState_; |
808 } | 827 } |
809 | 828 |
| 829 - (void)focusNotificationFor:(NSEvent*)event |
| 830 ofView:(AutocompleteTextField*)controlView { |
| 831 if ([controlView observer]) { |
| 832 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; |
| 833 [controlView observer]->OnSetFocus(controlDown); |
| 834 } |
| 835 } |
| 836 |
| 837 - (void)handleFocusEvent:(NSEvent*)event |
| 838 ofView:(AutocompleteTextField*)controlView { |
| 839 // Only intercept left button click. All other events cause immediate focus. |
| 840 if ([event type] == NSLeftMouseDown) { |
| 841 LocationBarDecoration* decoration = |
| 842 [self decorationForEvent:event |
| 843 inRect:[controlView bounds] |
| 844 ofView:controlView]; |
| 845 // Only ButtonDecorations need a delayed focus handling. |
| 846 if (decoration && decoration->AsButtonDecoration()) { |
| 847 focusEvent_.reset([event retain]); |
| 848 return; |
| 849 } |
| 850 } |
| 851 |
| 852 // Handle event immediately. |
| 853 [self focusNotificationFor:event ofView:controlView]; |
| 854 } |
| 855 |
810 @end | 856 @end |
OLD | NEW |