Browse Source

Fix DPI fallback value on LayoutRounding;

Rename `rowColDefsEmpty` to `IsTrivialGrid`;
pull/2563/head
Jumar Macato 7 years ago
parent
commit
689ca63c7a
No known key found for this signature in database GPG Key ID: B19884DAC3A5BF3F
  1. 66
      src/Avalonia.Controls/GridWPF.cs

66
src/Avalonia.Controls/GridWPF.cs

@ -33,6 +33,11 @@ namespace Avalonia.Controls
internal bool HasStarCellsU; internal bool HasStarCellsU;
internal bool HasStarCellsV; internal bool HasStarCellsV;
internal bool HasGroup3CellsInAutoRows; internal bool HasGroup3CellsInAutoRows;
internal bool MeasureOverrideInProgress;
internal bool ArrangeOverrideInProgress;
internal bool ColumnDefinitionsDirty;
internal bool RowDefinitionsDirty;
// index of the first cell in first cell group // index of the first cell in first cell group
internal int CellGroup1; internal int CellGroup1;
@ -44,7 +49,6 @@ namespace Avalonia.Controls
internal int CellGroup3; internal int CellGroup3;
// index of the first cell in fourth cell group // index of the first cell in fourth cell group
internal int CellGroup4; internal int CellGroup4;
// temporary array used during layout for various purposes // temporary array used during layout for various purposes
@ -60,9 +64,9 @@ namespace Avalonia.Controls
// Stores unrounded values and rounding errors during layout rounding. // Stores unrounded values and rounding errors during layout rounding.
private double[] _roundingErrors; private double[] _roundingErrors;
private DefinitionBase[] DefinitionsU = new DefinitionBase[1] { new ColumnDefinition() }; private DefinitionBase[] DefinitionsU;
private DefinitionBase[] DefinitionsV = new DefinitionBase[1] { new RowDefinition() }; private DefinitionBase[] DefinitionsV;
private const int c_layoutLoopMaxCount = 5; // 5 is an arbitrary constant chosen to end the measure loop private const int layoutLoopMaxCount = 5; // 5 is an arbitrary constant chosen to end the measure loop
private static readonly LocalDataStoreSlot s_tempDefinitionsDataSlot = Thread.AllocateDataSlot(); private static readonly LocalDataStoreSlot s_tempDefinitionsDataSlot = Thread.AllocateDataSlot();
private static readonly IComparer s_spanPreferredDistributionOrderComparer = new SpanPreferredDistributionOrderComparer(); private static readonly IComparer s_spanPreferredDistributionOrderComparer = new SpanPreferredDistributionOrderComparer();
private static readonly IComparer s_spanMaxDistributionOrderComparer = new SpanMaxDistributionOrderComparer(); private static readonly IComparer s_spanMaxDistributionOrderComparer = new SpanMaxDistributionOrderComparer();
@ -73,6 +77,7 @@ namespace Avalonia.Controls
static Grid() static Grid()
{ {
ShowGridLinesProperty.Changed.AddClassHandler<Grid>(OnShowGridLinesPropertyChanged); ShowGridLinesProperty.Changed.AddClassHandler<Grid>(OnShowGridLinesPropertyChanged);
ColumnProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged); ColumnProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged);
ColumnSpanProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged); ColumnSpanProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged);
RowProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged); RowProperty.Changed.AddClassHandler<Visual>(OnCellAttachedPropertyChanged);
@ -167,7 +172,6 @@ namespace Avalonia.Controls
return element.GetValue(RowSpanProperty); return element.GetValue(RowSpanProperty);
} }
/// <summary> /// <summary>
/// Gets the value of the IsSharedSizeScope attached property for a control. /// Gets the value of the IsSharedSizeScope attached property for a control.
/// </summary> /// </summary>
@ -243,6 +247,8 @@ namespace Avalonia.Controls
if (_columnDefinitions.Count > 0) if (_columnDefinitions.Count > 0)
DefinitionsU = _columnDefinitions.Cast<DefinitionBase>().ToArray(); DefinitionsU = _columnDefinitions.Cast<DefinitionBase>().ToArray();
else
DefinitionsU = new DefinitionBase[1] { new ColumnDefinition() };
_columnDefinitions.CollectionChanged += (_, e) => _columnDefinitions.CollectionChanged += (_, e) =>
{ {
@ -283,6 +289,8 @@ namespace Avalonia.Controls
if (_rowDefinitions.Count > 0) if (_rowDefinitions.Count > 0)
DefinitionsV = _rowDefinitions.Cast<DefinitionBase>().ToArray(); DefinitionsV = _rowDefinitions.Cast<DefinitionBase>().ToArray();
else
DefinitionsV = new DefinitionBase[1] { new ColumnDefinition() };
_rowDefinitions.CollectionChanged += (_, e) => _rowDefinitions.CollectionChanged += (_, e) =>
{ {
@ -300,8 +308,8 @@ namespace Avalonia.Controls
} }
} }
private bool rowColDefsEmpty => (DefinitionsU.Length == 0) && private bool IsTrivialGrid => (DefinitionsU?.Length <= 1) &&
(DefinitionsV.Length == 0); (DefinitionsV?.Length <= 1);
/// <summary> /// <summary>
/// Content measurement. /// Content measurement.
@ -317,7 +325,7 @@ namespace Avalonia.Controls
ListenToNotifications = true; ListenToNotifications = true;
MeasureOverrideInProgress = true; MeasureOverrideInProgress = true;
if (rowColDefsEmpty) if (IsTrivialGrid)
{ {
gridDesiredSize = new Size(); gridDesiredSize = new Size();
@ -358,10 +366,10 @@ namespace Avalonia.Controls
} }
} }
// ValidateColumnDefinitionsStructure(); ValidateColumnDefinitionsStructure();
ValidateDefinitionsLayout(DefinitionsU, sizeToContentU); ValidateDefinitionsLayout(DefinitionsU, sizeToContentU);
// ValidateRowDefinitionsStructure(); ValidateRowDefinitionsStructure();
ValidateDefinitionsLayout(DefinitionsV, sizeToContentV); ValidateDefinitionsLayout(DefinitionsV, sizeToContentV);
CellsStructureDirty |= (SizeToContentU != sizeToContentU) || (SizeToContentV != sizeToContentV); CellsStructureDirty |= (SizeToContentU != sizeToContentU) || (SizeToContentV != sizeToContentV);
@ -429,9 +437,9 @@ namespace Avalonia.Controls
ApplyCachedMinSizes(group2MinSizes, false); ApplyCachedMinSizes(group2MinSizes, false);
if (HasStarCellsV) { ResolveStar(DefinitionsV, constraint.Height); } if (HasStarCellsV) { ResolveStar(DefinitionsV, constraint.Height); }
MeasureCellsGroup(CellGroup2, constraint, cnt == c_layoutLoopMaxCount, false, out hasDesiredSizeUChanged); MeasureCellsGroup(CellGroup2, constraint, cnt == layoutLoopMaxCount, false, out hasDesiredSizeUChanged);
} }
while (hasDesiredSizeUChanged && ++cnt <= c_layoutLoopMaxCount); while (hasDesiredSizeUChanged && ++cnt <= layoutLoopMaxCount);
} }
} }
} }
@ -451,6 +459,18 @@ namespace Avalonia.Controls
return (gridDesiredSize); return (gridDesiredSize);
} }
private void ValidateColumnDefinitionsStructure()
{
if (DefinitionsU == null || DefinitionsU?.Count() == 0)
DefinitionsU = new DefinitionBase[1] { new ColumnDefinition() };
}
private void ValidateRowDefinitionsStructure()
{
if (DefinitionsV == null || DefinitionsV?.Count() == 0)
DefinitionsV = new DefinitionBase[1] { new RowDefinition() };
}
/// <summary> /// <summary>
/// Content arrangement. /// Content arrangement.
/// </summary> /// </summary>
@ -459,10 +479,9 @@ namespace Avalonia.Controls
{ {
try try
{ {
ArrangeOverrideInProgress = true; ArrangeOverrideInProgress = true;
if (rowColDefsEmpty) if (IsTrivialGrid)
{ {
for (int i = 0, count = Children.Count; i < count; ++i) for (int i = 0, count = Children.Count; i < count; ++i)
{ {
@ -527,11 +546,6 @@ namespace Avalonia.Controls
InvalidateMeasure(); InvalidateMeasure();
} }
internal bool MeasureOverrideInProgress;
internal bool ArrangeOverrideInProgress;
internal bool ColumnDefinitionsDirty;
internal bool RowDefinitionsDirty;
/// <summary> /// <summary>
/// Lays out cells according to rows and columns, and creates lookup grids. /// Lays out cells according to rows and columns, and creates lookup grids.
/// </summary> /// </summary>
@ -1638,9 +1652,9 @@ namespace Avalonia.Controls
double remainingAvailableSize = finalSize - takenSize; double remainingAvailableSize = finalSize - takenSize;
double remainingStarWeight = totalStarWeight - takenStarWeight; double remainingStarWeight = totalStarWeight - takenStarWeight;
MinRatioIndexComparer minRatioIndexComparer = new MinRatioIndexComparer((DefinitionBase[])definitions); MinRatioIndexComparer minRatioIndexComparer = new MinRatioIndexComparer(definitions);
Array.Sort(definitionIndices, 0, minCount, minRatioIndexComparer); Array.Sort(definitionIndices, 0, minCount, minRatioIndexComparer);
MaxRatioIndexComparer maxRatioIndexComparer = new MaxRatioIndexComparer((DefinitionBase[])definitions); MaxRatioIndexComparer maxRatioIndexComparer = new MaxRatioIndexComparer(definitions);
Array.Sort(definitionIndices, defCount, maxCount, maxRatioIndexComparer); Array.Sort(definitionIndices, defCount, maxCount, maxRatioIndexComparer);
while (minCount + maxCount > 0 && remainingAvailableSize > 0.0) while (minCount + maxCount > 0 && remainingAvailableSize > 0.0)
@ -1835,7 +1849,7 @@ namespace Avalonia.Controls
// unrounded sizes, to avoid breaking assumptions in the previous phases // unrounded sizes, to avoid breaking assumptions in the previous phases
if (UseLayoutRounding) if (UseLayoutRounding)
{ {
var dpi = (VisualRoot as ILayoutRoot)?.LayoutScaling ?? 96; var dpi = (VisualRoot as ILayoutRoot)?.LayoutScaling ?? 1.0;
double[] roundingErrors = RoundingErrors; double[] roundingErrors = RoundingErrors;
double roundedTakenSize = 0.0; double roundedTakenSize = 0.0;
@ -2043,7 +2057,7 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
private void SetValid() private void SetValid()
{ {
if (rowColDefsEmpty) if (IsTrivialGrid)
{ {
if (_tempDefinitions != null) if (_tempDefinitions != null)
{ {
@ -2127,7 +2141,7 @@ namespace Avalonia.Controls
private static void OnShowGridLinesPropertyChanged(Grid grid, AvaloniaPropertyChangedEventArgs e) private static void OnShowGridLinesPropertyChanged(Grid grid, AvaloniaPropertyChangedEventArgs e)
{ {
if (grid.rowColDefsEmpty // trivial grid is 1 by 1. there is no grid lines anyway if (!grid.IsTrivialGrid // trivial grid is 1 by 1. there is no grid lines anyway
&& grid.ListenToNotifications) && grid.ListenToNotifications)
{ {
grid.InvalidateVisual(); grid.InvalidateVisual();
@ -2140,7 +2154,7 @@ namespace Avalonia.Controls
{ {
var grid = child.GetVisualParent() as Grid; var grid = child.GetVisualParent() as Grid;
if (grid != null if (grid != null
&& grid.rowColDefsEmpty && !grid.IsTrivialGrid
&& grid.ListenToNotifications) && grid.ListenToNotifications)
{ {
grid.CellsStructureDirty = true; grid.CellsStructureDirty = true;
@ -2153,7 +2167,7 @@ namespace Avalonia.Controls
/// Helper for Comparer methods. /// Helper for Comparer methods.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// true iff one or both of x and y are null, in which case result holds /// true if one or both of x and y are null, in which case result holds
/// the relative sort order. /// the relative sort order.
/// </returns> /// </returns>
private static bool CompareNullRefs(object x, object y, out int result) private static bool CompareNullRefs(object x, object y, out int result)

Loading…
Cancel
Save