Repository navigation
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
104 lines (89 loc) · 3.71 KB
/
Copy pathApp.xaml.cs
File metadata and controls
104 lines (89 loc) · 3.71 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
using System.IO;
namespace OneColumnEncoder;
/// <summary>
/// Application entry point: loads persisted app settings and data, wires up
/// unhandled-exception and startup-language handling, and hosts the main
/// window with its top-level ViewModel.
/// </summary>
public partial class App : Application
{
// Pass navigation, app settings and tools imported to MainVM
internal readonly ModalNavS _modalNavM;
private readonly AppConfM _appConfM;
private readonly AppDataM _appDataM;
private readonly ForkSnapshot? _forkSnapshot;
private readonly string? _forkSnapshotLoadError;
public App()
{
string? forkSnapshotLoadError;
_forkSnapshot = ForkSnapshot.LoadFromCommandLine(out forkSnapshotLoadError);
_forkSnapshotLoadError = forkSnapshotLoadError;
if (_forkSnapshot != null && !string.IsNullOrWhiteSpace(_forkSnapshot.ConfigSubFolder))
PersistencePaths.ConfigSubFolder = _forkSnapshot.ConfigSubFolder;
_modalNavM = new ModalNavS();
_appConfM = AppConfM.Load();
_appDataM = AppDataM.Load();
// Fresh installs need one startup language probe; existing configs keep their saved state.
if (!File.Exists(Path.Combine(AppConfM.GetConfigDirectory(), "appconfig.json")))
_appConfM.InitLang = true;
_forkSnapshot?.ApplyTo(_appDataM, _appConfM);
}
protected override void OnStartup(StartupEventArgs e)
{
try
{
DispatcherUnhandledException += (_, ex) =>
{
new OpenErrModalCmd(_modalNavM, "Unhandled UI Exception", ex.Exception.ToString()).Execute(null);
ex.Handled = true;
};
ApplyStartupLanguageOnce();
_ = new UILangProvider(_appConfM.Lang.LanguageCode);
if (_forkSnapshotLoadError != null)
{
new OpenErrModalCmd(
_modalNavM,
UICaptionProvider.Errors.ForkSnapshotLoadTitle,
string.Format(
UICaptionProvider.Errors.ForkSnapshotLoadMessage,
_forkSnapshotLoadError)).Execute(null);
Shutdown(-1);
return;
}
AppFontProvider.Refresh();
AppFontProvider.ApplyFrom(_appConfM);
OpenAppConfCmd openAppConf = new(_modalNavM, _appConfM);
OpenUsagesCmd openUsages = new(_modalNavM, _appConfM);
MainVM mainVM = new(openAppConf, openUsages, _appDataM, _appConfM, _modalNavM);
if (_forkSnapshot != null)
mainVM.ApplyForkSnapshot(_forkSnapshot);
MainWindow = new MainWindow { DataContext = mainVM };
MainWindow.Show();
base.OnStartup(e);
}
catch (Exception ex)
{
new OpenErrModalCmd(_modalNavM, "Startup Exception", ex.ToString()).Execute(null);
Shutdown(-1);
}
}
private void ApplyStartupLanguageOnce()
{
if (!_appConfM.InitLang) return;
_appConfM.Lang.LanguageCode = ResolveSupportedLanguageCode(CultureInfo.CurrentUICulture);
_appConfM.InitLang = false;
_appConfM.Save();
}
private static string ResolveSupportedLanguageCode(CultureInfo culture)
{
string name = culture.Name.ToLowerInvariant();
string twoLetterName = culture.TwoLetterISOLanguageName.ToLowerInvariant();
return name switch
{
"zh-cn" or "zh-sg" or "zh-hans" => "zh-cn",
"zh-tw" or "zh-hk" or "zh-mo" or "zh-hant" => "zh-tw",
_ when UICaptionProvider.AppConf.LanguageOptions.Codes.Contains(twoLetterName) => twoLetterName,
_ => "en"
};
}
}