|
12 | 12 | #import "NSTouchDevice.h" |
13 | 13 |
|
14 | 14 | @import AppKit; |
| 15 | +@import IOKit; |
15 | 16 |
|
16 | 17 | NS_ASSUME_NONNULL_BEGIN |
17 | 18 |
|
| 19 | +static const uint32_t kCGEventFieldTouchContxtID = 0x92; |
| 20 | + |
18 | 21 | @interface HTKTapGestureEventListener () <HTKEventTapDelegate> |
19 | 22 |
|
20 | 23 | @property (nonatomic, readonly) HTKEventTap *eventTap; |
@@ -47,20 +50,26 @@ - (BOOL)isEnabled |
47 | 50 |
|
48 | 51 | // MARK: - HTKEventTapDelegate |
49 | 52 |
|
50 | | -- (void)eventTap:(HTKEventTap *)eventTap didTapEvent:(NSEvent *)event |
| 53 | +- (void)eventTap:(HTKEventTap *)eventTap didTapCGEvent:(CGEventRef)eventRef |
51 | 54 | { |
52 | | - for (NSTouch * const touch in [event allTouches]) { |
53 | | - NSTouchDevice * const touchDevice = touch.device; |
54 | | - if (!touch.resting && touchDevice.deviceType == NSTouchDeviceTypeTouchBar) { |
55 | | - switch (touch.phase) { |
56 | | - case NSTouchPhaseBegan: |
57 | | - [self _htk_main_didListenEvent:[[HTKEvent alloc] initWithPhase:HTKEventPhaseBegin]]; |
58 | | - return; |
59 | | - case NSTouchPhaseEnded: |
60 | | - [self _htk_main_didListenEvent:[[HTKEvent alloc] initWithPhase:HTKEventPhaseEnd]]; |
61 | | - return; |
62 | | - default: |
63 | | - break; |
| 55 | + // `eventWithCGEvent:` is relatively expensive. |
| 56 | + // Check touch contextID exists or not first. All touches on TouchBar has this ID. |
| 57 | + const int64_t contextID = CGEventGetIntegerValueField(eventRef, kCGEventFieldTouchContxtID); |
| 58 | + if (contextID != 0) { |
| 59 | + NSEvent * const event = [NSEvent eventWithCGEvent:eventRef]; |
| 60 | + for (NSTouch * const touch in [event allTouches]) { |
| 61 | + NSTouchDevice * const touchDevice = touch.device; |
| 62 | + if (!touch.resting && touchDevice.deviceType == NSTouchDeviceTypeTouchBar) { |
| 63 | + switch (touch.phase) { |
| 64 | + case NSTouchPhaseBegan: |
| 65 | + [self _htk_main_didListenEvent:[[HTKEvent alloc] initWithPhase:HTKEventPhaseBegin]]; |
| 66 | + return; |
| 67 | + case NSTouchPhaseEnded: |
| 68 | + [self _htk_main_didListenEvent:[[HTKEvent alloc] initWithPhase:HTKEventPhaseEnd]]; |
| 69 | + return; |
| 70 | + default: |
| 71 | + break; |
| 72 | + } |
64 | 73 | } |
65 | 74 | } |
66 | 75 | } |
|
0 commit comments