-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
726 lines (626 loc) · 24.1 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
726 lines (626 loc) · 24.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Windows.Forms;
using smartClass.Models;
using smartClass.Services;
using smartClass.Windows;
namespace smartClass
{
public partial class MainWindow : Window
{
private AppState _state = new AppState();
private DispatcherTimer _timer;
private DispatcherTimer _clockTimer;
private NotifyIcon _notifyIcon;
private ExamClockWindow? _examClock;
private ScheduleWindow? _scheduleWindow;
private bool _allowClose = false;
private bool _isShuttingDown = false;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
Closing += MainWindow_Closing;
// 阻止 Alt+F4 关闭
PreviewKeyDown += (s, e) =>
{
if (e.Key == System.Windows.Input.Key.System && e.SystemKey == System.Windows.Input.Key.F4)
{
e.Handled = true;
}
};
// 点击时钟 → 全屏数字时钟
ClockPanel.MouseLeftButtonDown += (s, e) =>
{
try { ShowExamClock(); }
catch (Exception ex) { LogService.Log(ex, "全屏时钟打开失败"); }
};
}
/// <summary>
/// 顶栏模式初始化:加载数据、定位到屏幕顶部、启动定时器
/// </summary>
public void InitializeTopBar()
{
try
{
_state = StorageService.Load();
LogService.Log($"数据加载完成: {_state.Students.Count} 学生, {_state.Courses.Count} 课程, {_state.DutyGroups.Count} 值日组");
}
catch (Exception ex)
{
LogService.Log(ex, "InitializeTopBar 加载数据失败");
_state = new AppState();
}
try
{
SetupTrayIcon();
}
catch (Exception ex)
{
LogService.Log(ex, "托盘图标初始化失败");
}
try
{
StartTimers();
}
catch (Exception ex)
{
LogService.Log(ex, "定时器启动失败");
}
// 窗口句柄就绪后定位到屏幕顶部并预留工作区
SourceInitialized += (s, e) =>
{
try { PositionAtTop(); }
catch (Exception ex) { LogService.Log(ex, "顶栏定位失败"); }
};
// 延迟检查昨日值日
Dispatcher.BeginInvoke(new Action(() =>
{
try { CheckYesterdayDuties(); }
catch (Exception ex) { LogService.Log(ex, "昨日值日检查失败"); }
}), DispatcherPriority.ApplicationIdle);
// 延迟创建课程表浮窗
Dispatcher.BeginInvoke(new Action(() =>
{
try { ShowScheduleWindow(); }
catch (Exception ex) { LogService.Log(ex, "课程表窗口创建失败"); }
}), DispatcherPriority.ApplicationIdle);
// 延迟重新打开未关闭的通知窗口
Dispatcher.BeginInvoke(new Action(() =>
{
try { ReopenNotifications(); }
catch (Exception ex) { LogService.Log(ex, "重新打开通知窗口失败"); }
}), DispatcherPriority.ApplicationIdle);
// 首次刷新 UI
RefreshUI();
}
#region 窗口定位
private void PositionAtTop()
{
var screen = System.Windows.Forms.Screen.PrimaryScreen;
if (screen == null) return;
var bounds = screen.Bounds;
Left = bounds.Left;
Top = bounds.Top;
Width = bounds.Width;
Height = 70;
Topmost = true;
// HWND 就绪后注册为 AppBar(自动缩小工作区排开桌面图标)
Dispatcher.BeginInvoke(new Action(() =>
{
try { AppBarHelper.Register(this); }
catch (Exception ex) { LogService.Log(ex, "AppBar 注册失败"); }
}), DispatcherPriority.ApplicationIdle);
}
#endregion
#region 定时器
private void StartTimers()
{
// 业务逻辑定时器(60秒检查课程/值日)
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(60);
_timer.Tick += Timer_Tick;
_timer.Start();
LogService.Log("业务定时器已启动 (间隔60秒)");
// UI 刷新定时器(1秒刷新时钟和倒计时)
_clockTimer = new DispatcherTimer();
_clockTimer.Interval = TimeSpan.FromSeconds(1);
_clockTimer.Tick += ClockTimer_Tick;
_clockTimer.Start();
}
private void ClockTimer_Tick(object? sender, EventArgs e)
{
try
{
RefreshUI();
}
catch (Exception ex)
{
LogService.Log(ex, "时钟刷新失败");
}
}
#endregion
#region UI 刷新
/// <summary>
/// 刷新顶栏所有 UI 元素(日期、时钟、倒计时、课程、值日)
/// </summary>
private void RefreshUI()
{
var now = DateTime.Now;
// 日期:M月d日 ddd
var weekMap = new[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
DateLabel.Text = $"{now.Month}月{now.Day}日 {weekMap[(int)now.DayOfWeek]}";
// 时钟
ClockLabel.Text = now.ToString("HH:mm:ss");
// 倒计时
var daysLeft = (_state.SemesterEndDate.Date - now.Date).Days;
if (daysLeft > 0)
CountdownLabel.Text = $"距期末 {daysLeft} 天";
else if (daysLeft == 0)
CountdownLabel.Text = "学期最后一天";
else
CountdownLabel.Text = "假期啦";
// 今日课程
var todayText = weekMap[(int)now.DayOfWeek];
var todayCourses = _state.Courses
.Where(c => c.DayOfWeek == todayText)
.OrderBy(c => c.StartTime)
.ToList();
// 找到下一节和下二节
string nextText = "", next2Text = "";
foreach (var c in todayCourses)
{
if (TimeSpan.TryParse(c.StartTime, out var start))
{
var courseTime = new TimeSpan(now.Hour, now.Minute, 0);
if (start > courseTime)
{
if (string.IsNullOrEmpty(nextText))
nextText = $"{c.Subject} {c.StartTime}-{c.EndTime}";
else if (string.IsNullOrEmpty(next2Text))
{
next2Text = $"{c.Subject} {c.StartTime}-{c.EndTime}";
break;
}
}
}
}
NextCourseLabel.Text = string.IsNullOrEmpty(nextText) ? "今日课程已结束" : nextText;
Next2CourseLabel.Text = next2Text;
}
#endregion
#region 托盘图标
private void SetupTrayIcon()
{
_notifyIcon = new NotifyIcon();
_notifyIcon.Visible = true;
try
{
var exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (!string.IsNullOrEmpty(exePath) && System.IO.File.Exists(exePath))
_notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(exePath);
}
catch (Exception ex)
{
LogService.Log(ex, "提取exe图标失败");
}
if (_notifyIcon.Icon == null)
LogService.Log("托盘图标为 null,将使用系统默认图标");
_notifyIcon.Text = "SmartClass";
var menu = new ContextMenuStrip();
var settingsItem = new ToolStripMenuItem("设置");
settingsItem.Click += (s, e) =>
{
try { OpenSettings(); }
catch (Exception ex) { LogService.Log(ex, "托盘菜单-设置"); }
};
var showSchedItem = new ToolStripMenuItem("显示/隐藏课程表");
showSchedItem.Click += (s, e) =>
{
try { ToggleScheduleWindow(); }
catch (Exception ex) { LogService.Log(ex, "托盘菜单-切换课程表"); }
};
var exportItem = new ToolStripMenuItem("导出报表");
exportItem.Click += (s, e) =>
{
try { ExportReports(); }
catch (Exception ex) { LogService.Log(ex, "托盘菜单-导出报表"); }
};
var exitItem = new ToolStripMenuItem("退出程序");
exitItem.Click += (s, e) => { _allowClose = true; Close(); };
var restartItem = new ToolStripMenuItem("重启程序");
restartItem.Click += (s, e) =>
{
try { RestartApplication(); }
catch (Exception ex) { LogService.Log(ex, "托盘菜单-重启"); }
};
menu.Items.Add(settingsItem);
menu.Items.Add(showSchedItem);
menu.Items.Add(exportItem);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add(restartItem);
menu.Items.Add(exitItem);
_notifyIcon.ContextMenuStrip = menu;
}
#endregion
#region 设置
private void OpenSettings()
{
Dispatcher.Invoke(() =>
{
var win = new SettingsWindow();
win.Owner = this;
win.ShowDialog();
_state = StorageService.Load();
RefreshUI();
UpdateScheduleWindow();
});
}
#endregion
#region 课程表浮窗
private void ShowScheduleWindow()
{
if (_scheduleWindow != null) return;
Dispatcher.Invoke(() =>
{
try
{
_scheduleWindow = new ScheduleWindow(_state);
_scheduleWindow.Closed += (s, e) => _scheduleWindow = null;
_scheduleWindow.Show();
}
catch (Exception ex)
{
LogService.Log(ex, "创建课程表窗口失败");
_scheduleWindow = null;
}
});
}
private void ToggleScheduleWindow()
{
if (_scheduleWindow == null)
ShowScheduleWindow();
else
{
try { _scheduleWindow.Close(); }
catch (Exception ex) { LogService.Log(ex, "关闭课程表窗口"); }
_scheduleWindow = null;
}
}
public void UpdateScheduleWindow()
{
try
{
if (_scheduleWindow != null)
_scheduleWindow.UpdateState(_state);
}
catch (Exception ex)
{
LogService.Log(ex, "更新课程表窗口失败");
_scheduleWindow = null;
ShowScheduleWindow();
}
}
#endregion
#region 通知窗口
private void ReopenNotifications()
{
try
{
if (_state.Notifications == null || _state.Notifications.Count == 0)
return;
foreach (var notification in _state.Notifications)
{
var win = new NotificationWindow(notification, _state.FontSize);
win.Show();
}
LogService.Log($"已重新打开 {_state.Notifications.Count} 个通知窗口");
}
catch (Exception ex)
{
LogService.Log(ex, "重新打开通知窗口失败");
}
}
#endregion
#region 全屏时钟
private void ShowExamClock()
{
if (_examClock != null && _examClock.IsVisible) return;
_examClock = new ExamClockWindow();
_examClock.Closed += (s, e) => _examClock = null;
_examClock.Show();
}
#endregion
#region 业务逻辑定时器
private void Timer_Tick(object? sender, EventArgs e)
{
try
{
if (_isShuttingDown) return;
var now = DateTime.Now;
foreach (var c in _state.Courses)
{
try
{
if (string.IsNullOrWhiteSpace(c.StartTime) ||
string.IsNullOrWhiteSpace(c.EndTime) ||
string.IsNullOrWhiteSpace(c.DayOfWeek))
continue;
if (!MatchesToday(c.DayOfWeek)) continue;
if (TimeMatches(now, c.StartTime))
AskBoardCleaned(c);
if (TimeMatches(now, c.EndTime))
NotifyAfterClass(c);
}
catch (Exception ex)
{
LogService.Log(ex, $"处理课程 '{c.Subject}' 时出错");
}
}
// 放学提醒
if (now.Hour == 17 && now.Minute == 30)
{
try { NotifyDutyAtEndOfDay(); }
catch (Exception ex) { LogService.Log(ex, "放学提醒失败"); }
}
}
catch (Exception ex)
{
LogService.Log(ex, "Timer_Tick 顶层异常(定时器继续运行)");
}
}
private bool MatchesToday(string dayOfWeek)
{
var map = new[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
return dayOfWeek == map[(int)DateTime.Now.DayOfWeek];
}
private bool TimeMatches(DateTime now, string time)
{
if (!TimeSpan.TryParse(time, out var t)) return false;
return now.Hour == t.Hours && now.Minute == t.Minutes;
}
#endregion
#region 提醒 / 对话框
private void NotifyAfterClass(Models.Course course)
{
try
{
var text = $"课程 {course.Subject} 下课,请老师确认并提醒值日生擦黑板。";
_notifyIcon?.ShowBalloonTip(5000, "下课提醒", text, ToolTipIcon.Info);
if (_state.EnableSpeech)
SpeechService.SpeakAsync($"下课了。{course.Subject}课程已结束,请提醒值日生擦黑板。");
}
catch (Exception ex)
{
LogService.Log(ex, $"下课提醒失败: {course.Subject}");
}
}
private void AskBoardCleaned(Models.Course course)
{
if (_state.EnableSpeech)
SpeechService.SpeakAsync($"上课了。{course.Subject}课程开始,请检查黑板是否已擦干净。");
Dispatcher.Invoke(() =>
{
try
{
var dlg = new AutoCloseDialog(
$"课程 {course.Subject} 上课:黑板是否已擦?",
TimeSpan.FromMinutes(10));
var res = dlg.ShowDialog();
if (res == true)
ApplyBoardResult(course, true);
else if (res == false)
ApplyBoardResult(course, false);
}
catch (Exception ex)
{
LogService.Log(ex, $"黑板询问对话框失败: {course.Subject}");
}
});
}
private void ApplyBoardResult(Models.Course course, bool cleaned)
{
try
{
var today = DateTime.Today;
var duty = _state.DailyDuties.FirstOrDefault(d => d.Date.Date == today);
if (duty == null) return;
var group = _state.DutyGroups.FirstOrDefault(g => g.Id == duty.DutyGroupId);
if (group == null) return;
foreach (var m in group.Members)
{
var student = _state.Students.FirstOrDefault(s => s.Id == m.StudentId);
if (student == null) continue;
student.SocialCredits += cleaned ? 1 : -1;
}
StorageService.Save(_state);
}
catch (Exception ex)
{
LogService.Log(ex, $"黑板评分失败: {course.Subject}, cleaned={cleaned}");
}
}
private void NotifyDutyAtEndOfDay()
{
try
{
var today = DateTime.Today;
var duty = _state.DailyDuties.FirstOrDefault(d => d.Date.Date == today);
if (duty == null) return;
var group = _state.DutyGroups.FirstOrDefault(g => g.Id == duty.DutyGroupId);
if (group == null) return;
var names = group.Members.Select(m =>
_state.Students.FirstOrDefault(s => s.Id == m.StudentId)?.Name ?? "未知");
var text = "请值日生执行任务: " + string.Join(",", names);
_notifyIcon?.ShowBalloonTip(7000, "放学提醒", text, ToolTipIcon.Info);
if (_state.EnableSpeech)
SpeechService.SpeakAsync("放学时间到。请今天的值日生" +
string.Join("、", names) + "执行值日任务。");
}
catch (Exception ex)
{
LogService.Log(ex, "放学值日通知失败");
}
}
private void CheckYesterdayDuties()
{
try
{
var yesterday = DateTime.Today.AddDays(-1);
var duty = _state.DailyDuties.FirstOrDefault(d => d.Date.Date == yesterday);
if (duty == null) return;
Dispatcher.Invoke(() =>
{
try
{
var res = System.Windows.MessageBox.Show(
"昨日值日是否已完成?\n点击 是=完成,否=未完成",
"昨日值日",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
var group = _state.DutyGroups.FirstOrDefault(g => g.Id == duty.DutyGroupId);
if (group == null) return;
foreach (var m in group.Members)
{
var student = _state.Students.FirstOrDefault(s => s.Id == m.StudentId);
if (student == null) continue;
student.SocialCredits += (res == MessageBoxResult.Yes) ? 5 : -5;
}
StorageService.Save(_state);
}
catch (Exception ex)
{
LogService.Log(ex, "昨日值日检查对话框");
}
});
}
catch (Exception ex)
{
LogService.Log(ex, "昨日值日检查失败");
}
}
#endregion
#region 报表 / 重启
private void ExportReports()
{
try
{
var dir = AppDomain.CurrentDomain.BaseDirectory;
var path = System.IO.Path.Combine(dir, "students.csv");
using (var sw = new System.IO.StreamWriter(path, false, System.Text.Encoding.UTF8))
{
sw.WriteLine("Id,Name,SocialCredits");
foreach (var s in _state.Students)
sw.WriteLine($"{s.Id},{s.Name},{s.SocialCredits}");
}
_notifyIcon?.ShowBalloonTip(4000, "导出完成", "导出 students.csv 到应用目录", ToolTipIcon.Info);
LogService.Log($"导出报表完成: {path}");
}
catch (Exception ex)
{
LogService.Log(ex, "导出报表失败");
_notifyIcon?.ShowBalloonTip(4000, "导出失败", "导出 students.csv 失败,请查看 error.log", ToolTipIcon.Error);
}
}
private void RestartApplication()
{
try
{
var exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (!string.IsNullOrEmpty(exePath))
System.Diagnostics.Process.Start(exePath);
AppBarHelper.Unregister();
_allowClose = true;
_isShuttingDown = true;
System.Windows.Application.Current.Shutdown();
}
catch (Exception ex)
{
LogService.Log(ex, "重启失败");
System.Windows.MessageBox.Show("重启失败:\n" + ex.Message, "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
#region 公开方法
public void ShowTestNotification(string title, string text, int durationMs = 5000)
{
try
{
_notifyIcon?.ShowBalloonTip(durationMs, title, text, ToolTipIcon.Info);
}
catch (Exception ex)
{
LogService.Log(ex, "测试通知失败");
}
}
public void ReloadState()
{
try
{
_state = StorageService.Load();
RefreshUI();
UpdateScheduleWindow();
}
catch (Exception ex)
{
LogService.Log(ex, "MainWindow 重新加载状态失败");
}
}
#endregion
#region 生命周期
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
if (_state.Students == null || !_state.Students.Any())
_state = StorageService.Load();
RefreshUI();
// 时钟弹入动画(弹性缩放 + 淡入)
var clockAnim = (Storyboard)FindResource("ClockEntranceAnimation");
clockAnim.Begin();
}
catch (Exception ex)
{
LogService.Log(ex, "MainWindow_Loaded");
}
}
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
{
// 阻止 Alt+F4 / 系统菜单关闭,仅允许托盘菜单"退出"触发
if (!_allowClose)
{
e.Cancel = true;
return;
}
_isShuttingDown = true;
AppBarHelper.Unregister();
try { _timer?.Stop(); LogService.Log("业务定时器已停止"); }
catch (Exception ex) { LogService.Log(ex, "停止定时器失败"); }
try { _clockTimer?.Stop(); }
catch (Exception ex) { LogService.Log(ex, "停止时钟定时器失败"); }
try { _scheduleWindow?.Close(); }
catch (Exception ex) { LogService.Log(ex, "关闭课程表窗口失败"); }
try
{
if (_notifyIcon != null)
{
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
}
}
catch (Exception ex) { LogService.Log(ex, "释放托盘图标失败"); }
try
{
StorageService.Save(_state);
LogService.Log("退出时数据已保存");
}
catch (Exception ex) { LogService.Log(ex, "退出保存数据失败"); }
}
#endregion
}
}