Skip to content

Commit

Permalink
Make FileDialog.FileName consistent across platforms
Browse files Browse the repository at this point in the history
Mac: Fix setting FileName to null
Mac/Gtk: Remember set value before/after dialog is shown
Added test to ensure things work as expected
  • Loading branch information
cwensley committed Sep 14, 2022
1 parent 31a4342 commit f793d8f
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 29 deletions.
24 changes: 18 additions & 6 deletions src/Eto.Gtk/Forms/GtkFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ public abstract class GtkFileDialog<TControl, TWidget> : WidgetHandler<TControl,
where TControl: Gtk.FileChooserDialog
where TWidget: FileDialog
{
public string FileName
public virtual string FileName
{
get { return Control.Filename; }
get { return Control.Filename ?? Control.CurrentName; }
set
{
Control.SetCurrentFolder(Path.GetDirectoryName(value));
Control.SetFilename(value);
Control.CurrentName = Path.GetFileName(value);
if (string.IsNullOrEmpty(value))
{
Control.UnselectAll();
if (!string.IsNullOrEmpty(Control.Filename))
Control.UnselectFilename(Control.Filename);
}
else
{
var dir = Path.GetDirectoryName(value);
if (!string.IsNullOrEmpty(dir))
Control.SetCurrentFolder(dir);
if (File.Exists(value))
Control.SetFilename(value);
}
Control.CurrentName = Path.GetFileName(value) ?? string.Empty;
}
}

Expand Down Expand Up @@ -79,7 +91,7 @@ public string Title
}


public DialogResult ShowDialog(Window parent)
public virtual DialogResult ShowDialog(Window parent)
{
SetFilters();
if (parent != null) Control.TransientFor = (Gtk.Window)parent.ControlObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using Eto.Forms;
using System.Collections.Generic;
using System.IO;

namespace Eto.GtkSharp.Forms
{
public class OpenFileDialogHandler : GtkFileDialog<Gtk.FileChooserDialog, OpenFileDialog>, OpenFileDialog.IHandler
{
string fileName;

public OpenFileDialogHandler()
{
Control = new Gtk.FileChooserDialog(string.Empty, null, Gtk.FileChooserAction.Open);
Control.SetCurrentFolder(System.IO.Directory.GetCurrentDirectory());

Control.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
Control.AddButton(Gtk.Stock.Open, Gtk.ResponseType.Ok);
Control.DefaultResponse = Gtk.ResponseType.Ok;
Expand All @@ -18,13 +21,32 @@ public OpenFileDialogHandler()
public bool MultiSelect
{
get { return Control.SelectMultiple; }
set { Control.SelectMultiple = value; }
set { Control.SelectMultiple = value; }
}

public IEnumerable<string> Filenames
{
get { return Control.Filenames; }
}

public override string FileName
{
get => base.FileName ?? fileName;
set => base.FileName = fileName = value;
}

public override DialogResult ShowDialog(Window parent)
{
var result = base.ShowDialog(parent);

// When cancelling, Control.Filename all of the sudden returns a value but is just the folder.
// so, combine it with the desired file name, if one was set.
if (result == DialogResult.Ok)
fileName = null;
else if (!string.IsNullOrEmpty(fileName) && string.IsNullOrEmpty(Path.GetDirectoryName(fileName)))
Control.SetFilename(Path.Combine(base.FileName, fileName));
return result;
}

}
}
File renamed without changes.
28 changes: 12 additions & 16 deletions src/Eto.Mac/Forms/MacFileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,18 @@ void Create()
Control.AccessoryView = null;
}

string fileName;

public virtual string FileName
{
get
{
return Control.Url.Path;
}
set { }
get => Control.Url?.Path ?? fileName;
set => fileName = value;
}

public Uri Directory
{
get
{
return new Uri(Control.DirectoryUrl.AbsoluteString);
}
set
{
Control.DirectoryUrl = new NSUrl(value.AbsoluteUri);
}
get => new Uri(Control.DirectoryUrl.AbsoluteString);
set => Control.DirectoryUrl = new NSUrl(value.AbsoluteUri);
}

public string GetDefaultExtension()
Expand Down Expand Up @@ -173,15 +166,18 @@ public string Title
get { return Control.Message; }
set { Control.Message = value ?? string.Empty; }
}

public DialogResult ShowDialog(Window parent)
public virtual DialogResult ShowDialog(Window parent)
{
//Control.AllowsOtherFileTypes = false;
Control.Delegate = new SavePanelDelegate{ Handler = this };
Create();

int ret = MacModal.Run(Control, parent);


if (ret == 1)
fileName = null;

return ret == 1 ? DialogResult.Ok : DialogResult.Cancel;
}

Expand Down
File renamed without changes.
19 changes: 14 additions & 5 deletions src/Eto.Mac/Forms/SaveFileDialogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Eto.Mac.Forms
{
public class SaveFileDialogHandler : MacFileDialog<NSSavePanel, SaveFileDialog>, SaveFileDialog.IHandler
{
bool hasShown;

public override string FileName
{
get
{
return base.FileName;
}
get => hasShown ? base.FileName : Control.NameFieldStringValue;
set
{
Control.NameFieldStringValue = value;
Control.NameFieldStringValue = value ?? string.Empty;
hasShown = false;
}
}

Expand All @@ -30,5 +30,14 @@ protected override void Initialize()
Control.CanSelectHiddenExtension = true;
base.Initialize();
}

public override DialogResult ShowDialog(Window parent)
{
hasShown = true;
var result = base.ShowDialog(parent);
if (result != DialogResult.Ok)
hasShown = false;
return result;
}
}
}
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions test/Eto.Test/UnitTests/Forms/FileDialogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Eto.Drawing;
using Eto.Forms;
using NUnit.Framework;

namespace Eto.Test.UnitTests.Forms
{
[TestFixture]
public class OpenFileDialogTests : FileDialogTests<OpenFileDialog>
{
}

[TestFixture]
public class SaveFileDialogTests : FileDialogTests<SaveFileDialog>
{
}

public class FileDialogTests<T> : TestBase
where T: FileDialog, new()
{
[Test, InvokeOnUI, ManualTest]
public void FileNameShouldHaveConsistentValues()
{
var fd = new T();
fd.Filters.Add(new FileFilter("Text Files (*.txt)", ".txt"));

Assert.That(fd.FileName, Is.Null.Or.Empty.Or.EqualTo("Untitled"), "#1");

fd.FileName = null;
Assert.That(fd.FileName, Is.Null.Or.Empty.Or.EqualTo("Untitled"), "#2");

fd.FileName = "SomeFile.txt";

Assert.AreEqual("SomeFile.txt", Path.GetFileName(fd.FileName), "#3");

var result = fd.ShowDialog(null);

if (result == DialogResult.Cancel || typeof(T) == typeof(SaveFileDialog))
Assert.AreEqual("SomeFile.txt", Path.GetFileName(fd.FileName), "#4");

if (result == DialogResult.Ok)
{
var directoryName = Path.GetDirectoryName(fd.FileName);
Assert.IsNotNull(directoryName, "#5.1");
Assert.IsNotEmpty(directoryName, "#5.2");
Console.WriteLine($"Directory: {directoryName}");
}

Console.WriteLine($"FileName: {fd.FileName}");

}
}
}

0 comments on commit f793d8f

Please sign in to comment.