Fixed uiForm on OS X.

This commit is contained in:
Pietro Gagliardi 2016-06-08 16:26:23 -04:00
parent d232342779
commit 69cad5e608
1 changed files with 36 additions and 7 deletions

View File

@ -1,6 +1,8 @@
// 7 june 2016 // 7 june 2016
#import "uipriv_darwin.h" #import "uipriv_darwin.h"
// TODO in the test program, sometimes one of the radio buttons can disappear (try when spaced)
@interface formChild : NSView @interface formChild : NSView
@property uiControl *c; @property uiControl *c;
@property (strong) NSTextField *label; @property (strong) NSTextField *label;
@ -8,8 +10,9 @@
@property NSLayoutPriority oldHorzHuggingPri; @property NSLayoutPriority oldHorzHuggingPri;
@property NSLayoutPriority oldVertHuggingPri; @property NSLayoutPriority oldVertHuggingPri;
@property (strong) NSLayoutConstraint *baseline; @property (strong) NSLayoutConstraint *baseline;
@property (strong) NSLayoutConstraint *trailing; @property (strong) NSLayoutConstraint *leading;
@property (strong) NSLayoutConstraint *top; @property (strong) NSLayoutConstraint *top;
@property (strong) NSLayoutConstraint *trailing;
@property (strong) NSLayoutConstraint *bottom; @property (strong) NSLayoutConstraint *bottom;
- (id)initWithLabel:(NSTextField *)l; - (id)initWithLabel:(NSTextField *)l;
- (void)onDestroy; - (void)onDestroy;
@ -59,20 +62,28 @@ struct uiForm {
[self.label setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.label setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal]; [self.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal];
[self.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical]; [self.label setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical];
[self.label setContentCompressionResistancePriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal];
[self.label setContentCompressionResistancePriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical];
[self addSubview:self.label]; [self addSubview:self.label];
self.trailing = mkConstraint(self.label, NSLayoutAttributeTrailing, self.leading = mkConstraint(self.label, NSLayoutAttributeLeading,
NSLayoutRelationEqual, NSLayoutRelationGreaterThanOrEqual,
self, NSLayoutAttributeTrailing, self, NSLayoutAttributeLeading,
1, 0, 1, 0,
@"uiForm label trailing"); @"uiForm label leading");
[self addConstraint:self.trailing]; [self addConstraint:self.leading];
self.top = mkConstraint(self.label, NSLayoutAttributeTop, self.top = mkConstraint(self.label, NSLayoutAttributeTop,
NSLayoutRelationEqual, NSLayoutRelationEqual,
self, NSLayoutAttributeTop, self, NSLayoutAttributeTop,
1, 0, 1, 0,
@"uiForm label top"); @"uiForm label top");
[self addConstraint:self.top]; [self addConstraint:self.top];
self.trailing = mkConstraint(self.label, NSLayoutAttributeTrailing,
NSLayoutRelationEqual,
self, NSLayoutAttributeTrailing,
1, 0,
@"uiForm label trailing");
[self addConstraint:self.trailing];
self.bottom = mkConstraint(self.label, NSLayoutAttributeBottom, self.bottom = mkConstraint(self.label, NSLayoutAttributeBottom,
NSLayoutRelationEqual, NSLayoutRelationEqual,
self, NSLayoutAttributeBottom, self, NSLayoutAttributeBottom,
@ -253,7 +264,7 @@ struct uiForm {
if (self->nStretchy != 0) if (self->nStretchy != 0)
relation = NSLayoutRelationLessThanOrEqual; relation = NSLayoutRelationLessThanOrEqual;
self->last = mkConstraint(prev, NSLayoutAttributeBottom, self->last = mkConstraint(prev, NSLayoutAttributeBottom,
NSLayoutRelationEqual, relation,
self, NSLayoutAttributeBottom, self, NSLayoutAttributeBottom,
1, 0, 1, 0,
@"uiForm last vertical constraint"); @"uiForm last vertical constraint");
@ -269,6 +280,16 @@ struct uiForm {
@"uiForm leading constraint"); @"uiForm leading constraint");
[self addConstraint:c]; [self addConstraint:c];
[self->leadings addObject:c]; [self->leadings addObject:c];
// coerce the control to be as wide as possible
// see http://stackoverflow.com/questions/37710892/in-auto-layout-i-set-up-labels-that-shouldnt-grow-horizontally-and-controls-th
c = mkConstraint(self, NSLayoutAttributeLeading,
NSLayoutRelationEqual,
[fc view], NSLayoutAttributeLeading,
1, 0,
@"uiForm leading constraint");
[c setPriority:NSLayoutPriorityDefaultHigh];
[self addConstraint:c];
[self->leadings addObject:c];
c = mkConstraint(fc, NSLayoutAttributeTrailing, c = mkConstraint(fc, NSLayoutAttributeTrailing,
NSLayoutRelationEqual, NSLayoutRelationEqual,
[fc view], NSLayoutAttributeLeading, [fc view], NSLayoutAttributeLeading,
@ -283,6 +304,14 @@ struct uiForm {
@"uiForm trailing constraint"); @"uiForm trailing constraint");
[self addConstraint:c]; [self addConstraint:c];
[self->trailings addObject:c]; [self->trailings addObject:c];
// TODO
c = mkConstraint(fc, NSLayoutAttributeBottom,
NSLayoutRelationLessThanOrEqual,
self, NSLayoutAttributeBottom,
1, 0,
@"TODO");
[self addConstraint:c];
[self->trailings 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) // 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)