Skip to content

Commit

Permalink
feat: add experimental UxmlNameAttribute for generating UxmlFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Oct 8, 2022
1 parent 5573b07 commit 5df8799
Show file tree
Hide file tree
Showing 45 changed files with 552 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<UXML xmlns:test="UIComponents.Tests.Roslyn">
<test:UxmlNameTest />
</UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/UIComponents.Tests/Roslyn/UxmlNameTestComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using UIComponents.Experimental;

namespace UIComponents.Tests.Roslyn
{
[UxmlName("UxmlNameTest")]
public partial class UxmlNameTestComponent : UIComponent {}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/UIComponents.Tests/Roslyn/UxmlNameTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UIElements;

namespace UIComponents.Tests.Roslyn
{
[TestFixture]
public class UxmlNameTests
{
[Test]
public void Generates_Uxml_Factory_For_Name()
{
var layout = Resources.Load<VisualTreeAsset>("UxmlNameTestComponent");

var container = new VisualElement();
layout.CloneTree(container);

var testElement = container.Q<UxmlNameTestComponent>();

Assert.That(testElement, Is.Not.Null);
}
}
}
3 changes: 3 additions & 0 deletions Assets/UIComponents.Tests/Roslyn/UxmlNameTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Assets/UIComponents/Core/Experimental/TraitAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ namespace UIComponents.Experimental
{
/// <summary>
/// When used in Unity 2021.2 or later, this attribute will generate
/// UxmlTraits and UxmlFactory implementations automatically when
/// applied to a field or property which has a set method. The class
/// must be partial.
/// an UxmlTraits implementation when applied to a field or property
/// which has a set method. The class must be partial.
/// </summary>
/// <example>
/// <code>
Expand Down
54 changes: 54 additions & 0 deletions Assets/UIComponents/Core/Experimental/UxmlNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Diagnostics;
using JetBrains.Annotations;
using UnityEngine.TestTools;
using UnityEngine.UIElements;

namespace UIComponents.Experimental
{
/// <summary>
/// When used in Unity 2021.2 or later, this attribute will generate
/// a UxmlFactory implementation automatically when
/// applied to a partial class.
/// </summary>
/// <example>
/// <code>
/// [UxmlName("ListHeader")]
/// public partial class ListHeaderComponent : UIComponent {}
///
/// // This generates:
///
/// public partial class ListHeaderComponent
/// {
/// public new class UxmlFactory : UxmlFactory&lt;ListHeaderComponent&gt;
/// {
/// public override string uxmlName
/// {
/// get { return "ListHeader"; }
/// }
///
/// public override string uxmlQualifiedName
/// {
/// get { return uxmlNamespace + "." + uxmlName; }
/// }
/// }
/// }
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
[Conditional("UNITY_EDITOR")]
[BaseTypeRequired(typeof(VisualElement))]
[ExcludeFromCoverage]
public sealed class UxmlNameAttribute : Attribute
{
/// <summary>
/// The custom UXML name.
/// </summary>
public readonly string Name;

public UxmlNameAttribute(string name)
{
Name = name;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Diagnostics.CodeAnalysis;

namespace UIComponents.Experimental
{
[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class UxmlNameAttribute : Attribute
{
public readonly string Name;

public UxmlNameAttribute(string name)
{
Name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: ComponentWithDefaultValueTraits.UxmlTraits.g.cs
//HintName: ComponentWithDefaultValueTraits.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: CustomNamespaceComponent.UxmlTraits.g.cs
//HintName: CustomNamespaceComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: BoolComponentWithUsing.UxmlTraits.g.cs
//HintName: BoolComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//HintName: ComponentWithUxmlNameAndTraits.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class ComponentWithUxmlNameAndTraits
{
public new partial class UxmlFactory : UxmlFactory<ComponentWithUxmlNameAndTraits, UxmlTraits>
{
public override string uxmlName
{
get { return "AwesomeUxmlName"; }
}

public override string uxmlQualifiedName
{
get { return uxmlNamespace + "." + uxmlName; }
}
}

public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlBoolAttributeDescription m_Value = new UxmlBoolAttributeDescription { name = "value" };

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
m_Value.defaultValue = true;
((ComponentWithUxmlNameAndTraits)ve).Value = m_Value.GetValueFromBag(bag, cc);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: ColorComponentWithUsing.UxmlTraits.g.cs
//HintName: ColorComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: DoubleComponentWithUsing.UxmlTraits.g.cs
//HintName: DoubleComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: OwnEnumComponent.UxmlTraits.g.cs
//HintName: OwnEnumComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: MyEnumComponentWithUsing.UxmlTraits.g.cs
//HintName: MyEnumComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: FloatComponentWithUsing.UxmlTraits.g.cs
//HintName: FloatComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: CustomNamespaceComponent.UxmlTraits.g.cs
//HintName: CustomNamespaceComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: IntComponentWithUsing.UxmlTraits.g.cs
//HintName: IntComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: LongComponentWithUsing.UxmlTraits.g.cs
//HintName: LongComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: StringComponentWithUsing.UxmlTraits.g.cs
//HintName: StringComponentWithUsing.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: FirstTraitClass.UxmlTraits.g.cs
//HintName: FirstTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: SecondTraitClass.UxmlTraits.g.cs
//HintName: SecondTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: ThirdTraitClass.UxmlTraits.g.cs
//HintName: ThirdTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//HintName: FirstTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class FirstTraitClass
{
public new partial class UxmlFactory : UxmlFactory<FirstTraitClass, UxmlTraits> {}

public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlIntAttributeDescription m_FieldTrait = new UxmlIntAttributeDescription { name = "custom-trait-name" };
UxmlFloatAttributeDescription m_PropertyTrait = new UxmlFloatAttributeDescription { name = "my-property" };

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((FirstTraitClass)ve).FieldTrait = m_FieldTrait.GetValueFromBag(bag, cc);
((FirstTraitClass)ve).PropertyTrait = m_PropertyTrait.GetValueFromBag(bag, cc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//HintName: SecondTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class SecondTraitClass
{
public new partial class UxmlFactory : UxmlFactory<SecondTraitClass, UxmlTraits> {}

public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlBoolAttributeDescription m_Enabled = new UxmlBoolAttributeDescription { name = "enabled" };
UxmlLongAttributeDescription m_SomeValue = new UxmlLongAttributeDescription { name = "secret" };

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
m_Enabled.defaultValue = true;
((SecondTraitClass)ve).Enabled = m_Enabled.GetValueFromBag(bag, cc);
((SecondTraitClass)ve).SomeValue = m_SomeValue.GetValueFromBag(bag, cc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: ThirdTraitClass.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class ThirdTraitClass
{
public new partial class UxmlFactory : UxmlFactory<ThirdTraitClass, UxmlTraits> {}

public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription m_Name = new UxmlStringAttributeDescription { name = "name" };

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((ThirdTraitClass)ve).Name = m_Name.GetValueFromBag(bag, cc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: MyUxmlNameAttributeComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class MyUxmlNameAttributeComponent
{
public new partial class UxmlFactory : UxmlFactory<MyUxmlNameAttributeComponent>
{
public override string uxmlName
{
get { return "MyUxmlNameAttribute"; }
}

public override string uxmlQualifiedName
{
get { return uxmlNamespace + "." + uxmlName; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: MyComponent.Uxml.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using UnityEngine.UIElements;

public partial class MyComponent
{
public new partial class UxmlFactory : UxmlFactory<MyComponent, UxmlTraits> {}

public new partial class UxmlTraits : VisualElement.UxmlTraits
{
UxmlDoubleAttributeDescription m_Trait = new UxmlDoubleAttributeDescription { name = "double-value" };

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((MyComponent)ve).Trait = m_Trait.GetValueFromBag(bag, cc);
}
}
}

0 comments on commit 5df8799

Please sign in to comment.