Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/SharpFileDialog.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ namespace SharpFileDialog.Sample
{
class Program
{
[STAThread]
static void Main(string[] args)
{
var dirDialog = new DirectoryDialog("Select a directory");
dirDialog.Open(result => OpenDirectory(result.FileName));
using (var dirDialog = new DirectoryDialog("Select a directory"))
dirDialog.Open(result => OpenDirectory(result.FileName));

Console.ReadKey();

var openDialog = new OpenFileDialog("This is a test dialog");
var filter = "Text files(*.txt) | *.txt | Png files(*.png) | *.png | All files(*.*) | *.*";
openDialog.Open(result => OpenFile(result.FileName), filter);
using (var openDialog = new OpenFileDialog("This is a test dialog"))
{
var filter = "Text files(*.txt) | *.txt | Png files(*.png) | *.png | All files(*.*) | *.*";
openDialog.Open(result => OpenFile(result.FileName), filter);
}

Console.ReadKey();

var saveDialog = new SaveFileDialog("Another test for saving");
filter = "Text files(*.txt) | *.txt | Png files(*.png) | *.png | All files(*.*) | *.*";
saveDialog.DefaultFileName = "filename.txt";
saveDialog.Save(result => SaveFile(result.FileName), filter);
using (var saveDialog = new SaveFileDialog("Another test for saving"))
{
var filter = "Text files(*.txt) | *.txt | Png files(*.png) | *.png | All files(*.*) | *.*";
saveDialog.DefaultFileName = "filename.txt";
saveDialog.Save(result => SaveFile(result.FileName), filter);
}

Console.ReadKey();
}
Expand All @@ -39,6 +44,5 @@ private static void SaveFile(string filename)
{
Console.WriteLine("Saving file: {0}", filename);
}

}
}
10 changes: 4 additions & 6 deletions src/SharpFileDialog.Sample/SharpFileDialog.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\SharpFileDialog\SharpFileDialog.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SharpFileDialog\SharpFileDialog.csproj" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/SharpFileDialog/DialogResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
namespace SharpFileDialog
{
/// <summary>
/// The result of a file/directory selection dialog
/// </summary>
public struct DialogResult
{
/// <summary>
/// The path to the file or directory chosen
/// </summary>
public string FileName { get; set; }

/// <summary>
/// True if a file or directory was chosen, false if the dialog was cancelled or an error was encountered.
/// </summary>
public bool Success { get; set; }
}
}
19 changes: 17 additions & 2 deletions src/SharpFileDialog/DirectoryDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

namespace SharpFileDialog
{

/// <summary>
/// Represents a dialog for selecting directories.
/// </summary>
public class DirectoryDialog : IDirectoryDialogBackend
{
private IDirectoryDialogBackend _backend;
private readonly IDirectoryDialogBackend _backend;

/// <summary>
/// Initializes a new instance of the <see cref="DirectoryDialog"/> class.
/// On Windows, the native dialog is used (if a hang is experienced, ensure your Main method has an [STAThread] attribute).
/// On Linux, Zenity is used if available, otherwise Gtk is used.
/// </summary>
/// <param name="title">The title of the dialog.</param>
public DirectoryDialog(string title = null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -27,11 +35,18 @@ public DirectoryDialog(string title = null)
}
}

/// <summary>
/// Releases all resources used by the <see cref="DirectoryDialog"/>.
/// </summary>
public void Dispose()
{
_backend.Dispose();
}

/// <summary>
/// Opens the directory dialog.
/// </summary>
/// <param name="callback">The callback to be invoked when the dialog is closed.</param>
public void Open(Action<DialogResult> callback)
{
_backend.Open(callback);
Expand Down
5 changes: 2 additions & 3 deletions src/SharpFileDialog/Gtk/GtkDirectoryDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

namespace SharpFileDialog.Gtk
{

internal class GtkDirectoryDialog : IDirectoryDialogBackend
{
private FileChooserDialog _dialog;
private readonly FileChooserDialog _dialog;

public GtkDirectoryDialog(string title)
{
Expand All @@ -33,7 +32,7 @@ public void Open(Action<DialogResult> callback)
{
if (_dialog.Run() == (int)ResponseType.Ok)
{
callback(new DialogResult()
callback(new DialogResult
{
FileName = _dialog.Filename,
Success = true
Expand Down
5 changes: 2 additions & 3 deletions src/SharpFileDialog/Gtk/GtkOpenFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

namespace SharpFileDialog.Gtk
{

internal class GtkOpenFileDialog : IOpenFileDialogBackend
{
private FileChooserDialog _dialog;
private readonly FileChooserDialog _dialog;

public GtkOpenFileDialog(string title)
{
Expand Down Expand Up @@ -42,7 +41,7 @@ public void Open(Action<DialogResult> callback, string filterString)

if (_dialog.Run() == (int)ResponseType.Ok)
{
callback(new DialogResult()
callback(new DialogResult
{
FileName = _dialog.Filename,
Success = true
Expand Down
10 changes: 3 additions & 7 deletions src/SharpFileDialog/Gtk/GtkSaveFileDialog.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using System.Threading.Tasks;
using Gtk;

namespace SharpFileDialog.Gtk
{
internal class GtkSaveFileDialog : ISaveFileDialogBackend
{
private FileChooserDialog _dialog;
private readonly FileChooserDialog _dialog;

public string DefaultFileName
{
set
{
_dialog.CurrentName = value;
}
set => _dialog.CurrentName = value;
}

public GtkSaveFileDialog(string title)
Expand Down Expand Up @@ -50,7 +46,7 @@ public void Save(Action<DialogResult> callback, string filterString)

if (_dialog.Run() == (int)ResponseType.Ok)
{
callback(new DialogResult()
callback(new DialogResult
{
FileName = _dialog.Filename,
Success = true
Expand Down
2 changes: 1 addition & 1 deletion src/SharpFileDialog/Gtk/GtkUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Filter
public string Pattern;
}

static internal class GtkUtil
internal static class GtkUtil
{
static bool _initialized = false;

Expand Down
2 changes: 1 addition & 1 deletion src/SharpFileDialog/ISaveFileDialogBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SharpFileDialog
{
interface ISaveFileDialogBackend : IDisposable
internal interface ISaveFileDialogBackend : IDisposable
{
string DefaultFileName { set; }
void Save(Action<DialogResult> callback, string filter);
Expand Down
20 changes: 18 additions & 2 deletions src/SharpFileDialog/OpenFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

namespace SharpFileDialog
{

/// <summary>
/// Represents a dialog for opening files.
/// </summary>
public class OpenFileDialog : IOpenFileDialogBackend
{
private IOpenFileDialogBackend _backend;
private readonly IOpenFileDialogBackend _backend;

/// <summary>
/// Initializes a new instance of the <see cref="OpenFileDialog"/> class.
/// On Windows, the native dialog is used.
/// On Linux, Zenity is used if available, otherwise Gtk is used.
/// </summary>
/// <param name="title">The title of the dialog.</param>
public OpenFileDialog(string title = null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -27,11 +35,19 @@ public OpenFileDialog(string title = null)
}
}

/// <summary>
/// Releases all resources used by the <see cref="OpenFileDialog"/>.
/// </summary>
public void Dispose()
{
_backend.Dispose();
}

/// <summary>
/// Opens the file dialog.
/// </summary>
/// <param name="callback">The callback to be invoked when the dialog is closed.</param>
/// <param name="filter">The filter for the types of files to be displayed.</param>
public void Open(Action<DialogResult> callback, string filter = "All files(*.*) | *.*")
{
_backend.Open(callback, filter);
Expand Down
28 changes: 23 additions & 5 deletions src/SharpFileDialog/SaveFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@

namespace SharpFileDialog
{
/// <summary>
/// Represents a dialog for saving files.
/// </summary>
public class SaveFileDialog : ISaveFileDialogBackend
{
private ISaveFileDialogBackend _backend;
private readonly ISaveFileDialogBackend _backend;

/// <summary>
/// Gets or sets the default file name.
/// </summary>
public string DefaultFileName
{
set
{
_backend.DefaultFileName = value;
}
set => _backend.DefaultFileName = value;
}

/// <summary>
/// Initializes a new instance of the <see cref="SaveFileDialog"/> class.
/// On Windows, the native dialog is used.
/// On Linux, Zenity is used if available, otherwise Gtk is used.
/// </summary>
/// <param name="title">The title of the dialog.</param>
public SaveFileDialog(string title = null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -33,11 +43,19 @@ public SaveFileDialog(string title = null)
}
}

/// <summary>
/// Releases all resources used by the <see cref="SaveFileDialog"/>.
/// </summary>
public void Dispose()
{
_backend.Dispose();
}

/// <summary>
/// Opens the save file dialog.
/// </summary>
/// <param name="callback">The callback to be invoked when the dialog is closed.</param>
/// <param name="filter">The filter for the types of files to be displayed.</param>
public void Save(Action<DialogResult> callback, string filter = "All files(*.*) | *.*")
{
_backend.Save(callback, filter);
Expand Down
4 changes: 2 additions & 2 deletions src/SharpFileDialog/SharpFileDialog.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>bin/SharpAudio.xml</DocumentationFile>
<DocumentationFile>bin/$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading