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 "ui/views/cocoa/cocoa_mouse_capture.h" | 5 #import "ui/views/cocoa/cocoa_mouse_capture.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #import "ui/views/cocoa/cocoa_mouse_capture_delegate.h" | 11 #import "ui/views/cocoa/cocoa_mouse_capture_delegate.h" |
12 | 12 |
13 namespace views { | 13 namespace views { |
14 | 14 |
15 // The ActiveEventTap is a RAII handle on the resources being used to capture | 15 // The ActiveEventTap is a RAII handle on the resources being used to capture |
16 // events. There is either 0 or 1 active instance of this class. If a second | 16 // events. There is either 0 or 1 active instance of this class. If a second |
17 // instance is created, it will destroy the other instance before returning from | 17 // instance is created, it will destroy the other instance before returning from |
18 // its constructor. | 18 // its constructor. |
19 class CocoaMouseCapture::ActiveEventTap { | 19 class CocoaMouseCapture::ActiveEventTap { |
20 public: | 20 public: |
21 explicit ActiveEventTap(CocoaMouseCapture* owner); | 21 explicit ActiveEventTap(CocoaMouseCapture* owner); |
22 ~ActiveEventTap(); | 22 ~ActiveEventTap(); |
| 23 |
| 24 // Returns the NSWindow with capture or nil if no window has capture |
| 25 // currently. |
| 26 static NSWindow* GetGlobalCaptureWindow(); |
| 27 |
23 void Init(); | 28 void Init(); |
24 | 29 |
25 private: | 30 private: |
| 31 // Returns the associated NSWindow with capture. |
| 32 NSWindow* GetCaptureWindow() const; |
| 33 |
26 // The currently active event tap, or null if there is none. | 34 // The currently active event tap, or null if there is none. |
27 static ActiveEventTap* g_active_event_tap; | 35 static ActiveEventTap* g_active_event_tap; |
28 | 36 |
29 CocoaMouseCapture* owner_; // Weak. Owns this. | 37 CocoaMouseCapture* owner_; // Weak. Owns this. |
30 id local_monitor_; | 38 id local_monitor_; |
31 id global_monitor_; | 39 id global_monitor_; |
32 | 40 |
33 DISALLOW_COPY_AND_ASSIGN(ActiveEventTap); | 41 DISALLOW_COPY_AND_ASSIGN(ActiveEventTap); |
34 }; | 42 }; |
35 | 43 |
36 CocoaMouseCapture::ActiveEventTap* | 44 CocoaMouseCapture::ActiveEventTap* |
37 CocoaMouseCapture::ActiveEventTap::g_active_event_tap = nullptr; | 45 CocoaMouseCapture::ActiveEventTap::g_active_event_tap = nullptr; |
38 | 46 |
39 CocoaMouseCapture::ActiveEventTap::ActiveEventTap(CocoaMouseCapture* owner) | 47 CocoaMouseCapture::ActiveEventTap::ActiveEventTap(CocoaMouseCapture* owner) |
40 : owner_(owner), local_monitor_(nil), global_monitor_(nil) { | 48 : owner_(owner), local_monitor_(nil), global_monitor_(nil) { |
41 if (g_active_event_tap) | 49 if (g_active_event_tap) |
42 g_active_event_tap->owner_->OnOtherClientGotCapture(); | 50 g_active_event_tap->owner_->OnOtherClientGotCapture(); |
43 DCHECK(!g_active_event_tap); | 51 DCHECK(!g_active_event_tap); |
44 g_active_event_tap = this; | 52 g_active_event_tap = this; |
45 } | 53 } |
46 | 54 |
47 CocoaMouseCapture::ActiveEventTap::~ActiveEventTap() { | 55 CocoaMouseCapture::ActiveEventTap::~ActiveEventTap() { |
48 DCHECK_EQ(g_active_event_tap, this); | 56 DCHECK_EQ(g_active_event_tap, this); |
49 [NSEvent removeMonitor:global_monitor_]; | 57 [NSEvent removeMonitor:global_monitor_]; |
50 [NSEvent removeMonitor:local_monitor_]; | 58 [NSEvent removeMonitor:local_monitor_]; |
51 g_active_event_tap = nullptr; | 59 g_active_event_tap = nullptr; |
52 owner_->delegate_->OnMouseCaptureLost(); | 60 owner_->delegate_->OnMouseCaptureLost(); |
53 } | 61 } |
54 | 62 |
| 63 // static |
| 64 NSWindow* CocoaMouseCapture::ActiveEventTap::GetGlobalCaptureWindow() { |
| 65 return g_active_event_tap ? g_active_event_tap->GetCaptureWindow() : nil; |
| 66 } |
| 67 |
55 void CocoaMouseCapture::ActiveEventTap::Init() { | 68 void CocoaMouseCapture::ActiveEventTap::Init() { |
56 NSEventMask event_mask = | 69 NSEventMask event_mask = |
57 NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | | 70 NSLeftMouseDownMask | NSLeftMouseUpMask | NSRightMouseDownMask | |
58 NSRightMouseUpMask | NSMouseMovedMask | NSLeftMouseDraggedMask | | 71 NSRightMouseUpMask | NSMouseMovedMask | NSLeftMouseDraggedMask | |
59 NSRightMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask | | 72 NSRightMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask | |
60 NSScrollWheelMask | NSOtherMouseDownMask | NSOtherMouseUpMask | | 73 NSScrollWheelMask | NSOtherMouseDownMask | NSOtherMouseUpMask | |
61 NSOtherMouseDraggedMask; | 74 NSOtherMouseDraggedMask; |
62 | 75 |
63 local_monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:event_mask | 76 local_monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:event_mask |
64 handler:^NSEvent*(NSEvent* event) { | 77 handler:^NSEvent*(NSEvent* event) { |
65 owner_->delegate_->PostCapturedEvent(event); | 78 owner_->delegate_->PostCapturedEvent(event); |
66 return nil; // Swallow all local events. | 79 return nil; // Swallow all local events. |
67 }]; | 80 }]; |
68 global_monitor_ = [NSEvent addGlobalMonitorForEventsMatchingMask:event_mask | 81 global_monitor_ = [NSEvent addGlobalMonitorForEventsMatchingMask:event_mask |
69 handler:^void(NSEvent* event) { | 82 handler:^void(NSEvent* event) { |
70 owner_->delegate_->PostCapturedEvent(event); | 83 owner_->delegate_->PostCapturedEvent(event); |
71 }]; | 84 }]; |
72 } | 85 } |
73 | 86 |
| 87 NSWindow* CocoaMouseCapture::ActiveEventTap::GetCaptureWindow() const { |
| 88 return owner_->delegate_->GetWindow(); |
| 89 } |
| 90 |
74 CocoaMouseCapture::CocoaMouseCapture(CocoaMouseCaptureDelegate* delegate) | 91 CocoaMouseCapture::CocoaMouseCapture(CocoaMouseCaptureDelegate* delegate) |
75 : delegate_(delegate), active_handle_(new ActiveEventTap(this)) { | 92 : delegate_(delegate), active_handle_(new ActiveEventTap(this)) { |
76 active_handle_->Init(); | 93 active_handle_->Init(); |
77 } | 94 } |
78 | 95 |
79 CocoaMouseCapture::~CocoaMouseCapture() { | 96 CocoaMouseCapture::~CocoaMouseCapture() { |
80 } | 97 } |
81 | 98 |
| 99 // static |
| 100 NSWindow* CocoaMouseCapture::GetGlobalCaptureWindow() { |
| 101 return ActiveEventTap::GetGlobalCaptureWindow(); |
| 102 } |
| 103 |
82 void CocoaMouseCapture::OnOtherClientGotCapture() { | 104 void CocoaMouseCapture::OnOtherClientGotCapture() { |
83 DCHECK(active_handle_); | 105 DCHECK(active_handle_); |
84 active_handle_.reset(); | 106 active_handle_.reset(); |
85 } | 107 } |
86 | 108 |
87 } // namespace views | 109 } // namespace views |
OLD | NEW |