Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijay-Nirmal committed Apr 12, 2020
1 parent b57e6df commit 3884163
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public sealed partial class ListViewExtensionsPage : Page, IXamlRenderListener
private ListView sampleListView;
private TextBlock indexInput;
private TextBlock itemPlacementInput;
private TextBlock disableAnimationInput;
private TextBlock scrollIfVisibileInput;
private CheckBox disableAnimationInput;
private CheckBox scrollIfVisibileInput;
private TextBlock additionalHorizontalOffsetInput;
private TextBlock additionalVerticalOffsetInput;

Expand All @@ -31,8 +31,8 @@ public void OnXamlRendered(FrameworkElement control)
sampleListView = control.FindChildByName("SampleListView") as ListView;
indexInput = control.FindChildByName("IndexInput") as TextBlock;
itemPlacementInput = control.FindChildByName("ItemPlacementInput") as TextBlock;
disableAnimationInput = control.FindChildByName("DisableAnimationInput") as TextBlock;
scrollIfVisibileInput = control.FindChildByName("ScrollIfVisibileInput") as TextBlock;
disableAnimationInput = control.FindChildByName("DisableAnimationInput") as CheckBox;
scrollIfVisibileInput = control.FindChildByName("ScrollIfVisibileInput") as CheckBox;
additionalHorizontalOffsetInput = control.FindChildByName("AdditionalHorizontalOffsetInput") as TextBlock;
additionalVerticalOffsetInput = control.FindChildByName("AdditionalVerticalOffsetInput") as TextBlock;

Expand All @@ -46,12 +46,12 @@ private void Load()
{
SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (sender, args) =>
{
var index = int.Parse(indexInput.Text);
var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput.Text);
var disableAnimation = bool.Parse(disableAnimationInput.Text);
var scrollIfVisibile = bool.Parse(scrollIfVisibileInput.Text);
var additionalHorizontalOffset = int.Parse(additionalHorizontalOffsetInput.Text);
var additionalVerticalOffset = int.Parse(additionalVerticalOffsetInput.Text);
var index = int.Parse(indexInput?.Text);
var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput?.Text);
var disableAnimation = disableAnimationInput?.IsChecked ?? false;
var scrollIfVisibile = scrollIfVisibileInput?.IsChecked ?? true;
var additionalHorizontalOffset = int.Parse(additionalHorizontalOffsetInput?.Text);
var additionalVerticalOffset = int.Parse(additionalVerticalOffsetInput?.Text);
sampleListView.SmoothScrollIntoViewWithIndex(index, itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<Grid>
<TextBlock x:Name="IndexInput" x:Load="False" Text="@[Index:Slider:100:0-200]" Visibility="Collapsed"></TextBlock>
<TextBlock x:Name="ItemPlacementInput" x:Load="False" Text="@[Item Placement:Enum:ItemPlacement.Default]" Visibility="Collapsed"></TextBlock>
<TextBlock x:Name="DisableAnimationInput" x:Load="False" Text="@[Disable Animation:Bool:False]" Visibility="Collapsed"></TextBlock>
<TextBlock x:Name="ScrollIfVisibileInput" x:Load="False" Text="@[Scroll If Visibile:Bool:True]" Visibility="Collapsed"></TextBlock>
<CheckBox x:Name="DisableAnimationInput" x:Load="False" IsChecked="@[Disable Animation:Bool:False]" Visibility="Collapsed"></CheckBox>
<CheckBox x:Name="ScrollIfVisibileInput" x:Load="False" IsChecked="@[Scroll If Visibile:Bool:True]" Visibility="Collapsed"></CheckBox>
<TextBlock x:Name="AdditionalHorizontalOffsetInput" x:Load="False" Text="@[Additional Horizontal Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>
<TextBlock x:Name="AdditionalVerticalOffsetInput" x:Load="False" Text="@[Additional Vertical Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public enum ItemPlacement
Top,

/// <summary>
/// Aligned centre
/// Aligned center
/// </summary>
Centre,
Center,

/// <summary>
/// Aligned right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@ public static class SmoothScrollIntoView
/// Smooth scrolling the list to bring the specified index into view
/// </summary>
/// <param name="listViewBase">List to scroll</param>
/// <param name="index">The intex to bring into view</param>
/// <param name="index">The index to bring into view. Index can be negative.</param>
/// <param name="itemPlacement">Set the item placement after scrolling</param>
/// <param name="disableAnimation">Set true to disable animation</param>
/// <param name="scrollIfVisibile">Set true to disable scrolling when the corresponding item is in view</param>
/// <param name="scrollIfVisible">Set false to disable scrolling when the corresponding item is in view</param>
/// <param name="additionalHorizontalOffset">Adds additional horizontal offset</param>
/// <param name="additionalVerticalOffset">Adds additional vertical offset</param>
/// <returns>Note: Even though this return <see cref="Task"/>, it will not wait until the scrolling completes</returns>
public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listViewBase, int index, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0)
public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listViewBase, int index, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisible = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0)
{
if (index > (listViewBase.Items.Count - 1))
{
index = listViewBase.Items.Count - 1;
}

if (index < -listViewBase.Items.Count)
{
index = -listViewBase.Items.Count;
}

index = (index < 0) ? (index + listViewBase.Items.Count) : index;

bool isVirtualizing = default;
Expand All @@ -42,6 +47,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
var scrollViewer = listViewBase.FindDescendant<ScrollViewer>();
var selectorItem = listViewBase.ContainerFromIndex(index) as SelectorItem;

// If selectorItem is null then the panel is virtualized.
// So in order to get the container of the item we need to scroll to that item first and then use ContainerFromIndex
if (selectorItem == null)
{
isVirtualizing = true;
Expand Down Expand Up @@ -70,6 +77,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
var transform = selectorItem.TransformToVisual((UIElement)scrollViewer.Content);
var position = transform.TransformPoint(new Point(0, 0));

// Scrolling back to previous position
if (isVirtualizing)
{
var tcs = new TaskCompletionSource<object>();
Expand Down Expand Up @@ -104,7 +112,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie

double finalXPosition, finalYPosition;

if (!scrollIfVisibile && (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) && (previousYOffset <= maxYPosition && previousYOffset >= minYPosition))
// If the Item is in view and scrollIfVisible is false then we don't need to scroll
if (!scrollIfVisible && (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) && (previousYOffset <= maxYPosition && previousYOffset >= minYPosition))
{
finalXPosition = previousXOffset;
finalYPosition = previousYOffset;
Expand Down Expand Up @@ -152,7 +161,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
finalYPosition = maxYPosition + additionalVerticalOffset;
break;

case ItemPlacement.Centre:
case ItemPlacement.Center:
var centreX = (listViewBaseWidth - selectorItemWidth) / 2.0;
var centreY = (listViewBaseHeight - selectorItemHeight) / 2.0;
finalXPosition = maxXPosition - centreX + additionalHorizontalOffset;
Expand Down

0 comments on commit 3884163

Please sign in to comment.