-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental UxmlNameAttribute for generating UxmlFactory
- Loading branch information
Showing
45 changed files
with
552 additions
and
102 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Assets/UIComponents.Tests/Roslyn/Resources/UxmlNameTestComponent.uxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<UXML xmlns:test="UIComponents.Tests.Roslyn"> | ||
<test:UxmlNameTest /> | ||
</UXML> |
3 changes: 3 additions & 0 deletions
3
Assets/UIComponents.Tests/Roslyn/Resources/UxmlNameTestComponent.uxml.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/UIComponents.Tests/Roslyn/UxmlNameTestComponent.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Assets/UIComponents/Core/Experimental/UxmlNameAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<ListHeaderComponent> | ||
/// { | ||
/// 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; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/UIComponents/Core/Experimental/UxmlNameAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
+2.5 KB
(110%)
Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
16 changes: 16 additions & 0 deletions
16
UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Resources/UxmlNameAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
....Allows_Setting_Default_Value.verified.cs → ....Allows_Setting_Default_Value.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s.Allows_Specifying_Uxml_Name.verified.cs → ...s.Allows_Specifying_Uxml_Name.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...otTests.Generates_Bool_Traits.verified.cs → ...otTests.Generates_Bool_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...shots/UxmlAugmentGeneratorSnapshotTests.Generates_Both_Traits_And_UxmlFactory.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tTests.Generates_Color_Traits.verified.cs → ...tTests.Generates_Color_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Tests.Generates_Double_Traits.verified.cs → ...Tests.Generates_Double_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Enum_Trait_With_Internal_Enum.verified.cs → ...Enum_Trait_With_Internal_Enum.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...otTests.Generates_Enum_Traits.verified.cs → ...otTests.Generates_Enum_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tTests.Generates_Float_Traits.verified.cs → ...tTests.Generates_Float_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tTests.Generates_In_Namespace.verified.cs → ...tTests.Generates_In_Namespace.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...hotTests.Generates_Int_Traits.verified.cs → ...hotTests.Generates_Int_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...otTests.Generates_Long_Traits.verified.cs → ...otTests.Generates_Long_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Tests.Generates_String_Traits.verified.cs → ...Tests.Generates_String_Traits.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...0FirstTraitClass.UxmlTraits.g.verified.cs → ...sses.00FirstTraitClass.Uxml.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...SecondTraitClass.UxmlTraits.g.verified.cs → ...ses.01SecondTraitClass.Uxml.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...2ThirdTraitClass.UxmlTraits.g.verified.cs → ...sses.02ThirdTraitClass.Uxml.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...hotTests.Generates_Traits_For_Many_Classes.DotNet6_0.00FirstTraitClass.Uxml.g.received.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...otTests.Generates_Traits_For_Many_Classes.DotNet6_0.01SecondTraitClass.Uxml.g.received.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...hotTests.Generates_Traits_For_Many_Classes.DotNet6_0.02ThirdTraitClass.Uxml.g.received.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...apshots/UxmlAugmentGeneratorSnapshotTests.Generates_UxmlFactory_With_UxmlName.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...shots/UxmlAugmentGeneratorSnapshotTests.Handles_Subclass.00MyComponent.Uxml.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
1 change: 1 addition & 0 deletions
1
...AugmentGeneratorSnapshotTests.Handles_Subclass.01MyComponentWithTraits.Uxml.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
23 changes: 23 additions & 0 deletions
23
...AugmentGeneratorSnapshotTests.Handles_Subclass.DotNet6_0.00MyComponent.Uxml.g.received.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.