Browse Source

Merge pull request #6160 from workgroupengineering/fixes/Issue_6033

fixes: Dialog cancel consistency on windows
release/0.10.9
Max Katz 5 years ago
committed by Dan Walmsley
parent
commit
b30b179571
  1. 99
      src/Avalonia.Controls/SystemDialog.cs
  2. 6
      src/Windows/Avalonia.Win32/SystemDialogImpl.cs

99
src/Avalonia.Controls/SystemDialog.cs

@ -4,30 +4,65 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls.Platform; using Avalonia.Controls.Platform;
#nullable enable
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
/// <summary>
/// Base class for system file dialogs.
/// </summary>
public abstract class FileDialog : FileSystemDialog public abstract class FileDialog : FileSystemDialog
{ {
/// <summary>
/// Gets or sets a collection of filters which determine the types of files displayed in an
/// <see cref="OpenFileDialog"/> or an <see cref="SaveFileDialog"/>.
/// </summary>
public List<FileDialogFilter> Filters { get; set; } = new List<FileDialogFilter>(); public List<FileDialogFilter> Filters { get; set; } = new List<FileDialogFilter>();
public string InitialFileName { get; set; }
/// <summary>
/// Gets or sets initial file name that is displayed when the dialog is opened.
/// </summary>
public string? InitialFileName { get; set; }
} }
/// <summary>
/// Base class for system file and directory dialogs.
/// </summary>
public abstract class FileSystemDialog : SystemDialog public abstract class FileSystemDialog : SystemDialog
{ {
[Obsolete("Use Directory")] [Obsolete("Use Directory")]
public string InitialDirectory public string? InitialDirectory
{ {
get => Directory; get => Directory;
set => Directory = value; set => Directory = value;
} }
public string Directory { get; set; }
/// <summary>
/// Gets or sets the initial directory that will be displayed when the file system dialog
/// is opened.
/// </summary>
public string? Directory { get; set; }
} }
/// <summary>
/// Represents a system dialog that prompts the user to select a location for saving a file.
/// </summary>
public class SaveFileDialog : FileDialog public class SaveFileDialog : FileDialog
{ {
public string DefaultExtension { get; set; } /// <summary>
/// Gets or sets the default extension to be used to save the file (including the period ".").
/// </summary>
public string? DefaultExtension { get; set; }
public async Task<string> ShowAsync(Window parent) /// <summary>
/// Shows the save file dialog.
/// </summary>
/// <param name="parent">The parent window.</param>
/// <returns>
/// A task that on completion contains the full path of the save location, or null if the
/// dialog was canceled.
/// </returns>
public async Task<string?> ShowAsync(Window parent)
{ {
if(parent == null) if(parent == null)
throw new ArgumentNullException(nameof(parent)); throw new ArgumentNullException(nameof(parent));
@ -37,11 +72,25 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// Represents a system dialog that allows the user to select one or more files to open.
/// </summary>
public class OpenFileDialog : FileDialog public class OpenFileDialog : FileDialog
{ {
/// <summary>
/// Gets or sets a value indicating whether the user can select multiple files.
/// </summary>
public bool AllowMultiple { get; set; } public bool AllowMultiple { get; set; }
public Task<string[]> ShowAsync(Window parent) /// <summary>
/// Shows the open file dialog.
/// </summary>
/// <param name="parent">The parent window.</param>
/// <returns>
/// A task that on completion returns an array containing the full path to the selected
/// files, or null if the dialog was canceled.
/// </returns>
public Task<string[]?> ShowAsync(Window parent)
{ {
if(parent == null) if(parent == null)
throw new ArgumentNullException(nameof(parent)); throw new ArgumentNullException(nameof(parent));
@ -49,15 +98,27 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// Represents a system dialog that allows the user to select a directory.
/// </summary>
public class OpenFolderDialog : FileSystemDialog public class OpenFolderDialog : FileSystemDialog
{ {
[Obsolete("Use Directory")] [Obsolete("Use Directory")]
public string DefaultDirectory public string? DefaultDirectory
{ {
get => Directory; get => Directory;
set => Directory = value; set => Directory = value;
} }
public Task<string> ShowAsync(Window parent)
/// <summary>
/// Shows the open folder dialog.
/// </summary>
/// <param name="parent">The parent window.</param>
/// <returns>
/// A task that on completion returns the full path of the selected directory, or null if the
/// dialog was canceled.
/// </returns>
public Task<string?> ShowAsync(Window parent)
{ {
if(parent == null) if(parent == null)
throw new ArgumentNullException(nameof(parent)); throw new ArgumentNullException(nameof(parent));
@ -65,14 +126,32 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// Base class for system dialogs.
/// </summary>
public abstract class SystemDialog public abstract class SystemDialog
{ {
public string Title { get; set; } /// <summary>
/// Gets or sets the dialog title.
/// </summary>
public string? Title { get; set; }
} }
/// <summary>
/// Represents a filter in an <see cref="OpenFileDialog"/> or an <see cref="SaveFileDialog"/>.
/// </summary>
public class FileDialogFilter public class FileDialogFilter
{ {
public string Name { get; set; } /// <summary>
/// Gets or sets the name of the filter, e.g. ("Text files (.txt)").
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Gets or sets a list of file extensions matched by the filter (e.g. "txt" or "*" for all
/// files).
/// </summary>
public List<string> Extensions { get; set; } = new List<string>(); public List<string> Extensions { get; set; } = new List<string>();
} }
} }

6
src/Windows/Avalonia.Win32/SystemDialogImpl.cs

@ -19,7 +19,7 @@ namespace Avalonia.Win32
var hWnd = parent?.PlatformImpl?.Handle?.Handle ?? IntPtr.Zero; var hWnd = parent?.PlatformImpl?.Handle?.Handle ?? IntPtr.Zero;
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
{ {
var result = Array.Empty<string>(); string[] result = default;
Guid clsid = dialog is OpenFileDialog ? UnmanagedMethods.ShellIds.OpenFileDialog : UnmanagedMethods.ShellIds.SaveFileDialog; Guid clsid = dialog is OpenFileDialog ? UnmanagedMethods.ShellIds.OpenFileDialog : UnmanagedMethods.ShellIds.SaveFileDialog;
Guid iid = UnmanagedMethods.ShellIds.IFileDialog; Guid iid = UnmanagedMethods.ShellIds.IFileDialog;
@ -100,7 +100,7 @@ namespace Avalonia.Win32
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
{ {
string result = string.Empty; string result = default;
var hWnd = parent?.PlatformImpl?.Handle?.Handle ?? IntPtr.Zero; var hWnd = parent?.PlatformImpl?.Handle?.Handle ?? IntPtr.Zero;
Guid clsid = UnmanagedMethods.ShellIds.OpenFileDialog; Guid clsid = UnmanagedMethods.ShellIds.OpenFileDialog;
@ -164,7 +164,7 @@ namespace Avalonia.Win32
} }
} }
} }
return ""; return default;
} }
} }
} }

Loading…
Cancel
Save