OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #import "base/mac/scoped_objc_class_swizzler.h" | 7 #import "base/mac/scoped_objc_class_swizzler.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "ui/events/event_processor.h" | 9 #include "ui/events/event_processor.h" |
10 #include "ui/events/event_target.h" | 10 #include "ui/events/event_target.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 @interface NSEventDonor : NSObject | 23 @interface NSEventDonor : NSObject |
24 @end | 24 @end |
25 | 25 |
26 @implementation NSEventDonor | 26 @implementation NSEventDonor |
27 | 27 |
28 // Donate +[NSEvent pressedMouseButtons] by retrieving the flags from the | 28 // Donate +[NSEvent pressedMouseButtons] by retrieving the flags from the |
29 // active generator. | 29 // active generator. |
30 + (NSUInteger)pressedMouseButtons { | 30 + (NSUInteger)pressedMouseButtons { |
| 31 if (!g_active_generator) |
| 32 return [NSEventDonor pressedMouseButtons]; // Call original implementation. |
| 33 |
31 int flags = g_active_generator->flags(); | 34 int flags = g_active_generator->flags(); |
32 NSUInteger bitmask = 0; | 35 NSUInteger bitmask = 0; |
33 if (flags & ui::EF_LEFT_MOUSE_BUTTON) | 36 if (flags & ui::EF_LEFT_MOUSE_BUTTON) |
34 bitmask |= 1; | 37 bitmask |= 1; |
35 if (flags & ui::EF_RIGHT_MOUSE_BUTTON) | 38 if (flags & ui::EF_RIGHT_MOUSE_BUTTON) |
36 bitmask |= 1 << 1; | 39 bitmask |= 1 << 1; |
37 if (flags & ui::EF_MIDDLE_MOUSE_BUTTON) | 40 if (flags & ui::EF_MIDDLE_MOUSE_BUTTON) |
38 bitmask |= 1 << 2; | 41 bitmask |= 1 << 2; |
39 return bitmask; | 42 return bitmask; |
40 } | 43 } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 case NSEventTypeSwipe: | 193 case NSEventTypeSwipe: |
191 // NSEventTypeSwipe events can't be generated using public interfaces on | 194 // NSEventTypeSwipe events can't be generated using public interfaces on |
192 // NSEvent, so this will need to be handled at a higher level. | 195 // NSEvent, so this will need to be handled at a higher level. |
193 NOTREACHED(); | 196 NOTREACHED(); |
194 break; | 197 break; |
195 default: | 198 default: |
196 NOTREACHED(); | 199 NOTREACHED(); |
197 } | 200 } |
198 } | 201 } |
199 | 202 |
200 void DispatchMouseEventInWindow(NSWindow* window, | 203 NSEvent* CreateMouseEventInWindow(NSWindow* window, |
201 ui::EventType event_type, | 204 ui::EventType event_type, |
202 const gfx::Point& point_in_root, | 205 const gfx::Point& point_in_root, |
203 int flags) { | 206 int flags) { |
204 NSUInteger click_count = 0; | 207 NSUInteger click_count = 0; |
205 if (event_type == ui::ET_MOUSE_PRESSED || | 208 if (event_type == ui::ET_MOUSE_PRESSED || |
206 event_type == ui::ET_MOUSE_RELEASED) { | 209 event_type == ui::ET_MOUSE_RELEASED) { |
207 if (flags & ui::EF_IS_TRIPLE_CLICK) | 210 if (flags & ui::EF_IS_TRIPLE_CLICK) |
208 click_count = 3; | 211 click_count = 3; |
209 else if (flags & ui::EF_IS_DOUBLE_CLICK) | 212 else if (flags & ui::EF_IS_DOUBLE_CLICK) |
210 click_count = 2; | 213 click_count = 2; |
211 else | 214 else |
212 click_count = 1; | 215 click_count = 1; |
213 } | 216 } |
214 NSPoint point = ConvertRootPointToTarget(window, point_in_root); | 217 NSPoint point = ConvertRootPointToTarget(window, point_in_root); |
215 NSUInteger modifiers = 0; | 218 NSUInteger modifiers = 0; |
216 NSEventType type = EventTypeToNative(event_type, flags, &modifiers); | 219 NSEventType type = EventTypeToNative(event_type, flags, &modifiers); |
217 NSEvent* event = [NSEvent mouseEventWithType:type | 220 return [NSEvent mouseEventWithType:type |
218 location:point | 221 location:point |
219 modifierFlags:modifiers | 222 modifierFlags:modifiers |
220 timestamp:0 | 223 timestamp:0 |
221 windowNumber:[window windowNumber] | 224 windowNumber:[window windowNumber] |
222 context:nil | 225 context:nil |
223 eventNumber:0 | 226 eventNumber:0 |
224 clickCount:click_count | 227 clickCount:click_count |
225 pressure:1.0]; | 228 pressure:1.0]; |
226 | |
227 // Typically events go through NSApplication. For tests, dispatch the event | |
228 // directly to make things more predicatble. | |
229 EmulateSendEvent(window, event); | |
230 } | 229 } |
231 | 230 |
232 // Implementation of ui::test::EventGeneratorDelegate for Mac. Everything | 231 // Implementation of ui::test::EventGeneratorDelegate for Mac. Everything |
233 // defined inline is just a stub. Interesting overrides are defined below the | 232 // defined inline is just a stub. Interesting overrides are defined below the |
234 // class. | 233 // class. |
235 class EventGeneratorDelegateMac : public ui::EventTarget, | 234 class EventGeneratorDelegateMac : public ui::EventTarget, |
236 public ui::EventSource, | 235 public ui::EventSource, |
237 public ui::EventProcessor, | 236 public ui::EventProcessor, |
238 public ui::EventTargeter, | 237 public ui::EventTargeter, |
239 public ui::test::EventGeneratorDelegate { | 238 public ui::test::EventGeneratorDelegate { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 311 |
313 scoped_ptr<ui::EventTargetIterator> | 312 scoped_ptr<ui::EventTargetIterator> |
314 EventGeneratorDelegateMac::GetChildIterator() const { | 313 EventGeneratorDelegateMac::GetChildIterator() const { |
315 // Return NULL to dispatch all events to the result of GetRootTarget(). | 314 // Return NULL to dispatch all events to the result of GetRootTarget(). |
316 return scoped_ptr<ui::EventTargetIterator>(); | 315 return scoped_ptr<ui::EventTargetIterator>(); |
317 } | 316 } |
318 | 317 |
319 void EventGeneratorDelegateMac::OnMouseEvent(ui::MouseEvent* event) { | 318 void EventGeneratorDelegateMac::OnMouseEvent(ui::MouseEvent* event) { |
320 // For mouse drag events, ensure the swizzled methods return the right flags. | 319 // For mouse drag events, ensure the swizzled methods return the right flags. |
321 base::AutoReset<ui::test::EventGenerator*> reset(&g_active_generator, owner_); | 320 base::AutoReset<ui::test::EventGenerator*> reset(&g_active_generator, owner_); |
322 DispatchMouseEventInWindow(window_, | 321 NSEvent* ns_event = CreateMouseEventInWindow(window_, |
323 event->type(), | 322 event->type(), |
324 event->location(), | 323 event->location(), |
325 event->changed_button_flags()); | 324 event->changed_button_flags()); |
| 325 if (owner_->targeting_application()) |
| 326 [NSApp sendEvent:ns_event]; |
| 327 else |
| 328 EmulateSendEvent(window_, ns_event); |
326 } | 329 } |
327 | 330 |
328 void EventGeneratorDelegateMac::SetContext(ui::test::EventGenerator* owner, | 331 void EventGeneratorDelegateMac::SetContext(ui::test::EventGenerator* owner, |
329 gfx::NativeWindow root_window, | 332 gfx::NativeWindow root_window, |
330 gfx::NativeWindow window) { | 333 gfx::NativeWindow window) { |
331 swizzle_pressed_.reset(); | 334 swizzle_pressed_.reset(); |
332 owner_ = owner; | 335 owner_ = owner; |
333 window_ = window; | 336 window_ = window; |
334 if (owner_) { | 337 if (owner_) { |
335 swizzle_pressed_.reset(new base::mac::ScopedObjCClassSwizzler( | 338 swizzle_pressed_.reset(new base::mac::ScopedObjCClassSwizzler( |
(...skipping 19 matching lines...) Expand all Loading... |
355 | 358 |
356 namespace views { | 359 namespace views { |
357 namespace test { | 360 namespace test { |
358 | 361 |
359 void InitializeMacEventGeneratorDelegate() { | 362 void InitializeMacEventGeneratorDelegate() { |
360 EventGeneratorDelegateMac::GetInstance(); | 363 EventGeneratorDelegateMac::GetInstance(); |
361 } | 364 } |
362 | 365 |
363 } // namespace test | 366 } // namespace test |
364 } // namespace views | 367 } // namespace views |
OLD | NEW |