-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDialogs.cs
More file actions
840 lines (725 loc) · 29.3 KB
/
Copy pathAppDialogs.cs
File metadata and controls
840 lines (725 loc) · 29.3 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
// Last comment Update 2026-08-21 09:20
using System;
using System.Windows.Forms;
namespace c2flux
{
public static class AppDialogs
{
// Central entry point for application dialogs.
// Visual styling must come from AntdThemeService; all visible UI text must use LocalizationService or
// already-localized parameters.
private const int IDI_QUESTION = 32514;
private const int IDI_WARNING = 32515;
private const int DI_NORMAL = 0x0003;
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool DrawIconEx(
IntPtr hdc,
int xLeft,
int yTop,
IntPtr hIcon,
int cxWidth,
int cyWidth,
int istepIfAniCur,
IntPtr hbrFlickerFreeDraw,
int diFlags);
// Uses the current AntdUI theme when no AppSettings instance is available.
public static DialogResult ShowWarningOk(
string messageText,
string title,
string okButtonText)
{
AppSettings settings = new AppSettings
{
Layout =
AntdUI.Config.Mode == AntdUI.TMode.Dark
? AppLayout.WindowsDarkMode
: AppLayout.WindowsLightMode
};
return ShowWarningOk(
settings,
messageText,
title,
okButtonText);
}
public static DialogResult ShowWarningOk(
AppSettings settings,
string messageText,
string title,
string okButtonText)
{
using WarningOkDialogForm dialogForm =
new WarningOkDialogForm(
settings,
title,
messageText,
okButtonText);
return dialogForm.ShowDialog();
}
public static DialogResult ShowWarningYesNo(
IWin32Window owner,
AppSettings settings,
string messageText,
string title,
string yesButtonText,
string noButtonText)
{
using WarningYesNoDialogForm dialogForm =
new WarningYesNoDialogForm(
settings,
title,
messageText,
yesButtonText,
noButtonText);
return dialogForm.ShowDialog(owner);
}
// Returns both the user's elevation choice and the "do not show again" state.
public static ElevationPromptResult ShowElevationPrompt(AppSettings settings)
{
using DialogForm dialogForm = new DialogForm(
settings,
AppConstants.ApplicationName,
LocalizationService.Format(
"Elevation.Message",
AppConstants.ApplicationName),
LocalizationService.GetText("Elevation.Important"),
LocalizationService.GetText("Elevation.DoNotShowAgain"),
LocalizationService.GetText("Common.Yes"),
LocalizationService.GetText("Common.No"));
DialogResult dialogResult = dialogForm.ShowDialog();
return new ElevationPromptResult(
dialogResult == DialogResult.Yes,
dialogForm.IsCheckBoxChecked);
}
public readonly struct ElevationPromptResult
{
public ElevationPromptResult(bool shouldRestartElevated, bool doNotShowAgain)
{
ShouldRestartElevated = shouldRestartElevated;
DoNotShowAgain = doNotShowAgain;
}
public bool ShouldRestartElevated { get; }
public bool DoNotShowAgain { get; }
}
public static DialogResult ShowSelectFolderDialog(
IWin32Window owner,
AppSettings settings,
string title,
out string selectedPath)
{
using AppFileDialog dialog =
new AppFileDialog(
settings,
AppFileDialogMode.SelectFolder,
title,
string.Empty,
string.Empty,
string.Empty,
LocalizationService.GetText(
"Dialog.SelectFolder"));
DialogResult result =
owner == null
? dialog.ShowDialog()
: dialog.ShowDialog(owner);
selectedPath =
result == DialogResult.OK
? dialog.SelectedPath
: string.Empty;
return result;
}
public static DialogResult ShowOpenFileDialog(
IWin32Window owner,
AppSettings settings,
string title,
string filter,
out string fileName)
{
using AppFileDialog dialog =
new AppFileDialog(
settings,
AppFileDialogMode.OpenFile,
title,
filter,
string.Empty,
string.Empty,
LocalizationService.GetText(
"Toolbar.Open"));
DialogResult result =
owner == null
? dialog.ShowDialog()
: dialog.ShowDialog(owner);
fileName =
result == DialogResult.OK
? dialog.SelectedPath
: string.Empty;
return result;
}
public static DialogResult ShowSaveFileDialog(
IWin32Window owner,
AppSettings settings,
string title,
string filter,
string defaultExtension,
string initialFileName,
string confirmButtonText,
out string fileName)
{
using AppFileDialog dialog =
new AppFileDialog(
settings,
AppFileDialogMode.SaveFile,
title,
filter,
defaultExtension,
initialFileName,
confirmButtonText);
DialogResult result =
owner == null
? dialog.ShowDialog()
: dialog.ShowDialog(owner);
fileName =
result == DialogResult.OK
? dialog.SelectedPath
: string.Empty;
return result;
}
// Fixed warning dialog using the shared application theme.
private sealed class WarningOkDialogForm : Form
{
private readonly AppSettings _settings;
private readonly string _messageText;
private readonly string _okButtonText;
private NativeWarningIconControl nativeWarningIconControl;
private AntdUI.Label labelMessage;
private AntdUI.Button buttonOk;
public WarningOkDialogForm(
AppSettings settings,
string title,
string messageText,
string okButtonText)
{
_settings = settings;
_messageText = messageText;
_okButtonText = okButtonText;
Text = title;
InitializeComponent();
AntdThemeService.Apply(this, _settings.Layout);
Shown += Dialog_Shown;
}
private void Dialog_Shown(
object sender,
EventArgs e)
{
SuspendLayout();
try
{
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
PerformAutoScale();
PerformLayout();
MinimumSize = Size;
MaximumSize = Size;
Invalidate(true);
Update();
}
finally
{
ResumeLayout(true);
}
}
private void InitializeComponent()
{
AutoScaleMode = AutoScaleMode.Dpi;
AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new System.Drawing.Size(430, 178);
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
BackColor = AntdThemeService.BackgroundPrimary;
ForeColor = AntdThemeService.TextPrimary;
nativeWarningIconControl = new NativeWarningIconControl
{
Name = "nativeWarningIconControl",
Location = new System.Drawing.Point(28, 42),
Size = new System.Drawing.Size(32, 32)
};
labelMessage = new AntdUI.Label
{
Name = "labelMessage",
Text = _messageText,
Location = new System.Drawing.Point(82, 28),
Size = new System.Drawing.Size(324, 60),
ForeColor = AntdThemeService.TextPrimary,
TextAlign = System.Drawing.ContentAlignment.MiddleLeft
};
buttonOk = new AntdUI.Button
{
Name = "buttonOk",
Text = _okButtonText,
Location = new System.Drawing.Point(326, 122),
Size = new System.Drawing.Size(84, 32),
Type = AntdUI.TTypeMini.Primary,
DialogResult = DialogResult.OK
};
Controls.Add(nativeWarningIconControl);
Controls.Add(labelMessage);
Controls.Add(buttonOk);
AcceptButton = buttonOk;
CancelButton = buttonOk;
}
}
// Fixed Yes/No warning dialog using the shared application theme.
private sealed class WarningYesNoDialogForm : Form
{
private readonly AppSettings _settings;
private readonly string _messageText;
private readonly string _yesButtonText;
private readonly string _noButtonText;
private NativeWarningIconControl nativeWarningIconControl;
private AntdUI.Label labelMessage;
private AntdUI.Button buttonYes;
private AntdUI.Button buttonNo;
public WarningYesNoDialogForm(
AppSettings settings,
string title,
string messageText,
string yesButtonText,
string noButtonText)
{
_settings = settings;
_messageText = messageText;
_yesButtonText = yesButtonText;
_noButtonText = noButtonText;
Text = title;
InitializeComponent();
AntdThemeService.Apply(this, _settings.Layout);
Shown += Dialog_Shown;
}
private void Dialog_Shown(
object sender,
EventArgs e)
{
SuspendLayout();
try
{
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
PerformAutoScale();
PerformLayout();
MinimumSize = Size;
MaximumSize = Size;
Invalidate(true);
Update();
}
finally
{
ResumeLayout(true);
}
}
private void InitializeComponent()
{
AutoScaleMode = AutoScaleMode.Dpi;
AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
StartPosition = FormStartPosition.CenterParent;
ClientSize = new System.Drawing.Size(430, 178);
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
BackColor = AntdThemeService.BackgroundPrimary;
ForeColor = AntdThemeService.TextPrimary;
nativeWarningIconControl = new NativeWarningIconControl
{
Name = "nativeWarningIconControl",
Location = new System.Drawing.Point(28, 42),
Size = new System.Drawing.Size(32, 32)
};
labelMessage = new AntdUI.Label
{
Name = "labelMessage",
Text = _messageText,
Location = new System.Drawing.Point(82, 28),
Size = new System.Drawing.Size(324, 60),
ForeColor = AntdThemeService.TextPrimary,
TextAlign = System.Drawing.ContentAlignment.MiddleLeft
};
buttonYes = new AntdUI.Button
{
Name = "buttonYes",
Text = _yesButtonText,
Location = new System.Drawing.Point(232, 122),
Size = new System.Drawing.Size(84, 32),
Type = AntdUI.TTypeMini.Default,
DialogResult = DialogResult.Yes
};
buttonNo = new AntdUI.Button
{
Name = "buttonNo",
Text = _noButtonText,
Location = new System.Drawing.Point(326, 122),
Size = new System.Drawing.Size(84, 32),
Type = AntdUI.TTypeMini.Primary,
DialogResult = DialogResult.No
};
Controls.Add(nativeWarningIconControl);
Controls.Add(labelMessage);
Controls.Add(buttonYes);
Controls.Add(buttonNo);
AcceptButton = buttonYes;
CancelButton = buttonNo;
}
}
// Elevation prompt with an additional persisted user choice.
private sealed class DialogForm : Form
{
private readonly AppSettings _settings;
private readonly string _messageText;
private readonly string _importantText;
private readonly string _checkBoxText;
private readonly string _yesButtonText;
private readonly string _noButtonText;
private NativeQuestionIconControl nativeQuestionIconControl;
private AntdUI.Label labelMessage;
private AntdUI.Label labelImportant;
private AntdUI.Checkbox checkBoxOption;
private AntdUI.Button buttonYes;
private AntdUI.Button buttonNo;
public bool IsCheckBoxChecked
{
get { return checkBoxOption.Checked; }
}
public DialogForm(
AppSettings settings,
string title,
string messageText,
string importantText,
string checkBoxText,
string yesButtonText,
string noButtonText)
{
_settings = settings;
_messageText = messageText;
_importantText = importantText;
_checkBoxText = checkBoxText;
_yesButtonText = yesButtonText;
_noButtonText = noButtonText;
Text = title;
InitializeComponent();
AntdThemeService.Apply(this, _settings.Layout);
ApplyThemeColors();
ApplyImportantLabelStyle();
Shown += Dialog_Shown;
}
private void Dialog_Shown(
object sender,
EventArgs e)
{
SuspendLayout();
try
{
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
if (DeviceDpi >= 144)
{
int rightMargin = Math.Max(
12,
ClientSize.Width -
labelMessage.Right);
int messageImportantGap = Math.Max(
6,
labelImportant.Top -
labelMessage.Bottom);
int importantCheckBoxGap = Math.Max(
6,
checkBoxOption.Top -
labelImportant.Bottom);
int checkBoxButtonGap = Math.Max(
6,
buttonYes.Top -
checkBoxOption.Bottom);
int buttonGap = Math.Max(
6,
buttonNo.Left -
buttonYes.Right);
int bottomMargin = Math.Max(
10,
ClientSize.Height -
buttonNo.Bottom);
int checkBoxTextWidth =
TextRenderer.MeasureText(
_checkBoxText ?? string.Empty,
checkBoxOption.Font,
System.Drawing.Size.Empty,
TextFormatFlags.SingleLine |
TextFormatFlags.NoPadding).Width;
int requiredClientWidth = Math.Max(
ClientSize.Width,
checkBoxOption.Left +
checkBoxTextWidth +
rightMargin +
24);
if (requiredClientWidth >
ClientSize.Width)
{
ClientSize =
new System.Drawing.Size(
requiredClientWidth,
ClientSize.Height);
}
int textWidth = Math.Max(
1,
ClientSize.Width -
labelMessage.Left -
rightMargin);
labelMessage.Width = textWidth;
labelImportant.Width = textWidth;
int messageHeight =
TextRenderer.MeasureText(
_messageText ?? string.Empty,
labelMessage.Font,
new System.Drawing.Size(
textWidth,
int.MaxValue),
TextFormatFlags.WordBreak |
TextFormatFlags.NoPadding).Height;
labelMessage.Height = Math.Max(
labelMessage.Height,
messageHeight + 8);
labelImportant.Top =
labelMessage.Bottom +
messageImportantGap;
int importantHeight =
TextRenderer.MeasureText(
_importantText ?? string.Empty,
labelImportant.Font,
new System.Drawing.Size(
textWidth,
int.MaxValue),
TextFormatFlags.WordBreak |
TextFormatFlags.NoPadding).Height;
labelImportant.Height = Math.Max(
labelImportant.Height,
importantHeight + 8);
checkBoxOption.Top =
labelImportant.Bottom +
importantCheckBoxGap;
checkBoxOption.Width = Math.Max(
checkBoxOption.Width,
ClientSize.Width -
checkBoxOption.Left -
rightMargin);
buttonYes.Top =
checkBoxOption.Bottom +
checkBoxButtonGap;
buttonNo.Top = buttonYes.Top;
buttonNo.Left =
ClientSize.Width -
rightMargin -
buttonNo.Width;
buttonYes.Left =
buttonNo.Left -
buttonGap -
buttonYes.Width;
ClientSize =
new System.Drawing.Size(
ClientSize.Width,
buttonNo.Bottom +
bottomMargin);
}
PerformLayout();
MinimumSize = Size;
MaximumSize = Size;
Invalidate(true);
Update();
}
finally
{
ResumeLayout(true);
}
}
private void InitializeComponent()
{
AutoScaleMode = AutoScaleMode.Dpi;
AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new System.Drawing.Size(480, 250);
MinimumSize = System.Drawing.Size.Empty;
MaximumSize = System.Drawing.Size.Empty;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
BackColor = AntdThemeService.BackgroundPrimary;
ForeColor = AntdThemeService.TextPrimary;
nativeQuestionIconControl = new NativeQuestionIconControl
{
Name = "nativeQuestionIconControl",
Location = new System.Drawing.Point(24, 37),
Size = new System.Drawing.Size(32, 32)
};
labelMessage = new AntdUI.Label
{
Name = "labelMessage",
Text = _messageText,
Location = new System.Drawing.Point(78, 20),
Size = new System.Drawing.Size(378, 64),
ForeColor = AntdThemeService.TextPrimary,
TextAlign = System.Drawing.ContentAlignment.MiddleLeft
};
labelImportant = new AntdUI.Label
{
Name = "labelImportant",
Text = _importantText,
Location = new System.Drawing.Point(78, 94),
Size = new System.Drawing.Size(378, 64),
TextAlign = System.Drawing.ContentAlignment.MiddleLeft
};
checkBoxOption = new AntdUI.Checkbox
{
Name = "checkBoxOption",
Text = _checkBoxText,
Location = new System.Drawing.Point(24, 170),
Size = new System.Drawing.Size(300, 28),
BackColor = AntdThemeService.BackgroundPrimary,
ForeColor = AntdThemeService.TextPrimary
};
buttonYes = new AntdUI.Button
{
Name = "buttonYes",
Text = _yesButtonText,
Location = new System.Drawing.Point(294, 208),
Size = new System.Drawing.Size(84, 32),
Type = AntdUI.TTypeMini.Default,
DialogResult = DialogResult.Yes
};
buttonNo = new AntdUI.Button
{
Name = "buttonNo",
Text = _noButtonText,
Location = new System.Drawing.Point(386, 208),
Size = new System.Drawing.Size(84, 32),
Type = AntdUI.TTypeMini.Primary,
DialogResult = DialogResult.No
};
Controls.Add(nativeQuestionIconControl);
Controls.Add(labelMessage);
Controls.Add(labelImportant);
Controls.Add(checkBoxOption);
Controls.Add(buttonYes);
Controls.Add(buttonNo);
AcceptButton = buttonYes;
CancelButton = buttonNo;
}
private void ApplyThemeColors()
{
BackColor = AntdThemeService.BackgroundPrimary;
ForeColor = AntdThemeService.TextPrimary;
labelMessage.ForeColor = AntdThemeService.TextPrimary;
checkBoxOption.BackColor = AntdThemeService.BackgroundPrimary;
checkBoxOption.ForeColor = AntdThemeService.TextPrimary;
}
private void ApplyImportantLabelStyle()
{
labelImportant.Font = new System.Drawing.Font(
labelImportant.Font.FontFamily,
labelImportant.Font.Size,
System.Drawing.FontStyle.Bold);
labelImportant.ForeColor =
BackColor.GetBrightness() < 0.5f
? System.Drawing.Color.Gold
: System.Drawing.Color.Red;
}
}
// Draws the native Windows warning icon without external image assets.
private sealed class NativeWarningIconControl : Control
{
public NativeWarningIconControl()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null)
{
using System.Drawing.SolidBrush backgroundBrush =
new System.Drawing.SolidBrush(Parent.BackColor);
e.Graphics.FillRectangle(
backgroundBrush,
ClientRectangle);
}
IntPtr warningIconHandle =
LoadIcon(
IntPtr.Zero,
new IntPtr(IDI_WARNING));
if (warningIconHandle == IntPtr.Zero)
return;
IntPtr hdc = e.Graphics.GetHdc();
try
{
DrawIconEx(
hdc,
0,
0,
warningIconHandle,
32,
32,
0,
IntPtr.Zero,
DI_NORMAL);
}
finally
{
e.Graphics.ReleaseHdc(hdc);
}
}
}
// Draws the native Windows question icon without external image assets.
private sealed class NativeQuestionIconControl : Control
{
public NativeQuestionIconControl()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null)
{
using System.Drawing.SolidBrush backgroundBrush = new System.Drawing.SolidBrush(Parent.BackColor);
e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
}
IntPtr questionIconHandle = LoadIcon(IntPtr.Zero, new IntPtr(IDI_QUESTION));
if (questionIconHandle == IntPtr.Zero)
return;
IntPtr hdc = e.Graphics.GetHdc();
try
{
DrawIconEx(
hdc,
0,
0,
questionIconHandle,
32,
32,
0,
IntPtr.Zero,
DI_NORMAL);
}
finally
{
e.Graphics.ReleaseHdc(hdc);
}
}
}
}
}