-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMACHView.m
214 lines (178 loc) · 5.52 KB
/
MACHView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
@interface MACHView : NSView
@end
@implementation MACHView {
void (^_keyDown_block)(NSEvent *);
void (^_keyUp_block)(NSEvent *);
void (^_flagsChanged_block)(NSEvent *);
void (^_mouseMoved_block)(NSEvent *);
void (^_mouseDown_block)(NSEvent *);
void (^_mouseUp_block)(NSEvent *);
void (^_scrollWheel_block)(NSEvent *);
void (^_magnify_block)(NSEvent *);
void (^_insertText_block)(NSEvent *, uint32_t);
NSTrackingArea *trackingArea;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)setBlock_keyDown:(void (^)(NSEvent *))keyDown_block
__attribute__((objc_direct)) {
_keyDown_block = keyDown_block;
}
- (void)setBlock_keyUp:(void (^)(NSEvent *))keyUp_block
__attribute__((objc_direct)) {
_keyUp_block = keyUp_block;
}
- (void)setBlock_mouseMoved:(void (^)(NSEvent *))mouseMoved_block
__attribute__((objc_direct)) {
_mouseMoved_block = mouseMoved_block;
}
- (void)setBlock_mouseDown:(void (^)(NSEvent *))mouseDown_block
__attribute__((objc_direct)) {
_mouseDown_block = mouseDown_block;
}
- (void)setBlock_mouseUp:(void (^)(NSEvent *))mouseUp_block
__attribute__((objc_direct)) {
_mouseUp_block = mouseUp_block;
}
- (void)setBlock_scrollWheel:(void (^)(NSEvent *))scrollWheel_block
__attribute__((objc_direct)) {
_scrollWheel_block = scrollWheel_block;
}
- (void)setBlock_flagsChanged:(void (^)(NSEvent *))flagsChanged_block
__attribute__((objc_direct)) {
_flagsChanged_block = flagsChanged_block;
}
- (void)setBlock_insertText:(void (^)(NSEvent *, uint32_t))insertText_block
__attribute__((objc_direct)) {
_insertText_block = insertText_block;
}
- (void)setBlock_magnify:(void (^)(NSEvent *))magnify_block
__attribute__((objc_direct)) {
_magnify_block = magnify_block;
}
- (void)keyDown:(NSEvent *)event {
if (_keyDown_block)
_keyDown_block(event);
[self interpretKeyEvents:@[ event ]];
}
- (void)insertText:(id)string {
NSString *characters;
NSEvent *event = [NSApp currentEvent];
if ([string isKindOfClass:[NSAttributedString class]])
characters = [string string];
else
characters = (NSString *)string;
NSRange range = NSMakeRange(0, [characters length]);
while (range.length) {
uint32_t codepoint = 0;
if ([characters getBytes:&codepoint
maxLength:sizeof(codepoint)
usedLength:NULL
encoding:NSUTF32StringEncoding
options:0
range:range
remainingRange:&range]) {
if (codepoint >= 0xf700 && codepoint <= 0xf7ff) {
continue;
}
if (_insertText_block)
_insertText_block(event, codepoint);
}
}
}
- (void)keyUp:(NSEvent *)event {
if (_keyUp_block)
_keyUp_block(event);
}
- (void)flagsChanged:(NSEvent *)event {
if (_flagsChanged_block)
_flagsChanged_block(event);
}
- (void)mouseMoved:(NSEvent *)event {
if (_mouseMoved_block)
_mouseMoved_block(event);
}
- (void)mouseDragged:(NSEvent *)event {
if (_mouseMoved_block)
_mouseMoved_block(event);
}
- (void)rightMouseDragged:(NSEvent *)event {
if (_mouseMoved_block)
_mouseMoved_block(event);
}
- (void)otherMouseDragged:(NSEvent *)event {
if (_mouseMoved_block)
_mouseMoved_block(event);
}
- (void)mouseDown:(NSEvent *)event {
if (_mouseDown_block)
_mouseDown_block(event);
}
- (void)rightMouseDown:(NSEvent *)event {
if (_mouseDown_block)
_mouseDown_block(event);
}
- (void)otherMouseDown:(NSEvent *)event {
if (_mouseDown_block)
_mouseDown_block(event);
}
- (void)mouseUp:(NSEvent *)event {
if (_mouseUp_block)
_mouseUp_block(event);
}
- (void)rightMouseUp:(NSEvent *)event {
if (_mouseUp_block)
_mouseUp_block(event);
}
- (void)otherMouseUp:(NSEvent *)event {
if (_mouseUp_block)
_mouseUp_block(event);
}
- (void)scrollWheel:(NSEvent *)event {
if (_scrollWheel_block)
_scrollWheel_block(event);
}
- (void)magnifyWithEvent:(NSEvent *)event {
if (_magnify_block)
_magnify_block(event);
}
// This overrides the default initializer and creates a tracking area over the
// views visible rect
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Create a new tracking area to monitor mouse movement
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveInActiveApp;
NSRect rect = self.visibleRect;
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options:options
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
}
return self;
}
// This is automatically called each time the view size changes
- (void)updateTrackingAreas {
// Remove any existing tracking area
[self removeTrackingArea:trackingArea];
// Create a new tracking area to monitor mouse movement
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveInActiveApp;
NSRect rect = self.visibleRect;
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options:options
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
}
@end