2015-08-15 16:05:48 -05:00
|
|
|
// 15 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
NSLayoutConstraint *mkConstraint(id view1, NSLayoutAttribute attr1, NSLayoutRelation relation, id view2, NSLayoutAttribute attr2, CGFloat multiplier, CGFloat c, NSString *desc)
|
2015-08-15 16:05:48 -05:00
|
|
|
{
|
2016-04-30 16:14:14 -05:00
|
|
|
NSLayoutConstraint *c;
|
2015-08-15 16:05:48 -05:00
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
c = [NSLayoutConstraint constraintWithItem:view1
|
|
|
|
attribute:attr1
|
|
|
|
relatedBy:relation
|
|
|
|
toItem:view2
|
|
|
|
attribute:attr2
|
|
|
|
multiplier:multiplier
|
|
|
|
constant:c];
|
|
|
|
// apparently only added in 10.9
|
|
|
|
if ([c respondsToSelector:@selector(setIdentifier:)])
|
|
|
|
[((id) c) setIdentifier:desc];
|
|
|
|
return c;
|
2015-08-15 16:05:48 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 16:45:44 -05:00
|
|
|
void setHuggingPri(NSView *view, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
|
|
|
|
{
|
|
|
|
[view setContentHuggingPriority:priority forOrientation:orientation];
|
|
|
|
}
|
|
|
|
|
2015-08-15 22:32:34 -05:00
|
|
|
NSLayoutPriority horzHuggingPri(NSView *view)
|
|
|
|
{
|
|
|
|
return [view contentHuggingPriorityForOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
|
|
}
|
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
void setHorzHuggingPri(NSView *view, NSLayoutPriority priority)
|
|
|
|
{
|
|
|
|
[view setContentHuggingPriority:priority forOrientation:NSLayoutConstraintOrientationHorizontal];
|
|
|
|
}
|
|
|
|
|
2015-08-15 22:32:34 -05:00
|
|
|
NSLayoutPriority vertHuggingPri(NSView *view)
|
|
|
|
{
|
|
|
|
return [view contentHuggingPriorityForOrientation:NSLayoutConstraintOrientationVertical];
|
|
|
|
}
|
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
void setVertHuggingPri(NSView *view, NSLayoutPriority priority)
|
2015-08-15 22:32:34 -05:00
|
|
|
{
|
2016-04-30 16:14:14 -05:00
|
|
|
[view setContentHuggingPriority:priority forOrientation:NSLayoutConstraintOrientationVertical];
|
2015-08-15 22:32:34 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
// precondition: subview is a subview of superview already
|
|
|
|
void layoutSingleView(NSView *superview, NSView *subview, int margined, NSString *desc)
|
2015-08-15 16:05:48 -05:00
|
|
|
{
|
2016-04-30 16:14:14 -05:00
|
|
|
NSLayoutConstraint *constraint;
|
|
|
|
CGFloat margin;
|
2015-08-15 16:05:48 -05:00
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
[superview removeConstraints:[superview constraints]];
|
2015-08-15 16:05:48 -05:00
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
margin = 0;
|
2015-08-15 16:05:48 -05:00
|
|
|
if (margined)
|
2016-04-30 16:14:14 -05:00
|
|
|
margin = 20; // TODO named constant
|
2015-08-15 16:05:48 -05:00
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
constraint = mkConstraint(subview, NSLayoutAttributeLeading,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
superview, NSLayoutAttributeLeading,
|
|
|
|
1, margin,
|
|
|
|
[desc stringByAppendingString:@" single child horizontal leading"]);
|
|
|
|
[superview addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(superview, NSLayoutAttributeTrailing,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
subview, NSLayoutAttributeTrailing,
|
|
|
|
1, margin,
|
|
|
|
[desc stringByAppendingString:@" single child horizontal trailing"]);
|
|
|
|
[superview addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(subview, NSLayoutAttributeTop,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
superview, NSLayoutAttributeTop,
|
|
|
|
1, margin,
|
|
|
|
[desc stringByAppendingString:@" single child top"]);
|
|
|
|
[superview addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(superview, NSLayoutAttributeBottom,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
subview, NSLayoutAttributeBottom,
|
|
|
|
1, margin,
|
|
|
|
[desc stringByAppendingString:@" single child bottom"]);
|
|
|
|
[superview addConstraint:constraint];
|
2015-08-15 16:05:48 -05:00
|
|
|
}
|
2015-08-16 10:43:51 -05:00
|
|
|
|
2016-04-30 16:14:14 -05:00
|
|
|
// via https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html#//apple_ref/doc/uid/TP40010853-CH24-SW1
|
|
|
|
NSMutableArray *layoutScrollViewContents(NSScrollView *sv, BOOL noHScroll, BOOL noVScroll NSString *desc)
|
2015-08-16 10:43:51 -05:00
|
|
|
{
|
2016-04-30 16:14:14 -05:00
|
|
|
NSView *dv;
|
|
|
|
NSLayoutConstraint *constraint;
|
|
|
|
NSMutableArray *array;
|
|
|
|
|
|
|
|
dv = [sv documentView];
|
|
|
|
|
|
|
|
array = [NSMutableArray new];
|
|
|
|
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeLeading,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeLeading,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view horizontal leading"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeTrailing,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeTrailing,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view horizontal trailing"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeTop,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeTop,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view top"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeBottom,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeBottom,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view bottom"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
|
|
|
|
if (noHScroll) {
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeWidth,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeWidth,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view width"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noVScroll) {
|
|
|
|
constraint = mkConstraint(dv, NSLayoutAttributeHeight,
|
|
|
|
NSLayoutRelationEqual,
|
|
|
|
sv, NSLayoutAttributeHeight,
|
|
|
|
1, 0,
|
|
|
|
[desc stringByAppendingString:@" scroll view height"]);
|
|
|
|
[array addObject:constraint];
|
|
|
|
[sv addConstraint:constraint];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
2015-08-16 10:43:51 -05:00
|
|
|
}
|