2019-01-31 09:29:12 -08:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-04-10 10:42:09 -07:00
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-04-10 10:42:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
2017-05-03 09:22:35 -07:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
#if __IOS__
|
|
|
|
using ObjCRuntime;
|
|
|
|
#endif
|
|
|
|
#if ENABLE_IL2CPP
|
|
|
|
using AOT;
|
|
|
|
#endif
|
2017-04-10 10:42:09 -07:00
|
|
|
|
|
|
|
namespace Facebook.Yoga
|
|
|
|
{
|
|
|
|
public class YogaConfig
|
|
|
|
{
|
2017-05-03 09:22:35 -07:00
|
|
|
internal static readonly YogaConfig Default = new YogaConfig(YGConfigHandle.Default);
|
|
|
|
private static YogaLogger _managedLogger;
|
2017-04-10 10:42:09 -07:00
|
|
|
|
2017-05-03 09:22:35 -07:00
|
|
|
private YGConfigHandle _ygConfig;
|
|
|
|
private Logger _logger;
|
|
|
|
|
|
|
|
private YogaConfig(YGConfigHandle ygConfig)
|
2017-04-10 10:42:09 -07:00
|
|
|
{
|
2017-05-03 09:22:35 -07:00
|
|
|
_ygConfig = ygConfig;
|
2017-04-10 10:42:09 -07:00
|
|
|
if (_ygConfig.IsInvalid)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Failed to allocate native memory");
|
|
|
|
}
|
2017-05-03 09:22:35 -07:00
|
|
|
|
|
|
|
_ygConfig.SetContext(this);
|
|
|
|
|
|
|
|
if (_ygConfig == YGConfigHandle.Default)
|
|
|
|
{
|
|
|
|
_managedLogger = LoggerInternal;
|
|
|
|
Native.YGInteropSetLogger(_managedLogger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public YogaConfig()
|
|
|
|
: this(Native.YGConfigNew())
|
|
|
|
{
|
2017-04-10 10:42:09 -07:00
|
|
|
}
|
|
|
|
|
2017-05-03 09:22:35 -07:00
|
|
|
internal YGConfigHandle Handle
|
2017-04-10 10:42:09 -07:00
|
|
|
{
|
|
|
|
get {
|
|
|
|
return _ygConfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:22:35 -07:00
|
|
|
#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
|
|
|
|
[MonoPInvokeCallback(typeof(YogaLogger))]
|
|
|
|
#endif
|
|
|
|
private static void LoggerInternal(
|
|
|
|
IntPtr unmanagedConfigPtr,
|
|
|
|
IntPtr unmanagedNodePtr,
|
|
|
|
YogaLogLevel level,
|
|
|
|
string message)
|
|
|
|
{
|
|
|
|
var config = YGConfigHandle.GetManaged(unmanagedConfigPtr);
|
|
|
|
if (config == null || config._logger == null)
|
|
|
|
{
|
|
|
|
// Default logger
|
2017-05-16 12:27:53 -07:00
|
|
|
System.Diagnostics.Debug.WriteLine(message);
|
2017-05-03 09:22:35 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var node = YGNodeHandle.GetManaged(unmanagedNodePtr);
|
|
|
|
config._logger(config, node, level, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == YogaLogLevel.Error || level == YogaLogLevel.Fatal)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Logger Logger
|
|
|
|
{
|
|
|
|
get {
|
|
|
|
return _logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
_logger = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:42:09 -07:00
|
|
|
public void SetExperimentalFeatureEnabled(
|
|
|
|
YogaExperimentalFeature feature,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
Native.YGConfigSetExperimentalFeatureEnabled(_ygConfig, feature, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
|
|
|
|
{
|
|
|
|
return Native.YGConfigIsExperimentalFeatureEnabled(_ygConfig, feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool UseWebDefaults
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Native.YGConfigGetUseWebDefaults(_ygConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Native.YGConfigSetUseWebDefaults(_ygConfig, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:29:12 -08:00
|
|
|
public bool UseLegacyStretchBehaviour
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Native.YGConfigGetUseLegacyStretchBehaviour(_ygConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Native.YGConfigSetUseLegacyStretchBehaviour(_ygConfig, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:42:09 -07:00
|
|
|
public float PointScaleFactor
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Native.YGConfigSetPointScaleFactor(_ygConfig, value);
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 14:22:23 -07:00
|
|
|
|
|
|
|
public static int GetInstanceCount()
|
|
|
|
{
|
|
|
|
return Native.YGConfigGetInstanceCount();
|
|
|
|
}
|
2017-05-03 09:22:35 -07:00
|
|
|
|
|
|
|
public static void SetDefaultLogger(Logger logger)
|
|
|
|
{
|
|
|
|
Default.Logger = logger;
|
|
|
|
}
|
2017-04-10 10:42:09 -07:00
|
|
|
}
|
|
|
|
}
|