Applied the previous edge case fix to the single-child layout stuff. Just one more edge case left!

This commit is contained in:
Pietro Gagliardi 2016-05-13 13:08:00 -04:00
parent 26f9d86800
commit 8f55b7e331
2 changed files with 66 additions and 28 deletions

View File

@ -47,6 +47,7 @@ void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *c
1, -margin, 1, -margin,
[desc stringByAppendingString:@" leading constraint"]); [desc stringByAppendingString:@" leading constraint"]);
[contentView addConstraint:c->leadingConstraint]; [contentView addConstraint:c->leadingConstraint];
[c->leadingConstraint retain];
c->topConstraint = mkConstraint(contentView, NSLayoutAttributeTop, c->topConstraint = mkConstraint(contentView, NSLayoutAttributeTop,
NSLayoutRelationEqual, NSLayoutRelationEqual,
@ -54,26 +55,47 @@ void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *c
1, -margin, 1, -margin,
[desc stringByAppendingString:@" top constraint"]); [desc stringByAppendingString:@" top constraint"]);
[contentView addConstraint:c->topConstraint]; [contentView addConstraint:c->topConstraint];
[c->topConstraint retain];
relation = NSLayoutRelationGreaterThanOrEqual; c->trailingConstraintGreater = mkConstraint(contentView, NSLayoutAttributeTrailing,
if (hugsTrailing) NSLayoutRelationGreaterThanOrEqual,
relation = NSLayoutRelationEqual;
c->trailingConstraint = mkConstraint(contentView, NSLayoutAttributeTrailing,
relation,
childView, NSLayoutAttributeTrailing, childView, NSLayoutAttributeTrailing,
1, margin, 1, margin,
[desc stringByAppendingString:@" trailing constraint"]); [desc stringByAppendingString:@" trailing >= constraint"]);
[contentView addConstraint:c->trailingConstraint]; if (hugsTrailing)
[c->trailingConstraintGreater setPriority:NSLayoutPriorityDefaultLow];
[contentView addConstraint:c->trailingConstraintGreater];
[c->trailingConstraintGreater retain];
relation = NSLayoutRelationGreaterThanOrEqual; c->trailingConstraintEqual = mkConstraint(contentView, NSLayoutAttributeTrailing,
if (hugsBottom) NSLayoutRelationEqual,
relation = NSLayoutRelationEqual; childView, NSLayoutAttributeTrailing,
c->bottomConstraint = mkConstraint(contentView, NSLayoutAttributeBottom, 1, margin,
relation, [desc stringByAppendingString:@" trailing == constraint"]);
if (!hugsTrailing)
[c->trailingConstraintEqual setPriority:NSLayoutPriorityDefaultLow];
[contentView addConstraint:c->trailingConstraintEqual];
[c->trailingConstraintEqual retain];
c->bottomConstraintGreater = mkConstraint(contentView, NSLayoutAttributeBottom,
NSLayoutRelationGreaterThanOrEqual,
childView, NSLayoutAttributeBottom, childView, NSLayoutAttributeBottom,
1, margin, 1, margin,
[desc stringByAppendingString:@" bottom constraint"]); [desc stringByAppendingString:@" bottom >= constraint"]);
[contentView addConstraint:c->bottomConstraint]; if (hugsBottom)
[c->bottomConstraintGreater setPriority:NSLayoutPriorityDefaultLow];
[contentView addConstraint:c->bottomConstraintGreater];
[c->bottomConstraintGreater retain];
c->bottomConstraintEqual = mkConstraint(contentView, NSLayoutAttributeBottom,
NSLayoutRelationEqual,
childView, NSLayoutAttributeBottom,
1, margin,
[desc stringByAppendingString:@" bottom == constraint"]);
if (!hugsBottom)
[c->bottomConstraintEqual setPriority:NSLayoutPriorityDefaultLow];
[contentView addConstraint:c->bottomConstraintEqual];
[c->bottomConstraintEqual retain];
} }
void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv) void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv)
@ -88,15 +110,25 @@ void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv)
[c->topConstraint release]; [c->topConstraint release];
c->topConstraint = nil; c->topConstraint = nil;
} }
if (c->trailingConstraint != nil) { if (c->trailingConstraintGreater != nil) {
[cv removeConstraint:c->trailingConstraint]; [cv removeConstraint:c->trailingConstraintGreater];
[c->trailingConstraint release]; [c->trailingConstraintGreater release];
c->trailingConstraint = nil; c->trailingConstraintGreater = nil;
} }
if (c->bottomConstraint != nil) { if (c->trailingConstraintEqual != nil) {
[cv removeConstraint:c->bottomConstraint]; [cv removeConstraint:c->trailingConstraintEqual];
[c->bottomConstraint release]; [c->trailingConstraintEqual release];
c->bottomConstraint = nil; c->trailingConstraintEqual = nil;
}
if (c->bottomConstraintGreater != nil) {
[cv removeConstraint:c->bottomConstraintGreater];
[c->bottomConstraintGreater release];
c->bottomConstraintGreater = nil;
}
if (c->bottomConstraintEqual != nil) {
[cv removeConstraint:c->bottomConstraintEqual];
[c->bottomConstraintEqual release];
c->bottomConstraintEqual = nil;
} }
} }
@ -109,10 +141,14 @@ void singleChildConstraintsSetMargined(struct singleChildConstraints *c, int mar
[c->leadingConstraint setConstant:-margin]; [c->leadingConstraint setConstant:-margin];
if (c->topConstraint != nil) if (c->topConstraint != nil)
[c->topConstraint setConstant:-margin]; [c->topConstraint setConstant:-margin];
if (c->trailingConstraint != nil) if (c->trailingConstraintGreater != nil)
[c->trailingConstraint setConstant:margin]; [c->trailingConstraintGreater setConstant:margin];
if (c->bottomConstraint != nil) if (c->trailingConstraintEqual != nil)
[c->bottomConstraint setConstant:margin]; [c->trailingConstraintEqual setConstant:margin];
if (c->bottomConstraintGreater != nil)
[c->bottomConstraintGreater setConstant:margin];
if (c->bottomConstraintEqual != nil)
[c->bottomConstraintEqual setConstant:margin];
} }
// from http://blog.bjhomer.com/2014/08/nsscrollview-and-autolayout.html because (as pointed out there) Apple's official guide is really only for iOS // from http://blog.bjhomer.com/2014/08/nsscrollview-and-autolayout.html because (as pointed out there) Apple's official guide is really only for iOS

View File

@ -61,8 +61,10 @@ extern void jiggleViewLayout(NSView *view);
struct singleChildConstraints { struct singleChildConstraints {
NSLayoutConstraint *leadingConstraint; NSLayoutConstraint *leadingConstraint;
NSLayoutConstraint *topConstraint; NSLayoutConstraint *topConstraint;
NSLayoutConstraint *trailingConstraint; NSLayoutConstraint *trailingConstraintGreater;
NSLayoutConstraint *bottomConstraint; NSLayoutConstraint *trailingConstraintEqual;
NSLayoutConstraint *bottomConstraintGreater;
NSLayoutConstraint *bottomConstraintEqual;
}; };
extern void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *contentView, NSView *childView, BOOL hugsTrailing, BOOL hugsBottom, int margined, NSString *desc); extern void singleChildConstraintsEstablish(struct singleChildConstraints *c, NSView *contentView, NSView *childView, BOOL hugsTrailing, BOOL hugsBottom, int margined, NSString *desc);
extern void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv); extern void singleChildConstraintsRemove(struct singleChildConstraints *c, NSView *cv);