Skip to content

Commit

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

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

[JSImport($"{JsType}.pickSingleFolderAsync")]
internal static partial Task<string> PickSingleFolderAsync(string id, string startIn);
}
}
}
#endif
#endif
12 changes: 11 additions & 1 deletion src/Uno.UWP/Storage/Pickers/FolderPicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ namespace Windows.Storage.Pickers
{
public partial class FolderPicker
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.Storage.Pickers.FolderPicker";
#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 All @@ -38,10 +44,14 @@ internal static bool IsNativePickerSupported()
throw new NotSupportedException("Could not handle the request using any picker implementation.");
}

var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier);
var startIn = SuggestedStartLocation.ToStartInDirectory();

#if NET7_0_OR_GREATER
var pickedFolderJson = await NativeMethods.PickSingleFolderAsync(SettingsIdentifier, startIn);
#else
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier);
var pickedFolderJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickSingleFolderAsync('{id}','{startIn}')");
#endif

if (pickedFolderJson is null)
{
Expand Down

0 comments on commit 1a2e00f

Please sign in to comment.