-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
230 lines (205 loc) · 7.15 KB
/
Copy pathSettingsForm.cs
File metadata and controls
230 lines (205 loc) · 7.15 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
using System.Drawing;
namespace BatteryDown;
internal sealed class SettingsForm : Form
{
private readonly NumericUpDown delaySeconds = new();
private readonly NumericUpDown shutdownDelaySeconds = new();
private readonly CheckBox shutdownWhenLocked = new();
private readonly Label statusLabel = new();
private readonly Button enableButton = new();
private readonly Button disableButton = new();
public SettingsForm()
{
Text = "Battery Down";
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ClientSize = new Size(480, 315);
Font = new Font("Microsoft YaHei UI", 9F);
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
BuildLayout();
LoadConfig();
RefreshStatus();
}
private void BuildLayout()
{
var title = new Label
{
Text = "电池使用提醒",
AutoSize = true,
Font = new Font(Font.FontFamily, 15F, FontStyle.Bold),
Location = new Point(24, 22),
};
var description = new Label
{
Text = "拔出电源一段时间后开始倒数;未选择继续使用时自动关机。",
AutoSize = true,
ForeColor = SystemColors.GrayText,
Location = new Point(26, 61),
};
var delayLabel = new Label
{
Text = "拔电后等待",
AutoSize = true,
Location = new Point(26, 104),
};
delaySeconds.Minimum = 0;
delaySeconds.Maximum = 3600;
delaySeconds.Location = new Point(144, 100);
delaySeconds.Size = new Size(90, 23);
delaySeconds.TextAlign = HorizontalAlignment.Right;
var delayUnitLabel = new Label
{
Text = "秒后提醒(0 = 立即)",
AutoSize = true,
Location = new Point(245, 104),
};
var shutdownLabel = new Label
{
Text = "通知倒计时",
AutoSize = true,
Location = new Point(26, 145),
};
shutdownDelaySeconds.Minimum = 10;
shutdownDelaySeconds.Maximum = 3600;
shutdownDelaySeconds.Location = new Point(144, 141);
shutdownDelaySeconds.Size = new Size(90, 23);
shutdownDelaySeconds.TextAlign = HorizontalAlignment.Right;
var secondsLabel = new Label
{
Text = "秒后自动关机",
AutoSize = true,
Location = new Point(245, 145),
};
shutdownWhenLocked.Text = "锁屏时拔电立即关机(关闭时解锁后再提醒)";
shutdownWhenLocked.AutoSize = true;
shutdownWhenLocked.Location = new Point(25, 181);
statusLabel.AutoSize = true;
statusLabel.Location = new Point(26, 220);
var testButton = new Button
{
Text = "测试通知",
Location = new Point(25, 256),
Size = new Size(92, 34),
};
testButton.Click += async (_, _) => await RunActionAsync(
async () =>
{
SaveConfig(updateTasks: false);
await ToastService.ShowPromptAsync(AppConfig.Load(), isPreview: true);
},
"正在预览动态通知;预览结束不会关机。");
disableButton.Text = "停用";
disableButton.Location = new Point(265, 256);
disableButton.Size = new Size(82, 34);
disableButton.Click += (_, _) => RunAction(TaskSchedulerService.Uninstall, "已停用系统触发器。");
enableButton.Text = "保存并启用";
enableButton.Location = new Point(357, 256);
enableButton.Size = new Size(98, 34);
enableButton.Click += (_, _) => RunAction(() => SaveConfig(updateTasks: true), "设置已保存并启用。");
Controls.AddRange([
title,
description,
delayLabel,
delaySeconds,
delayUnitLabel,
shutdownLabel,
shutdownDelaySeconds,
secondsLabel,
shutdownWhenLocked,
statusLabel,
testButton,
disableButton,
enableButton,
]);
}
private void LoadConfig()
{
var config = AppConfig.Load();
delaySeconds.Value = config.DelaySeconds;
shutdownDelaySeconds.Value = config.ShutdownDelaySeconds;
shutdownWhenLocked.Checked = config.ShutdownImmediatelyWhenLocked;
}
private void SaveConfig(bool updateTasks)
{
var config = new AppConfig
{
DelaySeconds = Decimal.ToInt32(delaySeconds.Value),
ShutdownDelaySeconds = Decimal.ToInt32(shutdownDelaySeconds.Value),
ShutdownImmediatelyWhenLocked = shutdownWhenLocked.Checked,
};
config.Save();
if (updateTasks)
{
TaskSchedulerService.Install(config);
}
}
private void RunAction(Action action, string successMessage, bool showDialog = true)
{
try
{
UseWaitCursor = true;
action();
RefreshStatus();
if (showDialog)
{
MessageBox.Show(successMessage, "Battery Down", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
statusLabel.Text = successMessage;
statusLabel.ForeColor = Color.FromArgb(30, 120, 60);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Battery Down", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
UseWaitCursor = false;
}
}
private async Task RunActionAsync(Func<Task> action, string statusMessage)
{
try
{
UseWaitCursor = true;
enableButton.Enabled = false;
disableButton.Enabled = false;
statusLabel.Text = statusMessage;
statusLabel.ForeColor = Color.FromArgb(30, 120, 60);
await action();
RefreshStatus();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Battery Down", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
UseWaitCursor = false;
enableButton.Enabled = true;
RefreshStatus();
}
}
private void RefreshStatus()
{
var installed = TaskSchedulerService.IsInstalled;
var lockTaskMissing = installed &&
AppConfig.Load().ShutdownImmediatelyWhenLocked &&
!TaskSchedulerService.IsLockedSystemTaskInstalled;
statusLabel.Text = lockTaskMissing
? "状态:锁屏关机任务未安装"
: installed
? "状态:已启用"
: "状态:未启用";
statusLabel.ForeColor = lockTaskMissing
? Color.FromArgb(180, 90, 20)
: installed
? Color.FromArgb(30, 120, 60)
: SystemColors.GrayText;
disableButton.Enabled = installed;
}
}