Browse Source

Fixed ScrollContentPresenter.

Did not react correctly to child changing.
pull/545/head
Steven Kirk 10 years ago
parent
commit
4c000b9ce4
  1. 18
      src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs
  2. 2
      tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj
  3. 50
      tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests_ILogicalScrollable.cs

18
src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs

@ -59,6 +59,7 @@ namespace Avalonia.Controls.Presenters
static ScrollContentPresenter()
{
ClipToBoundsProperty.OverrideDefaultValue(typeof(ScrollContentPresenter), true);
ChildProperty.Changed.AddClassHandler<ScrollContentPresenter>(x => x.ChildChanged);
AffectsArrange(OffsetProperty);
}
@ -258,6 +259,16 @@ namespace Avalonia.Controls.Presenters
e.Handled = BringDescendentIntoView(e.TargetObject, e.TargetRect);
}
private void ChildChanged(AvaloniaPropertyChangedEventArgs e)
{
UpdateScrollableSubscription((IControl)e.NewValue);
if (e.OldValue != null)
{
Offset = default(Vector);
}
}
private void UpdateScrollableSubscription(IControl child)
{
var scrollable = child as ILogicalScrollable;
@ -286,12 +297,7 @@ namespace Avalonia.Controls.Presenters
if (logicalScroll != scrollable.IsLogicalScrollEnabled)
{
UpdateScrollableSubscription(Child);
if (!scrollable.IsLogicalScrollEnabled)
{
Offset = default(Vector);
}
Offset = default(Vector);
InvalidateMeasure();
}

2
tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj

@ -109,7 +109,7 @@
<Compile Include="ListBoxTests_Single.cs" />
<Compile Include="NameScopeTests.cs" />
<Compile Include="Primitives\SelectingItemsControlTests_Multiple.cs" />
<Compile Include="Presenters\ScrollContentPresenterTests_IScrollable.cs" />
<Compile Include="Presenters\ScrollContentPresenterTests_ILogicalScrollable.cs" />
<Compile Include="TextBoxTests.cs" />
<Compile Include="TextBlockTests.cs" />
<Compile Include="WindowingPlatformMock.cs" />

50
tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests_ILogicalScrollable.cs

@ -236,6 +236,56 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
}
[Fact]
public void Changing_Content_Should_Update_State()
{
var logicalScrollable = new TestScrollable
{
Extent = new Size(100, 100),
Offset = new Vector(50, 50),
Viewport = new Size(25, 25),
};
var nonLogicalScrollable = new TestScrollable
{
IsLogicalScrollEnabled = false,
};
var target = new ScrollContentPresenter
{
Content = logicalScrollable,
};
target.UpdateChild();
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(logicalScrollable.Extent, target.Extent);
Assert.Equal(logicalScrollable.Offset, target.Offset);
Assert.Equal(logicalScrollable.Viewport, target.Viewport);
Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);
target.Content = nonLogicalScrollable;
target.UpdateChild();
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(new Size(150, 150), target.Extent);
Assert.Equal(new Vector(0, 0), target.Offset);
Assert.Equal(new Size(100, 100), target.Viewport);
Assert.Equal(new Rect(0, 0, 150, 150), nonLogicalScrollable.Bounds);
target.Content = logicalScrollable;
target.UpdateChild();
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(logicalScrollable.Extent, target.Extent);
Assert.Equal(logicalScrollable.Offset, target.Offset);
Assert.Equal(logicalScrollable.Viewport, target.Viewport);
Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);
}
private class TestScrollable : Control, ILogicalScrollable
{
private Size _extent;

Loading…
Cancel
Save