Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions SRCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ FOUNDATION_STATIC_INLINE NSImage *SRImage(NSString *anImageName)
#else
NSBundle *b = [NSBundle bundleWithIdentifier:@"com.kulakov.ShortcutRecorder"];

if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
return [[NSImage alloc] initByReferencingURL:[b URLForImageResource:anImageName]];
else
return [b imageForResource:anImageName];
return [b imageForResource:anImageName];
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions SRRecorderControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ extern NSString *const SRShortcutCharactersIgnoringModifiers;
*/
@property BOOL allowsDeleteToClearShortcutAndEndRecording;

@property BOOL isEnabled;

/*!
@brief Determines whether recording is in process.
*/
Expand Down
98 changes: 52 additions & 46 deletions SRRecorderControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ @implementation SRRecorderControl
NSMutableDictionary *_bindingInfo;

BOOL _shouldDrawBackground;
BOOL _isEnabled;
}

- (instancetype)initWithFrame:(NSRect)aFrameRect
Expand All @@ -129,25 +130,23 @@ - (instancetype)initWithFrame:(NSRect)aFrameRect
_drawsASCIIEquivalentOfShortcut = YES;
_allowsEscapeToCancelRecording = YES;
_allowsDeleteToClearShortcutAndEndRecording = YES;
_isEnabled = YES;
_mouseTrackingButtonTag = _SRRecorderControlInvalidButtonTag;
_snapBackButtonToolTipTag = NSIntegerMax;
_bindingInfo = [NSMutableDictionary dictionary];
_fontSize = 12.0;

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.translatesAutoresizingMaskIntoConstraints = NO;

[self setContentHuggingPriority:NSLayoutPriorityDefaultLow
forOrientation:NSLayoutConstraintOrientationHorizontal];
[self setContentHuggingPriority:NSLayoutPriorityRequired
forOrientation:NSLayoutConstraintOrientationVertical];

[self setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow
forOrientation:NSLayoutConstraintOrientationHorizontal];
[self setContentCompressionResistancePriority:NSLayoutPriorityRequired
forOrientation:NSLayoutConstraintOrientationVertical];

NSString* sgwaesf = NSLocalizedStringFromTableInBundle(@"+ Shortcut",
@"ShortcutRecorder",
Expand Down Expand Up @@ -210,12 +209,22 @@ - (void)setObjectValue:(NSDictionary *)newObjectValue
}
}

- (void)setIsEnabled:(BOOL)isEnabled
{
_isEnabled = isEnabled;
[self setNeedsDisplay:YES];
}

-(BOOL)isEnabled
{
return _isEnabled;
}

#pragma mark Methods

- (BOOL)beginRecording
{
if (self.isRecording)
if (self.isRecording || !self.isEnabled)
return YES;

[self setNeedsDisplay:YES];
Expand Down Expand Up @@ -541,10 +550,12 @@ - (void)drawBackground:(NSRect)aDirtyRect
CGFloat corner = frame.size.height / 2;
frame = NSInsetRect(frame, 1, 1);

CGFloat opacity = self.isEnabled ? 1 : 0.5;

NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:corner yRadius:corner];
path.lineWidth = 1;
[[NSColor controlColor] setFill];
[[NSColor grayColor] setStroke];
[[[NSColor controlColor] colorWithAlphaComponent:opacity] setFill];
[[[NSColor grayColor] colorWithAlphaComponent:opacity] setStroke];
[path fill];
[path stroke];

Expand Down Expand Up @@ -760,6 +771,11 @@ - (NSString *)accessibilityActionDescription:(NSString *)anAction

- (void)accessibilityPerformAction:(NSString *)anAction
{
if (!self.isEnabled)
{
return;
}

if ([anAction isEqualToString:NSAccessibilityPressAction])
[self beginRecording];
else if (self.isRecording && [anAction isEqualToString:NSAccessibilityCancelAction])
Expand Down Expand Up @@ -823,10 +839,7 @@ - (void)unbind:(NSString *)aBinding

if (valueBindingInfo)
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
[valueBindingInfo[NSObservedObjectKey] removeObserver:self forKeyPath:valueBindingInfo[NSObservedKeyPathKey]];
else
[valueBindingInfo[NSObservedObjectKey] removeObserver:self forKeyPath:valueBindingInfo[NSObservedKeyPathKey] context:&_SRValueObservationContext];
[valueBindingInfo[NSObservedObjectKey] removeObserver:self forKeyPath:valueBindingInfo[NSObservedKeyPathKey] context:&_SRValueObservationContext];

[_bindingInfo removeObjectForKey:NSValueBinding];
}
Expand Down Expand Up @@ -883,22 +896,11 @@ - (void)drawRect:(NSRect)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)
if (self.window.firstResponder == self && self.isEnabled)
[self.controlShape fill];
}

Expand All @@ -912,7 +914,7 @@ - (NSRect)focusRingMaskBounds

- (NSEdgeInsets)alignmentRectInsets
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6 || self.window == nil)
if (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);
Expand Down Expand Up @@ -1021,19 +1023,8 @@ - (BOOL)acceptsFirstResponder
return YES;
}

- (BOOL)becomeFirstResponder
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
[self setKeyboardFocusRingNeedsDisplayInRect:self.bounds];

return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_6)
[self setKeyboardFocusRingNeedsDisplayInRect:self.bounds];

[self endRecording];
_mouseTrackingButtonTag = _SRRecorderControlInvalidButtonTag;
return [super resignFirstResponder];
Expand All @@ -1058,6 +1049,11 @@ - (BOOL)needsPanelToBecomeKey

- (void)mouseDown:(NSEvent *)anEvent
{
if (!self.isEnabled)
{
return;
}

NSPoint locationInView = [self convertPoint:anEvent.locationInWindow fromView:nil];

if (self.isRecording)
Expand Down Expand Up @@ -1123,6 +1119,11 @@ - (void)mouseUp:(NSEvent *)anEvent

- (void)mouseEntered:(NSEvent *)anEvent
{
if (!self.isEnabled)
{
return;
}

if (_borderlessButton)
{
_shouldDrawBackground = YES;
Expand All @@ -1141,6 +1142,11 @@ - (void)mouseEntered:(NSEvent *)anEvent

- (void)mouseExited:(NSEvent *)anEvent
{
if (self.isEnabled)
{
return;
}

if (_borderlessButton)
{
_shouldDrawBackground = NO;
Expand All @@ -1165,7 +1171,7 @@ - (void)keyDown:(NSEvent *)anEvent

- (BOOL)performKeyEquivalent:(NSEvent *)anEvent
{
if (self.window.firstResponder != self)
if (self.window.firstResponder != self || !self.isEnabled)
return NO;

if (_mouseTrackingButtonTag != _SRRecorderControlInvalidButtonTag)
Expand Down