Repository navigation
Expand file tree
/
Copy pathDriverManagerForm.cs
More file actions
984 lines (924 loc) · 45.2 KB
/
Copy pathDriverManagerForm.cs
File metadata and controls
984 lines (924 loc) · 45.2 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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
using System.Globalization;
using System.Management;
using System.Drawing.Drawing2D;
using Microsoft.Win32;
namespace HonorPCHelper;
internal sealed class DriverManagerForm : Form
{
private sealed record DriverRow(DriverComponent Component, Label Date, LinkLabel Version);
private sealed record UpdateCheck(DriverCheckResult Drivers, ApplicationUpdateCheck? Application);
private static readonly Lock UpdateCheckLock = new();
private static Task<UpdateCheck>? _firstUpdateCheckTask;
// Шрифты общие для всех открытий окна: раньше каждая надпись создавала
// свой Font, и GDI-объекты копились при каждом открытии списка драйверов.
private static readonly Font BaseFont = new("Segoe UI", 9F);
private static readonly Font BoldFont = new("Segoe UI", 9F, FontStyle.Bold);
private static readonly Font LinkFont = new("Segoe UI", 9F, FontStyle.Underline);
private static readonly Font LegendFont = new("Segoe UI", 8.5F);
private static readonly Font LegendBoldFont = new("Segoe UI", 8.5F, FontStyle.Bold);
private static readonly TimeSpan InitialListTimeout = TimeSpan.FromSeconds(5);
private readonly DriverUpdateService _service = new();
private readonly ApplicationUpdateService _applicationUpdateService = new();
private readonly Panel _content = new();
private readonly TableLayoutPanel _biosTable = CreateUpdateTable();
private readonly TableLayoutPanel _driversTable = CreateUpdateTable();
private readonly Label _updatesLabel = new();
private readonly Button _refreshButton = new();
private readonly Button _exportButton = new();
private readonly Button _importButton = new();
private readonly TableLayoutPanel _legend = new();
private readonly ToolTip _toolTip = new();
private readonly ProgressBar _progress = new();
private readonly TextBox _serialNumber = new();
private readonly bool _dark = IsDarkTheme();
private readonly Color _eco = Color.FromArgb(0, 184, 148);
private readonly Color _turbo = Color.FromArgb(255, 45, 45);
private readonly Color _background;
private readonly Color _foreground;
private readonly Color _muted;
private CancellationTokenSource? _scanCancellation;
private readonly CancellationTokenSource _transferCancellation = new();
private Task<IReadOnlyList<DriverComponent>>? _initialComponentsTask;
private IReadOnlyList<DriverComponent> _components = [];
private bool _initialListRendered;
private readonly Dictionary<int, DriverRow> _driverRows = [];
private LinkLabel? _applicationVersion;
private ApplicationUpdate? _requestedApplicationUpdate;
internal DriverManagerForm(Task<IReadOnlyList<DriverComponent>>? initialComponentsTask = null)
{
_initialComponentsTask = initialComponentsTask;
_background = _dark ? Color.FromArgb(28, 28, 28) : Color.White;
_foreground = _dark ? Color.WhiteSmoke : Color.FromArgb(30, 30, 30);
_muted = _dark ? Color.FromArgb(135, 135, 135) : Color.Gray;
Text = L.T("BIOS и обновления драйверов", "BIOS and Driver Updates", "BIOS 和驱动程序更新");
StartPosition = FormStartPosition.CenterScreen;
MinimumSize = new Size(1050, 700);
Size = new Size(1280, 1050);
Opacity = 0;
AutoScaleMode = AutoScaleMode.Dpi;
ShowIcon = false;
Font = BaseFont;
BackColor = _background;
ForeColor = _foreground;
var biosTitle = CreateTitle("BIOS", true);
var biosPanel = CreatePanel(_biosTable);
var driversTitle = CreateTitle(L.T("Драйверы и программы", "Drivers and Software", "驱动程序和软件"), false);
var driversPanel = CreatePanel(_driversTable);
ConfigureLegend();
_content.Dock = DockStyle.Fill;
_content.AutoScroll = false;
_content.BackColor = _background;
_content.Controls.Add(driversPanel);
_content.Controls.Add(driversTitle);
_content.Controls.Add(biosPanel);
_content.Controls.Add(biosTitle);
_progress.Dock = DockStyle.Bottom;
_progress.Height = 3;
_progress.Style = ProgressBarStyle.Marquee;
_progress.Visible = false;
Controls.Add(_content);
Controls.Add(_progress);
Controls.Add(_legend);
_refreshButton.Click += async (_, _) => await ScanAsync(force: true);
Shown += (_, _) => BeginInvoke(async () => await InitializeAsync());
FormClosed += (_, _) =>
{
_scanCancellation?.Cancel();
_scanCancellation?.Dispose();
_transferCancellation.Cancel();
_transferCancellation.Dispose();
_toolTip.Dispose();
};
}
/// <summary>
/// Список устройств собирается ещё при запуске программы, поэтому к моменту
/// открытия окна он обычно уже готов. Тогда строки появляются до первого
/// показа окна: оно открывается сразу нужной высоты и не подрастает через
/// секунду после открытия.
/// </summary>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var initialTask = _initialComponentsTask;
if (initialTask is not { IsCompletedSuccessfully: true }) return;
_initialComponentsTask = null;
_components = initialTask.Result;
RenderRows(_components, [], null);
SetCheckingLabel();
Opacity = 1;
}
private Panel CreateTitle(string title, bool includeRefresh)
{
// Заголовок раздела драйверов должен вмещать кнопки переноса, а их
// высота считается от шрифта: при масштабировании интерфейса он
// растёт, и фиксированные 52 точки обрезали бы подписи.
var panel = new Panel
{
Dock = DockStyle.Top,
Height = includeRefresh ? 60 : DriversTitleHeight,
BackColor = _background
};
var iconPanel = new Panel
{
Size = new Size(46, 46),
Location = new Point(12, includeRefresh ? 7 : 3),
BackColor = _background
};
iconPanel.Paint += (_, e) => DrawTitleIcon(e.Graphics, iconPanel.ClientRectangle, includeRefresh);
panel.Controls.Add(iconPanel);
panel.Controls.Add(new Label
{
Text = title,
Font = BoldFont,
AutoSize = true,
Location = new Point(68, includeRefresh ? 20 : 14)
});
if (!includeRefresh) return AddTransferButtons(panel);
_updatesLabel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
_updatesLabel.Font = BoldFont;
_updatesLabel.TextAlign = ContentAlignment.MiddleRight;
_updatesLabel.Size = new Size(360, 36);
_updatesLabel.ForeColor = _eco;
_updatesLabel.AutoEllipsis = false;
_refreshButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
_refreshButton.Text = string.Empty;
_refreshButton.TextAlign = ContentAlignment.MiddleCenter;
_refreshButton.Padding = Padding.Empty;
_refreshButton.FlatStyle = FlatStyle.Flat;
_refreshButton.FlatAppearance.BorderSize = 0;
_refreshButton.BackColor = _dark ? Color.FromArgb(45, 45, 45) : Color.FromArgb(235, 235, 235);
_refreshButton.ForeColor = _foreground;
_refreshButton.Size = new Size(52, 46);
_refreshButton.Paint += (_, e) => DrawRefreshIcon(e.Graphics, _refreshButton.ClientRectangle,
_refreshButton.Enabled ? _foreground : _muted);
_toolTip.SetToolTip(_refreshButton, L.T("Обновить список", "Refresh list", "刷新列表"));
panel.Controls.Add(_updatesLabel);
panel.Controls.Add(_refreshButton);
void Align()
{
_refreshButton.Location = new Point(panel.ClientSize.Width - _refreshButton.Width - 12, 4);
_updatesLabel.Location = new Point(_refreshButton.Left - _updatesLabel.Width - 10, 9);
}
panel.Resize += (_, _) => Align();
Align();
return panel;
}
/// <summary>
/// Кнопки переноса драйверов живут в заголовке раздела «Драйверы и
/// программы»: они относятся ко всему списку, а не к отдельной строке.
/// </summary>
private Panel AddTransferButtons(Panel panel)
{
// Подписи короткие: раздел уже называется «Драйверы и программы»,
// а подробность уходит в подсказку.
var export = L.T("Экспорт", "Export", "导出");
var import = L.T("Импорт", "Import", "导入");
// Одинаковая ширина по самой длинной подписи: пара кнопок разного
// размера в заголовке смотрится неряшливо, а длина подписи зависит
// и от языка, и от масштаба интерфейса.
var size = new Size(
Math.Max(TransferButtonWidth(export), TransferButtonWidth(import)),
TransferButtonHeight);
ConfigureTransferButton(_exportButton, export, size,
L.T("Сохранить все сторонние драйверы системы в один архив.",
"Save every third-party driver installed in the system to a single archive.",
"将系统中安装的所有第三方驱动程序保存到一个存档中。"));
ConfigureTransferButton(_importButton, import, size,
L.T("Установить драйверы из архива или файла INF.",
"Install drivers from an archive or an INF file.",
"从存档或 INF 文件安装驱动程序。"));
_exportButton.Click += async (_, _) => await ExportDriversAsync();
_importButton.Click += async (_, _) => await ImportDriversAsync();
panel.Controls.Add(_exportButton);
panel.Controls.Add(_importButton);
void Align()
{
var top = Math.Max(0, (panel.ClientSize.Height - _importButton.Height) / 2);
_importButton.Location = new Point(panel.ClientSize.Width - _importButton.Width - 14, top);
_exportButton.Location = new Point(_importButton.Left - _exportButton.Width - 8, top);
}
panel.Resize += (_, _) => Align();
Align();
return panel;
}
private static int DriversTitleHeight => Math.Max(52, TransferButtonHeight + 14);
private static int TransferButtonHeight => Math.Max(28, BaseFont.Height + 12);
private static int TransferButtonWidth(string text)
=> TextRenderer.MeasureText(text, BaseFont).Width + BaseFont.Height;
private void ConfigureTransferButton(Button button, string text, Size size, string hint)
{
button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
button.Text = text;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.BackColor = _dark ? Color.FromArgb(45, 45, 45) : Color.FromArgb(235, 235, 235);
button.FlatAppearance.MouseOverBackColor = _dark ? Color.FromArgb(58, 58, 58) : Color.FromArgb(222, 222, 222);
button.FlatAppearance.MouseDownBackColor = _dark ? Color.FromArgb(70, 70, 70) : Color.FromArgb(208, 208, 208);
button.ForeColor = _foreground;
button.UseVisualStyleBackColor = false;
button.TextAlign = ContentAlignment.MiddleCenter;
button.Padding = Padding.Empty;
button.Margin = Padding.Empty;
button.Size = size;
_toolTip.SetToolTip(button, hint);
}
private async Task ExportDriversAsync()
{
using var dialog = new SaveFileDialog
{
Title = L.T("Экспорт драйверов", "Export drivers", "导出驱动程序"),
FileName = $"{ReadModel()}-drivers-{DateTime.Now:yyyy-MM-dd}.zip",
Filter = $"{L.T("Архив драйверов", "Driver archive", "驱动程序存档")} (*.zip)|*.zip",
AddExtension = true,
DefaultExt = "zip",
OverwritePrompt = true
};
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
var archivePath = dialog.FileName;
await RunTransferAsync(
L.T("Экспорт драйверов…", "Exporting drivers…", "正在导出驱动程序…"),
token => DriverTransferService.ExportAsync(archivePath, token),
result => L.T(
$"Сохранено пакетов драйверов: {result.PackageCount}.\n{archivePath}",
$"Driver packages saved: {result.PackageCount}.\n{archivePath}",
$"已保存驱动程序包:{result.PackageCount}。\n{archivePath}"),
refreshAfterwards: false);
}
private async Task ImportDriversAsync()
{
using var dialog = new OpenFileDialog
{
Title = L.T("Импорт драйверов", "Import drivers", "导入驱动程序"),
Filter = $"{L.T("Драйверы", "Drivers", "驱动程序")} (*.zip;*.inf)|*.zip;*.inf|"
+ $"{L.T("Все файлы", "All files", "所有文件")} (*.*)|*.*",
CheckFileExists = true,
Multiselect = false
};
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
var sourcePath = dialog.FileName;
await RunTransferAsync(
L.T("Импорт драйверов…", "Importing drivers…", "正在导入驱动程序…"),
token => DriverTransferService.ImportAsync(sourcePath, token),
result => L.T(
$"Установлено пакетов драйверов: {result.PackageCount}.",
$"Driver packages installed: {result.PackageCount}.",
$"已安装驱动程序包:{result.PackageCount}。")
+ (result.RebootRequired
? L.T("\nДля завершения установки нужна перезагрузка.",
"\nA restart is required to finish the installation.",
"\n需要重新启动以完成安装。")
: string.Empty),
refreshAfterwards: true);
}
private async Task RunTransferAsync(
string busyText,
Func<CancellationToken, Task<DriverTransferResult>> operation,
Func<DriverTransferResult, string> describe,
bool refreshAfterwards)
{
var previousText = _updatesLabel.Text;
var previousColor = _updatesLabel.ForeColor;
_exportButton.Enabled = false;
_importButton.Enabled = false;
_refreshButton.Enabled = false;
_updatesLabel.Text = busyText;
_updatesLabel.ForeColor = _eco;
_progress.Visible = true;
var succeeded = false;
try
{
var result = await operation(_transferCancellation.Token);
if (IsDisposed)
return;
succeeded = result.Succeeded;
MessageBox.Show(this,
result.Succeeded
? describe(result)
: result.Error ?? L.T("Не удалось перенести драйверы.",
"The drivers could not be transferred.", "无法传输驱动程序。"),
Text, MessageBoxButtons.OK,
result.Succeeded ? MessageBoxIcon.Information : MessageBoxIcon.Error);
}
catch (OperationCanceledException) { }
catch (Exception exception)
{
AppLog.Error("Driver transfer failed", exception);
if (!IsDisposed)
MessageBox.Show(this, exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (!IsDisposed)
{
_progress.Visible = false;
_updatesLabel.Text = previousText;
_updatesLabel.ForeColor = previousColor;
_exportButton.Enabled = true;
_importButton.Enabled = true;
_refreshButton.Enabled = true;
}
}
// Установленные версии изменились, поэтому список пересобирается
// заново — иначе он показывал бы то, что было до импорта.
if (succeeded && refreshAfterwards && !IsDisposed)
{
_components = await _service.BuildDeviceListAsync();
RenderRows(_components, [], null);
await ScanAsync(force: true);
}
}
private Panel CreatePanel(TableLayoutPanel table)
{
var panel = new Panel
{
Dock = DockStyle.Top,
AutoSize = true,
Padding = new Padding(14, 4, 14, 12),
BackColor = _background
};
table.BackColor = _background;
table.ForeColor = _foreground;
panel.Controls.Add(table);
return panel;
}
private void ConfigureLegend()
{
_legend.Dock = DockStyle.Bottom;
var legendRowHeight = Math.Max(50, Font.Height + 24);
_legend.Height = legendRowHeight * 2 + 28;
_legend.Padding = new Padding(14, 8, 14, 14);
_legend.ColumnCount = 4;
_legend.RowCount = 2;
_legend.BackColor = _background;
_legend.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 16));
_legend.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 28));
_legend.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 28));
_legend.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 28));
_legend.RowStyles.Add(new RowStyle(SizeType.Absolute, legendRowHeight));
_legend.RowStyles.Add(new RowStyle(SizeType.Absolute, legendRowHeight));
_legend.Controls.Add(LegendLabel(L.T("Серийный номер", "Serial", "序列号"), true), 0, 0);
_serialNumber.Text = "...";
_serialNumber.ReadOnly = true;
_serialNumber.Dock = DockStyle.Fill;
_serialNumber.BorderStyle = BorderStyle.FixedSingle;
_serialNumber.BackColor = _background;
_serialNumber.ForeColor = _foreground;
_serialNumber.Margin = new Padding(4);
_legend.Controls.Add(_serialNumber, 1, 0);
_legend.Controls.Add(LegendLabel(L.T("Легенда", "Legend", "图例"), true), 0, 1);
_legend.Controls.Add(LegendLabel(L.T("Версия не определена", "Can't check local version", "无法检查本地版本"), false, _muted), 1, 1);
_legend.Controls.Add(LegendLabel(L.T("Актуально", "Updated", "已更新"), false, _eco), 2, 1);
_legend.Controls.Add(LegendLabel(L.T("Доступно обновление", "Update Available", "有可用更新"), false, _turbo), 3, 1);
}
private Label LegendLabel(string text, bool bold, Color? backColor = null) => new()
{
Text = text,
Dock = DockStyle.Fill,
AutoEllipsis = true,
TextAlign = ContentAlignment.MiddleLeft,
Font = bold ? LegendBoldFont : LegendFont,
BackColor = backColor ?? _background,
ForeColor = backColor is null ? _foreground : Color.White,
Padding = new Padding(5, 0, 5, 0),
Margin = new Padding(4)
};
private async Task ScanAsync(bool force)
{
_scanCancellation?.Cancel();
_scanCancellation?.Dispose();
_scanCancellation = new CancellationTokenSource();
var cancellation = _scanCancellation.Token;
SetBusy(true);
_updatesLabel.Text = L.T("Проверка…", "Checking…", "正在检查…");
_updatesLabel.ForeColor = _eco;
try
{
var checkTask = GetUpdateCheckTask(force, _components);
var check = await checkTask.WaitAsync(cancellation);
cancellation.ThrowIfCancellationRequested();
var result = check.Drivers;
var applicationCheck = check.Application;
if (!result.IsComplete)
{
_updatesLabel.Text = L.T("Сервер HONOR не ответил, попробуйте позже", "HONOR server did not respond, try again later", "荣耀服务器无响应,请稍后再试");
_updatesLabel.ForeColor = _turbo;
return;
}
_components = result.Components;
ApplyUpdates(result.Updates, applicationCheck);
var count = result.Updates.Count(update => update.IsUpdate
&& _driverRows.ContainsKey(update.Component.Id))
+ (applicationCheck?.Update is null ? 0 : 1);
_updatesLabel.Text = count == 0
? L.T("Новых обновлений нет", "No new updates", "没有新更新")
: L.T($"Новых обновлений: {count}", $"New updates: {count}", $"新更新:{count}");
_updatesLabel.ForeColor = count == 0 ? _eco : _turbo;
}
catch (OperationCanceledException) { }
catch (Exception exception)
{
AppLog.Error("Driver update check failed", exception);
_updatesLabel.Text = L.T("Ошибка проверки", "Check failed", "检查失败");
_updatesLabel.ForeColor = _turbo;
}
finally { SetBusy(false); }
}
private async Task InitializeAsync()
{
_ = LoadSerialNumberAsync();
if (!_initialListRendered)
{
// Пока список не собран, окно остаётся невидимым: иначе пользователь
// видит пустое окно, которое через секунду вырастает по высоте.
// Ожидание ограничено по времени, чтобы окно не пропало совсем,
// если инвентаризация устройств затянулась.
var loading = LoadInitialListAsync();
try { await loading.WaitAsync(InitialListTimeout); }
catch (TimeoutException) { }
catch (Exception exception) { AppLog.Error("Initial driver list failed", exception); }
if (IsDisposed) return;
Opacity = 1;
try { await loading; }
catch { /* причина уже записана в журнал */ }
}
if (!IsDisposed)
await ScanAsync(force: false);
}
private Task<UpdateCheck> GetUpdateCheckTask(
bool force,
IReadOnlyList<DriverComponent> components)
{
lock (UpdateCheckLock)
{
if (!force && _firstUpdateCheckTask is not null)
return _firstUpdateCheckTask;
_firstUpdateCheckTask = RunUpdateCheckAsync(components);
return _firstUpdateCheckTask;
}
}
private async Task<UpdateCheck> RunUpdateCheckAsync(IReadOnlyList<DriverComponent> components)
{
var driverTask = Task.Run(() => _service.CheckAsync(components));
var applicationTask = Task.Run(() => _applicationUpdateService.CheckAsync());
ApplicationUpdateCheck? application = null;
try { application = await applicationTask; }
catch (Exception exception) { AppLog.Error("Application update check failed", exception); }
// Сорвавшаяся проверка ничего не говорит о наличии обновления, поэтому
// точку на значке трея гасит или зажигает только удавшаяся.
if (application is not null)
ApplicationUpdateWatcher.Publish(application.Update);
return new UpdateCheck(await driverTask, application);
}
private async Task LoadInitialListAsync()
{
var initialTask = Interlocked.Exchange(ref _initialComponentsTask, null);
try
{
_components = initialTask is null
? await _service.BuildDeviceListAsync()
: await initialTask;
}
catch (Exception exception)
{
AppLog.Error("Initial driver device inventory failed; retrying", exception);
_components = await _service.BuildDeviceListAsync();
}
RenderRows(_components, [], null);
SetCheckingLabel();
}
private void SetCheckingLabel()
{
_updatesLabel.Text = L.T("Проверка…", "Checking…", "正在检查…");
_updatesLabel.ForeColor = _eco;
}
private async Task LoadSerialNumberAsync()
{
try
{
var serialNumber = await Task.Run(ReadSerialNumber);
if (!IsDisposed)
_serialNumber.Text = serialNumber;
}
catch (Exception exception)
{
AppLog.Error("BIOS serial number lookup failed", exception);
if (!IsDisposed)
_serialNumber.Text = "—";
}
}
private void RenderRows(
IReadOnlyList<DriverComponent> components,
IReadOnlyList<DriverUpdate> updates,
ApplicationUpdateCheck? applicationCheck)
{
SuspendLayout();
try
{
ClearTable(_biosTable);
ClearTable(_driversTable);
_driverRows.Clear();
_applicationVersion = null;
var updatesById = updates.GroupBy(update => update.Component.Id)
.ToDictionary(group => group.Key, group => group.First());
var bios = components.FirstOrDefault(component => component.Id == 23);
if (bios is not null) AddRow(_biosTable, bios, updatesById.GetValueOrDefault(23));
AddApplicationRow(_driversTable, applicationCheck);
foreach (var component in components
.Where(c => c.Id > 0 && c.Id != 23 && c.Id is not 87 and not 88)
.OrderBy(Category).ThenBy(c => c.DisplayName))
AddRow(_driversTable, component, updatesById.GetValueOrDefault(component.Id));
Text = $"{L.T("BIOS и обновления драйверов", "BIOS and Driver Updates", "BIOS 和驱动程序更新")}: {ReadModel()} {bios?.CurrentVersion}";
_initialListRendered = true;
FitWindowToRows();
}
finally { ResumeLayout(true); }
// Запуск откладывается до конца раскладки: иначе полоса загрузки
// появилась бы в окне, которое ещё подбирает себе высоту.
if (_requestedApplicationUpdate is not null && IsHandleCreated)
BeginInvoke(StartRequestedApplicationUpdate);
}
private void AddRow(TableLayoutPanel table, DriverComponent component, DriverUpdate? update)
{
var row = table.RowCount++;
table.RowStyles.Add(new RowStyle(SizeType.Absolute, Math.Max(40, Font.Height + 16)));
table.Controls.Add(Cell(Category(component)), 0, row);
// Название строки берётся только из локальных данных об устройстве:
// после проверки обновлений оно не меняется, поэтому список остаётся
// тем же самым, а проверка лишь дополняет строки состоянием версий.
var title = Cell(RowTitle(component));
var date = Cell(FormatDate(update?.ReleaseDate));
table.Controls.Add(title, 1, row);
table.Controls.Add(date, 2, row);
var version = new LinkLabel
{
Text = DisplayVersion(update, component),
Dock = DockStyle.Fill,
AutoEllipsis = true,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(2, 3, 3, 2),
Font = LinkFont,
LinkColor = component.CurrentVersion == "0"
? _muted
: update?.IsUpdate == true ? _turbo : _eco,
ActiveLinkColor = component.CurrentVersion == "0"
? _muted
: update?.IsUpdate == true ? _turbo : _eco,
Cursor = update is null ? Cursors.Default : Cursors.Hand,
LinkBehavior = update is null ? LinkBehavior.NeverUnderline : LinkBehavior.AlwaysUnderline
};
version.Tag = update;
if (update is not null)
_toolTip.SetToolTip(version, L.T(
$"Нажмите, чтобы скачать драйвер версии {update.Version}.",
$"Click to download driver version {update.Version}.",
$"点击下载驱动程序版本 {update.Version}。"));
version.LinkClicked += async (_, _) =>
{
if (version.Tag is DriverUpdate driverUpdate)
await DownloadDriverAsync(driverUpdate, version);
};
table.Controls.Add(version, 4, row);
_driverRows[component.Id] = new DriverRow(component, date, version);
}
/// <summary>
/// Обновление, запрошенное из меню трея. Строка приложения появляется
/// только после первой отрисовки списка, поэтому запрос либо выполняется
/// сразу, либо ждёт её.
/// </summary>
internal void RequestApplicationUpdate(ApplicationUpdate update)
{
_requestedApplicationUpdate = update;
if (_applicationVersion is not null)
StartRequestedApplicationUpdate();
}
private void StartRequestedApplicationUpdate()
{
var update = _requestedApplicationUpdate;
_requestedApplicationUpdate = null;
// Отключённая ссылка означает, что загрузка уже идёт.
if (update is null || IsDisposed || _applicationVersion is not { Enabled: true } link)
return;
_ = DownloadApplicationUpdateAsync(update, link);
}
private void AddApplicationRow(TableLayoutPanel table, ApplicationUpdateCheck? check)
{
var row = table.RowCount++;
table.RowStyles.Add(new RowStyle(SizeType.Absolute, Math.Max(40, Font.Height + 16)));
table.Controls.Add(Cell(L.T("Программа", "Software", "软件")), 0, row);
table.Controls.Add(Cell("Honor PC Helper"), 1, row);
table.Controls.Add(Cell(string.Empty), 2, row);
var update = check?.Update;
var displayedVersion = update?.Version ?? check?.LatestVersion ?? _applicationUpdateService.CurrentVersion;
var version = new LinkLabel
{
Text = displayedVersion.ToString(3),
Dock = DockStyle.Fill,
AutoEllipsis = true,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(2, 3, 3, 2),
Font = LinkFont,
LinkColor = update is null ? _eco : _turbo,
ActiveLinkColor = update is null ? _eco : _turbo,
Cursor = update is null ? Cursors.Default : Cursors.Hand,
LinkBehavior = update is null ? LinkBehavior.NeverUnderline : LinkBehavior.AlwaysUnderline
};
version.Tag = update;
version.LinkClicked += async (_, _) =>
{
if (version.Tag is ApplicationUpdate applicationUpdate)
await DownloadApplicationUpdateAsync(applicationUpdate, version);
};
table.Controls.Add(version, 4, row);
_applicationVersion = version;
}
private void ApplyUpdates(
IReadOnlyList<DriverUpdate> updates,
ApplicationUpdateCheck? applicationCheck)
{
_biosTable.SuspendLayout();
_driversTable.SuspendLayout();
try
{
foreach (var update in updates)
{
if (!_driverRows.TryGetValue(update.Component.Id, out var row)) continue;
row.Date.Text = FormatDate(update.ReleaseDate);
row.Version.Text = DisplayVersion(update, row.Component);
row.Version.LinkColor = row.Component.CurrentVersion == "0"
? _muted
: update.IsUpdate ? _turbo : _eco;
row.Version.ActiveLinkColor = row.Version.LinkColor;
row.Version.Cursor = Cursors.Hand;
row.Version.LinkBehavior = LinkBehavior.AlwaysUnderline;
row.Version.Tag = update;
var installedVersion = row.Component.CurrentVersion == "0" ? "—" : row.Component.CurrentVersion;
_toolTip.SetToolTip(row.Version,
L.T($"Доступно: {update.Version}\nУстановлено: {installedVersion}",
$"Available: {update.Version}\nInstalled: {installedVersion}",
$"可用版本:{update.Version}\n已安装:{installedVersion}"));
}
if (_applicationVersion is null) return;
var applicationUpdate = applicationCheck?.Update;
var displayedVersion = applicationUpdate?.Version ?? applicationCheck?.LatestVersion
?? _applicationUpdateService.CurrentVersion;
_applicationVersion.Text = displayedVersion.ToString(3);
_applicationVersion.LinkColor = applicationUpdate is null ? _eco : _turbo;
_applicationVersion.ActiveLinkColor = _applicationVersion.LinkColor;
_applicationVersion.Cursor = applicationUpdate is null ? Cursors.Default : Cursors.Hand;
_applicationVersion.LinkBehavior = applicationUpdate is null
? LinkBehavior.NeverUnderline : LinkBehavior.AlwaysUnderline;
_applicationVersion.Tag = applicationUpdate;
_toolTip.SetToolTip(_applicationVersion, applicationUpdate is null
? string.Empty
: L.T(
$"Нажмите, чтобы скачать и установить Honor PC Helper {applicationUpdate.Version}.",
$"Click to download and install Honor PC Helper {applicationUpdate.Version}.",
$"点击下载并安装 Honor PC Helper {applicationUpdate.Version}。"));
}
finally
{
_driversTable.ResumeLayout(true);
_biosTable.ResumeLayout(true);
}
}
private async Task DownloadApplicationUpdateAsync(ApplicationUpdate update, LinkLabel link)
{
var versionText = link.Text;
link.Enabled = false;
link.Text = L.T("Загрузка... 0%", "Downloading... 0%", "正在下载... 0%");
_progress.Style = ProgressBarStyle.Continuous;
_progress.Value = 0;
_progress.Visible = true;
try
{
await _applicationUpdateService.DownloadAndRestartAsync(update, new Progress<int>(percent =>
{
percent = Math.Clamp(percent, _progress.Minimum, _progress.Maximum);
_progress.Value = percent;
link.Text = percent < 100
? L.T($"Загрузка... {percent}%", $"Downloading... {percent}%", $"正在下载... {percent}%")
: L.T("Установка...", "Installing...", "正在安装...");
}));
}
catch (OperationCanceledException) { }
catch (Exception exception)
{
AppLog.Error("Application update download failed", exception);
if (!IsDisposed)
MessageBox.Show(this, exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// После удачного обновления форма уже закрывается вместе с приложением.
if (!IsDisposed)
{
_progress.Visible = false;
_progress.Style = ProgressBarStyle.Marquee;
link.Text = versionText;
link.Enabled = true;
}
}
}
private async Task DownloadDriverAsync(DriverUpdate update, LinkLabel link)
{
var versionText = link.Text;
link.Enabled = false;
link.Text = L.T("Загрузка... 0%", "Downloading... 0%", "正在下载... 0%");
_progress.Style = ProgressBarStyle.Continuous;
_progress.Value = 0;
_progress.Visible = true;
try
{
await _service.DownloadAsync(update, new Progress<int>(percent =>
{
percent = Math.Clamp(percent, _progress.Minimum, _progress.Maximum);
_progress.Value = percent;
link.Text = percent < 100
? L.T($"Загрузка... {percent}%", $"Downloading... {percent}%", $"正在下载... {percent}%")
: L.T("Проверка файла...", "Verifying file...", "正在验证文件...");
}));
}
catch (OperationCanceledException) { }
catch (Exception exception)
{
AppLog.Error("Driver download failed", exception);
MessageBox.Show(this, exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
_progress.Visible = false;
_progress.Style = ProgressBarStyle.Marquee;
link.Text = versionText;
link.Enabled = true;
}
}
private void SetBusy(bool busy)
{
_refreshButton.Enabled = !busy;
_progress.Visible = busy;
}
private void DrawTitleIcon(Graphics graphics, Rectangle bounds, bool bios)
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
using var pen = new Pen(_foreground, Math.Max(2F, bounds.Width / 18F))
{
StartCap = LineCap.Square,
EndCap = LineCap.Square
};
var size = Math.Min(bounds.Width, bounds.Height);
var left = bounds.Left + (bounds.Width - size) / 2F;
var top = bounds.Top + (bounds.Height - size) / 2F;
if (bios)
{
var body = new RectangleF(left + size * .28F, top + size * .28F, size * .44F, size * .44F);
graphics.DrawRectangle(pen, body.X, body.Y, body.Width, body.Height);
graphics.DrawRectangle(pen, left + size * .4F, top + size * .4F, size * .2F, size * .2F);
for (var index = 0; index < 3; index++)
{
var offset = size * (.34F + index * .16F);
graphics.DrawLine(pen, left + offset, top + size * .14F, left + offset, body.Top);
graphics.DrawLine(pen, left + offset, body.Bottom, left + offset, top + size * .86F);
graphics.DrawLine(pen, left + size * .14F, top + offset, body.Left, top + offset);
graphics.DrawLine(pen, body.Right, top + offset, left + size * .86F, top + offset);
}
return;
}
var outer = new RectangleF(left + size * .2F, top + size * .2F, size * .6F, size * .6F);
var inner = new RectangleF(left + size * .34F, top + size * .34F, size * .32F, size * .32F);
graphics.DrawRectangle(pen, outer.X, outer.Y, outer.Width, outer.Height);
graphics.DrawRectangle(pen, inner.X, inner.Y, inner.Width, inner.Height);
}
private static void DrawRefreshIcon(Graphics graphics, Rectangle bounds, Color color)
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
var size = Math.Min(bounds.Width, bounds.Height) * .48F;
var left = bounds.Left + (bounds.Width - size) / 2F;
var top = bounds.Top + (bounds.Height - size) / 2F;
using var pen = new Pen(color, Math.Max(2F, size / 9F))
{
StartCap = LineCap.Round,
EndCap = LineCap.Round
};
graphics.DrawArc(pen, left, top, size, size, 35, 285);
var tip = new PointF(left + size * .96F, top + size * .4F);
graphics.DrawLine(pen, tip, new PointF(left + size * .72F, top + size * .27F));
graphics.DrawLine(pen, tip, new PointF(left + size * .82F, top + size * .64F));
}
private Label Cell(string text) => new()
{
Text = text,
Dock = DockStyle.Fill,
AutoEllipsis = true,
TextAlign = ContentAlignment.MiddleLeft,
ForeColor = _foreground,
Padding = new Padding(5, 3, 3, 2),
Margin = Padding.Empty
};
private static TableLayoutPanel CreateUpdateTable()
{
var table = new TableLayoutPanel
{
Dock = DockStyle.Top,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
ColumnCount = 5,
RowCount = 0,
Margin = Padding.Empty
};
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 15));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 39));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 18));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 0));
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 28));
return table;
}
private void FitWindowToRows()
{
var desiredClientHeight = _biosTable.PreferredSize.Height + _driversTable.PreferredSize.Height
+ 60 + DriversTitleHeight + _legend.Height + _progress.Height + 34;
ClientSize = new Size(1260, Math.Max(680, desiredClientHeight));
var area = Screen.FromControl(this).WorkingArea;
Location = new Point(
area.Left + (area.Width - Width) / 2,
area.Top + (area.Height - Height) / 2);
}
private static void ClearTable(TableLayoutPanel table)
{
while (table.Controls.Count > 0) table.Controls[0].Dispose();
table.RowCount = 0;
table.RowStyles.Clear();
}
private static string Category(DriverComponent component) => component.Id switch
{
1 or 2 or 4 or 6 or 12 or 55 or 56 or 65 or 73 or 74 => L.T("Чипсет", "Chipset", "芯片组"),
3 or 87 => L.T("Графика", "Graphics", "显卡"),
14 => L.T("Аудио", "Audio", "音频"),
15 => L.T("Сеть", "Networking", "网络"),
16 => "Bluetooth",
18 => L.T("Сканер отпечатка", "Fingerprint", "指纹识别"),
41 => L.T("Монитор", "Monitor", "显示器"),
52 => "NFC",
// Windows Studio Effects processes the camera image; under "Graphics" the
// row read as a display driver.
76 or 78 => L.T("Камера", "Camera", "摄像头"),
88 => L.T("Программы и утилиты", "Software and utilities", "软件和实用工具"),
23 => "BIOS",
_ => component.DisplayName
};
private static string RowTitle(DriverComponent component)
=> string.IsNullOrWhiteSpace(component.DeviceName)
? ComponentTitle(component)
: component.DeviceName;
private static string ComponentTitle(DriverComponent component) => component.Id switch
{
1 => L.T("Чипсет", "Chipset", "芯片组"),
2 => "Intel Management Engine",
3 => L.T("Графика", "Graphics", "显卡"),
4 => "Serial IO",
6 => L.T("Платформенная служба", "Platform framework", "平台框架"),
12 => L.T("Сторожевой таймер", "Watchdog", "看门狗"),
14 => L.T("Аудио", "Audio", "音频"),
15 => "Wi-Fi",
16 => "Bluetooth",
18 => L.T("Сканер отпечатка", "Fingerprint", "指纹识别"),
23 => "BIOS",
41 => L.T("Монитор", "Monitor", "显示器"),
52 => "NFC",
55 => "Intel PPM",
56 => "Intel TXT",
65 => "Intel Smart Sound",
73 => "Intel PMT",
74 => "Intel NPU",
76 => L.T("Камера", "Camera", "摄像头"),
78 => "Windows Studio Effects",
87 => L.T("Виртуальный дисплей", "Virtual Display", "虚拟显示器"),
88 => L.T("Виртуальное HID-устройство", "Virtual HID", "虚拟 HID 设备"),
_ => component.DisplayName
};
private static string FormatDate(string? value)
=> DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out var date)
? date.ToString("d", CultureInfo.CurrentCulture)
: string.Empty;
private static string DisplayVersion(DriverUpdate? update, DriverComponent component)
=> update is null || !update.IsUpdate && component.CurrentVersion != "0"
? component.CurrentVersion == "0" ? "—" : component.CurrentVersion
: update.Version;
private static string ReadModel() => Convert.ToString(Registry.GetValue(
@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS", "SystemProductName", "HONOR")) ?? "HONOR";
private static string ReadSerialNumber()
{
try
{
using var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BIOS");
using var results = searcher.Get();
return results.Cast<ManagementObject>().Select(i => Convert.ToString(i["SerialNumber"]))
.FirstOrDefault(v => !string.IsNullOrWhiteSpace(v)) ?? "—";
}
catch { return "—"; }
}
private static bool IsDarkTheme() => Convert.ToInt32(Registry.GetValue(
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
"AppsUseLightTheme", 1)) == 0;
}