Skip to content

Commit 37c4825

Browse files
woehrl01facebook-github-bot
authored andcommitted
Move configuration to new YGConfig and pass them down to CalculateLayout
Summary: Move configuration to new ```YGConfig``` and pass them down to CalculateLayout. See #418 . Adds ```YGConfigNew()``` + ```YGConfigFree```, and changed ```YGSetExperimentalFeatureEnabled``` to use the config. New function for calculation is ```YGNodeCalculateLayoutWithConfig```. Closes #432 Reviewed By: astreet Differential Revision: D4611359 Pulled By: emilsjolander fbshipit-source-id: a1332f0e1b21cec02129dd021ee57408449e10b0
1 parent 8668e43 commit 37c4825

File tree

89 files changed

+4536
-3049
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+4536
-3049
lines changed

YogaKit/Source/YGLayout.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ - (void)set##objc_capitalized_name:(CGFloat)objc_lowercased_name
8585
YG_VALUE_EDGE_PROPERTY(lowercased_name##Vertical, capitalized_name##Vertical, capitalized_name, YGEdgeVertical) \
8686
YG_VALUE_EDGE_PROPERTY(lowercased_name, capitalized_name, capitalized_name, YGEdgeAll)
8787

88+
static YGConfigRef globalConfig;
89+
8890
@interface YGLayout ()
8991

9092
@property (nonatomic, weak, readonly) UIView *view;
@@ -99,14 +101,15 @@ @implementation YGLayout
99101

100102
+ (void)initialize
101103
{
102-
YGSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);
104+
globalConfig = YGConfigNew();
105+
YGConfigSetExperimentalFeatureEnabled(globalConfig, YGExperimentalFeatureWebFlexBasis, true);
103106
}
104107

105108
- (instancetype)initWithView:(UIView*)view
106109
{
107110
if (self = [super init]) {
108111
_view = view;
109-
_node = YGNodeNew();
112+
_node = YGNodeNewWithConfig(globalConfig);
110113
YGNodeSetContext(_node, (__bridge void *) view);
111114
_isEnabled = NO;
112115
_isIncludedInLayout = YES;

csharp/Facebook.Yoga/Native.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,72 @@ public static YogaNode GetManaged(IntPtr ygNodePtr)
7575
#endif
7676
}
7777

78+
internal class YGConfigHandle : SafeHandle
79+
{
80+
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
81+
private GCHandle _managed;
82+
#endif
83+
84+
private YGConfigHandle() : base(IntPtr.Zero, true)
85+
{
86+
}
87+
88+
public override bool IsInvalid
89+
{
90+
get
91+
{
92+
return this.handle == IntPtr.Zero;
93+
}
94+
}
95+
96+
protected override bool ReleaseHandle()
97+
{
98+
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
99+
if (_managed.IsAllocated)
100+
{
101+
_managed.Free();
102+
}
103+
#endif
104+
Native.YGConfigFree(this.handle);
105+
GC.KeepAlive(this);
106+
return true;
107+
}
108+
}
109+
78110
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
79111
public static extern void YGInteropSetLogger(
80112
[MarshalAs(UnmanagedType.FunctionPtr)] YogaLogger.Func func);
81113

82114
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
83115
public static extern YGNodeHandle YGNodeNew();
84116

117+
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
118+
public static extern YGNodeHandle YGNodeNewWithConfig(YGConfigHandle config);
119+
85120
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
86121
public static extern void YGNodeFree(IntPtr node);
87122

88123
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
89124
public static extern void YGNodeReset(YGNodeHandle node);
90125

126+
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
127+
public static extern YGConfigHandle YGConfigNew();
128+
129+
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
130+
public static extern void YGConfigFree(IntPtr node);
131+
91132
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
92133
public static extern int YGNodeGetInstanceCount();
93134

94135
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
95-
public static extern void YGSetExperimentalFeatureEnabled(
136+
public static extern void YGConfigSetExperimentalFeatureEnabled(
137+
YGConfigHandle config,
96138
YogaExperimentalFeature feature,
97139
bool enabled);
98140

99141
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
100-
public static extern bool YGIsExperimentalFeatureEnabled(
142+
public static extern bool YGConfigIsExperimentalFeatureEnabled(
143+
YGConfigHandle config,
101144
YogaExperimentalFeature feature);
102145

103146
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
@@ -258,7 +301,7 @@ public static extern void YGNodeSetHasNewLayout(
258301

259302
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
260303
public static extern void YGNodeStyleSetHeightPercent(YGNodeHandle node, float height);
261-
304+
262305
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
263306
public static extern void YGNodeStyleSetHeightAuto(YGNodeHandle node);
264307

csharp/Facebook.Yoga/YogaNode.cs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@
2121

2222
namespace Facebook.Yoga
2323
{
24+
public class YogaConfig
25+
{
26+
27+
private Native.YGConfigHandle _ygConfig;
28+
29+
public YogaConfig()
30+
{
31+
_ygConfig = Native.YGConfigNew();
32+
if (_ygConfig.IsInvalid)
33+
{
34+
throw new InvalidOperationException("Failed to allocate native memory");
35+
}
36+
}
37+
38+
internal Native.YGConfigHandle Handle {
39+
get {
40+
return _ygConfig;
41+
}
42+
}
43+
44+
public void SetExperimentalFeatureEnabled(
45+
YogaExperimentalFeature feature,
46+
bool enabled)
47+
{
48+
Native.YGConfigSetExperimentalFeatureEnabled(_ygConfig, feature, enabled);
49+
}
50+
51+
public bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
52+
{
53+
return Native.YGConfigIsExperimentalFeatureEnabled(_ygConfig, feature);
54+
}
55+
56+
}
57+
2458
public partial class YogaNode : IEnumerable<YogaNode>
2559
{
2660
private Native.YGNodeHandle _ygNode;
@@ -48,6 +82,17 @@ public YogaNode()
4882
}
4983
}
5084

85+
public YogaNode(YogaConfig config)
86+
{
87+
YogaLogger.Initialize();
88+
89+
_ygNode = Native.YGNodeNewWithConfig(config.Handle);
90+
if (_ygNode.IsInvalid)
91+
{
92+
throw new InvalidOperationException("Failed to allocate native memory");
93+
}
94+
}
95+
5196
public YogaNode(YogaNode srcNode)
5297
: this()
5398
{
@@ -681,17 +726,5 @@ public static int GetInstanceCount()
681726
{
682727
return Native.YGNodeGetInstanceCount();
683728
}
684-
685-
public static void SetExperimentalFeatureEnabled(
686-
YogaExperimentalFeature feature,
687-
bool enabled)
688-
{
689-
Native.YGSetExperimentalFeatureEnabled(feature, enabled);
690-
}
691-
692-
public static bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
693-
{
694-
return Native.YGIsExperimentalFeatureEnabled(feature);
695-
}
696729
}
697730
}

0 commit comments

Comments
 (0)