Implement layout constraint conversion to internal solver constraints

This commit is contained in:
Benjamin Johnson 2023-05-27 10:58:27 +10:00
parent fccf8771e6
commit 2e98bd5366
9 changed files with 1124 additions and 146 deletions

View file

@ -618,109 +618,6 @@ static NSMutableArray *activeConstraints = nil;
@end
@implementation NSView (NSConstraintBasedLayoutCoreMethods)
- (void) updateConstraintsForSubtreeIfNeeded
{
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
[subview updateConstraintsForSubtreeIfNeeded];
END_FOR_IN (subviews);
if ([self needsUpdateConstraints])
{
[self updateConstraints];
}
}
- (void) updateConstraints
{
if ([self translatesAutoresizingMaskIntoConstraints] &&
[self superview] != nil)
{
NSArray *autoresizingConstraints = [NSAutoresizingMaskLayoutConstraint
constraintsWithAutoresizingMask: [self autoresizingMask]
subitem: self
frame: [self frame]
superitem: [self superview]
bounds: [[self superview] bounds]];
[self addConstraints: autoresizingConstraints];
}
[self _setNeedsUpdateConstraints: NO];
}
@end
@implementation NSView (NSConstraintBasedLayoutInstallingConstraints)
- (GSAutoLayoutEngine*) _getOrCreateLayoutEngine
{
if (![self window])
{
return nil;
}
if (![[self window] _layoutEngine])
{
[[self window] _bootstrapAutoLayout];
}
return [[self window] _layoutEngine];
}
- (void) addConstraint: (NSLayoutConstraint *)constraint
{
if (![self _getOrCreateLayoutEngine])
{
return;
}
[[self _layoutEngine] addConstraint: constraint];
}
- (void) addConstraints: (NSArray*)constraints
{
if (![self _getOrCreateLayoutEngine])
{
return;
}
[[self _layoutEngine] addConstraints: constraints];
}
- (void) removeConstraint: (NSLayoutConstraint *)constraint
{
if (![self _layoutEngine])
{
return;
}
[[self _layoutEngine] removeConstraint: constraint];
}
- (void) removeConstraints: (NSArray *)constraints
{
if (![self _layoutEngine])
{
return;
}
[[self _layoutEngine] removeConstraints: constraints];
}
- (NSArray*) constraints
{
GSAutoLayoutEngine *engine = [self _layoutEngine];
if (!engine)
{
return [NSArray array];
}
return [engine constraintsForView: self];
}
@end
@implementation NSWindow (NSConstraintBasedLayoutCoreMethods)
- (void) layoutIfNeeded