Skip to content
This repository has been archived by the owner on Apr 25, 2018. It is now read-only.

Commit

Permalink
Renamed methods to not conflict with methods in AppKit. Officially st…
Browse files Browse the repository at this point in the history
…opping active development.
  • Loading branch information
davedelong committed Apr 27, 2011
1 parent 3b68651 commit d65ffa0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
8 changes: 4 additions & 4 deletions CHLayoutManager.h
Expand Up @@ -38,11 +38,11 @@


+ (id) sharedLayoutManager; + (id) sharedLayoutManager;


- (void) removeAllConstraints; - (void) removeAllLayoutConstraints;


- (void) addConstraint:(CHLayoutConstraint *)constraint toView:(NSView *)view; - (void) addLayoutConstraint:(CHLayoutConstraint *)constraint toView:(NSView *)view;
- (void) removeConstraintsFromView:(NSView *)view; - (void) removeLayoutConstraintsFromView:(NSView *)view;
- (NSArray *) constraintsOnView:(NSView *)view; - (NSArray *) layoutConstraintsOnView:(NSView *)view;
- (NSString *) layoutNameForView:(NSView *)view; - (NSString *) layoutNameForView:(NSView *)view;
- (void) setLayoutName:(NSString *)name forView:(NSView *)view; - (void) setLayoutName:(NSString *)name forView:(NSView *)view;


Expand Down
38 changes: 19 additions & 19 deletions CHLayoutManager.m
Expand Up @@ -31,16 +31,16 @@ of this software and associated documentation files (the "Software"), to deal
@interface CHLayoutContainer : NSObject @interface CHLayoutContainer : NSObject
{ {
NSString * layoutName; NSString * layoutName;
NSMutableArray * constraints; NSMutableArray * layoutConstraints;
} }


@property (nonatomic, copy) NSString * layoutName; @property (nonatomic, copy) NSString * layoutName;
@property (readonly) NSMutableArray * constraints; @property (readonly) NSMutableArray * layoutConstraints;


@end @end


@implementation CHLayoutContainer @implementation CHLayoutContainer
@synthesize layoutName, constraints; @synthesize layoutName, layoutConstraints;


+ (id) container { + (id) container {
return [[[self alloc] init] autorelease]; return [[[self alloc] init] autorelease];
Expand All @@ -49,13 +49,13 @@ + (id) container {
- (id) init { - (id) init {
self = [super init]; self = [super init];
if (self) { if (self) {
constraints = [[NSMutableArray alloc] init]; layoutConstraints = [[NSMutableArray alloc] init];
} }
return self; return self;
} }


- (void) dealloc { - (void) dealloc {
[constraints release]; [layoutConstraints release];
[layoutName release]; [layoutName release];
[super dealloc]; [super dealloc];
} }
Expand Down Expand Up @@ -138,15 +138,15 @@ - (id) init {


- (void) dealloc { - (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
[self removeAllConstraints]; [self removeAllLayoutConstraints];


[viewsToProcess release]; [viewsToProcess release];
[processedViews release]; [processedViews release];
[constraints release]; [constraints release];
[super dealloc]; [super dealloc];
} }


- (void) removeAllConstraints { - (void) removeAllLayoutConstraints {
[constraints removeAllObjects]; [constraints removeAllObjects];
} }


Expand All @@ -157,7 +157,7 @@ - (void) processView:(NSView *)aView {
} }
[processedViews addObject:aView]; [processedViews addObject:aView];


NSArray * viewConstraints = [self constraintsOnView:aView]; NSArray * viewConstraints = [self layoutConstraintsOnView:aView];
for (CHLayoutConstraint * constraint in viewConstraints) { for (CHLayoutConstraint * constraint in viewConstraints) {
[constraint applyToTargetView:aView]; [constraint applyToTargetView:aView];
} }
Expand All @@ -175,7 +175,7 @@ - (void) processView:(NSView *)aView {
for (NSView * subview in superSubviews) { for (NSView * subview in superSubviews) {
if (subview == aView) { continue; } if (subview == aView) { continue; }


NSArray * subviewConstraints = [self constraintsOnView:subview]; NSArray * subviewConstraints = [self layoutConstraintsOnView:subview];
for (CHLayoutConstraint * subviewConstraint in subviewConstraints) { for (CHLayoutConstraint * subviewConstraint in subviewConstraints) {
NSView * sourceView = [subview relativeViewForName:[subviewConstraint sourceName]]; NSView * sourceView = [subview relativeViewForName:[subviewConstraint sourceName]];
if (sourceView == aView) { if (sourceView == aView) {
Expand All @@ -188,7 +188,7 @@ - (void) processView:(NSView *)aView {
//subviews constrained to this view //subviews constrained to this view
NSArray * subviews = [aView subviews]; NSArray * subviews = [aView subviews];
for (NSView * subview in subviews) { for (NSView * subview in subviews) {
NSArray * subviewConstraints = [self constraintsOnView:subview]; NSArray * subviewConstraints = [self layoutConstraintsOnView:subview];
for (CHLayoutConstraint * subviewConstraint in subviewConstraints) { for (CHLayoutConstraint * subviewConstraint in subviewConstraints) {
NSView * sourceView = [subview relativeViewForName:[subviewConstraint sourceName]]; NSView * sourceView = [subview relativeViewForName:[subviewConstraint sourceName]];
if (sourceView == aView) { if (sourceView == aView) {
Expand Down Expand Up @@ -265,38 +265,38 @@ and all is (hopefully) well in the world.


- (void) chlayoutautoremove_dynamicDealloc { - (void) chlayoutautoremove_dynamicDealloc {
if ([self isKindOfClass:[NSView class]]) { //to prevent people from being stupid if ([self isKindOfClass:[NSView class]]) { //to prevent people from being stupid
[[CHLayoutManager sharedLayoutManager] removeConstraintsFromView:(NSView *)self]; [[CHLayoutManager sharedLayoutManager] removeLayoutConstraintsFromView:(NSView *)self];
[[CHLayoutManager sharedLayoutManager] setLayoutName:nil forView:(NSView *)self]; [[CHLayoutManager sharedLayoutManager] setLayoutName:nil forView:(NSView *)self];
//THIS IS NOT A RECURSIVE CALL //THIS IS NOT A RECURSIVE CALL
//see the big comment above for why //see the big comment above for why
[self chlayoutautoremove_dynamicDealloc]; [self chlayoutautoremove_dynamicDealloc];
} }
} }


- (void) addConstraint:(CHLayoutConstraint *)constraint toView:(NSView *)view { - (void) addLayoutConstraint:(CHLayoutConstraint *)constraint toView:(NSView *)view {
CHLayoutContainer * viewContainer = [constraints objectForKey:view]; CHLayoutContainer * viewContainer = [constraints objectForKey:view];
if (viewContainer == nil) { if (viewContainer == nil) {
viewContainer = [CHLayoutContainer container]; viewContainer = [CHLayoutContainer container];
[constraints setObject:viewContainer forKey:view]; [constraints setObject:viewContainer forKey:view];
} }


[[viewContainer constraints] addObject:constraint]; [[viewContainer layoutConstraints] addObject:constraint];
[self beginProcessingView:view]; [self beginProcessingView:view];
} }


- (void) removeConstraintsFromView:(NSView *)view { - (void) removeLayoutConstraintsFromView:(NSView *)view {
CHLayoutContainer * viewContainer = [constraints objectForKey:view]; CHLayoutContainer * viewContainer = [constraints objectForKey:view];
[[viewContainer constraints] removeAllObjects]; [[viewContainer layoutConstraints] removeAllObjects];


if ([[viewContainer constraints] count] == 0 && [viewContainer layoutName] == nil) { if ([[viewContainer layoutConstraints] count] == 0 && [viewContainer layoutName] == nil) {
[constraints removeObjectForKey:view]; [constraints removeObjectForKey:view];
} }
} }


- (NSArray *) constraintsOnView:(NSView *)view { - (NSArray *) layoutConstraintsOnView:(NSView *)view {
CHLayoutContainer * container = [constraints objectForKey:view]; CHLayoutContainer * container = [constraints objectForKey:view];
if (container == nil) { return [NSArray array]; } if (container == nil) { return [NSArray array]; }
return [[[container constraints] copy] autorelease]; return [[[container layoutConstraints] copy] autorelease];
} }


- (NSString *) layoutNameForView:(NSView *)view { - (NSString *) layoutNameForView:(NSView *)view {
Expand All @@ -307,7 +307,7 @@ - (NSString *) layoutNameForView:(NSView *)view {
- (void) setLayoutName:(NSString *)name forView:(NSView *)view { - (void) setLayoutName:(NSString *)name forView:(NSView *)view {
CHLayoutContainer * viewContainer = [constraints objectForKey:view]; CHLayoutContainer * viewContainer = [constraints objectForKey:view];


if (name == nil && [[viewContainer constraints] count] == 0) { if (name == nil && [[viewContainer layoutConstraints] count] == 0) {
[constraints removeObjectForKey:view]; [constraints removeObjectForKey:view];
} else { } else {
if (viewContainer == nil) { if (viewContainer == nil) {
Expand Down
22 changes: 11 additions & 11 deletions CHLayoutManagerAppDelegate.m
Expand Up @@ -36,36 +36,36 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application // Insert code here to initialize your application


[button1 setLayoutName:@"button1"]; [button1 setLayoutName:@"button1"];
[button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxX]]; [button2 addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxX]];
[button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxY]]; [button2 addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxY]];
[button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeWidth relativeTo:@"button1" attribute:CHLayoutConstraintAttributeWidth]]; [button2 addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeWidth relativeTo:@"button1" attribute:CHLayoutConstraintAttributeWidth]];


[progress startAnimation:nil]; [progress startAnimation:nil];
CHLayoutConstraint * centerHorizontal = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidX]; CHLayoutConstraint * centerHorizontal = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidX];
CHLayoutConstraint * centerVertical = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidY]; CHLayoutConstraint * centerVertical = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidY];
[progress addConstraint:centerHorizontal]; [progress addLayoutConstraint:centerHorizontal];
[progress addConstraint:centerVertical]; [progress addLayoutConstraint:centerVertical];


[progress addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidXMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeBoundsCenter]]; [progress addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidXMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeBoundsCenter]];




[leftVerticalButton setLayoutName:@"leftVertical"]; [leftVerticalButton setLayoutName:@"leftVertical"];
[leftVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMinX offset:37]]; [leftVerticalButton addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMinX offset:37]];
[rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMaxX offset:-13]]; [rightVerticalButton addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMaxX offset:-13]];
[rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY]]; [rightVerticalButton addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY]];


#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE


CHLayoutTransformer transformer = ^(CGFloat source) { CHLayoutTransformer transformer = ^(CGFloat source) {
CGFloat superViewHeight = [[rightVerticalButton superview] frame].size.height; CGFloat superViewHeight = [[rightVerticalButton superview] frame].size.height;
return (superViewHeight - source); return (superViewHeight - source);
}; };
[rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY blockTransformer:transformer]]; [rightVerticalButton addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY blockTransformer:transformer]];


#endif #endif


// [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY scale:2.0 offset:0.0]]; // [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY scale:2.0 offset:0.0]];
[helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidXMidY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeBoundsCenter]]; [helpButton addLayoutConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidXMidY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeBoundsCenter]];


SinTransformer * sinTransformer = [[SinTransformer alloc] init]; SinTransformer * sinTransformer = [[SinTransformer alloc] init];
// [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMinY valueTransformer:sinTransformer]]; // [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMinY valueTransformer:sinTransformer]];
Expand Down
6 changes: 3 additions & 3 deletions NSView+CHLayout.h
Expand Up @@ -31,9 +31,9 @@
- (void) setLayoutName:(NSString *)newLayoutName; - (void) setLayoutName:(NSString *)newLayoutName;
- (NSString *) layoutName; - (NSString *) layoutName;


- (void) addConstraint:(CHLayoutConstraint *)constraint; - (void) addLayoutConstraint:(CHLayoutConstraint *)constraint;
- (NSArray *) constraints; - (NSArray *) layoutConstraints;
- (void) removeAllConstraints; - (void) removeAllLayoutConstraints;


- (NSRect) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute; - (NSRect) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute;
- (void) setValue:(NSRect)newValue forLayoutAttribute:(CHLayoutConstraintAttribute)attribute; - (void) setValue:(NSRect)newValue forLayoutAttribute:(CHLayoutConstraintAttribute)attribute;
Expand Down
12 changes: 6 additions & 6 deletions NSView+CHLayout.m
Expand Up @@ -49,16 +49,16 @@ - (NSString *) layoutName {
return [[CHLayoutManager sharedLayoutManager] layoutNameForView:self]; return [[CHLayoutManager sharedLayoutManager] layoutNameForView:self];
} }


- (void) addConstraint:(CHLayoutConstraint *)constraint { - (void) addLayoutConstraint:(CHLayoutConstraint *)constraint {
[[CHLayoutManager sharedLayoutManager] addConstraint:constraint toView:self]; [[CHLayoutManager sharedLayoutManager] addLayoutConstraint:constraint toView:self];
} }


- (NSArray *) constraints { - (NSArray *) layoutConstraints {
return [[CHLayoutManager sharedLayoutManager] constraintsOnView:self]; return [[CHLayoutManager sharedLayoutManager] layoutConstraintsOnView:self];
} }


- (void) removeAllConstraints { - (void) removeAllLayoutConstraints {
[[CHLayoutManager sharedLayoutManager] removeConstraintsFromView:self]; [[CHLayoutManager sharedLayoutManager] removeLayoutConstraintsFromView:self];
} }


- (NSRect) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute { - (NSRect) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute {
Expand Down
8 changes: 6 additions & 2 deletions README.markdown
@@ -1,3 +1,5 @@
**This project is no longer considered under active development, and has been deprecated in favor of built-in API. Please see the "Supported Platforms" section for more information.**

#CHLayoutManager #CHLayoutManager


CHLayoutManager is a way to add positioning and sizing constraints on views. The easiest way to understand this is with an example: CHLayoutManager is a way to add positioning and sizing constraints on views. The easiest way to understand this is with an example:
Expand Down Expand Up @@ -35,7 +37,9 @@ Then `#import "CHLayout.h"` in any .m file that needs to apply constraints to vi


##Supported Platforms ##Supported Platforms


- Mac OS X 10.5+ - Mac OS X 10.5 - 10.6.

`CHLayoutManager` will not be supported beyond 10.6. `CHLayoutManager` will work on 10.7, but it is recommended that you use the layout system in 10.7 as opposed to this. While they are compatible, the mechanism in 10.7 is far more flexible.


##Special Considerations ##Special Considerations


Expand All @@ -51,7 +55,7 @@ Then `#import "CHLayout.h"` in any .m file that needs to apply constraints to vi


CHLayoutManager is licensed under the MIT license, which is reproduced in its entirety here: CHLayoutManager is licensed under the MIT license, which is reproduced in its entirety here:


>Copyright (c) 2010 Dave DeLong >Copyright (c) 2011 Dave DeLong
> >
>Permission is hereby granted, free of charge, to any person obtaining a copy >Permission is hereby granted, free of charge, to any person obtaining a copy
>of this software and associated documentation files (the "Software"), to deal >of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit d65ffa0

Please sign in to comment.