Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: ui/views/cocoa/cocoa_mouse_capture.mm

Issue 789763002: MacViews: Implement capture using NSEvent local+global monitors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@20140812-MacViews-LAYERS2-PRESQUASH
Patch Set: NULL -> nullptr Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/cocoa/cocoa_mouse_capture.h ('k') | ui/views/cocoa/cocoa_mouse_capture_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « ui/views/cocoa/cocoa_mouse_capture.h ('k') | ui/views/cocoa/cocoa_mouse_capture_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698