Browse Source
Merge pull request #12093 from AvaloniaUI/fix-window-incorrect-positioning-with-windowStartupLocation-centerScreen
Fix window incorrect positioning with window startup location CenterScreen
pull/12261/head
Max Katz
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
42 additions and
0 deletions
-
src/Avalonia.Controls/Window.cs
-
tests/Avalonia.Controls.UnitTests/WindowTests.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 && |
|
|
|
|
|
|
|
@ -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() |
|
|
|
{ |
|
|
|
|