Skip to content

Commit

Permalink
Added OpenDirectoryDialogService
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandKoenig committed Feb 7, 2024
1 parent 7544845 commit 7e9109d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/RolandK.AvaloniaExtensions.TestApp/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Open"
<MenuItem Header="Open file"
Command="{Binding OpenFileCommand}" />
<MenuItem Header="Open directory"
Command="{Binding OpenDirectoryCommand}" />

<Separator />

Expand Down
19 changes: 18 additions & 1 deletion src/RolandK.AvaloniaExtensions.TestApp/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenDirectoryViewService>();
var srvMessageBox = this.GetViewService<IMessageBoxViewService>();

var selectedDirectory = await srvOpenDirectory.ShowOpenDirectoryDialogAsync(
"Open directory");
if (string.IsNullOrEmpty(selectedDirectory)) { return; }

await srvMessageBox.ShowAsync(
"Open directory",
$"Directory {selectedDirectory} selected",
MessageBoxButtons.Ok);
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ internal static class DefaultViewServices

return new MessageBoxViewControlService(dlgHostControl);
}

if (viewServiceType == typeof(IOpenDirectoryViewService))
{
var parentWindow = host.FindLogicalAncestorOfType<Window>(true);
if (parentWindow == null) { return null; }

return new OpenDirectoryDialogService(parentWindow);
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

/// <inheritdoc />
public async Task<string?> 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);
}
}

0 comments on commit 7e9109d

Please sign in to comment.