Added view models and storage providers.
This commit is contained in:
90
Harmonia.WinUI/Storage/WindowsStorageProvider.cs
Normal file
90
Harmonia.WinUI/Storage/WindowsStorageProvider.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user