Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Samples/Samples/ViewModel/FilePickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 105 additions & 0 deletions Xamarin.Essentials/FilePicker/FilePicker.macos.cs
Original file line number Diff line number Diff line change
@@ -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<FilePickerResult> 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<IEnumerable<FilePickerResult>> 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<FilePickerResult>();
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<IEnumerable<FilePickerResult>>(resultList);
}

static void SetFileTypes(PickOptions options, NSOpenPanel panel)
{
var allowedFileTypes = new List<string>();

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, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.PNG, UTType.JPEG, "jpeg" } }
});

public static FilePickerFileType PlatformPngFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.PNG } }
});
}

public partial class FilePickerResult
{
internal FilePickerResult(string filePath)
: base(filePath)
{
FileName = Path.GetFileName(filePath);
}

Task<Stream> PlatformOpenReadStreamAsync()
=> Task.FromResult<Stream>(File.OpenRead(FullPath));
}
}