Skip to content

Commit

Permalink
prevent floating point errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Poker-sang committed Nov 28, 2024
1 parent 4fc769e commit 03ca1cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
21 changes: 11 additions & 10 deletions components/Primitives/src/StaggeredLayout/StaggeredLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size
return new Size(availableSize.Width, 0);
}

if ((context.RealizationRect.Width == 0) && (context.RealizationRect.Height == 0))
if (context.RealizationRect is { Width: 0, Height: 0 })
{
return new Size(availableSize.Width, 0.0f);
}
Expand All @@ -174,13 +174,14 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size
}
else
{
var tempAvailableWidth = availableWidth + ColumnSpacing;
// 0.00001 is to prevent floating point errors
var tempAvailableWidth = availableWidth + ColumnSpacing - 0.00001;
numColumns = (int)Math.Floor(tempAvailableWidth / DesiredColumnWidth);
columnWidth = tempAvailableWidth / numColumns - ColumnSpacing;
}
}

if (columnWidth != state.ColumnWidth)
if (Math.Abs(columnWidth - state.ColumnWidth) > double.Epsilon)
{
// The items will need to be remeasured
state.Clear();
Expand All @@ -205,10 +206,10 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size
state.ClearColumns();
}

if (RowSpacing != state.RowSpacing)
if (Math.Abs(this.RowSpacing - state.RowSpacing) > double.Epsilon)
{
// If the RowSpacing changes the height of the rows will be different.
// The columns stores the height so we'll want to clear them out to
// The columns store the height so we'll want to clear them out to
// get the proper height
state.ClearColumns();
state.RowSpacing = RowSpacing;
Expand All @@ -228,7 +229,7 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size
{
// Item has not been measured yet. Get the element and store the values
item.Element = context.GetOrCreateElementAt(i);
item.Element.Measure(new Size((float)state.ColumnWidth, (float)availableHeight));
item.Element.Measure(new Size(state.ColumnWidth, availableHeight));
item.Height = item.Element.DesiredSize.Height;
measured = true;
}
Expand Down Expand Up @@ -260,12 +261,12 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size

deadColumns.Add(columnIndex);
}
else if (measured == false)
else if (!measured)
{
// We ALWAYS want to measure an item that will be in the bounds
item.Element = context.GetOrCreateElementAt(i);
item.Element.Measure(new Size((float)state.ColumnWidth, (float)availableHeight));
if (item.Height != item.Element.DesiredSize.Height)
item.Element.Measure(new Size(state.ColumnWidth, availableHeight));
if (Math.Abs(item.Height - item.Element.DesiredSize.Height) > double.Epsilon)
{
// this item changed size; we need to recalculate layout for everything after this
state.RemoveFromIndex(i + 1);
Expand All @@ -282,7 +283,7 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size

double desiredHeight = state.GetHeight();

return new Size((float)availableWidth, (float)desiredHeight);
return new Size(availableWidth, desiredHeight);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ internal void ClearColumns()
/// </summary>
/// <returns>The estimated height of the layout.</returns>
/// <remarks>
/// If all of the items have been calculated then the actual height will be returned.
/// If all of the items have not been calculated then an estimated height will be calculated based on the average height of the items.
/// If all the items have been calculated then the actual height will be returned.
/// If all the items have not been calculated then an estimated height will be calculated based on the average height of the items.
/// </remarks>
internal double GetHeight()
{
double desiredHeight = Enumerable.Max(_columnLayout.Values, c => c.Height);
double desiredHeight = this._columnLayout.Values.Max(c => c.Height);

var itemCount = Enumerable.Sum(_columnLayout.Values, c => c.Count);
var itemCount = this._columnLayout.Values.Sum(c => c.Count);
if (itemCount == _context.ItemCount)
{
return desiredHeight;
Expand Down
2 changes: 1 addition & 1 deletion tooling
Submodule tooling updated 39 files
+5 −4 .github/workflows/build.yml
+13 −33 Build-Toolkit-Components.ps1
+9 −13 Build-Toolkit-Gallery.ps1
+1 −1 CommunityToolkit.App.Shared/Converters/StringToUriConverter.cs
+1 −1 CommunityToolkit.App.Shared/DocOrSampleTemplateSelector.cs
+1 −1 CommunityToolkit.App.Shared/Helpers/NavigationViewHelper.cs
+2 −2 CommunityToolkit.App.Shared/Pages/GettingStartedPage.xaml
+0 −3 CommunityToolkit.App.Shared/Pages/GettingStartedPage.xaml.cs
+2 −2 CommunityToolkit.App.Shared/Pages/SettingsPage.xaml
+0 −4 CommunityToolkit.App.Shared/Pages/SettingsPage.xaml.cs
+1 −1 CommunityToolkit.App.Shared/Renderers/GeneratedSampleOptionTemplateSelector.cs
+3 −3 CommunityToolkit.App.Shared/Renderers/GeneratedSampleOptionsRenderer.xaml.cs
+3 −3 CommunityToolkit.App.Shared/Renderers/ToolkitDocumentationRenderer.xaml.cs
+3 −6 CommunityToolkit.App.Shared/Renderers/ToolkitSampleRenderer.xaml.cs
+1 −1 CommunityToolkit.Tooling.SampleGen/Metadata/IToolkitSampleGeneratedOptionPropertyContainer.cs
+2 −2 CommunityToolkit.Tooling.SampleGen/Metadata/ToolkitSampleMetadata.cs
+2 −2 CommunityToolkit.Tooling.SampleGen/ToolkitSampleOptionGenerator.cs
+23 −89 GenerateAllSolution.ps1
+13 −13 MultiTarget/AvailableTargetFrameworks.props
+12 −16 MultiTarget/EnabledTargetFrameworks.props
+3 −11 MultiTarget/GenerateAllProjectReferences.ps1
+17 −27 MultiTarget/GenerateMultiTargetAwareProjectReferenceProps.ps1
+7 −7 MultiTarget/MultiTargetToTargetFramework.props
+2 −2 MultiTarget/PackageReferences/WinAppSdk.props
+49 −52 MultiTarget/UseTargetFrameworks.ps1
+1 −10 MultiTarget/WinUI.Extra.props
+0 −0 ProjectHeads/AllComponents/Tests.WinAppSdk/CommunityToolkit.Tests.WinAppSdk.csproj
+0 −0 ProjectHeads/AllComponents/Tests.WinAppSdk/Package.appxmanifest
+0 −0 ProjectHeads/AllComponents/Tests.WinAppSdk/Properties/launchSettings.json
+0 −0 ProjectHeads/AllComponents/Tests.WinAppSdk/app.manifest
+0 −0 ProjectHeads/AllComponents/WinAppSdk/CommunityToolkit.App.WinAppSdk.csproj
+1 −2 ProjectHeads/AllComponents/WinAppSdk/Package.appxmanifest
+0 −0 ProjectHeads/AllComponents/WinAppSdk/Properties/launchSettings.json
+0 −0 ProjectHeads/AllComponents/WinAppSdk/app.manifest
+0 −4 ProjectHeads/App.Head.WinAppSdk.props
+1 −1 ProjectHeads/Head.Uwp.props
+3 −4 ProjectHeads/Head.WinAppSdk.props
+0 −1 ToolkitComponent.SourceProject.props
+1 −1 global.json

0 comments on commit 03ca1cb

Please sign in to comment.