Merge pull request #171 from BennyKJohnson/core-view-layout

Implement supporting core layout methods on NSView
This commit is contained in:
Fred Kiefer 2023-02-23 09:06:12 +01:00 committed by GitHub
commit 829069af34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 193 additions and 1 deletions

View file

@ -29,8 +29,9 @@
#import <Foundation/NSGeometry.h>
#import <Foundation/NSKeyedArchiver.h>
#import <AppKit/NSLayoutAnchor.h>
#import <AppKit/NSView.h>
@class NSControl, NSView, NSAnimation, NSArray, NSMutableArray, NSDictionary;
@class NSControl, NSAnimation, NSArray, NSMutableArray, NSDictionary;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
@ -166,6 +167,22 @@ APPKIT_EXPORT_CLASS
@end
@interface NSView (NSConstraintBasedLayoutCoreMethods)
- (void) updateConstraints;
- (void) updateConstraintsForSubtreeIfNeeded;
@end
@interface NSView (NSConstraintBasedLayoutInstallingConstraints)
- (void) addConstraint: (NSLayoutConstraint *)constraint;
- (void) addConstraints: (NSArray*)constraints;
@end
#if defined(__cplusplus)
}
#endif

View file

@ -179,6 +179,9 @@ PACKAGE_SCOPE
BOOL _renew_gstate;
BOOL _is_hidden;
BOOL _in_live_resize;
BOOL _needsLayout;
BOOL _needsUpdateConstraints;
BOOL _translatesAutoresizingMaskIntoConstraints;
NSUInteger _autoresizingMask;
NSFocusRingType _focusRingType;
@ -641,6 +644,36 @@ PACKAGE_SCOPE
#endif
#endif
/**
* Layout
*/
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
- (void) layoutSubtreeIfNeeded;
- (void) layout;
#if GS_HAS_DECLARED_PROPERTIES
@property (nonatomic) BOOL needsLayout;
#else
-(BOOL) needsLayout;
-(void) setNeedsLayout: (BOOL)needsLayout;
#endif
#if GS_HAS_DECLARED_PROPERTIES
@property (nonatomic) BOOL needsUpdateConstraints;
#else
- (BOOL) needsUpdateConstraints;
- (void) setNeedsUpdateConstraints: (BOOL)needsUpdateConstraints;
#endif
#if GS_HAS_DECLARED_PROPERTIES
@property BOOL translatesAutoresizingMaskIntoConstraints;
#else
- (BOOL) translatesAutoresizingMaskIntoConstraints;
- (void) setTranslatesAutoresizingMaskIntoConstraints: (BOOL)translatesAutoresizingMaskIntoConstraints;
#endif
#endif
@end

View file

@ -30,8 +30,11 @@
#import "AppKit/NSView.h"
#import "AppKit/NSAnimation.h"
#import "AppKit/NSLayoutConstraint.h"
#import "NSViewPrivate.h"
#import "AppKit/NSWindow.h"
#import "AppKit/NSApplication.h"
#import "NSAutoresizingMaskLayoutConstraint.h"
#import "GSFastEnumeration.h"
#import "GSAutoLayoutVFLParser.h"
static NSMutableArray *activeConstraints = nil;
@ -612,3 +615,53 @@ 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)
- (void) addConstraint: (NSLayoutConstraint *)constraint
{
// FIXME: Implement adding constraint to layout engine
}
- (void) addConstraints: (NSArray*)constraints
{
FOR_IN (NSLayoutConstraint*, constraint, constraints)
[self addConstraint: constraint];
END_FOR_IN (constraints);
}
@end

View file

@ -62,6 +62,7 @@
#import "AppKit/NSFont.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSKeyValueBinding.h"
#import "AppKit/NSLayoutConstraint.h"
#import "AppKit/NSMenu.h"
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSPrintInfo.h"
@ -76,6 +77,7 @@
#import "GNUstepGUI/GSNibLoading.h"
#import "GSToolTips.h"
#import "GSBindingHelpers.h"
#import "GSFastEnumeration.h"
#import "GSGuiPrivate.h"
#import "NSViewPrivate.h"
@ -630,6 +632,9 @@ GSSetDragTypes(NSView* obj, NSArray *types)
//_previousKeyView = 0;
_alphaValue = 1.0;
_needsUpdateConstraints = YES;
_translatesAutoresizingMaskIntoConstraints = YES;
return self;
}
@ -5129,6 +5134,84 @@ static NSView* findByTag(NSView *view, NSInteger aTag, NSUInteger *level)
return;
}
/**
* Layout
*/
- (void) layout
{
// FIXME: Implement asking layout engine for the alignment rect of each subview and set the alignment rect of each sub view
}
- (void) layoutSubtreeIfNeeded
{
[self updateConstraintsForSubtreeIfNeeded];
[self _layoutViewAndSubViews];
}
- (void) _layoutViewAndSubViews
{
if (_needsLayout)
{
[self layout];
_needsLayout = NO;
}
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
[subview _layoutViewAndSubViews];
END_FOR_IN (subviews);
}
- (void) setNeedsLayout: (BOOL) needsLayout
{
if (!needsLayout)
{
return;
}
_needsLayout = needsLayout;
}
- (BOOL) needsLayout
{
return _needsLayout;
}
- (void) setNeedsUpdateConstraints: (BOOL)needsUpdateConstraints
{
// Calling setNeedsUpdateConstraints with NO should not have an effect
if (!needsUpdateConstraints)
{
return;
}
_needsUpdateConstraints = YES;
}
- (BOOL) needsUpdateConstraints
{
return _needsUpdateConstraints;
}
- (void) setTranslatesAutoresizingMaskIntoConstraints: (BOOL)translate
{
_translatesAutoresizingMaskIntoConstraints = translate;
}
- (BOOL) translatesAutoresizingMaskIntoConstraints
{
return _translatesAutoresizingMaskIntoConstraints;
}
@end
@implementation NSView (NSConstraintBasedLayoutCorePrivateMethods)
// This private setter allows the updateConstraints method to toggle needsUpdateConstraints
- (void) _setNeedsUpdateConstraints: (BOOL)needsUpdateConstraints
{
_needsUpdateConstraints = needsUpdateConstraints;
}
@end
@implementation NSView (__NSViewPrivateMethods__)

View file

@ -38,4 +38,10 @@
- (void) _insertSubview: (NSView *)sv atIndex: (NSUInteger)idx;
@end
@interface NSView (NSConstraintBasedLayoutCorePrivateMethods)
- (void) _setNeedsUpdateConstraints: (BOOL)needsUpdateConstraints;
@end
#endif // _GNUstep_H_NSViewPrivate