From 0fe5214c09ead8b21dd1a29ce375f7f6041b3f6a Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 7 Jun 2016 18:57:02 -0400 Subject: [PATCH] Revert "Undid all of that. There's an easier way..." ...that only works for nibs. :( This reverts commit 0fa706b8d4773498a5d3ab46f04284f2d1ecb8a1. --- darwin/form.m | 90 +++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 68 deletions(-) diff --git a/darwin/form.m b/darwin/form.m index fdd8e79d..0e3c4b86 100644 --- a/darwin/form.m +++ b/darwin/form.m @@ -20,10 +20,7 @@ NSLayoutConstraint *first; NSMutableArray *inBetweens; NSLayoutConstraint *last; - NSMutableArray *widths; - NSMutableArray *leadings; - NSMutableArray *middles; - NSMutableArray *trailings; + NSMutableArray *hEdges; } - (id)initWithF:(uiForm *)ff; - (void)onDestroy; @@ -65,10 +62,7 @@ struct uiForm { self->nStretchy = 0; self->inBetweens = [NSMutableArray new]; - self->widths = [NSMutableArray new]; - self->leadings = [NSMutableArray new]; - self->middles = [NSMutableArray new]; - self->trailings = [NSMutableArray new]; + self->hEdges = [NSMutableArray new]; } return self; } @@ -79,10 +73,7 @@ struct uiForm { [self removeOurConstraints]; [self->inBetweens release]; - [self->widths release]; - [self->leadings release]; - [self->middles release]; - [self->trailings release]; + [self->hEdges release]; for (fc in self->children) { [self removeConstraint:fc.baseline]; @@ -112,21 +103,9 @@ struct uiForm { [self->last release]; self->last = nil; } - if ([self->widths count] != 0) { - [self removeConstraints:self->widths]; - [self->widths removeAllObjects]; - } - if ([self->leadings count] != 0) { - [self removeConstraints:self->leadings]; - [self->leadings removeAllObjects]; - } - if ([self->middles count] != 0) { - [self removeConstraints:self->middles]; - [self->middles removeAllObjects]; - } - if ([self->trailings count] != 0) { - [self removeConstraints:self->trailings]; - [self->trailings removeAllObjects]; + if ([self->hEdges count] != 0) { + [self removeConstraints:self->hEdges]; + [self->hEdges removeAllObjects]; } } @@ -158,81 +137,55 @@ struct uiForm { return; padding = [self paddingAmount]; - // first arrange the children vertically and make them the same width + // first arrange the main controls vertically prev = nil; for (fc in self->children) { - if (prev == nil) { // first view + if (prev == nil) { // first control; tie to top self->first = mkConstraint(self, NSLayoutAttributeTop, NSLayoutRelationEqual, [fc view], NSLayoutAttributeTop, 1, 0, - @"uiForm first vertical constraint"); + @"uiForm first child top constraint"); [self addConstraint:self->first]; [self->first retain]; prev = [fc view]; - prevlabel = fc.label; continue; } - // not the first; link it + // not first; tie to previous c = mkConstraint(prev, NSLayoutAttributeBottom, NSLayoutRelationEqual, [fc view], NSLayoutAttributeTop, 1, -padding, - @"uiForm in-between vertical constraint"); + @"uiForm middle vertical constraint"); [self addConstraint:c]; [self->inBetweens addObject:c]; - // and make the same width - c = mkConstraint(prev, NSLayoutAttributeWidth, - NSLayoutRelationEqual, - [fc view], NSLayoutAttributeWidth, - 1, 0, - @"uiForm control width constraint"); - [self addConstraint:c]; - [self->widths addObject:c]; - c = mkConstraint(prevlabel, NSLayoutAttributeWidth, - NSLayoutRelationEqual, - fc.label, NSLayoutAttributeWidth, - 1, 0, - @"uiForm label lwidth constraint"); - [self addConstraint:c]; - [self->widths addObject:c]; prev = [fc view]; - prevlabel = fc.label; } - relation = NSLayoutRelationEqual; - if (self->nStretchy != 0) - relation = NSLayoutRelationLessThanOrEqual; + // and the last one self->last = mkConstraint(prev, NSLayoutAttributeBottom, NSLayoutRelationEqual, self, NSLayoutAttributeBottom, 1, 0, - @"uiForm last vertical constraint"); + @"uiForm last child bottom constraint"); [self addConstraint:self->last]; [self->last retain]; - // now arrange the controls horizontally + // now tiethe labels to the left (weakly, for right-alignment) and tie the controls to the right (strongly) for (fc in self->children) { c = mkConstraint(self, NSLayoutAttributeLeading, - NSLayoutRelationEqual, + NSLayoutRelationLessThanOrEqual, fc.label, NSLayoutAttributeLeading, 1, 0, - @"uiForm leading constraint"); + @"uiForm label leading edge constraint"); [self addConstraint:c]; - [self->leadings addObject:c]; - c = mkConstraint(fc.label, NSLayoutAttributeTrailing, - NSLayoutRelationEqual, - [fc view], NSLayoutAttributeLeading, - 1, -padding, - @"uiForm middle constraint"); - [self addConstraint:c]; - [self->middles addObject:c]; + [self->hEdges addObject:c]; c = mkConstraint([fc view], NSLayoutAttributeTrailing, NSLayoutRelationEqual, self, NSLayoutAttributeTrailing, 1, 0, - @"uiForm trailing constraint"); + @"uiForm child trailing edge constraint"); [self addConstraint:c]; - [self->trailings addObject:c]; + [self->hEdges addObject:c]; } // we don't arrange the labels vertically; that's done when we add the control since those constraints don't need to change (they just need to be at their baseline) @@ -249,6 +202,9 @@ struct uiForm { fc.c = c; fc.label = newLabel(label); [fc.label setTranslatesAutoresizingMaskIntoConstraints:NO]; + // and make the label no larger than it needs to be + [fc.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal]; + [fc.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical]; [self addSubview:fc.label]; fc.stretchy = stretchy; fc.oldHorzHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(fc.c), NSLayoutConstraintOrientationHorizontal); @@ -311,8 +267,6 @@ struct uiForm { padding = [self paddingAmount]; for (c in self->inBetweens) [c setConstant:-padding]; - for (c in self->middles) - [c setConstant:-padding]; } - (BOOL)hugsTrailing