|
|
@@ -1,13 +1,21 @@
|
|
|
using System;
|
|
|
+using System.Buffers;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Avalonia;
|
|
|
using Avalonia.Controls;
|
|
|
using Avalonia.Controls.Presenters;
|
|
|
using Avalonia.Dialogs;
|
|
|
using Avalonia.Layout;
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
-#pragma warning disable 4014
|
|
|
+using Avalonia.Platform.Storage;
|
|
|
+using Avalonia.Platform.Storage.FileIO;
|
|
|
+
|
|
|
+#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
+#nullable enable
|
|
|
+
|
|
|
namespace ControlCatalog.Pages
|
|
|
{
|
|
|
public class DialogsPage : UserControl
|
|
|
@@ -18,13 +26,16 @@ namespace ControlCatalog.Pages
|
|
|
|
|
|
var results = this.Get<ItemsPresenter>("PickerLastResults");
|
|
|
var resultsVisible = this.Get<TextBlock>("PickerLastResultsVisible");
|
|
|
+ var bookmarkContainer = this.Get<TextBox>("BookmarkContainer");
|
|
|
+ var openedFileContent = this.Get<TextBox>("OpenedFileContent");
|
|
|
+ var openMultiple = this.Get<CheckBox>("OpenMultiple");
|
|
|
|
|
|
- string? lastSelectedDirectory = null;
|
|
|
+ IStorageFolder? lastSelectedDirectory = null;
|
|
|
|
|
|
- List<FileDialogFilter>? GetFilters()
|
|
|
+ List<FileDialogFilter> GetFilters()
|
|
|
{
|
|
|
if (this.Get<CheckBox>("UseFilters").IsChecked != true)
|
|
|
- return null;
|
|
|
+ return new List<FileDialogFilter>();
|
|
|
return new List<FileDialogFilter>
|
|
|
{
|
|
|
new FileDialogFilter
|
|
|
@@ -39,12 +50,23 @@ namespace ControlCatalog.Pages
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ List<FilePickerFileType>? GetFileTypes()
|
|
|
+ {
|
|
|
+ if (this.Get<CheckBox>("UseFilters").IsChecked != true)
|
|
|
+ return null;
|
|
|
+ return new List<FilePickerFileType>
|
|
|
+ {
|
|
|
+ FilePickerFileTypes.All,
|
|
|
+ FilePickerFileTypes.TextPlain
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
this.Get<Button>("OpenFile").Click += async delegate
|
|
|
{
|
|
|
// Almost guaranteed to exist
|
|
|
- var fullPath = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName;
|
|
|
- var initialFileName = fullPath == null ? null : System.IO.Path.GetFileName(fullPath);
|
|
|
- var initialDirectory = fullPath == null ? null : System.IO.Path.GetDirectoryName(fullPath);
|
|
|
+ var uri = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName;
|
|
|
+ var initialFileName = uri == null ? null : System.IO.Path.GetFileName(uri);
|
|
|
+ var initialDirectory = uri == null ? null : System.IO.Path.GetDirectoryName(uri);
|
|
|
|
|
|
var result = await new OpenFileDialog()
|
|
|
{
|
|
|
@@ -62,7 +84,7 @@ namespace ControlCatalog.Pages
|
|
|
{
|
|
|
Title = "Open multiple files",
|
|
|
Filters = GetFilters(),
|
|
|
- Directory = lastSelectedDirectory,
|
|
|
+ Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
|
|
|
AllowMultiple = true
|
|
|
}.ShowAsync(GetWindow());
|
|
|
results.Items = result;
|
|
|
@@ -70,11 +92,13 @@ namespace ControlCatalog.Pages
|
|
|
};
|
|
|
this.Get<Button>("SaveFile").Click += async delegate
|
|
|
{
|
|
|
+ var filters = GetFilters();
|
|
|
var result = await new SaveFileDialog()
|
|
|
{
|
|
|
Title = "Save file",
|
|
|
- Filters = GetFilters(),
|
|
|
- Directory = lastSelectedDirectory,
|
|
|
+ Filters = filters,
|
|
|
+ Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
|
|
|
+ DefaultExtension = filters?.Any() == true ? "txt" : null,
|
|
|
InitialFileName = "test.txt"
|
|
|
}.ShowAsync(GetWindow());
|
|
|
results.Items = new[] { result };
|
|
|
@@ -85,14 +109,9 @@ namespace ControlCatalog.Pages
|
|
|
var result = await new OpenFolderDialog()
|
|
|
{
|
|
|
Title = "Select folder",
|
|
|
- Directory = lastSelectedDirectory,
|
|
|
+ Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null
|
|
|
}.ShowAsync(GetWindow());
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(result))
|
|
|
- {
|
|
|
- lastSelectedDirectory = result;
|
|
|
- }
|
|
|
-
|
|
|
+ lastSelectedDirectory = new BclStorageFolder(new System.IO.DirectoryInfo(result));
|
|
|
results.Items = new [] { result };
|
|
|
resultsVisible.IsVisible = result != null;
|
|
|
};
|
|
|
@@ -101,7 +120,7 @@ namespace ControlCatalog.Pages
|
|
|
var result = await new OpenFileDialog()
|
|
|
{
|
|
|
Title = "Select both",
|
|
|
- Directory = lastSelectedDirectory,
|
|
|
+ Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
|
|
|
AllowMultiple = true
|
|
|
}.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
|
|
|
{
|
|
|
@@ -116,20 +135,20 @@ namespace ControlCatalog.Pages
|
|
|
};
|
|
|
this.Get<Button>("DecoratedWindowDialog").Click += delegate
|
|
|
{
|
|
|
- new DecoratedWindow().ShowDialog(GetWindow());
|
|
|
+ _ = new DecoratedWindow().ShowDialog(GetWindow());
|
|
|
};
|
|
|
this.Get<Button>("Dialog").Click += delegate
|
|
|
{
|
|
|
var window = CreateSampleWindow();
|
|
|
window.Height = 200;
|
|
|
- window.ShowDialog(GetWindow());
|
|
|
+ _ = window.ShowDialog(GetWindow());
|
|
|
};
|
|
|
this.Get<Button>("DialogNoTaskbar").Click += delegate
|
|
|
{
|
|
|
var window = CreateSampleWindow();
|
|
|
window.Height = 200;
|
|
|
window.ShowInTaskbar = false;
|
|
|
- window.ShowDialog(GetWindow());
|
|
|
+ _ = window.ShowDialog(GetWindow());
|
|
|
};
|
|
|
this.Get<Button>("OwnedWindow").Click += delegate
|
|
|
{
|
|
|
@@ -146,13 +165,166 @@ namespace ControlCatalog.Pages
|
|
|
|
|
|
window.Show(GetWindow());
|
|
|
};
|
|
|
+
|
|
|
+ this.Get<Button>("OpenFilePicker").Click += async delegate
|
|
|
+ {
|
|
|
+ var result = await GetStorageProvider().OpenFilePickerAsync(new FilePickerOpenOptions()
|
|
|
+ {
|
|
|
+ Title = "Open file",
|
|
|
+ FileTypeFilter = GetFileTypes(),
|
|
|
+ SuggestedStartLocation = lastSelectedDirectory,
|
|
|
+ AllowMultiple = openMultiple.IsChecked == true
|
|
|
+ });
|
|
|
+
|
|
|
+ await SetPickerResult(result);
|
|
|
+ };
|
|
|
+ this.Get<Button>("SaveFilePicker").Click += async delegate
|
|
|
+ {
|
|
|
+ var fileTypes = GetFileTypes();
|
|
|
+ var file = await GetStorageProvider().SaveFilePickerAsync(new FilePickerSaveOptions()
|
|
|
+ {
|
|
|
+ Title = "Save file",
|
|
|
+ FileTypeChoices = fileTypes,
|
|
|
+ SuggestedStartLocation = lastSelectedDirectory,
|
|
|
+ SuggestedFileName = "FileName",
|
|
|
+ DefaultExtension = fileTypes?.Any() == true ? "txt" : null,
|
|
|
+ ShowOverwritePrompt = false
|
|
|
+ });
|
|
|
+
|
|
|
+ if (file is not null && file.CanOpenWrite)
|
|
|
+ {
|
|
|
+ // Sync disposal of StreamWriter is not supported on WASM
|
|
|
+#if NET6_0_OR_GREATER
|
|
|
+ await using var stream = await file.OpenWrite();
|
|
|
+ await using var reader = new System.IO.StreamWriter(stream);
|
|
|
+#else
|
|
|
+ using var stream = await file.OpenWrite();
|
|
|
+ using var reader = new System.IO.StreamWriter(stream);
|
|
|
+#endif
|
|
|
+ await reader.WriteLineAsync(openedFileContent.Text);
|
|
|
+
|
|
|
+ lastSelectedDirectory = await file.GetParentAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ await SetPickerResult(file is null ? null : new [] {file});
|
|
|
+ };
|
|
|
+ this.Get<Button>("OpenFolderPicker").Click += async delegate
|
|
|
+ {
|
|
|
+ var folders = await GetStorageProvider().OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
|
|
+ {
|
|
|
+ Title = "Folder file",
|
|
|
+ SuggestedStartLocation = lastSelectedDirectory,
|
|
|
+ AllowMultiple = openMultiple.IsChecked == true
|
|
|
+ });
|
|
|
+
|
|
|
+ await SetPickerResult(folders);
|
|
|
+
|
|
|
+ lastSelectedDirectory = folders.FirstOrDefault();
|
|
|
+ };
|
|
|
+ this.Get<Button>("OpenFileFromBookmark").Click += async delegate
|
|
|
+ {
|
|
|
+ var file = bookmarkContainer.Text is not null
|
|
|
+ ? await GetStorageProvider().OpenFileBookmarkAsync(bookmarkContainer.Text)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ await SetPickerResult(file is null ? null : new[] { file });
|
|
|
+ };
|
|
|
+ this.Get<Button>("OpenFolderFromBookmark").Click += async delegate
|
|
|
+ {
|
|
|
+ var folder = bookmarkContainer.Text is not null
|
|
|
+ ? await GetStorageProvider().OpenFolderBookmarkAsync(bookmarkContainer.Text)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ await SetPickerResult(folder is null ? null : new[] { folder });
|
|
|
+
|
|
|
+ lastSelectedDirectory = folder;
|
|
|
+ };
|
|
|
+
|
|
|
+ async Task SetPickerResult(IReadOnlyCollection<IStorageItem>? items)
|
|
|
+ {
|
|
|
+ items ??= Array.Empty<IStorageItem>();
|
|
|
+ var mappedResults = items.Select(FullPathOrName).ToList();
|
|
|
+ bookmarkContainer.Text = items.FirstOrDefault(f => f.CanBookmark) is { } f ? await f.SaveBookmark() : "Can't bookmark";
|
|
|
+
|
|
|
+ if (items.FirstOrDefault() is IStorageItem item)
|
|
|
+ {
|
|
|
+ var resultText = item is IStorageFile ? "File:" : "Folder:";
|
|
|
+ resultText += Environment.NewLine;
|
|
|
+
|
|
|
+ var props = await item.GetBasicPropertiesAsync();
|
|
|
+ resultText += @$"Size: {props.Size}
|
|
|
+DateCreated: {props.DateCreated}
|
|
|
+DateModified: {props.DateModified}
|
|
|
+CanBookmark: {item.CanBookmark}
|
|
|
+";
|
|
|
+ if (item is IStorageFile file)
|
|
|
+ {
|
|
|
+ resultText += @$"
|
|
|
+CanOpenRead: {file.CanOpenRead}
|
|
|
+CanOpenWrite: {file.CanOpenWrite}
|
|
|
+Content:
|
|
|
+";
|
|
|
+ if (file.CanOpenRead)
|
|
|
+ {
|
|
|
+#if NET6_0_OR_GREATER
|
|
|
+ await using var stream = await file.OpenRead();
|
|
|
+#else
|
|
|
+ using var stream = await file.OpenRead();
|
|
|
+#endif
|
|
|
+ using var reader = new System.IO.StreamReader(stream);
|
|
|
+
|
|
|
+ // 4GB file test, shouldn't load more than 10000 chars into a memory.
|
|
|
+ const int length = 10000;
|
|
|
+ var buffer = ArrayPool<char>.Shared.Rent(length);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var charsRead = await reader.ReadAsync(buffer, 0, length);
|
|
|
+ resultText += new string(buffer, 0, charsRead);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ ArrayPool<char>.Shared.Return(buffer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ openedFileContent.Text = resultText;
|
|
|
+
|
|
|
+ lastSelectedDirectory = await item.GetParentAsync();
|
|
|
+ if (lastSelectedDirectory is not null)
|
|
|
+ {
|
|
|
+ mappedResults.Insert(0, "Parent: " + FullPathOrName(lastSelectedDirectory));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ results.Items = mappedResults;
|
|
|
+ resultsVisible.IsVisible = mappedResults.Any();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
+ {
|
|
|
+ base.OnAttachedToVisualTree(e);
|
|
|
+
|
|
|
+ var openedFileContent = this.Get<TextBox>("OpenedFileContent");
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var storageProvider = GetStorageProvider();
|
|
|
+ openedFileContent.Text = $@"CanOpen: {storageProvider.CanOpen}
|
|
|
+CanSave: {storageProvider.CanSave}
|
|
|
+CanPickFolder: {storageProvider.CanPickFolder}";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ openedFileContent.Text = "Storage provider is not available: " + ex.Message;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private Window CreateSampleWindow()
|
|
|
{
|
|
|
Button button;
|
|
|
Button dialogButton;
|
|
|
-
|
|
|
+
|
|
|
var window = new Window
|
|
|
{
|
|
|
Height = 200,
|
|
|
@@ -191,7 +363,22 @@ namespace ControlCatalog.Pages
|
|
|
return window;
|
|
|
}
|
|
|
|
|
|
- Window GetWindow() => this.VisualRoot as Window ?? throw new NullReferenceException("Invalid Owner");
|
|
|
+ private IStorageProvider GetStorageProvider()
|
|
|
+ {
|
|
|
+ var forceManaged = this.Get<CheckBox>("ForceManaged").IsChecked ?? false;
|
|
|
+ return forceManaged
|
|
|
+ ? new ManagedStorageProvider<Window>(GetWindow(), null)
|
|
|
+ : GetTopLevel().StorageProvider;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string FullPathOrName(IStorageItem? item)
|
|
|
+ {
|
|
|
+ if (item is null) return "(null)";
|
|
|
+ return item.TryGetUri(out var uri) ? uri.ToString() : item.Name;
|
|
|
+ }
|
|
|
+
|
|
|
+ Window GetWindow() => this.VisualRoot as Window ?? throw new NullReferenceException("Invalid Owner");
|
|
|
+ TopLevel GetTopLevel() => this.VisualRoot as TopLevel ?? throw new NullReferenceException("Invalid Owner");
|
|
|
|
|
|
private void InitializeComponent()
|
|
|
{
|
|
|
@@ -199,3 +386,4 @@ namespace ControlCatalog.Pages
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+#pragma warning restore CS0618 // Type or member is obsolete
|