-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
87 lines (75 loc) · 2.59 KB
/
Copy pathMainWindowViewModel.cs
File metadata and controls
87 lines (75 loc) · 2.59 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
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GeneralUpdate.Tools.Models;
using GeneralUpdate.Tools.Services;
namespace GeneralUpdate.Tools.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
private readonly LocalizationService _loc = LocalizationService.Instance;
[ObservableProperty] private ViewModelBase _currentPage = new PatchViewModel();
[ObservableProperty] private bool _isDarkTheme;
[ObservableProperty] private string _localeText = "EN";
public ObservableCollection<NavItem> NavItems { get; } = new();
public MainWindowViewModel()
{
SyncNavItems();
_loc.PropertyChanged += (_, e) =>
{
if (e.PropertyName != nameof(LocalizationService.Locale))
return;
SyncNavItems();
LocaleText = _loc.Locale == "zh-CN" ? "EN" : "中";
};
}
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));
}
[RelayCommand]
private void Navigate(NavItem item)
{
foreach (var n in NavItems) n.IsSelected = false;
item.IsSelected = true;
CurrentPage = item.Key switch
{
"Patch" => new PatchViewModel(),
"Extension" => new ExtensionViewModel(),
"OSS" => new OSSViewModel(),
_ => new SimulateViewModel()
};
}
[RelayCommand]
private void ToggleTheme()
{
IsDarkTheme = !IsDarkTheme;
var app = Avalonia.Application.Current;
if (app != null)
app.RequestedThemeVariant = IsDarkTheme
? Avalonia.Styling.ThemeVariant.Dark
: Avalonia.Styling.ThemeVariant.Light;
}
[RelayCommand]
private void ToggleLocale()
{
_loc.Locale = _loc.Locale == "zh-CN" ? "en-US" : "zh-CN";
}
}
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;
}
}