Browse Source

Assert control invalidation behavior.

Controls not attached to the visual tree should not notify the `LayoutManager` that they have had their layout invalidated. Similarly when added to the visual tree their parents and themselves should have their layout invalidated.
pull/1021/head
Steven Kirk 9 years ago
parent
commit
40c342989b
  1. 28
      src/Avalonia.Layout/LayoutManager.cs
  2. 16
      src/Avalonia.Layout/Layoutable.cs
  3. 90
      tests/Avalonia.Layout.UnitTests/LayoutableTests.cs

28
src/Avalonia.Layout/LayoutManager.cs

@ -30,12 +30,19 @@ namespace Avalonia.Layout
Contract.Requires<ArgumentNullException>(control != null);
Dispatcher.UIThread.VerifyAccess();
if (control.IsAttachedToVisualTree)
if (!control.IsAttachedToVisualTree)
{
_toMeasure.Enqueue(control);
_toArrange.Enqueue(control);
QueueLayoutPass();
#if DEBUG
throw new AvaloniaInternalException(
"LayoutManager.InvalidateMeasure called on a control that is detached from the visual tree.");
#else
return;
#endif
}
_toMeasure.Enqueue(control);
_toArrange.Enqueue(control);
QueueLayoutPass();
}
/// <inheritdoc/>
@ -44,11 +51,18 @@ namespace Avalonia.Layout
Contract.Requires<ArgumentNullException>(control != null);
Dispatcher.UIThread.VerifyAccess();
if (control.IsAttachedToVisualTree)
if (!control.IsAttachedToVisualTree)
{
_toArrange.Enqueue(control);
QueueLayoutPass();
#if DEBUG
throw new AvaloniaInternalException(
"LayoutManager.InvalidateArrange called on a control that is detached from the visual tree.");
#else
return;
#endif
}
_toArrange.Enqueue(control);
QueueLayoutPass();
}
/// <inheritdoc/>

16
src/Avalonia.Layout/Layoutable.cs

@ -378,8 +378,12 @@ namespace Avalonia.Layout
IsMeasureValid = false;
IsArrangeValid = false;
LayoutManager.Instance?.InvalidateMeasure(this);
InvalidateVisual();
if (((ILayoutable)this).IsAttachedToVisualTree)
{
LayoutManager.Instance?.InvalidateMeasure(this);
InvalidateVisual();
}
}
}
@ -393,8 +397,12 @@ namespace Avalonia.Layout
Logger.Verbose(LogArea.Layout, this, "Invalidated arrange");
IsArrangeValid = false;
LayoutManager.Instance?.InvalidateArrange(this);
InvalidateVisual();
if (((ILayoutable)this).IsAttachedToVisualTree)
{
LayoutManager.Instance?.InvalidateArrange(this);
InvalidateVisual();
}
}
}

90
tests/Avalonia.Layout.UnitTests/LayoutableTests.cs

@ -0,0 +1,90 @@
using System;
using Avalonia.Controls;
using Moq;
using Xunit;
namespace Avalonia.Layout.UnitTests
{
public class LayoutableTests
{
[Fact]
public void Only_Calls_LayoutManager_InvalidateMeasure_Once()
{
var target = new Mock<ILayoutManager>();
using (Start(target.Object))
{
var control = new Decorator();
var root = new LayoutTestRoot { Child = control };
root.Measure(Size.Infinity);
root.Arrange(new Rect(root.DesiredSize));
target.ResetCalls();
control.InvalidateMeasure();
control.InvalidateMeasure();
target.Verify(x => x.InvalidateMeasure(control), Times.Once());
}
}
[Fact]
public void Only_Calls_LayoutManager_InvalidateArrange_Once()
{
var target = new Mock<ILayoutManager>();
using (Start(target.Object))
{
var control = new Decorator();
var root = new LayoutTestRoot { Child = control };
root.Measure(Size.Infinity);
root.Arrange(new Rect(root.DesiredSize));
target.ResetCalls();
control.InvalidateArrange();
control.InvalidateArrange();
target.Verify(x => x.InvalidateArrange(control), Times.Once());
}
}
[Fact]
public void Attaching_Control_To_Tree_Invalidates_Parent_Measure()
{
var target = new Mock<ILayoutManager>();
using (Start(target.Object))
{
var control = new Decorator();
var root = new LayoutTestRoot { Child = control };
root.Measure(Size.Infinity);
root.Arrange(new Rect(root.DesiredSize));
Assert.True(control.IsMeasureValid);
root.Child = null;
root.Measure(Size.Infinity);
root.Arrange(new Rect(root.DesiredSize));
Assert.False(control.IsMeasureValid);
Assert.True(root.IsMeasureValid);
target.ResetCalls();
root.Child = control;
Assert.False(root.IsMeasureValid);
Assert.False(control.IsMeasureValid);
target.Verify(x => x.InvalidateMeasure(root), Times.Once());
}
}
private IDisposable Start(ILayoutManager layoutManager)
{
var result = AvaloniaLocator.EnterScope();
AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(layoutManager);
return result;
}
}
}
Loading…
Cancel
Save