Skip to content

Commit

Permalink
Fix issue where setting StaggeredLayout.ColumnWidth to NaN would caus…
Browse files Browse the repository at this point in the history
…e an exception (#4064)

<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 -->

<!-- 👉 It is imperative to resolve ONE ISSUE PER PR and avoid making multiple changes unless the changes interrelate with each other --> 

<!-- 📝 Please always keep the "☑️ Allow edits by maintainers" button checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 -->


## Fixes #4063 
<!-- Add the relevant issue number after the "#" mentioned above (for ex: "## Fixes #1234") which will automatically close the issue once the PR is merged. -->

<!-- Add a brief overview here of the feature/bug & fix. -->
The exception was caused by having ``NaN`` the value of width in ``item.Element.Measure``. This PR fixes this by adding a check if ``DesiredColumnWidth`` is ``NaN``, and if so setting it instead to the available width.

## PR Type

<!-- Please uncomment one or more options below that apply to this PR. -->

Bugfix
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
When ``DesiredColumnWidth`` is ``NaN``,  an exception will be thrown when measuring item sizes, which causes a crash or will freeze the app.

## What is the new behavior?
If ``DesiredColumnWidth`` is ``NaN``, items are stretched to fit the available width.
Example:
![image](https://user-images.githubusercontent.com/59544401/120867269-6eaac500-c546-11eb-91e1-a2caad418212.png)


## PR Checklist

Please check if your PR fulfills the following requirements:

- [x] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->
- [ ] Sample in sample app has been added / updated (for bug fixes / features)
    - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)
- [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
- [ ] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [x] Contains **NO** breaking changes

<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below.
     Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. -->


## Other information
  • Loading branch information
msftbot[bot] authored Jun 29, 2021
2 parents 9ec1414 + f3416b3 commit 0dde090
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,16 @@ protected override Size MeasureOverride(VirtualizingLayoutContext context, Size
double availableWidth = availableSize.Width;
double availableHeight = availableSize.Height;

double columnWidth = Math.Min(DesiredColumnWidth, availableWidth);
// This ternary prevents the column width from being NaN, which would otherwise cause an exception when measuring item sizes
double columnWidth = double.IsNaN(DesiredColumnWidth) ? availableWidth : Math.Min(DesiredColumnWidth, availableWidth);
if (columnWidth != state.ColumnWidth)
{
// The items will need to be remeasured
state.Clear();
}

state.ColumnWidth = Math.Min(DesiredColumnWidth, availableWidth);
// This ternary prevents the column width from being NaN, which would otherwise cause an exception when measuring item sizes
state.ColumnWidth = double.IsNaN(DesiredColumnWidth) ? availableWidth : Math.Min(DesiredColumnWidth, availableWidth);
int numColumns = Math.Max(1, (int)Math.Floor(availableWidth / state.ColumnWidth));

// adjust for column spacing on all columns expect the first
Expand Down

0 comments on commit 0dde090

Please sign in to comment.