forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add native filesystem folder picker
Also adds preliminary implementation of the WASM native version of StorageFolder
- Loading branch information
1 parent
c78a700
commit bdc424c
Showing
17 changed files
with
700 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.