OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_non_client_drag.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/logging.h" | |
10 #import "base/mac/foundation_util.h" | |
11 #include "base/process/process_handle.h" | |
12 | |
13 namespace views { | |
14 | |
15 namespace { | |
16 | |
17 int g_ref_count = 0; | |
jackhou1
2015/05/20 05:50:37
Do we already have something that implements this
tapted
2015/05/20 07:18:17
Yeah I would just create it lazily, once, and not
jackhou1
2015/05/22 02:49:17
Done.
| |
18 id g_event_monitor = nil; | |
19 bool is_first_repost = false; | |
tapted
2015/05/20 07:18:18
make this a local static of DoubleRepostEventIfHan
jackhou1
2015/05/22 02:49:16
Fortunately, the eventNumber does remain unchanged
| |
20 | |
21 bool DoubleRepostEventIfHandledByWindow(NSWindow* window, | |
22 NSPoint location, | |
23 CGEventRef event) { | |
24 // If the event was just reposted, repost it again, otherwise it won't work. | |
25 if (is_first_repost) { | |
26 LOG(INFO) << "double reposting " << event; | |
27 is_first_repost = false; | |
28 CGEventPost(kCGSessionEventTap, event); | |
29 return true; | |
30 } | |
31 if ([window respondsToSelector:@selector(willReceiveLeftMouseDown:)] && | |
tapted
2015/05/20 07:18:17
maybe just check if [[window delegate] respondsToS
jackhou1
2015/05/22 02:49:17
Done.
| |
32 [base::mac::ObjCCast<NativeWidgetMacNSWindow>(window) | |
33 willReceiveLeftMouseDown:location]) { | |
34 is_first_repost = true; | |
35 CGEventPost(kCGSessionEventTap, event); | |
36 return true; | |
37 } | |
38 | |
39 return false; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 CocoaNonClientDrag::CocoaNonClientDrag() { | |
45 if (g_ref_count) | |
46 return; | |
47 | |
48 NSEvent* (^monitor_callback)(NSEvent* ns_event); | |
49 monitor_callback = ^NSEvent*(NSEvent* ns_event) { | |
50 CGEventRef cg_event = [ns_event CGEvent]; | |
51 LOG(INFO) << "MonitorCallback " << cg_event; | |
52 if (DoubleRepostEventIfHandledByWindow( | |
tapted
2015/05/20 07:18:18
just pass ns_event?
jackhou1
2015/05/22 02:49:16
Done.
| |
53 [ns_event window], [ns_event locationInWindow], cg_event)) { | |
54 return nil; | |
55 } | |
56 return ns_event; | |
57 }; | |
58 | |
59 g_event_monitor = | |
tapted
2015/05/20 07:18:18
I guess this can be a static local too.
jackhou1
2015/05/22 02:49:17
Done.
| |
60 [NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask | |
61 handler:monitor_callback]; | |
62 | |
63 ++g_ref_count; | |
64 } | |
65 | |
66 CocoaNonClientDrag::~CocoaNonClientDrag() { | |
67 --g_ref_count; | |
tapted
2015/05/20 07:18:18
I guess this would just become a NOTREACHED() with
jackhou1
2015/05/22 02:49:16
Done.
| |
68 if (g_ref_count) | |
69 return; | |
70 | |
71 if (g_event_monitor) { | |
72 [NSEvent removeMonitor:g_event_monitor]; | |
73 g_event_monitor = nil; | |
74 } | |
75 } | |
76 | |
77 } // namespace views | |
OLD | NEW |