@@ -17,6 +17,8 @@ namespace Files.App
1717 /// </summary>
1818 public partial class App : Application
1919 {
20+ private static SystemTrayIcon ? SystemTrayIcon { get ; set ; }
21+
2022 public static TaskCompletionSource ? SplashScreenLoadingTCS { get ; private set ; }
2123 public static string ? OutputPath { get ; set ; }
2224
@@ -55,8 +57,7 @@ public App()
5557 }
5658
5759 /// <summary>
58- /// Invoked when the application is launched normally by the end user.
59- /// Other entry points will be used such as when the application is launched to open a specific file.
60+ /// Gets invoked when the application is launched normally by the end user.
6061 /// </summary>
6162 protected override void OnLaunched ( LaunchActivatedEventArgs e )
6263 {
@@ -108,13 +109,16 @@ async Task ActivateAsync()
108109 await SplashScreenLoadingTCS ! . Task . WithTimeoutAsync ( TimeSpan . FromMilliseconds ( 500 ) ) ;
109110 SplashScreenLoadingTCS = null ;
110111
112+ // Create a system tray icon
113+ SystemTrayIcon = new SystemTrayIcon ( ) . Show ( ) ;
114+
111115 _ = AppLifecycleHelper . InitializeAppComponentsAsync ( ) ;
112116 _ = MainWindow . Instance . InitializeApplicationAsync ( appActivationArguments . Data ) ;
113117 }
114118 }
115119
116120 /// <summary>
117- /// Invoked when the application is activated.
121+ /// Gets invoked when the application is activated.
118122 /// </summary>
119123 public async Task OnActivatedAsync ( AppActivationArguments activatedEventArgs )
120124 {
@@ -126,7 +130,7 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(()
126130 }
127131
128132 /// <summary>
129- /// Invoked when the main window is activated.
133+ /// Gets invoked when the main window is activated.
130134 /// </summary>
131135 private void Window_Activated ( object sender , WindowActivatedEventArgs args )
132136 {
@@ -135,12 +139,15 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args)
135139 args . WindowActivationState != WindowActivationState . PointerActivated )
136140 return ;
137141
138- ApplicationData . Current . LocalSettings . Values [ "INSTANCE_ACTIVE" ] = - Process . GetCurrentProcess ( ) . Id ;
142+ ApplicationData . Current . LocalSettings . Values [ "INSTANCE_ACTIVE" ] = - Environment . ProcessId ;
139143 }
140144
141145 /// <summary>
142- /// Invoked when application execution is being closed. Save application state .
146+ /// Gets invoked when the application execution is closed.
143147 /// </summary>
148+ /// <remarks>
149+ /// Saves the current state of the app such as opened tabs, and disposes all cached resources.
150+ /// </remarks>
144151 private async void Window_Closed ( object sender , WindowEventArgs args )
145152 {
146153 // Save application state and stop any background activity
@@ -156,9 +163,10 @@ private async void Window_Closed(object sender, WindowEventArgs args)
156163 return ;
157164 }
158165
166+ // Continue running the app on the background
159167 if ( userSettingsService . GeneralSettingsService . LeaveAppRunning &&
160168 ! AppModel . ForceProcessTermination &&
161- ! Process . GetProcessesByName ( "Files" ) . Any ( x => x . Id != Process . GetCurrentProcess ( ) . Id ) )
169+ ! Process . GetProcessesByName ( "Files" ) . Any ( x => x . Id != Environment . ProcessId ) )
162170 {
163171 // Close open content dialogs
164172 UIHelpers . CloseAllDialogs ( ) ;
@@ -168,7 +176,6 @@ private async void Window_Closed(object sender, WindowEventArgs args)
168176
169177 // Cache the window instead of closing it
170178 MainWindow . Instance . AppWindow . Hide ( ) ;
171- args . Handled = true ;
172179
173180 // Save and close all tabs
174181 AppLifecycleHelper . SaveSessionTabs ( ) ;
@@ -180,16 +187,22 @@ private async void Window_Closed(object sender, WindowEventArgs args)
180187
181188 // Sleep current instance
182189 Program . Pool = new ( 0 , 1 , $ "Files-{ ApplicationService . AppEnvironment } -Instance") ;
190+
183191 Thread . Yield ( ) ;
192+
184193 if ( Program . Pool . WaitOne ( ) )
185194 {
186195 // Resume the instance
187196 Program . Pool . Dispose ( ) ;
197+ Program . Pool = null ;
188198
189- _ = AppLifecycleHelper . CheckAppUpdate ( ) ;
199+ if ( ! AppModel . ForceProcessTermination )
200+ {
201+ args . Handled = true ;
202+ _ = AppLifecycleHelper . CheckAppUpdate ( ) ;
203+ return ;
204+ }
190205 }
191-
192- return ;
193206 }
194207
195208 // Method can take a long time, make sure the window is hidden
@@ -237,6 +250,9 @@ await SafetyExtensions.IgnoreExceptions(async () =>
237250 FileOperationsHelpers . WaitForCompletion ( ) ;
238251 }
239252
253+ /// <summary>
254+ /// Gets invoked when the last opened flyout is closed.
255+ /// </summary>
240256 private static void LastOpenedFlyout_Closed ( object ? sender , object e )
241257 {
242258 if ( sender is not CommandBarFlyout commandBarFlyout )
0 commit comments