forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZKHotKeyRecorderCell.m
397 lines (285 loc) · 13.2 KB
/
ZKHotKeyRecorderCell.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#import "ZKHotKeyRecorderCell.h"
#import "ZKHotKey.h"
#import "ZKHotKeyTranslator.h"
#import "ZKHotKeyValidator.h"
#import "ZKHotKeyRecorder.h"
#import "ZKHotKeyRecorderDelegate.h"
#import "SpectacleUtilities.h"
#define MakeRelativePoint(a, b, c) NSMakePoint((a * horizontalScale) + c.origin.x, (b * verticalScale) + c.origin.y)
#pragma mark -
@interface ZKHotKeyRecorderCell ()
@property (nonatomic) NSUInteger modifierFlags;
@property (nonatomic) BOOL isRecording;
@property (nonatomic) NSTrackingArea *trackingArea;
@property (nonatomic) BOOL isMouseAboveBadge;
@property (nonatomic) BOOL isMouseDown;
@property (nonatomic) void *hotKeyMode;
@end
#pragma mark -
@implementation ZKHotKeyRecorderCell
- (id)init {
if (self = [super init]) {
_hotKeyRecorder = nil;
_hotKeyName = nil;
_hotKey = nil;
_delegate = nil;
_additionalHotKeyValidators = [NSArray new];
_modifierFlags = 0;
_isRecording = NO;
_trackingArea = nil;
_isMouseAboveBadge = NO;
_isMouseDown = NO;
}
return self;
}
#pragma mark -
- (BOOL)resignFirstResponder {
if (_isRecording) {
PopSymbolicHotKeyMode(_hotKeyMode);
_isRecording = NO;
[self.controlView setNeedsDisplay: YES];
}
return YES;
}
#pragma mark -
- (BOOL)performKeyEquivalent: (NSEvent *)event {
NSInteger keyCode = event.keyCode;
NSUInteger newModifierFlags = _modifierFlags | event.modifierFlags;
if (_isRecording && [ZKHotKey validCocoaModifiers: newModifierFlags]) {
NSString *characters = event.charactersIgnoringModifiers.uppercaseString;
if (characters.length) {
ZKHotKey *newHotKey = [[ZKHotKey alloc] initWithHotKeyCode: keyCode hotKeyModifiers: newModifierFlags];
NSError *error = nil;
if (![ZKHotKeyValidator isHotKeyValid: newHotKey withValidators: _additionalHotKeyValidators error: &error]) {
[[NSAlert alertWithError: error] runModal];
} else {
newHotKey.hotKeyName = _hotKeyName;
self.hotKey = newHotKey;
[_delegate hotKeyRecorder: _hotKeyRecorder didReceiveNewHotKey: newHotKey];
}
} else {
NSBeep();
}
_modifierFlags = 0;
PopSymbolicHotKeyMode(_hotKeyMode);
_isRecording = NO;
[self.controlView setNeedsDisplay: YES];
return YES;
}
return NO;
}
- (void)flagsChanged: (NSEvent *)event {
if (_isRecording) {
_modifierFlags = event.modifierFlags;
if (_modifierFlags == 256) {
_modifierFlags = 0;
}
[self.controlView setNeedsDisplay: YES];
}
}
#pragma mark -
- (BOOL)trackMouse: (NSEvent *)event inRect: (NSRect)rect ofView: (NSView *)view untilMouseUp: (BOOL)untilMouseUp {
NSEvent *currentEvent = event;
do {
NSPoint mouseLocation = [view convertPoint: currentEvent.locationInWindow fromView: nil];
switch ([currentEvent type]) {
case NSLeftMouseDown:
_isMouseDown = YES;
[view setNeedsDisplay: YES];
break;
case NSLeftMouseDragged:
if ([view mouse: mouseLocation inRect: rect]) {
_isMouseDown = YES;
} else {
_isMouseDown = NO;
}
if (_isMouseAboveBadge && [view mouse: mouseLocation inRect: _trackingArea.rect]) {
_isMouseDown = YES;
_isMouseAboveBadge = YES;
} else {
_isMouseDown = NO;
_isMouseAboveBadge = NO;
}
[view setNeedsDisplay: YES];
break;
default:
_isMouseDown = NO;
if ([view mouse: mouseLocation inRect: rect] && !_isRecording && !_isMouseAboveBadge) {
_isRecording = YES;
_hotKeyMode = PushSymbolicHotKeyMode(kHIHotKeyModeAllDisabled);
[view.window makeFirstResponder: view];
} else if (_isRecording && _isMouseAboveBadge) {
PopSymbolicHotKeyMode(_hotKeyMode);
_isRecording = NO;
} else if (!_isRecording && _hotKey && _isMouseAboveBadge) {
[_delegate hotKeyRecorder: _hotKeyRecorder didClearExistingHotKey: _hotKey];
self.hotKey = nil;
}
[view setNeedsDisplay: YES];
return YES;
}
} while ((currentEvent = [[view window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)
untilDate: NSDate.distantFuture
inMode: NSEventTrackingRunLoopMode
dequeue: YES]));
return YES;
}
#pragma mark -
- (void)mouseEntered: (NSEvent *)event {
_isMouseAboveBadge = YES;
[self.controlView setNeedsDisplay: YES];
}
- (void)mouseExited: (NSEvent *)event {
_isMouseAboveBadge = NO;
[self.controlView setNeedsDisplay: YES];
}
#pragma mark -
- (void)drawWithFrame: (NSRect)frame inView: (NSView *)view {
CGFloat radius = NSHeight(frame) / 2.0f;
// Draw the border of the control.
[self drawBorderInRect: frame withRadius: radius];
// Draw the default background of the control.
[self drawBackgroundInRect: frame withRadius: radius];
// Draw the tracking area image, depending the control's current state.
[self drawBadgeInRect: frame];
// Draw the label of the control.
[self drawLabelInRect: frame];
}
#pragma mark -
- (void)drawBorderInRect: (NSRect)rect withRadius: (CGFloat)radius {
NSBezierPath *roundedPath = [NSBezierPath bezierPathWithRoundedRect: rect xRadius: radius yRadius: radius];
[NSGraphicsContext.currentContext saveGraphicsState];
[roundedPath addClip];
[NSColor.windowFrameColor set];
[NSBezierPath fillRect: rect];
[NSGraphicsContext.currentContext restoreGraphicsState];
}
- (void)drawBackgroundInRect: (NSRect)rect withRadius: (CGFloat)radius {
NSBezierPath *roundedPath = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 1.0f, 1.0f) xRadius: radius yRadius: radius];
NSColor *gradientStartingColor = nil;
NSColor *gradientEndingColor = nil;
NSGradient *gradient = nil;
[NSGraphicsContext.currentContext saveGraphicsState];
[roundedPath addClip];
if (_isRecording) {
gradientStartingColor = [NSColor colorWithDeviceRed: 0.784f green: 0.953f blue: 1.0f alpha: 1.0f];
gradientEndingColor = [NSColor colorWithDeviceRed: 0.694f green: 0.859f blue: 1.0f alpha: 1.0f];
} else {
gradientStartingColor = [[NSColor.whiteColor shadowWithLevel: 0.2f] colorWithAlphaComponent: 0.9f];
gradientEndingColor = [[NSColor.whiteColor highlightWithLevel: 0.2f] colorWithAlphaComponent: 0.9f];
}
if (!_isRecording && _isMouseDown && !_isMouseAboveBadge) {
gradient = [[NSGradient alloc] initWithStartingColor: gradientEndingColor endingColor: gradientStartingColor];
} else {
gradient = [[NSGradient alloc] initWithStartingColor: gradientStartingColor endingColor: gradientEndingColor];
}
[gradient drawInRect: rect angle: 90.0f];
[NSGraphicsContext.currentContext restoreGraphicsState];
}
#pragma mark -
- (void)drawBadgeInRect: (NSRect)rect {
NSRect badgeRect;
NSSize badgeSize;
// Calculate this! Eventually...
badgeSize.width = 13.0f;
badgeSize.height = 13.0f;
badgeRect.origin = NSMakePoint(NSMaxX(rect) - badgeSize.width - 4.0f, floor((NSMaxY(rect) - badgeSize.height) / 2.0f));
badgeRect.size = badgeSize;
if (_isRecording && !_hotKey) {
[self drawClearHotKeyBadgeInRect: badgeRect withOpacity: 0.25f];
} else if (_isRecording) {
[self drawRevertHotKeyBadgeInRect: badgeRect];
} else if (_hotKey) {
[self drawClearHotKeyBadgeInRect: badgeRect withOpacity: 0.25f];
}
if (((_hotKey && !_isRecording) || (!_hotKey && _isRecording)) && _isMouseAboveBadge && _isMouseDown) {
[self drawClearHotKeyBadgeInRect: badgeRect withOpacity: 0.50f];
}
if (!_trackingArea) {
_trackingArea = [[NSTrackingArea alloc] initWithRect: badgeRect
options: (NSTrackingActiveInKeyWindow | NSTrackingMouseEnteredAndExited)
owner: self
userInfo: nil];
[self.controlView addTrackingArea: _trackingArea];
}
}
#pragma mark -
- (void)drawClearHotKeyBadgeInRect: (NSRect)rect withOpacity: (CGFloat)opacity {
CGFloat horizontalScale = (rect.size.width / 13.0f);
CGFloat verticalScale = (rect.size.height / 13.0f);
[NSGraphicsContext.currentContext saveGraphicsState];
[[NSColor colorWithCalibratedWhite: 0.0f alpha: opacity] setFill];
[[NSBezierPath bezierPathWithOvalInRect: rect] fill];
[NSColor.whiteColor setStroke];
NSBezierPath *cross = [NSBezierPath new];
[cross setLineWidth: horizontalScale * 1.4f];
[cross moveToPoint: MakeRelativePoint(4.0f, 4.0f, rect)];
[cross lineToPoint: MakeRelativePoint(9.0f, 9.0f, rect)];
[cross moveToPoint: MakeRelativePoint(9.0f, 4.0f, rect)];
[cross lineToPoint: MakeRelativePoint(4.0f, 9.0f, rect)];
[cross stroke];
[NSGraphicsContext.currentContext restoreGraphicsState];
}
- (void)drawRevertHotKeyBadgeInRect: (NSRect)rect {
CGFloat horizontalScale = (rect.size.width / 1.0f);
CGFloat verticalScale = (rect.size.height / 1.0f);
[NSGraphicsContext.currentContext saveGraphicsState];
NSBezierPath *swoosh = [NSBezierPath new];
[swoosh setLineWidth: horizontalScale];
[swoosh moveToPoint: MakeRelativePoint(0.0489685f, 0.6181513f, rect)];
[swoosh lineToPoint: MakeRelativePoint(0.4085750f, 0.9469318f, rect)];
[swoosh lineToPoint: MakeRelativePoint(0.4085750f, 0.7226146f, rect)];
[swoosh curveToPoint: MakeRelativePoint(0.8508247f, 0.4836237f, rect)
controlPoint1: MakeRelativePoint(0.4085750f, 0.7226146f, rect)
controlPoint2: MakeRelativePoint(0.8371143f, 0.7491841f, rect)];
[swoosh curveToPoint: MakeRelativePoint(0.5507195f, 0.0530682f, rect)
controlPoint1: MakeRelativePoint(0.8677834f, 0.1545071f, rect)
controlPoint2: MakeRelativePoint(0.5507195f, 0.0530682f, rect)];
[swoosh curveToPoint: MakeRelativePoint(0.7421721f, 0.3391942f, rect)
controlPoint1: MakeRelativePoint(0.5507195f, 0.0530682f, rect)
controlPoint2: MakeRelativePoint(0.7458685f, 0.1913146f, rect)];
[swoosh curveToPoint: MakeRelativePoint(0.4085750f, 0.5154130f, rect)
controlPoint1: MakeRelativePoint(0.7383412f, 0.4930328f, rect)
controlPoint2: MakeRelativePoint(0.4085750f, 0.5154130f, rect)];
[swoosh lineToPoint: MakeRelativePoint(0.4085750f, 0.2654000f, rect)];
[swoosh fill];
[NSGraphicsContext.currentContext restoreGraphicsState];
}
#pragma mark -
- (void)drawLabelInRect: (NSRect)rect {
NSString *label = nil;
NSColor *foregroundColor = NSColor.blackColor;
if (_isRecording && !_isMouseAboveBadge) {
label = LocalizedString(@"Enter hot key");
} else if (_isRecording && _isMouseAboveBadge && !_hotKey) {
label = LocalizedString(@"Stop recording");
} else if (_isRecording && _isMouseAboveBadge) {
label = LocalizedString(@"Use existing");
} else if (_hotKey) {
label = _hotKey.displayString;
} else {
label = LocalizedString(@"Click to record");
}
// Recording is in progress and modifier flags have already been set, display them.
if (_isRecording && (_modifierFlags > 0)) {
label = [ZKHotKeyTranslator translateCocoaModifiers: _modifierFlags];
}
if (!self.isEnabled) {
foregroundColor = NSColor.disabledControlTextColor;
}
if (_isRecording) {
[self drawString: label withForegroundColor: foregroundColor inRect: rect];
} else {
[self drawString: label withForegroundColor: foregroundColor inRect: rect];
}
}
#pragma mark -
- (void)drawString: (NSString *)string withForegroundColor: (NSColor *)foregroundColor inRect: (NSRect)rect {
NSMutableDictionary *attributes = SpectacleUtilities.stringAttributesWithShadow;
NSRect labelRect = rect;
attributes[NSFontAttributeName] = [NSFont systemFontOfSize: NSFont.smallSystemFontSize];
attributes[NSForegroundColorAttributeName] = foregroundColor;
labelRect.origin.y = -(NSMidY(rect) - [string sizeWithAttributes: attributes].height / 2.0f);
[string drawInRect: labelRect withAttributes: attributes];
}
@end