Skip to content

Commit

Permalink
perf: Add FileSavePicker bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed May 10, 2023
1 parent 2482244 commit 7dbddbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/Uno.UWP/Storage/Pickers/FileSavePicker.Interop.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ internal partial class FileSavePicker
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Windows.Storage.Pickers.FileSavePicker";

[JSImport($"{JsType}.isNativeSupported")]
internal static partial bool IsNativeSupported();

[JSImport($"{JsType}.nativePickSaveFileAsync")]
internal static partial Task<string> PickSaveFileAsync(bool showAll, string fileTypeMap, string suggestedFileName, string id, string startIn);
}
}
}
#endif
#endif
15 changes: 12 additions & 3 deletions src/Uno.UWP/Storage/Pickers/FileSavePicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ namespace Windows.Storage.Pickers
{
public partial class FileSavePicker
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.Storage.Pickers.FileSavePicker";
#endif

private static bool? _fileSystemAccessApiSupported;

internal static bool IsNativePickerSupported()
{
if (_fileSystemAccessApiSupported is null)
{
#if NET7_0_OR_GREATER
_fileSystemAccessApiSupported = NativeMethods.IsNativeSupported();
#else
var isSupportedString = WebAssemblyRuntime.InvokeJS($"{JsType}.isNativeSupported()");
_fileSystemAccessApiSupported = bool.TryParse(isSupportedString, out var isSupported) && isSupported;
#endif
}

return _fileSystemAccessApiSupported.Value;
Expand Down Expand Up @@ -58,16 +64,19 @@ internal static bool IsNativePickerSupported()

private async Task<StorageFile?> NativePickerPickSaveFileAsync(CancellationToken token)
{
var showAllEntryParameter = "true";
var fileTypeMapParameter = JsonHelper.Serialize(BuildFileTypesMap());
var startIn = SuggestedStartLocation.ToStartInDirectory();

#if NET7_0_OR_GREATER
var nativeStorageItemInfo = await NativeMethods.PickSaveFileAsync(true, fileTypeMapParameter, SuggestedFileName, SettingsIdentifier, startIn);
#else
var suggestedFileName = SuggestedFileName != "" ? WebAssemblyRuntime.EscapeJs(SuggestedFileName) : "";

var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier);

var startIn = SuggestedStartLocation.ToStartInDirectory();
var promise = $"{JsType}.nativePickSaveFileAsync({showAllEntryParameter},'{WebAssemblyRuntime.EscapeJs(fileTypeMapParameter)}','{suggestedFileName}','{id}','{startIn}')";
var promise = $"{JsType}.nativePickSaveFileAsync(true,'{WebAssemblyRuntime.EscapeJs(fileTypeMapParameter)}','{suggestedFileName}','{id}','{startIn}')";
var nativeStorageItemInfo = await WebAssemblyRuntime.InvokeAsync(promise);
#endif
if (nativeStorageItemInfo is null)
{
return null;
Expand Down

0 comments on commit 7dbddbc

Please sign in to comment.