Skip to content

Commit

Permalink
make views draw their background color into their context so that we …
Browse files Browse the repository at this point in the history
…get subpixel AA, little bit a refactoring
  • Loading branch information
joshaber committed Jun 17, 2011
1 parent 27f3a75 commit fa446bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Classes/MAUIKit/MAUIView.h
Expand Up @@ -41,7 +41,7 @@ typedef enum {
- (void)setNeedsDisplay;

- (void)layoutSubviews;
- (void)drawRect:(CGRect)rect;
- (void)drawRect:(CGRect)dirtyRect;

- (CGPoint)convertPoint:(CGPoint)point fromView:(MAUIView *)view;
- (CGPoint)convertPoint:(CGPoint)point toView:(MAUIView *)view;
Expand Down
26 changes: 10 additions & 16 deletions Classes/MAUIKit/MAUIView.m
Expand Up @@ -7,13 +7,12 @@
//

#import "MAUIView.h"
#import "NSColor+MAUIKitExtensions.h"

static NSMutableArray *animationStack = nil;

@interface MAUIView ()
@property (nonatomic, retain) CALayer *layer;
@property (nonatomic, assign) BOOL manuallyDraw;
@property (nonatomic, assign) BOOL manuallyDrawingIntoSuperview;
@end


Expand All @@ -27,7 +26,8 @@ - (void)layoutSublayersOfLayer:(CALayer *)inLayer {
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
if(self.layer.hidden && !self.manuallyDraw) return;
// If a superview is flattening its subviews then those subviews are actually hidden. But we still want to draw if we're compositing the view into its superview.
if(self.layer.hidden && !self.manuallyDrawingIntoSuperview) return;

[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
Expand All @@ -48,9 +48,9 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
}

subview.layer.hidden = YES;
subview.manuallyDraw = YES;
subview.manuallyDrawingIntoSuperview = YES;
[subview.layer drawInContext:cgContext];
subview.manuallyDraw = NO;
subview.manuallyDrawingIntoSuperview = NO;

[NSGraphicsContext restoreGraphicsState];
}
Expand All @@ -74,8 +74,9 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
@synthesize subviews;
@synthesize superview;
@synthesize flattenSubviews;
@synthesize manuallyDraw;
@synthesize manuallyDrawingIntoSuperview;
@synthesize userInteractionEnabled;
@synthesize backgroundColor;

+ (void)initialize {
if(self == [MAUIView class]) {
Expand Down Expand Up @@ -157,16 +158,9 @@ - (void)layoutSubviews {

}

- (void)drawRect:(CGRect)rect {

}

- (NSColor *)backgroundColor {
return [NSColor colorWithCGColor:self.layer.backgroundColor];
}

- (void)setBackgroundColor:(NSColor *)newColor {
self.layer.backgroundColor = newColor.CGColor;
- (void)drawRect:(CGRect)dirtyRect {
[self.backgroundColor set];
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
}

- (CGFloat)alpha {
Expand Down

0 comments on commit fa446bb

Please sign in to comment.