Browse Source

Use Screen API.

pull/1044/head
José Pedro 9 years ago
parent
commit
2cc4b41acc
  1. 2
      src/Avalonia.Controls/Screens.cs
  2. 11
      src/Avalonia.Controls/Window.cs
  3. 4
      src/Windows/Avalonia.Win32/ScreenImpl.cs
  4. 18
      tests/Avalonia.Controls.UnitTests/WindowTests.cs

2
src/Avalonia.Controls/Screens.cs

@ -39,7 +39,7 @@ namespace Avalonia.Controls
return currMaxScreen;
}
public Screen SceenFromPoint(Point point)
public Screen ScreenFromPoint(Point point)
{
return All.FirstOrDefault(x=>x.Bounds.Contains(point));
}

11
src/Avalonia.Controls/Window.cs

@ -376,13 +376,10 @@ namespace Avalonia.Controls
{
if (WindowStartupLocation == WindowStartupLocation.CenterScreen)
{
// This should be using a Screen API, but we don't have one yet and
// PlatformImpl.MaxClientSize is the best we have.
if (PlatformImpl != null)
{
var positionAsSize = PlatformImpl.MaxClientSize / 2 - ClientSize / 2;
Position = new Point(positionAsSize.Width, positionAsSize.Height);
}
var screen = Screens.ScreenFromPoint(Bounds.Position);
if (screen != null)
Position = screen.WorkingArea.CenterRect(new Rect(ClientSize)).Position;
}
else if (WindowStartupLocation == WindowStartupLocation.CenterOwner)
{

4
src/Windows/Avalonia.Win32/ScreenImpl.cs

@ -41,8 +41,8 @@ namespace Avalonia.Win32
Rect avaloniaBounds = new Rect(bounds.left, bounds.top, bounds.right - bounds.left,
bounds.bottom - bounds.top);
Rect avaloniaWorkArea =
new Rect(workingArea.left, workingArea.top, workingArea.right - bounds.left,
workingArea.bottom - bounds.top);
new Rect(workingArea.left, workingArea.top, workingArea.right - workingArea.left,
workingArea.bottom - workingArea.top);
screens[index] =
new WinScreen(avaloniaBounds, avaloniaWorkArea, monitorInfo.dwFlags == 1,
monitor);

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

@ -239,32 +239,38 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void Window_Should_Be_Centered_When_Window_Startup_Location_Is_Center_Screen()
public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
{
var screen1 = new Mock<Screen>(new Rect(new Size(1920, 1080)), new Rect(new Size(1920, 1040)), true);
var screen2 = new Mock<Screen>(new Rect(new Size(1366, 768)), new Rect(new Size(1366, 728)), false);
var screens = new Mock<IScreenImpl>();
screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });
var windowImpl = new Mock<IWindowImpl>();
windowImpl.SetupProperty(x => x.Position);
windowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
windowImpl.Setup(x => x.Scaling).Returns(1);
windowImpl.Setup(x => x.Screen).Returns(screens.Object);
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = new Window();
var window = new Window(windowImpl.Object);
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.Position = new Point(60, 40);
window.Show();
var expectedPosition = new Point(
window.PlatformImpl.MaxClientSize.Width / 2 - window.ClientSize.Width / 2,
window.PlatformImpl.MaxClientSize.Height / 2 - window.ClientSize.Height / 2);
screen1.Object.WorkingArea.Size.Width / 2 - window.ClientSize.Width / 2,
screen1.Object.WorkingArea.Size.Height / 2 - window.ClientSize.Height / 2);
Assert.Equal(window.Position, expectedPosition);
}
}
[Fact]
public void Window_Should_Be_Centered_Relative_To_Owner_When_Window_Startup_Location_Is_Center_Owner()
public void Window_Should_Be_Centered_Relative_To_Owner_When_WindowStartupLocation_Is_CenterOwner()
{
var parentWindowImpl = new Mock<IWindowImpl>();
parentWindowImpl.SetupProperty(x => x.Position);

Loading…
Cancel
Save