Move layoutEngine ownership to NSWindow

This commit is contained in:
Benjamin Johnson 2023-02-27 19:56:37 +11:00
parent 93769f3a52
commit 2a1c0f88b3
7 changed files with 81 additions and 40 deletions

View file

@ -187,7 +187,6 @@ PACKAGE_SCOPE
NSUInteger _autoresizingMask;
NSFocusRingType _focusRingType;
NSRect _autoresizingFrameError;
GSAutoLayoutEngine *_layoutEngine;
}
/*

View file

@ -65,6 +65,7 @@
@class NSViewController;
@class GSWindowDecorationView;
@class GSAutoLayoutEngine;
/*
* Window levels are taken from MacOS-X
@ -240,6 +241,7 @@ APPKIT_EXPORT_CLASS
id _firstResponder;
id _futureFirstResponder;
NSView *_initialFirstResponder;
GSAutoLayoutEngine *_layoutEngine;
PACKAGE_SCOPE
id _delegate;
@protected

View file

@ -31,6 +31,7 @@
#import "AppKit/NSAnimation.h"
#import "AppKit/NSLayoutConstraint.h"
#import "NSViewPrivate.h"
#import "NSWindowPrivate.h"
#import "AppKit/NSWindow.h"
#import "AppKit/NSApplication.h"
#import "NSAutoresizingMaskLayoutConstraint.h"
@ -653,23 +654,25 @@ static NSMutableArray *activeConstraints = nil;
@implementation NSView (NSConstraintBasedLayoutInstallingConstraints)
- (void) _bootstrapAutoLayout
- (GSAutoLayoutEngine*) _getOrCreateLayoutEngine
{
if ([self window])
if (![self window])
{
return nil;
}
if (![[self window] _layoutEngine])
{
[[self window] _bootstrapAutoLayout];
}
else
{
[self _initializeLayoutEngine];
}
}
return [[self window] _layoutEngine];
}
- (void) addConstraint: (NSLayoutConstraint *)constraint
{
if (![self _layoutEngine])
if (![self _getOrCreateLayoutEngine])
{
[self _bootstrapAutoLayout];
return;
}
[[self _layoutEngine] addConstraint: constraint];
@ -677,9 +680,9 @@ static NSMutableArray *activeConstraints = nil;
- (void) addConstraints: (NSArray*)constraints
{
if (![self _layoutEngine])
if (![self _getOrCreateLayoutEngine])
{
[self _bootstrapAutoLayout];
return;
}
[[self _layoutEngine] addConstraints: constraints];
@ -716,11 +719,6 @@ static NSMutableArray *activeConstraints = nil;
return [engine constraintsForView: self];
}
- (void) _initializeLayoutEngine
{
[self _setLayoutEngine: [[GSAutoLayoutEngine alloc] init]];
}
@end
@implementation NSWindow (NSConstraintBasedLayoutCoreMethods)
@ -738,7 +736,7 @@ static NSMutableArray *activeConstraints = nil;
- (void) _bootstrapAutoLayout
{
[[[self contentView] superview] _initializeLayoutEngine];
[self _setLayoutEngine: [[GSAutoLayoutEngine alloc] init]];
}
@end

View file

@ -81,6 +81,7 @@
#import "GSGuiPrivate.h"
#import "GSAutoLayoutEngine.h"
#import "NSViewPrivate.h"
#import "NSWindowPrivate.h"
/*
* We need a fast array that can store objects without retain/release ...
@ -636,7 +637,6 @@ GSSetDragTypes(NSView* obj, NSArray *types)
_needsUpdateConstraints = YES;
_translatesAutoresizingMaskIntoConstraints = YES;
_layoutEngine = nil;
return self;
}
@ -770,11 +770,6 @@ GSSetDragTypes(NSView* obj, NSArray *types)
[self unregisterDraggedTypes];
[self releaseGState];
if (_layoutEngine && !_super_view)
{
RELEASE(_layoutEngine);
}
[super dealloc];
}
@ -833,8 +828,6 @@ GSSetDragTypes(NSView* obj, NSArray *types)
index += 1;
}
[aView _setLayoutEngine: [self _layoutEngine]];
[aView _viewWillMoveToWindow: _window];
[aView _viewWillMoveToSuperview: self];
[aView setNextResponder: self];
@ -5149,7 +5142,8 @@ static NSView* findByTag(NSView *view, NSInteger aTag, NSUInteger *level)
- (void) layout
{
if (![self _layoutEngine])
GSAutoLayoutEngine *engine = [self _layoutEngine];
if (!engine)
{
return;
}
@ -5157,7 +5151,7 @@ static NSView* findByTag(NSView *view, NSInteger aTag, NSUInteger *level)
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
NSRect subviewAlignmentRect =
[[self _layoutEngine] alignmentRectForView: subview];
[engine alignmentRectForView: subview];
[subview setFrame: subviewAlignmentRect];
END_FOR_IN (subviews);
}
@ -5232,18 +5226,13 @@ static NSView* findByTag(NSView *view, NSInteger aTag, NSUInteger *level)
}
- (GSAutoLayoutEngine*) _layoutEngine
{
return _layoutEngine;
}
- (void) _setLayoutEngine: (GSAutoLayoutEngine *)engine
{
_layoutEngine = engine;
if (!([self window] && [[self window] _layoutEngine]))
{
return nil;
}
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
[subview _setLayoutEngine: engine];
END_FOR_IN (subviews);
return [[self window] _layoutEngine];
}
@end

View file

@ -46,8 +46,6 @@
- (GSAutoLayoutEngine*) _layoutEngine;
- (void) _setLayoutEngine: (GSAutoLayoutEngine*)engine;
@end
#endif // _GNUstep_H_NSViewPrivate

View file

@ -91,6 +91,7 @@
#import "GSIconManager.h"
#import "NSToolbarFrameworkPrivate.h"
#import "NSViewPrivate.h"
#import "NSWindowPrivate.h"
#define GSI_ARRAY_TYPES 0
#define GSI_ARRAY_TYPE NSWindow *
@ -923,6 +924,8 @@ many times.
DESTROY(_lastDragView);
DESTROY(_screen);
TEST_RELEASE(_layoutEngine);
/*
* FIXME This should not be necessary - the views should have removed
* their drag types, so we should already have been removed.
@ -1125,6 +1128,8 @@ many times.
_f.cursor_rects_enabled = YES;
_f.cursor_rects_valid = NO;
_layoutEngine = nil;
/* Create the window view */
cframe.origin = NSZeroPoint;
cframe.size = _frame.size;
@ -6156,6 +6161,20 @@ current key view.<br />
}
@end
@implementation NSWindow (NSConstraintBasedLayoutCorePrivateMethods)
- (GSAutoLayoutEngine*) _layoutEngine
{
return _layoutEngine;
}
- (void) _setLayoutEngine: (GSAutoLayoutEngine*)layoutEngine
{
_layoutEngine = layoutEngine;
}
@end
BOOL GSViewAcceptsDrag(NSView *v, id<NSDraggingInfo> dragInfo)
{
NSPasteboard *pb = [dragInfo draggingPasteboard];

36
Source/NSWindowPrivate.h Normal file
View file

@ -0,0 +1,36 @@
/* Copyright (C) 2023 Free Software Foundation, Inc.
By: Benjamin Johnson
Date: 28-2-2023
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#ifndef _GNUstep_H_NSWindowPrivate
#define _GNUstep_H_NSWindowPrivate
#import "AppKit/NSWindow.h"
@interface NSWindow (NSConstraintBasedLayoutCorePrivateMethods)
- (GSAutoLayoutEngine*) _layoutEngine;
- (void) _setLayoutEngine: (GSAutoLayoutEngine*)layoutEngine;
@end
#endif // _GNUstep_H_NSWindowPrivate