-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
127 lines (105 loc) · 4.01 KB
/
Copy pathMainWindowViewModel.cs
File metadata and controls
127 lines (105 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GeneralUpdate.Tools.Configuration;
using GeneralUpdate.Tools.Models;
using GeneralUpdate.Tools.Services;
namespace GeneralUpdate.Tools.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
private readonly LocalizationService _loc = LocalizationService.Instance;
private readonly AppConfig _config;
[ObservableProperty] private ViewModelBase _currentPage;
[ObservableProperty] private bool _isDarkTheme;
[ObservableProperty] private string _localeText = "EN";
public ObservableCollection<NavItem> NavItems { get; } = new();
public MainWindowViewModel(AppConfig config)
{
_config = config;
// Apply saved theme
IsDarkTheme = config.Theme == "Dark";
ApplyTheme(IsDarkTheme);
// Apply saved locale
LocaleText = _loc.Locale == "zh-CN" ? "EN" : "中";
SyncNavItems();
_loc.PropertyChanged += (_, e) =>
{
if (e.PropertyName != nameof(LocalizationService.Locale))
return;
SyncNavItems();
LocaleText = _loc.Locale == "zh-CN" ? "EN" : "中";
};
// Default to Patch page
CurrentPage = new PatchViewModel(config);
}
private void SyncNavItems()
{
NavItems.Clear();
NavItems.Add(new("Patch", _loc["Nav.Patch"], typeof(PatchViewModel), true));
NavItems.Add(new("Extension", _loc["Nav.Extension"], typeof(ExtensionViewModel), false));
NavItems.Add(new("OSS", _loc["Nav.OSS"], typeof(OSSViewModel), false));
NavItems.Add(new("Simulate", _loc["Nav.Simulate"], typeof(SimulateViewModel), false));
NavItems.Add(new("Config", _loc["Nav.Config"], typeof(ConfigViewModel), false));
NavItems.Add(new("Settings", _loc["Nav.Settings"], typeof(SettingsViewModel), false));
}
[RelayCommand]
private void Navigate(NavItem item)
{
foreach (var n in NavItems) n.IsSelected = false;
item.IsSelected = true;
CurrentPage = item.Key switch
{
"Patch" => new PatchViewModel(_config),
"Extension" => new ExtensionViewModel(_config),
"OSS" => new OSSViewModel(_config),
"Config" => new ConfigViewModel(_config),
"Settings" => new SettingsViewModel(_config),
_ => new SimulateViewModel(_config)
};
}
[RelayCommand]
private void ToggleTheme()
{
IsDarkTheme = !IsDarkTheme;
ApplyTheme(IsDarkTheme);
// Persist
_config.Theme = IsDarkTheme ? "Dark" : "Light";
ConfigService.SafeFireAndForgetSave(ConfigServiceSingleton.Instance);
}
[RelayCommand]
private void ToggleLocale()
{
var newLocale = _loc.Locale == "zh-CN" ? "en-US" : "zh-CN";
// Update legacy localization (triggers PropertyChanged → Lingua syncs via listener)
_loc.Locale = newLocale;
// Also update Lingua directly for reactive observable subscribers
Irihi.Lingua.AppLanguageManager.Instance.UpdateCulture(
new System.Globalization.CultureInfo(newLocale));
// Persist
_config.Language = newLocale;
ConfigService.SafeFireAndForgetSave(ConfigServiceSingleton.Instance);
}
private static void ApplyTheme(bool isDark)
{
var app = Avalonia.Application.Current;
if (app != null)
app.RequestedThemeVariant = isDark
? Avalonia.Styling.ThemeVariant.Dark
: Avalonia.Styling.ThemeVariant.Light;
}
}
public partial class NavItem : ObservableObject
{
public string Key { get; }
public string Title { get; }
public System.Type PageType { get; }
[ObservableProperty] private bool _isSelected;
public NavItem(string key, string title, System.Type pageType, bool selected)
{
Key = key;
Title = title;
PageType = pageType;
_isSelected = selected;
}
}