From 38ae473e52f010af4de810c34283d9c5ee535b2a Mon Sep 17 00:00:00 2001 From: Herman K Date: Thu, 7 Mar 2024 08:23:25 +0200 Subject: [PATCH] fixed bug with initial size and position calculation for X11Window (in some cases scaling is not applied until first position change event, and all calculations before it provided wrong values) (#12833) Co-authored-by: Herman Kirshin --- src/Avalonia.Controls/Window.cs | 12 ++++++++++++ src/Avalonia.X11/X11Window.cs | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 0b33ed6431..b4abca5510 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -714,6 +714,11 @@ namespace Avalonia.Controls _shown = true; IsVisible = true; + // We need to set position first because it is required for getting correct display scale. If position is not manual then it can be + // determined only by calling this method. But here it will calculate not precise location because scaling may not yet be applied (see i.e. X11Window), + // thus we ought to call it again later to center window correctly if needed, when scaling will be already applied + SetWindowStartupLocation(owner); + var initialSize = new Size( double.IsNaN(Width) ? Math.Max(MinWidth, ClientSize.Width) : Width, double.IsNaN(Height) ? Math.Max(MinHeight, ClientSize.Height) : Height); @@ -727,6 +732,7 @@ namespace Avalonia.Controls Owner = owner; + // Second call will calculate correct position because both current and owner windows have correct scaling. SetWindowStartupLocation(owner); StartRendering(); @@ -787,6 +793,11 @@ namespace Avalonia.Controls _showingAsDialog = true; IsVisible = true; + // We need to set position first because it is required for getting correct display scale. If position is not manual then it can be + // determined only by calling this method. But here it will calculate not precise location because scaling may not yet be applied (see i.e. X11Window), + // thus we ought to call it again later to center window correctly if needed, when scaling will be already applied + SetWindowStartupLocation(owner); + var initialSize = new Size( double.IsNaN(Width) ? ClientSize.Width : Width, double.IsNaN(Height) ? ClientSize.Height : Height); @@ -802,6 +813,7 @@ namespace Avalonia.Controls Owner = owner; + // Second call will calculate correct position because both current and owner windows have correct scaling. SetWindowStartupLocation(owner); StartRendering(); diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 368a5a8e0f..2609a86a2c 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -1026,7 +1026,11 @@ namespace Avalonia.X11 public void Resize(Size clientSize, WindowResizeReason reason) => Resize(clientSize, false, reason); - public void Move(PixelPoint point) => Position = point; + public void Move(PixelPoint point) + { + Position = point; + UpdateScaling(); + } private void MoveResize(PixelPoint position, Size size, double scaling) { Move(position);