Skip to content

Commit

Permalink
fix(autosuggestbox): bring selected item from popup into view
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Aug 12, 2024
1 parent 859241f commit 5df1e2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ private void HandleUpDownKeys(KeyRoutedEventArgs e)
}

_suggestionsList.SelectedIndex = nextIndex;
_suggestionsList.ScrollIntoView(_suggestionsList.SelectedItem);

if (nextIndex == -1)
{
Expand Down
23 changes: 10 additions & 13 deletions src/Uno.UI/UI/Xaml/Controls/ListViewBase/ListViewBase.managed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,26 @@ private void ScrollIntoViewFastPath(UIElement element, ScrollIntoViewAlignment a
{
if (ScrollViewer is { } sv && sv.Presenter is { } presenter)
{
var offsetXY = element.TransformToVisual(presenter).TransformPoint(Point.Zero);
var offsetXY = element.TransformToVisual(presenter).TransformPoint(new Point(presenter.HorizontalOffset, presenter.VerticalOffset));

var (newOffset, elementLength, presenterOffset, presenterViewportLength) =
var (elementOffset, elementLength, presenterOffset, presenterViewportLength) =
ItemsPanelRoot.PhysicalOrientation is Orientation.Vertical
? (offsetXY.Y, element.ActualSize.Y, presenter.VerticalOffset, presenter.ViewportHeight)
: (offsetXY.X, element.ActualSize.X, presenter.HorizontalOffset, presenter.ViewportWidth);

if (presenterOffset < newOffset && newOffset + elementLength < presenterOffset + presenterViewportLength)
if (presenterOffset <= elementOffset && elementOffset + elementLength <= presenterOffset + presenterViewportLength)
{
// if the element is within the visible viewport, do nothing.
return;
}

// If we use the above offset directly, the item we want to jump to will be the start of the viewport, i.e. leading
if (alignment is ScrollIntoViewAlignment.Default)
{
if (presenterOffset < newOffset)
{
// scroll one "viewport page" less: this brings the element's start right after the viewport's length ends
// we then scroll again by elementLength so that the end of the element is the end of the viewport
newOffset += (-presenterViewportLength) + elementLength;
}
}
// If we use the above offset directly, the item we want to jump to will be the start of the viewport, i.e. leading.
// For the default alignment, we move the element to either of the viewport ends (i.e. to the top or the bottom of the
// viewport. To move to the bottom, we scroll one "viewport page" less. This brings the element's start right after the
// viewport's length ends we then scroll again by elementLength so that the end of the element is the end of the viewport.
var newOffset = alignment is ScrollIntoViewAlignment.Default && presenterOffset < elementOffset
? elementOffset - presenterViewportLength + elementLength
: elementOffset;

if (ItemsPanelRoot.PhysicalOrientation is Orientation.Vertical)
{
Expand Down

0 comments on commit 5df1e2f

Please sign in to comment.