using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Platform.Storage; namespace AvParser.UI.Services; /// Asks the user for a directory. /// /// An interface because the picker lives on the window, not on the view model: reaching for a /// TopLevel from a view model would make it untestable and would tie it to there being a /// window at all. Tests substitute a canned answer. /// public interface IFolderPicker { /// Returns the chosen directory, or null when the user cancelled. Task PickAsync(string title, string? startAt = null); } /// public sealed class FolderPicker : IFolderPicker { /// public async Task PickAsync(string title, string? startAt = null) { if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) { return null; } var window = desktop.MainWindow; if (window?.StorageProvider is not { CanPickFolder: true } storage) { return null; } var options = new FolderPickerOpenOptions { Title = title, AllowMultiple = false }; if (!string.IsNullOrWhiteSpace(startAt) && Directory.Exists(startAt)) { options.SuggestedStartLocation = await storage.TryGetFolderFromPathAsync(startAt).ConfigureAwait(true); } var chosen = await storage.OpenFolderPickerAsync(options).ConfigureAwait(true); return chosen.Count == 0 ? null : chosen[0].TryGetLocalPath(); } }