Skip to content

Commit ebf0c00

Browse files
Code Quality: Avoid crash when focusing file list after window is closed (#17707)
Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com>
1 parent d1988ba commit ebf0c00

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,20 @@ await Task.WhenAll(ParentShellPageInstance.SlimContentPage.SelectedItems.Select(
171171

172172
protected virtual void ItemManipulationModel_FocusFileListInvoked(object? sender, EventArgs e)
173173
{
174-
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
175-
var isFileListFocused = DependencyObjectHelpers.FindParent<ListViewBase>(focusedElement) == ItemsControl;
176-
if (!isFileListFocused)
177-
ListViewBase.Focus(FocusState.Programmatic);
174+
try
175+
{
176+
if (App.AppModel.IsMainWindowClosed)
177+
return;
178+
179+
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
180+
var isFileListFocused = DependencyObjectHelpers.FindParent<ListViewBase>(focusedElement) == ItemsControl;
181+
if (!isFileListFocused)
182+
ListViewBase.Focus(FocusState.Programmatic);
183+
}
184+
catch
185+
{
186+
// Handle exception in case the window is closed during the operation
187+
}
178188
}
179189

180190
protected virtual void ItemManipulationModel_SelectAllItemsInvoked(object? sender, EventArgs e)

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public async void MultitaskingControl_CurrentInstanceChanged(object? sender, Cur
171171

172172
// Focus the content of the selected tab item (this also avoids an issue where the Omnibar sometimes steals the focus)
173173
await Task.Delay(100);
174-
if (ContentPageContext?.ShellPage?.PaneHolder != null)
174+
if (!App.AppModel.IsMainWindowClosed && ContentPageContext?.ShellPage?.PaneHolder != null)
175175
ContentPageContext.ShellPage.PaneHolder.FocusActivePane();
176176
}
177177

0 commit comments

Comments
 (0)