Skip to content

Commit

Permalink
feat: Add native filesystem folder picker
Browse files Browse the repository at this point in the history
Also adds preliminary implementation of the WASM native version of StorageFolder
  • Loading branch information
Arlodotexe authored and MartinZikmund committed Jan 21, 2021
1 parent c78a700 commit bdc424c
Show file tree
Hide file tree
Showing 17 changed files with 700 additions and 70 deletions.
7 changes: 6 additions & 1 deletion src/Uno.UI-Wasm-only.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
"AddIns\\Uno.UI.Lottie\\Uno.UI.Lottie.Wasm.csproj",
"AddIns\\Uno.UI.MSAL\\Uno.UI.MSAL.Wasm.csproj",
"SamplesApp\\Benchmarks.Shared\\SamplesApp.Benchmarks.shproj",
"SamplesApp\\SamplesApp.Droid\\SamplesApp.Droid.csproj",
"SamplesApp\\SamplesApp.Shared\\SamplesApp.Shared.shproj",
"SamplesApp\\SamplesApp.Skia\\SamplesApp.Skia.csproj",
"SamplesApp\\SamplesApp.UITests.Generator\\Uno.Samples.UITest.Generator.csproj",
"SamplesApp\\SamplesApp.UITests\\SamplesApp.UITests.csproj",
"SamplesApp\\SamplesApp.UWP.Design\\SamplesApp.UWP.Design.csproj",
"SamplesApp\\SamplesApp.UWP\\SamplesApp.UWP.csproj",
"SamplesApp\\SamplesApp.UnitTests.Shared\\SamplesApp.UnitTests.Shared.shproj",
"SamplesApp\\SamplesApp.Wasm\\SamplesApp.Wasm.csproj",
"SamplesApp\\SamplesApp.iOS\\SamplesApp.iOS.csproj",
"SamplesApp\\SamplesApp.macOS\\SamplesApp.macOS.csproj",
"SamplesApp\\UITests.Shared\\UITests.Shared.shproj",
"SolutionTemplate\\Uno.ProjectTemplates.Dotnet\\Uno.ProjectTemplates.Dotnet.csproj",
"SourceGenerators\\System.Xaml\\Uno.Xaml.csproj",
Expand All @@ -36,4 +41,4 @@
"Uno.UWP\\Uno.Wasm.csproj"
]
}
}
}
7 changes: 7 additions & 0 deletions src/Uno.UI/Uno.UI.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
</ProjectReference>
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="ts\types\wicg-file-system-access\index.d.ts" />
<UpToDateCheckInput Remove="ts\Windows\Guid.ts" />
<UpToDateCheckInput Remove="ts\Windows\Storage\Pickers\FolderPicker.ts" />
<UpToDateCheckInput Remove="ts\Windows\Storage\StorageFolder.Native.ts" />
</ItemGroup>

<Target Name="_UnoWasmOverrideNuget" AfterTargets="AfterBuild" DependsOnTargets="BuiltProjectOutputGroup" Condition="'$(UnoNugetOverrideVersion)'!=''">

<PropertyGroup>
Expand Down
31 changes: 31 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ declare namespace Windows.UI.Core {
private static initMethods;
}
}
declare namespace Uno.Utils {
class Guid {
static NewGuid(): string;
}
}
declare namespace Uno.UI {
class HtmlDom {
/**
Expand Down Expand Up @@ -964,6 +969,27 @@ declare namespace Windows.Storage.Pickers {
static SaveAs(fileName: string, dataPtr: any, size: number): void;
}
}
declare namespace Windows.Storage {
class StorageFolderNative {
private static _folderMap;
static AddHandle(guid: string, handle: FileSystemDirectoryHandle): void;
static RemoveHandle(guid: string): void;
static GetHandle(guid: string): FileSystemDirectoryHandle;
/**
* Creates a new folder inside another folder.
* @param parentGuid The GUID of the folder to create in.
* @param folderName The name of the new folder.
*/
static CreateFolderAsync(parentGuid: string, folderName: string): Promise<string>;
/**
* Gets a folder in the given parent folder by name.
* @param parentGuid The GUID of the parent folder to get.
* @param folderName The name of the folder to look for.
* @returns A GUID of the folder if found, other "notfound" literal.
*/
static GetFolderAsync(parentGuid: string, folderName: string): Promise<string>;
}
}
declare namespace Windows.Storage {
class StorageFolder {
private static _isInit;
Expand Down Expand Up @@ -1096,6 +1122,11 @@ declare namespace Windows.Networking.Connectivity {
static networkStatusChanged(): void;
}
}
declare namespace Windows.Storage.Pickers {
class FolderPicker {
static ShowFolderPicker(): Promise<string>;
}
}
interface Navigator {
wakeLock: WakeLock;
}
Expand Down
91 changes: 91 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ var Windows;
})(UI = Windows.UI || (Windows.UI = {}));
})(Windows || (Windows = {}));
var Uno;
(function (Uno) {
var Utils;
(function (Utils) {
class Guid {
static NewGuid() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
}
}
Utils.Guid = Guid;
})(Utils = Uno.Utils || (Uno.Utils = {}));
})(Uno || (Uno = {}));
var Uno;
(function (Uno) {
var UI;
(function (UI) {
Expand Down Expand Up @@ -3152,6 +3164,65 @@ var Windows;
})(Pickers = Storage.Pickers || (Storage.Pickers = {}));
})(Storage = Windows.Storage || (Windows.Storage = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var Storage;
(function (Storage) {
class StorageFolderNative {
static AddHandle(guid, handle) {
this._folderMap.set(guid, handle);
}
static RemoveHandle(guid) {
this._folderMap.delete(guid);
}
static GetHandle(guid) {
return this._folderMap.get(guid);
}
/**
* Creates a new folder inside another folder.
* @param parentGuid The GUID of the folder to create in.
* @param folderName The name of the new folder.
*/
static CreateFolderAsync(parentGuid, folderName) {
return __awaiter(this, void 0, void 0, function* () {
const parentHandle = this.GetHandle(parentGuid);
const newDirectoryHandle = yield parentHandle.getDirectoryHandle(folderName, {
create: true,
});
var guid = Uno.Utils.Guid.NewGuid();
this.AddHandle(guid, newDirectoryHandle);
return guid;
});
}
/**
* Gets a folder in the given parent folder by name.
* @param parentGuid The GUID of the parent folder to get.
* @param folderName The name of the folder to look for.
* @returns A GUID of the folder if found, other "notfound" literal.
*/
static GetFolderAsync(parentGuid, folderName) {
return __awaiter(this, void 0, void 0, function* () {
const parentHandle = this.GetHandle(parentGuid);
let nestedDirectoryHandle = undefined;
let returnedGuid = Uno.Utils.Guid.NewGuid();
try {
nestedDirectoryHandle = yield parentHandle.getDirectoryHandle(folderName);
}
catch (ex) {
if (ex instanceof DOMException && ex.message.includes("could not be found")) {
returnedGuid = "notfound";
}
}
if (nestedDirectoryHandle)
this.AddHandle(returnedGuid, nestedDirectoryHandle);
return returnedGuid;
});
}
}
StorageFolderNative._folderMap = new Map();
Storage.StorageFolderNative = StorageFolderNative;
})(Storage = Windows.Storage || (Windows.Storage = {}));
})(Windows || (Windows = {}));
// eslint-disable-next-line @typescript-eslint/no-namespace
var Windows;
(function (Windows) {
Expand Down Expand Up @@ -3640,6 +3711,26 @@ var Windows;
})(Connectivity = Networking.Connectivity || (Networking.Connectivity = {}));
})(Networking = Windows.Networking || (Windows.Networking = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var Storage;
(function (Storage) {
var Pickers;
(function (Pickers) {
class FolderPicker {
static ShowFolderPicker() {
return __awaiter(this, void 0, void 0, function* () {
const selectedFolder = yield showDirectoryPicker();
const guid = Uno.Utils.Guid.NewGuid();
Storage.StorageFolderNative.AddHandle(guid, selectedFolder);
return guid;
});
}
}
Pickers.FolderPicker = FolderPicker;
})(Pickers = Storage.Pickers || (Storage.Pickers = {}));
})(Storage = Windows.Storage || (Windows.Storage = {}));
})(Windows || (Windows = {}));
var WakeLockType;
(function (WakeLockType) {
WakeLockType["screen"] = "screen";
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/ts/Guid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Uno.Utils {
export class Guid {
public static NewGuid(): string {
return (([1e7] as any) + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: any) =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
}
}
14 changes: 14 additions & 0 deletions src/Uno.UI/ts/Windows/Storage/Pickers/FolderPicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Windows.Storage.Pickers {

export class FolderPicker {
public static async ShowFolderPicker(): Promise<string> {
const selectedFolder = await showDirectoryPicker();

const guid = Uno.Utils.Guid.NewGuid();

StorageFolderNative.AddHandle(guid, selectedFolder);

return guid;
}
}
}
64 changes: 64 additions & 0 deletions src/Uno.UI/ts/Windows/Storage/StorageFolder.Native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

namespace Windows.Storage {

export class StorageFolderNative {
private static _folderMap: Map<string, FileSystemDirectoryHandle> = new Map<string, FileSystemDirectoryHandle>();

public static AddHandle(guid: string, handle: FileSystemDirectoryHandle) {
this._folderMap.set(guid, handle);
}

public static RemoveHandle(guid: string) {
this._folderMap.delete(guid);
}

public static GetHandle(guid: string): FileSystemDirectoryHandle {
return this._folderMap.get(guid);
}

/**
* Creates a new folder inside another folder.
* @param parentGuid The GUID of the folder to create in.
* @param folderName The name of the new folder.
*/
public static async CreateFolderAsync(parentGuid: string, folderName: string): Promise<string> {
const parentHandle = this.GetHandle(parentGuid);

const newDirectoryHandle = await parentHandle.getDirectoryHandle(folderName, {
create: true,
});

var guid = Uno.Utils.Guid.NewGuid();

this.AddHandle(guid, newDirectoryHandle);

return guid;
}

/**
* Gets a folder in the given parent folder by name.
* @param parentGuid The GUID of the parent folder to get.
* @param folderName The name of the folder to look for.
* @returns A GUID of the folder if found, other "notfound" literal.
*/
public static async GetFolderAsync(parentGuid: string, folderName: string): Promise<string> {
const parentHandle = this.GetHandle(parentGuid);

let nestedDirectoryHandle: FileSystemDirectoryHandle = undefined;
let returnedGuid = Uno.Utils.Guid.NewGuid();

try {
nestedDirectoryHandle = await parentHandle.getDirectoryHandle(folderName);
} catch (ex) {
if (ex instanceof DOMException && (ex as DOMException).message.includes("could not be found")) {
returnedGuid = "notfound";
}
}

if (nestedDirectoryHandle)
this.AddHandle(returnedGuid, nestedDirectoryHandle);

return returnedGuid;
}
}
}
Loading

0 comments on commit bdc424c

Please sign in to comment.