[YogaKit] support macOS.
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "YGLayout.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
@@ -43,48 +43,76 @@ static void YogaSwizzleInstanceMethod(Class cls, SEL originalSelector, SEL swizz
|
||||
@implementation UIView (YogaKitAutoApplyLayout)
|
||||
|
||||
+ (void)load {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
YogaSwizzleInstanceMethod(self, @selector(initWithFrame:), @selector(_yoga_initWithFrame:));
|
||||
YogaSwizzleInstanceMethod(self, @selector(setFrame:), @selector(_yoga_setFrame:));
|
||||
YogaSwizzleInstanceMethod(self, @selector(setBounds:), @selector(_yoga_setBounds:));
|
||||
});
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
YogaSwizzleInstanceMethod(self, @selector(initWithFrame:), @selector(_yoga_initWithFrame:));
|
||||
YogaSwizzleInstanceMethod(self, @selector(setFrame:), @selector(_yoga_setFrame:));
|
||||
YogaSwizzleInstanceMethod(self, @selector(setBounds:), @selector(_yoga_setBounds:));
|
||||
#if TARGET_OS_OSX
|
||||
YogaSwizzleInstanceMethod(self, @selector(setFrameSize:), @selector(_yoga_setFrameSize:));
|
||||
YogaSwizzleInstanceMethod(self, @selector(setBoundsSize:), @selector(_yoga_setBoundsSize:));
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
- (CGSize)_yoga_boundsSize {
|
||||
NSValue *value = (NSValue *)objc_getAssociatedObject(self, kYGBoundsSizeAssociatedKey);
|
||||
NSValue *value = (NSValue *)objc_getAssociatedObject(self, kYGBoundsSizeAssociatedKey);
|
||||
|
||||
return value ? value.CGSizeValue : CGSizeMake(YGUndefined, YGUndefined);
|
||||
return value ?
|
||||
#if TARGET_OS_OSX
|
||||
value.sizeValue
|
||||
#else
|
||||
value.CGSizeValue
|
||||
#endif
|
||||
: CGSizeMake(YGUndefined, YGUndefined);
|
||||
}
|
||||
|
||||
- (void)set_yoga_boundsSize:(CGSize)size {
|
||||
objc_setAssociatedObject(self,
|
||||
kYGBoundsSizeAssociatedKey,
|
||||
[NSValue valueWithCGSize:size],
|
||||
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
objc_setAssociatedObject(self,
|
||||
kYGBoundsSizeAssociatedKey,
|
||||
#if TARGET_OS_OSX
|
||||
[NSValue valueWithSize:size]
|
||||
#else
|
||||
[NSValue valueWithCGSize:size]
|
||||
#endif
|
||||
, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (instancetype)_yoga_initWithFrame:(CGRect)frame {
|
||||
id _self = [self _yoga_initWithFrame:frame];
|
||||
if (_self) {
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
id _self = [self _yoga_initWithFrame:frame];
|
||||
if (_self) {
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
|
||||
return _self;
|
||||
return _self;
|
||||
}
|
||||
|
||||
- (void)_yoga_setFrame:(CGRect)frame {
|
||||
[self _yoga_setFrame:frame];
|
||||
[self _yoga_setFrame:frame];
|
||||
|
||||
[self _yoga_applyLayout];
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
|
||||
- (void)_yoga_setBounds:(CGRect)bounds {
|
||||
[self _yoga_setBounds:bounds];
|
||||
[self _yoga_setBounds:bounds];
|
||||
|
||||
[self _yoga_applyLayout];
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
|
||||
#if TARGET_OS_OSX
|
||||
- (void)_yoga_setFrameSize:(NSSize)newSize {
|
||||
[self _yoga_setFrameSize:newSize];
|
||||
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
|
||||
- (void)_yoga_setBoundsSize:(NSSize)newSize {
|
||||
[self _yoga_setBoundsSize:newSize];
|
||||
|
||||
[self _yoga_applyLayout];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)_yoga_applyLayout {
|
||||
if (self.isYogaEnabled && self.yoga.isEnabled) {
|
||||
CGSize size = self.bounds.size;
|
||||
|
@@ -5,7 +5,14 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#import <TargetConditionals.h>
|
||||
#if TARGET_OS_OSX
|
||||
#import <AppKit/AppKit.h>
|
||||
#define UIView NSView
|
||||
#else
|
||||
#import <UIKit/UIKit.h>
|
||||
#endif
|
||||
|
||||
#import <yoga/YGEnums.h>
|
||||
#import <yoga/YGMacros.h>
|
||||
#import <yoga/Yoga.h>
|
||||
|
@@ -160,7 +160,11 @@ static CGFloat YGScaleFactor() {
|
||||
static CGFloat scale;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^() {
|
||||
#if TARGET_OS_OSX
|
||||
scale = [NSScreen mainScreen].backingScaleFactor;
|
||||
#else
|
||||
scale = [UIScreen mainScreen].scale;
|
||||
#endif
|
||||
});
|
||||
|
||||
return scale;
|
||||
@@ -388,10 +392,18 @@ static YGSize YGMeasureView(
|
||||
//
|
||||
// See https://github.com/facebook/yoga/issues/606 for more information.
|
||||
if (!view.yoga.isUIView || [view.subviews count] > 0) {
|
||||
#if TARGET_OS_OSX
|
||||
CGSize fittingSize = view.fittingSize;
|
||||
sizeThatFits = (CGSize){
|
||||
.width = MIN(constrainedWidth, fittingSize.width),
|
||||
.height = MIN(constrainedHeight, fittingSize.height)
|
||||
};
|
||||
#else
|
||||
sizeThatFits = [view sizeThatFits:(CGSize){
|
||||
.width = constrainedWidth,
|
||||
.height = constrainedHeight,
|
||||
}];
|
||||
#endif
|
||||
}
|
||||
|
||||
return (YGSize){
|
||||
@@ -513,12 +525,17 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
|
||||
topLeft.y + YGNodeLayoutGetHeight(node),
|
||||
};
|
||||
|
||||
#if TARGET_OS_OSX
|
||||
const CGPoint origin = preserveOrigin ? view.frame.origin : CGPointZero;
|
||||
#else
|
||||
// use bounds/center and not frame if non-identity transform.
|
||||
const CGPoint origin = preserveOrigin ? (CGPoint) {
|
||||
.x = view.center.x - CGRectGetWidth(view.bounds) * 0.5,
|
||||
.y = view.center.y - CGRectGetHeight(view.bounds) * 0.5
|
||||
}
|
||||
: CGPointZero;
|
||||
#endif
|
||||
|
||||
CGRect frame = (CGRect){
|
||||
.origin =
|
||||
{
|
||||
@@ -532,6 +549,16 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
|
||||
},
|
||||
};
|
||||
|
||||
#if TARGET_OS_OSX
|
||||
if (!view.superview.isFlipped && view.superview.isYogaEnabled && view.superview.yoga.isEnabled) {
|
||||
frame.origin.y = YGNodeLayoutGetHeight(view.superview.yoga.node) - CGRectGetMaxY(frame);
|
||||
}
|
||||
|
||||
view.frame = (CGRect) {
|
||||
.origin = CGPointMake(YGRoundPixelValue(frame.origin.x), YGRoundPixelValue(frame.origin.y)),
|
||||
.size = CGSizeMake(YGRoundPixelValue(frame.size.width), YGRoundPixelValue(frame.size.height))
|
||||
};
|
||||
#else
|
||||
view.bounds = (CGRect) {
|
||||
.origin = view.bounds.origin,
|
||||
.size = CGSizeMake(YGRoundPixelValue(CGRectGetWidth(frame)), YGRoundPixelValue(CGRectGetHeight(frame)))
|
||||
@@ -541,6 +568,7 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
|
||||
.x = YGRoundPixelValue(CGRectGetMinX(frame) + CGRectGetWidth(frame) * 0.5),
|
||||
.y = YGRoundPixelValue(CGRectGetMinY(frame) + CGRectGetHeight(frame) * 0.5)
|
||||
};
|
||||
#endif
|
||||
|
||||
if (!yoga.isLeaf) {
|
||||
for (NSUInteger i = 0; i < view.subviews.count; i++) {
|
||||
|
Reference in New Issue
Block a user