-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented FileSavePicker on MacOS
- Loading branch information
Showing
5 changed files
with
110 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
||
} | ||
|
||
} |