Skip to content

Instantly share code, notes, and snippets.

@kuon
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuon/c8013d29c5a1a422761e to your computer and use it in GitHub Desktop.
Save kuon/c8013d29c5a1a422761e to your computer and use it in GitHub Desktop.
Quick focus ring
#import <Cocoa/Cocoa.h>
@interface FocusView : NSTextView
@end
#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
@kuon
Copy link
Author

kuon commented Jun 29, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment