diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..4d1a6bec --- /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: "YogaKit", + exclude: ["Source/YGLayoutExtensions.swift"], + sources: ["Source"], + publicHeadersPath: "Source/modulemap", + cSettings: [ + .headerSearchPath("..") + ]), + .target( + name: "Yoga", + path: "yoga", + sources: ["."], + publicHeadersPath: "modulemap", + cSettings: [ + .headerSearchPath("..") + ]) + ], + cLanguageStandard: .gnu11, + cxxLanguageStandard: .gnucxx14 +) diff --git a/YogaKit/Source/UIView+Yoga.h b/YogaKit/Source/UIView+Yoga.h index 4c85dcc4..078e4d75 100644 --- a/YogaKit/Source/UIView+Yoga.h +++ b/YogaKit/Source/UIView+Yoga.h @@ -5,8 +5,16 @@ * LICENSE file in the root directory of this source tree. */ -#import -#import "YGLayout.h" +#import + +#if TARGET_OS_OSX +@import AppKit; +#define UIView NSView +#else +@import UIKit; +#endif + +#import "YogaKit.h" NS_ASSUME_NONNULL_BEGIN diff --git a/YogaKit/Source/UIView+Yoga.m b/YogaKit/Source/UIView+Yoga.m index e472c9c7..d47954b4 100644 --- a/YogaKit/Source/UIView+Yoga.m +++ b/YogaKit/Source/UIView+Yoga.m @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -#import #import "UIView+Yoga.h" #import "YGLayout+Private.h" +@import ObjectiveC; static const void* kYGYogaAssociatedKey = &kYGYogaAssociatedKey; diff --git a/YogaKit/Source/YGLayout+Private.h b/YogaKit/Source/YGLayout+Private.h index 0588d950..a511149d 100644 --- a/YogaKit/Source/YGLayout+Private.h +++ b/YogaKit/Source/YGLayout+Private.h @@ -5,8 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -#import -#import "YGLayout.h" +#import "../../yoga/Yoga.h" +#import "YogaKit.h" @interface YGLayout () diff --git a/YogaKit/Source/YGLayout.h b/YogaKit/Source/YogaKit.h similarity index 97% rename from YogaKit/Source/YGLayout.h rename to YogaKit/Source/YogaKit.h index 5a60f95e..8235f325 100644 --- a/YogaKit/Source/YGLayout.h +++ b/YogaKit/Source/YogaKit.h @@ -5,10 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -#import -#import -#import -#import +#import + +#if TARGET_OS_OSX +@import AppKit; +#else +@import UIKit; +#endif + +#import "../../yoga/YGEnums.h" +#import "../../yoga/YGMacros.h" +#import "../../yoga/Yoga.h" YG_EXTERN_C_BEGIN diff --git a/YogaKit/Source/YGLayout.m b/YogaKit/Source/YogaKit.m similarity index 96% rename from YogaKit/Source/YGLayout.m rename to YogaKit/Source/YogaKit.m index 4a95a5ca..2565a012 100644 --- a/YogaKit/Source/YGLayout.m +++ b/YogaKit/Source/YogaKit.m @@ -158,6 +158,20 @@ YGValue YGPercentValue(CGFloat value) { static YGConfigRef globalConfig; +static CGFloat scaleFactor(void) { + static CGFloat scaleFactor = 1; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ +#if TARGET_OS_OSX + scaleFactor = [NSScreen mainScreen].backingScaleFactor; +#else + scaleFactor = [UIScreen mainScreen].scale; +#endif + }); + + return scaleFactor; +} + @interface YGLayout () @property(nonatomic, weak, readonly) UIView* view; @@ -175,7 +189,7 @@ static YGConfigRef globalConfig; globalConfig = YGConfigNew(); YGConfigSetExperimentalFeatureEnabled( globalConfig, YGExperimentalFeatureWebFlexBasis, true); - YGConfigSetPointScaleFactor(globalConfig, [UIScreen mainScreen].scale); + YGConfigSetPointScaleFactor(globalConfig, scaleFactor()); } - (instancetype)initWithView:(UIView*)view { @@ -362,10 +376,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 +474,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; diff --git a/YogaKit/Source/modulemap/module.modulemap b/YogaKit/Source/modulemap/module.modulemap new file mode 100644 index 00000000..0ffc9ecf --- /dev/null +++ b/YogaKit/Source/modulemap/module.modulemap @@ -0,0 +1,5 @@ +module YogaKit { + header "../YogaKit.h" + header "../UIView+Yoga.h" + export * +} diff --git a/yoga/modulemap/module.modulemap b/yoga/modulemap/module.modulemap new file mode 100644 index 00000000..590f7ded --- /dev/null +++ b/yoga/modulemap/module.modulemap @@ -0,0 +1,7 @@ +module Yoga [extern_c] { + header "../Yoga.h" + header "../YGValue.h" + header "../YGMacros.h" + header "../YGEnums.h" + export * +}