From e6b8914b7d97b0c64f909e74832bf234c1c98032 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 14 Feb 2023 20:13:46 -0500 Subject: [PATCH] Rename TryGetFileFromPath-like method to end with Async suffix --- .../ControlCatalog/Pages/DialogsPage.xaml.cs | 6 +-- .../Storage/FileIO/BclStorageProvider.cs | 6 +-- .../Platform/Storage/IStorageProvider.cs | 6 +-- .../Storage/StorageProviderExtensions.cs | 45 +++++++++---------- .../Platform/Dialogs/SystemDialogImpl.cs | 6 +-- .../Screenshots/FilePickerHandler.cs | 4 +- .../Internal/ManagedFileChooserViewModel.cs | 2 +- .../ManagedFileDialogExtensions.cs | 2 +- src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 2 +- src/Avalonia.Native/SystemDialogs.cs | 6 +-- .../NativeDialogs/CompositeStorageProvider.cs | 12 ++--- .../NativeDialogs/GtkNativeFileDialogs.cs | 2 +- .../Avalonia.Win32/Win32StorageProvider.cs | 2 +- 13 files changed, 50 insertions(+), 51 deletions(-) diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs index 61e10729cc..e24860e3e1 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs @@ -40,7 +40,7 @@ namespace ControlCatalog.Pages if (Enum.TryParse(currentFolderBox.Text, true, out var folderEnum)) { - lastSelectedDirectory = await GetStorageProvider().TryGetWellKnownFolder(folderEnum); + lastSelectedDirectory = await GetStorageProvider().TryGetWellKnownFolderAsync(folderEnum); } else { @@ -51,7 +51,7 @@ namespace ControlCatalog.Pages if (folderLink is not null) { - lastSelectedDirectory = await GetStorageProvider().TryGetFolderFromPath(folderLink); + lastSelectedDirectory = await GetStorageProvider().TryGetFolderFromPathAsync(folderLink); } } }; @@ -148,7 +148,7 @@ namespace ControlCatalog.Pages } else { - SetFolder(await GetStorageProvider().TryGetFolderFromPath(result)); + SetFolder(await GetStorageProvider().TryGetFolderFromPathAsync(result)); results.Items = new[] { result }; resultsVisible.IsVisible = true; } diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs index ee169d62a5..34409f5fda 100644 --- a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs @@ -34,7 +34,7 @@ internal abstract class BclStorageProvider : IStorageProvider : Task.FromResult(null); } - public virtual Task TryGetFileFromPath(Uri filePath) + public virtual Task TryGetFileFromPathAsync(Uri filePath) { if (filePath.IsAbsoluteUri) { @@ -48,7 +48,7 @@ internal abstract class BclStorageProvider : IStorageProvider return Task.FromResult(null); } - public virtual Task TryGetFolderFromPath(Uri folderPath) + public virtual Task TryGetFolderFromPathAsync(Uri folderPath) { if (folderPath.IsAbsoluteUri) { @@ -62,7 +62,7 @@ internal abstract class BclStorageProvider : IStorageProvider return Task.FromResult(null); } - public virtual Task TryGetWellKnownFolder(WellKnownFolder wellKnownFolder) + public virtual Task TryGetWellKnownFolderAsync(WellKnownFolder wellKnownFolder) { // Note, this BCL API returns different values depending on the .NET version. // We should also document it. diff --git a/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs b/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs index 6922151e02..9d3c961e51 100644 --- a/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs +++ b/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs @@ -66,7 +66,7 @@ public interface IStorageProvider /// It also might ask user for the permission, and throw an exception if it was denied. /// /// File or null if it doesn't exist. - Task TryGetFileFromPath(Uri filePath); + Task TryGetFileFromPathAsync(Uri filePath); /// /// Attempts to read folder from the file-system by its path. @@ -78,12 +78,12 @@ public interface IStorageProvider /// It also might ask user for the permission, and throw an exception if it was denied. /// /// Folder or null if it doesn't exist. - Task TryGetFolderFromPath(Uri folderPath); + Task TryGetFolderFromPathAsync(Uri folderPath); /// /// Attempts to read folder from the file-system by its path /// /// Well known folder identifier. /// Folder or null if it doesn't exist. - Task TryGetWellKnownFolder(WellKnownFolder wellKnownFolder); + Task TryGetWellKnownFolderAsync(WellKnownFolder wellKnownFolder); } diff --git a/src/Avalonia.Base/Platform/Storage/StorageProviderExtensions.cs b/src/Avalonia.Base/Platform/Storage/StorageProviderExtensions.cs index c7772d1196..6f8b945cd6 100644 --- a/src/Avalonia.Base/Platform/Storage/StorageProviderExtensions.cs +++ b/src/Avalonia.Base/Platform/Storage/StorageProviderExtensions.cs @@ -8,48 +8,47 @@ namespace Avalonia.Platform.Storage; /// public static class StorageProviderExtensions { - /// - public static Task TryGetFileFromPath(this IStorageProvider provider, string filePath) + /// + public static Task TryGetFileFromPathAsync(this IStorageProvider provider, string filePath) { - return provider.TryGetFileFromPath(StorageProviderHelpers.FilePathToUri(filePath)); + return provider.TryGetFileFromPathAsync(StorageProviderHelpers.FilePathToUri(filePath)); } - /// - public static Task TryGetFolderFromPath(this IStorageProvider provider, string folderPath) + /// + public static Task TryGetFolderFromPathAsync(this IStorageProvider provider, string folderPath) { - return provider.TryGetFolderFromPath(StorageProviderHelpers.FilePathToUri(folderPath)); + return provider.TryGetFolderFromPathAsync(StorageProviderHelpers.FilePathToUri(folderPath)); } - internal static string? TryGetFullPath(this IStorageFolder folder) + /// + /// Gets the local file system path of the item as a string. + /// + /// Storage folder or file. + /// Full local path to the folder or file if possible, otherwise null. + /// + /// Android platform usually uses "content:" virtual file paths + /// and Browser platform has isolated access without full paths, + /// so on these platforms this method will return null. + /// + public static string? TryGetLocalPath(this IStorageItem item) { // We can avoid double escaping of the path by checking for BclStorageFolder. // Ideally, `folder.Path.LocalPath` should also work, as that's only available way for the users. - if (folder is BclStorageFolder storageFolder) + if (item is BclStorageFolder storageFolder) { return storageFolder.DirectoryInfo.FullName; } - - if (folder.Path is { IsAbsoluteUri: true, Scheme: "file" } absolutePath) - { - return absolutePath.LocalPath; - } - - // android "content:", browser and ios relative links go here. - return null; - } - - internal static string? TryGetFullPath(this IStorageFile file) - { - if (file is BclStorageFile storageFolder) + if (item is BclStorageFile storageFile) { - return storageFolder.FileInfo.FullName; + return storageFile.FileInfo.FullName; } - if (file.Path is { IsAbsoluteUri: true, Scheme: "file" } absolutePath) + if (item.Path is { IsAbsoluteUri: true, Scheme: "file" } absolutePath) { return absolutePath.LocalPath; } + // android "content:", browser and ios relative links go here. return null; } } diff --git a/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs b/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs index a8a266e378..20bfb440e3 100644 --- a/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs +++ b/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs @@ -27,7 +27,7 @@ namespace Avalonia.Controls.Platform var files = await filePicker.OpenFilePickerAsync(options); return files - .Select(file => file.TryGetFullPath() ?? file.Name) + .Select(file => file.TryGetLocalPath() ?? file.Name) .ToArray(); } else if (dialog is SaveFileDialog saveDialog) @@ -46,7 +46,7 @@ namespace Avalonia.Controls.Platform return null; } - var filePath = file.TryGetFullPath() ?? file.Name; + var filePath = file.TryGetLocalPath() ?? file.Name; return new[] { filePath }; } return null; @@ -64,7 +64,7 @@ namespace Avalonia.Controls.Platform var folders = await filePicker.OpenFolderPickerAsync(options); return folders - .Select(folder => folder.TryGetFullPath() ?? folder.Name) + .Select(folder => folder.TryGetLocalPath() ?? folder.Name) .FirstOrDefault(u => u is not null); } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/Screenshots/FilePickerHandler.cs b/src/Avalonia.Diagnostics/Diagnostics/Screenshots/FilePickerHandler.cs index 548d177643..6ea46b6d54 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Screenshots/FilePickerHandler.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Screenshots/FilePickerHandler.cs @@ -54,8 +54,8 @@ namespace Avalonia.Diagnostics.Screenshots protected override async Task GetStream(Control control) { var storageProvider = GetTopLevel(control).StorageProvider; - var defaultFolder = await storageProvider.TryGetFolderFromPath(_screenshotRoot) - ?? await storageProvider.TryGetWellKnownFolder(WellKnownFolder.Pictures); + var defaultFolder = await storageProvider.TryGetFolderFromPathAsync(_screenshotRoot) + ?? await storageProvider.TryGetWellKnownFolderAsync(WellKnownFolder.Pictures); var result = await storageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { diff --git a/src/Avalonia.Dialogs/Internal/ManagedFileChooserViewModel.cs b/src/Avalonia.Dialogs/Internal/ManagedFileChooserViewModel.cs index 46de460b1a..c929cb9ac9 100644 --- a/src/Avalonia.Dialogs/Internal/ManagedFileChooserViewModel.cs +++ b/src/Avalonia.Dialogs/Internal/ManagedFileChooserViewModel.cs @@ -260,7 +260,7 @@ namespace Avalonia.Dialogs.Internal public void Navigate(IStorageFolder path, string initialSelectionName = null) { - var fullDirectoryPath = path?.TryGetFullPath() ?? Directory.GetCurrentDirectory(); + var fullDirectoryPath = path?.TryGetLocalPath() ?? Directory.GetCurrentDirectory(); Navigate(fullDirectoryPath, initialSelectionName); } diff --git a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs index cad938ac35..e9a75ab46a 100644 --- a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs +++ b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs @@ -51,7 +51,7 @@ namespace Avalonia.Dialogs var files = await impl.OpenFilePickerAsync(dialog.ToFilePickerOpenOptions()); return files - .Select(file => file.TryGetFullPath() ?? file.Name) + .Select(file => file.TryGetLocalPath() ?? file.Name) .ToArray(); } } diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index 6f647215be..905ce1f272 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -88,7 +88,7 @@ namespace Avalonia.FreeDesktop if (options.SuggestedFileName is { } currentName) chooserOptions.Add("current_name", currentName); - if (options.SuggestedStartLocation?.TryGetFullPath() is { } folderPath) + if (options.SuggestedStartLocation?.TryGetLocalPath() is { } folderPath) chooserOptions.Add("current_folder", Encoding.UTF8.GetBytes(folderPath)); objectPath = await _fileChooser.SaveFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions); diff --git a/src/Avalonia.Native/SystemDialogs.cs b/src/Avalonia.Native/SystemDialogs.cs index 60724507f3..1d23ad9997 100644 --- a/src/Avalonia.Native/SystemDialogs.cs +++ b/src/Avalonia.Native/SystemDialogs.cs @@ -33,7 +33,7 @@ namespace Avalonia.Native { using var events = new SystemDialogEvents(); - var suggestedDirectory = options.SuggestedStartLocation?.TryGetFullPath() ?? string.Empty; + var suggestedDirectory = options.SuggestedStartLocation?.TryGetLocalPath() ?? string.Empty; _native.OpenFileDialog((IAvnWindow)_window.Native, events, @@ -53,7 +53,7 @@ namespace Avalonia.Native { using var events = new SystemDialogEvents(); - var suggestedDirectory = options.SuggestedStartLocation?.TryGetFullPath() ?? string.Empty; + var suggestedDirectory = options.SuggestedStartLocation?.TryGetLocalPath() ?? string.Empty; _native.SaveFileDialog((IAvnWindow)_window.Native, events, @@ -72,7 +72,7 @@ namespace Avalonia.Native { using var events = new SystemDialogEvents(); - var suggestedDirectory = options.SuggestedStartLocation?.TryGetFullPath() ?? string.Empty; + var suggestedDirectory = options.SuggestedStartLocation?.TryGetLocalPath() ?? string.Empty; _native.SelectFolderDialog((IAvnWindow)_window.Native, events, options.AllowMultiple.AsComBool(), options.Title ?? "", suggestedDirectory); diff --git a/src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs b/src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs index 07a11ff2ec..16416c7e56 100644 --- a/src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs +++ b/src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs @@ -62,21 +62,21 @@ internal class CompositeStorageProvider : IStorageProvider return await provider.OpenFolderBookmarkAsync(bookmark).ConfigureAwait(false); } - public async Task TryGetFileFromPath(Uri filePath) + public async Task TryGetFileFromPathAsync(Uri filePath) { var provider = await EnsureStorageProvider().ConfigureAwait(false); - return await provider.TryGetFileFromPath(filePath).ConfigureAwait(false); + return await provider.TryGetFileFromPathAsync(filePath).ConfigureAwait(false); } - public async Task TryGetFolderFromPath(Uri folderPath) + public async Task TryGetFolderFromPathAsync(Uri folderPath) { var provider = await EnsureStorageProvider().ConfigureAwait(false); - return await provider.TryGetFolderFromPath(folderPath).ConfigureAwait(false); + return await provider.TryGetFolderFromPathAsync(folderPath).ConfigureAwait(false); } - public async Task TryGetWellKnownFolder(WellKnownFolder wellKnownFolder) + public async Task TryGetWellKnownFolderAsync(WellKnownFolder wellKnownFolder) { var provider = await EnsureStorageProvider().ConfigureAwait(false); - return await provider.TryGetWellKnownFolder(wellKnownFolder).ConfigureAwait(false); + return await provider.TryGetWellKnownFolderAsync(wellKnownFolder).ConfigureAwait(false); } } diff --git a/src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs b/src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs index ceb32e52e9..16e1a1bce8 100644 --- a/src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs +++ b/src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs @@ -196,7 +196,7 @@ namespace Avalonia.X11.NativeDialogs gtk_dialog_add_button(dlg, open, GtkResponseType.Cancel); } - var folderLocalPath = initialFolder?.TryGetFullPath(); + var folderLocalPath = initialFolder?.TryGetLocalPath(); if (folderLocalPath is not null) { using var dir = new Utf8Buffer(folderLocalPath); diff --git a/src/Windows/Avalonia.Win32/Win32StorageProvider.cs b/src/Windows/Avalonia.Win32/Win32StorageProvider.cs index 2fd49c8b09..86574f2fa3 100644 --- a/src/Windows/Avalonia.Win32/Win32StorageProvider.cs +++ b/src/Windows/Avalonia.Win32/Win32StorageProvider.cs @@ -131,7 +131,7 @@ namespace Avalonia.Win32 } } - if (folder?.TryGetFullPath() is { } folderPath) + if (folder?.TryGetLocalPath() is { } folderPath) { var riid = UnmanagedMethods.ShellIds.IShellItem; if (UnmanagedMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref riid, out var directoryShellItem)