diff --git a/src/Files.App/App.xaml.cs b/src/Files.App/App.xaml.cs index 653422bc2ff4..baef7548d023 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 21759ce74dbb..0d334059557f 100644 --- a/src/Files.App/BaseLayout.cs +++ b/src/Files.App/BaseLayout.cs @@ -556,6 +556,8 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) public async void ItemContextFlyout_Opening(object? sender, object e) { + App.LastOpenedFlyout = sender as CommandBarFlyout; + try { if (!IsItemSelected && ((sender as CommandBarFlyout)?.Target as ListViewItem)?.Content is ListedItem li) // Workaround for item sometimes not getting selected @@ -574,6 +576,8 @@ public async void ItemContextFlyout_Opening(object? sender, object e) public async void BaseContextFlyout_Opening(object? sender, object e) { + App.LastOpenedFlyout = sender as CommandBarFlyout; + try { // Reset menu max height diff --git a/src/Files.App/UserControls/SidebarControl.xaml.cs b/src/Files.App/UserControls/SidebarControl.xaml.cs index 90667df4cf35..3199067348fc 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 30a85ebc2f78..24bf21f11c75 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 e71c44ae04e5..b7ed0572f4d8 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 5b1613765d68..fbef8198e88c 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;