Skip to content

Commit

Permalink
Image drawing updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
avaidyam committed Dec 19, 2012
1 parent eabdf66 commit 49bef4e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
3 changes: 0 additions & 3 deletions lib/UIKit/TUIButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ typedef enum TUIButtonType : NSUInteger {
// The titleLabel is the label on which the button text will be dynamically drawn.
@property (nonatomic, strong, readonly) TUILabel *titleLabel;

// The imageView is the view on which the button content image will be drawn.
@property (nonatomic, strong, readonly) TUIImageView *imageView;

// Allows a pop up menu to be displayed if the button is pressed.
// Setting this overrides selectable property as YES.
@property (nonatomic, strong) NSMenu *menu;
Expand Down
36 changes: 7 additions & 29 deletions lib/UIKit/TUIButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ @interface TUIButton () {
}

@property (nonatomic, strong) NSMutableDictionary *contentLookup;

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

@end

Expand Down Expand Up @@ -99,7 +97,7 @@ - (TUIButtonType)buttonType {
return _buttonFlags.buttonType;
}

#pragma mark - Content
#pragma mark - Properties

- (TUILabel *)titleLabel {
if(!_titleLabel) {
Expand All @@ -114,18 +112,6 @@ - (TUILabel *)titleLabel {
return _titleLabel;
}

- (TUIImageView *)imageView {
if(!_imageView) {
_imageView = [[TUIImageView alloc] initWithFrame:TUIEdgeInsetsInsetRect(self.bounds, self.imageEdgeInsets)];
_imageView.backgroundColor = [NSColor clearColor];

[self addSubview:_imageView];
}
return _imageView;
}

#pragma mark - Properties

- (BOOL)dimsInBackground {
return _buttonFlags.dimsInBackground;
}
Expand Down Expand Up @@ -218,7 +204,7 @@ - (void)drawBackground:(CGRect)rect {

// Set the graphics renderer states so CoreUI draws the button for us properly.
[[TUIButton sharedGraphicsRenderer] setHighlighted:secondaryState];
[[TUIButton sharedGraphicsRenderer] setEnabled:backgroundState];
[[TUIButton sharedGraphicsRenderer] setEnabled:!backgroundState];

// If we found the proper graphics style, allow the graphics renderer to draw it.
// If not, draw it ourselves (Inline or Custom styles only, currently).
Expand Down Expand Up @@ -290,19 +276,11 @@ - (void)drawContent:(CGRect)rect {
appliedImageRect = imageRect = ABRectCenteredInRect(imageRect, TUIEdgeInsetsInsetRect(b, self.imageEdgeInsets));
}

// Shadow or highlight the image if either option is enabled.
CGFloat alpha = backgroundState ? 0.5f : 1.0f;
if(_buttonFlags.adjustsImageWhenDisabled || _buttonFlags.adjustsImageWhenHighlighted) {
[image lockFocus]; {
if(_buttonFlags.adjustsImageWhenHighlighted)
[[NSColor colorWithCalibratedWhite:0.0 alpha:(1.0f / 3.0f)] set];
else if(_buttonFlags.adjustsImageWhenDisabled)
[[NSColor colorWithCalibratedWhite:1.0 alpha:(1.0f / 3.0f)] set];
NSRectFillUsingOperation((NSRect) {.size = image.size}, NSCompositeSourceAtop);
} [image unlockFocus];
}

[image drawInRect:ABRectRoundOrigin(imageRect) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:alpha];
// Using the shared graphics renderer, we get free template image drawing,
// along with disabled and highlighted state drawing.
[[TUIButton sharedGraphicsRenderer] setEnabled:!backgroundState];
[[TUIButton sharedGraphicsRenderer] setHighlighted:secondaryState];
[[TUIButton sharedGraphicsRenderer] drawImage:image withFrame:imageRect inView:self.nsView];
}

if(self.imagePosition != TUIControlImagePositionOnly) {
Expand Down

0 comments on commit 49bef4e

Please sign in to comment.