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 #include "ui/views/event_monitor_mac.h" | 5 #import "ui/views/event_monitor_mac.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
6 | 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
9 #include "ui/events/event_handler.h" | 11 #include "ui/events/event_handler.h" |
10 #include "ui/events/event_utils.h" | 12 #include "ui/events/event_utils.h" |
11 | 13 |
12 namespace views { | 14 namespace views { |
13 | 15 |
14 // static | 16 // static |
15 EventMonitor* EventMonitor::Create(ui::EventHandler* event_handler) { | 17 scoped_ptr<EventMonitor> EventMonitor::CreateApplicationMonitor( |
16 return new EventMonitorMac(event_handler); | 18 ui::EventHandler* event_handler) { |
| 19 return scoped_ptr<EventMonitor>(new EventMonitorMac(event_handler, nullptr)); |
17 } | 20 } |
18 | 21 |
19 // static | 22 // static |
| 23 scoped_ptr<EventMonitor> EventMonitor::CreateWindowMonitor( |
| 24 ui::EventHandler* event_handler, |
| 25 gfx::NativeWindow target_window) { |
| 26 return scoped_ptr<EventMonitor>( |
| 27 new EventMonitorMac(event_handler, target_window)); |
| 28 } |
| 29 |
| 30 // static |
20 gfx::Point EventMonitor::GetLastMouseLocation() { | 31 gfx::Point EventMonitor::GetLastMouseLocation() { |
21 NSPoint mouseLocation = [NSEvent mouseLocation]; | 32 NSPoint mouseLocation = [NSEvent mouseLocation]; |
22 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. | 33 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen. |
23 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 34 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
24 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; | 35 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y; |
25 return gfx::Point(mouseLocation.x, mouseLocation.y); | 36 return gfx::Point(mouseLocation.x, mouseLocation.y); |
26 } | 37 } |
27 | 38 |
28 EventMonitorMac::EventMonitorMac(ui::EventHandler* event_handler) { | 39 EventMonitorMac::EventMonitorMac(ui::EventHandler* event_handler, |
| 40 gfx::NativeWindow target_window) { |
29 DCHECK(event_handler); | 41 DCHECK(event_handler); |
30 monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSAnyEventMask | 42 monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSAnyEventMask |
31 handler:^NSEvent*(NSEvent* event){ | 43 handler:^NSEvent*(NSEvent* event) { |
32 scoped_ptr<ui::Event> ui_event = ui::EventFromNative(event); | 44 if (!target_window || [event window] == target_window) { |
33 event_handler->OnEvent(ui_event.get()); | 45 scoped_ptr<ui::Event> ui_event = ui::EventFromNative(event); |
34 return event; | 46 event_handler->OnEvent(ui_event.get()); |
35 }]; | 47 } |
| 48 return event; |
| 49 }]; |
36 } | 50 } |
37 | 51 |
38 EventMonitorMac::~EventMonitorMac() { | 52 EventMonitorMac::~EventMonitorMac() { |
39 [NSEvent removeMonitor:monitor_]; | 53 [NSEvent removeMonitor:monitor_]; |
40 } | 54 } |
41 | 55 |
42 } // namespace views | 56 } // namespace views |
OLD | NEW |