Skip to content

Commit

Permalink
Move configuration to new YGConfig and pass them down to CalculateLayout
Browse files Browse the repository at this point in the history
Summary:
Move configuration to new ```YGConfig``` and pass them down to CalculateLayout. See facebook#418 .

Adds ```YGConfigNew()``` + ```YGConfigFree```, and changed ```YGSetExperimentalFeatureEnabled``` to use the config.

New function for calculation is ```YGNodeCalculateLayoutWithConfig```.
Closes facebook#432

Reviewed By: astreet

Differential Revision: D4611359

Pulled By: emilsjolander

fbshipit-source-id: a1332f0e1b21cec02129dd021ee57408449e10b0
  • Loading branch information
woehrl01 authored and facebook-github-bot committed Mar 1, 2017
1 parent 8668e43 commit 37c4825
Show file tree
Hide file tree
Showing 89 changed files with 4,536 additions and 3,049 deletions.
7 changes: 5 additions & 2 deletions YogaKit/Source/YGLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ - (void)set##objc_capitalized_name:(CGFloat)objc_lowercased_name
YG_VALUE_EDGE_PROPERTY(lowercased_name##Vertical, capitalized_name##Vertical, capitalized_name, YGEdgeVertical) \
YG_VALUE_EDGE_PROPERTY(lowercased_name, capitalized_name, capitalized_name, YGEdgeAll)

static YGConfigRef globalConfig;

@interface YGLayout ()

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

+ (void)initialize
{
YGSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);
globalConfig = YGConfigNew();
YGConfigSetExperimentalFeatureEnabled(globalConfig, YGExperimentalFeatureWebFlexBasis, true);
}

- (instancetype)initWithView:(UIView*)view
{
if (self = [super init]) {
_view = view;
_node = YGNodeNew();
_node = YGNodeNewWithConfig(globalConfig);
YGNodeSetContext(_node, (__bridge void *) view);
_isEnabled = NO;
_isIncludedInLayout = YES;
Expand Down
49 changes: 46 additions & 3 deletions csharp/Facebook.Yoga/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,72 @@ public static YogaNode GetManaged(IntPtr ygNodePtr)
#endif
}

internal class YGConfigHandle : SafeHandle
{
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
private GCHandle _managed;
#endif

private YGConfigHandle() : base(IntPtr.Zero, true)
{
}

public override bool IsInvalid
{
get
{
return this.handle == IntPtr.Zero;
}
}

protected override bool ReleaseHandle()
{
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
if (_managed.IsAllocated)
{
_managed.Free();
}
#endif
Native.YGConfigFree(this.handle);
GC.KeepAlive(this);
return true;
}
}

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGInteropSetLogger(
[MarshalAs(UnmanagedType.FunctionPtr)] YogaLogger.Func func);

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

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern YGNodeHandle YGNodeNewWithConfig(YGConfigHandle config);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGNodeFree(IntPtr node);

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

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern YGConfigHandle YGConfigNew();

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigFree(IntPtr node);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int YGNodeGetInstanceCount();

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGSetExperimentalFeatureEnabled(
public static extern void YGConfigSetExperimentalFeatureEnabled(
YGConfigHandle config,
YogaExperimentalFeature feature,
bool enabled);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern bool YGIsExperimentalFeatureEnabled(
public static extern bool YGConfigIsExperimentalFeatureEnabled(
YGConfigHandle config,
YogaExperimentalFeature feature);

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

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGNodeStyleSetHeightPercent(YGNodeHandle node, float height);

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

Expand Down
57 changes: 45 additions & 12 deletions csharp/Facebook.Yoga/YogaNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@

namespace Facebook.Yoga
{
public class YogaConfig
{

private Native.YGConfigHandle _ygConfig;

public YogaConfig()
{
_ygConfig = Native.YGConfigNew();
if (_ygConfig.IsInvalid)
{
throw new InvalidOperationException("Failed to allocate native memory");
}
}

internal Native.YGConfigHandle Handle {
get {
return _ygConfig;
}
}

public void SetExperimentalFeatureEnabled(
YogaExperimentalFeature feature,
bool enabled)
{
Native.YGConfigSetExperimentalFeatureEnabled(_ygConfig, feature, enabled);
}

public bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
{
return Native.YGConfigIsExperimentalFeatureEnabled(_ygConfig, feature);
}

}

public partial class YogaNode : IEnumerable<YogaNode>
{
private Native.YGNodeHandle _ygNode;
Expand Down Expand Up @@ -48,6 +82,17 @@ public YogaNode()
}
}

public YogaNode(YogaConfig config)
{
YogaLogger.Initialize();

_ygNode = Native.YGNodeNewWithConfig(config.Handle);
if (_ygNode.IsInvalid)
{
throw new InvalidOperationException("Failed to allocate native memory");
}
}

public YogaNode(YogaNode srcNode)
: this()
{
Expand Down Expand Up @@ -681,17 +726,5 @@ public static int GetInstanceCount()
{
return Native.YGNodeGetInstanceCount();
}

public static void SetExperimentalFeatureEnabled(
YogaExperimentalFeature feature,
bool enabled)
{
Native.YGSetExperimentalFeatureEnabled(feature, enabled);
}

public static bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
{
return Native.YGIsExperimentalFeatureEnabled(feature);
}
}
}
Loading

0 comments on commit 37c4825

Please sign in to comment.