Skip to content

Commit

Permalink
chore: non-FE templated-parent injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jul 23, 2024
1 parent 08041e0 commit 1251899
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ private void ProcessType(INamedTypeSymbol typeSymbol)
AnalyzerSuppressionsGenerator.Generate(builder, _analyzerSuppressions);
};

var internalDependencyObject = _isUnoSolution && !typeSymbol.IsSealed ? ", IDependencyObjectInternal" : "";
using (typeSymbol.AddToIndentedStringBuilder(builder, beforeClassHeaderAction, afterClassHeader: $" : IDependencyObjectStoreProvider, IWeakReferenceProvider{internalDependencyObject}"))
var implementations = new string?[]
{
"IDependencyObjectStoreProvider",
_isUnoSolution && !typeSymbol.IsSealed ? "IDependencyObjectInternal" : null,
"ITemplatedParentProvider",
"IWeakReferenceProvider",
}.Where(x => x is not null);
using (typeSymbol.AddToIndentedStringBuilder(builder, beforeClassHeaderAction, afterClassHeader: " : " + string.Join(", ", implementations)))
{
AnalyzerSuppressionsGenerator.Generate(builder, _analyzerSuppressions);

Expand Down Expand Up @@ -774,33 +780,33 @@ private void GenerateDependencyObjectImplementation(INamedTypeSymbol typeSymbol,
}
}

//builder.AppendLineIndented("[EditorBrowsable(EditorBrowsableState.Never)]");
//using (builder.BlockInvariant($"DependencyObject IDependencyObjectInternal.TemplatedParent"))
//{
// builder.AppendLineIndented("get => GetTemplatedParent();");
// builder.AppendLineIndented("set => SetTemplatedParent(value);");
//}
var unoBrowsableOnly = _isUnoSolution ? null : "[EditorBrowsable(EditorBrowsableState.Never)]";

builder.AppendLineIndented($"public {(typeSymbol.IsSealed ? "" : "virtual")} DependencyObject GetTemplatedParent() => null;");
builder.AppendLine();
builder.AppendMultiLineIndented($$"""
{{unoBrowsableOnly}}private ManagedWeakReference _templatedParentWeakRef;
{{unoBrowsableOnly}}public ManagedWeakReference GetTemplatedParentWeakRef() => _templatedParentWeakRef;

builder.AppendMultiLineIndented("""
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetTemplatedParent(DependencyObject parent)
{{unoBrowsableOnly}}public DependencyObject GetTemplatedParent() => _templatedParentWeakRef?.Target as DependencyObject;
{{unoBrowsableOnly}}public void SetTemplatedParent(DependencyObject parent)
{
if (parent != null)
{
//global::System.Diagnostics.Debug.Assert(parent
// is global::Windows.UI.Xaml.Controls.Control
// or global::Windows.UI.Xaml.Controls.ContentPresenter
// or global::Windows.UI.Xaml.Controls.ItemsPresenter);
//global::System.Diagnostics.Debug.Assert(GetTemplatedParent() == null);
}
//if (parent != null)
//{
// global::System.Diagnostics.Debug.Assert(parent
// is global::Windows.UI.Xaml.Controls.Control
// or global::Windows.UI.Xaml.Controls.ContentPresenter
// or global::Windows.UI.Xaml.Controls.ItemsPresenter);
// global::System.Diagnostics.Debug.Assert(GetTemplatedParent() == null);
//}

SetTemplatedParentImpl(parent);
}
""");

builder.AppendLineIndented($"{(typeSymbol.IsSealed ? "private" : "private protected virtual")} void SetTemplatedParentImpl(DependencyObject parent) {{ }}");
{{unoBrowsableOnly}}{{(typeSymbol.IsSealed ? "private" : "private protected virtual")}} void SetTemplatedParentImpl(DependencyObject parent)
{
_templatedParentWeakRef = (parent as IWeakReferenceProvider)?.WeakReference;
}
"""
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3045,7 +3045,8 @@ private void BuildExtendedProperties(IIndentedStringBuilder outerwriter, XamlObj
}

var isInsideFrameworkTemplate = IsMemberInsideFrameworkTemplate(objectDefinition).isInside;
if (isInsideFrameworkTemplate && isFrameworkElement)
var isDependencyObject = IsType(objectDefinitionType, Generation.DependencyObjectSymbol.Value);
if (isInsideFrameworkTemplate && isDependencyObject)
{
writer.AppendLineIndented($"{closureName}.SetTemplatedParent(__settings?.TemplatedParent);");
writer.AppendLineIndented($"__settings?.TemplateMemberCreatedCallback?.Invoke({closureName});");
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/DataBinding/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public object DataContext
{
if (ParentBinding.IsTemplateBinding)
{
return (_view?.Target as FrameworkElement)?.GetTemplatedParent();
return (_view?.Target as ITemplatedParentProvider)?.GetTemplatedParent();
}
if (_isElementNameSource || ExplicitSource != null)
{
Expand Down Expand Up @@ -149,7 +149,7 @@ Binding binding
ApplyElementName();
}

private ManagedWeakReference GetWeakTemplatedParent() => (_view?.Target as FrameworkElement)?.GetTemplatedParentWeakRef();
private ManagedWeakReference GetWeakTemplatedParent() => (_view?.Target as ITemplatedParentProvider)?.GetTemplatedParentWeakRef();

private ManagedWeakReference GetWeakDataContext()
{
Expand Down
19 changes: 19 additions & 0 deletions src/Uno.UI/DataBinding/ITemplatedParentProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;

namespace Uno.UI.DataBinding;

[EditorBrowsable(EditorBrowsableState.Never)]
public interface ITemplatedParentProvider
{
ManagedWeakReference GetTemplatedParentWeakRef();

DependencyObject GetTemplatedParent();

void SetTemplatedParent(DependencyObject parent);
}
10 changes: 0 additions & 10 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,6 @@ public bool IsParsing
}
}

internal bool IsTemplateRoot { get; set; } // fixme@xy: remove debug code

private ManagedWeakReference _templatedParentWeakRef;
internal ManagedWeakReference GetTemplatedParentWeakRef() => _templatedParentWeakRef;
public override DependencyObject GetTemplatedParent() => _templatedParentWeakRef?.Target as DependencyObject;
private protected override void SetTemplatedParentImpl(DependencyObject parent)
{
_templatedParentWeakRef = (parent as IWeakReferenceProvider)?.WeakReference;
}

/// <summary>
/// Provides the behavior for the "Measure" pass of the layout cycle. Classes can override this method to define their own "Measure" pass behavior.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Uno.UI/UI/Xaml/FrameworkTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public FrameworkTemplate(object? owner, FrameworkTemplateBuilder? factory)
FrameworkTemplatePool.Instance.TrackMaterializedTemplate(this, view, members);
}

if (view is FrameworkElement fe)
{
fe.IsTemplateRoot = true;
}
//if (view is FrameworkElement fe)
//{
// fe.IsTemplateRoot = true;
//}

return view;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/IDependencyObjectInternal.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Microsoft.UI.Xaml
{
/// <summary>
/// Internal implemenation for <see cref="DependencyObject"/>
/// Internal implementation for <see cref="DependencyObject"/>
/// </summary>
internal partial interface IDependencyObjectInternal
{
Expand Down

0 comments on commit 1251899

Please sign in to comment.