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