Skip to content

[TransitioningContentControl] Manage his LogicalChildren #12173

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

Merged
merged 9 commits into from
Aug 15, 2023
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
15 changes: 10 additions & 5 deletions src/Avalonia.Controls/ContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public class ContentControl : TemplatedControl, IContentControl, IContentPresent
public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
AvaloniaProperty.Register<ContentControl, VerticalAlignment>(nameof(VerticalContentAlignment));

static ContentControl()
{
ContentProperty.Changed.AddClassHandler<ContentControl>((x, e) => x.ContentChanged(e));
}

/// <summary>
/// Gets or sets the content to display.
/// </summary>
Expand Down Expand Up @@ -100,6 +95,16 @@ bool IContentPresenterHost.RegisterContentPresenter(ContentPresenter presenter)
{
return RegisterContentPresenter(presenter);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == ContentProperty)
{
ContentChanged(change);
}
}

/// <summary>
/// Called when an <see cref="ContentPresenter"/> is registered with the control.
Expand Down
13 changes: 9 additions & 4 deletions src/Avalonia.Controls/TransitioningContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

protected override bool RegisterContentPresenter(ContentPresenter presenter)
{
if (!base.RegisterContentPresenter(presenter) &&
presenter is ContentPresenter p &&
if (base.RegisterContentPresenter(presenter))
{
return true;
}

if (presenter is ContentPresenter p &&
p.Name == "PART_ContentPresenter2")
{
_presenter2 = p;
Expand All @@ -94,12 +98,13 @@ presenter is ContentPresenter p &&

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == ContentProperty)
{
UpdateContent(true);
return;
}

base.OnPropertyChanged(change);
}

private void UpdateContent(bool withTransition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ public void New_Transition_Should_Be_Started_If_Content_Changes_While_Running()
Assert.Equal("bar", presenter2.Content);
}

[Fact]
public void Logical_Children_Should_Not_Be_Duplicated()
{
using var app = Start();
var (target, transition) = CreateTarget("");
target.PageTransition = null;

var childControl = new Control();
target.Content = childControl;

Assert.Equal(1, target.LogicalChildren.Count);
Assert.Equal(target.LogicalChildren[0], childControl);
}

[Fact]
public void First_Presenter_Should_Register_TCC_As_His_Host()
{
using var app = Start();
var (target, transition) = CreateTarget("");
target.PageTransition = null;

var childControl = new Control();
target.Presenter!.Content = childControl;

Assert.Equal(1, target.LogicalChildren.Count);
Assert.Equal(target.LogicalChildren[0], childControl);
}

private static IDisposable Start()
{
return UnitTestApplication.Start(
Expand Down