Browse Source

Do not set Window Startup Location multiple times

pull/12093/head
Daniil Pavliuchyk 3 years ago
parent
commit
0e038492f7
  1. 8
      samples/Sandbox/MainWindow.axaml
  2. 7
      src/Avalonia.Controls/Window.cs
  3. 35
      tests/Avalonia.Controls.UnitTests/WindowTests.cs

8
samples/Sandbox/MainWindow.axaml

@ -1,11 +1,5 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow">
<ScrollViewer>
<StackPanel>
<Button Margin="0 100000000000000000 0 0">0</Button>
<Button>1</Button>
</StackPanel>
</ScrollViewer>
<Button Name="dwwdw">dewwddwdwwd</Button>
</Window>

7
src/Avalonia.Controls/Window.cs

@ -169,6 +169,7 @@ namespace Avalonia.Controls
private readonly Size _maxPlatformClientSize;
private bool _shown;
private bool _showingAsDialog;
private bool _wasShownBefore;
/// <summary>
/// Initializes static members of the <see cref="Window"/> class.
@ -718,6 +719,7 @@ namespace Avalonia.Controls
StartRendering();
PlatformImpl?.Show(ShowActivated, false);
OnOpened(EventArgs.Empty);
_wasShownBefore = true;
}
}
@ -871,6 +873,11 @@ namespace Avalonia.Controls
private void SetWindowStartupLocation(Window? owner = null)
{
if (_wasShownBefore == true)
{
return;
}
var startupLocation = WindowStartupLocation;
if (startupLocation == WindowStartupLocation.CenterOwner &&

35
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@ -513,6 +513,41 @@ namespace Avalonia.Controls.UnitTests
}
}
[Fact]
public void Window_Should_Not_Be_Centered_When_WindowStartupLocation_Is_CenterScreen_And_Window_Is_Hidden_And_Shown()
{
var screen1 = new Mock<Screen>(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true);
var screens = new Mock<IScreenImpl>();
screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object });
screens.Setup(x => x.ScreenFromPoint(It.IsAny<PixelPoint>())).Returns(screen1.Object);
var windowImpl = MockWindowingPlatform.CreateWindowMock();
windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
windowImpl.Setup(x => x.DesktopScaling).Returns(1);
windowImpl.Setup(x => x.RenderScaling).Returns(1);
windowImpl.Setup(x => x.Screen).Returns(screens.Object);
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = new Window(windowImpl.Object)
{
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
window.Show();
var expected = new PixelPoint(150, 400);
window.Position = expected;
window.IsVisible = false;
window.IsVisible = true;
Assert.Equal(expected, window.Position);
}
}
[Fact]
public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
{

Loading…
Cancel
Save