Added view models and storage providers.

This commit is contained in:
2025-03-22 19:05:13 -04:00
parent c3a87dee6a
commit 9245e9c4cc
8 changed files with 883 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Harmonia.WinUI.Storage;
public sealed class FilePickerFileType
{
public string Name { get; set; } = string.Empty;
public IReadOnlyList<string> Patterns { get; set; } = [];
}

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Harmonia.WinUI.Storage;
public class FilePickerOptions
{
public IReadOnlyList<FilePickerFileType> FileTypeFilter { get; set; } = [];
public StoragePickerViewMode ViewMode { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace Harmonia.WinUI.Storage;
public class FolderPickerOptions
{
public StoragePickerViewMode ViewMode { get; set; }
}

View File

@@ -0,0 +1,10 @@
using System.Threading.Tasks;
namespace Harmonia.WinUI.Storage;
public interface IStorageProvider
{
public Task<string> GetFileAsync(FilePickerOptions? options = null);
public Task<string[]> GetFilesAsync(FilePickerOptions? options = null);
public Task<string> GetPathAsync();
}

View File

@@ -0,0 +1,7 @@
namespace Harmonia.WinUI.Storage;
public enum StoragePickerViewMode
{
List,
Thumbnail
}

View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
namespace Harmonia.WinUI.Storage;
public class WindowsStorageProvider(MainWindow mainWindow) : IStorageProvider
{
public async Task<string> GetFileAsync(FilePickerOptions? options = null)
{
FileOpenPicker fileOpenPicker = GetFileOpenPicker(options);
InitializePicker(fileOpenPicker);
StorageFile storageFile = await fileOpenPicker.PickSingleFileAsync();
return storageFile.Path;
}
public async Task<string[]> GetFilesAsync(FilePickerOptions? options = null)
{
FileOpenPicker fileOpenPicker = GetFileOpenPicker(options);
InitializePicker(fileOpenPicker);
var storageFiles = await fileOpenPicker.PickMultipleFilesAsync();
return [.. storageFiles.Select(storageFile => storageFile.Path)];
}
private static FileOpenPicker GetFileOpenPicker(FilePickerOptions? options = null)
{
FilePickerOptions filePickerOptions = options ?? new();
FileOpenPicker fileOpenPicker = new()
{
ViewMode = GetViewMode(filePickerOptions.ViewMode)
};
string[] filters = GetFilters(filePickerOptions.FileTypeFilter);
foreach (string filter in filters)
{
fileOpenPicker.FileTypeFilter.Add(filter);
}
if (filePickerOptions.FileTypeFilter.Count == 0)
{
fileOpenPicker.FileTypeFilter.Add("*");
}
return fileOpenPicker;
}
private static PickerViewMode GetViewMode(StoragePickerViewMode viewMode)
{
return viewMode switch
{
StoragePickerViewMode.Thumbnail => PickerViewMode.Thumbnail,
_ => PickerViewMode.List,
};
}
private static string[] GetFilters(IReadOnlyList<FilePickerFileType> fileTypes)
{
return [.. fileTypes.SelectMany(fileType => fileType.Patterns)];
}
public async Task<string> GetPathAsync()
{
var folderPicker = new FolderPicker
{
ViewMode = PickerViewMode.Thumbnail
};
folderPicker.FileTypeFilter.Add("*");
InitializePicker(folderPicker);
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
return folder.Path;
}
private void InitializePicker(object target)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(mainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(target, hWnd);
}
}