Add NSAppearanceCustomization to NSApplication and NSView

This commit is contained in:
ethanc8 2023-02-02 18:13:34 -06:00 committed by ethanc8
parent 6aff0bd0f8
commit 2303052e50
5 changed files with 39 additions and 270 deletions

View file

@ -81,5 +81,23 @@ APPKIT_EXPORT NSAppearanceName const NSAppearanceNameLightContent;
#endif /* GS_API_MACOSX */
@protocol NSAppearanceCustomization
#if OS_API_VERSION(MAC_OS_X_VERSION_10_0, GS_API_LATEST)
@required
#if GS_HAS_DECLARED_PROPERTIES
/** Set the appearance of the reciever. Default is nil. */
@property(strong) NSAppearance* appearance;
/** Get the appearance of the reciever. If self.appearance is nil, then go up the
view hierarchy to find an appearance. If still nil, go up to NSApp. If still nil,
go up to [NSAppearance currentAppearance]. */
@property(readonly, strong) NSAppearance* effectiveAppearance;
#else
- (NSAppearance*) appearance;
- (void) setAppearance: (NSAppearance*) appearance;
- (NSAppearance*) effectiveAppearance;
#endif
#endif
@end
#endif /* _NSAppearance_h_GNUSTEP_GUI_INCLUDE */

View file

@ -47,6 +47,7 @@ extern "C" {
#endif
@class NSArray;
@class NSAppearance;
@class NSDate;
@class NSError;
@class NSException;
@ -232,6 +233,7 @@ APPKIT_EXPORT_CLASS
NSWindow *_hidden_key;
NSWindow *_hidden_main;
GSInfoPanel *_infoPanel;
NSAppearance *_appearance;
NSApplicationPresentationOptions _presentationOptions;
/* This autorelease pool should only be created and used by -run, with

View file

@ -39,6 +39,7 @@
#import <AppKit/NSUserInterfaceLayout.h>
#import <AppKit/NSLayoutConstraint.h>
@class NSAppearance;
@class NSArray;
@class NSAttributedString;
@class NSData;
@ -129,7 +130,7 @@ extern const CGFloat NSViewNoInstrinsicMetric;
extern const CGFloat NSViewNoIntrinsicMetric;
APPKIT_EXPORT_CLASS
@interface NSView : NSResponder
@interface NSView : NSResponder <NSAppearanceCustomization>
{
NSRect _frame;
NSRect _bounds;
@ -195,6 +196,7 @@ PACKAGE_SCOPE
NSFocusRingType _focusRingType;
NSRect _autoresizingFrameError;
NSShadow *_shadow;
NSAppearance* _appearance;
}
/*

View file

@ -62,6 +62,7 @@
#import "AppKit/AppKitExceptions.h"
#import "AppKit/NSAlert.h"
#import "AppKit/NSAppearance.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSCell.h"
#import "AppKit/NSCursor.h"
@ -3834,6 +3835,20 @@ struct _DelegateWrapper
return self;
}
- (NSAppearance*) appearance {
return _appearance;
}
- (void) setAppearance: (NSAppearance*) appearance {
ASSIGNCOPY(_appearance, appearance);
}
- (NSAppearance*) effectiveAppearance {
if(_appearance) return _appearance;
else return [NSAppearance currentAppearance];
}
@end /* NSApplication */

View file

@ -72,6 +72,7 @@
#import "AppKit/NSView.h"
#import "AppKit/NSWindow.h"
#import "AppKit/NSWorkspace.h"
#import "AppKit/NSAppearance.h"
#import "AppKit/PSOperators.h"
#import "GNUstepGUI/GSDisplayServer.h"
#import "GNUstepGUI/GSTrackingRect.h"
@ -5165,277 +5166,8 @@ static NSView* findByTag(NSView *view, NSInteger aTag, NSUInteger *level)
return;
}
/**
* Layout
*/
- (void) layout
{
GSAutoLayoutEngine *engine = [self _layoutEngine];
if (!engine)
{
return;
}
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
NSRect subviewAlignmentRect =
[engine alignmentRectForView: subview];
[subview setFrame: subviewAlignmentRect];
END_FOR_IN (subviews);
}
- (void) layoutSubtreeIfNeeded
{
[self updateConstraintsForSubtreeIfNeeded];
[self _layoutViewAndSubViews];
}
- (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;
}
- (NSLayoutPriority) contentCompressionResistancePriority
{
return _contentCompressionResistancePriority;
}
- (void) setContentCompressionResistancePriority: (NSLayoutPriority)priority
{
_contentCompressionResistancePriority = priority;
}
- (NSSize) intrinsicContentSize
{
return NSMakeSize(NSViewNoIntrinsicMetric, NSViewNoIntrinsicMetric);
}
- (CGFloat) baselineOffsetFromBottom
{
return 0;
}
- (CGFloat) firstBaselineOffsetFromTop
{
return 0;
}
@end
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
@implementation NSView (NSConstraintBasedLayoutCorePrivateMethods)
// This private setter allows the updateConstraints method to toggle needsUpdateConstraints
- (void) _setNeedsUpdateConstraints: (BOOL)needsUpdateConstraints
{
_needsUpdateConstraints = needsUpdateConstraints;
}
- (void) _layoutViewAndSubViews
{
if (_needsLayout)
{
[self layout];
_needsLayout = NO;
}
NSArray *subviews = [self subviews];
FOR_IN (NSView *, subview, subviews)
[subview _layoutViewAndSubViews];
END_FOR_IN (subviews);
}
- (GSAutoLayoutEngine*) _layoutEngine
{
if (![self window])
{
return nil;
}
return [[self window] _layoutEngine];
}
- (void) _layoutEngineDidChangeAlignmentRect
{
[[self superview] setNeedsLayout: YES];
}
- (GSAutoLayoutEngine*) _getOrCreateLayoutEngine
{
if (![self window])
{
return nil;
}
if (![[self window] _layoutEngine])
{
[[self window] _bootstrapAutoLayout];
}
return [[self window] _layoutEngine];
}
@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];
}
- (NSLayoutPriority) contentCompressionResistancePriorityForOrientation: (NSLayoutConstraintOrientation)orientation {
if (orientation == NSLayoutConstraintOrientationHorizontal) {
return _compressionPriorities.horizontal;
} else {
return _compressionPriorities.vertical;
}
}
- (void) setContentCompressionResistancePriority: (NSLayoutPriority)priority forOrientation: (NSLayoutConstraintOrientation)orientation {
if (orientation == NSLayoutConstraintOrientationHorizontal) {
_compressionPriorities.horizontal = priority;
} else {
_compressionPriorities.vertical = priority;
}
}
- (NSLayoutPriority) contentHuggingPriorityForOrientation: (NSLayoutConstraintOrientation)orientation {
if (orientation == NSLayoutConstraintOrientationHorizontal) {
return _huggingPriorities.horizontal;
} else {
return _huggingPriorities.vertical;
}
}
- (void) setContentHuggingPriority: (NSLayoutPriority)priority forOrientation: (NSLayoutConstraintOrientation)orientation
{
if (orientation == NSLayoutConstraintOrientationHorizontal) {
_huggingPriorities.horizontal = priority;
} else {
_huggingPriorities.vertical = priority;
}
}
@end
@implementation NSView (NSConstraintBasedLayoutInstallingConstraints)
- (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
#endif
@implementation NSView (__NSViewPrivateMethods__)
/*