66using Microsoft . Extensions . DependencyInjection ;
77using System ;
88using System . Collections . Generic ;
9+ using System . Collections . ObjectModel ;
910using System . Globalization ;
1011using System . Linq ;
1112using System . Threading . Tasks ;
12- using Avalonia . CpuLimiter . Config ;
13+ using Avalonia . Controls ;
1314using Avalonia . CpuLimiter . Models ;
15+ using Avalonia . Media ;
1416
1517
1618namespace Avalonia . CpuLimiter
@@ -19,9 +21,6 @@ public partial class App : Application
1921 {
2022 public override void Initialize ( )
2123 {
22- //dependency injection and load config.json from file
23- Services = ConfigureServices ( ) ;
24- ConfigModel = ConfigFileService . LoadConfigAsync ( ) ;
2524 AvaloniaXamlLoader . Load ( this ) ;
2625 }
2726
@@ -32,7 +31,6 @@ public override void Initialize()
3231
3332 public ServiceProvider ? Services { get ; private set ; }
3433
35-
3634 private ServiceProvider ConfigureServices ( )
3735 {
3836 if ( ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop )
@@ -43,48 +41,70 @@ private ServiceProvider ConfigureServices()
4341 services . AddSingleton < IHistoryItemFileService , HistoryItemFileService > ( ) ;
4442
4543 services . AddSingleton < MainWindowViewModel > ( ) ;
46- services . AddSingleton < MainWindow > ( sp => new MainWindow ( ) { DataContext = sp . GetRequiredService < MainWindowViewModel > ( ) } ) ;
47- services . AddSingleton < SettingWindowViewModel > ( ) ;
48- services . AddSingleton < SettingWindow > ( _ =>
44+ services . AddSingleton < MainWindow > ( sp =>
4945 {
50- var settingWindow = new SettingWindow ( )
46+ MainWindow mainWindow = new ( )
5147 {
48+ DataContext = sp . GetRequiredService < MainWindowViewModel > ( ) ,
5249 RequestedThemeVariant = ConfigModel . ThemeVariantConfig ,
53- DataContext = Services . GetRequiredService < SettingWindowViewModel > ( )
5450 } ;
55- settingWindow . SettingBorder . Material . TintColor = ConfigModel . UserColor . Color ;
51+ mainWindow . MainBorder . Material . TintColor = ColorCollection [ ConfigModel . ColorIndex ] . Color ;
52+ return mainWindow ;
53+ } ) ;
54+
55+ services . AddTransient < SettingWindowViewModel > ( ) ;
56+ services . AddTransient < SettingWindow > ( _ =>
57+ {
58+ var settingWindow = new SettingWindow ( ) ;
59+
60+ settingWindow . SettingBorder . Material . TintColor = ColorCollection [ ConfigModel . ColorIndex ] . Color ;
5661 return settingWindow ;
5762 } ) ;
5863
5964 services . AddTransient < AboutWindow > ( _ =>
6065 {
6166 var aboutWindow = new AboutWindow ( ) ;
6267 aboutWindow . RequestedThemeVariant = ConfigModel . ThemeVariantConfig ;
63- aboutWindow . AboutBorder . Material . TintColor = ConfigModel . UserColor . Color ;
68+ aboutWindow . AboutBorder . Material . TintColor = ColorCollection [ ConfigModel . ColorIndex ] . Color ;
6469 // to do theme related
6570 return aboutWindow ;
6671 } ) ;
6772 return services . BuildServiceProvider ( ) ;
6873 }
6974
70- public override async void OnFrameworkInitializationCompleted ( )
75+ public override void OnFrameworkInitializationCompleted ( )
7176 {
72- if ( ! string . IsNullOrWhiteSpace ( ConfigModel . StartupCultureConfig ) )
73- Lang . Resources . Culture = new CultureInfo ( ConfigModel . StartupCultureConfig ) ;
74-
7577 if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
7678 {
77- desktop . MainWindow = Services ! . GetService < MainWindow > ( ) ;
78- _mainWindowViewModel = Services . GetRequiredService < MainWindowViewModel > ( ) ;
79-
79+ //dependency injection and load config.json from file
80+ Services = ConfigureServices ( ) ;
81+ ConfigModel = ConfigFileService . LoadConfig ( ) ;
82+
83+ if ( ! string . IsNullOrWhiteSpace ( ConfigModel . StartupCultureConfig ) )
84+ Lang . Resources . Culture = new CultureInfo ( ConfigModel . StartupCultureConfig ) ;
85+
86+ try
87+ {
88+ desktop . MainWindow = Services . GetService < MainWindow > ( ) ;
89+ _mainWindowViewModel = desktop . MainWindow . DataContext as MainWindowViewModel ;
90+ }
91+ catch ( Exception e )
92+ {
93+ Console . WriteLine ( e ) ;
94+ throw ;
95+ }
8096 desktop . ShutdownRequested += DesktopOnShutdownRequested ;
8197
8298 ExitApplication += OnExitApplicationTriggered ;
8399 }
84100 base . OnFrameworkInitializationCompleted ( ) ;
85-
86- // init and load from config.json
87- await LoadHistoryItemToMainVM ( ) ;
101+
102+ if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime normal )
103+ {
104+ // init and load from config.json
105+ LoadHistoryItemToMainVM ( ) ;
106+ }
107+
88108 }
89109
90110 private bool _canClose ;
@@ -147,5 +167,31 @@ public void OnExitApplicationTriggered(object? sender, EventArgs eventArgs)
147167 // the program calling this Event to exit;
148168 // App.Current.ExitApplication.invoke
149169 public event EventHandler ? ExitApplication ;
170+
171+ public Collection < CustomColor > ColorCollection { get ; } = new Collection < CustomColor >
172+ {
173+ new CustomColor ( "#e95815" ) ,
174+ new CustomColor ( "#f1a100" ) ,
175+ new CustomColor ( "#f0c400" ) ,
176+ new CustomColor ( "#e7e542" ) ,
177+
178+ new CustomColor ( "#bbd53e" ) ,
179+ new CustomColor ( "#4fb142" ) ,
180+ new CustomColor ( "#068bce" ) ,
181+ // new CustomColor("#014da1"),
182+
183+ // new CustomColor("#192c92"),
184+ // new CustomColor("#522a8b"),
185+ new CustomColor ( "#b01e4f" ) ,
186+ new CustomColor ( "#e83a17" ) ,
187+
188+ new CustomColor ( Colors . Silver . ToString ( ) ) ,
189+ new CustomColor ( Colors . Gray . ToString ( ) ) ,
190+ new CustomColor ( Colors . Black . ToString ( ) ) ,
191+ new CustomColor ( Colors . Aqua . ToString ( ) ) ,
192+ new CustomColor ( Colors . SkyBlue . ToString ( ) ) ,
193+ new CustomColor ( Colors . DeepSkyBlue . ToString ( ) ) ,
194+ new CustomColor ( Colors . LightSkyBlue . ToString ( ) )
195+ } ;
150196 }
151197}
0 commit comments