Skip to content

Commit

Permalink
grow datagrid height if there's available space (#16527)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Katz <maxkatz6@outlook.com>
  • Loading branch information
emmauss and maxkatz6 authored Aug 19, 2024
1 parent 1d9a018 commit f989fec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 52 deletions.
22 changes: 7 additions & 15 deletions src/Avalonia.Controls.DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,14 +1539,11 @@ internal double AvailableSlotElementRoom
set;
}

// Height currently available for cells this value is smaller. This height is reduced by the existence of ColumnHeaders
// or a horizontal scrollbar. Layout is asynchronous so changes to the ColumnHeaders or the horizontal scrollbar are
// not reflected immediately.
internal double CellsHeight
internal double CellsEstimatedHeight
{
get
{
return RowsPresenterEstimatedAvailableHeight ?? 0;
return RowsPresenterAvailableSize?.Height ?? 0;
}
}

Expand Down Expand Up @@ -1832,11 +1829,6 @@ internal Size? RowsPresenterAvailableSize
_rowsPresenterAvailableSize = value;
}
}
internal double? RowsPresenterEstimatedAvailableHeight
{
get;
set;
}

internal double[] RowGroupSublevelIndents
{
Expand Down Expand Up @@ -2364,7 +2356,7 @@ internal bool UpdateScroll(Vector delta)
}
else
{
double maximum = EdgedRowsHeightCalculated - CellsHeight;
double maximum = EdgedRowsHeightCalculated - CellsEstimatedHeight;
scrollHeight = Math.Min(Math.Max(0, maximum - _verticalOffset), -delta.Y);
}
}
Expand Down Expand Up @@ -3186,7 +3178,7 @@ internal void UpdateVerticalScrollBar()
{
if (_vScrollBar != null && _vScrollBar.IsVisible)
{
double cellsHeight = CellsHeight;
double cellsHeight = CellsEstimatedHeight;
double edgedRowsHeightCalculated = EdgedRowsHeightCalculated;
UpdateVerticalScrollBar(
needVertScrollbar: edgedRowsHeightCalculated > cellsHeight,
Expand Down Expand Up @@ -3320,7 +3312,7 @@ private void UpdateRowDetailsVisibilityMode(DataGridRowDetailsVisibilityMode new
}
if (updated)
{
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsEstimatedHeight);
InvalidateRowsMeasure(invalidateIndividualElements: false);
}
}
Expand Down Expand Up @@ -3554,7 +3546,7 @@ private void ComputeScrollBarsLayout()
bool isVerticalScrollBarOverCells = IsVerticalScrollBarOverCells;

double cellsWidth = CellsWidth;
double cellsHeight = CellsHeight;
double cellsHeight = CellsEstimatedHeight;

bool allowHorizScrollbar = false;
bool forceHorizScrollbar = false;
Expand Down Expand Up @@ -3611,7 +3603,7 @@ private void ComputeScrollBarsLayout()
double totalVisibleWidth = ColumnsInternal.VisibleEdgedColumnsWidth;
double totalVisibleFrozenWidth = ColumnsInternal.GetVisibleFrozenEdgedColumnsWidth();

UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsEstimatedHeight);
double totalVisibleHeight = EdgedRowsHeightCalculated;

if (!forceHorizScrollbar && !forceVertScrollbar)
Expand Down
34 changes: 17 additions & 17 deletions src/Avalonia.Controls.DataGrid/DataGridRows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ internal bool ScrollSlotIntoView(int slot, bool scrolledHorizontally)
{
row.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
}
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsEstimatedHeight);
}

if (DisplayData.FirstScrollingSlot < slot && (DisplayData.LastScrollingSlot > slot || DisplayData.LastScrollingSlot == -1))
Expand Down Expand Up @@ -462,7 +462,7 @@ internal bool ScrollSlotIntoView(int slot, bool scrolledHorizontally)
ResetDisplayedRows();
}
NegVerticalOffset = 0;
UpdateDisplayedRows(slot, CellsHeight);
UpdateDisplayedRows(slot, CellsEstimatedHeight);
}
else if (DisplayData.LastScrollingSlot <= slot)
{
Expand Down Expand Up @@ -502,11 +502,11 @@ internal bool ScrollSlotIntoView(int slot, bool scrolledHorizontally)
{
ResetDisplayedRows();
}
if (MathUtilities.GreaterThanOrClose(GetExactSlotElementHeight(slot), CellsHeight))
if (MathUtilities.GreaterThanOrClose(GetExactSlotElementHeight(slot), CellsEstimatedHeight))
{
// The entire row won't fit in the DataGrid so we start showing it from the top
NegVerticalOffset = 0;
UpdateDisplayedRows(slot, CellsHeight);
UpdateDisplayedRows(slot, CellsEstimatedHeight);
}
else
{
Expand Down Expand Up @@ -972,7 +972,7 @@ private void ExpandSlots(int startSlot, int endSlot, bool isDisplayed, ref int s

if (isDisplayed)
{
double availableHeight = CellsHeight - heightAboveStartSlot;
double availableHeight = CellsEstimatedHeight - heightAboveStartSlot;
// Actually expand the displayed slots up to what we can display
for (int i = startSlot; (i <= endSlot) && (currentHeightChange < availableHeight); i++)
{
Expand Down Expand Up @@ -1645,7 +1645,7 @@ private void ResetDisplayedRows()
}

DisplayData.ClearElements(recycle: true);
AvailableSlotElementRoom = CellsHeight;
AvailableSlotElementRoom = CellsEstimatedHeight;
}

/// <summary>
Expand All @@ -1662,7 +1662,7 @@ private bool SlotIsDisplayed(int slot)
return true;
}
else if (DisplayData.FirstScrollingSlot == -1 &&
CellsHeight > 0 &&
CellsEstimatedHeight > 0 &&
CellsWidth > 0)
{
return true;
Expand Down Expand Up @@ -1716,7 +1716,7 @@ private void ScrollSlotsByHeight(double height)
// Figure out what row we've scrolled down to and update the value for NegVerticalOffset
NegVerticalOffset = 0;
//
if (height > 2 * CellsHeight &&
if (height > 2 * CellsEstimatedHeight &&
(RowDetailsVisibilityMode != DataGridRowDetailsVisibilityMode.VisibleWhenSelected || RowDetailsTemplate == null))
{
// Very large scroll occurred. Instead of determining the exact number of scrolled off rows,
Expand Down Expand Up @@ -1778,7 +1778,7 @@ private void ScrollSlotsByHeight(double height)
NegVerticalOffset = 0;
//

if (height < -2 * CellsHeight &&
if (height < -2 * CellsEstimatedHeight &&
(RowDetailsVisibilityMode != DataGridRowDetailsVisibilityMode.VisibleWhenSelected || RowDetailsTemplate == null))
{
// Very large scroll occurred. Instead of determining the exact number of scrolled off rows,
Expand Down Expand Up @@ -1838,7 +1838,7 @@ private void ScrollSlotsByHeight(double height)
// strategy. For most data, this should be unnoticeable.
ResetDisplayedRows();
NegVerticalOffset = 0;
UpdateDisplayedRows(0, CellsHeight);
UpdateDisplayedRows(0, CellsEstimatedHeight);
newFirstScrollingSlot = 0;
}
}
Expand All @@ -1857,7 +1857,7 @@ private void ScrollSlotsByHeight(double height)
NegVerticalOffset = 0;
}

UpdateDisplayedRows(newFirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(newFirstScrollingSlot, CellsEstimatedHeight);

double firstElementHeight = GetExactSlotElementHeight(DisplayData.FirstScrollingSlot);
if (MathUtilities.GreaterThan(NegVerticalOffset, firstElementHeight))
Expand All @@ -1883,7 +1883,7 @@ private void ScrollSlotsByHeight(double height)
// We could be smarter about this, but it's not common so we wouldn't gain much from optimizing here
if (firstElementSlot != DisplayData.FirstScrollingSlot)
{
UpdateDisplayedRows(firstElementSlot, CellsHeight);
UpdateDisplayedRows(firstElementSlot, CellsEstimatedHeight);
}
}

Expand Down Expand Up @@ -2011,7 +2011,7 @@ private void UnloadElements(bool recycle)
DisplayData.ClearElements(recycle);

// Update the AvailableRowRoom since we're displaying 0 rows now
AvailableSlotElementRoom = CellsHeight;
AvailableSlotElementRoom = CellsEstimatedHeight;
VisibleSlotCount = 0;
}

Expand Down Expand Up @@ -2122,7 +2122,7 @@ private void UpdateDisplayedRowsFromBottom(int newLastDisplayedScrollingRow)

int lastDisplayedScrollingRow = newLastDisplayedScrollingRow;
int firstDisplayedScrollingRow = -1;
double displayHeight = CellsHeight;
double displayHeight = CellsEstimatedHeight;
double deltaY = 0;
int visibleScrollingRows = 0;

Expand Down Expand Up @@ -2633,7 +2633,7 @@ private double UpdateRowGroupVisibility(DataGridRowGroupInfo targetRowGroupInfo,
}
if (isDisplayed)
{
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(DisplayData.FirstScrollingSlot, CellsEstimatedHeight);
}
}
else
Expand Down Expand Up @@ -2692,7 +2692,7 @@ private double UpdateRowGroupVisibility(DataGridRowGroupInfo targetRowGroupInfo,
}
else
{
UpdateDisplayedRows(newFirstScrollingSlot, CellsHeight);
UpdateDisplayedRows(newFirstScrollingSlot, CellsEstimatedHeight);
}
}
}
Expand Down Expand Up @@ -2736,7 +2736,7 @@ private double UpdateRowGroupVisibility(DataGridRowGroupInfo targetRowGroupInfo,
NegVerticalOffset = 0;
SetVerticalOffset(0);
int firstDisplayedRow = GetNextVisibleSlot(-1);
UpdateDisplayedRows(firstDisplayedRow, CellsHeight);
UpdateDisplayedRows(firstDisplayedRow, CellsEstimatedHeight);
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ internal DataGrid OwningGrid
set;
}

private double _measureHeightOffset = 0;

private double CalculateEstimatedAvailableHeight(Size availableSize)
{
if (!Double.IsPositiveInfinity(availableSize.Height))
{
return availableSize.Height + _measureHeightOffset;
}
else
{
return availableSize.Height;
}
}

event EventHandler<ChildIndexChangedEventArgs> IChildIndexProvider.ChildIndexChanged
{
add => _childIndexChanged += value;
Expand Down Expand Up @@ -88,11 +74,6 @@ protected override Size ArrangeOverride(Size finalSize)
if (OwningGrid.RowsPresenterAvailableSize.HasValue)
{
var availableHeight = OwningGrid.RowsPresenterAvailableSize.Value.Height;
if (!Double.IsPositiveInfinity(availableHeight))
{
_measureHeightOffset = finalSize.Height - availableHeight;
OwningGrid.RowsPresenterEstimatedAvailableHeight = finalSize.Height;
}
}

OwningGrid.OnFillerColumnWidthNeeded(finalSize.Width);
Expand Down Expand Up @@ -167,7 +148,6 @@ protected override Size MeasureOverride(Size availableSize)
// The DataGrid uses the RowsPresenter available size in order to autogrow
// and calculate the scrollbars
OwningGrid.RowsPresenterAvailableSize = availableSize;
OwningGrid.RowsPresenterEstimatedAvailableHeight = CalculateEstimatedAvailableHeight(availableSize);

OwningGrid.OnRowsMeasure();

Expand Down

0 comments on commit f989fec

Please sign in to comment.