From 02a002f59497ffe4402fe190ef55dadec4b98361 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Mon, 5 Jul 2021 21:27:24 +0800 Subject: [PATCH 1/4] [yoga] Support SPM (Swift Package Manager) --- Package.swift | 48 ++++++++++++++++++++++++++++++++++++ YogaKit/Source/UIView+Yoga.h | 8 ++++++ YogaKit/Source/YGLayout.h | 7 ++++++ YogaKit/Source/YGLayout.m | 17 +++++++++++-- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Package.swift 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; -- 2.50.1.windows.1 From 733e346320f76ee4df221e051ebb09df126915cd Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Mon, 15 Nov 2021 23:11:39 +0800 Subject: [PATCH 2/4] Fix SPM ompilation error --- Package.swift | 18 +++++++++--------- YogaKit/Source/UIView+Yoga.h | 2 +- YogaKit/Source/YGLayout+Private.h | 4 ++-- YogaKit/Source/{YGLayout.h => YogaKit.h} | 6 +++--- YogaKit/Source/{YGLayout.m => YogaKit.m} | 0 YogaKit/Source/modulemap/YogaKit-umbrella.h | 2 ++ YogaKit/Source/modulemap/module.modulemap | 4 ++++ yoga/modulemap/Yoga-umbrella.h | 8 ++++++++ yoga/modulemap/module.modulemap | 4 ++++ 9 files changed, 33 insertions(+), 15 deletions(-) rename YogaKit/Source/{YGLayout.h => YogaKit.h} (98%) rename YogaKit/Source/{YGLayout.m => YogaKit.m} (100%) create mode 100644 YogaKit/Source/modulemap/YogaKit-umbrella.h create mode 100644 YogaKit/Source/modulemap/module.modulemap create mode 100644 yoga/modulemap/Yoga-umbrella.h create mode 100644 yoga/modulemap/module.modulemap diff --git a/Package.swift b/Package.swift index 0446a73a..4d1a6bec 100644 --- a/Package.swift +++ b/Package.swift @@ -27,20 +27,20 @@ let package = Package( .target( name: "YogaKit", dependencies: ["Yoga"], - path: ".", - exclude: ["YogaKit/Source/YGLayoutExtensions.swift"], - sources: ["YogaKit/Source"], - publicHeadersPath: "YogaKit/Source", + path: "YogaKit", + exclude: ["Source/YGLayoutExtensions.swift"], + sources: ["Source"], + publicHeadersPath: "Source/modulemap", cSettings: [ - .headerSearchPath(".") + .headerSearchPath("..") ]), .target( name: "Yoga", - path: ".", - sources: ["yoga"], - publicHeadersPath: "yoga/include", + path: "yoga", + sources: ["."], + publicHeadersPath: "modulemap", cSettings: [ - .headerSearchPath(".") + .headerSearchPath("..") ]) ], cLanguageStandard: .gnu11, diff --git a/YogaKit/Source/UIView+Yoga.h b/YogaKit/Source/UIView+Yoga.h index d115baf8..b3c2c2f9 100644 --- a/YogaKit/Source/UIView+Yoga.h +++ b/YogaKit/Source/UIView+Yoga.h @@ -14,7 +14,7 @@ #import #endif -#import "YGLayout.h" +#import "YogaKit.h" NS_ASSUME_NONNULL_BEGIN 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 98% rename from YogaKit/Source/YGLayout.h rename to YogaKit/Source/YogaKit.h index 55f00836..5e4f3631 100644 --- a/YogaKit/Source/YGLayout.h +++ b/YogaKit/Source/YogaKit.h @@ -13,9 +13,9 @@ #import #endif -#import -#import -#import +#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 100% rename from YogaKit/Source/YGLayout.m rename to YogaKit/Source/YogaKit.m diff --git a/YogaKit/Source/modulemap/YogaKit-umbrella.h b/YogaKit/Source/modulemap/YogaKit-umbrella.h new file mode 100644 index 00000000..eab3f408 --- /dev/null +++ b/YogaKit/Source/modulemap/YogaKit-umbrella.h @@ -0,0 +1,2 @@ +#import "../YogaKit.h" +#import "../UIView+Yoga.h" diff --git a/YogaKit/Source/modulemap/module.modulemap b/YogaKit/Source/modulemap/module.modulemap new file mode 100644 index 00000000..c956df7d --- /dev/null +++ b/YogaKit/Source/modulemap/module.modulemap @@ -0,0 +1,4 @@ +module YogaKit { + umbrella header "YogaKit-umbrella.h" + export * +} diff --git a/yoga/modulemap/Yoga-umbrella.h b/yoga/modulemap/Yoga-umbrella.h new file mode 100644 index 00000000..2c8877ac --- /dev/null +++ b/yoga/modulemap/Yoga-umbrella.h @@ -0,0 +1,8 @@ +#ifdef __OBJC__ +#import +#endif + +#import "../YGEnums.h" +#import "../YGMacros.h" +#import "../YGValue.h" +#import "../Yoga.h" diff --git a/yoga/modulemap/module.modulemap b/yoga/modulemap/module.modulemap new file mode 100644 index 00000000..89ae11c1 --- /dev/null +++ b/yoga/modulemap/module.modulemap @@ -0,0 +1,4 @@ +module Yoga [extern_c] { + umbrella header "Yoga-umbrella.h" + export * +} -- 2.50.1.windows.1 From cdb5f82ab5698e624467eb733027f8399801e1d9 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 25 Nov 2021 12:59:04 +0800 Subject: [PATCH 3/4] Remove umbrella header Replace `#import` with `@import` --- YogaKit/Source/UIView+Yoga.h | 4 ++-- YogaKit/Source/UIView+Yoga.m | 2 +- YogaKit/Source/YogaKit.h | 4 ++-- YogaKit/Source/modulemap/YogaKit-umbrella.h | 2 -- YogaKit/Source/modulemap/module.modulemap | 3 ++- yoga/modulemap/Yoga-umbrella.h | 8 -------- yoga/modulemap/module.modulemap | 5 ++++- 7 files changed, 11 insertions(+), 17 deletions(-) delete mode 100644 YogaKit/Source/modulemap/YogaKit-umbrella.h delete mode 100644 yoga/modulemap/Yoga-umbrella.h diff --git a/YogaKit/Source/UIView+Yoga.h b/YogaKit/Source/UIView+Yoga.h index b3c2c2f9..078e4d75 100644 --- a/YogaKit/Source/UIView+Yoga.h +++ b/YogaKit/Source/UIView+Yoga.h @@ -8,10 +8,10 @@ #import #if TARGET_OS_OSX -#import +@import AppKit; #define UIView NSView #else -#import +@import UIKit; #endif #import "YogaKit.h" 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/YogaKit.h b/YogaKit/Source/YogaKit.h index 5e4f3631..8235f325 100644 --- a/YogaKit/Source/YogaKit.h +++ b/YogaKit/Source/YogaKit.h @@ -8,9 +8,9 @@ #import #if TARGET_OS_OSX -#import +@import AppKit; #else -#import +@import UIKit; #endif #import "../../yoga/YGEnums.h" diff --git a/YogaKit/Source/modulemap/YogaKit-umbrella.h b/YogaKit/Source/modulemap/YogaKit-umbrella.h deleted file mode 100644 index eab3f408..00000000 --- a/YogaKit/Source/modulemap/YogaKit-umbrella.h +++ /dev/null @@ -1,2 +0,0 @@ -#import "../YogaKit.h" -#import "../UIView+Yoga.h" diff --git a/YogaKit/Source/modulemap/module.modulemap b/YogaKit/Source/modulemap/module.modulemap index c956df7d..0ffc9ecf 100644 --- a/YogaKit/Source/modulemap/module.modulemap +++ b/YogaKit/Source/modulemap/module.modulemap @@ -1,4 +1,5 @@ module YogaKit { - umbrella header "YogaKit-umbrella.h" + header "../YogaKit.h" + header "../UIView+Yoga.h" export * } diff --git a/yoga/modulemap/Yoga-umbrella.h b/yoga/modulemap/Yoga-umbrella.h deleted file mode 100644 index 2c8877ac..00000000 --- a/yoga/modulemap/Yoga-umbrella.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifdef __OBJC__ -#import -#endif - -#import "../YGEnums.h" -#import "../YGMacros.h" -#import "../YGValue.h" -#import "../Yoga.h" diff --git a/yoga/modulemap/module.modulemap b/yoga/modulemap/module.modulemap index 89ae11c1..590f7ded 100644 --- a/yoga/modulemap/module.modulemap +++ b/yoga/modulemap/module.modulemap @@ -1,4 +1,7 @@ module Yoga [extern_c] { - umbrella header "Yoga-umbrella.h" + header "../Yoga.h" + header "../YGValue.h" + header "../YGMacros.h" + header "../YGEnums.h" export * } -- 2.50.1.windows.1 From 354c358ac592c954620137bd319d95ffdf7216cb Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 25 Nov 2021 13:16:45 +0800 Subject: [PATCH 4/4] Update scaleFactor() function --- YogaKit/Source/YogaKit.m | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/YogaKit/Source/YogaKit.m b/YogaKit/Source/YogaKit.m index 360fe4c8..2565a012 100644 --- a/YogaKit/Source/YogaKit.m +++ b/YogaKit/Source/YogaKit.m @@ -157,11 +157,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 -NS_INLINE CGFloat scaleFactor() { return [NSScreen mainScreen].backingScaleFactor; } + scaleFactor = [NSScreen mainScreen].backingScaleFactor; #else -NS_INLINE CGFloat scaleFactor() { return [UIScreen mainScreen].scale; } + scaleFactor = [UIScreen mainScreen].scale; #endif + }); + + return scaleFactor; +} @interface YGLayout () -- 2.50.1.windows.1