From 1f05ee41bbccbf17de73e5da68d32054b1b61e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Tue, 20 Sep 2022 04:42:58 +0100 Subject: [PATCH 1/6] fallback from CenterOwner to CenterScreen when owner is null --- src/Avalonia.Controls/Window.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 03c66aff2b..db161968b0 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -830,10 +830,11 @@ namespace Avalonia.Controls var startupLocation = WindowStartupLocation; if (startupLocation == WindowStartupLocation.CenterOwner && - Owner is Window ownerWindow && - ownerWindow.WindowState == WindowState.Minimized) + (Owner is null || + (Owner is Window ownerWindow && ownerWindow.WindowState == WindowState.Minimized)) + ) { - // If startup location is CenterOwner, but owner is minimized then fall back + // If startup location is CenterOwner, but owner is null or minimized then fall back // to CenterScreen. This behavior is consistent with WPF. startupLocation = WindowStartupLocation.CenterScreen; } From 541b2e74dae3bf2d653be0b0ff45ad2f4eb5a9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Tue, 20 Sep 2022 05:03:32 +0100 Subject: [PATCH 2/6] Use owner argument instead of Owner property --- src/Avalonia.Controls/Window.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index db161968b0..674b47f0d8 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -830,8 +830,8 @@ namespace Avalonia.Controls var startupLocation = WindowStartupLocation; if (startupLocation == WindowStartupLocation.CenterOwner && - (Owner is null || - (Owner is Window ownerWindow && ownerWindow.WindowState == WindowState.Minimized)) + (owner is null || + (owner is Window ownerWindow && ownerWindow.WindowState == WindowState.Minimized)) ) { // If startup location is CenterOwner, but owner is null or minimized then fall back From fa78c1c886be76f52adfd06f3fecc1ab7099c3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Tue, 20 Sep 2022 05:07:01 +0100 Subject: [PATCH 3/6] Revert original check to the Owner property --- src/Avalonia.Controls/Window.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 674b47f0d8..a7f82fc26f 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -831,7 +831,7 @@ namespace Avalonia.Controls if (startupLocation == WindowStartupLocation.CenterOwner && (owner is null || - (owner is Window ownerWindow && ownerWindow.WindowState == WindowState.Minimized)) + (Owner is Window ownerWindow && ownerWindow.WindowState == WindowState.Minimized)) ) { // If startup location is CenterOwner, but owner is null or minimized then fall back From e3238ed20d47b3cc5d1efd7c38b0f73d7fa419b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Tue, 20 Sep 2022 05:09:19 +0100 Subject: [PATCH 4/6] Remove useless check, it's always true --- src/Avalonia.Controls/Window.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index a7f82fc26f..f0369c416c 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -869,14 +869,11 @@ namespace Avalonia.Controls } else if (startupLocation == WindowStartupLocation.CenterOwner) { - if (owner != null) - { - var ownerSize = owner.FrameSize ?? owner.ClientSize; - var ownerRect = new PixelRect( - owner.Position, - PixelSize.FromSize(ownerSize, scaling)); - Position = ownerRect.CenterRect(rect).Position; - } + var ownerSize = owner.FrameSize ?? owner.ClientSize; + var ownerRect = new PixelRect( + owner.Position, + PixelSize.FromSize(ownerSize, scaling)); + Position = ownerRect.CenterRect(rect).Position; } } From 80edb2d1a4f5e5dc9357d31acb7d9120eff96549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Tue, 20 Sep 2022 05:12:29 +0100 Subject: [PATCH 5/6] Minor improvement to reuse parent and spare a property get --- src/Avalonia.Controls/Window.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index f0369c416c..b1d50cf430 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -668,7 +668,7 @@ namespace Avalonia.Controls Owner = parent; parent?.AddChild(this, false); - SetWindowStartupLocation(Owner?.PlatformImpl); + SetWindowStartupLocation(parent?.PlatformImpl); PlatformImpl?.Show(ShowActivated, false); Renderer?.Start(); From 280f373d7c368d7658d26915f77e91659eaaef6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Mon, 26 Sep 2022 23:29:44 +0100 Subject: [PATCH 6/6] Fix Dereference of a possibly null reference and simplify checks --- src/Avalonia.Controls/Window.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index b1d50cf430..1a7dca737e 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -852,24 +852,20 @@ namespace Avalonia.Controls if (owner is not null) { - screen = Screens.ScreenFromWindow(owner); - - screen ??= Screens.ScreenFromPoint(owner.Position); + screen = Screens.ScreenFromWindow(owner) + ?? Screens.ScreenFromPoint(owner.Position); } - if (screen is null) - { - screen = Screens.ScreenFromPoint(Position); - } + screen ??= Screens.ScreenFromPoint(Position); - if (screen != null) + if (screen is not null) { Position = screen.WorkingArea.CenterRect(rect).Position; } } else if (startupLocation == WindowStartupLocation.CenterOwner) { - var ownerSize = owner.FrameSize ?? owner.ClientSize; + var ownerSize = owner!.FrameSize ?? owner.ClientSize; var ownerRect = new PixelRect( owner.Position, PixelSize.FromSize(ownerSize, scaling));