18 changed files with 576 additions and 270 deletions
@ -1,29 +1,57 @@ |
|||
<UserControl xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
x:Class="ControlCatalog.Pages.DialogsPage"> |
|||
<StackPanel Orientation="Vertical" Spacing="4" Margin="4"> |
|||
<CheckBox Name="UseFilters">Use filters</CheckBox> |
|||
<Button Name="OpenFile">_Open File</Button> |
|||
<Button Name="OpenMultipleFiles">Open _Multiple File</Button> |
|||
<Button Name="SaveFile">_Save File</Button> |
|||
<Button Name="SelectFolder">Select Fo_lder</Button> |
|||
<Button Name="OpenBoth">Select _Both</Button> |
|||
<UserControl x:Class="ControlCatalog.Pages.DialogsPage" |
|||
xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
|||
<StackPanel Margin="4" |
|||
Orientation="Vertical" |
|||
Spacing="4"> |
|||
|
|||
<TextBlock x:Name="PickerLastResultsVisible" |
|||
Classes="h2" |
|||
IsVisible="False" |
|||
Text="Last picker results:" /> |
|||
<ItemsPresenter x:Name="PickerLastResults" /> |
|||
<TextBlock Text="Windows:" /> |
|||
|
|||
<TextBlock Margin="0, 8, 0, 0" |
|||
Classes="h1" |
|||
Text="Window dialogs" /> |
|||
<Button Name="DecoratedWindow">Decorated _window</Button> |
|||
<Button Name="DecoratedWindowDialog">Decorated w_indow (dialog)</Button> |
|||
<Button Name="Dialog" ToolTip.Tip="Shows a dialog">_Dialog</Button> |
|||
<Button Name="DialogNoTaskbar">Dialog (_No taskbar icon)</Button> |
|||
<Button Name="OwnedWindow">Own_ed window</Button> |
|||
<Button Name="OwnedWindowNoTaskbar">Owned window (No tas_kbar icon)</Button> |
|||
<Expander Header="Window dialogs"> |
|||
<StackPanel Spacing="4"> |
|||
<Button Name="DecoratedWindow">Decorated _window</Button> |
|||
<Button Name="DecoratedWindowDialog">Decorated w_indow (dialog)</Button> |
|||
<Button Name="Dialog" ToolTip.Tip="Shows a dialog">_Dialog</Button> |
|||
<Button Name="DialogNoTaskbar">Dialog (_No taskbar icon)</Button> |
|||
<Button Name="OwnedWindow">Own_ed window</Button> |
|||
<Button Name="OwnedWindowNoTaskbar">Owned window (No tas_kbar icon)</Button> |
|||
</StackPanel> |
|||
</Expander> |
|||
|
|||
<TextBlock Margin="0,20,0,0" Text="Pickers:" /> |
|||
|
|||
<CheckBox Name="UseFilters">Use filters</CheckBox> |
|||
<Expander Header="FilePicker API"> |
|||
<StackPanel Spacing="4"> |
|||
<CheckBox Name="ForceManaged">Force managed dialog</CheckBox> |
|||
<CheckBox Name="OpenMultiple">Open multiple</CheckBox> |
|||
<Button Name="OpenFolderPicker">Select Fo_lder</Button> |
|||
<Button Name="OpenFilePicker">_Open File</Button> |
|||
<Button Name="SaveFilePicker">_Save File</Button> |
|||
<Button Name="OpenFileFromBookmark">Open File Bookmark</Button> |
|||
<Button Name="OpenFolderFromBookmark">Open Folder Bookmark</Button> |
|||
</StackPanel> |
|||
</Expander> |
|||
<Expander Header="Legacy OpenFileDialog"> |
|||
<StackPanel Spacing="4"> |
|||
<Button Name="OpenFile">_Open File</Button> |
|||
<Button Name="OpenMultipleFiles">Open _Multiple File</Button> |
|||
<Button Name="SaveFile">_Save File</Button> |
|||
<Button Name="SelectFolder">Select Fo_lder</Button> |
|||
<Button Name="OpenBoth">Select _Both</Button> |
|||
</StackPanel> |
|||
</Expander> |
|||
|
|||
<TextBlock x:Name="PickerLastResultsVisible" |
|||
Classes="h2" |
|||
IsVisible="False" |
|||
Text="Last picker results:" /> |
|||
<ItemsPresenter x:Name="PickerLastResults" /> |
|||
|
|||
<TextBox Name="BookmarkContainer" Watermark="Bookmark" /> |
|||
<TextBox Name="OpenedFileContent" |
|||
MaxLines="10" |
|||
Watermark="Picked file content" /> |
|||
|
|||
</StackPanel> |
|||
</UserControl> |
|||
|
|||
@ -0,0 +1,147 @@ |
|||
#nullable enable |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Platform.Storage; |
|||
using Avalonia.Platform.Storage.FileIO; |
|||
|
|||
namespace Avalonia.Dialogs; |
|||
|
|||
public class ManagedStorageProvider<T> : BclStorageProvider where T : Window, new() |
|||
{ |
|||
private readonly Window _parent; |
|||
private readonly ManagedFileDialogOptions _managedOptions; |
|||
|
|||
public ManagedStorageProvider(Window parent, ManagedFileDialogOptions? managedOptions) |
|||
{ |
|||
_parent = parent; |
|||
_managedOptions = managedOptions ?? new ManagedFileDialogOptions(); |
|||
} |
|||
|
|||
public override bool CanSave => true; |
|||
public override bool CanOpen => true; |
|||
public override bool CanPickFolder => true; |
|||
|
|||
public override async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options) |
|||
{ |
|||
var model = new ManagedFileChooserViewModel(options, _managedOptions); |
|||
var results = await Show(model, _parent); |
|||
|
|||
return results.Select(f => new BclStorageFile(new FileInfo(f))).ToArray(); |
|||
} |
|||
|
|||
public override async Task<IStorageFile?> SaveFilePickerAsync(FilePickerSaveOptions options) |
|||
{ |
|||
var model = new ManagedFileChooserViewModel(options, _managedOptions); |
|||
var results = await Show(model, _parent); |
|||
|
|||
return results.FirstOrDefault() is { } result |
|||
? new BclStorageFile(new FileInfo(result)) |
|||
: null; |
|||
} |
|||
|
|||
public override async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options) |
|||
{ |
|||
var model = new ManagedFileChooserViewModel(options, _managedOptions); |
|||
var results = await Show(model, _parent); |
|||
|
|||
return results.Select(f => new BclStorageFolder(new DirectoryInfo(f))).ToArray(); |
|||
} |
|||
|
|||
private async Task<string[]> Show(ManagedFileChooserViewModel model, Window parent) |
|||
{ |
|||
var dialog = new T |
|||
{ |
|||
Content = new ManagedFileChooser(), |
|||
Title = model.Title, |
|||
DataContext = model |
|||
}; |
|||
|
|||
dialog.Closed += delegate { model.Cancel(); }; |
|||
|
|||
string[]? result = null; |
|||
|
|||
model.CompleteRequested += items => |
|||
{ |
|||
result = items; |
|||
dialog.Close(); |
|||
}; |
|||
|
|||
model.OverwritePrompt += async (filename) => |
|||
{ |
|||
var overwritePromptDialog = new Window() |
|||
{ |
|||
Title = "Confirm Save As", |
|||
SizeToContent = SizeToContent.WidthAndHeight, |
|||
WindowStartupLocation = WindowStartupLocation.CenterOwner, |
|||
Padding = new Thickness(10), |
|||
MinWidth = 270 |
|||
}; |
|||
|
|||
string name = Path.GetFileName(filename); |
|||
|
|||
var panel = new DockPanel() |
|||
{ |
|||
HorizontalAlignment = Layout.HorizontalAlignment.Stretch |
|||
}; |
|||
|
|||
var label = new Label() |
|||
{ |
|||
Content = $"{name} already exists.\nDo you want to replace it?" |
|||
}; |
|||
|
|||
panel.Children.Add(label); |
|||
DockPanel.SetDock(label, Dock.Top); |
|||
|
|||
var buttonPanel = new StackPanel() |
|||
{ |
|||
HorizontalAlignment = Layout.HorizontalAlignment.Right, |
|||
Orientation = Layout.Orientation.Horizontal, |
|||
Spacing = 10 |
|||
}; |
|||
|
|||
var button = new Button() |
|||
{ |
|||
Content = "Yes", |
|||
HorizontalAlignment = Layout.HorizontalAlignment.Right |
|||
}; |
|||
|
|||
button.Click += (sender, args) => |
|||
{ |
|||
result = new string[1] { filename }; |
|||
overwritePromptDialog.Close(); |
|||
dialog.Close(); |
|||
}; |
|||
|
|||
buttonPanel.Children.Add(button); |
|||
|
|||
button = new Button() |
|||
{ |
|||
Content = "No", |
|||
HorizontalAlignment = Layout.HorizontalAlignment.Right |
|||
}; |
|||
|
|||
button.Click += (sender, args) => |
|||
{ |
|||
overwritePromptDialog.Close(); |
|||
}; |
|||
|
|||
buttonPanel.Children.Add(button); |
|||
|
|||
panel.Children.Add(buttonPanel); |
|||
DockPanel.SetDock(buttonPanel, Dock.Bottom); |
|||
|
|||
overwritePromptDialog.Content = panel; |
|||
|
|||
await overwritePromptDialog.ShowDialog(dialog); |
|||
}; |
|||
|
|||
model.CancelRequested += dialog.Close; |
|||
|
|||
await dialog.ShowDialog<object>(parent); |
|||
return result ?? Array.Empty<string>(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue