Browse Source

Implemented Window.WindowStartupLocation and WindowBase.Owner.

pull/1044/head
José Pedro 9 years ago
parent
commit
1eee17345b
  1. 28
      src/Avalonia.Controls/Window.cs
  2. 15
      src/Avalonia.Controls/WindowBase.cs
  3. 23
      src/Avalonia.Controls/WindowStartupLocation.cs
  4. 3
      src/Avalonia.DotNetCoreRuntime/AppBuilder.cs
  5. 77
      tests/Avalonia.Controls.UnitTests/WindowTests.cs

28
src/Avalonia.Controls/Window.cs

@ -183,6 +183,15 @@ namespace Avalonia.Controls
set { SetValue(IconProperty, value); }
}
/// <summary>
/// Gets or sets the startup location of the window.
/// </summary>
public WindowStartupLocation WindowStartupLocation
{
get;
set;
}
/// <inheritdoc/>
Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
@ -246,6 +255,7 @@ namespace Avalonia.Controls
s_windows.Add(this);
EnsureInitialized();
SetWindowStartupLocation();
IsVisible = true;
LayoutManager.Instance.ExecuteInitialLayoutPass(this);
@ -285,6 +295,7 @@ namespace Avalonia.Controls
s_windows.Add(this);
EnsureInitialized();
SetWindowStartupLocation();
IsVisible = true;
LayoutManager.Instance.ExecuteInitialLayoutPass(this);
@ -321,6 +332,23 @@ namespace Avalonia.Controls
}
}
void SetWindowStartupLocation()
{
if (WindowStartupLocation == WindowStartupLocation.CenterScreen)
{
var positionAsSize = PlatformImpl.MaxClientSize / 2 - ClientSize / 2;
Position = new Point(positionAsSize.Width, positionAsSize.Height);
}
else if (WindowStartupLocation == WindowStartupLocation.CenterOwner)
{
if (Owner != null)
{
var positionAsSize = Owner.ClientSize / 2 - ClientSize / 2;
Position = Owner.Position + new Point(positionAsSize.Width, positionAsSize.Height);
}
}
}
/// <inheritdoc/>
void INameScope.Register(string name, object element)
{

15
src/Avalonia.Controls/WindowBase.cs

@ -29,6 +29,12 @@ namespace Avalonia.Controls
public static readonly DirectProperty<WindowBase, bool> IsActiveProperty =
AvaloniaProperty.RegisterDirect<WindowBase, bool>(nameof(IsActive), o => o.IsActive);
/// <summary>
/// Defines the <see cref="Owner"/> property.
/// </summary>
public static readonly StyledProperty<WindowBase> OwnerProperty =
AvaloniaProperty.Register<WindowBase, WindowBase>(nameof(Owner));
private bool _hasExecutedInitialLayoutPass;
private bool _isActive;
private bool _ignoreVisibilityChange;
@ -100,6 +106,15 @@ namespace Avalonia.Controls
private set;
}
/// <summary>
/// Gets or sets the owner of the window.
/// </summary>
public WindowBase Owner
{
get { return GetValue(OwnerProperty); }
set { SetValue(OwnerProperty, value); }
}
/// <summary>
/// Activates the window.
/// </summary>

23
src/Avalonia.Controls/WindowStartupLocation.cs

@ -0,0 +1,23 @@
namespace Avalonia.Controls
{
/// <summary>
/// Determines the startup location of the window.
/// </summary>
public enum WindowStartupLocation
{
/// <summary>
/// The startup location is defined by the Position property.
/// </summary>
Manual,
/// <summary>
/// The startup location is the center of the screen.
/// </summary>
CenterScreen,
/// <summary>
/// The startup location is the center of the owner window. If the owner window is not specified, the startup location will be <see cref="Manual"/>.
/// </summary>
CenterOwner
}
}

3
src/Avalonia.DotNetCoreRuntime/AppBuilder.cs

@ -10,6 +10,9 @@ using Avalonia.Shared.PlatformSupport;
namespace Avalonia
{
/// <summary>
/// Initializes platform-specific services for an <see cref="Application"/>.
/// </summary>
public sealed class AppBuilder : AppBuilderBase<AppBuilder>
{
/// <summary>

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

@ -68,7 +68,7 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void IsVisible_Should_Be_False_Atfer_Hide()
public void IsVisible_Should_Be_False_After_Hide()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
@ -82,7 +82,7 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void IsVisible_Should_Be_False_Atfer_Close()
public void IsVisible_Should_Be_False_After_Close()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
@ -96,7 +96,7 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void IsVisible_Should_Be_False_Atfer_Impl_Signals_Close()
public void IsVisible_Should_Be_False_After_Impl_Signals_Close()
{
var windowImpl = new Mock<IWindowImpl>();
windowImpl.SetupProperty(x => x.Closed);
@ -191,5 +191,76 @@ namespace Avalonia.Controls.UnitTests
// AvaloniaLocator scopes.
((IList<Window>)Window.OpenWindows).Clear();
}
[Fact]
public void Window_Should_Be_Centered_When_Window_Startup_Location_Is_Center_Screen()
{
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);
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = new Window();
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);
Assert.Equal(window.Position, expectedPosition);
}
}
[Fact]
public void Window_Should_Be_Centered_Relative_To_Owner_When_Window_Startup_Location_Is_Center_Owner()
{
var parentWindowImpl = new Mock<IWindowImpl>();
parentWindowImpl.SetupProperty(x => x.Position);
parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
parentWindowImpl.Setup(x => x.Scaling).Returns(1);
var windowImpl = new Mock<IWindowImpl>();
windowImpl.SetupProperty(x => x.Position);
windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
windowImpl.Setup(x => x.Scaling).Returns(1);
var parentWindowServices = TestServices.StyledWindow.With(
windowingPlatform: new MockWindowingPlatform(() => parentWindowImpl.Object));
var windowServices = TestServices.StyledWindow.With(
windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
using (UnitTestApplication.Start(parentWindowServices))
{
var parentWindow = new Window();
parentWindow.Position = new Point(60, 40);
parentWindow.Show();
using (UnitTestApplication.Start(windowServices))
{
var window = new Window();
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.Position = new Point(60, 40);
window.Owner = parentWindow;
window.Show();
var expectedPosition = new Point(
parentWindow.Position.X + parentWindow.ClientSize.Width / 2 - window.ClientSize.Width / 2,
parentWindow.Position.Y + parentWindow.ClientSize.Height / 2 - window.ClientSize.Height / 2);
Assert.Equal(window.Position, expectedPosition);
}
}
}
}
}

Loading…
Cancel
Save