Last active
August 29, 2015 14:23
-
-
Save kuon/c8013d29c5a1a422761e to your computer and use it in GitHub Desktop.
Quick focus ring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface FocusView : NSTextView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FocusView.h" | |
@interface FocusView () | |
@property BOOL focusRing; | |
@end | |
@implementation FocusView | |
- (BOOL)becomeFirstResponder { | |
BOOL res = [super becomeFirstResponder]; | |
self.focusRing = res; | |
[self setNeedsDisplay:YES]; | |
return res; | |
} | |
- (BOOL)resignFirstResponder { | |
BOOL res = [super resignFirstResponder]; | |
self.focusRing = !res; | |
[self setNeedsDisplay:YES]; | |
return res; | |
} | |
- (void)drawRect:(NSRect)dirtyRect { | |
[super drawRect:dirtyRect]; | |
if (self.focusRing) { | |
CGContextRef ctx = NSGraphicsContext.currentContext.CGContext; | |
CGRect r = (CGRect){CGPointZero, self.frame.size}; | |
CGContextSetStrokeColorWithColor(ctx, NSColor.keyboardFocusIndicatorColor.CGColor); | |
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), 4, NSColor.keyboardFocusIndicatorColor.CGColor); | |
CGContextSetLineWidth(ctx, 2); | |
CGContextStrokeRect(ctx, r); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might also check if the Window is in the background, to not draw the focus ring when the window is not the front most.