From 3c763d9a4d5da7920cf78da3df69256e5dfb3d65 Mon Sep 17 00:00:00 2001 From: Danny Greg Date: Mon, 18 Feb 2013 20:12:59 +0000 Subject: [PATCH] Add in the shadowed text field cell. --- Rebel/RBLShadowedTextFieldCell.h | 17 +++++++ Rebel/RBLShadowedTextFieldCell.m | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 Rebel/RBLShadowedTextFieldCell.h create mode 100644 Rebel/RBLShadowedTextFieldCell.m diff --git a/Rebel/RBLShadowedTextFieldCell.h b/Rebel/RBLShadowedTextFieldCell.h new file mode 100644 index 0000000..c1799e2 --- /dev/null +++ b/Rebel/RBLShadowedTextFieldCell.h @@ -0,0 +1,17 @@ +// +// RBLShadowedTextFieldCell.h +// Rebel +// +// Created by Danny Greg on 18/02/2013. +// Copyright (c) 2013 GitHub. All rights reserved. +// + +#import + +extern NSInteger const RBLShadowedTextFieldAllBackgroundStyles; + +@interface RBLShadowedTextFieldCell : NSTextFieldCell + +- (void)setShadow:(NSShadow *)shadow forBackgroundStyle:(NSBackgroundStyle)backgroundStyle; + +@end diff --git a/Rebel/RBLShadowedTextFieldCell.m b/Rebel/RBLShadowedTextFieldCell.m new file mode 100644 index 0000000..0ea2081 --- /dev/null +++ b/Rebel/RBLShadowedTextFieldCell.m @@ -0,0 +1,76 @@ +// +// RBLShadowedTextFieldCell.m +// Rebel +// +// Created by Danny Greg on 18/02/2013. +// Copyright (c) 2013 GitHub. All rights reserved. +// + +#import "RBLShadowedTextFieldCell.h" + +#import "NSColor+RBLCGColorAdditions.h" + +NSInteger const RBLShadowedTextFieldAllBackgroundStyles = 0xFFFFFFFF; + +@interface RBLShadowedTextFieldCell () + +@property (nonatomic, readonly, strong) NSMutableDictionary *backgroundStylesToShadows; + +@end + +@implementation RBLShadowedTextFieldCell + +#pragma mark - Lifecycle + +- (void)commonInit { + _backgroundStylesToShadows = [NSMutableDictionary dictionary]; +} + +- (instancetype)initWithCoder:(NSCoder *)aDecoder { + self = [super initWithCoder:aDecoder]; + if (self == nil) return nil; + + [self commonInit]; + + return self; +} + +- (instancetype)initTextCell:(NSString *)aString { + self = [super initTextCell:aString]; + if (self == nil) return nil; + + [self commonInit]; + + return self; +} + +#pragma mark - Drawing + +- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { + [NSGraphicsContext saveGraphicsState]; + NSShadow *shadow = self.backgroundStylesToShadows[@(self.backgroundStyle)]; + if (shadow == nil) { + shadow = self.backgroundStylesToShadows[@(RBLShadowedTextFieldAllBackgroundStyles)]; + } + + if (shadow != nil) { + CGContextSetShadowWithColor(NSGraphicsContext.currentContext.graphicsPort, shadow.shadowOffset, shadow.shadowBlurRadius, shadow.shadowColor.rbl_CGColor); + } + + [super drawWithFrame:cellFrame inView:controlView]; + + [NSGraphicsContext restoreGraphicsState]; +} + +#pragma mark - API + +- (void)setShadow:(NSShadow *)shadow forBackgroundStyle:(NSBackgroundStyle)backgroundStyle { + if (shadow == nil) { + [self.backgroundStylesToShadows removeObjectForKey:@(backgroundStyle)]; + return; + } + + self.backgroundStylesToShadows[@(backgroundStyle)] = shadow; +} + +@end