Skip to content

Commit

Permalink
feat: Support for SettingsIdentifier, SuggestedStartLocation and Sugg…
Browse files Browse the repository at this point in the history
…estedFileName on WASM
  • Loading branch information
MartinZikmund committed Aug 23, 2021
1 parent 60d4a34 commit 0048e4b
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<TextBox Header="Settings identifier" Text="{x:Bind ViewModel.SettingsIdentifier, Mode=TwoWay}" />
<TextBox Header="Commit button text" Text="{x:Bind ViewModel.CommitButtonText, Mode=TwoWay}" />

<TextBox Header="Suggested file name" Text="{x:Bind ViewModel.SuggestedFileName, Mode=TwoWay}" />
<StackPanel Orientation="Horizontal" Spacing="4">
<Button Click="{x:Bind ViewModel.PickSuggestedSaveFile}" Content="Pick suggested save file" />
<Button Click="{x:Bind ViewModel.PickSuggestedSaveFile}" Content="Clear" />
Expand Down
6 changes: 3 additions & 3 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,21 +1024,21 @@ declare namespace Windows.Storage {
declare namespace Windows.Storage.Pickers {
class FileOpenPicker {
static isNativeSupported(): boolean;
static nativePickFilesAsync(multiple: boolean, showAllEntry: boolean, fileTypesJson: string): Promise<string>;
static nativePickFilesAsync(multiple: boolean, showAllEntry: boolean, fileTypesJson: string, id: string, startIn: StartInDirectory): Promise<string>;
static uploadPickFilesAsync(multiple: boolean, targetPath: string, accept: string): Promise<string>;
}
}
declare namespace Windows.Storage.Pickers {
class FileSavePicker {
static isNativeSupported(): boolean;
static nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string): Promise<string>;
static nativePickSaveFileAsync(showAllEntry: boolean, fileTypesJson: string, suggestedFileName: string, id: string, startIn: StartInDirectory): Promise<string>;
static SaveAs(fileName: string, dataPtr: any, size: number): void;
}
}
declare namespace Windows.Storage.Pickers {
class FolderPicker {
static isNativeSupported(): boolean;
static pickSingleFolderAsync(): Promise<string>;
static pickSingleFolderAsync(id: string, startIn: StartInDirectory): Promise<string>;
}
}
declare namespace Uno.Storage.Pickers {
Expand Down
19 changes: 15 additions & 4 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3499,14 +3499,16 @@ var Windows;
static isNativeSupported() {
return typeof showOpenFilePicker === "function";
}
static async nativePickFilesAsync(multiple, showAllEntry, fileTypesJson) {
static async nativePickFilesAsync(multiple, showAllEntry, fileTypesJson, id, startIn) {
if (!FileOpenPicker.isNativeSupported()) {
return JSON.stringify([]);
}
const options = {
excludeAcceptAllOption: !showAllEntry,
multiple: multiple,
types: [],
id: id,
startIn: startIn
};
const acceptTypes = JSON.parse(fileTypesJson);
for (const acceptType of acceptTypes) {
Expand Down Expand Up @@ -3578,14 +3580,19 @@ var Windows;
static isNativeSupported() {
return typeof showSaveFilePicker === "function";
}
static async nativePickSaveFileAsync(showAllEntry, fileTypesJson) {
static async nativePickSaveFileAsync(showAllEntry, fileTypesJson, suggestedFileName, id, startIn) {
if (!FileSavePicker.isNativeSupported()) {
return null;
}
const options = {
excludeAcceptAllOption: !showAllEntry,
types: [],
id: id,
startIn: startIn
};
if (suggestedFileName != "") {
options.suggestedName = suggestedFileName;
}
const acceptTypes = JSON.parse(fileTypesJson);
for (const acceptType of acceptTypes) {
const pickerAcceptType = {
Expand Down Expand Up @@ -3637,12 +3644,16 @@ var Windows;
static isNativeSupported() {
return typeof showDirectoryPicker === "function";
}
static async pickSingleFolderAsync() {
static async pickSingleFolderAsync(id, startIn) {
if (!FolderPicker.isNativeSupported()) {
return null;
}
try {
const selectedFolder = await showDirectoryPicker();
const options = {
id: id,
startIn: startIn
};
const selectedFolder = await showDirectoryPicker(options);
const info = Uno.Storage.NativeStorageItem.getInfos(selectedFolder)[0];
return JSON.stringify(info);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.UI/ts/Windows/Storage/Pickers/FileOpenPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
public static async nativePickFilesAsync(
multiple: boolean,
showAllEntry: boolean,
fileTypesJson: string): Promise<string> {
fileTypesJson: string,
id: string,
startIn: StartInDirectory
): Promise<string> {

if (!FileOpenPicker.isNativeSupported()) {
return JSON.stringify([]);
Expand All @@ -18,6 +21,8 @@
excludeAcceptAllOption: !showAllEntry,
multiple: multiple,
types: [],
id: id,
startIn: startIn
};

const acceptTypes = <Uno.Storage.Pickers.NativeFilePickerAcceptType[]>JSON.parse(fileTypesJson);
Expand Down
8 changes: 7 additions & 1 deletion src/Uno.UI/ts/Windows/Storage/Pickers/FileSavePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
return typeof showSaveFilePicker === "function";
}

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

if (!FileSavePicker.isNativeSupported()) {
return null;
Expand All @@ -14,8 +14,14 @@
const options: SaveFilePickerOptions = {
excludeAcceptAllOption: !showAllEntry,
types: [],
id: id,
startIn: startIn
};

if (suggestedFileName != "") {
options.suggestedName = suggestedFileName;
}

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

for (const acceptType of acceptTypes) {
Expand Down
9 changes: 7 additions & 2 deletions src/Uno.UI/ts/Windows/Storage/Pickers/FolderPicker.Native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
return typeof showDirectoryPicker === "function";
}

public static async pickSingleFolderAsync(): Promise<string> {
public static async pickSingleFolderAsync(id: string, startIn: StartInDirectory): Promise<string> {
if (!FolderPicker.isNativeSupported()) {
return null;
}

try {
const selectedFolder = await showDirectoryPicker();
const options: DirectoryPickerOptions = {
id: id,
startIn: startIn
};

const selectedFolder = await showDirectoryPicker(options);

const info = Uno.Storage.NativeStorageItem.getInfos(selectedFolder)[0];
return JSON.stringify(info);
Expand Down
22 changes: 20 additions & 2 deletions src/Uno.UI/ts/types/wicg-file-system-access/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ declare class BaseFileSystemHandle {
}

declare global {

type WellKnownDirectory =
"desktop" |
"documents" |
"downloads" |
"music" |
"pictures" |
"videos";

type StartInDirectory = WellKnownDirectory | FileSystemHandle;

interface FilePickerAcceptType {
description?: string;
accept: Record<string, string | string[]>;
Expand All @@ -36,17 +47,24 @@ declare global {
interface FilePickerOptions {
types?: FilePickerAcceptType[];
excludeAcceptAllOption?: boolean;
id?: string;
startIn?: StartInDirectory;
}

interface OpenFilePickerOptions extends FilePickerOptions {
multiple?: boolean;
}

// tslint:disable-next-line:no-empty-interface
interface SaveFilePickerOptions extends FilePickerOptions { }
interface SaveFilePickerOptions extends FilePickerOptions {
suggestedName?: string;
}

// tslint:disable-next-line:no-empty-interface
interface DirectoryPickerOptions { }
interface DirectoryPickerOptions {
id?: string;
startIn?: StartInDirectory;
}

type FileSystemPermissionMode = 'read' | 'readwrite';

Expand Down
6 changes: 4 additions & 2 deletions src/Uno.UWP/Storage/Pickers/FileOpenPicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.Foundation;
using Uno.Helpers.Serialization;
using Uno.Storage.Internal;
using Uno.Storage.Pickers;
using Uno.Storage.Pickers.Internal;

namespace Windows.Storage.Pickers
Expand Down Expand Up @@ -62,8 +63,9 @@ private async Task<FilePickerSelectedFilesArray> NativePickerPickFilesAsync(bool
var fileTypeAcceptTypes = BuildFileTypesMap();
var fileTypeAcceptTypesJson = JsonHelper.Serialize(fileTypeAcceptTypes);
var fileTypeMapParameter = WebAssemblyRuntime.EscapeJs(fileTypeAcceptTypesJson);

var nativeStorageItemInfosJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.nativePickFilesAsync({multipleParameter},{showAllEntryParameter},'{fileTypeMapParameter}')");
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier ?? "");
var startIn = SuggestedStartLocation.ToStartInDirectory();
var nativeStorageItemInfosJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.nativePickFilesAsync({multipleParameter},{showAllEntryParameter},'{fileTypeMapParameter}','{id}','{startIn}')");
var infos = JsonHelper.Deserialize<NativeStorageItemInfo[]>(nativeStorageItemInfosJson);

var results = new List<StorageFile>();
Expand Down
8 changes: 7 additions & 1 deletion src/Uno.UWP/Storage/Pickers/FileSavePicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.Foundation;
using Uno.Helpers.Serialization;
using Uno.Storage.Internal;
using Uno.Storage.Pickers;
using Uno.Storage.Pickers.Internal;

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

var promise = $"{JsType}.nativePickSaveFileAsync({showAllEntryParameter},'{WebAssemblyRuntime.EscapeJs(fileTypeMapParameter)}')";
var suggestedFileName = string.IsNullOrEmpty(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 nativeStorageItemInfo = await WebAssemblyRuntime.InvokeAsync(promise);
if (nativeStorageItemInfo is null)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.UWP/Storage/Pickers/FolderPicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Uno.Foundation;
using Uno.Helpers.Serialization;
using Uno.Storage.Internal;
using Uno.Storage.Pickers;

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

var pickedFolderJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickSingleFolderAsync()");
var id = WebAssemblyRuntime.EscapeJs(SettingsIdentifier ?? "");
var startIn = SuggestedStartLocation.ToStartInDirectory();

var pickedFolderJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.pickSingleFolderAsync('{id}','{startIn}')");

if (pickedFolderJson is null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Windows.Storage.Pickers;

namespace Uno.Storage.Pickers
{
internal static class PickerLocationIdExtensions
{
internal static string ToStartInDirectory(this PickerLocationId location) =>
location switch
{
PickerLocationId.DocumentsLibrary => "documents",
PickerLocationId.Desktop => "desktop",
PickerLocationId.Downloads => "downloads",
PickerLocationId.MusicLibrary => "music",
PickerLocationId.PicturesLibrary => "pictures",
PickerLocationId.VideosLibrary => "videos",
_ => ""
};
}
}

0 comments on commit 0048e4b

Please sign in to comment.