Skip to content

Commit 980ea7e

Browse files
authored
Feature: Auto restore tabs if the app crashes (#12005)
1 parent f978298 commit 980ea7e

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

src/Files.App/App.xaml.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
using Windows.ApplicationModel;
4848
using Windows.ApplicationModel.DataTransfer;
4949
using Windows.Storage;
50+
using Windows.System;
5051
using Windows.UI.Notifications;
5152

5253
namespace Files.App
@@ -445,19 +446,41 @@ private static void AppUnhandledException(Exception ex, bool shouldShowNotificat
445446
{
446447
Buttons =
447448
{
448-
new ToastButton("ExceptionNotificationReportButton".GetLocalizedResource(), "report")
449+
new ToastButton("ExceptionNotificationReportButton".GetLocalizedResource(), Constants.GitHub.BugReportUrl)
449450
{
450-
ActivationType = ToastActivationType.Foreground
451+
ActivationType = ToastActivationType.Protocol
451452
}
452453
}
453-
}
454+
},
455+
ActivationType = ToastActivationType.Protocol
454456
};
455457

456458
// Create the toast notification
457459
var toastNotif = new ToastNotification(toastContent.GetXml());
458460

459461
// And send the notification
460462
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
463+
464+
// Restart the app
465+
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
466+
var lastSessionTabList = userSettingsService.GeneralSettingsService.LastSessionTabList;
467+
468+
if (userSettingsService.GeneralSettingsService.LastCrashedTabList?.SequenceEqual(lastSessionTabList) ?? false)
469+
{
470+
// Avoid infinite restart loop
471+
userSettingsService.GeneralSettingsService.LastSessionTabList = null;
472+
}
473+
else
474+
{
475+
userSettingsService.AppSettingsService.RestoreTabsOnStartup = true;
476+
userSettingsService.GeneralSettingsService.LastCrashedTabList = lastSessionTabList;
477+
}
478+
479+
Window.DispatcherQueue.EnqueueAsync(async () =>
480+
{
481+
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
482+
}).Wait(1000);
483+
Process.GetCurrentProcess().Kill();
461484
}
462485

463486
public static void CloseApp()

src/Files.App/MainWindow.xaml.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,6 @@ public async Task InitializeApplication(object activatedEventArgs)
139139
}
140140
break;
141141

142-
case IToastNotificationActivatedEventArgs eventArgsForNotification:
143-
if (eventArgsForNotification.Argument == "report")
144-
{
145-
await Windows.System.Launcher.LaunchUriAsync(new Uri(Constants.GitHub.BugReportUrl));
146-
}
147-
break;
148-
149-
case IStartupTaskActivatedEventArgs:
150-
break;
151-
152142
case IFileActivatedEventArgs fileArgs:
153143
var index = 0;
154144
if (rootFrame.Content is null)

src/Files.App/ServicesImplementation/Settings/GeneralSettingsService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public List<string> LastSessionTabList
6363
set => Set(value);
6464
}
6565

66+
public List<string> LastCrashedTabList
67+
{
68+
get => Get<List<string>>(null);
69+
set => Set(value);
70+
}
71+
6672
public DateTimeFormats DateTimeFormat
6773
{
6874
get => Get(DateTimeFormats.Application);

src/Files.App/ServicesImplementation/Settings/UserSettingsService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public override object ExportSettings()
6969

7070
// Remove session settings
7171
export.Remove(nameof(GeneralSettingsService.LastSessionTabList));
72+
export.Remove(nameof(GeneralSettingsService.LastCrashedTabList));
7273

7374
return JsonSettingsSerializer.SerializeToJson(export);
7475
}

src/Files.Backend/Services/Settings/IGeneralSettingsService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
4646
/// </summary>
4747
List<string> LastSessionTabList { get; set; }
4848

49+
/// <summary>
50+
/// A list containing paths of the tabs from the previous session that crashed.
51+
/// </summary>
52+
List<string> LastCrashedTabList { get; set; }
53+
4954
/// <summary>
5055
/// Gets or sets a value indicating which date and time format to use.
5156
/// </summary>

0 commit comments

Comments
 (0)