-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyConfigWindow.xaml.cs
More file actions
285 lines (241 loc) · 11 KB
/
ProxyConfigWindow.xaml.cs
File metadata and controls
285 lines (241 loc) · 11 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using VivaldiUpdater.Model;
using VivaldiUpdater.ViewModel;
namespace VivaldiUpdater
{
public partial class ProxyConfigWindow : Window
{
public AppSettings AppSettings { get; private set; }
public ProxyConfig ProxySettings => AppSettings.Proxy;
private ProxyConfigViewModel _viewModel;
public ProxyConfigWindow(AppSettings appSettings = null)
{
// Apply current language settings before initialization
AppSettings = appSettings ?? AppSettings.Load();
AppSettings.ApplyLanguage();
InitializeComponent();
_viewModel = new ProxyConfigViewModel();
DataContext = _viewModel;
// 延迟加载设置,确保所有控件都已正确初始化
this.Loaded += ProxyConfigWindow_Loaded;
// 添加键盘快捷键
this.KeyDown += ProxyConfigWindow_KeyDown;
// Subscribe to language change events
LanguageManager.LanguageChanged += OnLanguageChanged;
// Handle window closing to unsubscribe from events
this.Closed += (s, e) => LanguageManager.LanguageChanged -= OnLanguageChanged;
}
private void ProxyConfigWindow_Loaded(object sender, RoutedEventArgs e)
{
// 确保在窗口完全加载后再加载设置
LoadSettings();
// 延迟调用UpdateControlStates,确保所有控件都设置完成
this.Dispatcher.BeginInvoke(new System.Action(() => {
UpdateControlStates();
}), System.Windows.Threading.DispatcherPriority.Loaded);
}
private void OnLanguageChanged(object sender, EventArgs e)
{
// Refresh the view model's localized properties
_viewModel?.RefreshLocalizedProperties();
// 重新应用语言设置以确保消息框也使用正确的语言
AppSettings?.ApplyLanguage();
}
private void ProxyConfigWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
OkButton_Click(sender, null);
}
else if (e.Key == System.Windows.Input.Key.Escape)
{
CancelButton_Click(sender, null);
}
}
private void LoadSettings()
{
try
{
if (AppSettings == null || ProxySettings == null)
{
return;
}
// 设置启用代理复选框
if (EnableProxyCheckBox != null)
{
// 临时解除事件绑定,避免触发Checked/Unchecked事件
EnableProxyCheckBox.Checked -= EnableProxyCheckBox_Checked;
EnableProxyCheckBox.Unchecked -= EnableProxyCheckBox_Unchecked;
// 设置复选框状态
EnableProxyCheckBox.IsChecked = ProxySettings.UseProxy;
// 重新绑定事件
EnableProxyCheckBox.Checked += EnableProxyCheckBox_Checked;
EnableProxyCheckBox.Unchecked += EnableProxyCheckBox_Unchecked;
}
// 设置代理类型
if (ProxyTypeComboBox != null)
{
bool typeFound = false;
for (int i = 0; i < ProxyTypeComboBox.Items.Count; i++)
{
var item = ProxyTypeComboBox.Items[i] as ComboBoxItem;
if (item != null && item.Tag?.ToString() == ProxySettings.ProxyType.ToString())
{
ProxyTypeComboBox.SelectedIndex = i;
ProxyTypeComboBox.SelectedItem = item;
typeFound = true;
break;
}
}
if (!typeFound && ProxyTypeComboBox.Items.Count > 0)
{
ProxyTypeComboBox.SelectedIndex = 0;
}
}
// 设置主机地址
if (ProxyHostTextBox != null)
{
ProxyHostTextBox.Text = ProxySettings.ProxyHost ?? "";
}
// 设置端口
if (ProxyPortTextBox != null)
{
ProxyPortTextBox.Text = ProxySettings.ProxyPort > 0 ? ProxySettings.ProxyPort.ToString() : "";
}
// 设置用户名
if (ProxyUsernameTextBox != null)
{
ProxyUsernameTextBox.Text = ProxySettings.ProxyUsername ?? "";
}
// 设置密码
if (ProxyPasswordTextBox != null)
{
ProxyPasswordTextBox.Password = ProxySettings.ProxyPassword ?? "";
}
}
catch (Exception)
{
// 在出错时尝试恢复默认设置
try
{
if (EnableProxyCheckBox != null) EnableProxyCheckBox.IsChecked = false;
if (ProxyTypeComboBox != null && ProxyTypeComboBox.Items.Count > 0) ProxyTypeComboBox.SelectedIndex = 0;
if (ProxyHostTextBox != null) ProxyHostTextBox.Text = "";
if (ProxyPortTextBox != null) ProxyPortTextBox.Text = "";
if (ProxyUsernameTextBox != null) ProxyUsernameTextBox.Text = "";
if (ProxyPasswordTextBox != null) ProxyPasswordTextBox.Password = "";
}
catch
{
// 忽略错误
}
}
}
private void UpdateControlStates()
{
bool enabled = EnableProxyCheckBox.IsChecked == true;
ProxyTypePanel.IsEnabled = enabled;
ProxyHostPanel.IsEnabled = enabled;
ProxyPortPanel.IsEnabled = enabled;
ProxyUsernamePanel.IsEnabled = enabled;
ProxyPasswordPanel.IsEnabled = enabled;
}
private void EnableProxyCheckBox_Checked(object sender, RoutedEventArgs e)
{
UpdateControlStates();
}
private void EnableProxyCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
UpdateControlStates();
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
try
{
ProxySettings.UseProxy = EnableProxyCheckBox.IsChecked == true;
if (ProxyTypeComboBox.SelectedItem is ComboBoxItem selectedItem)
{
ProxySettings.ProxyType = (ProxyType)Enum.Parse(typeof(ProxyType), selectedItem.Tag.ToString());
}
ProxySettings.ProxyHost = ProxyHostTextBox.Text.Trim();
if (int.TryParse(ProxyPortTextBox.Text, out int port))
{
ProxySettings.ProxyPort = port;
}
else if (ProxySettings.UseProxy)
{
// 使用统一的多语言资源机制
string errorMessage = Properties.Resources.text_invalid_port ?? "请输入有效的端口号";
string errorTitle = Properties.Resources.text_error ?? "错误";
CustomMessageBox.Show(errorMessage, errorTitle, CustomMessageBox.MessageBoxButtons.OK, this);
return;
}
else
{
ProxySettings.ProxyPort = 0; // 代理未启用时设置为0
}
ProxySettings.ProxyUsername = ProxyUsernameTextBox.Text.Trim();
ProxySettings.ProxyPassword = ProxyPasswordTextBox.Password;
// 保存到统一配置文件
AppSettings.Save();
// Show success message to user
if (ProxySettings.UseProxy)
{
// 使用统一的多语言资源机制和格式化字符串
string messageFormat = Properties.Resources.text_proxy_settings_saved ?? "代理设置已保存!\n类型: {0}\n地址: {1}:{2}";
messageFormat = messageFormat.Replace("\\n", "\n");
string messageText = string.Format(
messageFormat,
ProxySettings.ProxyType, ProxySettings.ProxyHost, ProxySettings.ProxyPort);
string titleText = Properties.Resources.text_proxy_settings ?? "代理设置";
CustomMessageBox.Show(messageText, titleText, CustomMessageBox.MessageBoxButtons.OK, this);
}
else
{
string messageText = Properties.Resources.text_proxy_disabled_saved ?? "代理已禁用,设置已保存!";
string titleText = Properties.Resources.text_proxy_settings ?? "代理设置";
CustomMessageBox.Show(messageText, titleText, CustomMessageBox.MessageBoxButtons.OK, this);
}
DialogResult = true;
Close();
}
catch (Exception ex)
{
// 使用统一的多语言资源机制
string errorMessage = string.Format("{0}: {1}", Properties.Resources.text_proxy_save_failed ?? "保存代理设置失败", ex.Message);
string errorTitle = Properties.Resources.text_error ?? "错误";
CustomMessageBox.Show(errorMessage, errorTitle, CustomMessageBox.MessageBoxButtons.OK, this);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void TitleBar_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ButtonState == System.Windows.Input.MouseButtonState.Pressed)
{
DragMove();
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void Overlay_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// 只在点击遵罩区域(非内容卡片)时关闭窗口
if (e.OriginalSource == sender)
{
DialogResult = false;
Close();
}
}
}
}