Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(netstd): late template application is ignored #18944

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,40 @@ public async Task VisualStateGroup_TP_Inheritance()
""";
VerifyTree(expectations, setup, checkVSG: true);
}

[TestMethod]
public Task LateTemplateSwapping_NonContentControl() => LateTemplateSwapping<TextBox>();

[TestMethod]
public Task LateTemplateSwapping_ContentControl() => LateTemplateSwapping<ContentControl>();

public async Task LateTemplateSwapping<TControl>() where TControl : Control, new()
{
var templateA = XamlHelper.LoadXaml<ControlTemplate>("""
<ControlTemplate>
<Grid x:Name="RootA" Width="150" Height="50" Background="SkyBlue">
<TextBlock>Template A</TextBlock>
</Grid>
</ControlTemplate>
""");
var templateB = XamlHelper.LoadXaml<ControlTemplate>("""
<ControlTemplate>
<Grid x:Name="RootB" Width="150" Height="50" Background="Pink">
<TextBlock>Template B</TextBlock>
</Grid>
</ControlTemplate>
""");

var sut = new TControl();

sut.Template = templateA;
await UITestHelper.Load(sut, x => x.IsLoaded);
sut.FindFirstDescendantOrThrow<Grid>("RootA");

sut.Template = templateB;
await UITestHelper.WaitForIdle();
sut.FindFirstDescendantOrThrow<Grid>("RootB");
}
}
public partial class TemplatedParentTests // helper methods
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ private void RemoveTemplateChild()
}
#endif

private UIElement GetFirstChildNoAddRef() => GetFirstChild();

private UIElement GetFirstChild()
internal override UIElement GetFirstChild()
{
UIElement spFirstChild;
// added in UIElement.GetFirstChild()
Expand Down
25 changes: 24 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/Control/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,30 @@ public ControlTemplate Template

private protected virtual void OnTemplateChanged(DependencyPropertyChangedEventArgs e)
{
#if !UNO_HAS_ENHANCED_LIFECYCLE
#if UNO_HAS_ENHANCED_LIFECYCLE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF we are making template application closer and closer to WinUI on Skia and Wasm, then I think TemplateProperty should have AffectsMeasure flag.

We are not 100% matching WinUI though, so I'm not sure if the flag can have a bad effect currently.

But basically after the child removal, the way the template should re-apply is through the template application that happens during measure, which is why WinUI has AffectsMeasure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, adding a template will automatically incur a measure invalidation, since it's adding a frameworkelement to the tree that will itself request a measure.

Copy link
Member

@Youssef1313 Youssef1313 Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeromelaban I don't think that's correct.

Changing the template doesn't directly add a FrameworkElement to the visual tree. The template is applied (or should be applied) during measure and this is when the child is added to the tree (we do it during measure for Skia/Wasm already).

At least, this is how it works in WinUI. And as we are starting to align it with WinUI, we should do the same.

And yes, we are now removing a child which will likely invalidate measure anyways, but there can be really very weird corner cases where we still need to invalidate measure (e.g, first child is null already somehow but measure was done already), I think. It's safer to align with WinUI code anyways. Note that this can never be an extra invalidation. If the new code added here already removed a child, then invalidate measure will happen anyway. If it didn't remove a child in some edge case, then the only way for the template to be applied is to invalidate the measure. So it's safe and more correct to have AffectsMeasure on TemplateProperty (at least on Skia and Wasm. The other platforms do very weird things)

if (e.OldValue != e.NewValue)
{
// Reset the template bindings for this control
//ClearPropertySubscriptions();

// When the control template property is set, we clear the visual children
var pUIElement = this.GetFirstChild();
if (pUIElement is { })
{
//CFrameworkTemplate* pNewTemplate = NULL;
//if (e.NewValue?.GetType() == valueObject)
//{
// IFC(DoPointerCast(pNewTemplate, args.m_value.AsObject()));
//}
//else if (args.m_value.GetType() != valueNull)
//{
// IFC(E_INVALIDARG);
//}
RemoveChild(pUIElement);
//IFC(GetContext()->RemoveNameScope(this, Jupiter::NameScoping::NameScopeType::TemplateNameScope));
}
}
#else
_updateTemplate = true;
SetUpdateControlTemplate();
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,9 @@ private protected virtual bool HasTemplateChild()
return GetFirstChild() is not null;
}

private UIElement/*?*/ GetFirstChild()
internal UIElement GetFirstChildNoAddRef() => GetFirstChild();

internal virtual UIElement/*?*/ GetFirstChild()
{
#if __CROSSRUNTIME__ && !__NETSTD_REFERENCE__
if (GetChildren() is { Count: > 0 } children)
Expand Down
Loading