Skip to content

Commit 6c15813

Browse files
committed
fix some config problem
1 parent bf46169 commit 6c15813

File tree

11 files changed

+178
-71
lines changed

11 files changed

+178
-71
lines changed

Avalonia.CpuLimiter/App.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
77

88
<Application.DataTemplates>
9-
<local:ViewLocator/>
109
</Application.DataTemplates>
1110

1211
<Application.Styles>

Avalonia.CpuLimiter/App.axaml.cs

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using System;
88
using System.Collections.Generic;
9+
using System.Collections.ObjectModel;
910
using System.Globalization;
1011
using System.Linq;
1112
using System.Threading.Tasks;
12-
using Avalonia.CpuLimiter.Config;
13+
using Avalonia.Controls;
1314
using Avalonia.CpuLimiter.Models;
15+
using Avalonia.Media;
1416

1517

1618
namespace 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
}

Avalonia.CpuLimiter/Avalonia.CpuLimiter.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@
88
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
99
<PublishAot>false</PublishAot>
1010
<RootNamespace>Avalonia.CpuLimiter</RootNamespace>
11+
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
1112
</PropertyGroup>
1213

1314
<ItemGroup>
1415
<AvaloniaResource Include="Assets\**" />
1516
<AvaloniaResource Include="Lang\**" />
16-
1717
</ItemGroup>
1818

1919
<ItemGroup>
2020
<PackageReference Include="Avalonia" Version="11.2.0" />
21+
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.4" />
2122
<PackageReference Include="Avalonia.Desktop" Version="11.2.0" />
2223
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.0" />
23-
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.0" />
2424
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
2525
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.0" />
2626
<PackageReference Include="Avalonia.ReactiveUI" Version="11.2.0" />
2727
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
2828
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
2929
</ItemGroup>
30+
31+
<ItemGroup>
32+
<Compile Remove="ViewLocator.cs" />
33+
</ItemGroup>
34+
3035
</Project>

Avalonia.CpuLimiter/Models/ProcessHelper.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.Diagnostics;
73
using System.Numerics;
84
using System.Runtime.InteropServices;

Avalonia.CpuLimiter/Services/FilesService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public FilesService(IClassicDesktopStyleApplicationLifetime desktop)
2323
{
2424
FilePickerOpenOptions options = new()
2525
{
26-
Title = "Select executable file",
26+
// Title = "Select executable file",
27+
Title = $@"{Lang.Resources.FilePickerTitle}",
2728
AllowMultiple = false,
2829
};
2930

@@ -46,7 +47,7 @@ public FilesService(IClassicDesktopStyleApplicationLifetime desktop)
4647
{
4748
return await storageProvider.SaveFilePickerAsync(new FilePickerSaveOptions()
4849
{
49-
Title = "Save Text File"
50+
Title = $"{Lang.Resources.SaveFilePickerTitle}"
5051
});
5152
}
5253
throw new NullReferenceException("Missing storageProvider instance.");

Avalonia.CpuLimiter/Services/HistoryItemFileService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Avalonia.CpuLimiter.Services;
1010

1111
[JsonSourceGenerationOptions(WriteIndented = true)]
1212
[JsonSerializable(typeof(IEnumerable<HistoryItem>))]
13-
[JsonSerializable(typeof(MyConfigModel))]
1413
internal partial class SourceGenerationContext : JsonSerializerContext
1514
{
1615

Avalonia.CpuLimiter/ViewModels/MainWindowViewModel.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,51 @@ namespace Avalonia.CpuLimiter.ViewModels
2222
{
2323
public class MainWindowViewModel : ViewModelBase
2424
{
25+
// just for preview don't use it.
26+
public MainWindowViewModel()
27+
{
28+
// design mode non-parameter constructor
29+
// ChooseExeFileCommand = ReactiveCommand.CreateFromTask(ChooseExeFile);
30+
RunGameCommand = ReactiveCommand.CreateFromTask(RunGame, canSave);
31+
RemoveHistoryItemCommand = ReactiveCommand.CreateFromTask<HistoryItemViewModel>( item => RemoveHistoryItemAsync(item) );
32+
SaveCommand = ReactiveCommand.CreateFromTask(SaveAsync, canSave);
33+
34+
// open setting window with local config model
35+
OpenSettingWindowCommand = ReactiveCommand.CreateFromTask(async () =>
36+
{
37+
SettingWindowViewModel settingModel = App.Current.Services.GetRequiredService<SettingWindowViewModel>();
38+
SettingWindowViewModel? result = await InteractionSettingWindow.Handle(settingModel);
39+
});
40+
41+
this.WhenAnyValue(x => x.CpuCoreCount)
42+
.Subscribe(x => Console.WriteLine($@"CPU core count: {x}"));
43+
44+
this.WhenAnyValue(x => x.GamePath)
45+
.Subscribe( x => Console.WriteLine($@"Game path: {x}"));
46+
47+
this.WhenAnyValue(x => x.SelectedComboboxIndex)
48+
.Subscribe(x =>
49+
{
50+
Console.WriteLine($@"change Selected combobox index: {x}");
51+
// refresh gamepath when selectedindex is not -1
52+
if(HistoryItems.Count > 0 ? true : false)
53+
GamePath = HistoryItems[SelectedComboboxIndex]?.Path;
54+
});
55+
56+
if (Design.IsDesignMode)
57+
{
58+
HistoryItems.Add(new HistoryItemViewModel(new HistoryItem()
59+
{
60+
CPUCoreUsed = 1,
61+
LastUsed = new DateTime(2018, 9, 30),
62+
63+
Path = "~/App_Data/CpuCoreHistory.json"
64+
}));
65+
GamePath = "~/App_Data/CpuCoreHistory.json";
66+
}
67+
}
68+
69+
// real constructor
2570
public MainWindowViewModel(IHistoryItemFileService historyItemFileService, IClipBoardService clipBoardService,
2671
IFilesService filesService)
2772
{
@@ -55,6 +100,7 @@ public MainWindowViewModel(IHistoryItemFileService historyItemFileService, IClip
55100
if(HistoryItems.Count > 0 ? true : false)
56101
GamePath = HistoryItems[SelectedComboboxIndex]?.Path;
57102
});
103+
this.HistoryLimit = App.Current.ConfigModel.HistoryLimit;
58104

59105
if (Design.IsDesignMode)
60106
{
@@ -113,6 +159,8 @@ public string? GamePath
113159
this.RaiseAndSetIfChanged(ref _gamePath, value);
114160
}
115161
}
162+
163+
public int HistoryLimit { get; set; }
116164

117165
private async Task AddHistoryItemAsync(HistoryItemViewModel historyItem)
118166
{
@@ -122,9 +170,9 @@ private async Task AddHistoryItemAsync(HistoryItemViewModel historyItem)
122170
// determine whether the item was already in HistoryItems
123171
await HistoryItemViewModel.SortHistoryItems(HistoryItems);
124172

125-
if (HistoryItems.Count() > 4)
173+
if (HistoryItems.Count() > HistoryLimit)
126174
{
127-
var items = HistoryItems.Skip(0).Take(4).ToList();
175+
var items = HistoryItems.Skip(0).Take(HistoryLimit).ToList();
128176
HistoryItems.Clear();
129177

130178
foreach (var item in items)

Avalonia.CpuLimiter/Views/HistoryComboboxView.axaml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,31 @@
99
x:DataType="vm:HistoryItemViewModel">
1010

1111
<Grid ColumnDefinitions="* Auto"
12-
ToolTip.Tip="{Binding LastUsed}"
12+
1313
RowDefinitions="30" Margin="0" MinWidth="40">
1414
<TextBox Grid.Column="0" Text="{Binding Path}"
1515
IsReadOnly="{Binding $parent.IsFocused}"
1616
HorizontalAlignment="Left"
17+
ToolTip.Tip="{Binding LastUsed}"
1718
VerticalContentAlignment="Center"
18-
FontSize="16" Height="20" Margin="0 5 1 5"
19+
FontSize="16" Height="20" Margin="0 0 1 0"
1920
MinWidth="300"
2021
Padding="0 0 0 0"
2122
BorderThickness="0"
22-
Background="Transparent"/>
23-
<Button Grid.Column="1" Padding="1 10 0 10"
23+
Background="Transparent">
24+
<TextBox.Styles>
25+
<Style Selector="TextBox:pointerover /template/ Border#PART_BorderElement">
26+
<Setter Property="Background" Value="Transparent"></Setter>
27+
</Style>
28+
</TextBox.Styles>
29+
</TextBox>
30+
<Button Grid.Column="1" Padding="1 0 0 0"
2431
Margin="8 0" Width="38" Height="38"
2532
IsVisible="{Binding }"
2633
Command="{Binding $parent[Window].((vm:MainWindowViewModel)DataContext).RemoveHistoryItemCommand}"
27-
CommandParameter="{Binding .}">
28-
<PathIcon Data="{StaticResource DeleteRegular}" Padding="0 10" IsVisible="True"/>
34+
CommandParameter="{Binding .}"
35+
ToolTip.Tip="Click to delete record">
36+
<PathIcon Data="{StaticResource DeleteRegular}" Padding="0 10"/>
2937
</Button>
3038
</Grid>
3139
</UserControl>

Avalonia.CpuLimiter/Views/HistoryComboboxView.axaml.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,4 @@ public HistoryComboboxView()
3434
// HistoryComboBox.SelectedIndex = 0;
3535
// }
3636

37-
private async void ResourcesChanged(object? sender, ResourcesChangedEventArgs e)
38-
{
39-
// todo
40-
}
41-
4237
}

0 commit comments

Comments
 (0)