@@ -372,13 +372,6 @@ protected virtual void BaseFolderSettings_LayoutModeChangeRequested(object? send
372372
373373 if ( layoutType != ParentShellPageInstance . CurrentPageType )
374374 {
375- // Layout changes can cause the active pane to lose focus. To prevent this,
376- // the pane is locked here and focus is restored when file loading completes
377- // in the RefreshItem() method in BaseLayoutPage.cs.
378- // See https://github.com/files-community/Files/issues/15397
379- // See https://github.com/files-community/Files/issues/16530
380- ParentShellPageInstance ! . PaneHolder . LockActivePane ( ) ;
381-
382375 ParentShellPageInstance . NavigateWithArguments ( layoutType , new NavigationArguments ( )
383376 {
384377 NavPathParam = navigationArguments ! . NavPathParam ,
@@ -395,6 +388,12 @@ protected virtual void BaseFolderSettings_LayoutModeChangeRequested(object? send
395388 }
396389
397390 ParentShellPageInstance . ShellViewModel . UpdateEmptyTextType ( ) ;
391+
392+ // Focus on the active pane in case it was lost during the layout switch.
393+ // Allthough the focus is also set from SetSelectedItemsOnNavigation,
394+ // that is only called when switching between a Grid based layout and Details,
395+ // not between different Grid based layouts (eg. List and Cards).
396+ ParentShellPageInstance ! . PaneHolder . FocusActivePane ( ) ;
398397 }
399398 }
400399
@@ -513,7 +512,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
513512 BaseContextMenuFlyout . Opening += BaseContextFlyout_Opening ;
514513 }
515514
516- public void SetSelectedItemsOnNavigation ( )
515+ public async void SetSelectedItemsOnNavigation ( )
517516 {
518517 try
519518 {
@@ -529,10 +528,14 @@ navigationArguments.SelectItems is not null &&
529528 ItemManipulationModel . SetSelectedItems ( listedItemsToSelect ) ;
530529 ItemManipulationModel . FocusSelectedItems ( ) ;
531530 }
532- else if ( navigationArguments is not null && navigationArguments . FocusOnNavigation )
531+ else if ( navigationArguments is not null && ParentShellPageInstance ! . InstanceViewModel . FolderSettings . LayoutMode is not FolderLayoutModes . ColumnView )
533532 {
534- // Set focus on layout specific file list control
535- ItemManipulationModel . FocusFileList ( ) ;
533+ // Delay to ensure the new layout is loaded
534+ if ( navigationArguments . IsLayoutSwitch )
535+ await Task . Delay ( 100 ) ;
536+
537+ // Focus on the active pane in case it was lost during navigation
538+ ParentShellPageInstance ! . PaneHolder . FocusActivePane ( ) ;
536539 }
537540 }
538541 catch ( Exception ) { }
@@ -1246,14 +1249,6 @@ private void RefreshItem(SelectorItem container, object item, bool inRecycleQueu
12461249 await ParentShellPageInstance ! . ShellViewModel . LoadExtendedItemPropertiesAsync ( listedItem ) ;
12471250 if ( ParentShellPageInstance . ShellViewModel . EnabledGitProperties is not GitProperties . None && listedItem is IGitItem gitItem )
12481251 await ParentShellPageInstance . ShellViewModel . LoadGitPropertiesAsync ( gitItem ) ;
1249-
1250- // Layout changes can cause the active pane to lose focus. To prevent this,
1251- // the pane is locked in LayoutModeChangeRequested() and focus is restored here
1252- // when file loading completes.
1253- // See https://github.com/files-community/Files/issues/15397
1254- // See https://github.com/files-community/Files/issues/16530
1255- if ( ParentShellPageInstance . IsCurrentPane && ParentShellPageInstance . InstanceViewModel . FolderSettings . LayoutMode is not FolderLayoutModes . ColumnView )
1256- ItemManipulationModel . FocusFileList ( ) ;
12571252 } ) ;
12581253 }
12591254 }
0 commit comments