Skip to content

Commit db22867

Browse files
authored
Merge pull request #171 from unoplatform/dev/jela/datagrid-loop2
fix(datagrid): Add workaround for layout loop
2 parents 0c21b24 + 0698688 commit db22867

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid/DataGrid.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,16 +3336,15 @@ protected override Size MeasureOverride(Size availableSize)
33363336
}
33373337
else
33383338
{
3339-
#if !HAS_UNO
3340-
// This section is commented out in Uno until the measure
3341-
// loop in uno is adressed
3342-
if (_rowsPresenter != null)
3339+
if (DataGridFeatureConfiguation.EnableInvalidateMeasureInMeasureOverride)
33433340
{
3344-
_rowsPresenter.InvalidateMeasure();
3345-
}
3341+
if (_rowsPresenter != null)
3342+
{
3343+
_rowsPresenter.InvalidateMeasure();
3344+
}
33463345

3347-
InvalidateColumnHeadersMeasure();
3348-
#endif
3346+
InvalidateColumnHeadersMeasure();
3347+
}
33493348

33503349
desiredSize = base.MeasureOverride(availableSize);
33513350

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.WinUI.UI.Controls
6+
{
7+
/// <summary>
8+
/// Uno-specific feature configuration
9+
/// </summary>
10+
public static class DataGridFeatureConfiguation
11+
{
12+
/// <summary>
13+
/// Gets or sets a value indicating whether if InvalidateMeasure invocations can be done in MeasureOverride locations.
14+
/// </summary>
15+
/// <remarks>
16+
/// This configuration is required until https://github.com/unoplatform/uno/issues/3519 is fixed. Without this
17+
/// the layout engine turns into a loop and consumes CPU excessively, or freezes the app.
18+
/// </remarks>
19+
public static bool EnableInvalidateMeasureInMeasureOverride { get; set; } = false;
20+
}
21+
}

CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid/DataGridRow.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -780,20 +780,23 @@ protected override Size MeasureOverride(Size availableSize)
780780
return base.MeasureOverride(availableSize);
781781
}
782782

783-
// Allow the DataGrid specific components to adjust themselves based on new values
784-
if (_headerElement != null)
783+
if (DataGridFeatureConfiguation.EnableInvalidateMeasureInMeasureOverride)
785784
{
786-
_headerElement.InvalidateMeasure();
787-
}
785+
// Allow the DataGrid specific components to adjust themselves based on new values
786+
if (_headerElement != null)
787+
{
788+
_headerElement.InvalidateMeasure();
789+
}
788790

789-
if (_cellsElement != null)
790-
{
791-
_cellsElement.InvalidateMeasure();
792-
}
791+
if (_cellsElement != null)
792+
{
793+
_cellsElement.InvalidateMeasure();
794+
}
793795

794-
if (_detailsElement != null)
795-
{
796-
_detailsElement.InvalidateMeasure();
796+
if (_detailsElement != null)
797+
{
798+
_detailsElement.InvalidateMeasure();
799+
}
797800
}
798801

799802
bool currentAddItemIsDataContext = false;

CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid/DataGridRowsPresenter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ protected override Size MeasureOverride(Size availableSize)
133133
DataGridRow row = element as DataGridRow;
134134
if (row != null)
135135
{
136-
if (invalidateRows)
136+
if (DataGridFeatureConfiguation.EnableInvalidateMeasureInMeasureOverride)
137137
{
138-
row.InvalidateMeasure();
138+
if (invalidateRows)
139+
{
140+
row.InvalidateMeasure();
141+
}
139142
}
140143
}
141144

0 commit comments

Comments
 (0)