Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/base/cocoa/base_view.h" | 5 #include "ui/base/cocoa/base_view.h" |
| 6 | 6 |
| 7 NSString* kViewDidBecomeFirstResponder = | 7 NSString* kViewDidBecomeFirstResponder = |
| 8 @"Chromium.kViewDidBecomeFirstResponder"; | 8 @"Chromium.kViewDidBecomeFirstResponder"; |
| 9 NSString* kSelectionDirection = @"Chromium.kSelectionDirection"; | 9 NSString* kSelectionDirection = @"Chromium.kSelectionDirection"; |
| 10 | 10 |
| 11 const int kTrackingOptions = NSTrackingMouseMoved | | 11 @implementation BaseView |
| 12 NSTrackingMouseEnteredAndExited | | |
| 13 NSTrackingActiveAlways; | |
| 14 | 12 |
| 15 @implementation BaseView | 13 - (instancetype)initWithFrame:(NSRect)frame useEventTap:(BOOL)useEventTap { |
| 14 if ((self = [super initWithFrame:frame])) { | |
| 15 [self initBaseViewUsingEventTap:YES]; | |
| 16 } | |
| 17 return self; | |
| 18 } | |
| 19 | |
| 20 - (instancetype)initWithFrame:(NSRect)frame { | |
| 21 if ((self = [super initWithFrame:frame])) { | |
| 22 [self initBaseViewUsingEventTap:NO]; | |
| 23 } | |
| 24 return self; | |
| 25 } | |
| 26 | |
| 27 - (instancetype)initWithCoder:(NSCoder*)decoder { | |
| 28 if ((self = [super initWithCoder:decoder])) { | |
| 29 [self initBaseViewUsingEventTap:NO]; | |
| 30 } | |
| 31 return self; | |
| 32 } | |
| 33 | |
| 34 - (void)initBaseViewUsingEventTap:(BOOL)useEventTap { | |
| 35 NSTrackingAreaOptions trackingOptions = NSTrackingMouseEnteredAndExited | | |
| 36 NSTrackingActiveAlways | | |
| 37 NSTrackingInVisibleRect; | |
| 38 if (!useEventTap) | |
| 39 trackingOptions |= NSTrackingMouseMoved; | |
| 40 trackingArea_.reset([[CrTrackingArea alloc] initWithRect:NSZeroRect | |
| 41 options:trackingOptions | |
| 42 owner:self | |
| 43 userInfo:nil]); | |
| 44 [self addTrackingArea:trackingArea_.get()]; | |
| 45 | |
| 46 if (useEventTap) { | |
| 47 // Workaround for http://crbug.com/176725 | |
| 48 // Check if the mouse moved over us. | |
| 49 // Don't do it if we are firstResponder to avoid processing the event twice. | |
| 50 eventTap_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSMouseMovedMask | |
| 51 handler:^NSEvent*(NSEvent* event) { | |
| 52 NSWindow* window = [self window]; | |
| 53 if (window && window == [event window] && | |
| 54 [window firstResponder] != self) { | |
| 55 NSPoint location = | |
| 56 [[self superview] convertPoint:[event locationInWindow] | |
| 57 fromView:nil]; | |
| 58 if ([self hitTest:location]) | |
| 59 [self mouseMoved:event]; | |
| 60 } | |
| 61 return event; | |
| 62 }]; | |
| 63 } | |
| 64 } | |
| 16 | 65 |
| 17 - (void)dealloc { | 66 - (void)dealloc { |
| 18 if (trackingArea_.get()) | 67 if (trackingArea_.get()) |
| 19 [self removeTrackingArea:trackingArea_.get()]; | 68 [self removeTrackingArea:trackingArea_.get()]; |
| 20 trackingArea_.reset(nil); | 69 trackingArea_.reset(nil); |
| 21 | 70 |
| 71 if (eventTap_) | |
| 72 [NSEvent removeMonitor:eventTap_]; | |
| 73 | |
| 22 [super dealloc]; | 74 [super dealloc]; |
| 23 } | 75 } |
| 24 | 76 |
| 77 - (void)viewDidMoveToWindow { | |
| 78 [super viewDidMoveToWindow]; | |
| 79 | |
| 80 // Ensure our event tap gets mouse moved events. The mouse moved events are | |
| 81 // normally sent to the window's firstResponder (like key events, unlike | |
| 82 // other mouse events). | |
| 83 if (eventTap_) | |
| 84 [[self window] setAcceptsMouseMovedEvents:YES]; | |
|
Nico
2015/02/26 22:16:07
Does this have to be called with NO at some point?
Andre
2015/02/26 22:29:58
We might if this view is removed from the window,
| |
| 85 } | |
| 86 | |
| 25 - (void)mouseEvent:(NSEvent*)theEvent { | 87 - (void)mouseEvent:(NSEvent*)theEvent { |
| 26 // This method left intentionally blank. | 88 // This method left intentionally blank. |
| 27 } | 89 } |
| 28 | 90 |
| 29 - (EventHandled)keyEvent:(NSEvent*)theEvent { | 91 - (EventHandled)keyEvent:(NSEvent*)theEvent { |
| 30 // The default implementation of this method does not handle any key events. | 92 // The default implementation of this method does not handle any key events. |
| 31 // Derived classes should return kEventHandled if they handled an event, | 93 // Derived classes should return kEventHandled if they handled an event, |
| 32 // otherwise it will be forwarded on to |super|. | 94 // otherwise it will be forwarded on to |super|. |
| 33 return kEventNotHandled; | 95 return kEventNotHandled; |
| 34 } | 96 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 new_rect.set_y(NSHeight([self bounds]) - new_rect.bottom()); | 193 new_rect.set_y(NSHeight([self bounds]) - new_rect.bottom()); |
| 132 return new_rect; | 194 return new_rect; |
| 133 } | 195 } |
| 134 | 196 |
| 135 - (NSRect)flipRectToNSRect:(gfx::Rect)rect { | 197 - (NSRect)flipRectToNSRect:(gfx::Rect)rect { |
| 136 NSRect new_rect(NSRectFromCGRect(rect.ToCGRect())); | 198 NSRect new_rect(NSRectFromCGRect(rect.ToCGRect())); |
| 137 new_rect.origin.y = NSHeight([self bounds]) - NSMaxY(new_rect); | 199 new_rect.origin.y = NSHeight([self bounds]) - NSMaxY(new_rect); |
| 138 return new_rect; | 200 return new_rect; |
| 139 } | 201 } |
| 140 | 202 |
| 141 - (void)updateTrackingAreas { | |
| 142 [super updateTrackingAreas]; | |
| 143 | |
| 144 // NSTrackingInVisibleRect doesn't work correctly with Lion's window resizing, | |
| 145 // http://crbug.com/176725 / http://openradar.appspot.com/radar?id=2773401 . | |
| 146 // Tear down old tracking area and create a new one as workaround. | |
| 147 if (trackingArea_.get()) | |
| 148 [self removeTrackingArea:trackingArea_.get()]; | |
| 149 trackingArea_.reset([[CrTrackingArea alloc] initWithRect:[self bounds] | |
| 150 options:kTrackingOptions | |
| 151 owner:self | |
| 152 userInfo:nil]); | |
| 153 [self addTrackingArea:trackingArea_.get()]; | |
| 154 } | |
| 155 | |
| 156 @end | 203 @end |
| OLD | NEW |