Skip to content

Commit

Permalink
fix(grid): Fix regression when setting ColumnDefinition.Width
Browse files Browse the repository at this point in the history
Fix regression where changing ColumnDefinition.Width (or another property) would immediately set ActualWidth to 0. This is now returned to UWP behavior, where ActualWidth will stay at the old value and be updated once the Grid is re-measured.
  • Loading branch information
davidjohnoliver committed May 18, 2021
1 parent 74494c2 commit cb3b962
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,60 @@ await RunOnUIThread.ExecuteAsync(() =>
});
}

[TestMethod]
[RunsOnUIThread]
public async Task When_ColumnDefinition_Width_Changed()
{
var outerShell = new Grid { Width = 290, Height = 220 };

var colDef0 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) };
var colDef1 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) };
var SUT = new Grid
{
ColumnDefinitions =
{
colDef0,
colDef1,
}
};
outerShell.Children.Add(SUT);
AddChild(SUT, new Border { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }, 0, 0);
AddChild(SUT, new Border { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }, 0, 1);

TestServices.WindowHelper.WindowContent = outerShell;
await TestServices.WindowHelper.WaitForLoaded(SUT);

const double expectedColWidth = 290 / 2;
Assert.AreEqual(expectedColWidth, colDef0.ActualWidth);
Assert.AreEqual(expectedColWidth, colDef1.ActualWidth);

colDef0.Width = new GridLength(80, GridUnitType.Pixel);
Assert.AreEqual(expectedColWidth, colDef0.ActualWidth);
Assert.AreEqual(expectedColWidth, colDef1.ActualWidth);

await TestServices.WindowHelper.WaitForRelayouted(SUT);

Assert.AreEqual(80, colDef0.ActualWidth);
Assert.AreEqual(210, colDef1.ActualWidth);
}

private static void AddChild(Grid parent, FrameworkElement child, int row, int col, int? rowSpan = null, int? colSpan = null)
{
Grid.SetRow(child, row);
Grid.SetColumn(child, col);

if (rowSpan is { } rs)
{
Grid.SetRowSpan(child, rs);
}
if (colSpan is { } cs)
{
Grid.SetColumnSpan(child, cs);
}

parent.Children.Add(child);
}

private async Task WaitForMeasure(FrameworkElement view, int timeOutMs = 1000)
{
var isMeasured = false;
Expand Down
14 changes: 6 additions & 8 deletions src/Uno.UI/UI/Xaml/Controls/Grid/ColumnDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public ColumnDefinition()
this.RegisterDisposablePropertyChangedCallback((i, p, args) =>
{
Changed?.Invoke(this, EventArgs.Empty);
SetDefaultState();
InvalidateDefinition();
});
SetDefaultState();
}

#region Width DependencyProperty
Expand Down Expand Up @@ -77,13 +76,12 @@ public double ActualWidth

#region internal DefinitionBase

private void SetDefaultState()
private void InvalidateDefinition()
{
_effectiveMinSize = default;
_measureArrangeSize = default;
_sizeCache = default;
_finalOffset = default;
_effectiveUnitType = GridUnitType.Auto;
if (this.GetParent() is Grid parentGrid)
{
parentGrid.InvalidateDefinitions();
}
}

private double _effectiveMinSize;
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Grid/Grid.h.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ bool HasGridFlags(GridFlags mask)
// return DependencyObjectTraits<CGrid>.Index;
//}

void InvalidateDefinitions()
internal void InvalidateDefinitions()
{
SetGridFlags(GridFlags.DefinitionsChanged);
InvalidateMeasure();
Expand Down
14 changes: 6 additions & 8 deletions src/Uno.UI/UI/Xaml/Controls/Grid/RowDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public RowDefinition()
this.RegisterDisposablePropertyChangedCallback((i, p, args) =>
{
Changed?.Invoke(this, EventArgs.Empty);
SetDefaultState();
InvalidateDefinition();
});
SetDefaultState();
}

#region Height DependencyProperty
Expand Down Expand Up @@ -78,13 +77,12 @@ public double ActualHeight

#region internal DefinitionBase

private void SetDefaultState()
private void InvalidateDefinition()
{
_effectiveMinSize = default;
_measureArrangeSize = default;
_sizeCache = default;
_finalOffset = default;
_effectiveUnitType = GridUnitType.Auto;
if (this.GetParent() is Grid parentGrid)
{
parentGrid.InvalidateDefinitions();
}
}

private double _effectiveMinSize;
Expand Down

0 comments on commit cb3b962

Please sign in to comment.