diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..0446a73a --- /dev/null +++ b/Package.swift @@ -0,0 +1,48 @@ +// swift-tools-version:5.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "yoga", + platforms: [ + .macOS(.v10_10), .iOS(.v9), .tvOS(.v9) + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "YogaKit", + targets: ["YogaKit"]), + .library( + name: "Yoga", + targets: ["Yoga"]) + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "YogaKit", + dependencies: ["Yoga"], + path: ".", + exclude: ["YogaKit/Source/YGLayoutExtensions.swift"], + sources: ["YogaKit/Source"], + publicHeadersPath: "YogaKit/Source", + cSettings: [ + .headerSearchPath(".") + ]), + .target( + name: "Yoga", + path: ".", + sources: ["yoga"], + publicHeadersPath: "yoga/include", + cSettings: [ + .headerSearchPath(".") + ]) + ], + cLanguageStandard: .gnu11, + cxxLanguageStandard: .gnucxx14 +) diff --git a/YogaKit/Source/UIView+Yoga.h b/YogaKit/Source/UIView+Yoga.h index 4c85dcc4..d115baf8 100644 --- a/YogaKit/Source/UIView+Yoga.h +++ b/YogaKit/Source/UIView+Yoga.h @@ -5,7 +5,15 @@ * LICENSE file in the root directory of this source tree. */ +#import + +#if TARGET_OS_OSX +#import +#define UIView NSView +#else #import +#endif + #import "YGLayout.h" NS_ASSUME_NONNULL_BEGIN diff --git a/YogaKit/Source/YGLayout.h b/YogaKit/Source/YGLayout.h index 5a60f95e..55f00836 100644 --- a/YogaKit/Source/YGLayout.h +++ b/YogaKit/Source/YGLayout.h @@ -5,7 +5,14 @@ * LICENSE file in the root directory of this source tree. */ +#import + +#if TARGET_OS_OSX +#import +#else #import +#endif + #import #import #import diff --git a/YogaKit/Source/YGLayout.m b/YogaKit/Source/YGLayout.m index 4a95a5ca..360fe4c8 100644 --- a/YogaKit/Source/YGLayout.m +++ b/YogaKit/Source/YGLayout.m @@ -157,6 +157,11 @@ YGValue YGPercentValue(CGFloat value) { } static YGConfigRef globalConfig; +#if TARGET_OS_OSX +NS_INLINE CGFloat scaleFactor() { return [NSScreen mainScreen].backingScaleFactor; } +#else +NS_INLINE CGFloat scaleFactor() { return [UIScreen mainScreen].scale; } +#endif @interface YGLayout () @@ -175,7 +180,7 @@ static YGConfigRef globalConfig; globalConfig = YGConfigNew(); YGConfigSetExperimentalFeatureEnabled( globalConfig, YGExperimentalFeatureWebFlexBasis, true); - YGConfigSetPointScaleFactor(globalConfig, [UIScreen mainScreen].scale); + YGConfigSetPointScaleFactor(globalConfig, scaleFactor()); } - (instancetype)initWithView:(UIView*)view { @@ -362,10 +367,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 = fmin(constrainedWidth, fittingSize.width), + .height = fmin(constrainedHeight, fittingSize.height) + }; +#else sizeThatFits = [view sizeThatFits:(CGSize){ .width = constrainedWidth, .height = constrainedHeight, }]; +#endif } return (YGSize){ @@ -452,7 +465,7 @@ static CGFloat YGRoundPixelValue(CGFloat value) { static CGFloat scale; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^() { - scale = [UIScreen mainScreen].scale; + scale = scaleFactor(); }); return roundf(value * scale) / scale;