Skip to content

Commit 0048e4b

Browse files
committed
feat: Support for SettingsIdentifier, SuggestedStartLocation and SuggestedFileName on WASM
1 parent 60d4a34 commit 0048e4b

File tree

11 files changed

+94
-17
lines changed

11 files changed

+94
-17
lines changed

src/SamplesApp/UITests.Shared/Windows_Storage/Pickers/FileSavePickerTests.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<TextBox Header="Settings identifier" Text="{x:Bind ViewModel.SettingsIdentifier, Mode=TwoWay}" />
2424
<TextBox Header="Commit button text" Text="{x:Bind ViewModel.CommitButtonText, Mode=TwoWay}" />
2525

26+
<TextBox Header="Suggested file name" Text="{x:Bind ViewModel.SuggestedFileName, Mode=TwoWay}" />
2627
<StackPanel Orientation="Horizontal" Spacing="4">
2728
<Button Click="{x:Bind ViewModel.PickSuggestedSaveFile}" Content="Pick suggested save file" />
2829
<Button Click="{x:Bind ViewModel.PickSuggestedSaveFile}" Content="Clear" />

src/Uno.UI/WasmScripts/Uno.UI.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,21 +1024,21 @@ declare namespace Windows.Storage {
10241024
declare namespace Windows.Storage.Pickers {
10251025
class FileOpenPicker {
10261026
static isNativeSupported(): boolean;
1027-
static nativePickFilesAsync(multiple: boolean, showAllEntry: boolean, fileTypesJson: string): Promise<string>;
1027+
static nativePickFilesAsync(multiple: boolean, showAllEntry: boolean, fileTypesJson: string, id: string, startIn: StartInDirectory): Promise<string>;
10281028
static uploadPickFilesAsync(multiple: boolean, targetPath: string, accept: string): Promise<string>;
10291029
}
10301030
}
10311031
declare namespace Windows.Storage.Pickers {
10321032
class FileSavePicker {
10331033
static isNativeSupported(): boolean;
1034-
static nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string): Promise<string>;
1034+
static nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string, suggestedFileName: string, id: string, startIn: StartInDirectory): Promise<string>;
10351035
static SaveAs(fileName: string, dataPtr: any, size: number): void;
10361036
}
10371037
}
10381038
declare namespace Windows.Storage.Pickers {
10391039
class FolderPicker {
10401040
static isNativeSupported(): boolean;
1041-
static pickSingleFolderAsync(): Promise<string>;
1041+
static pickSingleFolderAsync(id: string, startIn: StartInDirectory): Promise<string>;
10421042
}
10431043
}
10441044
declare namespace Uno.Storage.Pickers {

src/Uno.UI/WasmScripts/Uno.UI.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,14 +3499,16 @@ var Windows;
34993499
static isNativeSupported() {
35003500
return typeof showOpenFilePicker === "function";
35013501
}
3502-
static async nativePickFilesAsync(multiple, showAllEntry, fileTypesJson) {
3502+
static async nativePickFilesAsync(multiple, showAllEntry, fileTypesJson, id, startIn) {
35033503
if (!FileOpenPicker.isNativeSupported()) {
35043504
return JSON.stringify([]);
35053505
}
35063506
const options = {
35073507
excludeAcceptAllOption: !showAllEntry,
35083508
multiple: multiple,
35093509
types: [],
3510+
id: id,
3511+
startIn: startIn
35103512
};
35113513
const acceptTypes = JSON.parse(fileTypesJson);
35123514
for (const acceptType of acceptTypes) {
@@ -3578,14 +3580,19 @@ var Windows;
35783580
static isNativeSupported() {
35793581
return typeof showSaveFilePicker === "function";
35803582
}
3581-
static async nativePickSaveFileAsync(showAllEntry, fileTypesJson) {
3583+
static async nativePickSaveFileAsync(showAllEntry, fileTypesJson, suggestedFileName, id, startIn) {
35823584
if (!FileSavePicker.isNativeSupported()) {
35833585
return null;
35843586
}
35853587
const options = {
35863588
excludeAcceptAllOption: !showAllEntry,
35873589
types: [],
3590+
id: id,
3591+
startIn: startIn
35883592
};
3593+
if (suggestedFileName != "") {
3594+
options.suggestedName = suggestedFileName;
3595+
}
35893596
const acceptTypes = JSON.parse(fileTypesJson);
35903597
for (const acceptType of acceptTypes) {
35913598
const pickerAcceptType = {
@@ -3637,12 +3644,16 @@ var Windows;
36373644
static isNativeSupported() {
36383645
return typeof showDirectoryPicker === "function";
36393646
}
3640-
static async pickSingleFolderAsync() {
3647+
static async pickSingleFolderAsync(id, startIn) {
36413648
if (!FolderPicker.isNativeSupported()) {
36423649
return null;
36433650
}
36443651
try {
3645-
const selectedFolder = await showDirectoryPicker();
3652+
const options = {
3653+
id: id,
3654+
startIn: startIn
3655+
};
3656+
const selectedFolder = await showDirectoryPicker(options);
36463657
const info = Uno.Storage.NativeStorageItem.getInfos(selectedFolder)[0];
36473658
return JSON.stringify(info);
36483659
}

src/Uno.UI/ts/Windows/Storage/Pickers/FileOpenPicker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
public static async nativePickFilesAsync(
99
multiple: boolean,
1010
showAllEntry: boolean,
11-
fileTypesJson: string): Promise<string> {
11+
fileTypesJson: string,
12+
id: string,
13+
startIn: StartInDirectory
14+
): Promise<string> {
1215

1316
if (!FileOpenPicker.isNativeSupported()) {
1417
return JSON.stringify([]);
@@ -18,6 +21,8 @@
1821
excludeAcceptAllOption: !showAllEntry,
1922
multiple: multiple,
2023
types: [],
24+
id: id,
25+
startIn: startIn
2126
};
2227

2328
const acceptTypes = <Uno.Storage.Pickers.NativeFilePickerAcceptType[]>JSON.parse(fileTypesJson);

src/Uno.UI/ts/Windows/Storage/Pickers/FileSavePicker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
return typeof showSaveFilePicker === "function";
66
}
77

8-
public static async nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string): Promise<string> {
8+
public static async nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string, suggestedFileName: string, id: string, startIn: StartInDirectory): Promise<string> {
99

1010
if (!FileSavePicker.isNativeSupported()) {
1111
return null;
@@ -14,8 +14,14 @@
1414
const options: SaveFilePickerOptions = {
1515
excludeAcceptAllOption: !showAllEntry,
1616
types: [],
17+
id: id,
18+
startIn: startIn
1719
};
1820

21+
if (suggestedFileName != "") {
22+
options.suggestedName = suggestedFileName;
23+
}
24+
1925
const acceptTypes = <Uno.Storage.Pickers.NativeFilePickerAcceptType[]>JSON.parse(fileTypesJson);
2026

2127
for (const acceptType of acceptTypes) {

src/Uno.UI/ts/Windows/Storage/Pickers/FolderPicker.Native.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
return typeof showDirectoryPicker === "function";
66
}
77

8-
public static async pickSingleFolderAsync(): Promise<string> {
8+
public static async pickSingleFolderAsync(id: string, startIn: StartInDirectory): Promise<string> {
99
if (!FolderPicker.isNativeSupported()) {
1010
return null;
1111
}
1212

1313
try {
14-
const selectedFolder = await showDirectoryPicker();
14+
const options: DirectoryPickerOptions = {
15+
id: id,
16+
startIn: startIn
17+
};
18+
19+
const selectedFolder = await showDirectoryPicker(options);
1520

1621
const info = Uno.Storage.NativeStorageItem.getInfos(selectedFolder)[0];
1722
return JSON.stringify(info);

src/Uno.UI/ts/types/wicg-file-system-access/index.d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ declare class BaseFileSystemHandle {
2828
}
2929

3030
declare global {
31+
32+
type WellKnownDirectory =
33+
"desktop" |
34+
"documents" |
35+
"downloads" |
36+
"music" |
37+
"pictures" |
38+
"videos";
39+
40+
type StartInDirectory = WellKnownDirectory | FileSystemHandle;
41+
3142
interface FilePickerAcceptType {
3243
description?: string;
3344
accept: Record<string, string | string[]>;
@@ -36,17 +47,24 @@ declare global {
3647
interface FilePickerOptions {
3748
types?: FilePickerAcceptType[];
3849
excludeAcceptAllOption?: boolean;
50+
id?: string;
51+
startIn?: StartInDirectory;
3952
}
4053

4154
interface OpenFilePickerOptions extends FilePickerOptions {
4255
multiple?: boolean;
4356
}
4457

4558
// tslint:disable-next-line:no-empty-interface
46-
interface SaveFilePickerOptions extends FilePickerOptions { }
59+
interface SaveFilePickerOptions extends FilePickerOptions {
60+
suggestedName?: string;
61+
}
4762

4863
// tslint:disable-next-line:no-empty-interface
49-
interface DirectoryPickerOptions { }
64+
interface DirectoryPickerOptions {
65+
id?: string;
66+
startIn?: StartInDirectory;
67+
}
5068

5169
type FileSystemPermissionMode = 'read' | 'readwrite';
5270

src/Uno.UWP/Storage/Pickers/FileOpenPicker.wasm.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Uno.Foundation;
1111
using Uno.Helpers.Serialization;
1212
using Uno.Storage.Internal;
13+
using Uno.Storage.Pickers;
1314
using Uno.Storage.Pickers.Internal;
1415

1516
namespace Windows.Storage.Pickers
@@ -62,8 +63,9 @@ private async Task<FilePickerSelectedFilesArray> NativePickerPickFilesAsync(bool
6263
var fileTypeAcceptTypes = BuildFileTypesMap();
6364
var fileTypeAcceptTypesJson = JsonHelper.Serialize(fileTypeAcceptTypes);
6465
var fileTypeMapParameter = WebAssemblyRuntime.EscapeJs(fileTypeAcceptTypesJson);
65-
66-
var nativeStorageItemInfosJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.nativePickFilesAsync({multipleParameter},{showAllEntryParameter},'{fileTypeMapParameter}')");
66+
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier ?? "");
67+
var startIn = SuggestedStartLocation.ToStartInDirectory();
68+
var nativeStorageItemInfosJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.nativePickFilesAsync({multipleParameter},{showAllEntryParameter},'{fileTypeMapParameter}','{id}','{startIn}')");
6769
var infos = JsonHelper.Deserialize<NativeStorageItemInfo[]>(nativeStorageItemInfosJson);
6870

6971
var results = new List<StorageFile>();

src/Uno.UWP/Storage/Pickers/FileSavePicker.wasm.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Uno.Foundation;
1111
using Uno.Helpers.Serialization;
1212
using Uno.Storage.Internal;
13+
using Uno.Storage.Pickers;
1314
using Uno.Storage.Pickers.Internal;
1415

1516
namespace Windows.Storage.Pickers
@@ -49,7 +50,12 @@ private bool IsNativePickerSupported()
4950
var showAllEntryParameter = "true";
5051
var fileTypeMapParameter = JsonHelper.Serialize(BuildFileTypesMap());
5152

52-
var promise = $"{JsType}.nativePickSaveFileAsync({showAllEntryParameter},'{WebAssemblyRuntime.EscapeJs(fileTypeMapParameter)}')";
53+
var suggestedFileName = string.IsNullOrEmpty(SuggestedFileName) ?
54+
"" : WebAssemblyRuntime.EscapeJs(SuggestedFileName);
55+
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier ?? "");
56+
57+
var startIn = SuggestedStartLocation.ToStartInDirectory();
58+
var promise = $"{JsType}.nativePickSaveFileAsync({showAllEntryParameter},'{WebAssemblyRuntime.EscapeJs(fileTypeMapParameter)}','{suggestedFileName}','{id}','{startIn}')";
5359
var nativeStorageItemInfo = await WebAssemblyRuntime.InvokeAsync(promise);
5460
if (nativeStorageItemInfo is null)
5561
{

src/Uno.UWP/Storage/Pickers/FolderPicker.wasm.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Uno.Foundation;
77
using Uno.Helpers.Serialization;
88
using Uno.Storage.Internal;
9+
using Uno.Storage.Pickers;
910

1011
namespace Windows.Storage.Pickers
1112
{
@@ -20,7 +21,10 @@ public partial class FolderPicker
2021
throw new NotSupportedException("Could not handle the request using any picker implementation.");
2122
}
2223

23-
var pickedFolderJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickSingleFolderAsync()");
24+
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier ?? "");
25+
var startIn = SuggestedStartLocation.ToStartInDirectory();
26+
27+
var pickedFolderJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickSingleFolderAsync('{id}','{startIn}')");
2428

2529
if (pickedFolderJson is null)
2630
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Windows.Storage.Pickers;
2+
3+
namespace Uno.Storage.Pickers
4+
{
5+
internal static class PickerLocationIdExtensions
6+
{
7+
internal static string ToStartInDirectory(this PickerLocationId location) =>
8+
location switch
9+
{
10+
PickerLocationId.DocumentsLibrary => "documents",
11+
PickerLocationId.Desktop => "desktop",
12+
PickerLocationId.Downloads => "downloads",
13+
PickerLocationId.MusicLibrary => "music",
14+
PickerLocationId.PicturesLibrary => "pictures",
15+
PickerLocationId.VideosLibrary => "videos",
16+
_ => ""
17+
};
18+
}
19+
}

0 commit comments

Comments
 (0)