From 70d75c0eb5db776fa59fffaaf69c1b9c8ba6d9f1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 12 Feb 2018 23:55:36 +0100 Subject: [PATCH 1/2] Fix Window auto-sizing. `Window` was acting like `SizeToContent="WidthAndHeight"` was set even when it wasn't. This fixes that. --- src/Avalonia.Controls/Window.cs | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 7fed712e07..f3ad4dbbd9 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -374,27 +374,31 @@ namespace Avalonia.Controls { var sizeToContent = SizeToContent; var clientSize = ClientSize; - Size constraint; + Size constraint = clientSize; - switch (sizeToContent) + if ((sizeToContent & SizeToContent.Width) != 0) { - case SizeToContent.Width: - constraint = new Size(double.PositiveInfinity, ClientSize.Height); - break; - case SizeToContent.Height: - constraint = new Size(ClientSize.Width, double.PositiveInfinity); - break; - case SizeToContent.WidthAndHeight: - constraint = Size.Infinity; - break; - case SizeToContent.Manual: - constraint = ClientSize; - break; - default: - throw new InvalidOperationException("Invalid value for SizeToContent."); + constraint = constraint.WithWidth(double.PositiveInfinity); } - return base.MeasureOverride(constraint); + if ((sizeToContent & SizeToContent.Height) != 0) + { + constraint = constraint.WithHeight(double.PositiveInfinity); + } + + var result = base.MeasureOverride(constraint); + + if ((sizeToContent & SizeToContent.Width) == 0) + { + result = result.WithWidth(clientSize.Width); + } + + if ((sizeToContent & SizeToContent.Height) == 0) + { + result = result.WithHeight(clientSize.Height); + } + + return result; } protected override void HandleClosed() From 2af1f20108a1d3ce1c854e30899f7a67327cbe89 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 13 Feb 2018 00:12:12 +0100 Subject: [PATCH 2/2] Mark SizeToContent with [Flags]. And explicitly assign values. --- src/Avalonia.Controls/Window.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index f3ad4dbbd9..893859b915 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -19,27 +19,28 @@ namespace Avalonia.Controls /// /// Determines how a will size itself to fit its content. /// + [Flags] public enum SizeToContent { /// /// The window will not automatically size itself to fit its content. /// - Manual, + Manual = 0, /// /// The window will size itself horizontally to fit its content. /// - Width, + Width = 1, /// /// The window will size itself vertically to fit its content. /// - Height, + Height = 2, /// /// The window will size itself horizontally and vertically to fit its content. /// - WidthAndHeight, + WidthAndHeight = 3, } ///