Browse Source

Merge pull request #3885 from AvaloniaUI/managed-dialog-extras

Allow extra options for managed file dialogs
fixes/handle-invalid-dirty-rects
danwalmsley 6 years ago
committed by GitHub
parent
commit
e980f942e4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      samples/ControlCatalog/Pages/DialogsPage.xaml
  2. 14
      samples/ControlCatalog/Pages/DialogsPage.xaml.cs
  3. 12
      src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs
  4. 20
      src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs
  5. 7
      src/Avalonia.Dialogs/ManagedFileDialogOptions.cs

1
samples/ControlCatalog/Pages/DialogsPage.xaml

@ -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>

14
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Avalonia.Controls;
using Avalonia.Dialogs;
using Avalonia.Markup.Xaml;
#pragma warning disable 4014
@ -58,6 +59,19 @@ namespace ControlCatalog.Pages
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();

12
src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs

@ -14,6 +14,7 @@ namespace Avalonia.Dialogs
{
internal class ManagedFileChooserViewModel : InternalViewModelBase
{
private readonly ManagedFileDialogOptions _options;
public event Action CancelRequested;
public event Action<string[]> CompleteRequested;
@ -103,8 +104,9 @@ namespace Avalonia.Dialogs
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
@ -202,10 +204,12 @@ namespace Avalonia.Dialogs
}
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)

20
src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs

@ -9,13 +9,15 @@ namespace Avalonia.Dialogs
{
private class ManagedSystemDialogImpl<T> : ISystemDialogImpl where T : Window, new()
{
async Task<string[]> Show(SystemDialog d, Window parent)
async Task<string[]> Show(SystemDialog d, Window 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
};
@ -44,6 +46,11 @@ namespace Avalonia.Dialogs
{
return (await Show(dialog, parent))?.FirstOrDefault();
}
public async Task<string[]> ShowFileDialogAsync(FileDialog dialog, Window parent, ManagedFileDialogOptions options)
{
return await Show(dialog, parent, options);
}
}
public static TAppBuilder UseManagedSystemDialogs<TAppBuilder>(this TAppBuilder builder)
@ -61,5 +68,14 @@ namespace Avalonia.Dialogs
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
src/Avalonia.Dialogs/ManagedFileDialogOptions.cs

@ -0,0 +1,7 @@
namespace Avalonia.Dialogs
{
public class ManagedFileDialogOptions
{
public bool AllowDirectorySelection { get; set; }
}
}
Loading…
Cancel
Save