diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index ce4fc8dd5c..dc58947994 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/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; /// /// Initializes static members of the 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 && diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index cd45a3d920..186b2f6836 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/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(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true); + + var screens = new Mock(); + screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object }); + screens.Setup(x => x.ScreenFromPoint(It.IsAny())).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() {