From 3882bc021739e2623953a65a7fa33962181ee62b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 18 Apr 2023 11:27:21 +0200 Subject: [PATCH] Remove ControlTemplateResult. Use `TempateResult` instead as described in #6666. Fixes #10525. --- .../Templates/FuncControlTemplate.cs | 4 ++-- .../Templates/IControlTemplate.cs | 18 +----------------- .../Templates/ControlTemplate.cs | 3 ++- .../Templates/DataTemplate.cs | 2 +- .../Templates/ItemsPanelTemplate.cs | 2 +- .../Avalonia.Markup.Xaml/Templates/Template.cs | 2 +- .../Templates/TemplateContent.cs | 5 +++-- .../Templates/TreeDataTemplate.cs | 2 +- .../XamlIl/Runtime/XamlIlRuntimeHelpers.cs | 2 +- .../CompiledBindingExtensionTests.cs | 2 +- .../Xaml/BasicTests.cs | 2 +- .../Xaml/ControlTemplateTests.cs | 6 +++--- 12 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/Avalonia.Controls/Templates/FuncControlTemplate.cs b/src/Avalonia.Controls/Templates/FuncControlTemplate.cs index 64a883e88c6..895ce53907a 100644 --- a/src/Avalonia.Controls/Templates/FuncControlTemplate.cs +++ b/src/Avalonia.Controls/Templates/FuncControlTemplate.cs @@ -18,10 +18,10 @@ public FuncControlTemplate(Func build) { } - public new ControlTemplateResult Build(TemplatedControl param) + public new TemplateResult Build(TemplatedControl param) { var (control, scope) = BuildWithNameScope(param); - return new ControlTemplateResult(control, scope); + return new(control, scope); } } } diff --git a/src/Avalonia.Controls/Templates/IControlTemplate.cs b/src/Avalonia.Controls/Templates/IControlTemplate.cs index 38ad6561abb..c3f9c9e8aab 100644 --- a/src/Avalonia.Controls/Templates/IControlTemplate.cs +++ b/src/Avalonia.Controls/Templates/IControlTemplate.cs @@ -5,23 +5,7 @@ namespace Avalonia.Controls.Templates /// /// Interface representing a template used to build a . /// - public interface IControlTemplate : ITemplate + public interface IControlTemplate : ITemplate?> { } - - public class ControlTemplateResult : TemplateResult - { - public Control Control { get; } - - public ControlTemplateResult(Control control, INameScope nameScope) : base(control, nameScope) - { - Control = control; - } - - public new void Deconstruct(out Control control, out INameScope scope) - { - control = Control; - scope = NameScope; - } - } } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs index 4bbdda31d8a..b94eccf7c06 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs @@ -1,4 +1,5 @@ using System; +using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Metadata; @@ -13,6 +14,6 @@ public class ControlTemplate : IControlTemplate public Type? TargetType { get; set; } - public ControlTemplateResult? Build(TemplatedControl control) => TemplateContent.Load(Content); + public TemplateResult? Build(TemplatedControl control) => TemplateContent.Load(Content); } } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs index 89b0468c6ee..b45898d8bd3 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs @@ -30,7 +30,7 @@ public bool Match(object? data) public Control? Build(object? data, Control? existing) { - return existing ?? TemplateContent.Load(Content)?.Control; + return existing ?? TemplateContent.Load(Content)?.Result; } } } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/ItemsPanelTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/ItemsPanelTemplate.cs index c228a58990e..f31a693e725 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/ItemsPanelTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/ItemsPanelTemplate.cs @@ -10,7 +10,7 @@ public class ItemsPanelTemplate : ITemplate [TemplateContent] public object? Content { get; set; } - public Panel? Build() => (Panel?)TemplateContent.Load(Content)?.Control; + public Panel? Build() => (Panel?)TemplateContent.Load(Content)?.Result; object? ITemplate.Build() => Build(); } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/Template.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/Template.cs index 62febebc8cb..5999a8021e1 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/Template.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/Template.cs @@ -10,7 +10,7 @@ public class Template : ITemplate [TemplateContent] public object? Content { get; set; } - public Control? Build() => TemplateContent.Load(Content)?.Control; + public Control? Build() => TemplateContent.Load(Content)?.Result; object? ITemplate.Build() => Build(); } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs index 08e897c5140..504478f9b34 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs @@ -1,15 +1,16 @@ using System; +using Avalonia.Controls; using Avalonia.Controls.Templates; namespace Avalonia.Markup.Xaml.Templates { public static class TemplateContent { - public static ControlTemplateResult? Load(object? templateContent) + public static TemplateResult? Load(object? templateContent) { if (templateContent is Func direct) { - return (ControlTemplateResult?)direct(null); + return (TemplateResult?)direct(null); } if (templateContent is null) diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs index a5b308523f5..98c3b61c9f1 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs @@ -54,7 +54,7 @@ public bool Match(object? data) public Control? Build(object? data) { - var visualTreeForItem = TemplateContent.Load(Content)?.Control; + var visualTreeForItem = TemplateContent.Load(Content)?.Result; if (visualTreeForItem != null) { visualTreeForItem.DataContext = data; diff --git a/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs b/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs index ba96ac15b37..0cc7cc54680 100644 --- a/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs +++ b/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs @@ -35,7 +35,7 @@ public static Func DeferredTransformationFactoryV2( scope.Complete(); if(typeof(T) == typeof(Control)) - return new ControlTemplateResult((Control)obj, scope); + return new TemplateResult((Control)obj, scope); return new TemplateResult((T)obj, scope); }; diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs index 57d6a8902a1..9f0b84733da 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs @@ -1978,7 +1978,7 @@ public class CustomDataTemplate : IDataTemplate public bool Match(object data) => FancyDataType?.IsInstanceOfType(data) ?? true; - public Control Build(object data) => TemplateContent.Load(Content)?.Control; + public Control Build(object data) => TemplateContent.Load(Content)?.Result; } public class CustomDataTemplateInherit : CustomDataTemplate { } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs index 5e30198d00d..421ed2c9795 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs @@ -605,7 +605,7 @@ public void Style_ControlTemplate_Is_Built() var control = new ContentControl(); - var result = (ContentPresenter)template.Build(control).Control; + var result = (ContentPresenter)template.Build(control).Result; Assert.NotNull(result); } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs index 0a45814efee..44045647338 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs @@ -258,7 +258,7 @@ public void ControlTemplate_With_Nested_Child_Is_Operational() "; var template = AvaloniaRuntimeXamlLoader.Parse(xaml); - var parent = (ContentControl)template.Build(new ContentControl()).Control; + var parent = (ContentControl)template.Build(new ContentControl()).Result; Assert.Equal("parent", parent.Name); @@ -283,7 +283,7 @@ public void ControlTemplate_With_TargetType_Is_Operational() Assert.Equal(typeof(ContentControl), template.TargetType); - Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Control); + Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Result); } [Fact] @@ -299,7 +299,7 @@ public void ControlTemplate_With_Panel_Children_Are_Added() "; var template = AvaloniaRuntimeXamlLoader.Parse(xaml); - var panel = (Panel)template.Build(new ContentControl()).Control; + var panel = (Panel)template.Build(new ContentControl()).Result; Assert.Equal(2, panel.Children.Count);