From 6106ce6817933ce32077ba109a013e42680684b8 Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Mon, 12 Jun 2023 16:18:46 -0700 Subject: [PATCH] Fixes #85 - ContentSizer manipulating component Width to Zero on load ContentSizer was overwriting Width to Zero causing initial layout problems of a component, instead this code is removed as it is not needed to maintain the same behavior. Added a test to validate the old behavior was incorrect and the new desired behavior should work. --- ...munityToolkit.WinUI.Controls.Sizers.csproj | 2 +- .../ContentSizer/ContentSizer.Properties.cs | 25 +------------------ .../tests/ContentSizerTestInitialLayout.xaml | 20 +++++++++++++++ .../ContentSizerTestInitialLayout.xaml.cs | 16 ++++++++++++ .../Sizers/tests/ExampleSizerBaseTestClass.cs | 13 ++++++++++ .../Sizers/tests/Sizers.Tests.projitems | 7 ++++++ 6 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 components/Sizers/tests/ContentSizerTestInitialLayout.xaml create mode 100644 components/Sizers/tests/ContentSizerTestInitialLayout.xaml.cs diff --git a/components/Sizers/src/CommunityToolkit.WinUI.Controls.Sizers.csproj b/components/Sizers/src/CommunityToolkit.WinUI.Controls.Sizers.csproj index 8cd3b114..9ec90a83 100644 --- a/components/Sizers/src/CommunityToolkit.WinUI.Controls.Sizers.csproj +++ b/components/Sizers/src/CommunityToolkit.WinUI.Controls.Sizers.csproj @@ -2,7 +2,7 @@ Sizers This package contains SizerBase. - 8.0.0-beta.1 + 8.0.0-beta.2 CommunityToolkit.WinUI.Controls.SizersRns diff --git a/components/Sizers/src/ContentSizer/ContentSizer.Properties.cs b/components/Sizers/src/ContentSizer/ContentSizer.Properties.cs index 4bcf58aa..97ce3a91 100644 --- a/components/Sizers/src/ContentSizer/ContentSizer.Properties.cs +++ b/components/Sizers/src/ContentSizer/ContentSizer.Properties.cs @@ -35,28 +35,5 @@ public FrameworkElement? TargetControl /// Identifies the dependency property. /// public static readonly DependencyProperty TargetControlProperty = - DependencyProperty.Register(nameof(TargetControl), typeof(FrameworkElement), typeof(ContentSizer), new PropertyMetadata(null, OnTargetControlChanged)); - - private static void OnTargetControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - // TODO: Should we do this after the TargetControl is Loaded? (And use ActualWidth?) - // Or should we just do it in the manipulation event if Width is null? - - // Check if our width can be manipulated - if (d is SizerBase splitterBase && e.NewValue is FrameworkElement element) - { - // TODO: For Auto ResizeDirection we might want to do detection logic (TBD) here first? - if (splitterBase.Orientation != Orientation.Horizontal && double.IsNaN(element.Width)) - { - // We need to set the Width or Height somewhere, - // as if it's NaN we won't be able to manipulate it. - element.Width = element.DesiredSize.Width; - } - - if (splitterBase.Orientation != Orientation.Vertical && double.IsNaN(element.Height)) - { - element.Height = element.DesiredSize.Height; - } - } - } + DependencyProperty.Register(nameof(TargetControl), typeof(FrameworkElement), typeof(ContentSizer), new PropertyMetadata(null)); } diff --git a/components/Sizers/tests/ContentSizerTestInitialLayout.xaml b/components/Sizers/tests/ContentSizerTestInitialLayout.xaml new file mode 100644 index 00000000..dd58e085 --- /dev/null +++ b/components/Sizers/tests/ContentSizerTestInitialLayout.xaml @@ -0,0 +1,20 @@ + + + + + + + + + + + + diff --git a/components/Sizers/tests/ContentSizerTestInitialLayout.xaml.cs b/components/Sizers/tests/ContentSizerTestInitialLayout.xaml.cs new file mode 100644 index 00000000..bd42496c --- /dev/null +++ b/components/Sizers/tests/ContentSizerTestInitialLayout.xaml.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace SizersExperiment.Tests; + +/// +/// An empty page that can be used on its own or navigated to within a Frame. +/// +public sealed partial class ContentSizerTestInitialLayout : Page +{ + public ContentSizerTestInitialLayout() + { + this.InitializeComponent(); + } +} diff --git a/components/Sizers/tests/ExampleSizerBaseTestClass.cs b/components/Sizers/tests/ExampleSizerBaseTestClass.cs index 4d0385e7..18c56028 100644 --- a/components/Sizers/tests/ExampleSizerBaseTestClass.cs +++ b/components/Sizers/tests/ExampleSizerBaseTestClass.cs @@ -58,4 +58,17 @@ public void PropertySizer_TestChangeBinding(PropertySizerTestInitialBinding test // Set in XAML Page LINK: PropertySizerTestInitialBinding.xaml#L14 Assert.AreEqual(200, propertySizer.Binding, "Property Sizer not at expected changed value."); } + + [UIThreadTestMethod] + public void ContentSizer_TestAutoLayout_NonZeroAndNaN(ContentSizerTestInitialLayout testControl) + { + var contentSizer = testControl.FindDescendant(); + var textBlock = testControl.FindDescendant(); + + Assert.IsNotNull(contentSizer, "Could not find ContentSizer control."); + Assert.IsNotNull(textBlock, "Could not find TextBlock control."); + + Assert.IsTrue(textBlock.DesiredSize.Width > 5, "TextBlock desired size is too small."); + Assert.IsTrue(double.IsNaN(textBlock.Width), "TextBlock width should not be constrained and be NaN."); + } } diff --git a/components/Sizers/tests/Sizers.Tests.projitems b/components/Sizers/tests/Sizers.Tests.projitems index 0773d66f..5ea41099 100644 --- a/components/Sizers/tests/Sizers.Tests.projitems +++ b/components/Sizers/tests/Sizers.Tests.projitems @@ -9,12 +9,19 @@ SizersExperiment.Tests + + ContentSizerTestInitialLayout.xaml + PropertySizerTestInitialBinding.xaml + + Designer + MSBuild:Compile + Designer MSBuild:Compile