@@ -228,6 +228,17 @@ public bool IsCurrentInstance
228228 }
229229 }
230230
231+ private bool _IsActivePaneLocked ;
232+ public bool IsActivePaneLocked
233+ {
234+ get => _IsActivePaneLocked ;
235+ set
236+ {
237+ if ( _IsActivePaneLocked != value )
238+ _IsActivePaneLocked = value ;
239+ }
240+ }
241+
231242 // Events
232243
233244 public static event EventHandler < ShellPanesPage > ? CurrentInstanceChanged ;
@@ -378,6 +389,12 @@ public void FocusActivePane()
378389 GetPane ( 1 ) ? . Focus ( FocusState . Programmatic ) ;
379390 }
380391
392+ /// <inheritdoc/>
393+ public void LockActivePane ( )
394+ {
395+ IsActivePaneLocked = true ;
396+ }
397+
381398 /// <inheritdoc/>
382399 public IEnumerable < ModernShellPage > GetPanes ( )
383400 {
@@ -656,9 +673,19 @@ private void Pane_Loaded(object sender, RoutedEventArgs e)
656673
657674 private void Pane_GettingFocus ( UIElement sender , GettingFocusEventArgs args )
658675 {
659- // Workaround for https://github.com/files-community/Files/issues/15397
660- if ( args ? . NewFocusedElement is not null && args . NewFocusedElement is not ( ListViewItem or GridViewItem or ListView or GridView or TextBox ) )
676+ // Layout changes can cause the active pane to lose focus, to prevent this
677+ // from happening, the pane is locked and focus is restored when file loading
678+ // completes.
679+ // The locking occurs in the `LayoutModeChangeRequested()` method in ShellViewModel.cs.
680+ // The focus is restored in the `RefreshItem()` method in BaseLayoutPage.cs
681+ // See https://github.com/files-community/Files/issues/15397
682+ // See https://github.com/files-community/Files/issues/16530
683+
684+ if ( IsActivePaneLocked )
685+ {
686+ IsActivePaneLocked = false ;
661687 args . TryCancel ( ) ;
688+ }
662689 }
663690
664691 private void Pane_ContentChanged ( object ? sender , TabBarItemParameter e )
0 commit comments