@@ -65,17 +65,21 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
6565
6666 async Task ActivateAsync ( )
6767 {
68- // Initialize and activate MainWindow
69- MainWindow . Instance . Activate ( ) ;
68+ // Get AppActivationArguments
69+ var appActivationArguments = Microsoft . Windows . AppLifecycle . AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
70+ var isStartupTask = appActivationArguments . Data is Windows . ApplicationModel . Activation . IStartupTaskActivatedEventArgs ;
7071
71- // Wait for the Window to initialize
72- await Task . Delay ( 10 ) ;
72+ if ( ! isStartupTask )
73+ {
74+ // Initialize and activate MainWindow
75+ MainWindow . Instance . Activate ( ) ;
7376
74- SplashScreenLoadingTCS = new TaskCompletionSource ( ) ;
75- MainWindow . Instance . ShowSplashScreen ( ) ;
77+ // Wait for the Window to initialize
78+ await Task . Delay ( 10 ) ;
7679
77- // Get AppActivationArguments
78- var appActivationArguments = Microsoft . Windows . AppLifecycle . AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
80+ SplashScreenLoadingTCS = new TaskCompletionSource ( ) ;
81+ MainWindow . Instance . ShowSplashScreen ( ) ;
82+ }
7983
8084 // Start tracking app usage
8185 if ( appActivationArguments . Data is Windows . ApplicationModel . Activation . IActivatedEventArgs activationEventArgs )
@@ -90,6 +94,21 @@ async Task ActivateAsync()
9094 AppLifecycleHelper . ConfigureAppCenter ( ) ;
9195#endif
9296
97+ var userSettingsService = Ioc . Default . GetRequiredService < IUserSettingsService > ( ) ;
98+ var isLeaveAppRunning = userSettingsService . GeneralSettingsService . LeaveAppRunning ;
99+
100+ if ( isStartupTask && ! isLeaveAppRunning )
101+ {
102+ // Initialize and activate MainWindow
103+ MainWindow . Instance . Activate ( ) ;
104+
105+ // Wait for the Window to initialize
106+ await Task . Delay ( 10 ) ;
107+
108+ SplashScreenLoadingTCS = new TaskCompletionSource ( ) ;
109+ MainWindow . Instance . ShowSplashScreen ( ) ;
110+ }
111+
93112 // TODO: Replace with DI
94113 QuickAccessManager = Ioc . Default . GetRequiredService < QuickAccessManager > ( ) ;
95114 HistoryWrapper = Ioc . Default . GetRequiredService < StorageHistoryWrapper > ( ) ;
@@ -105,15 +124,28 @@ async Task ActivateAsync()
105124
106125 Logger . LogInformation ( $ "App launched. Launch args type: { appActivationArguments . Data . GetType ( ) . Name } ") ;
107126
108- // Wait for the UI to update
109- await SplashScreenLoadingTCS ! . Task . WithTimeoutAsync ( TimeSpan . FromMilliseconds ( 500 ) ) ;
110- SplashScreenLoadingTCS = null ;
127+ if ( ! ( isStartupTask && isLeaveAppRunning ) )
128+ {
129+ // Wait for the UI to update
130+ await SplashScreenLoadingTCS ! . Task . WithTimeoutAsync ( TimeSpan . FromMilliseconds ( 500 ) ) ;
131+ SplashScreenLoadingTCS = null ;
111132
112- // Create a system tray icon
113- SystemTrayIcon = new SystemTrayIcon ( ) . Show ( ) ;
133+ // Create a system tray icon
134+ SystemTrayIcon = new SystemTrayIcon ( ) . Show ( ) ;
114135
115- _ = AppLifecycleHelper . InitializeAppComponentsAsync ( ) ;
116- _ = MainWindow . Instance . InitializeApplicationAsync ( appActivationArguments . Data ) ;
136+ _ = MainWindow . Instance . InitializeApplicationAsync ( appActivationArguments . Data ) ;
137+ }
138+
139+ await AppLifecycleHelper . InitializeAppComponentsAsync ( ) ;
140+
141+ if ( isStartupTask && isLeaveAppRunning )
142+ {
143+ // Create a system tray icon when initialization is done
144+ SystemTrayIcon = new SystemTrayIcon ( ) . Show ( ) ;
145+ App . Current . Exit ( ) ;
146+ }
147+ else
148+ await AppLifecycleHelper . CheckAppUpdate ( ) ;
117149 }
118150 }
119151
@@ -122,11 +154,12 @@ async Task ActivateAsync()
122154 /// </summary>
123155 public async Task OnActivatedAsync ( AppActivationArguments activatedEventArgs )
124156 {
125- Logger . LogInformation ( $ "The app is being activated. Activation type: { activatedEventArgs . Data . GetType ( ) . Name } ") ;
157+ var activatedEventArgsData = activatedEventArgs . Data ;
158+ Logger . LogInformation ( $ "The app is being activated. Activation type: { activatedEventArgsData . GetType ( ) . Name } ") ;
126159
127160 // InitializeApplication accesses UI, needs to be called on UI thread
128161 await MainWindow . Instance . DispatcherQueue . EnqueueOrInvokeAsync ( ( )
129- => MainWindow . Instance . InitializeApplicationAsync ( activatedEventArgs . Data ) ) ;
162+ => MainWindow . Instance . InitializeApplicationAsync ( activatedEventArgsData ) ) ;
130163 }
131164
132165 /// <summary>
0 commit comments