Compare commits

...

1 Commits

Author SHA1 Message Date
Zoldorf
cf5ac18c59 modify and add async folder ops 2023-02-05 02:52:48 -07:00
3 changed files with 70 additions and 16 deletions

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using TerraFX.Interop.Windows;
namespace Robust.Client.UserInterface
{
@@ -13,6 +15,10 @@ namespace Robust.Client.UserInterface
return Task.FromResult<Stream?>(null);
}
public async IAsyncEnumerable<FileInFolder> OpenFolder()
{
yield break;
}
public Task<(Stream fileStream, bool alreadyExisted)?> SaveFile(FileDialogFilters? filters = null)
{
return Task.FromResult<(Stream fileStream, bool alreadyExisted)?>(null);

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@@ -41,6 +42,42 @@ namespace Robust.Client.UserInterface
return File.Open(name, FileMode.Open);
}
public async IAsyncEnumerable<FileInFolder> OpenFolder()
{
var folderName = await GetOpenFolderName();
if (folderName == null)
yield break;
// Grab RSIs/folders nested 1 deep
foreach (var dir in Directory.EnumerateDirectories(folderName))
{
foreach (var file in Directory.EnumerateFiles(dir))
{
yield return new FileInFolder(File.Open(file,FileMode.Open),
Path.GetRelativePath(folderName,file));
}
}
foreach (var file in Directory.EnumerateFiles(folderName))
{
yield return new FileInFolder(File.Open(file,FileMode.Open),
Path.GetRelativePath(folderName,file));
}
}
private async Task<string?> GetOpenFolderName()
{
if (await IsKDialogAvailable())
{
return await OpenFolderKDialog();
}
return await OpenFolderNfd();
}
private async Task<string?> GetOpenFileName(FileDialogFilters? filters)
{
if (await IsKDialogAvailable())
@@ -144,20 +181,20 @@ namespace Robust.Client.UserInterface
});
}
/*
private unsafe Task<string?> OpenFolderNfd()
{
// Have to run it in the thread pool to avoid blocking the main thread.
return RunAsyncMaybe(() =>
{
byte* outPath;
var result = sw_NFD_PickFolder(null, &outPath);
private unsafe Task<string?> OpenFolderNfd()
{
// Have to run it in the thread pool to avoid blocking the main thread.
return RunAsyncMaybe(() =>
{
byte* outPath;
var result = sw_NFD_PickFolder(null, &outPath);
return HandleNfdResult(result, outPath);
});
}
return HandleNfdResult(result, outPath);
});
}
*/
// ReSharper disable once MemberCanBeMadeStatic.Local
private Task<string?> RunAsyncMaybe(Func<string?> action)
@@ -303,12 +340,11 @@ namespace Robust.Client.UserInterface
return RunKDialog("--getsavefilename", Environment.GetEnvironmentVariable("HOME")!, filtersFormatted);
}
/*
private Task<string?> OpenFolderKDialog()
{
return RunKDialog("--getexistingdirectory");
}
*/
private async Task<string?> RunKDialog(params string[] options)
{
@@ -371,11 +407,11 @@ namespace Robust.Client.UserInterface
private static extern unsafe sw_nfdresult
sw_NFD_SaveDialog(byte* filterList, byte* defaultPath, byte** outPath);
/*
[DllImport("swnfd.dll")]
private static extern unsafe sw_nfdresult
sw_NFD_PickFolder(byte* defaultPath, byte** outPath);
*/
[DllImport("swnfd.dll")]
private static extern unsafe void sw_NFD_Free(void* ptr);

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
@@ -21,6 +22,15 @@ namespace Robust.Client.UserInterface
/// </returns>
Task<Stream?> OpenFile(FileDialogFilters? filters = null);
/// <summary>
/// Open a folder dialog used for opening a directory.
/// </summary>
/// <returns>
/// An array of task<streams> for all items in the directory.
/// <see langword="null" /> if the user cancelled the action.
/// </returns>
IAsyncEnumerable<FileInFolder> OpenFolder();
/// <summary>
/// Open a file dialog used for saving a single file.
/// </summary>
@@ -30,4 +40,6 @@ namespace Robust.Client.UserInterface
/// </returns>
Task<(Stream fileStream, bool alreadyExisted)?> SaveFile(FileDialogFilters? filters = null);
}
public readonly record struct FileInFolder(Stream Stream, string filename);
}