Started implementing the previous commit's changes in a more permanent way: set REAL hugging priorities and implemented them on uiBox.

This commit is contained in:
Pietro Gagliardi 2016-05-08 10:42:20 -04:00
parent 8867742ec0
commit 07930279c0
3 changed files with 55 additions and 69 deletions

View File

@ -11,8 +11,6 @@
@interface boxChild : NSObject @interface boxChild : NSObject
@property uiControl *c; @property uiControl *c;
@property BOOL stretchy; @property BOOL stretchy;
@property NSLayoutPriority oldHorzHuggingPri;
@property NSLayoutPriority oldVertHuggingPri;
- (NSView *)view; - (NSView *)view;
@end @end
@ -24,7 +22,8 @@
NSLayoutConstraint *first; NSLayoutConstraint *first;
NSMutableArray *inBetweens; NSMutableArray *inBetweens;
NSLayoutConstraint *last, *last2; NSLayoutConstraint *last;
NSLayoutConstraint *lastHugging;
NSMutableArray *otherConstraints; NSMutableArray *otherConstraints;
NSLayoutAttribute primaryStart; NSLayoutAttribute primaryStart;
@ -34,6 +33,9 @@
NSLayoutAttribute primarySize; NSLayoutAttribute primarySize;
NSLayoutConstraintOrientation primaryOrientation; NSLayoutConstraintOrientation primaryOrientation;
NSLayoutConstraintOrientation secondaryOrientation; NSLayoutConstraintOrientation secondaryOrientation;
NSLayoutPriority horzHuggingPri;
NSLayoutPriority vertHuggingPri;
} }
- (id)initWithVertical:(BOOL)vert b:(uiBox *)bb; - (id)initWithVertical:(BOOL)vert b:(uiBox *)bb;
- (void)onDestroy; - (void)onDestroy;
@ -45,6 +47,7 @@
- (void)delete:(uintmax_t)n; - (void)delete:(uintmax_t)n;
- (int)isPadded; - (int)isPadded;
- (void)setPadded:(int)p; - (void)setPadded:(int)p;
- (void)setRealHuggingPriority:(NSLayoutPriority)priority forOrientation:(NSLayoutConstraintOrientation)orientation;
@end @end
struct uiBox { struct uiBox {
@ -91,6 +94,10 @@ struct uiBox {
self->primaryOrientation = NSLayoutConstraintOrientationHorizontal; self->primaryOrientation = NSLayoutConstraintOrientationHorizontal;
self->secondaryOrientation = NSLayoutConstraintOrientationVertical; self->secondaryOrientation = NSLayoutConstraintOrientationVertical;
} }
// TODO required?
self->horzHuggingPri = NSLayoutPriorityDefaultHigh;
self->vertHuggingPri = NSLayoutPriorityDefaultHigh;
} }
return self; return self;
} }
@ -100,11 +107,12 @@ struct uiBox {
boxChild *bc; boxChild *bc;
uintmax_t i, n; uintmax_t i, n;
// TODO if guard these
[self removeOurConstraints]; [self removeOurConstraints];
[self->first release]; [self->first release];
[self->inBetweens release]; [self->inBetweens release];
[self->last release]; [self->last release];
[self->last2 release]; [self->lastHugging release];
[self->otherConstraints release]; [self->otherConstraints release];
n = [self->children count]; n = [self->children count];
@ -119,11 +127,14 @@ struct uiBox {
- (void)removeOurConstraints - (void)removeOurConstraints
{ {
// TODO if guard these
[self removeConstraint:self->first]; [self removeConstraint:self->first];
[self removeConstraints:self->inBetweens]; [self removeConstraints:self->inBetweens];
[self->inBetweens removeAllObjects];
[self removeConstraint:self->last]; [self removeConstraint:self->last];
[self removeConstraint:self->last2]; [self removeConstraint:self->last2];
[self removeConstraints:self->otherConstraints]; [self removeConstraints:self->otherConstraints];
[self->otherConstraints removeAllObjects];
} }
- (void)forAll:(void (^)(uintmax_t i, boxChild *b))closure - (void)forAll:(void (^)(uintmax_t i, boxChild *b))closure
@ -200,7 +211,7 @@ struct uiBox {
prev = next; prev = next;
} }
// and finally end the primary direction // end the primary direction with the <= constraint
self->last = mkConstraint(prev, self->primaryEnd, self->last = mkConstraint(prev, self->primaryEnd,
NSLayoutRelationLessThanOrEqual, NSLayoutRelationLessThanOrEqual,
self, self->primaryEnd, self, self->primaryEnd,
@ -209,29 +220,19 @@ struct uiBox {
[self addConstraint:self->last]; [self addConstraint:self->last];
[self->last retain]; [self->last retain];
// if there is a stretchy control, add the no-stretchy view // end the primary direction with the == hugging constraint, with the appropriate priority
self->last2 = mkConstraint(prev, self->primaryEnd, self->lastHugging = mkConstraint(prev, self->primaryEnd,
NSLayoutRelationEqual, NSLayoutRelationEqual,
self, self->primaryEnd, self, self->primaryEnd,
1, 0, 1, 0,
@"uiBox last2 primary constraint"); @"uiBox last2 primary constraint");
priority = NSLayoutPriorityRequired; priority = NSLayoutPriorityRequired;
if (!hasStretchy) {
BOOL shouldExpand = NO;
uiControl *parent;
parent = uiControlParent(uiControl(self->b));
if (parent != nil)
if (self->vertical) if (self->vertical)
shouldExpand = uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl(parent)); [self->lastHugging setPriority:self->vertHuggingPri];
else else
shouldExpand = uiDarwinControlChildrenShouldAllowSpaceAtTrailingEdge(uiDarwinControl(parent)); [self->lastHugging setPriority:self->horzHuggingPri];
if (shouldExpand) [self addConstraint:self->lastHugging];
priority = NSLayoutPriorityDefaultLow; [self->lastHugging retain];
}
[self->last2 setPriority:priority];
[self addConstraint:self->last2];
[self->last2 retain];
// next: assemble the views in the secondary direction // next: assemble the views in the secondary direction
// each of them will span the secondary direction // each of them will span the secondary direction
@ -242,6 +243,10 @@ struct uiBox {
self, self->secondaryStart, self, self->secondaryStart,
1, 0, 1, 0,
@"uiBox start secondary constraint"); @"uiBox start secondary constraint");
if (self->vertical)
[c setPriority:self->horzHuggingPri];
else
[c setPriority:self->vertHuggingPri];
[self addConstraint:c]; [self addConstraint:c];
[self->otherConstraints addObject:c]; [self->otherConstraints addObject:c];
c = mkConstraint(prev, self->secondaryEnd, c = mkConstraint(prev, self->secondaryEnd,
@ -249,6 +254,10 @@ struct uiBox {
self, self->secondaryEnd, self, self->secondaryEnd,
1, 0, 1, 0,
@"uiBox start secondary constraint"); @"uiBox start secondary constraint");
if (self->vertical)
[c setPriority:self->horzHuggingPri];
else
[c setPriority:self->vertHuggingPri];
[self addConstraint:c]; [self addConstraint:c];
[self->otherConstraints addObject:c]; [self->otherConstraints addObject:c];
} }
@ -274,14 +283,10 @@ struct uiBox {
- (void)append:(uiControl *)c stretchy:(int)stretchy - (void)append:(uiControl *)c stretchy:(int)stretchy
{ {
boxChild *bc; boxChild *bc;
NSView *childView;
bc = [boxChild new]; bc = [boxChild new];
bc.c = c; bc.c = c;
bc.stretchy = stretchy; bc.stretchy = stretchy;
childView = [bc view];
bc.oldHorzHuggingPri = horzHuggingPri(childView);
bc.oldVertHuggingPri = vertHuggingPri(childView);
uiControlSetParent(bc.c, uiControl(self->b)); uiControlSetParent(bc.c, uiControl(self->b));
uiDarwinControlSetSuperview(uiDarwinControl(bc.c), self); uiDarwinControlSetSuperview(uiDarwinControl(bc.c), self);
@ -290,12 +295,12 @@ struct uiBox {
// if a control is stretchy, it should not hug in the primary direction // if a control is stretchy, it should not hug in the primary direction
// otherwise, it should *forcibly* hug // otherwise, it should *forcibly* hug
if (stretchy) if (stretchy)
setHuggingPri(childView, NSLayoutPriorityDefaultLow, self->primaryOrientation); uiDarwinControlSetRealHuggingPriority(uiDarwinControl(bc.c), NSLayoutPriorityDefaultLow, self->primaryOrientation);
else else
// TODO will default high work? // TODO will default high work?
setHuggingPri(childView, NSLayoutPriorityRequired, self->primaryOrientation); uiDarwinControlSetRealHuggingPriority(uiDarwinControl(bc.c), NSLayoutPriorityRequired, self->primaryOrientation);
// make sure controls don't hug their secondary direction so they fill the width of the view // make sure controls don't hug their secondary direction so they fill the width of the view
setHuggingPri(childView, NSLayoutPriorityDefaultLow, self->secondaryOrientation); uiDarwinControlSetRealHuggingPriority(uiDarwinControl(bc.cc), NSLayoutPriorityDefaultLow, self->secondaryOrientation);
[self->children addObject:bc]; [self->children addObject:bc];
[bc release]; // we don't need the initial reference now [bc release]; // we don't need the initial reference now
@ -306,17 +311,12 @@ struct uiBox {
- (void)delete:(uintmax_t)n - (void)delete:(uintmax_t)n
{ {
boxChild *bc; boxChild *bc;
NSView *removedView;
bc = [self child:n]; bc = [self child:n];
removedView = [bc view];
uiControlSetParent(bc.c, NULL); uiControlSetParent(bc.c, NULL);
uiDarwinControlSetSuperview(uiDarwinControl(bc.c), nil); uiDarwinControlSetSuperview(uiDarwinControl(bc.c), nil);
setHorzHuggingPri(removedView, bc.oldHorzHuggingPri);
setVertHuggingPri(removedView, bc.oldVertHuggingPri);
[self->children removeObjectAtIndex:n]; [self->children removeObjectAtIndex:n];
[self setNeedsUpdateConstraints:YES]; [self setNeedsUpdateConstraints:YES];
@ -347,6 +347,15 @@ struct uiBox {
// TODO call anything? // TODO call anything?
} }
- (void)setRealHuggingPriority:(NSLayoutPriority)priority forOrientation:(NSLayoutConstraintOrientation)orientation
{
if (orientation == NSLayoutConstraintOrientationVertical)
self->vertHuggingPri = priority;
else
self->horzHuggingPri = priority;
[self setNeedsUpdateConstraints:YES];
}
@end @end
static void uiBoxDestroy(uiControl *c) static void uiBoxDestroy(uiControl *c)
@ -382,20 +391,11 @@ static void uiBoxSyncEnableState(uiDarwinControl *c, int enabled)
uiDarwinControlDefaultSetSuperview(uiBox, view) uiDarwinControlDefaultSetSuperview(uiBox, view)
static BOOL uiBoxChildrenShouldAllowSpaceAtTrailingEdge(uiDarwinControl *c) static void uiBoxSetRealHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{ {
uiBox *b = uiBox(c); uiBox *b = uiBox(c);
// return NO if this box is horizontal so nested horizontal boxes don't lead to ambiguity [b->view setRealHuggingPriority:priority forOrientation:orientation];
return [b->view isVertical];
}
static BOOL uiBoxChildrenShouldAllowSpaceAtBottom(uiDarwinControl *c)
{
uiBox *b = uiBox(c);
// return NO if this box is vertical so nested vertical boxes don't lead to ambiguity
return ![b->view isVertical];
} }
void uiBoxAppend(uiBox *b, uiControl *c, int stretchy) void uiBoxAppend(uiBox *b, uiControl *c, int stretchy)

View File

@ -11,14 +11,9 @@ void uiDarwinControlSetSuperview(uiDarwinControl *c, NSView *superview)
(*(c->SetSuperview))(c, superview); (*(c->SetSuperview))(c, superview);
} }
BOOL uiDarwinControlChildrenShouldAllowSpaceAtTrailingEdge(uiDarwinControl *c) void uiDarwinControlSetRealHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{ {
return (*(c->ChildrenShouldAllowSpaceAtTrailingEdge))(c); (*(c->SetRealHuggingPriority))(c, priority, orientation);
}
BOOL uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl *c)
{
return (*(c->ChildrenShouldAllowSpaceAtBottom))(c);
} }
void uiDarwinSetControlFont(NSControl *c, NSControlSize size) void uiDarwinSetControlFont(NSControl *c, NSControlSize size)

View File

@ -19,15 +19,13 @@ struct uiDarwinControl {
BOOL visible; BOOL visible;
void (*SyncEnableState)(uiDarwinControl *, int); void (*SyncEnableState)(uiDarwinControl *, int);
void (*SetSuperview)(uiDarwinControl *, NSView *); void (*SetSuperview)(uiDarwinControl *, NSView *);
BOOL (*ChildrenShouldAllowSpaceAtTrailingEdge)(uiDarwinControl *); void (*SetRealHuggingPriority)(uiDarwinControl *, NSLayoutPriority, NSLayoutConstraintOrientation);
BOOL (*ChildrenShouldAllowSpaceAtBottom)(uiDarwinControl *);
}; };
#define uiDarwinControl(this) ((uiDarwinControl *) (this)) #define uiDarwinControl(this) ((uiDarwinControl *) (this))
// TODO document // TODO document
_UI_EXTERN void uiDarwinControlSyncEnableState(uiDarwinControl *, int); _UI_EXTERN void uiDarwinControlSyncEnableState(uiDarwinControl *, int);
_UI_EXTERN void uiDarwinControlSetSuperview(uiDarwinControl *, NSView *); _UI_EXTERN void uiDarwinControlSetSuperview(uiDarwinControl *, NSView *);
_UI_EXTERN BOOL uiDarwinControlChildrenShouldAllowSpaceAtTrailingEdge(uiDarwinControl *); _UI_EXTERN void uiDarwinControlSetRealHuggingPriority(uiDarwinControl *, NSLayoutPriority, NSLayoutConstraintOrientation);
_UI_EXTERN BOOL uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl *);
#define uiDarwinControlDefaultDestroy(type, handlefield) \ #define uiDarwinControlDefaultDestroy(type, handlefield) \
static void type ## Destroy(uiControl *c) \ static void type ## Destroy(uiControl *c) \
@ -108,15 +106,10 @@ _UI_EXTERN BOOL uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl
else \ else \
[superview addSubview:type(c)->handlefield]; \ [superview addSubview:type(c)->handlefield]; \
} }
#define uiDarwinControlDefaultChildrenShouldAllowSpaceAtTrailingEdge(type, handlefield) \ #define uiDarwinControlDefaultSetRealHuggingPriority(type, handlefield) \
static BOOL type ## ChildrenShouldAllowSpaceAtTrailingEdge(uiDarwinControl *c) \ static void type ## SetRealHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation) \
{ \ { \
return NO; /* TODO irrelevant */ \ [type(c)->handlefield setContentHuggingPriority:priority forOrientation:orientation]; \
}
#define uiDarwinControlDefaultChildrenShouldAllowSpaceAtBottom(type, handlefield) \
static BOOL type ## ChildrenShouldAllowSpaceAtBottom(uiDarwinControl *c) \
{ \
return NO; /* TODO irrelevant */ \
} }
#define uiDarwinControlAllDefaultsExceptDestroy(type, handlefield) \ #define uiDarwinControlAllDefaultsExceptDestroy(type, handlefield) \
@ -132,8 +125,7 @@ _UI_EXTERN BOOL uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl
uiDarwinControlDefaultDisable(type, handlefield) \ uiDarwinControlDefaultDisable(type, handlefield) \
uiDarwinControlDefaultSyncEnableState(type, handlefield) \ uiDarwinControlDefaultSyncEnableState(type, handlefield) \
uiDarwinControlDefaultSetSuperview(type, handlefield) \ uiDarwinControlDefaultSetSuperview(type, handlefield) \
uiDarwinControlDefaultChildrenShouldAllowSpaceAtTrailingEdge(type, handlefield) \ uiDarwinControlDefaultSetRealHuggingPriority(type, handlefield)
uiDarwinControlDefaultChildrenShouldAllowSpaceAtBottom(type, handlefield)
#define uiDarwinControlAllDefaults(type, handlefield) \ #define uiDarwinControlAllDefaults(type, handlefield) \
uiDarwinControlDefaultDestroy(type, handlefield) \ uiDarwinControlDefaultDestroy(type, handlefield) \
@ -155,8 +147,7 @@ _UI_EXTERN BOOL uiDarwinControlChildrenShouldAllowSpaceAtBottom(uiDarwinControl
uiControl(var)->Disable = type ## Disable; \ uiControl(var)->Disable = type ## Disable; \
uiDarwinControl(var)->SyncEnableState = type ## SyncEnableState; \ uiDarwinControl(var)->SyncEnableState = type ## SyncEnableState; \
uiDarwinControl(var)->SetSuperview = type ## SetSuperview; \ uiDarwinControl(var)->SetSuperview = type ## SetSuperview; \
uiDarwinControl(var)->ChildrenShouldAllowSpaceAtTrailingEdge = type ## ChildrenShouldAllowSpaceAtTrailingEdge; \ uiDarwinControl(var)->SetRealHuggingPriority = type ## SetRealHuggingPriority; \
uiDarwinControl(var)->ChildrenShouldAllowSpaceAtBottom = type ## ChildrenShouldAllowSpaceAtBottom; \
uiDarwinControl(var)->visible = YES; \ uiDarwinControl(var)->visible = YES; \
uiDarwinControl(var)->enabled = YES; uiDarwinControl(var)->enabled = YES;
// TODO document // TODO document