Repository navigation
Expand file tree
/
Copy pathDialogFactory.FileTypes.cs
More file actions
executable file
·83 lines (77 loc) · 4.67 KB
/
Copy pathDialogFactory.FileTypes.cs
File metadata and controls
executable file
·83 lines (77 loc) · 4.67 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
#if MVVMDIALOGS_MAUI
namespace HanumanInstitute.MvvmDialogs.Maui;
#else
namespace HanumanInstitute.MvvmDialogs.Avalonia;
#endif
public partial class DialogFactory
{
/// <summary>
/// Registers custom picker types during application setup. Each extension accepts a MIME type,
/// an Apple type identifier, or both. Omitted fields retain their previous values.
/// </summary>
/// <param name="overrides">Custom entries with or without a leading dot on the extension.</param>
/// <exception cref="ArgumentException">An entry has no extension, uses a wildcard, or supplies neither type.</exception>
public void RegisterFileTypes(params ReadOnlySpan<FileTypeRegistration> overrides)
{
FileTypeRegistration.Validate(overrides);
// Publish a new table once all supplied fields are merged.
var mappings = new Dictionary<string, FilePickerTypes>(_fileTypes, StringComparer.OrdinalIgnoreCase);
foreach (var registration in overrides)
{
var key = registration.NormalizedExtension;
mappings.TryGetValue(key, out var previous);
mappings[key] = new(
string.IsNullOrWhiteSpace(registration.MimeType) ? previous.MimeType : registration.MimeType,
string.IsNullOrWhiteSpace(registration.AppleType) ? previous.AppleType : registration.AppleType);
}
_fileTypes = mappings;
}
private static readonly IReadOnlyDictionary<string, FilePickerTypes> s_defaultFileTypes =
new Dictionary<string, FilePickerTypes>(StringComparer.OrdinalIgnoreCase)
{
["7z"] = new("application/x-7z-compressed", "org.7-zip.7-zip-archive"),
["aac"] = new("audio/aac", "public.aac-audio"),
["avif"] = new("image/avif", "public.avif"),
["bmp"] = new("image/bmp", "com.microsoft.bmp"),
["csv"] = new("text/csv", "public.comma-separated-values-text"),
["doc"] = new("application/msword", "com.microsoft.word.doc"),
["docx"] = new("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "org.openxmlformats.wordprocessingml.document"),
["epub"] = new("application/epub+zip", "org.idpf.epub-container"),
["gif"] = new("image/gif", "com.compuserve.gif"),
["gz"] = new("application/gzip", "org.gnu.gnu-zip-archive"),
["heic"] = new("image/heic", "public.heic"),
["htm"] = new("text/html", "public.html"),
["html"] = new("text/html", "public.html"),
["jpeg"] = new("image/jpeg", "public.jpeg"),
["jpg"] = new("image/jpeg", "public.jpeg"),
["json"] = new("application/json", "public.json"),
["log"] = new("text/plain", "public.log"),
["m4a"] = new("audio/mp4", "com.apple.m4a-audio"),
["md"] = new("text/markdown", "net.daringfireball.markdown"),
["mov"] = new("video/quicktime", "com.apple.quicktime-movie"),
["mp3"] = new("audio/mpeg", "public.mp3"),
["mp4"] = new("video/mp4", "public.mpeg-4"),
["pdf"] = new("application/pdf", "com.adobe.pdf"),
["png"] = new("image/png", "public.png"),
["ppt"] = new("application/vnd.ms-powerpoint", "com.microsoft.powerpoint.ppt"),
["pptx"] = new("application/vnd.openxmlformats-officedocument.presentationml.presentation", "org.openxmlformats.presentationml.presentation"),
["rtf"] = new("application/rtf", "public.rtf"),
["svg"] = new("image/svg+xml", "public.svg-image"),
["tar"] = new("application/x-tar", "public.tar-archive"),
["tif"] = new("image/tiff", "public.tiff"),
["tiff"] = new("image/tiff", "public.tiff"),
["txt"] = new("text/plain", "public.plain-text"),
["wav"] = new("audio/wav", "com.microsoft.waveform-audio"),
["webp"] = new("image/webp", "org.webmproject.webp"),
["xls"] = new("application/vnd.ms-excel", "com.microsoft.excel.xls"),
["xlsx"] = new("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "org.openxmlformats.spreadsheetml.sheet"),
["xml"] = new("application/xml", "public.xml"),
["zip"] = new("application/zip", "public.zip-archive")
};
private IReadOnlyDictionary<string, FilePickerTypes> _fileTypes = s_defaultFileTypes;
// Resolves a picker extension against this factory's configured types.
private FilePickerTypes GetFileType(string extension) =>
_fileTypes.GetValueOrDefault(extension.TrimStart('.'));
// Holds the MIME and Apple types resolved for one extension.
private readonly record struct FilePickerTypes(string? MimeType, string? AppleType);
}