Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #85 - ContentSizer manipulating component Width to Zero on load #86

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ToolkitComponentName>Sizers</ToolkitComponentName>
<Description>This package contains SizerBase.</Description>
<Version>8.0.0-beta.1</Version>
<Version>8.0.0-beta.2</Version>

<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 -->
<RootNamespace>CommunityToolkit.WinUI.Controls.SizersRns</RootNamespace>
Expand Down
25 changes: 1 addition & 24 deletions components/Sizers/src/ContentSizer/ContentSizer.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,5 @@ public FrameworkElement? TargetControl
/// Identifies the <see cref="TargetControl"/> dependency property.
/// </summary>
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));
}
20 changes: 20 additions & 0 deletions components/Sizers/tests/ContentSizerTestInitialLayout.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- 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. -->
<Page x:Class="SizersExperiment.Tests.ContentSizerTestInitialLayout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="TargetText"
Text="This is some text" />
<controls:ContentSizer Grid.Column="1"
TargetControl="{x:Bind TargetText}" />
</Grid>
</Page>
16 changes: 16 additions & 0 deletions components/Sizers/tests/ContentSizerTestInitialLayout.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ContentSizerTestInitialLayout : Page
{
public ContentSizerTestInitialLayout()
{
this.InitializeComponent();
}
}
13 changes: 13 additions & 0 deletions components/Sizers/tests/ExampleSizerBaseTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentSizer>();
var textBlock = testControl.FindDescendant<TextBlock>();

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.");
}
}
7 changes: 7 additions & 0 deletions components/Sizers/tests/Sizers.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
<Import_RootNamespace>SizersExperiment.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml.cs">
<DependentUpon>ContentSizerTestInitialLayout.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ExampleSizerBaseTestClass.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PropertySizerTestInitialBinding.xaml.cs">
<DependentUpon>PropertySizerTestInitialBinding.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)PropertySizerTestInitialBinding.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down