-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFileDialogs.cs
More file actions
646 lines (610 loc) · 22.5 KB
/
FileDialogs.cs
File metadata and controls
646 lines (610 loc) · 22.5 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
using BytecodeApi.Extensions;
using BytecodeApi.IO;
using Microsoft.Win32;
using System.Windows;
namespace BytecodeApi.Wpf.Dialogs;
/// <summary>
/// Class to display UI dialogs, such as Open and Save for files and directories.
/// </summary>
public static class FileDialogs
{
/// <summary>
/// Creates an open file dialog.
/// </summary>
/// <returns>
/// A <see cref="OpenFileDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static OpenFileDialogBuilder Open()
{
return new();
}
/// <summary>
/// Creates an open file dialog that opens multiple files.
/// </summary>
/// <returns>
/// A <see cref="OpenMultipleFilesDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static OpenMultipleFilesDialogBuilder OpenMultiple()
{
return new();
}
/// <summary>
/// Creates an open folder dialog.
/// </summary>
/// <returns>
/// A <see cref="OpenFolderDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static OpenFolderDialogBuilder OpenFolder()
{
return new();
}
/// <summary>
/// Creates an open folder dialog that opens multiple folders.
/// </summary>
/// <returns>
/// A <see cref="OpenMultipleFoldersDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static OpenMultipleFoldersDialogBuilder OpenMultipleFolders()
{
return new();
}
/// <summary>
/// Creates an icon selection dialog.
/// </summary>
/// <returns>
/// A <see cref="SelectIconDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static SelectIconDialogBuilder SelectIcon()
{
return new();
}
/// <summary>
/// Creates a save file dialog.
/// </summary>
/// <returns>
/// A <see cref="SaveFileDialogBuilder" /> that can be used to configure and display the dialog.
/// </returns>
public static SaveFileDialogBuilder Save()
{
return new();
}
private static string GetFilter(IEnumerable<DialogFileType> fileTypes)
{
return fileTypes.Any()
? fileTypes.Select(fileType => GetFilter(fileType.Extensions, fileType.Description)).AsString("|")
: GetFilter(null, null);
}
private static string GetFilter(string?[]? extensions, string? description)
{
if (extensions.IsNullOrEmpty())
{
return $"{description ?? "All Files"}|*.*";
}
else
{
string[] descriptions = description != null
? [description]
: extensions.Select(extension => new FileExtensionInfo(extension ?? "").FriendlyDocName).ExceptNull().Distinct().ToArray();
return $"{(descriptions.Length == 1 ? descriptions.First() : "Miscellaneous Files")}|{extensions.Select(extension => $"*.{NormalizeExtension(extension)}").AsString(";")}";
}
}
private static string? NormalizeExtension(string? extension)
{
return extension.ToNullIfEmptyOrWhiteSpace()?.Trim().TrimStart('.').ToLower();
}
private static bool ShowDialog(CommonDialog dialog, Window? owner)
{
if (owner != null)
{
return dialog.ShowDialog(owner) == true;
}
else
{
return dialog.ShowDialog() == true;
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying an open file dialog.
/// </summary>
public sealed class OpenFileDialogBuilder
{
private Window? _Owner;
private readonly List<DialogFileType> _FileTypes;
private string? _InitialDirectory;
internal OpenFileDialogBuilder()
{
_FileTypes = [];
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFileDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Specifies the file extensions that are allowed to be opened.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be opened.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFileDialogBuilder FileType(params string[] extensions)
{
return FileType(extensions, null);
}
/// <summary>
/// Specifies the file extensions that are allowed to be opened.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be opened.</param>
/// <param name="description">The description to be used. If set to <see langword="null" />, the description is retrieved automatically from the shell.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFileDialogBuilder FileType(string[] extensions, string? description)
{
Check.ArgumentNull(extensions);
Check.ArgumentEx.ArrayElementsRequired(extensions);
Check.ArgumentEx.ArrayValuesNotNull(extensions);
Check.ArgumentEx.ArrayValuesNotStringEmptyOrWhiteSpace(extensions);
_FileTypes.Add(new(extensions.Select(extension => NormalizeExtension(extension)!).ToArray(), description));
return this;
}
/// <summary>
/// Sets the initial directory for the dialog. If set to <see langword="null" />, the dialog will open in the last used directory or a default directory determined by the system.
/// </summary>
/// <param name="initialDirectory">A <see cref="string" /> specifying the initial directory for the dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFileDialogBuilder InitialDirectory(string? initialDirectory)
{
_InitialDirectory = initialDirectory;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" /> and the selected file name is returned in the <paramref name="fileName" /> parameter.
/// </summary>
/// <param name="fileName">When this method returns, contains the selected file name if the user clicked the OK button; otherwise, <see langword="null" />.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show([NotNullWhen(true)] out string? fileName)
{
OpenFileDialog dialog = new()
{
Filter = GetFilter(_FileTypes),
InitialDirectory = _InitialDirectory ?? ""
};
if (ShowDialog(dialog, _Owner))
{
fileName = dialog.FileName;
return true;
}
else
{
fileName = null;
return false;
}
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying an open file dialog that opens multiple files.
/// </summary>
public sealed class OpenMultipleFilesDialogBuilder
{
private Window? _Owner;
private readonly List<DialogFileType> _FileTypes;
private string? _InitialDirectory;
internal OpenMultipleFilesDialogBuilder()
{
_FileTypes = [];
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFilesDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Specifies the file extensions that are allowed to be opened.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be opened.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFilesDialogBuilder FileType(params string[] extensions)
{
return FileType(extensions, null);
}
/// <summary>
/// Specifies the file extensions that are allowed to be opened.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be opened.</param>
/// <param name="description">The description to be used. If set to <see langword="null" />, the description is retrieved automatically from the shell.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFilesDialogBuilder FileType(string[] extensions, string? description)
{
Check.ArgumentNull(extensions);
Check.ArgumentEx.ArrayElementsRequired(extensions);
Check.ArgumentEx.ArrayValuesNotNull(extensions);
Check.ArgumentEx.ArrayValuesNotStringEmptyOrWhiteSpace(extensions);
_FileTypes.Add(new(extensions.Select(extension => NormalizeExtension(extension)!).ToArray(), description));
return this;
}
/// <summary>
/// Sets the initial directory for the dialog. If set to <see langword="null" />, the dialog will open in the last used directory or a default directory determined by the system.
/// </summary>
/// <param name="initialDirectory">A <see cref="string" /> specifying the initial directory for the dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFilesDialogBuilder InitialDirectory(string? initialDirectory)
{
_InitialDirectory = initialDirectory;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" /> and the selected file names are returned in the <paramref name="fileNames" /> parameter.
/// </summary>
/// <param name="fileNames">When this method returns, contains the selected file names if the user clicked the OK button; otherwise, an empty array.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show(out string[] fileNames)
{
OpenFileDialog dialog = new()
{
Filter = GetFilter(_FileTypes),
Multiselect = true,
InitialDirectory = _InitialDirectory ?? ""
};
if (ShowDialog(dialog, _Owner))
{
fileNames = dialog.FileNames;
return true;
}
else
{
fileNames = [];
return false;
}
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying an open folder dialog.
/// </summary>
public sealed class OpenFolderDialogBuilder
{
private Window? _Owner;
private string? _InitialDirectory;
internal OpenFolderDialogBuilder()
{
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFolderDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Sets the initial directory for the dialog. If set to <see langword="null" />, the dialog will open in the last used directory or a default directory determined by the system.
/// </summary>
/// <param name="initialDirectory">A <see cref="string" /> specifying the initial directory for the dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenFolderDialogBuilder InitialDirectory(string? initialDirectory)
{
_InitialDirectory = initialDirectory;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" /> and the selected folder path is returned in the <paramref name="path" /> parameter.
/// </summary>
/// <param name="path">When this method returns, contains the selected folder path if the user clicked the OK button; otherwise, <see langword="null" />.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show([NotNullWhen(true)] out string? path)
{
OpenFolderDialog dialog = new()
{
InitialDirectory = _InitialDirectory ?? ""
};
if (ShowDialog(dialog, _Owner))
{
path = dialog.FolderName;
return true;
}
else
{
path = null;
return false;
}
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying an open folder dialog that opens multiple folders.
/// </summary>
public sealed class OpenMultipleFoldersDialogBuilder
{
private Window? _Owner;
private string? _InitialDirectory;
internal OpenMultipleFoldersDialogBuilder()
{
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFoldersDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Sets the initial directory for the dialog. If set to <see langword="null" />, the dialog will open in the last used directory or a default directory determined by the system.
/// </summary>
/// <param name="initialDirectory">A <see cref="string" /> specifying the initial directory for the dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public OpenMultipleFoldersDialogBuilder InitialDirectory(string? initialDirectory)
{
_InitialDirectory = initialDirectory;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" /> and the selected folder paths are returned in the <paramref name="paths" /> parameter.
/// </summary>
/// <param name="paths">When this method returns, contains the selected folder paths if the user clicked the OK button; otherwise, an empty array.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show(out string[] paths)
{
OpenFolderDialog dialog = new()
{
Multiselect = true,
InitialDirectory = _InitialDirectory ?? ""
};
if (ShowDialog(dialog, _Owner))
{
paths = dialog.FolderNames;
return true;
}
else
{
paths = [];
return false;
}
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying an icon selection dialog.
/// </summary>
public sealed class SelectIconDialogBuilder
{
private Window? _Owner;
private string? _FileName;
internal SelectIconDialogBuilder()
{
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SelectIconDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Sets the initial filename of the icon selection dialog.
/// </summary>
/// <param name="fileName">The initial filename of the icon selection dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SelectIconDialogBuilder FileName(string? fileName)
{
_FileName = fileName;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" />, the selected file name is returned in the <paramref name="fileName" /> parameter, and the selected icon index is returned in the <paramref name="index" /> parameter.
/// </summary>
/// <param name="fileName">When this method returns, contains the selected file name if the user clicked the OK button; otherwise, <see langword="null" />.</param>
/// <param name="index">When this method returns, contains the selected icon index if the user clicked the OK button; otherwise, 0.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show([NotNullWhen(true)] out string? fileName, out int index)
{
IconPickerDialog dialog = new()
{
FileName = _FileName
};
if (ShowDialog(dialog, _Owner) && dialog.FileName != null)
{
fileName = dialog.FileName;
index = dialog.IconIndex;
return true;
}
else
{
fileName = null;
index = 0;
return false;
}
}
}
/// <summary>
/// Provides a fluent builder for configuring and displaying a save file dialog.
/// </summary>
public sealed class SaveFileDialogBuilder
{
private Window? _Owner;
private string? _FileName;
private readonly List<DialogFileType> _FileTypes;
private string? _InitialDirectory;
internal SaveFileDialogBuilder()
{
_FileTypes = [];
}
/// <summary>
/// Sets the owner of the dialog.
/// </summary>
/// <param name="owner">A <see cref="Window" /> to use as the owner of the dialog, or <see langword="null" /> to not specify an owner.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SaveFileDialogBuilder Owner(Window? owner)
{
_Owner = owner;
return this;
}
/// <summary>
/// Sets the initial filename of the save file dialog.
/// </summary>
/// <param name="fileName">The initial filename of the save file dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SaveFileDialogBuilder FileName(string? fileName)
{
_FileName = fileName;
return this;
}
/// <summary>
/// Specifies the file extensions that are allowed to be saved.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// If no extension is specified, the extension of the initial file name is used.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be saved.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SaveFileDialogBuilder FileType(params string[] extensions)
{
return FileType(extensions, null);
}
/// <summary>
/// Specifies the file extensions that are allowed to be saved.
/// This method can be called multiple times to specify multiple sets of extensions to choose from.
/// If no extension is specified, the extension of the initial file name is used.
/// </summary>
/// <param name="extensions">The extensions that are allowed to be saved.</param>
/// <param name="description">The description to be used. If set to <see langword="null" />, the description is retrieved automatically from the shell.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SaveFileDialogBuilder FileType(string[] extensions, string? description)
{
Check.ArgumentNull(extensions);
Check.ArgumentEx.ArrayElementsRequired(extensions);
Check.ArgumentEx.ArrayValuesNotNull(extensions);
Check.ArgumentEx.ArrayValuesNotStringEmptyOrWhiteSpace(extensions);
_FileTypes.Add(new(extensions.Select(extension => NormalizeExtension(extension)!).ToArray(), description));
return this;
}
/// <summary>
/// Sets the initial directory for the dialog. If set to <see langword="null" />, the dialog will open in the last used directory or a default directory determined by the system.
/// </summary>
/// <param name="initialDirectory">A <see cref="string" /> specifying the initial directory for the dialog.</param>
/// <returns>
/// A reference to this instance after the operation has completed.
/// </returns>
public SaveFileDialogBuilder InitialDirectory(string? initialDirectory)
{
_InitialDirectory = initialDirectory;
return this;
}
/// <summary>
/// Displays the dialog. If the user clicks the OK button, this method returns <see langword="true" /> and the selected file name is returned in the <paramref name="fileName" /> parameter.
/// </summary>
/// <param name="fileName">When this method returns, contains the selected file name if the user clicked the OK button; otherwise, <see langword="null" />.</param>
/// <returns>
/// <see langword="true" />, if the user clicked the OK button;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Show([NotNullWhen(true)] out string? fileName)
{
DialogFileType[] fileTypes = _FileTypes.Any()
? _FileTypes.ToArray()
: NormalizeExtension(Path.GetExtension(_FileName)) is string fileNameExtension
? [new([fileNameExtension], null)]
: [];
SaveFileDialog dialog = new()
{
FileName = _FileName ?? "",
Filter = GetFilter(fileTypes),
InitialDirectory = _InitialDirectory ?? ""
};
if (ShowDialog(dialog, _Owner))
{
string result = dialog.FileName;
string[] allExtensions = fileTypes.SelectMany(fileType => fileType.Extensions).ToArray();
string? selectedExtension = dialog.FilterIndex > 0 && dialog.FilterIndex <= fileTypes.Length ? fileTypes[dialog.FilterIndex - 1].Extensions.First() : null;
if (selectedExtension != null &&
allExtensions.Any() &&
allExtensions.None(extension => result.EndsWith($".{extension}", StringComparison.OrdinalIgnoreCase)))
{
result = $"{result.TrimEnd('.')}.{selectedExtension}";
}
fileName = result;
return true;
}
else
{
fileName = null;
return false;
}
}
}
private sealed class DialogFileType
{
public string[] Extensions { get; }
public string? Description { get; }
public DialogFileType(string[] extensions, string? description)
{
Extensions = extensions;
Description = description;
}
}
}