Skip to content
This repository has been archived by the owner on Nov 1, 2017. It is now read-only.

Commit

Permalink
update the layer's scale when the screen changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Apr 9, 2012
1 parent fcb1553 commit 1e6362a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
29 changes: 25 additions & 4 deletions lib/UIKit/TUINSView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@interface TUINSView ()
- (void)windowDidResignKey:(NSNotification *)notification;
- (void)windowDidBecomeKey:(NSNotification *)notification;
- (void)screenDidChange:(NSNotification *)notification;
@end


Expand Down Expand Up @@ -159,7 +160,11 @@ - (void)setNextResponder:(NSResponder *)r
}

- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:self.window];
if(self.window != nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidChangeScreenNotification object:self.window];
}

if(newWindow != nil && rootView.layer.superlayer != [self layer]) {
rootView.layer.frame = self.layer.bounds;
Expand All @@ -179,8 +184,11 @@ - (void)viewDidMoveToWindow

[self.rootView didMoveToWindow];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:self.window];
if(self.window != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidChange:) name:NSWindowDidChangeScreenNotification object:self.window];
}
}

- (void)_updateLayerScaleFactor {
Expand All @@ -191,11 +199,24 @@ - (void)_updateLayerScaleFactor {
}

if([self.layer respondsToSelector:@selector(setContentsScale:)]) {
self.layer.contentsScale = scale;
if(fabs(self.layer.contentsScale - scale) > 0.1f) {
self.layer.contentsScale = scale;
}
}

[self.rootView _updateLayerScaleFactor];
}
}

- (void)screenDidChange:(NSNotification *)notification {
// at the time this is called, the window's backing scale hasn't been updated for the new screen
[self performSelector:@selector(backingScaleMayHaveChanged) withObject:nil afterDelay:0];
}

- (void)backingScaleMayHaveChanged {
[self _updateLayerScaleFactor];
}

- (TUIView *)viewForLocalPoint:(NSPoint)p
{
return [rootView hitTest:p withEvent:nil];
Expand Down
2 changes: 2 additions & 0 deletions lib/UIKit/TUIView+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@

- (TUITextRenderer *)textRendererAtPoint:(CGPoint)point;

- (void)_updateLayerScaleFactor;

@end
18 changes: 18 additions & 0 deletions lib/UIKit/TUIView+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ - (TUITextRenderer *)textRendererAtPoint:(CGPoint)point
return nil;
}

- (void)_updateLayerScaleFactor {
if([self nsWindow] != nil) {
[self.subviews makeObjectsPerformSelector:_cmd];

CGFloat scale = 1.0f;
if([[self nsWindow] respondsToSelector:@selector(backingScaleFactor)]) {
scale = [[self nsWindow] backingScaleFactor];
}

if([self.layer respondsToSelector:@selector(setContentsScale:)]) {
if(fabs(self.layer.contentsScale - scale) > 0.1f) {
self.layer.contentsScale = scale;
[self.layer setNeedsDisplay];
}
}
}
}

@end
16 changes: 3 additions & 13 deletions lib/UIKit/TUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -803,21 +803,11 @@ - (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) {
CGFloat scale = 1.0f;
if([[self nsWindow] respondsToSelector:@selector(backingScaleFactor)]) {
scale = [[self nsWindow] backingScaleFactor];
}

if([self.layer respondsToSelector:@selector(setContentsScale:)]) {
self.layer.contentsScale = scale;
}
}
[self _updateLayerScaleFactor];

for(TUIView *subview in self.subviews) {
[subview didMoveToWindow];
}
[self.subviews makeObjectsPerformSelector:_cmd];

[[NSNotificationCenter defaultCenter] postNotificationName:TUIViewDidMoveToWindowNotification object:self userInfo:self.nsView.window != nil ? [NSDictionary dictionaryWithObject:self.nsView.window forKey:TUIViewWindow] : nil];
}
Expand Down

0 comments on commit 1e6362a

Please sign in to comment.