Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move configuration to new YGConfig and pass them down to CalculateLayout #432

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions csharp/Facebook.Yoga/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ 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);
Expand All @@ -88,16 +120,24 @@ public static extern void YGInteropSetLogger(
[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(
YGConfigHandle config,
YogaExperimentalFeature feature,
bool enabled);

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

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
Expand All @@ -116,6 +156,14 @@ public static extern void YGNodeCalculateLayout(
float availableHeight,
YogaDirection parentDirection);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGNodeCalculateLayoutWithConfig(
YGNodeHandle node,
float availableWidth,
float availableHeight,
YogaDirection parentDirection,
YGConfigHandle config);

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

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

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");
}
}

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

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

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

}

public partial class YogaNode : IEnumerable<YogaNode>
{
private Native.YGNodeHandle _ygNode;
Expand Down Expand Up @@ -603,6 +636,15 @@ public void CalculateLayout()
Native.YGNodeStyleGetDirection(_ygNode));
}

public void CalculateLayout(YGConfig config)
{
Native.YGNodeCalculateLayoutWithConfig(
_ygNode,
YogaConstants.Undefined,
YogaConstants.Undefined,
Native.YGNodeStyleGetDirection(_ygNode), config.Handle);
}

#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
[MonoPInvokeCallback(typeof(YogaMeasureFunc))]
private static YogaSize MeasureInternalIOS(
Expand Down Expand Up @@ -681,17 +723,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