Skip to content

Commit

Permalink
Remove ControlTemplateResult.
Browse files Browse the repository at this point in the history
Use `TempateResult<Control>` instead as described in #6666.

Fixes #10525.
  • Loading branch information
grokys committed Apr 18, 2023
1 parent 7158628 commit 3882bc0
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Avalonia.Controls/Templates/FuncControlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public FuncControlTemplate(Func<TemplatedControl, INameScope, Control> build)
{
}

public new ControlTemplateResult Build(TemplatedControl param)
public new TemplateResult<Control> Build(TemplatedControl param)
{
var (control, scope) = BuildWithNameScope(param);
return new ControlTemplateResult(control, scope);
return new(control, scope);
}
}
}
18 changes: 1 addition & 17 deletions src/Avalonia.Controls/Templates/IControlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@ namespace Avalonia.Controls.Templates
/// <summary>
/// Interface representing a template used to build a <see cref="TemplatedControl"/>.
/// </summary>
public interface IControlTemplate : ITemplate<TemplatedControl, ControlTemplateResult?>
public interface IControlTemplate : ITemplate<TemplatedControl, TemplateResult<Control>?>
{
}

public class ControlTemplateResult : TemplateResult<Control>
{
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;
}
}
}
3 changes: 2 additions & 1 deletion src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Metadata;
Expand All @@ -13,6 +14,6 @@ public class ControlTemplate : IControlTemplate

public Type? TargetType { get; set; }

public ControlTemplateResult? Build(TemplatedControl control) => TemplateContent.Load(Content);
public TemplateResult<Control>? Build(TemplatedControl control) => TemplateContent.Load(Content);
}
}
2 changes: 1 addition & 1 deletion src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ItemsPanelTemplate : ITemplate<Panel?>
[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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Markup/Avalonia.Markup.Xaml/Templates/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Template : ITemplate<Control?>
[TemplateContent]
public object? Content { get; set; }

public Control? Build() => TemplateContent.Load(Content)?.Control;
public Control? Build() => TemplateContent.Load(Content)?.Result;

object? ITemplate.Build() => Build();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs
Original file line number Diff line number Diff line change
@@ -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<Control>? Load(object? templateContent)
{
if (templateContent is Func<IServiceProvider?, object?> direct)
{
return (ControlTemplateResult?)direct(null);
return (TemplateResult<Control>?)direct(null);
}

if (templateContent is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static Func<IServiceProvider, object> DeferredTransformationFactoryV2<T>(
scope.Complete();

if(typeof(T) == typeof(Control))
return new ControlTemplateResult((Control)obj, scope);
return new TemplateResult<Control>((Control)obj, scope);

return new TemplateResult<T>((T)obj, scope);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down
2 changes: 1 addition & 1 deletion tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void ControlTemplate_With_Nested_Child_Is_Operational()
";
var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);

var parent = (ContentControl)template.Build(new ContentControl()).Control;
var parent = (ContentControl)template.Build(new ContentControl()).Result;

Assert.Equal("parent", parent.Name);

Expand All @@ -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]
Expand All @@ -299,7 +299,7 @@ public void ControlTemplate_With_Panel_Children_Are_Added()
";
var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);

var panel = (Panel)template.Build(new ContentControl()).Control;
var panel = (Panel)template.Build(new ContentControl()).Result;

Assert.Equal(2, panel.Children.Count);

Expand Down

0 comments on commit 3882bc0

Please sign in to comment.