diff --git a/Samples/Samples/ViewModel/FilePickerViewModel.cs b/Samples/Samples/ViewModel/FilePickerViewModel.cs index dd3653621..f9fda69d0 100644 --- a/Samples/Samples/ViewModel/FilePickerViewModel.cs +++ b/Samples/Samples/ViewModel/FilePickerViewModel.cs @@ -77,6 +77,7 @@ async void DoPickCustomType() { DevicePlatform.Android, new[] { "application/comics" } }, { DevicePlatform.UWP, new[] { ".cbr", ".cbz" } }, { DevicePlatform.Tizen, new[] { "*/*" } }, + { DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // or general UTType values }); var options = new PickOptions diff --git a/Xamarin.Essentials/FilePicker/FilePicker.macos.cs b/Xamarin.Essentials/FilePicker/FilePicker.macos.cs new file mode 100644 index 000000000..2b3fabd64 --- /dev/null +++ b/Xamarin.Essentials/FilePicker/FilePicker.macos.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using AppKit; +using MobileCoreServices; + +namespace Xamarin.Essentials +{ + public static partial class FilePicker + { + static Task PlatformPickFileAsync(PickOptions options) + { + var openPanel = new NSOpenPanel + { + CanChooseFiles = true, + AllowsMultipleSelection = false, + CanChooseDirectories = false + }; + + if (options.PickerTitle != null) + openPanel.Title = options.PickerTitle; + + SetFileTypes(options, openPanel); + + FilePickerResult result = null; + var panelResult = openPanel.RunModal(); + if (panelResult == (nint)(long)NSModalResponse.OK) + { + result = new FilePickerResult(openPanel.Urls[0].Path); + } + + return Task.FromResult(result); + } + + static Task> PlatformPickMultipleFilesAsync(PickOptions options) + { + var openPanel = new NSOpenPanel + { + CanChooseFiles = true, + AllowsMultipleSelection = true, + CanChooseDirectories = false + }; + + if (options.PickerTitle != null) + openPanel.Title = options.PickerTitle; + + SetFileTypes(options, openPanel); + + var resultList = new List(); + var panelResult = openPanel.RunModal(); + if (panelResult == (nint)(long)NSModalResponse.OK) + { + foreach (var url in openPanel.Urls) + { + resultList.Add(new FilePickerResult(url.Path)); + } + } + + return Task.FromResult>(resultList); + } + + static void SetFileTypes(PickOptions options, NSOpenPanel panel) + { + var allowedFileTypes = new List(); + + if (options?.FileTypes?.Value != null) + { + foreach (var type in options.FileTypes.Value) + { + allowedFileTypes.Add(type.TrimStart('*', '.')); + } + } + + panel.AllowedFileTypes = allowedFileTypes.ToArray(); + } + } + + public partial class FilePickerFileType + { + public static FilePickerFileType PlatformImageFileType() => + new FilePickerFileType(new Dictionary> + { + { DevicePlatform.macOS, new string[] { UTType.PNG, UTType.JPEG, "jpeg" } } + }); + + public static FilePickerFileType PlatformPngFileType() => + new FilePickerFileType(new Dictionary> + { + { DevicePlatform.macOS, new string[] { UTType.PNG } } + }); + } + + public partial class FilePickerResult + { + internal FilePickerResult(string filePath) + : base(filePath) + { + FileName = Path.GetFileName(filePath); + } + + Task PlatformOpenReadStreamAsync() + => Task.FromResult(File.OpenRead(FullPath)); + } +}