From 7e9109dfd2a180d32bdaca68c9066bb0ed10a6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Ko=CC=88nig?= Date: Wed, 7 Feb 2024 10:04:46 +0800 Subject: [PATCH] Added OpenDirectoryDialogService --- .../MainWindow.axaml | 4 ++- .../MainWindowViewModel.cs | 19 ++++++++++- .../ViewServices/DefaultViewServices.cs | 8 +++++ .../FileDialogs/OpenDirectoryDialogService.cs | 33 +++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/RolandK.AvaloniaExtensions/ViewServices/FileDialogs/OpenDirectoryDialogService.cs diff --git a/src/RolandK.AvaloniaExtensions.TestApp/MainWindow.axaml b/src/RolandK.AvaloniaExtensions.TestApp/MainWindow.axaml index 596566c..cc30298 100644 --- a/src/RolandK.AvaloniaExtensions.TestApp/MainWindow.axaml +++ b/src/RolandK.AvaloniaExtensions.TestApp/MainWindow.axaml @@ -20,8 +20,10 @@ - + diff --git a/src/RolandK.AvaloniaExtensions.TestApp/MainWindowViewModel.cs b/src/RolandK.AvaloniaExtensions.TestApp/MainWindowViewModel.cs index 21a2da9..0edcbc2 100644 --- a/src/RolandK.AvaloniaExtensions.TestApp/MainWindowViewModel.cs +++ b/src/RolandK.AvaloniaExtensions.TestApp/MainWindowViewModel.cs @@ -48,7 +48,24 @@ public async Task OpenFileAsync() await srvMessageBox.ShowAsync( "Open file", - $"File {selectedFile} selected", MessageBoxButtons.Ok); + $"File {selectedFile} selected", + MessageBoxButtons.Ok); + } + + [RelayCommand] + public async Task OpenDirectoryAsync() + { + var srvOpenDirectory = this.GetViewService(); + var srvMessageBox = this.GetViewService(); + + var selectedDirectory = await srvOpenDirectory.ShowOpenDirectoryDialogAsync( + "Open directory"); + if (string.IsNullOrEmpty(selectedDirectory)) { return; } + + await srvMessageBox.ShowAsync( + "Open directory", + $"Directory {selectedDirectory} selected", + MessageBoxButtons.Ok); } [RelayCommand] diff --git a/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs b/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs index 56d8ab5..daaf6db 100644 --- a/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs +++ b/src/RolandK.AvaloniaExtensions/ViewServices/DefaultViewServices.cs @@ -33,6 +33,14 @@ internal static class DefaultViewServices return new MessageBoxViewControlService(dlgHostControl); } + + if (viewServiceType == typeof(IOpenDirectoryViewService)) + { + var parentWindow = host.FindLogicalAncestorOfType(true); + if (parentWindow == null) { return null; } + + return new OpenDirectoryDialogService(parentWindow); + } return null; } diff --git a/src/RolandK.AvaloniaExtensions/ViewServices/FileDialogs/OpenDirectoryDialogService.cs b/src/RolandK.AvaloniaExtensions/ViewServices/FileDialogs/OpenDirectoryDialogService.cs new file mode 100644 index 0000000..bb53f10 --- /dev/null +++ b/src/RolandK.AvaloniaExtensions/ViewServices/FileDialogs/OpenDirectoryDialogService.cs @@ -0,0 +1,33 @@ +using System.Web; +using Avalonia.Controls; +using Avalonia.Platform.Storage; +using RolandK.AvaloniaExtensions.ViewServices.Base; + +namespace RolandK.AvaloniaExtensions.ViewServices.FileDialogs; + +public class OpenDirectoryDialogService : ViewServiceBase, IOpenDirectoryViewService +{ + private Window _parent; + + public OpenDirectoryDialogService(Window parent) + { + _parent = parent; + } + + /// + public async Task ShowOpenDirectoryDialogAsync(string title) + { + var options = new FolderPickerOpenOptions(); + options.AllowMultiple = false; + options.Title = title; + + var selectedFolders = await _parent.StorageProvider.OpenFolderPickerAsync(options); + if ((selectedFolders == null) || + (selectedFolders.Count == 0)) + { + return null; + } + + return HttpUtility.UrlDecode(selectedFolders[0].Path.AbsolutePath); + } +} \ No newline at end of file