Skip to content

Commit

Permalink
Feature: Keep scroll offset when changing item size (#14914)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 committed Mar 10, 2024
1 parent 6eca67c commit c79b2cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/ColumnLayoutPage.xaml
Expand Up @@ -191,6 +191,7 @@
IsRightTapEnabled="True"
IsTabStop="True"
ItemsSource="{x:Bind CollectionViewSource.View, Mode=OneWay}"
Loaded="FileList_Loaded"
PreviewKeyDown="FileList_PreviewKeyDown"
RightTapped="FileList_RightTapped"
ScrollViewer.IsScrollInertiaEnabled="True"
Expand Down
15 changes: 12 additions & 3 deletions src/Files.App/Views/Layouts/ColumnLayoutPage.xaml.cs
Expand Up @@ -40,7 +40,7 @@ public sealed partial class ColumnLayoutPage : BaseGroupableLayoutPage

protected override ListViewBase ListViewBase => FileList;
protected override SemanticZoom RootZoom => RootGridZoom;

public ScrollViewer? ContentScroller { get; private set; }

/// <summary>
/// Row height in the Columns View
Expand All @@ -66,6 +66,11 @@ public ColumnLayoutPage() : base()

// Methods

private void FileList_Loaded(object sender, RoutedEventArgs e)
{
ContentScroller = FileList.FindDescendant<ScrollViewer>(x => x.Name == "ScrollViewer");
}

private void ColumnViewBase_GotFocus(object sender, RoutedEventArgs e)
{
if (FileList.SelectedItem == null && openedFolderPresenter != null)
Expand Down Expand Up @@ -167,14 +172,18 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)

private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// TODO keep scroll position when changing styles (see details view)

if (e.PropertyName == nameof(ILayoutSettingsService.ColumnsViewSize))
{
// Get current scroll position
var previousOffset = ContentScroller?.VerticalOffset;

NotifyPropertyChanged(nameof(RowHeight));

// Update the container style to match the item size
SetItemContainerStyle();

// Restore correct scroll position
ContentScroller?.ChangeView(null, previousOffset, null);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/GridLayoutPage.xaml
Expand Up @@ -778,6 +778,7 @@
IsTabStop="True"
ItemContainerTransitions="{x:Null}"
ItemsSource="{x:Bind CollectionViewSource.View, Mode=OneWay}"
Loaded="FileList_Loaded"
PreviewKeyDown="FileList_PreviewKeyDown"
ScrollViewer.IsHorizontalScrollChainingEnabled="False"
SelectionChanged="FileList_SelectionChanged"
Expand Down
14 changes: 13 additions & 1 deletion src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs
Expand Up @@ -28,6 +28,8 @@ public sealed partial class GridLayoutPage : BaseGroupableLayoutPage

// Properties

public ScrollViewer? ContentScroller { get; private set; }

protected override ListViewBase ListViewBase => FileList;
protected override SemanticZoom RootZoom => RootGridZoom;

Expand Down Expand Up @@ -147,7 +149,9 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)

private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// TODO keep scroll position when changing styles (see details view)
// Get current scroll position
var previousHorizontalOffset = ContentScroller?.HorizontalOffset;
var previousVerticalOffset = ContentScroller?.VerticalOffset;

if (e.PropertyName == nameof(ILayoutSettingsService.ListViewSize))
{
Expand Down Expand Up @@ -175,6 +179,9 @@ private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChang

FolderSettings_IconHeightChanged();
}

// Restore correct scroll position
ContentScroller?.ChangeView(previousHorizontalOffset, previousVerticalOffset, null);
}

private async void FolderSettings_LayoutModeChangeRequested(object? sender, LayoutModeEventArgs e)
Expand Down Expand Up @@ -249,6 +256,11 @@ private void SetItemContainerStyle()
}
}

private void FileList_Loaded(object sender, RoutedEventArgs e)
{
ContentScroller = FileList.FindDescendant<ScrollViewer>(x => x.Name == "ScrollViewer");
}

protected override void FileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
base.FileList_SelectionChanged(sender, e);
Expand Down

0 comments on commit c79b2cb

Please sign in to comment.