Skip to content

Commit

Permalink
real HiDPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Apr 6, 2012
1 parent 293d472 commit 0940950
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/UIKit/TUIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ extern TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void));
*/
extern NSData *TUIGraphicsDrawAsPDF(CGRect *optionalMediaBox, void(^draw)(CGContextRef));

extern CGFloat Screen_Scale; // set this at launch for experimental hidpi support
extern BOOL AtLeastLion; // set at launch
1 change: 0 additions & 1 deletion lib/UIKit/TUIKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#import "TUIKit.h"

CGFloat Screen_Scale = 1.0;
BOOL AtLeastLion = NO;

CGContextRef TUIGraphicsGetCurrentContext(void)
Expand Down
10 changes: 7 additions & 3 deletions lib/UIKit/TUINSView.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ - (void)setRootView:(TUIView *)v
CALayer *layer = [self layer];
[layer setDelegate:self];
[layer addSublayer:rootView.layer];
if(Screen_Scale != 1.0) {
layer.anchorPoint = CGPointMake(0, 0);
layer.transform = CATransform3DMakeScale(Screen_Scale, Screen_Scale, Screen_Scale);

if([self window] != nil) {
self.layer.contentsScale = [[self window] backingScaleFactor];
}
}

Expand Down Expand Up @@ -177,6 +177,10 @@ - (void)viewWillMoveToWindow:(NSWindow *)newWindow {

- (void)viewDidMoveToWindow
{
if([self window] != nil) {
self.layer.contentsScale = [[self window] backingScaleFactor];
}

[self.rootView didMoveToWindow];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self.window];
Expand Down
1 change: 1 addition & 0 deletions lib/UIKit/TUIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ extern CGRect(^TUIViewCenteredLayout)(TUIView*);
BOOL lastOpaque;
CGContextRef context;
CGRect dirtyRect;
CGFloat lastContentsScale;
} _context;

struct {
Expand Down
17 changes: 11 additions & 6 deletions lib/UIKit/TUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ - (void)setViewDelegate:(id <TUIViewDelegate>)d

// actionForLayer:forKey: implementetd in TUIView+Animation

extern CGFloat Screen_Scale;

- (BOOL)_disableDrawRect
{
return NO;
Expand All @@ -256,7 +254,8 @@ - (CGContextRef)_CGContext
// kill if we're a different size
if(w != _context.lastWidth ||
h != _context.lastHeight ||
o != _context.lastOpaque)
o != _context.lastOpaque ||
fabs(self.layer.contentsScale - _context.lastContentsScale) > 0.1f)
{
CGContextRelease(_context.context);
_context.context = NULL;
Expand All @@ -268,9 +267,10 @@ - (CGContextRef)_CGContext
_context.lastWidth = w;
_context.lastHeight = h;
_context.lastOpaque = o;
_context.lastContentsScale = self.layer.contentsScale;

b.size.width *= Screen_Scale;
b.size.height *= Screen_Scale;
b.size.width *= self.layer.contentsScale;
b.size.height *= self.layer.contentsScale;
if(b.size.width < 1) b.size.width = 1;
if(b.size.height < 1) b.size.height = 1;
CGContextRef ctx = TUICreateGraphicsContextWithOptions(b.size, o);
Expand Down Expand Up @@ -304,7 +304,7 @@ - (void)displayLayer:(CALayer *)layer
TUIGraphicsPushContext(context); \
if(_viewFlags.clearsContextBeforeDrawing) \
CGContextClearRect(context, b); \
CGContextScaleCTM(context, Screen_Scale, Screen_Scale); \
CGContextScaleCTM(context, self.layer.contentsScale, self.layer.contentsScale); \
CGContextSetAllowsAntialiasing(context, true); \
CGContextSetShouldAntialias(context, true); \
CGContextSetShouldSmoothFonts(context, !_viewFlags.disableSubpixelTextRendering);
Expand All @@ -313,6 +313,7 @@ - (void)displayLayer:(CALayer *)layer
CA_COLOR_OVERLAY_DEBUG \
TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext(); \
layer.contents = (id)image.CGImage; \
CGContextScaleCTM(context, 1.0f / self.layer.contentsScale, 1.0f / self.layer.contentsScale); \
TUIGraphicsPopContext(); \
if(self.drawInBackground) [CATransaction flush];

Expand Down Expand Up @@ -801,6 +802,10 @@ - (void)willMoveToWindow:(TUINSWindow *)newWindow {
[[NSNotificationCenter defaultCenter] postNotificationName:TUIViewWillMoveToWindowNotification object:self userInfo:newWindow != nil ? [NSDictionary dictionaryWithObject:newWindow forKey:TUIViewWindow] : nil];
}
- (void)didMoveToWindow {
if(self.nsWindow != nil) {
self.layer.contentsScale = [self.nsWindow backingScaleFactor];
}

for(TUIView *subview in self.subviews) {
[subview didMoveToWindow];
}
Expand Down

0 comments on commit 0940950

Please sign in to comment.