#if UNITY_2019_4_OR_NEWER
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace com.zibra.liquid.Foundation.Editor
{
///
/// Editor related UI Toolkit utility methods.
///
internal static class UIToolkitEditorUtility
{
///
/// Helper method to Clone VisualTreeAsset into the target and apply uss styles including the skin.
/// For example if you provide path `Assets/MyWindow/AwesomeTab` the following will happen:
/// * The `Assets/MyWindow/AwesomeTab.uxml` cloned into the `target`
/// * The `Assets/MyWindow/AwesomeTab.uss` style is applied for the `target` if file exists.
/// * The `Assets/MyWindow/AwesomeDark.uss` or `Assets/MyWindow/AwesomeLight.uss` (depends on current editor
/// skin option) style is applied for the `target` if file exists.
///
/// The target to clone visual tree and apply styles to.
/// The VisualTreeAsset path without extension.
public static void CloneTreeAndApplyStyle(VisualElement target, string path)
{
var uxmlPath = $"{path}.uxml";
var visualTree = AssetDatabase.LoadAssetAtPath(uxmlPath);
if (visualTree == null)
{
Debug.LogError($"Failed to load VisualTreeAsset at path: {uxmlPath}");
return;
}
visualTree.CloneTree(target);
ApplyStyle(target, path);
}
///
/// Helper method to Apply Style & Skin style to the target.
/// For example if you provide path `Assets/MyWindow/AwesomeTab` the following styles will apply:
/// * `Assets/MyWindow/AwesomeTab.uss` if file exists.
/// * `Assets/MyWindow/AwesomeDark.uss` or `Assets/MyWindow/AwesomeLight.uss` (depends on current editor skin
/// option) if file exists.
///
/// The target to apply styles to.
/// The StyleSheet path without extension.
public static void ApplyStyle(VisualElement target, string path)
{
var stylesheet = AssetDatabase.LoadAssetAtPath($"{path}.uss");
var ussSkinPath = EditorGUIUtility.isProSkin ? $"{path}Dark.uss" : $"{path}Light.uss";
var skitStylesheet = AssetDatabase.LoadAssetAtPath(ussSkinPath);
if (stylesheet != null)
target.styleSheets.Add(stylesheet);
if (skitStylesheet != null)
target.styleSheets.Add(skitStylesheet);
}
// TODO: make it internal
public static void ApplyStyleForInternalControl(T target)
where T : VisualElement
{
var name = typeof(T).Name;
ApplyStyleForInternalControl(target, name);
}
// TODO: make it internal
public static void ApplyStyleForInternalControl(VisualElement target, string name)
{
ApplyStyle(target, $"{ZibraAIPackage.UIToolkitControlsPath}/{name}/{name}");
}
}
}
#endif