diff --git a/src/Files.App/App.xaml.cs b/src/Files.App/App.xaml.cs index 653422bc2ff47..baef7548d0237 100644 --- a/src/Files.App/App.xaml.cs +++ b/src/Files.App/App.xaml.cs @@ -2,7 +2,6 @@ using CommunityToolkit.WinUI; using CommunityToolkit.WinUI.Helpers; using CommunityToolkit.WinUI.Notifications; -using Files.App.Controllers; using Files.App.DataModels; using Files.App.Extensions; using Files.App.Filesystem; @@ -30,6 +29,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; using Microsoft.Windows.AppLifecycle; using System; using System.Diagnostics; @@ -54,6 +54,7 @@ public partial class App : Application private static bool ShowErrorNotification = false; public static string OutputPath { get; set; } + public static CommandBarFlyout? LastOpenedFlyout { get; set; } public static StorageHistoryWrapper HistoryWrapper = new StorageHistoryWrapper(); public static SettingsViewModel AppSettings { get; private set; } public static AppModel AppModel { get; private set; } @@ -287,6 +288,15 @@ private async void Window_Closed(object sender, WindowEventArgs args) { // Save application state and stop any background activity + // A Workaround for the crash (#10110) + if (LastOpenedFlyout?.IsOpen ?? false) + { + args.Handled = true; + LastOpenedFlyout.Closed += (sender, e) => App.Current.Exit(); + LastOpenedFlyout.Hide(); + return; + } + await Task.Yield(); // Method can take a long time, make sure the window is hidden SaveSessionTabs(); diff --git a/src/Files.App/BaseLayout.cs b/src/Files.App/BaseLayout.cs index 21759ce74dbbb..3d6f5deb08e4c 100644 --- a/src/Files.App/BaseLayout.cs +++ b/src/Files.App/BaseLayout.cs @@ -563,6 +563,8 @@ public async void ItemContextFlyout_Opening(object? sender, object e) if (IsItemSelected) await LoadMenuItemsAsync(); + + App.LastOpenedFlyout = sender as CommandBarFlyout; } catch (Exception error) { @@ -607,6 +609,8 @@ public async void BaseContextFlyout_Opening(object? sender, object e) else RemoveOverflow(BaseContextMenuFlyout); } + + App.LastOpenedFlyout = sender as CommandBarFlyout; } catch (Exception error) { diff --git a/src/Files.App/UserControls/SidebarControl.xaml.cs b/src/Files.App/UserControls/SidebarControl.xaml.cs index 90667df4cf35e..3199067348fca 100644 --- a/src/Files.App/UserControls/SidebarControl.xaml.cs +++ b/src/Files.App/UserControls/SidebarControl.xaml.cs @@ -420,6 +420,7 @@ private void PaneRoot_RightTapped(object sender, RightTappedRoutedEventArgs e) private void NavigationViewItem_RightTapped(object sender, RightTappedRoutedEventArgs e) { var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full }; + itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout; if (sender is not NavigationViewItem sidebarItem || sidebarItem.DataContext is not INavigationControlItem item) return; diff --git a/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs b/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs index 30a85ebc2f78a..24bf21f11c756 100644 --- a/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs +++ b/src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs @@ -65,6 +65,7 @@ private async void FileTagItem_ItemClick(object sender, ItemClickEventArgs e) private void Item_RightTapped(object sender, RightTappedRoutedEventArgs e) { var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full }; + itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout; if (sender is not StackPanel tagsItemsStackPanel || tagsItemsStackPanel.DataContext is not FileTagsItemViewModel item) return; itemContextMenuFlyout.ShowAt(tagsItemsStackPanel, new FlyoutShowOptions { Position = e.GetPosition(tagsItemsStackPanel) }); diff --git a/src/Files.App/UserControls/Widgets/HomePageWidget.cs b/src/Files.App/UserControls/Widgets/HomePageWidget.cs index e71c44ae04e52..b7ed0572f4d8f 100644 --- a/src/Files.App/UserControls/Widgets/HomePageWidget.cs +++ b/src/Files.App/UserControls/Widgets/HomePageWidget.cs @@ -47,6 +47,7 @@ public abstract class HomePageWidget : UserControl public void Button_RightTapped(object sender, RightTappedRoutedEventArgs e) { var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full }; + itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout; if (sender is not Button widgetCardItem || widgetCardItem.DataContext is not WidgetCardItem item) return; diff --git a/src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs b/src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs index 5b1613765d681..fbef8198e88ca 100644 --- a/src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs +++ b/src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs @@ -109,6 +109,7 @@ public RecentFilesWidget() private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e) { var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full }; + itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout; if (sender is not Grid recentItemsGrid || recentItemsGrid.DataContext is not RecentItem item) return;