Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix window incorrect positioning with window startup location CenterScreen #12093

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions samples/Sandbox/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<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>
</Window>
7 changes: 7 additions & 0 deletions src/Avalonia.Controls/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public class Window : WindowBase, IFocusScope, ILayoutRoot
private readonly Size _maxPlatformClientSize;
private bool _shown;
private bool _showingAsDialog;
private bool _wasShownBefore;

/// <summary>
/// Initializes static members of the <see cref="Window"/> class.
Expand Down Expand Up @@ -718,6 +719,7 @@ private void ShowCore(Window? owner)
StartRendering();
PlatformImpl?.Show(ShowActivated, false);
OnOpened(EventArgs.Empty);
_wasShownBefore = true;
}
}

Expand Down Expand Up @@ -871,6 +873,11 @@ private void OnGotInputWhenDisabled()

private void SetWindowStartupLocation(Window? owner = null)
{
if (_wasShownBefore == true)
{
return;
}

var startupLocation = WindowStartupLocation;

if (startupLocation == WindowStartupLocation.CenterOwner &&
Expand Down
35 changes: 35 additions & 0 deletions tests/Avalonia.Controls.UnitTests/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,41 @@ public void Hiding_Parent_Window_Should_Close_Dialog_Children()
}
}

[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()
{
Expand Down