Browse Source

Make template instantiated by style a name scope.

To do this had to move the name scope stuff into Avalonia.Styling.
Steven Kirk 9 years ago
parent
commit
2268227f20

+ 0 - 4
src/Avalonia.Controls/Avalonia.Controls.csproj

@@ -56,7 +56,6 @@
     <Compile Include="HotkeyManager.cs" />
     <Compile Include="IApplicationLifecycle.cs" />
     <Compile Include="IScrollable.cs" />
-    <Compile Include="INameScope.cs" />
     <Compile Include="IPseudoClasses.cs" />
     <Compile Include="DropDownItem.cs" />
     <Compile Include="ISetInheritanceParent.cs" />
@@ -65,9 +64,6 @@
     <Compile Include="IVirtualizingPanel.cs" />
     <Compile Include="LayoutTransformControl.cs" />
     <Compile Include="Mixins\ContentControlMixin.cs" />
-    <Compile Include="NameScope.cs" />
-    <Compile Include="NameScopeEventArgs.cs" />
-    <Compile Include="NameScopeExtensions.cs" />
     <Compile Include="Platform\ITopLevelRenderer.cs" />
     <Compile Include="Platform\IWindowingPlatform.cs" />
     <Compile Include="Platform\PlatformManager.cs" />

+ 8 - 0
src/Avalonia.Styling/Avalonia.Styling.csproj

@@ -43,13 +43,17 @@
     <Compile Include="..\Shared\SharedAssemblyInfo.cs">
       <Link>Properties\SharedAssemblyInfo.cs</Link>
     </Compile>
+    <Compile Include="Controls\NameScopeEventArgs.cs" />
+    <Compile Include="Controls\NameScopeExtensions.cs" />
     <Compile Include="LogicalTree\ILogical.cs" />
     <Compile Include="LogicalTree\LogicalExtensions.cs" />
     <Compile Include="LogicalTree\LogicalTreeAttachmentEventArgs.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Styling\ActivatedSubject.cs" />
     <Compile Include="Styling\ActivatedValue.cs" />
+    <Compile Include="Controls\INameScope.cs" />
     <Compile Include="Styling\ITemplate.cs" />
+    <Compile Include="Controls\NameScope.cs" />
     <Compile Include="Styling\TemplateSelector.cs" />
     <Compile Include="Styling\DescendentSelector.cs" />
     <Compile Include="Styling\ChildSelector.cs" />
@@ -93,6 +97,10 @@
     <None Include="Styling\packages.config" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\Avalonia.Animation\Avalonia.Animation.csproj">
+      <Project>{d211e587-d8bc-45b9-95a4-f297c8fa5200}</Project>
+      <Name>Avalonia.Animation</Name>
+    </ProjectReference>
     <ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj">
       <Project>{B09B78D8-9B26-48B0-9149-D64A2F120F3F}</Project>
       <Name>Avalonia.Base</Name>

+ 0 - 0
src/Avalonia.Controls/INameScope.cs → src/Avalonia.Styling/Controls/INameScope.cs


+ 3 - 3
src/Avalonia.Controls/NameScope.cs → src/Avalonia.Styling/Controls/NameScope.cs

@@ -15,7 +15,7 @@ namespace Avalonia.Controls
         /// Defines the NameScope attached property.
         /// </summary>
         public static readonly AttachedProperty<INameScope> NameScopeProperty =
-            AvaloniaProperty.RegisterAttached<NameScope, Control, INameScope>("NameScope");
+            AvaloniaProperty.RegisterAttached<NameScope, Visual, INameScope>("NameScope");
 
         private readonly Dictionary<string, object> _inner = new Dictionary<string, object>();
 
@@ -34,7 +34,7 @@ namespace Avalonia.Controls
         /// </summary>
         /// <param name="visual">The visual.</param>
         /// <returns>The value of the NameScope attached property.</returns>
-        public static INameScope GetNameScope(Control visual)
+        public static INameScope GetNameScope(Visual visual)
         {
             return visual.GetValue(NameScopeProperty);
         }
@@ -44,7 +44,7 @@ namespace Avalonia.Controls
         /// </summary>
         /// <param name="visual">The visual.</param>
         /// <param name="value">The value to set.</param>
-        public static void SetNameScope(Control visual, INameScope value)
+        public static void SetNameScope(Visual visual, INameScope value)
         {
             visual.SetValue(NameScopeProperty, value);
         }

+ 0 - 0
src/Avalonia.Controls/NameScopeEventArgs.cs → src/Avalonia.Styling/Controls/NameScopeEventArgs.cs


+ 0 - 0
src/Avalonia.Controls/NameScopeExtensions.cs → src/Avalonia.Styling/Controls/NameScopeExtensions.cs


+ 4 - 1
src/Avalonia.Styling/Styling/Setter.cs

@@ -4,6 +4,7 @@
 using System;
 using System.Reactive.Disposables;
 using System.Reactive.Subjects;
+using Avalonia.Controls;
 using Avalonia.Data;
 using Avalonia.Metadata;
 using Avalonia.Reactive;
@@ -100,7 +101,9 @@ namespace Avalonia.Styling
 
                 if (template != null)
                 {
-                    value = template.Build();
+                    var materialized = template.Build();
+                    NameScope.SetNameScope((Visual)materialized, new NameScope());
+                    value = materialized;
                 }
 
                 if (activator == null)

+ 13 - 0
tests/Avalonia.Styling.UnitTests/SetterTests.cs

@@ -48,5 +48,18 @@ namespace Avalonia.Styling.UnitTests
 
             Assert.IsType<Canvas>(control.Child);
         }
+
+        [Fact]
+        public void Materializes_Template_Should_Be_NameScope()
+        {
+            var control = new Decorator();
+            var template = new FuncTemplate<Canvas>(() => new Canvas());
+            var style = Mock.Of<IStyle>();
+            var setter = new Setter(Decorator.ChildProperty, template);
+
+            setter.Apply(style, control, null);
+
+            Assert.NotNull(NameScope.GetNameScope((Control)control.Child));
+        }
     }
 }