Skip to content

Commit

Permalink
Highly efficient shared button renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
avaidyam committed Dec 15, 2012
1 parent 069000c commit 7398ca9
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/UIKit/TUIButton.m
Expand Up @@ -49,15 +49,20 @@ @interface TUIButton () {

@property (nonatomic, strong) NSMutableDictionary *contentLookup;

@property (nonatomic, strong) NSButtonCell *backingCell;

@property (nonatomic, strong, readwrite) TUILabel *titleLabel;
@property (nonatomic, strong, readwrite) TUIImageView *imageView;

@end

@implementation TUIButton

+ (NSButtonCell *)sharedButtonRenderer {
static NSButtonCell *_backingCell = nil;
if(!_backingCell)
_backingCell = [NSButtonCell new];
return _backingCell;
}

#pragma mark - Initialization

+ (instancetype)buttonWithType:(TUIButtonType)buttonType {
Expand All @@ -69,8 +74,6 @@ + (instancetype)buttonWithType:(TUIButtonType)buttonType {

- (id)initWithFrame:(CGRect)frame {
if((self = [super initWithFrame:frame])) {
self.backingCell = [NSButtonCell new];

self.contentLookup = [NSMutableDictionary dictionary];
_buttonFlags.buttonType = TUIButtonTypeStandard;

Expand Down Expand Up @@ -195,18 +198,18 @@ - (CGSize)sizeThatFits:(CGSize)size {
- (void)drawBackground:(CGRect)rect {
BOOL secondaryState = (self.state & TUIControlStateHighlighted) || (self.state & TUIControlStateSelected);
CGRect drawingRect = [self backgroundRectForBounds:self.bounds];
[self.backingCell setHighlighted:secondaryState];
[[TUIButton sharedButtonRenderer] setHighlighted:secondaryState];

if(self.buttonType == TUIButtonTypeStandard) {
[self.backingCell setBezelStyle:NSRoundedBezelStyle];
[[TUIButton sharedButtonRenderer] setBezelStyle:NSRoundedBezelStyle];
} else if(self.buttonType == TUIButtonTypeMinimal) {
[self.backingCell setBezelStyle:NSRoundRectBezelStyle];
[[TUIButton sharedButtonRenderer] setBezelStyle:NSRoundRectBezelStyle];
} else if(self.buttonType == TUIButtonTypeTextured) {
[self.backingCell setBezelStyle:NSTexturedRoundedBezelStyle];
[[TUIButton sharedButtonRenderer] setBezelStyle:NSTexturedRoundedBezelStyle];
} else if(self.buttonType == TUIButtonTypeRectangular) {
[self.backingCell setBezelStyle:NSSmallSquareBezelStyle];
[[TUIButton sharedButtonRenderer] setBezelStyle:NSSmallSquareBezelStyle];
} else if(self.buttonType == TUIButtonTypeCircular) {
[self.backingCell setBezelStyle:NSCircularBezelStyle];
[[TUIButton sharedButtonRenderer] setBezelStyle:NSCircularBezelStyle];
} else if(self.buttonType == TUIButtonTypeInline) {
CGFloat radius = self.bounds.size.height / 2;
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:drawingRect xRadius:radius yRadius:radius];
Expand Down Expand Up @@ -235,7 +238,7 @@ - (void)drawBackground:(CGRect)rect {
}

if(self.buttonType != TUIButtonTypeInline && self.buttonType != TUIButtonTypeCustom)
[self.backingCell drawBezelWithFrame:drawingRect inView:self.nsView];
[[TUIButton sharedButtonRenderer] drawBezelWithFrame:drawingRect inView:self.nsView];
}

- (void)drawRect:(CGRect)rect {
Expand Down

0 comments on commit 7398ca9

Please sign in to comment.