Skip to content

Commit

Permalink
feat: Implemented FileSavePicker on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
snickler committed Oct 3, 2020
1 parent 58597df commit a7b8e80
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Storage.Pickers
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented]
#endif
public partial class FileSavePicker
{
#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__SKIA__", "__NETSTD_REFERENCE__")]
public global::Windows.Storage.Pickers.PickerLocationId SuggestedStartLocation
{
get
Expand Down Expand Up @@ -140,7 +140,7 @@ public string EnterpriseId
}
#endif

#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public FileSavePicker()
{
Expand Down Expand Up @@ -173,7 +173,7 @@ public void PickSaveFileAndContinue()
// Forced skipping of method Windows.Storage.Pickers.FileSavePicker.SuggestedFileName.get
// Forced skipping of method Windows.Storage.Pickers.FileSavePicker.SuggestedFileName.set

#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncOperation<global::Windows.Storage.StorageFile> PickSaveFileAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Storage.Pickers
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public enum PickerLocationId
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
DocumentsLibrary,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
ComputerFolder,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
Desktop,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
Downloads,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
HomeGroup,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
MusicLibrary,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
PicturesLibrary,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
VideosLibrary,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
Objects3D,
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
Unspecified,
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Storage/Pickers/FileSavePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public partial class FileSavePicker

public string CommitButtonText { get; set; }

public IDictionary<string, IList<string>> FileTypeChoices { get; set; }
public IDictionary<string, IList<string>> FileTypeChoices { get; set; } = new Dictionary<string, IList<string>>();
}
}
54 changes: 54 additions & 0 deletions src/Uno.UWP/Storage/Pickers/FileSavePicker.macOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AppKit;
using Foundation;
using Windows.Foundation;

namespace Windows.Storage.Pickers
{
public partial class FileSavePicker
{
public PickerLocationId SuggestedStartLocation { get; set; }
public IAsyncOperation<StorageFile> PickSaveFileAsync() => PickFileTaskAsync().AsAsyncOperation();


private async Task<StorageFile> PickFileTaskAsync()
{
var savePicker = new NSSavePanel();
savePicker.DirectoryUrl = new NSUrl(GetStartPath(), true);
savePicker.AllowedFileTypes = GetFileTypes();
if(SuggestedFileName != null)
{
savePicker.NameFieldStringValue = SuggestedFileName;
}
if (savePicker.RunModal() == 1)
{
return await StorageFile.GetFileFromPathAsync(savePicker.Url.Path);
}
else
{
return null;
}
}

private string GetStartPath()
{
var specialFolder = SuggestedStartLocation switch
{
PickerLocationId.DocumentsLibrary => Environment.SpecialFolder.Personal,
PickerLocationId.ComputerFolder => Environment.SpecialFolder.MyComputer,
PickerLocationId.Desktop => Environment.SpecialFolder.Desktop,
PickerLocationId.MusicLibrary => Environment.SpecialFolder.MyMusic,
PickerLocationId.PicturesLibrary => Environment.SpecialFolder.MyPictures,
PickerLocationId.VideosLibrary => Environment.SpecialFolder.MyVideos,
_ => Environment.SpecialFolder.Personal

};

return Environment.GetFolderPath(specialFolder);
}

private string[] GetFileTypes() => FileTypeChoices.SelectMany(x => x.Value.Select(val=>val.TrimStart(new[] { '.' }))).ToArray();
}
}
39 changes: 39 additions & 0 deletions src/Uno.UWP/Storage/Pickers/PickerLocationId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Windows.Storage.Pickers
{


public enum PickerLocationId
{

DocumentsLibrary,


ComputerFolder,


Desktop,


Downloads,


HomeGroup,


MusicLibrary,


PicturesLibrary,


VideosLibrary,


Objects3D,


Unspecified,

}

}

0 comments on commit a7b8e80

Please sign in to comment.