forked from Kentzo/ShortcutRecorder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SRRecorderControl.m
executable file
·1258 lines (1025 loc) · 41 KB
/
SRRecorderControl.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SRRecorderControl.m
// ShortcutRecorder
//
// Copyright 2006-2012 Contributors. All rights reserved.
//
// License: BSD
//
// Contributors:
// David Dauer
// Jesper
// Jamie Kirkpatrick
// Ilya Kulakov
#import "SRRecorderControl.h"
#import "SRKeyCodeTransformer.h"
#import "SRModifierFlagsTransformer.h"
NSString *const SRShortcutKeyCode = @"keyCode";
NSString *const SRShortcutModifierFlagsKey = @"modifierFlags";
NSString *const SRShortcutCharacters = @"characters";
NSString *const SRShortcutCharactersIgnoringModifiers = @"charactersIgnoringModifiers";
// Control Layout Constants
static const CGFloat _SRRecorderControlShapeXRadius = 11.0;
static const CGFloat _SRRecorderControlShapeYRadius = 12.0;
static const CGFloat _SRRecorderControlHeight = 25.0;
static const CGFloat _SRRecorderControlBottomShadowHeightInPixels = 1.0;
static const CGFloat _SRRecorderControlBaselineOffset = 5.0;
// Clear Button Layout Constants
static const CGFloat _SRRecorderControlClearButtonWidth = 14.0;
static const CGFloat _SRRecorderControlClearButtonHeight = 14.0;
static const CGFloat _SRRecorderControlClearButtonRightOffset = 4.0;
static const CGFloat _SRRecorderControlClearButtonLeftOffset = 1.0;
static const NSSize _SRRecorderControlClearButtonSize = {.width = _SRRecorderControlClearButtonWidth, .height = _SRRecorderControlClearButtonHeight};
// SanpBack Button Layout Constants
static const CGFloat _SRRecorderControlSnapBackButtonWidth = 14.0;
static const CGFloat _SRRecorderControlSnapBackButtonHeight = 14.0;
static const CGFloat _SRRecorderControlSnapBackButtonRightOffset = 1.0;
static const CGFloat _SRRecorderControlSnapBackButtonLeftOffset = 3.0;
static const NSSize _SRRecorderControlSnapBackButtonSize = {.width = _SRRecorderControlSnapBackButtonWidth, .height = _SRRecorderControlSnapBackButtonHeight};
static NSImage *_SRImages[16];
static NSUInteger _SRValueObservationContext;
typedef NS_ENUM(NSUInteger, _SRRecorderControlButtonTag)
{
_SRRecorderControlInvalidButtonTag = -1,
_SRRecorderControlSnapBackButtonTag = 0,
_SRRecorderControlClearButtonTag = 1,
_SRRecorderControlMainButtonTag = 2
};
/*!
@brief Extracts value transformer from binding options.
@result Returns an instance of NSValueTransformer or nil if there is not transformer.
*/
static NSValueTransformer *_SRValueTransformerFromBindingOptions(NSDictionary *aBindingOptions)
{
NSValueTransformer *valueTransformer = aBindingOptions[NSValueTransformerBindingOption];
if (!valueTransformer || (NSNull *)valueTransformer == [NSNull null])
{
NSString *valueTransformerName = aBindingOptions[NSValueTransformerNameBindingOption];
if (valueTransformerName && (NSNull *)valueTransformerName != [NSNull null])
valueTransformer = [NSValueTransformer valueTransformerForName:valueTransformerName];
}
if ((NSNull *)valueTransformer != [NSNull null])
return valueTransformer;
else
return nil;
}
@implementation SRRecorderControl
{
NSTrackingArea *_mainButtonTrackingArea;
NSTrackingArea *_snapBackButtonTrackingArea;
NSTrackingArea *_clearButtonTrackingArea;
_SRRecorderControlButtonTag _mouseTrackingButtonTag;
NSToolTipTag _snapBackButtonToolTipTag;
NSMutableDictionary *_bindingInfo;
}
- (instancetype)initWithFrame:(NSRect)aFrameRect
{
self = [super initWithFrame:aFrameRect];
if (self)
{
_allowedModifierFlags = SRCocoaModifierFlagsMask;
_requiredModifierFlags = 0;
_allowsEmptyModifierFlags = NO;
_drawsASCIIEquivalentOfShortcut = YES;
_allowsEscapeToCancelRecording = YES;
_allowsDeleteToClearShortcutAndEndRecording = YES;
_mouseTrackingButtonTag = _SRRecorderControlInvalidButtonTag;
_snapBackButtonToolTipTag = NSIntegerMax;
_bindingInfo = [NSMutableDictionary dictionary];
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
{
self.translatesAutoresizingMaskIntoConstraints = NO;
[self setContentHuggingPriority:NSLayoutPriorityDefaultLow
forOrientation:NSLayoutConstraintOrientationHorizontal];
[self setContentHuggingPriority:NSLayoutPriorityRequired
forOrientation:NSLayoutConstraintOrientationVertical];
[self setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow
forOrientation:NSLayoutConstraintOrientationHorizontal];
[self setContentCompressionResistancePriority:NSLayoutPriorityRequired
forOrientation:NSLayoutConstraintOrientationVertical];
}
[self setToolTip:SRLoc(@"Click to record shortcut")];
[self updateTrackingAreas];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_bindingInfo unbind:NSValueBinding];
}
#pragma mark Properties
- (void)setAllowedModifierFlags:(NSUInteger)newAllowedModifierFlags
requiredModifierFlags:(NSUInteger)newRequiredModifierFlags
allowsEmptyModifierFlags:(BOOL)newAllowsEmptyModifierFlags
{
newAllowedModifierFlags &= SRCocoaModifierFlagsMask;
newRequiredModifierFlags &= SRCocoaModifierFlagsMask;
if ((newAllowedModifierFlags & newRequiredModifierFlags) != newRequiredModifierFlags)
{
[NSException raise:NSInvalidArgumentException
format:@"Required flags (%lu) MUST be allowed (%lu)", newAllowedModifierFlags, newRequiredModifierFlags];
}
if (newAllowsEmptyModifierFlags && newRequiredModifierFlags != 0)
{
[NSException raise:NSInvalidArgumentException
format:@"Empty modifier flags MUST be disallowed if required modifier flags are not empty."];
}
_allowedModifierFlags = newAllowedModifierFlags;
_requiredModifierFlags = newRequiredModifierFlags;
_allowsEmptyModifierFlags = newAllowsEmptyModifierFlags;
}
- (void)setObjectValue:(NSDictionary *)newObjectValue
{
// Cocoa KVO and KVC frequently uses NSNull as object substituation of nil.
// SRRecorderControl expects either nil or valid object value, it it's convenient
// to handle handle NSNull here and convert it into nil.
if ((NSNull *)newObjectValue == [NSNull null])
newObjectValue = nil;
_objectValue = [newObjectValue copy];
if (!self.isRecording)
{
NSAccessibilityPostNotification(self, NSAccessibilityTitleChangedNotification);
[self setNeedsDisplay:YES];
}
}
#pragma mark Methods
- (BOOL)beginRecording
{
if (self.isRecording)
return YES;
[self setNeedsDisplay:YES];
if ([self.delegate respondsToSelector:@selector(shortcutRecorderShouldBeginRecording:)])
{
if (![self.delegate shortcutRecorderShouldBeginRecording:self])
{
NSBeep();
return NO;
}
}
[self willChangeValueForKey:@"isRecording"];
_isRecording = YES;
[self didChangeValueForKey:@"isRecording"];
[self updateTrackingAreas];
[self setToolTip:SRLoc(@"Type shortcut")];
NSAccessibilityPostNotification(self, NSAccessibilityTitleChangedNotification);
return YES;
}
- (void)endRecording
{
[self endRecordingWithObjectValue:self.objectValue];
}
- (void)clearAndEndRecording
{
[self endRecordingWithObjectValue:nil];
}
- (void)endRecordingWithObjectValue:(NSDictionary *)anObjectValue
{
if (!self.isRecording)
return;
[self willChangeValueForKey:@"isRecording"];
_isRecording = NO;
[self didChangeValueForKey:@"isRecording"];
NSDictionary *valueBindingInfo = _bindingInfo[NSValueBinding];
if (valueBindingInfo)
{
NSValueTransformer *transformer = _SRValueTransformerFromBindingOptions(valueBindingInfo);
if ([[transformer class] allowsReverseTransformation])
{
[valueBindingInfo[NSObservedObjectKey] setValue:[transformer reverseTransformedValue:anObjectValue]
forKeyPath:valueBindingInfo[NSObservedKeyPathKey]];
}
else
[valueBindingInfo[NSObservedObjectKey] setValue:anObjectValue forKeyPath:valueBindingInfo[NSObservedKeyPathKey]];
// objectValue will be set in -observeValueForKeyPath:ofObject:change:context:
}
else
self.objectValue = anObjectValue;
[self updateTrackingAreas];
[self setToolTip:SRLoc(@"Click to record shortcut")];
[self setNeedsDisplay:YES];
NSAccessibilityPostNotification(self, NSAccessibilityTitleChangedNotification);
if (self.window.firstResponder == self && ![self canBecomeKeyView])
[self.window makeFirstResponder:nil];
if ([self.delegate respondsToSelector:@selector(shortcutRecorderDidEndRecording:)])
[self.delegate shortcutRecorderDidEndRecording:self];
}
#pragma mark -
- (NSBezierPath *)controlShape
{
NSRect shapeBounds = self.bounds;
shapeBounds.size.height = _SRRecorderControlHeight - self.alignmentRectInsets.bottom;
shapeBounds = NSInsetRect(shapeBounds, 1.0, 1.0);
return [NSBezierPath bezierPathWithRoundedRect:shapeBounds
xRadius:_SRRecorderControlShapeXRadius
yRadius:_SRRecorderControlShapeYRadius];
}
- (NSRect)rectForLabel:(NSString *)aLabel withAttributes:(NSDictionary *)anAttributes
{
NSSize labelSize = [aLabel sizeWithAttributes:anAttributes];
NSRect enclosingRect = NSInsetRect(self.bounds, _SRRecorderControlShapeXRadius, 0.0);
labelSize.width = fmin(ceil(labelSize.width), NSWidth(enclosingRect));
labelSize.height = ceil(labelSize.height);
CGFloat fontBaselineOffsetFromTop = labelSize.height + [anAttributes[NSFontAttributeName] descender];
CGFloat baselineOffsetFromTop = _SRRecorderControlHeight - self.baselineOffsetFromBottom;
NSRect labelRect = {
.origin = NSMakePoint(NSMidX(enclosingRect) - labelSize.width / 2.0, baselineOffsetFromTop - fontBaselineOffsetFromTop),
.size = labelSize
};
labelRect = [self centerScanRect:labelRect];
// Ensure label and buttons do not overlap.
if (self.isRecording)
{
CGFloat rightOffsetFromButtons = NSMinX(self.snapBackButtonRect) - NSMaxX(labelRect);
if (rightOffsetFromButtons < 0.0)
{
labelRect = NSOffsetRect(labelRect, rightOffsetFromButtons, 0.0);
if (NSMinX(labelRect) < NSMinX(enclosingRect))
{
labelRect.size.width -= NSMinX(enclosingRect) - NSMinX(labelRect);
labelRect.origin.x = NSMinX(enclosingRect);
}
}
}
return labelRect;
}
- (NSRect)snapBackButtonRect
{
NSRect clearButtonRect = self.clearButtonRect;
NSRect bounds = self.bounds;
NSRect snapBackButtonRect = NSZeroRect;
snapBackButtonRect.origin.x = NSMinX(clearButtonRect) - _SRRecorderControlSnapBackButtonRightOffset - _SRRecorderControlSnapBackButtonSize.width - _SRRecorderControlSnapBackButtonLeftOffset;
snapBackButtonRect.origin.y = NSMinY(bounds);
snapBackButtonRect.size.width = fdim(NSMinX(clearButtonRect), NSMinX(snapBackButtonRect));
snapBackButtonRect.size.height = _SRRecorderControlHeight;
return snapBackButtonRect;
}
- (NSRect)clearButtonRect
{
NSRect bounds = self.bounds;
if ([self.objectValue count])
{
NSRect clearButtonRect = NSZeroRect;
clearButtonRect.origin.x = NSMaxX(bounds) - _SRRecorderControlClearButtonRightOffset - _SRRecorderControlClearButtonSize.width - _SRRecorderControlClearButtonLeftOffset;
clearButtonRect.origin.y = NSMinY(bounds);
clearButtonRect.size.width = fdim(NSMaxX(bounds), NSMinX(clearButtonRect));
clearButtonRect.size.height = _SRRecorderControlHeight;
return clearButtonRect;
}
else
{
return NSMakeRect(NSMaxX(bounds) - _SRRecorderControlClearButtonRightOffset - _SRRecorderControlClearButtonLeftOffset,
NSMinY(bounds),
0.0,
_SRRecorderControlHeight);
}
}
#pragma mark -
- (NSString *)label
{
NSString *label = nil;
if (self.isRecording)
{
NSUInteger modifierFlags = [NSEvent modifierFlags] & self.allowedModifierFlags;
if (modifierFlags)
label = [[SRModifierFlagsTransformer sharedTransformer] transformedValue:@(modifierFlags)];
else
label = self.stringValue;
if (![label length])
label = label = SRLoc(@"Type shortcut");
}
else
{
label = self.stringValue;
if (![label length])
label = SRLoc(@"Click to record shortcut");
}
return label;
}
- (NSString *)accessibilityLabel
{
NSString *label = nil;
if (self.isRecording)
{
NSUInteger modifierFlags = [NSEvent modifierFlags] & self.allowedModifierFlags;
label = [[SRModifierFlagsTransformer sharedPlainTransformer] transformedValue:@(modifierFlags)];
if (![label length])
label = SRLoc(@"Type shortcut");
}
else
{
label = self.accessibilityStringValue;
if (![label length])
label = SRLoc(@"Click to record shortcut");
}
return label;
}
- (NSString *)stringValue
{
if (![self.objectValue count])
return nil;
NSString *f = [[SRModifierFlagsTransformer sharedTransformer] transformedValue:self.objectValue[SRShortcutModifierFlagsKey]];
SRKeyCodeTransformer *transformer = nil;
if (self.drawsASCIIEquivalentOfShortcut)
transformer = [SRKeyCodeTransformer sharedPlainASCIITransformer];
else
transformer = [SRKeyCodeTransformer sharedPlainTransformer];
NSString *c = [transformer transformedValue:self.objectValue[SRShortcutKeyCode]];
if (![transformer isKeyCodeSpecial:[self.objectValue[SRShortcutKeyCode] unsignedShortValue]])
c = [c uppercaseString];
return [NSString stringWithFormat:@"%@%@", f, c];
}
- (NSString *)accessibilityStringValue
{
if (![self.objectValue count])
return nil;
NSString *f = [[SRModifierFlagsTransformer sharedPlainTransformer] transformedValue:self.objectValue[SRShortcutModifierFlagsKey]];
NSString *c = nil;
if (self.drawsASCIIEquivalentOfShortcut)
c = [[SRKeyCodeTransformer sharedPlainASCIITransformer] transformedValue:self.objectValue[SRShortcutKeyCode]];
else
c = [[SRKeyCodeTransformer sharedPlainTransformer] transformedValue:self.objectValue[SRShortcutKeyCode]];
if ([f length] > 0)
return [NSString stringWithFormat:@"%@-%@", f, c];
else
return [NSString stringWithFormat:@"%@", c];
}
- (NSDictionary *)labelAttributes
{
return self.isRecording ? [self recordingLabelAttributes] : [self normalLabelAttributes];
}
- (NSDictionary *)normalLabelAttributes
{
static dispatch_once_t OnceToken;
static NSDictionary *NormalAttributes = nil;
dispatch_once(&OnceToken, ^{
NSMutableParagraphStyle *p = [[NSMutableParagraphStyle alloc] init];
p.alignment = NSCenterTextAlignment;
p.lineBreakMode = NSLineBreakByTruncatingTail;
p.baseWritingDirection = NSWritingDirectionLeftToRight;
NormalAttributes = @{
NSParagraphStyleAttributeName: [p copy],
NSFontAttributeName: [NSFont labelFontOfSize:[NSFont systemFontSize]],
NSForegroundColorAttributeName: [NSColor controlTextColor]
};
});
return NormalAttributes;
}
- (NSDictionary *)recordingLabelAttributes
{
static dispatch_once_t OnceToken;
static NSDictionary *RecordingAttributes = nil;
dispatch_once(&OnceToken, ^{
NSMutableParagraphStyle *p = [[NSMutableParagraphStyle alloc] init];
p.alignment = NSCenterTextAlignment;
p.lineBreakMode = NSLineBreakByTruncatingTail;
p.baseWritingDirection = NSWritingDirectionLeftToRight;
RecordingAttributes = @{
NSParagraphStyleAttributeName: [p copy],
NSFontAttributeName: [NSFont labelFontOfSize:[NSFont systemFontSize]],
NSForegroundColorAttributeName: [NSColor disabledControlTextColor]
};
});
return RecordingAttributes;
}
#pragma mark -
- (void)drawBackground:(NSRect)aDirtyRect
{
[NSGraphicsContext saveGraphicsState];
NSRect frame = self.bounds;
frame.size.height = _SRRecorderControlHeight;
if (self.isRecording)
{
NSDrawThreePartImage(frame,
_SRImages[3],
_SRImages[4],
_SRImages[5],
NO,
NSCompositeSourceOver,
1.0,
self.isFlipped);
}
else
{
if (self.isMainButtonHighlighted)
{
if ([NSColor currentControlTint] == NSBlueControlTint)
{
NSDrawThreePartImage(frame,
_SRImages[0],
_SRImages[1],
_SRImages[2],
NO,
NSCompositeSourceOver,
1.0,
self.isFlipped);
}
else
{
NSDrawThreePartImage(frame,
_SRImages[6],
_SRImages[7],
_SRImages[8],
NO,
NSCompositeSourceOver,
1.0,
self.isFlipped);
}
}
else
{
NSDrawThreePartImage(frame,
_SRImages[9],
_SRImages[10],
_SRImages[11],
NO,
NSCompositeSourceOver,
1.0,
self.isFlipped);
}
}
[NSGraphicsContext restoreGraphicsState];
}
- (void)drawInterior:(NSRect)aDirtyRect
{
[self drawLabel:aDirtyRect];
if (self.isRecording)
{
[self drawSnapBackButton:aDirtyRect];
[self drawClearButton:aDirtyRect];
}
}
- (void)drawLabel:(NSRect)aDirtyRect
{
NSString *label = self.label;
NSDictionary *labelAttributes = self.labelAttributes;
NSRect labelRect = [self rectForLabel:label withAttributes:labelAttributes];
if (!NSIntersectsRect(labelRect, aDirtyRect))
return;
[NSGraphicsContext saveGraphicsState];
[label drawInRect:labelRect withAttributes:labelAttributes];
[NSGraphicsContext restoreGraphicsState];
}
- (void)drawSnapBackButton:(NSRect)aDirtyRect
{
NSRect imageRect = self.snapBackButtonRect;
imageRect.origin.x += _SRRecorderControlSnapBackButtonLeftOffset;
imageRect.origin.y += floor(self.alignmentRectInsets.top + (NSHeight(imageRect) - _SRRecorderControlSnapBackButtonSize.height) / 2.0);
imageRect.size = _SRRecorderControlSnapBackButtonSize;
imageRect = [self centerScanRect:imageRect];
if (!NSIntersectsRect(imageRect, aDirtyRect))
return;
[NSGraphicsContext saveGraphicsState];
if (self.isSnapBackButtonHighlighted)
{
[_SRImages[14] drawInRect:imageRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
}
else
{
[_SRImages[15] drawInRect:imageRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
}
[NSGraphicsContext restoreGraphicsState];
}
- (void)drawClearButton:(NSRect)aDirtyRect
{
NSRect imageRect = self.clearButtonRect;
// If there is no reason to draw clear button (e.g. no shortcut was set)
// rect will have empty width.
if (NSWidth(imageRect) == 0.0)
return;
imageRect.origin.x += _SRRecorderControlClearButtonLeftOffset;
imageRect.origin.y += floor(self.alignmentRectInsets.top + (NSHeight(imageRect) - _SRRecorderControlClearButtonSize.height) / 2.0);
imageRect.size = _SRRecorderControlClearButtonSize;
imageRect = [self centerScanRect:imageRect];
if (!NSIntersectsRect(imageRect, aDirtyRect))
return;
[NSGraphicsContext saveGraphicsState];
if (self.isClearButtonHighlighted)
{
[_SRImages[12] drawInRect:imageRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
}
else
{
[_SRImages[13] drawInRect:imageRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
}
[NSGraphicsContext restoreGraphicsState];
}
#pragma mark -
- (BOOL)isMainButtonHighlighted
{
if (_mouseTrackingButtonTag == _SRRecorderControlMainButtonTag)
{
NSPoint locationInView = [self convertPoint:self.window.mouseLocationOutsideOfEventStream
fromView:nil];
return [self mouse:locationInView inRect:self.bounds];
}
else
return NO;
}
- (BOOL)isSnapBackButtonHighlighted
{
if (_mouseTrackingButtonTag == _SRRecorderControlSnapBackButtonTag)
{
NSPoint locationInView = [self convertPoint:self.window.mouseLocationOutsideOfEventStream
fromView:nil];
return [self mouse:locationInView inRect:self.snapBackButtonRect];
}
else
return NO;
}
- (BOOL)isClearButtonHighlighted
{
if (_mouseTrackingButtonTag == _SRRecorderControlClearButtonTag)
{
NSPoint locationInView = [self convertPoint:self.window.mouseLocationOutsideOfEventStream
fromView:nil];
return [self mouse:locationInView inRect:self.clearButtonRect];
}
else
return NO;
}
- (BOOL)areModifierFlagsValid:(NSUInteger)aModifierFlags
{
aModifierFlags &= SRCocoaModifierFlagsMask;
if (aModifierFlags == 0 && !self.allowsEmptyModifierFlags)
return NO;
else if ((aModifierFlags & self.requiredModifierFlags) != self.requiredModifierFlags)
return NO;
else if ((aModifierFlags & self.allowedModifierFlags) != aModifierFlags)
return NO;
else
return YES;
}
#pragma mark NSAccessibility
- (BOOL)accessibilityIsIgnored
{
return NO;
}
- (NSArray *)accessibilityAttributeNames
{
static NSArray *AttributeNames = nil;
static dispatch_once_t OnceToken;
dispatch_once(&OnceToken, ^
{
AttributeNames = [[super accessibilityAttributeNames] mutableCopy];
NSArray *newAttributes = @[
NSAccessibilityRoleAttribute,
NSAccessibilityTitleAttribute
];
for (NSString *attributeName in newAttributes)
{
if (![AttributeNames containsObject:attributeName])
[(NSMutableArray *)AttributeNames addObject:attributeName];
}
AttributeNames = [AttributeNames copy];
});
return AttributeNames;
}
- (id)accessibilityAttributeValue:(NSString *)anAttributeName
{
if ([anAttributeName isEqualToString:NSAccessibilityRoleAttribute])
return NSAccessibilityButtonRole;
else if ([anAttributeName isEqualToString:NSAccessibilityTitleAttribute])
return self.accessibilityLabel;
else
return [super accessibilityAttributeValue:anAttributeName];
}
- (NSArray *)accessibilityActionNames
{
static NSArray *ActionNames = nil;
static dispatch_once_t OnceToken;
dispatch_once(&OnceToken, ^
{
ActionNames = @[
NSAccessibilityPressAction,
NSAccessibilityCancelAction,
NSAccessibilityDeleteAction
];
});
return ActionNames;
}
- (NSString *)accessibilityActionDescription:(NSString *)anAction
{
return NSAccessibilityActionDescription(anAction);
}
- (void)accessibilityPerformAction:(NSString *)anAction
{
if ([anAction isEqualToString:NSAccessibilityPressAction])
[self beginRecording];
else if (self.isRecording && [anAction isEqualToString:NSAccessibilityCancelAction])
[self endRecording];
else if (self.isRecording && [anAction isEqualToString:NSAccessibilityDeleteAction])
[self clearAndEndRecording];
}
#pragma mark NSKeyValueBindingCreation
- (Class)valueClassForBinding:(NSString *)aBinding
{
if ([aBinding isEqualToString:NSValueBinding])
return [NSDictionary class];
else
return [super valueClassForBinding:aBinding];
}
- (void)bind:(NSString *)aBinding toObject:(id)anObservable withKeyPath:(NSString *)aKeyPath options:(NSDictionary *)anOptions
{
if ([aBinding isEqualToString:NSValueBinding])
{
[self unbind:aBinding];
[anObservable addObserver:self
forKeyPath:aKeyPath
options:0
context:&_SRValueObservationContext];
_bindingInfo[aBinding] = @{
NSObservedObjectKey: anObservable,
NSObservedKeyPathKey: [aKeyPath copy],
NSOptionsKey: [NSDictionary dictionaryWithDictionary:anOptions]
};
self.objectValue = [anObservable valueForKeyPath:aKeyPath];
// This method is typically called when view is not presented to a user.
// If'd use -setNeedsDisplay:, the user may notice flickering when window with view is shown first time.
// Therefore ensure view is shown with correct value drawn by -displayIfNeeded
[self displayIfNeeded];
}
else
[super bind:aBinding toObject:anObservable withKeyPath:aKeyPath options:anOptions];
}
- (NSDictionary *)infoForBinding:(NSString *)aBinding
{
NSDictionary *info = _bindingInfo[aBinding];
if (!info)
info = [super infoForBinding:aBinding];
return info;
}
- (void)unbind:(NSString *)aBinding
{
if ([aBinding isEqualToString:NSValueBinding])
{
NSDictionary *valueBindingInfo = _bindingInfo[NSValueBinding];
if (valueBindingInfo)
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
[valueBindingInfo[NSObservedObjectKey] removeObserver:self forKeyPath:valueBindingInfo[NSObservedKeyPathKey]];
else
[valueBindingInfo[NSObservedObjectKey] removeObserver:self forKeyPath:valueBindingInfo[NSObservedKeyPathKey] context:&_SRValueObservationContext];
[_bindingInfo removeObjectForKey:NSValueBinding];
}
}
else
[super unbind:aBinding];
}
#pragma mark NSToolTipOwner
- (NSString *)view:(NSView *)aView stringForToolTip:(NSToolTipTag)aTag point:(NSPoint)aPoint userData:(void *)aData
{
if (aTag == _snapBackButtonToolTipTag)
return SRLoc(@"Use old shortcut");
else
return [super view:aView stringForToolTip:aTag point:aPoint userData:aData];
}
#pragma mark NSView
- (BOOL)isOpaque
{
return NO;
}
- (BOOL)isFlipped
{
return YES;
}
- (void)viewWillDraw
{
[super viewWillDraw];
static dispatch_once_t OnceToken;
dispatch_once(&OnceToken, ^{
_SRImages[0] = SRImage(@"shortcut-recorder-bezel-blue-highlighted-left");
_SRImages[1] = SRImage(@"shortcut-recorder-bezel-blue-highlighted-middle");
_SRImages[2] = SRImage(@"shortcut-recorder-bezel-blue-highlighted-right");
_SRImages[3] = SRImage(@"shortcut-recorder-bezel-editing-left");
_SRImages[4] = SRImage(@"shortcut-recorder-bezel-editing-middle");
_SRImages[5] = SRImage(@"shortcut-recorder-bezel-editing-right");
_SRImages[6] = SRImage(@"shortcut-recorder-bezel-graphite-highlight-mask-left");
_SRImages[7] = SRImage(@"shortcut-recorder-bezel-graphite-highlight-mask-middle");
_SRImages[8] = SRImage(@"shortcut-recorder-bezel-graphite-highlight-mask-right");
_SRImages[9] = SRImage(@"shortcut-recorder-bezel-left");
_SRImages[10] = SRImage(@"shortcut-recorder-bezel-middle");
_SRImages[11] = SRImage(@"shortcut-recorder-bezel-right");
_SRImages[12] = SRImage(@"shortcut-recorder-clear-highlighted");
_SRImages[13] = SRImage(@"shortcut-recorder-clear");
_SRImages[14] = SRImage(@"shortcut-recorder-snapback-highlighted");
_SRImages[15] = SRImage(@"shortcut-recorder-snapback");
});
}
- (void)drawRect:(NSRect)aDirtyRect
{
[self drawBackground:aDirtyRect];
[self drawInterior:aDirtyRect];
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
{
if (self.window.firstResponder == self)
{
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[self.controlShape fill];
[NSGraphicsContext restoreGraphicsState];
}
}
}
- (void)drawFocusRingMask
{
if (self.window.firstResponder == self)
[self.controlShape fill];
}
- (NSRect)focusRingMaskBounds
{
if (self.window.firstResponder == self)
return self.controlShape.bounds;
else
return NSZeroRect;
}
- (NSEdgeInsets)alignmentRectInsets
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6 || self.window == nil)
return NSEdgeInsetsMake(0.0, 0.0, _SRRecorderControlBottomShadowHeightInPixels, 0.0);
else
return NSEdgeInsetsMake(0.0, 0.0, _SRRecorderControlBottomShadowHeightInPixels / self.window.backingScaleFactor, 0.0);
}
- (CGFloat)baselineOffsetFromBottom
{
// True method to calculate is presented above. Unfortunately Cocoa implementation of Mac OS X 10.8.2 expects this value to be persistant.
// If baselineOffsetFromBottom depends on some other properties and may return different values for different calls,
// NSLayoutFormatAlignAllBaseline may not work. For this reason we return the constant.
// If you're going to change layout of the view, uncomment the line below, look what it typically returns and update the constant.
// TODO: Hopefully it will be fixed some day in Cocoa and therefore in SRRecorderControl.
// CGFloat baseline = fdim(NSHeight(self.bounds), _SRRecorderControlHeight) + floor(_SRRecorderControlBaselineOffset - [self.labelAttributes[NSFontAttributeName] descender]);
return 8.0;
}
- (NSSize)intrinsicContentSize
{
return NSMakeSize(NSWidth([self rectForLabel:SRLoc(@"Click to record shortcut") withAttributes:self.normalLabelAttributes]) + _SRRecorderControlShapeXRadius + _SRRecorderControlShapeXRadius,
_SRRecorderControlHeight);
}
- (void)updateTrackingAreas
{
static const NSUInteger TrackingOptions = NSTrackingMouseEnteredAndExited | NSTrackingActiveWhenFirstResponder | NSTrackingEnabledDuringMouseDrag;
if (_mainButtonTrackingArea)
[self removeTrackingArea:_mainButtonTrackingArea];
_mainButtonTrackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
options:TrackingOptions
owner:self
userInfo:nil];
[self addTrackingArea:_mainButtonTrackingArea];
if (_snapBackButtonTrackingArea)
{
[self removeTrackingArea:_snapBackButtonTrackingArea];
_snapBackButtonTrackingArea = nil;
}
if (_clearButtonTrackingArea)
{
[self removeTrackingArea:_clearButtonTrackingArea];
_clearButtonTrackingArea = nil;
}
if (_snapBackButtonToolTipTag != NSIntegerMax)
{
[self removeToolTip:_snapBackButtonToolTipTag];
_snapBackButtonToolTipTag = NSIntegerMax;
}
if (self.isRecording)
{
_snapBackButtonTrackingArea = [[NSTrackingArea alloc] initWithRect:self.snapBackButtonRect
options:TrackingOptions
owner:self
userInfo:nil];
[self addTrackingArea:_snapBackButtonTrackingArea];
_clearButtonTrackingArea = [[NSTrackingArea alloc] initWithRect:self.clearButtonRect
options:TrackingOptions
owner:self
userInfo:nil];
[self addTrackingArea:_clearButtonTrackingArea];
// Since this method is used to set up tracking rects of aux buttons, the rest of the code is aware
// it should be called whenever geometry or apperance changes. Therefore it's a good place to set up tooltip rects.
_snapBackButtonToolTipTag = [self addToolTipRect:[_snapBackButtonTrackingArea rect] owner:self userData:NULL];
}