Browse Source

Fix: RelativePanel calculation error with extensible and aligned child (#19474)

* Feat: Add unit test for RelativePanel.
The ScrollViewer's scrollbar does not display correctly.

* Fix: RelativePanel calculation error
The ScrollViewer's scrollbar does not display correctly.
pull/19488/head
msojocs 6 months ago
committed by GitHub
parent
commit
990852383e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 24
      src/Avalonia.Controls/RelativePanel.cs
  2. 116
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs

24
src/Avalonia.Controls/RelativePanel.cs

@ -333,6 +333,22 @@ namespace Avalonia.Controls
: node.AlignBottomWithNode.Bottom * 0.5;
}
if (node.BelowNode != null)
{
if (node.Top.IsNaN())
{
node.Top = AvailableSize.Height - node.BelowNode.Bottom;
}
}
if (node.RightOfNode != null)
{
if (node.Left.IsNaN())
{
node.Left = AvailableSize.Width - node.RightOfNode.Right;
}
}
var availableHeight = AvailableSize.Height - node.Top - node.Bottom;
if (availableHeight.IsNaN())
{
@ -383,10 +399,6 @@ namespace Avalonia.Controls
node.Right = node.RightOfNode.Right - childSize.Width;
}
if (node.Left.IsNaN())
{
node.Left = AvailableSize.Width - node.RightOfNode.Right;
}
}
if (node.BelowNode != null)
@ -396,10 +408,6 @@ namespace Avalonia.Controls
node.Bottom = node.BelowNode.Bottom - childSize.Height;
}
if (node.Top.IsNaN())
{
node.Top = AvailableSize.Height - node.BelowNode.Bottom;
}
}
if (node.AlignHorizontalCenterWith != null)

116
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/RelativePanelTests.cs

@ -0,0 +1,116 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reactive.Subjects;
using System.Threading;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.UnitTests;
using Xunit;
namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{
public class RelativePanelTests : XamlTestBase
{
[Fact]
public void ScrollViewer_Viewport_Small_Than_Bounds()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
Height='800'
Width='1000'
>
<RelativePanel x:Name=""TestRelativePanel"">
<Panel
x:Name=""Area1""
RelativePanel.AlignTopWithPanel=""True""
RelativePanel.AlignLeftWithPanel=""True""
RelativePanel.AlignRightWithPanel=""True""
Height=""100""
Background=""LightSkyBlue"">
<TextBlock
Text=""Area1""
HorizontalAlignment=""Center""
VerticalAlignment=""Center""
/>
<!-- <Button Click=""Button_OnClick"">Second</Button> -->
</Panel>
<Panel
x:Name=""Area2""
RelativePanel.Below=""Area1""
RelativePanel.AlignLeftWithPanel=""True""
RelativePanel.AlignBottomWithPanel=""True""
Background=""DeepSkyBlue""
Width=""100""
>
<TextBlock
Text=""Area2""
HorizontalAlignment=""Center""
VerticalAlignment=""Center""
Height=""100""
/>
</Panel>
<ScrollViewer
x:Name=""TestArea""
Background=""Aqua""
RelativePanel.Below=""Area1""
RelativePanel.RightOf=""Area2""
RelativePanel.AlignRightWithPanel=""True""
RelativePanel.AlignBottomWithPanel=""True""
HorizontalScrollBarVisibility=""Visible""
Margin=""0 0 0 0""
>
<ItemsControl
ItemsSource=""{Binding DataExample}""
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=""Horizontal"">
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
<TextBlock Width=""100"" Text=""{Binding Id}""></TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</RelativePanel>
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
{
var panel = window.FindControl<RelativePanel>("TestRelativePanel");
panel.DataContext = new
{
DataExample = Enumerable.Range(1001, 100).Select(e => new { Id = $"{e}" })
};
}
window.ApplyTemplate();
window.Show();
var sv = window.FindControl<ScrollViewer>("TestArea");
Assert.True(sv.Viewport.Width < sv.Bounds.Width);
Assert.True(sv.Viewport.Height < sv.Bounds.Height);
}
}
}
}
Loading…
Cancel
Save