From 5a280cf0bd1192a3d0b6e3f9c126c25746f6c2fd Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:27:49 +0900 Subject: [PATCH] Added support for background launch --- src/Files.App/App.xaml.cs | 67 ++++++++++++++----- .../Helpers/Application/AppLifecycleHelper.cs | 2 - src/Files.App/Utils/Taskbar/SystemTrayIcon.cs | 11 ++- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/Files.App/App.xaml.cs b/src/Files.App/App.xaml.cs index f5a6ac816912..2aefd6977a92 100644 --- a/src/Files.App/App.xaml.cs +++ b/src/Files.App/App.xaml.cs @@ -65,17 +65,21 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) async Task ActivateAsync() { - // Initialize and activate MainWindow - MainWindow.Instance.Activate(); + // Get AppActivationArguments + var appActivationArguments = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs(); + var isStartupTask = appActivationArguments.Data is Windows.ApplicationModel.Activation.IStartupTaskActivatedEventArgs; - // Wait for the Window to initialize - await Task.Delay(10); + if (!isStartupTask) + { + // Initialize and activate MainWindow + MainWindow.Instance.Activate(); - SplashScreenLoadingTCS = new TaskCompletionSource(); - MainWindow.Instance.ShowSplashScreen(); + // Wait for the Window to initialize + await Task.Delay(10); - // Get AppActivationArguments - var appActivationArguments = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs(); + SplashScreenLoadingTCS = new TaskCompletionSource(); + MainWindow.Instance.ShowSplashScreen(); + } // Start tracking app usage if (appActivationArguments.Data is Windows.ApplicationModel.Activation.IActivatedEventArgs activationEventArgs) @@ -90,6 +94,21 @@ async Task ActivateAsync() AppLifecycleHelper.ConfigureAppCenter(); #endif + var userSettingsService = Ioc.Default.GetRequiredService(); + var isLeaveAppRunning = userSettingsService.GeneralSettingsService.LeaveAppRunning; + + if (isStartupTask && !isLeaveAppRunning) + { + // Initialize and activate MainWindow + MainWindow.Instance.Activate(); + + // Wait for the Window to initialize + await Task.Delay(10); + + SplashScreenLoadingTCS = new TaskCompletionSource(); + MainWindow.Instance.ShowSplashScreen(); + } + // TODO: Replace with DI QuickAccessManager = Ioc.Default.GetRequiredService(); HistoryWrapper = Ioc.Default.GetRequiredService(); @@ -105,15 +124,28 @@ async Task ActivateAsync() Logger.LogInformation($"App launched. Launch args type: {appActivationArguments.Data.GetType().Name}"); - // Wait for the UI to update - await SplashScreenLoadingTCS!.Task.WithTimeoutAsync(TimeSpan.FromMilliseconds(500)); - SplashScreenLoadingTCS = null; + if (!(isStartupTask && isLeaveAppRunning)) + { + // Wait for the UI to update + await SplashScreenLoadingTCS!.Task.WithTimeoutAsync(TimeSpan.FromMilliseconds(500)); + SplashScreenLoadingTCS = null; - // Create a system tray icon - SystemTrayIcon = new SystemTrayIcon().Show(); + // Create a system tray icon + SystemTrayIcon = new SystemTrayIcon().Show(); - _ = AppLifecycleHelper.InitializeAppComponentsAsync(); - _ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data); + _ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data); + } + + await AppLifecycleHelper.InitializeAppComponentsAsync(); + + if (isStartupTask && isLeaveAppRunning) + { + // Create a system tray icon when initialization is done + SystemTrayIcon = new SystemTrayIcon().Show(); + App.Current.Exit(); + } + else + await AppLifecycleHelper.CheckAppUpdate(); } } @@ -122,11 +154,12 @@ async Task ActivateAsync() /// public async Task OnActivatedAsync(AppActivationArguments activatedEventArgs) { - Logger.LogInformation($"The app is being activated. Activation type: {activatedEventArgs.Data.GetType().Name}"); + var activatedEventArgsData = activatedEventArgs.Data; + Logger.LogInformation($"The app is being activated. Activation type: {activatedEventArgsData.GetType().Name}"); // InitializeApplication accesses UI, needs to be called on UI thread await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() - => MainWindow.Instance.InitializeApplicationAsync(activatedEventArgs.Data)); + => MainWindow.Instance.InitializeApplicationAsync(activatedEventArgsData)); } /// diff --git a/src/Files.App/Helpers/Application/AppLifecycleHelper.cs b/src/Files.App/Helpers/Application/AppLifecycleHelper.cs index 52d05abd8b8a..0a977ff7f1f2 100644 --- a/src/Files.App/Helpers/Application/AppLifecycleHelper.cs +++ b/src/Files.App/Helpers/Application/AppLifecycleHelper.cs @@ -52,8 +52,6 @@ await Task.WhenAll( FileTagsHelper.UpdateTagsDb(); - await CheckAppUpdate(); - static Task OptionalTaskAsync(Task task, bool condition) { if (condition) diff --git a/src/Files.App/Utils/Taskbar/SystemTrayIcon.cs b/src/Files.App/Utils/Taskbar/SystemTrayIcon.cs index 1e0ab7b3ce69..1bf0e530fe5a 100644 --- a/src/Files.App/Utils/Taskbar/SystemTrayIcon.cs +++ b/src/Files.App/Utils/Taskbar/SystemTrayIcon.cs @@ -35,7 +35,7 @@ public class SystemTrayIcon : IDisposable private bool _notifyIconCreated; - private DispatcherQueueTimer _timer; + private DateTime _lastLaunchDate; // Properties @@ -266,18 +266,17 @@ private void ShowContextMenu() private void OnLeftClicked() { // Prevents duplicate launch - if (_timer?.IsRunning ?? false) + if (DateTime.Now - _lastLaunchDate < TimeSpan.FromSeconds(1)) return; if (Program.Pool is not null) { - _timer ??= DispatcherQueue.GetForCurrentThread().CreateTimer(); - _timer.Interval = TimeSpan.FromSeconds(1); - _timer.IsRepeating = false; - _timer.Start(); + _lastLaunchDate = DateTime.Now; _ = Launcher.LaunchUriAsync(new Uri("files-uwp:")); } + else + MainWindow.Instance.Activate(); } private void OnDocumentationClicked()