Skip to content

Commit

Permalink
finished moving over the last bits of the original View Controller cl…
Browse files Browse the repository at this point in the history
…ass involving layout
  • Loading branch information
Machx committed Oct 31, 2012
1 parent 5ab1a05 commit 60f855c
Showing 1 changed file with 62 additions and 17 deletions.
79 changes: 62 additions & 17 deletions Rebel/NSView+RBLViewControllerAdditions.m
Expand Up @@ -12,18 +12,23 @@
#import <objc/runtime.h>

void *kRBLViewControllerKey = &kRBLViewControllerKey;
void *KRBLViewNeedsLayoutKey = &KRBLViewNeedsLayoutKey;

@implementation NSView (NSView_RBLViewControllerAdditions)

+ (void)initialize {
if(self == [NSView class]) {
[self loadSupportForLayoutSubviews];
}
}

#pragma mark - ViewController

-(id)viewController
{
-(id)viewController {
return objc_getAssociatedObject(self, kRBLViewControllerKey);
}

-(void)setViewController:(id)newViewController
{
-(void)setViewController:(id)newViewController {
if (self.viewController) {
NSResponder *controllerNextResponder = [self.viewController nextResponder];
[self setNextResponder:controllerNextResponder];
Expand All @@ -39,10 +44,55 @@ -(void)setViewController:(id)newViewController
}
}

#pragma mark - Custom Methods
#pragma mark - Layout Subviews

+ (void)loadSupportForLayoutSubviews {
[self swapMethod:@selector(setBounds:) with:@selector(custom_setBounds:)];
[self swapMethod:@selector(setFrame:) with:@selector(custom_setFrame:)];
[self swapMethod:@selector(viewWillDraw) with:@selector(custom_viewWillDraw)];
}

- (void)custom_setBounds:(NSRect)newBounds {
[self custom_setBounds:newBounds];

[self setNeedsLayout];
}

- (void)custom_setFrame:(NSRect)newFrame {
[self custom_setFrame:newFrame];

[self setNeedsLayout];
}

- (void)custom_viewWillDraw {
[self layoutIfNeeded];

[self custom_viewWillDraw];
}

- (void)layoutIfNeeded {
if([self needsLayout]) {
[self layoutSubviews];
}
}

- (void)layoutSubviews {
objc_setAssociatedObject(self, KRBLViewNeedsLayoutKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setNeedsLayout {
objc_setAssociatedObject(self, KRBLViewNeedsLayoutKey, [NSNull null], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

[self setNeedsDisplay:YES];
}

- (BOOL)needsLayout {
return objc_getAssociatedObject(self, KRBLViewNeedsLayoutKey) != nil;
}

#pragma mark - View Methods

+(void)loadSupportForRBLViewControllers
{
+(void)loadSupportForRBLViewControllers {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//swizzle swizzle...
Expand All @@ -56,8 +106,7 @@ +(void)loadSupportForRBLViewControllers
});
}

- (void)custom_viewWillMoveToSuperview:(NSView *)newSuperview
{
- (void)custom_viewWillMoveToSuperview:(NSView *)newSuperview {
[self custom_viewWillMoveToSuperview:newSuperview];

if ([self.viewController isKindOfClass:[RBLViewController class]]) {
Expand All @@ -77,8 +126,7 @@ - (void)custom_viewWillMoveToSuperview:(NSView *)newSuperview
}
}

- (void)custom_viewDidMoveToSuperview
{
- (void)custom_viewDidMoveToSuperview {
[self custom_viewDidMoveToSuperview];

if ([self.viewController isKindOfClass:[RBLViewController class]]) {
Expand All @@ -98,8 +146,7 @@ - (void)custom_viewDidMoveToSuperview
}
}

- (void)custom_viewWillMoveToWindow:(NSWindow *)newWindow
{
- (void)custom_viewWillMoveToWindow:(NSWindow *)newWindow {
[self custom_viewWillMoveToWindow:newWindow];

if ([self.viewController isKindOfClass:[RBLViewController class]]) {
Expand All @@ -119,8 +166,7 @@ - (void)custom_viewWillMoveToWindow:(NSWindow *)newWindow
}
}

- (void)custom_viewDidMoveToWindow
{
- (void)custom_viewDidMoveToWindow {
[self custom_viewDidMoveToWindow];

if ([self.viewController isKindOfClass:[RBLViewController class]]) {
Expand All @@ -140,8 +186,7 @@ - (void)custom_viewDidMoveToWindow
}
}

- (void)custom_setNextResponder:(NSResponder *)newNextResponder
{
- (void)custom_setNextResponder:(NSResponder *)newNextResponder {
if (self.viewController != nil) {
[self.viewController setNextResponder:newNextResponder];
return;
Expand Down

0 comments on commit 60f855c

Please sign in to comment.