diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 4306ecf338..2ac6299c0e 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -394,39 +394,42 @@ namespace Avalonia.Controls /// /// Shows the window as a dialog. /// + /// The dialog's owner window. /// /// A task that can be used to track the lifetime of the dialog. /// - public Task ShowDialog(Window parent) + public Task ShowDialog(Window owner) { - return ShowDialog(parent); + return ShowDialog(owner); } - /// /// Shows the window as a dialog. /// /// /// The type of the result produced by the dialog. /// + /// The dialog's owner window. /// . /// A task that can be used to retrieve the result of the dialog when it closes. /// - public Task ShowDialog(Window parent) => ShowDialog(parent.PlatformImpl); - + public Task ShowDialog(Window owner) => ShowDialog(owner.PlatformImpl); + /// /// Shows the window as a dialog. /// /// /// The type of the result produced by the dialog. /// + /// The dialog's owner window. /// . /// A task that can be used to retrieve the result of the dialog when it closes. /// - public Task ShowDialog(IWindowImpl parent) + public Task ShowDialog(IWindowImpl owner) { - if(parent == null) - throw new ArgumentNullException(nameof(parent)); + if(owner == null) + throw new ArgumentNullException(nameof(owner)); + if (IsVisible) { throw new InvalidOperationException("The window is already being shown."); @@ -435,15 +438,15 @@ namespace Avalonia.Controls AddWindow(this); EnsureInitialized(); - SetWindowStartupLocation(); IsVisible = true; LayoutManager.ExecuteInitialLayoutPass(this); + var result = new TaskCompletionSource(); + using (BeginAutoSizing()) { - PlatformImpl?.ShowDialog(parent); - var result = new TaskCompletionSource(); + PlatformImpl?.ShowDialog(owner); Renderer?.Start(); Observable.FromEventPattern( @@ -452,17 +455,18 @@ namespace Avalonia.Controls .Take(1) .Subscribe(_ => { - parent.Activate(); + owner.Activate(); result.SetResult((TResult)(_dialogResult ?? default(TResult))); }); - - return result.Task; } + + SetWindowStartupLocation(owner); + return result.Task; } - void SetWindowStartupLocation() + private void SetWindowStartupLocation(IWindowImpl owner = null) { - var scaling = PlatformImpl?.Scaling ?? 1; + var scaling = owner?.Scaling ?? PlatformImpl?.Scaling ?? 1; // TODO: We really need non-client size here. var rect = new PixelRect( @@ -471,7 +475,7 @@ namespace Avalonia.Controls if (WindowStartupLocation == WindowStartupLocation.CenterScreen) { - var screen = Screens.ScreenFromPoint(Position); + var screen = Screens.ScreenFromPoint(owner?.Position ?? Position); if (screen != null) { @@ -480,12 +484,12 @@ namespace Avalonia.Controls } else if (WindowStartupLocation == WindowStartupLocation.CenterOwner) { - if (Owner != null) + if (owner != null) { // TODO: We really need non-client size here. var ownerRect = new PixelRect( - Owner.Position, - PixelSize.FromSize(Owner.ClientSize, scaling)); + owner.Position, + PixelSize.FromSize(owner.ClientSize, scaling)); Position = ownerRect.CenterRect(rect).Position; } }