44using System . Threading ;
55using System . Threading . Tasks ;
66using System . Windows ;
7+ using System . Windows . Media ;
78using CommunityToolkit . Mvvm . DependencyInjection ;
89using Flow . Launcher . Core ;
910using Flow . Launcher . Core . Configuration ;
1819using Flow . Launcher . Infrastructure . Storage ;
1920using Flow . Launcher . Infrastructure . UserSettings ;
2021using Flow . Launcher . Plugin ;
22+ using Flow . Launcher . SettingPages . ViewModels ;
2123using Flow . Launcher . ViewModel ;
2224using Microsoft . Extensions . DependencyInjection ;
2325using Microsoft . Extensions . Hosting ;
@@ -29,6 +31,7 @@ public partial class App : IDisposable, ISingleInstanceApp
2931 #region Public Properties
3032
3133 public static IPublicAPI API { get ; private set ; }
34+ public static bool Exiting => _mainWindow . CanClose ;
3235
3336 #endregion
3437
@@ -37,7 +40,7 @@ public partial class App : IDisposable, ISingleInstanceApp
3740 private static readonly string ClassName = nameof ( App ) ;
3841
3942 private static bool _disposed ;
40- private MainWindow _mainWindow ;
43+ private static MainWindow _mainWindow ;
4144 private readonly MainViewModel _mainVM ;
4245 private readonly Settings _settings ;
4346
@@ -73,14 +76,27 @@ public App()
7376 . AddSingleton ( _ => _settings )
7477 . AddSingleton ( sp => new Updater ( sp . GetRequiredService < IPublicAPI > ( ) , Launcher . Properties . Settings . Default . GithubRepo ) )
7578 . AddSingleton < Portable > ( )
76- . AddSingleton < SettingWindowViewModel > ( )
7779 . AddSingleton < IAlphabet , PinyinAlphabet > ( )
7880 . AddSingleton < StringMatcher > ( )
7981 . AddSingleton < Internationalization > ( )
8082 . AddSingleton < IPublicAPI , PublicAPIInstance > ( )
81- . AddSingleton < MainViewModel > ( )
8283 . AddSingleton < Theme > ( )
84+ // Use one instance for main window view model because we only have one main window
85+ . AddSingleton < MainViewModel > ( )
86+ // Use one instance for welcome window view model & setting window view model because
87+ // pages in welcome window & setting window need to share the same instance and
88+ // these two view models do not need to be reset when creating new windows
8389 . AddSingleton < WelcomeViewModel > ( )
90+ . AddSingleton < SettingWindowViewModel > ( )
91+ // Use transient instance for setting window page view models because
92+ // pages in setting window need to be recreated when setting window is closed
93+ . AddTransient < SettingsPaneAboutViewModel > ( )
94+ . AddTransient < SettingsPaneGeneralViewModel > ( )
95+ . AddTransient < SettingsPaneHotkeyViewModel > ( )
96+ . AddTransient < SettingsPanePluginsViewModel > ( )
97+ . AddTransient < SettingsPanePluginStoreViewModel > ( )
98+ . AddTransient < SettingsPaneProxyViewModel > ( )
99+ . AddTransient < SettingsPaneThemeViewModel > ( )
84100 ) . Build ( ) ;
85101 Ioc . Default . ConfigureServices ( host . Services ) ;
86102 }
@@ -146,10 +162,14 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
146162
147163 Log . SetLogLevel ( _settings . LogLevel ) ;
148164
165+ // Update dynamic resources base on settings
166+ Current . Resources [ "SettingWindowFont" ] = new FontFamily ( _settings . SettingWindowFont ) ;
167+ Current . Resources [ "ContentControlThemeFontFamily" ] = new FontFamily ( _settings . SettingWindowFont ) ;
168+
149169 Ioc . Default . GetRequiredService < Portable > ( ) . PreStartCleanUpAfterPortabilityUpdate ( ) ;
150170
151171 API . LogInfo ( ClassName , "Begin Flow Launcher startup ----------------------------------------------------" ) ;
152- API . LogInfo ( ClassName , "Runtime info:{ErrorReporting.RuntimeInfo()}" ) ;
172+ API . LogInfo ( ClassName , $ "Runtime info:{ ErrorReporting . RuntimeInfo ( ) } ") ;
153173
154174 RegisterAppDomainExceptions ( ) ;
155175 RegisterDispatcherUnhandledException ( ) ;
@@ -169,19 +189,16 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
169189 await PluginManager . InitializePluginsAsync ( ) ;
170190
171191 // Change language after all plugins are initialized because we need to update plugin title based on their api
172- // TODO: Clean InternationalizationManager.Instance and InternationalizationManager.Instance.GetTranslation in future
173192 await Ioc . Default . GetRequiredService < Internationalization > ( ) . InitializeLanguageAsync ( ) ;
174193
175194 await imageLoadertask ;
176195
177196 _mainWindow = new MainWindow ( ) ;
178197
179- API . LogInfo ( ClassName , "Dependencies Info:{ErrorReporting.DependenciesInfo()}" ) ;
180-
181198 Current . MainWindow = _mainWindow ;
182199 Current . MainWindow . Title = Constant . FlowLauncher ;
183200
184- // main windows needs initialized before theme change because of blur settings
201+ // Main windows needs initialized before theme change because of blur settings
185202 Ioc . Default . GetRequiredService < Theme > ( ) . ChangeTheme ( ) ;
186203
187204 Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
@@ -198,6 +215,10 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
198215
199216#pragma warning restore VSTHRD100 // Avoid async void methods
200217
218+ /// <summary>
219+ /// Check startup only for Release
220+ /// </summary>
221+ [ Conditional ( "RELEASE" ) ]
201222 private void AutoStartup ( )
202223 {
203224 // we try to enable auto-startup on first launch, or reenable if it was removed
@@ -261,7 +282,7 @@ private void RegisterExitEvents()
261282 }
262283
263284 /// <summary>
264- /// let exception throw as normal is better for Debug
285+ /// Let exception throw as normal is better for Debug
265286 /// </summary>
266287 [ Conditional ( "RELEASE" ) ]
267288 private void RegisterDispatcherUnhandledException ( )
@@ -270,7 +291,7 @@ private void RegisterDispatcherUnhandledException()
270291 }
271292
272293 /// <summary>
273- /// let exception throw as normal is better for Debug
294+ /// Let exception throw as normal is better for Debug
274295 /// </summary>
275296 [ Conditional ( "RELEASE" ) ]
276297 private static void RegisterAppDomainExceptions ( )
@@ -279,7 +300,7 @@ private static void RegisterAppDomainExceptions()
279300 }
280301
281302 /// <summary>
282- /// let exception throw as normal is better for Debug
303+ /// Let exception throw as normal is better for Debug
283304 /// </summary>
284305 [ Conditional ( "RELEASE" ) ]
285306 private static void RegisterTaskSchedulerUnhandledException ( )
0 commit comments