-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: chore: Multi/Single file/folder picker extensions (for convenie…
…nce) The result of these extensions is an empty Optional when the user hits Cancel on the shown file picker.
- Loading branch information
Showing
2 changed files
with
58 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Avalonia.Platform.Storage; | ||
using Gommon; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ryujinx.Ava.Utilities | ||
{ | ||
public static class StorageProviderExtensions | ||
{ | ||
public static async Task<Optional<IStorageFolder>> OpenSingleFolderPickerAsync(this IStorageProvider storageProvider, FolderPickerOpenOptions openOptions = null) => | ||
await storageProvider.OpenFolderPickerAsync(FixOpenOptions(openOptions, false)) | ||
.Then(folders => folders.FindFirst()); | ||
|
||
public static async Task<Optional<IStorageFile>> OpenSingleFilePickerAsync(this IStorageProvider storageProvider, FilePickerOpenOptions openOptions = null) => | ||
await storageProvider.OpenFilePickerAsync(FixOpenOptions(openOptions, false)) | ||
.Then(files => files.FindFirst()); | ||
|
||
public static async Task<Optional<IReadOnlyList<IStorageFolder>>> OpenMultiFolderPickerAsync(this IStorageProvider storageProvider, FolderPickerOpenOptions openOptions = null) => | ||
await storageProvider.OpenFolderPickerAsync(FixOpenOptions(openOptions, true)) | ||
.Then(folders => folders.Count > 0 ? Optional.Of(folders) : default); | ||
|
||
public static async Task<Optional<IReadOnlyList<IStorageFile>>> OpenMultiFilePickerAsync(this IStorageProvider storageProvider, FilePickerOpenOptions openOptions = null) => | ||
await storageProvider.OpenFilePickerAsync(FixOpenOptions(openOptions, true)) | ||
.Then(files => files.Count > 0 ? Optional.Of(files) : default); | ||
|
||
private static FilePickerOpenOptions FixOpenOptions(this FilePickerOpenOptions openOptions, bool allowMultiple) | ||
{ | ||
if (openOptions is null) | ||
return new FilePickerOpenOptions { AllowMultiple = allowMultiple }; | ||
|
||
openOptions.AllowMultiple = allowMultiple; | ||
|
||
return openOptions; | ||
} | ||
|
||
private static FolderPickerOpenOptions FixOpenOptions(this FolderPickerOpenOptions openOptions, bool allowMultiple) | ||
{ | ||
if (openOptions is null) | ||
return new FolderPickerOpenOptions { AllowMultiple = allowMultiple }; | ||
|
||
openOptions.AllowMultiple = allowMultiple; | ||
|
||
return openOptions; | ||
} | ||
} | ||
} |