Skip to content

Commit

Permalink
Get X position from Columns.
Browse files Browse the repository at this point in the history
For columnar layouts, there should usually be be no need to estimate the x position of the column start in the viewport, as this information can be queried from `IColumns`. Make a new `GetOrEstimateAnchorElementForViewport` virtual method and override its behavior for columnar presenters.

Fixes #414
  • Loading branch information
grokys committed Nov 27, 2024
1 parent 7b9571a commit fed5361
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ protected sealed override Size GetInitialConstraint(Control element, int index,
return new Size(Math.Min(availableSize.Width, column.MaxActualWidth), availableSize.Height);
}

protected override (int index, double position) GetOrEstimateAnchorElementForViewport(
double viewportStart,
double viewportEnd,
int itemCount)
{
if (Columns?.GetColumnAt(viewportStart) is var (index, position) && index >= 0)
return (index, position);
return base.GetOrEstimateAnchorElementForViewport(viewportStart, viewportEnd, itemCount);
}

protected sealed override bool NeedsFinalMeasurePass(int firstIndex, IReadOnlyList<Control?> elements)
{
var columns = Columns!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ protected override Size ArrangeOverride(Size finalSize)
}
}

protected virtual (int index, double position) GetOrEstimateAnchorElementForViewport(
double viewportStart,
double viewportEnd,
int itemCount)
{
Debug.Assert(_realizedElements is not null);

return _realizedElements.GetOrEstimateAnchorElementForViewport(
viewportStart,
viewportEnd,
itemCount,
ref _lastEstimatedElementSizeU);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
Expand Down Expand Up @@ -539,11 +553,7 @@ private MeasureViewport CalculateMeasureViewport(IReadOnlyList<TItem> items, Siz

// Get or estimate the anchor element from which to start realization.
var itemCount = items.Count;
var (anchorIndex, anchorU) = _realizedElements.GetOrEstimateAnchorElementForViewport(
viewportStart,
viewportEnd,
itemCount,
ref _lastEstimatedElementSizeU);
var (anchorIndex, anchorU) = GetOrEstimateAnchorElementForViewport(viewportStart, viewportEnd, itemCount);

// Check if the anchor element is not within the currently realized elements.
var disjunct = anchorIndex < _realizedElements.FirstIndex ||
Expand Down

0 comments on commit fed5361

Please sign in to comment.