Skip to content

Commit

Permalink
Merge branch 'managed-dialog-extras-0.9' into stable/0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
danwalmsley committed May 7, 2020
2 parents c033386 + 9e004e3 commit eae0e72
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/SharedVersion.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Avalonia</Product>
<Version>0.9.10</Version>
<Version>0.9.10-preview</Version>
<Copyright>Copyright 2020 &#169; The AvaloniaUI Project</Copyright>
<PackageProjectUrl>https://avaloniaui.net</PackageProjectUrl>
<RepositoryUrl>https://github.com/AvaloniaUI/Avalonia/</RepositoryUrl>
Expand Down
1 change: 1 addition & 0 deletions samples/ControlCatalog/Pages/DialogsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Button Name="OpenFile">Open File</Button>
<Button Name="SaveFile">Save File</Button>
<Button Name="SelectFolder">Select Folder</Button>
<Button Name="OpenBoth">Select Both</Button>
<Button Name="DecoratedWindow">Decorated window</Button>
<Button Name="DecoratedWindowDialog">Decorated window (dialog)</Button>
<Button Name="Dialog">Dialog</Button>
Expand Down
14 changes: 14 additions & 0 deletions samples/ControlCatalog/Pages/DialogsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using Avalonia.Controls;
using Avalonia.Dialogs;
using Avalonia.Markup.Xaml;
#pragma warning disable 4014

Expand Down Expand Up @@ -58,6 +59,19 @@ List<FileDialogFilter> GetFilters()
Title = "Select folder",
}.ShowAsync(GetWindow());
};
this.FindControl<Button>("OpenBoth").Click += async delegate
{
var res = await new OpenFileDialog()
{
Title = "Select both",
AllowMultiple = true
}.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
{
AllowDirectorySelection = true
});
if (res != null)
Console.WriteLine("Selected: \n" + string.Join("\n", res));
};
this.FindControl<Button>("DecoratedWindow").Click += delegate
{
new DecoratedWindow().Show();
Expand Down
12 changes: 8 additions & 4 deletions src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Avalonia.Dialogs
{
internal class ManagedFileChooserViewModel : InternalViewModelBase
{
private readonly ManagedFileDialogOptions _options;
public event Action CancelRequested;
public event Action<string[]> CompleteRequested;

Expand Down Expand Up @@ -103,8 +104,9 @@ private void RefreshQuickLinks(ManagedFileChooserSources quickSources)
QuickLinks.AddRange(quickSources.GetAllItems().Select(i => new ManagedFileChooserItemViewModel(i)));
}

public ManagedFileChooserViewModel(FileSystemDialog dialog)
public ManagedFileChooserViewModel(FileSystemDialog dialog, ManagedFileDialogOptions options)
{
_options = options;
_disposables = new CompositeDisposable();

var quickSources = AvaloniaLocator.Current
Expand Down Expand Up @@ -202,10 +204,12 @@ await Dispatcher.UIThread.InvokeAsync(() =>
}
else
{
var invalidItems = SelectedItems.Where(i => i.ItemType == ManagedFileChooserItemType.Folder).ToList();
foreach (var item in invalidItems)
if (!_options.AllowDirectorySelection)
{
SelectedItems.Remove(item);
var invalidItems = SelectedItems.Where(i => i.ItemType == ManagedFileChooserItemType.Folder)
.ToList();
foreach (var item in invalidItems)
SelectedItems.Remove(item);
}

if (!_selectingDirectory)
Expand Down
20 changes: 18 additions & 2 deletions src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public static class ManagedFileDialogExtensions
{
class ManagedSystemDialogImpl<T> : ISystemDialogImpl where T : Window, new()
{
async Task<string[]> Show(SystemDialog d, IWindowImpl parent)
async Task<string[]> Show(SystemDialog d, IWindowImpl parent, ManagedFileDialogOptions options = null)
{
var model = new ManagedFileChooserViewModel((FileSystemDialog)d);
var model = new ManagedFileChooserViewModel((FileSystemDialog)d,
options ?? new ManagedFileDialogOptions());

var dialog = new T
{
Content = new ManagedFileChooser(),
Title = d.Title,
DataContext = model
};

Expand Down Expand Up @@ -48,6 +50,11 @@ public async Task<string> ShowFolderDialogAsync(OpenFolderDialog dialog, IWindow
{
return (await Show(dialog, parent))?.FirstOrDefault();
}

public async Task<string[]> ShowFileDialogAsync(FileDialog dialog, Window parent, ManagedFileDialogOptions options)
{
return await Show(dialog, parent.PlatformImpl, options);
}
}

public static TAppBuilder UseManagedSystemDialogs<TAppBuilder>(this TAppBuilder builder)
Expand All @@ -65,5 +72,14 @@ public static TAppBuilder UseManagedSystemDialogs<TAppBuilder, TWindow>(this TAp
AvaloniaLocator.CurrentMutable.Bind<ISystemDialogImpl>().ToSingleton<ManagedSystemDialogImpl<TWindow>>());
return builder;
}

public static Task<string[]> ShowManagedAsync(this OpenFileDialog dialog, Window parent,
ManagedFileDialogOptions options = null) => ShowManagedAsync<Window>(dialog, parent, options);

public static Task<string[]> ShowManagedAsync<TWindow>(this OpenFileDialog dialog, Window parent,
ManagedFileDialogOptions options = null) where TWindow : Window, new()
{
return new ManagedSystemDialogImpl<TWindow>().ShowFileDialogAsync(dialog, parent, options);
}
}
}
7 changes: 7 additions & 0 deletions src/Avalonia.Dialogs/ManagedFileDialogOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Avalonia.Dialogs
{
public class ManagedFileDialogOptions
{
public bool AllowDirectorySelection { get; set; }
}
}

0 comments on commit eae0e72

Please sign in to comment.