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

View File

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

View File

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