Skip to content

Commit c4b5cea

Browse files
Oleksii Dykanfacebook-github-bot
authored andcommitted
Move yoga based tree to separate protocol
Summary: This diff moves the yoga based tree logic from CKFlexboxComponent to a separate protocol Reviewed By: kfirapps Differential Revision: D7988764 fbshipit-source-id: ec1091bb4cd3aa2c6951533ee973b3d6b76a5702
1 parent 1b8505c commit c4b5cea

File tree

4 files changed

+127
-63
lines changed

4 files changed

+127
-63
lines changed

ComponentKit.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@
382382
534BB690204EBAF4001DC02C /* YGEnums.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 534BB667204EB2DA001DC02C /* YGEnums.cpp */; };
383383
534BB691204EBAF6001DC02C /* Utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 534BB669204EB2DA001DC02C /* Utils.h */; };
384384
534BB692204EBAF9001DC02C /* Utils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 534BB666204EB2D9001DC02C /* Utils.cpp */; };
385+
6016FC2420A9BA2900440702 /* CKComponent+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 6016FC1820A9BA2800440702 /* CKComponent+Yoga.h */; settings = {ATTRIBUTES = (Public, ); }; };
386+
6016FC2520A9BA2900440702 /* CKComponent+Yoga.h in Headers */ = {isa = PBXBuildFile; fileRef = 6016FC1820A9BA2800440702 /* CKComponent+Yoga.h */; settings = {ATTRIBUTES = (Public, ); }; };
387+
6016FC2620A9BA2900440702 /* CKComponent+Yoga.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6016FC2320A9BA2800440702 /* CKComponent+Yoga.mm */; };
388+
6016FC2720A9BA2900440702 /* CKComponent+Yoga.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6016FC2320A9BA2800440702 /* CKComponent+Yoga.mm */; };
385389
60E99CAD209B26EF0072AD84 /* CKCompositeComponentInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 60E99CAC209B26EF0072AD84 /* CKCompositeComponentInternal.h */; settings = {ATTRIBUTES = (Private, ); }; };
386390
60E99CAE209B26EF0072AD84 /* CKCompositeComponentInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 60E99CAC209B26EF0072AD84 /* CKCompositeComponentInternal.h */; settings = {ATTRIBUTES = (Private, ); }; };
387391
6DAC628C1F83DEB900536113 /* CKComponentScopeEnumeratorProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 6DAC628B1F83DEB800536113 /* CKComponentScopeEnumeratorProvider.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -957,6 +961,8 @@
957961
534BB66D204EB2DA001DC02C /* YGStyle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGStyle.h; sourceTree = "<group>"; };
958962
534BB66E204EB2DA001DC02C /* YGLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGLayout.h; sourceTree = "<group>"; };
959963
534BB66F204EB2DA001DC02C /* Yoga.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Yoga.cpp; sourceTree = "<group>"; };
964+
6016FC1820A9BA2800440702 /* CKComponent+Yoga.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CKComponent+Yoga.h"; sourceTree = "<group>"; };
965+
6016FC2320A9BA2800440702 /* CKComponent+Yoga.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CKComponent+Yoga.mm"; sourceTree = "<group>"; };
960966
60E99CAC209B26EF0072AD84 /* CKCompositeComponentInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CKCompositeComponentInternal.h; sourceTree = "<group>"; };
961967
6DAC628B1F83DEB800536113 /* CKComponentScopeEnumeratorProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKComponentScopeEnumeratorProvider.h; sourceTree = "<group>"; };
962968
72C575542099C6E400FF603E /* CKDataSourceModificationHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKDataSourceModificationHelper.h; sourceTree = "<group>"; };
@@ -1863,6 +1869,8 @@
18631869
7F6BAF651F1F6E5200600828 /* CKComponentLayoutBaseline.m */,
18641870
7F6BAF661F1F6E5200600828 /* CKFlexboxComponent.h */,
18651871
7F6BAF671F1F6E5200600828 /* CKFlexboxComponent.mm */,
1872+
6016FC1820A9BA2800440702 /* CKComponent+Yoga.h */,
1873+
6016FC2320A9BA2800440702 /* CKComponent+Yoga.mm */,
18661874
D0B47B471CBD926700BB33CE /* CKBackgroundLayoutComponent.h */,
18671875
D0B47B481CBD926700BB33CE /* CKBackgroundLayoutComponent.mm */,
18681876
D0B47B491CBD926700BB33CE /* CKCenterLayoutComponent.h */,
@@ -2141,6 +2149,7 @@
21412149
03B8B4EB1D2A346F00EDFF59 /* CKComponentDataSourceAttachControllerInternal.h in Headers */,
21422150
03B8B4ED1D2A346F00EDFF59 /* CKComponentHierarchyDebugHelper.h in Headers */,
21432151
03B8B4EE1D2A346F00EDFF59 /* CKTextComponent.h in Headers */,
2152+
6016FC2520A9BA2900440702 /* CKComponent+Yoga.h in Headers */,
21442153
03B8B4EF1D2A346F00EDFF59 /* CKStatefulViewReusePool.h in Headers */,
21452154
03B8B4F01D2A346F00EDFF59 /* CKComponentScopeRootFactory.h in Headers */,
21462155
534BB684204EBA9C001DC02C /* YGMacros.h in Headers */,
@@ -2364,6 +2373,7 @@
23642373
23FEC5C9203B38800068E09D /* CKRenderComponent.h in Headers */,
23652374
D0B47D301CBD948E00BB33CE /* CKComponentSizeRangeProviding.h in Headers */,
23662375
D0B47D031CBD948E00BB33CE /* CKSizeRange.h in Headers */,
2376+
6016FC2420A9BA2900440702 /* CKComponent+Yoga.h in Headers */,
23672377
D0B47D0A1CBD948E00BB33CE /* CKComponentScope.h in Headers */,
23682378
D0B47CFE1CBD948E00BB33CE /* CKComponentViewAttribute.h in Headers */,
23692379
D0B47D4A1CBD948E00BB33CE /* CKDataSourceChange.h in Headers */,
@@ -2948,6 +2958,7 @@
29482958
03B8B4C31D2A346F00EDFF59 /* CKTextKitRenderer+TextChecking.mm in Sources */,
29492959
03B8B4C41D2A346F00EDFF59 /* CKTextKitRenderer.mm in Sources */,
29502960
03B8B4C51D2A346F00EDFF59 /* CKTextKitRendererCache.mm in Sources */,
2961+
6016FC2720A9BA2900440702 /* CKComponent+Yoga.mm in Sources */,
29512962
03B8B4C61D2A346F00EDFF59 /* CKTextKitShadower.mm in Sources */,
29522963
03B8B4C71D2A346F00EDFF59 /* CKComponentDebugController.mm in Sources */,
29532964
534BB686204EBADA001DC02C /* Yoga.cpp in Sources */,
@@ -3228,6 +3239,7 @@
32283239
D0B47CD61CBD943400BB33CE /* CKTextComponentViewControlTracker.mm in Sources */,
32293240
D0B47CD71CBD943400BB33CE /* CKTextKitAttributes.mm in Sources */,
32303241
D0B47CD81CBD943400BB33CE /* CKTextKitContext.mm in Sources */,
3242+
6016FC2620A9BA2900440702 /* CKComponent+Yoga.mm in Sources */,
32313243
D0B47CD91CBD943400BB33CE /* CKTextKitEntityAttribute.m in Sources */,
32323244
D0B47CDA1CBD943400BB33CE /* CKTextKitRenderer+Positioning.mm in Sources */,
32333245
D0B47CDB1CBD943400BB33CE /* CKTextKitRenderer+TextChecking.mm in Sources */,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import <ComponentKit/CKComponent.h>
12+
#import <ComponentKit/CKComponentLayout.h>
13+
#import <ComponentKit/CKCompositeComponent.h>
14+
15+
#import "yoga/Yoga.h"
16+
17+
YGConfigRef ckYogaDefaultConfig();
18+
19+
/**
20+
A protocol that is used for the components that are powered by Yoga layout engine
21+
(https://github.com/facebook/yoga).
22+
*/
23+
@protocol CKYogaBasedComponentProtocol <NSObject>
24+
25+
/**
26+
A flag that represents whether the component's layout is based on Yoga.
27+
28+
The default value is NO
29+
*/
30+
- (BOOL)isYogaBasedLayout;
31+
32+
/**
33+
A method that returns a new yoga node for a constained size.
34+
35+
By default returns a Yoga node with default configuration
36+
*/
37+
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize;
38+
39+
/**
40+
A method to create a component layout instance from the given node and constrained size.
41+
42+
By default returns an empty layout
43+
*/
44+
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize;
45+
46+
@end
47+
48+
@interface CKComponent (Yoga) <CKYogaBasedComponentProtocol>
49+
@end
50+
51+
@interface CKCompositeComponent (Yoga)
52+
@end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#import "CKComponent+Yoga.h"
12+
#import "CKCompositeComponentInternal.h"
13+
14+
YGConfigRef ckYogaDefaultConfig()
15+
{
16+
static YGConfigRef defaultConfig;
17+
static dispatch_once_t onceToken;
18+
dispatch_once(&onceToken, ^{
19+
defaultConfig = YGConfigNew();
20+
YGConfigSetPointScaleFactor(defaultConfig, [UIScreen mainScreen].scale);
21+
});
22+
return defaultConfig;
23+
}
24+
25+
@implementation CKComponent (Yoga)
26+
27+
- (BOOL)isYogaBasedLayout
28+
{
29+
return NO;
30+
}
31+
32+
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize
33+
{
34+
return YGNodeNewWithConfig(ckYogaDefaultConfig());
35+
}
36+
37+
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize
38+
{
39+
return {};
40+
}
41+
42+
@end
43+
44+
@implementation CKCompositeComponent (Yoga)
45+
46+
- (BOOL)isYogaBasedLayout
47+
{
48+
return self.component.isYogaBasedLayout;
49+
}
50+
51+
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize
52+
{
53+
return [self.component ygNode:constrainedSize];
54+
}
55+
56+
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize
57+
{
58+
const CKComponentLayout l = [self.component layoutFromYgNode:layoutNode thatFits:constrainedSize];
59+
return {self, l.size, {{{0,0}, l}}};
60+
}
61+
62+
@end

ComponentKit/LayoutComponents/CKFlexboxComponent.mm

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
#import "yoga/Yoga.h"
1414

15+
#import "CKComponent+Yoga.h"
1516
#import "CKComponentInternal.h"
1617
#import "CKComponentLayout.h"
1718
#import "CKComponentLayoutBaseline.h"
1819
#import "CKComponentSubclass.h"
1920
#import "CKCompositeComponent.h"
20-
#import "CKCompositeComponentInternal.h"
2121
#import "CKInternalHelpers.h"
2222
#import "CKRenderTreeNodeWithChildren.h"
2323
#import "ComponentUtilities.h"
@@ -49,17 +49,6 @@ @implementation CKFlexboxChildCachedLayout
4949

5050
@end
5151

52-
static YGConfigRef ckYogaDefaultConfig()
53-
{
54-
static YGConfigRef defaultConfig;
55-
static dispatch_once_t onceToken;
56-
dispatch_once(&onceToken, ^{
57-
defaultConfig = YGConfigNew();
58-
YGConfigSetPointScaleFactor(defaultConfig, [UIScreen mainScreen].scale);
59-
});
60-
return defaultConfig;
61-
}
62-
6352
@interface CKFlexboxComponentContext ()
6453
@property(nonatomic, assign, readonly) BOOL usesDeepYogaTrees;
6554
@end
@@ -74,57 +63,6 @@ + (instancetype)newWithUsesDeepYogaTrees:(BOOL)usesDeepYogaTrees
7463
}
7564
@end
7665

77-
@interface CKComponent (CKYogaBasedComponent)
78-
79-
- (BOOL)isYogaBasedLayout;
80-
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize;
81-
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize;
82-
83-
@end
84-
85-
@implementation CKComponent (CKYogaBasedComponent)
86-
87-
- (BOOL)isYogaBasedLayout
88-
{
89-
return NO;
90-
}
91-
92-
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize
93-
{
94-
return YGNodeNewWithConfig(ckYogaDefaultConfig());
95-
}
96-
97-
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize
98-
{
99-
return {};
100-
}
101-
102-
@end
103-
104-
@interface CKCompositeComponent (CKYogaBasedCompositeComponent)
105-
@end
106-
107-
@implementation CKCompositeComponent (CKYogaBasedCompositeComponent)
108-
109-
- (BOOL)isYogaBasedLayout
110-
{
111-
return self.component.isYogaBasedLayout;
112-
}
113-
114-
- (YGNodeRef)ygNode:(CKSizeRange)constrainedSize
115-
{
116-
return [self.component ygNode:constrainedSize];
117-
}
118-
119-
- (CKComponentLayout)layoutFromYgNode:(YGNodeRef)layoutNode thatFits:(CKSizeRange)constrainedSize
120-
{
121-
const CKComponentLayout l = [self.component layoutFromYgNode:layoutNode thatFits:constrainedSize];
122-
return {self, l.size, {{{0,0}, l}}};
123-
}
124-
125-
@end
126-
127-
12866
@implementation CKFlexboxComponent {
12967
CKFlexboxComponentStyle _style;
13068
std::vector<CKFlexboxComponentChild> _children;

0 commit comments

Comments
 (0)