-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cpp
More file actions
215 lines (186 loc) · 6.36 KB
/
Copy pathApp.xaml.cpp
File metadata and controls
215 lines (186 loc) · 6.36 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Licensed under the MIT License
//
// App.xaml.cpp - code-behind for App.xaml. Wires locale loading, settings
// persistence, theme service, and MainWindow construction.
#include "pch.h"
#include "App.xaml.h"
#include "MainWindow.xaml.h"
#include "StringConvert.h"
#include <core/i18n_manager.hpp>
#include <core/settings.hpp>
#include <winrt/Windows.Storage.h>
#include <shlobj.h>
#include <fstream>
#include <sstream>
namespace winrt::ZBinary2CArray_WinUI3::implementation
{
using namespace Microsoft::UI::Xaml;
App* App::s_instance = nullptr;
namespace fs = std::filesystem;
namespace
{
constexpr std::string_view kLocaleTags[] = { "en-US", "zh-CN", "zh-TW" };
core::JsonValue read_locale_file(std::string_view tag)
{
wchar_t exe_path[MAX_PATH]{};
GetModuleFileNameW(nullptr, exe_path, MAX_PATH);
auto exe_root = fs::path(exe_path).parent_path();
std::string fname = std::string(tag) + ".json";
for (auto const& sub : { L"Locales", L"locales" })
{
auto p = exe_root / sub / fname;
std::error_code ec;
if (!fs::exists(p, ec)) continue;
std::ifstream ifs(p);
if (!ifs) continue;
std::stringstream ss;
ss << ifs.rdbuf();
try { return core::JsonValue::parse(ss.str()); }
catch (...) { continue; }
}
return core::JsonValue{};
}
// Detect the system UI language and map it to one of our supported
// locale tags. Falls back to en-US.
std::string detect_system_language()
{
wchar_t locale_name[LOCALE_NAME_MAX_LENGTH]{};
if (GetUserDefaultLocaleName(locale_name, LOCALE_NAME_MAX_LENGTH) > 0)
{
std::string tag;
for (wchar_t const* p = locale_name; *p; ++p)
tag.push_back(static_cast<char>(*p < 128 ? *p : '?'));
if (tag.find("zh-Hans") != std::string::npos || tag == "zh-CN")
return "zh-CN";
if (tag.find("zh-Hant") != std::string::npos || tag == "zh-TW")
return "zh-TW";
}
return "en-US";
}
} // anonymous namespace
App::App()
{
s_instance = this;
InitializeComponent();
m_settings_path = resolve_settings_path();
load_persisted_settings();
// Always follow system theme — no user-selectable theme.
m_settings.theme = core::ThemePreference::System;
m_theme.set_preference(m_settings.theme);
load_locales();
// When the user picks a different language in the UI, persist it.
core::I18nManager::instance().add_language_changed_listener(
[this](std::string const& tag)
{
m_settings.language = tag;
save_persisted_settings();
});
}
App::~App()
{
try { save_persisted_settings(); } catch (...) {}
}
App* App::instance() noexcept { return s_instance; }
void App::OnLaunched(LaunchActivatedEventArgs const&)
{
m_window = make<MainWindow>();
if (auto root = m_window.Content().try_as<FrameworkElement>())
{
m_theme.apply_to(root);
}
m_window.Activate();
// Refresh strings once the window has rendered. Catches the case
// where the initial locale load finished before the window existed.
auto main_window = m_window.as<::winrt::ZBinary2CArray_WinUI3::MainWindow>();
main_window.ApplyTr();
// First-run: show the language selection dialog.
if (m_first_run)
{
auto impl = winrt::get_self<implementation::MainWindow>(main_window);
impl->ShowFirstRunLanguageDialog();
}
}
std::filesystem::path App::resolve_settings_path() const
{
namespace fs = std::filesystem;
try
{
using namespace winrt::Windows::Storage;
auto local = ApplicationData::Current().LocalFolder().Path();
if (!local.empty())
{
fs::path dir(local.c_str());
std::error_code ec;
fs::create_directories(dir, ec);
return dir / "settings.json";
}
}
catch (winrt::hresult_error const&)
{
// Unpackaged - fall through.
}
PWSTR raw = nullptr;
if (SUCCEEDED(::SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &raw))
&& raw != nullptr)
{
fs::path dir = fs::path(raw) / "ZBinary2CArray";
std::error_code ec;
fs::create_directories(dir, ec);
auto p = dir / "settings.json";
::CoTaskMemFree(raw);
return p;
}
if (raw) ::CoTaskMemFree(raw);
return fs::current_path() / "settings.json";
}
void App::load_locales()
{
for (auto tag : kLocaleTags)
{
auto data = read_locale_file(tag);
if (!data.is_null())
{
core::I18nManager::instance().load_language(tag, data);
}
}
core::I18nManager::instance().set_language(m_settings.language);
}
void App::load_persisted_settings()
{
namespace fs = std::filesystem;
std::error_code ec;
if (!fs::exists(m_settings_path, ec))
{
// First run — detect system language as default.
m_first_run = true;
m_settings.language = detect_system_language();
return;
}
try
{
m_settings = core::load_settings(m_settings_path);
}
catch (...)
{
// Corrupt file - leave defaults in place.
}
}
void App::save_persisted_settings() const
{
namespace fs = std::filesystem;
try
{
std::error_code ec;
fs::create_directories(m_settings_path.parent_path(), ec);
core::save_settings(m_settings, m_settings_path);
}
catch (...)
{
// Best-effort persistence - never crash the destructor.
}
}
void App::save_settings() const
{
save_persisted_settings();
}
} // namespace winrt::ZBinary2CArray_WinUI3::implementation