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

Raise the EffectiveViewPortChanged when a control is added to the tree that has already has a valid layout #17570

Merged
merged 5 commits into from
Nov 26, 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
9 changes: 3 additions & 6 deletions src/Avalonia.Base/Layout/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ public void Dispose()
void ILayoutManager.RegisterEffectiveViewportListener(Layoutable control)
{
_effectiveViewportChangedListeners ??= new List<EffectiveViewportChangedListener>();
_effectiveViewportChangedListeners.Add(new EffectiveViewportChangedListener(
control,
CalculateEffectiveViewport(control)));
_effectiveViewportChangedListeners.Add(new EffectiveViewportChangedListener(control));
}

void ILayoutManager.UnregisterEffectiveViewportListener(Layoutable control)
Expand Down Expand Up @@ -438,14 +436,13 @@ private void CalculateEffectiveViewport(Visual target, Visual control, ref Rect

private class EffectiveViewportChangedListener
{
public EffectiveViewportChangedListener(Layoutable listener, Rect viewport)
public EffectiveViewportChangedListener(Layoutable listener)
{
Listener = listener;
Viewport = viewport;
}

public Layoutable Listener { get; }
public Rect Viewport { get; set; }
public Rect? Viewport { get; set; }
}

private enum ArrangeResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Avalonia.Base.UnitTests.Layout
public class LayoutableTests_EffectiveViewportChanged
{
[Fact]
public async Task EffectiveViewportChanged_Not_Raised_When_Control_Added_To_Tree()
public async Task EffectiveViewportChanged_Not_Raised_When_Control_Added_To_Tree_And_Layout_Pass_Has_Not_Run()
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
await RunOnUIThread.Execute(async () =>
Expand All @@ -34,6 +34,60 @@ await RunOnUIThread.Execute(async () =>
Assert.Equal(0, raised);
});
}

[Fact]
public async Task EffectiveViewportChanged_Raised_When_Control_Added_To_Tree_And_Layout_Pass_Has_Run()
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
await RunOnUIThread.Execute(async () =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
var root = CreateRoot();
var target = new Canvas();
var raised = 0;

target.EffectiveViewportChanged += (s, e) =>
{
++raised;
};

root.Child = target;

Assert.Equal(0, raised);

await ExecuteInitialLayoutPass(root);

Assert.Equal(1, raised);
});
}

[Fact]
public async Task EffectiveViewportChanged_Raised_When_Root_LayedOut_And_Then_Control_Added_To_Tree_And_Layout_Pass_Runs()
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
await RunOnUIThread.Execute(async () =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
var root = CreateRoot();
var target = new Canvas();
var raised = 0;

target.EffectiveViewportChanged += (s, e) =>
{
++raised;
};

await ExecuteInitialLayoutPass(root);

root.Child = target;

Assert.Equal(0, raised);

await ExecuteInitialLayoutPass(root);

Assert.Equal(1, raised);
});
}

[Fact]
public async Task EffectiveViewportChanged_Raised_Before_LayoutUpdated()
Expand Down Expand Up @@ -268,8 +322,11 @@ await RunOnUIThread.Execute(async () =>

root.Child = parent;

await ExecuteInitialLayoutPass(root);
target.EffectiveViewportChanged += (s, e) => ++raised;
await ExecuteInitialLayoutPass(root);

raised = 0; // The initial layout pass is expected to raise.

target.RenderTransform = new TranslateTransform { X = 8 };
target.InvalidateMeasure();
await ExecuteLayoutPass(root);
Expand Down
Loading