Browse Source

allow use of custom window for hosting file dialogs

pull/2777/head
Dan Walmsley 7 years ago
parent
commit
f5c8567459
  1. 2
      src/Avalonia.Dialogs/Internal/ManagedFileChooser.xaml
  2. 10
      src/Avalonia.Dialogs/Internal/ManagedFileDialog.xaml
  3. 20
      src/Avalonia.Dialogs/Internal/ManagedFileDialog.xaml.cs
  4. 82
      src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs

2
src/Avalonia.Dialogs/Internal/ManagedFileChooser.xaml

@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dialogs="clr-namespace:Avalonia.Dialogs.Internal"
xmlns:internal="clr-namespace:Avalonia.Dialogs.Internal"
x:Class="Avalonia.Dialogs.Internal.ManagedFileChooser">
x:Class="Avalonia.Dialogs.Internal.ManagedFileChooser" Margin="12 0 12 6">
<UserControl.Resources>
<internal:FileSizeStringConverter x:Key="FileSizeConverter" />
<DrawingGroup x:Key="LevelUp">

10
src/Avalonia.Dialogs/Internal/ManagedFileDialog.xaml

@ -1,10 +0,0 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dialogs="clr-namespace:Avalonia.Dialogs.Internal"
xmlns:internal="clr-namespace:Avalonia.Dialogs.Internal"
x:Class="Avalonia.Dialogs.Internal.ManagedFileDialog"
Title="{Binding Title}" Width="1100" Height="500" MinWidth="1100" MinHeight="500"
FontFamily="{DynamicResource UiFont}" FontSize="14"
Foreground="{DynamicResource ThemeForegroundBrush}">
<internal:ManagedFileChooser Margin="12 0 12 6"/>
</Window>

20
src/Avalonia.Dialogs/Internal/ManagedFileDialog.xaml.cs

@ -1,20 +0,0 @@
using System;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
namespace Avalonia.Dialogs.Internal
{
class ManagedFileDialog : Window
{
public ManagedFileDialog()
{
AvaloniaXamlLoader.Load(this);
#if DEBUG
this.AttachDevTools();
#endif
}
}
}

82
src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs

@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
@ -10,46 +11,55 @@ namespace Avalonia.Dialogs
{
public static class ManagedFileDialogExtensions
{
class ManagedSystemDialogImpl : ISystemDialogImpl
{
async Task<string[]> Show(SystemDialog d, IWindowImpl parent)
{
var model = new ManagedFileChooserViewModel((FileSystemDialog)d);
var dialog = new ManagedFileDialog
{
DataContext = model
};
string[] result = null;
model.CompleteRequested += items =>
{
result = items;
dialog.Close();
};
model.CancelRequested += dialog.Close;
await dialog.ShowDialog<object>(parent);
return result;
}
public async Task<string[]> ShowFileDialogAsync(FileDialog dialog, IWindowImpl parent)
{
return await Show(dialog, parent);
}
public async Task<string> ShowFolderDialogAsync(OpenFolderDialog dialog, IWindowImpl parent)
{
return (await Show(dialog, parent))?.FirstOrDefault();
}
}
class CustomWindowManagedSystemDialogImpl<T> : ISystemDialogImpl where T : Window, new()
{
async Task<string[]> Show(SystemDialog d, IWindowImpl parent)
{
var model = new ManagedFileChooserViewModel((FileSystemDialog)d);
var dialog = new T
{
Content = new ManagedFileChooser(),
DataContext = model
};
string[] result = null;
model.CompleteRequested += items =>
{
result = items;
dialog.Close();
};
model.CancelRequested += dialog.Close;
public static TAppBuilder UseManagedSystemDialogs<TAppBuilder>(this TAppBuilder builder)
await dialog.ShowDialog<object>(parent);
return result;
}
public async Task<string[]> ShowFileDialogAsync(FileDialog dialog, IWindowImpl parent)
{
return await Show(dialog, parent);
}
public async Task<string> ShowFolderDialogAsync(OpenFolderDialog dialog, IWindowImpl parent)
{
return (await Show(dialog, parent))?.FirstOrDefault();
}
}
public static TAppBuilder UseManagedSystemDialogs<TAppBuilder>(this TAppBuilder builder)
where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
{
builder.AfterSetup(_ =>
AvaloniaLocator.CurrentMutable.Bind<ISystemDialogImpl>().ToSingleton<ManagedSystemDialogImpl>());
AvaloniaLocator.CurrentMutable.Bind<ISystemDialogImpl>().ToSingleton<CustomWindowManagedSystemDialogImpl<Window>>());
return builder;
}
}
public static TAppBuilder UseManagedSystemDialogs<TAppBuilder, TWindow>(this TAppBuilder builder)
where TAppBuilder : AppBuilderBase<TAppBuilder>, new() where TWindow : Window, new()
{
builder.AfterSetup(_ =>
AvaloniaLocator.CurrentMutable.Bind<ISystemDialogImpl>().ToSingleton<CustomWindowManagedSystemDialogImpl<TWindow>>());
return builder;
}
}
}

Loading…
Cancel
Save