using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Avalonia.Controls.Platform; using Avalonia.Platform.Storage; using Avalonia.Platform.Storage.FileIO; namespace Avalonia.Controls { /// /// Base class for system file dialogs. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class FileDialog : FileSystemDialog { /// /// Gets or sets a collection of filters which determine the types of files displayed in an /// or an . /// public List? Filters { get; set; } = new List(); /// /// Gets or sets initial file name that is displayed when the dialog is opened. /// public string? InitialFileName { get; set; } } /// /// Base class for system file and directory dialogs. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class FileSystemDialog : SystemDialog { [Obsolete("Use Directory")] public string? InitialDirectory { get => Directory; set => Directory = value; } /// /// Gets or sets the initial directory that will be displayed when the file system dialog /// is opened. /// public string? Directory { get; set; } } /// /// Represents a system dialog that prompts the user to select a location for saving a file. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class SaveFileDialog : FileDialog { /// /// Gets or sets the default extension to be used to save the file (including the period "."). /// public string? DefaultExtension { get; set; } /// /// Gets or sets a value indicating whether to display a warning if the user specifies the name of a file that already exists. /// public bool? ShowOverwritePrompt { get; set; } /// /// Shows the save file dialog. /// /// The parent window. /// /// A task that on completion contains the full path of the save location, or null if the /// dialog was canceled. /// public async Task ShowAsync(Window parent) { if(parent == null) throw new ArgumentNullException(nameof(parent)); var service = AvaloniaLocator.Current.GetRequiredService(); return (await service.ShowFileDialogAsync(this, parent) ?? Array.Empty()).FirstOrDefault(); } public FilePickerSaveOptions ToFilePickerSaveOptions() { return new FilePickerSaveOptions { SuggestedFileName = InitialFileName, DefaultExtension = DefaultExtension, FileTypeChoices = Filters?.Select(f => new FilePickerFileType(f.Name!) { Patterns = f.Extensions.Select(e => $"*.{e}").ToArray() }).ToArray(), Title = Title, SuggestedStartLocation = InitialDirectory is { } directory ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) : null, ShowOverwritePrompt = ShowOverwritePrompt }; } } /// /// Represents a system dialog that allows the user to select one or more files to open. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class OpenFileDialog : FileDialog { /// /// Gets or sets a value indicating whether the user can select multiple files. /// public bool AllowMultiple { get; set; } /// /// Shows the open file dialog. /// /// The parent window. /// /// A task that on completion returns an array containing the full path to the selected /// files, or null if the dialog was canceled. /// public Task ShowAsync(Window parent) { if(parent == null) throw new ArgumentNullException(nameof(parent)); var service = AvaloniaLocator.Current.GetRequiredService(); return service.ShowFileDialogAsync(this, parent); } public FilePickerOpenOptions ToFilePickerOpenOptions() { return new FilePickerOpenOptions { AllowMultiple = AllowMultiple, FileTypeFilter = Filters?.Select(f => new FilePickerFileType(f.Name!) { Patterns = f.Extensions.Select(e => $"*.{e}").ToArray() }).ToArray(), Title = Title, SuggestedStartLocation = InitialDirectory is { } directory ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) : null }; } } /// /// Represents a system dialog that allows the user to select a directory. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class OpenFolderDialog : FileSystemDialog { [Obsolete("Use Directory")] public string? DefaultDirectory { get => Directory; set => Directory = value; } /// /// Shows the open folder dialog. /// /// The parent window. /// /// A task that on completion returns the full path of the selected directory, or null if the /// dialog was canceled. /// public Task ShowAsync(Window parent) { if(parent == null) throw new ArgumentNullException(nameof(parent)); var service = AvaloniaLocator.Current.GetRequiredService(); return service.ShowFolderDialogAsync(this, parent); } public FolderPickerOpenOptions ToFolderPickerOpenOptions() { return new FolderPickerOpenOptions { Title = Title, SuggestedStartLocation = InitialDirectory is { } directory ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) : null }; } } /// /// Base class for system dialogs. /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class SystemDialog { static SystemDialog() { if (AvaloniaLocator.Current.GetService() is null) { // Register default implementation. AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); } } /// /// Gets or sets the dialog title. /// public string? Title { get; set; } } /// /// Represents a filter in an or an . /// [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class FileDialogFilter { /// /// Gets or sets the name of the filter, e.g. ("Text files (.txt)"). /// public string? Name { get; set; } /// /// Gets or sets a list of file extensions matched by the filter (e.g. "txt" or "*" for all /// files). /// public List Extensions { get; set; } = new List(); } }