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

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