Merge pull request #845 from BDisp/file-dialog-directory-fix

Fixes #844. Now selects the correct Directory.
This commit is contained in:
Charlie Kindel
2020-08-07 05:58:07 -07:00
committed by GitHub
2 changed files with 39 additions and 15 deletions

View File

@@ -305,7 +305,7 @@ static class Demo {
Application.Run (d); Application.Run (d);
if (!d.Canceled) if (!d.Canceled)
MessageBox.Query (50, 7, "Selected File", string.Join (", ", d.FilePaths), "Ok"); MessageBox.Query (50, 7, "Selected File", d.FilePaths.Count > 0 ? string.Join (", ", d.FilePaths) : d.FilePath, "Ok");
} }
public static void ShowHex (Toplevel top) public static void ShowHex (Toplevel top)

View File

@@ -44,33 +44,49 @@ namespace Terminal.Gui {
return false; return false;
} }
internal void Reload () internal bool Reload (ustring value = null)
{ {
bool valid = false;
try { try {
dirInfo = new DirectoryInfo (directory.ToString ()); dirInfo = new DirectoryInfo (value == null ? directory.ToString () : value.ToString ());
infos = (from x in dirInfo.GetFileSystemInfos () infos = (from x in dirInfo.GetFileSystemInfos ()
where IsAllowed (x) where IsAllowed (x) && (!canChooseFiles ? x.Attributes.HasFlag (FileAttributes.Directory) : true)
orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name
select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList (); select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList ();
infos.Insert (0, ("..", true, false)); infos.Insert (0, ("..", true, false));
top = 0; top = 0;
selected = 0; selected = 0;
} catch (Exception) { valid = true;
dirInfo = null; } catch (Exception ex) {
infos.Clear (); switch (ex) {
case DirectoryNotFoundException _:
case ArgumentException _:
dirInfo = null;
infos.Clear ();
valid = true;
break;
default:
valid = false;
break;
}
} finally { } finally {
SetNeedsDisplay (); if (valid) {
SetNeedsDisplay ();
}
} }
return valid;
} }
ustring directory; ustring directory;
public ustring Directory { public ustring Directory {
get => directory; get => directory;
set { set {
if (directory == value) if (directory == value) {
return; return;
directory = value; }
Reload (); if (Reload (value)) {
directory = value;
}
} }
} }
@@ -347,13 +363,18 @@ namespace Terminal.Gui {
} }
} }
internal bool ExecuteSelection () internal bool ExecuteSelection (bool isPrompt = false)
{ {
if (infos.Count == 0) {
return false;
}
var isDir = infos [selected].Item2; var isDir = infos [selected].Item2;
if (isDir) { if (isDir) {
Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1)); if (!isPrompt) {
DirectoryChanged?.Invoke (Directory); Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1));
DirectoryChanged?.Invoke (Directory);
}
} else { } else {
FileChanged?.Invoke (infos [selected].Item1); FileChanged?.Invoke (infos [selected].Item1);
if (canChooseFiles) { if (canChooseFiles) {
@@ -408,6 +429,9 @@ namespace Terminal.Gui {
res.Add (MakePath (item.Item1)); res.Add (MakePath (item.Item1));
return res; return res;
} else { } else {
if (infos.Count == 0) {
return null;
}
if (infos [selected].Item2) { if (infos [selected].Item2) {
if (canChooseDirectories) if (canChooseDirectories)
return new List<string> () { MakePath (infos [selected].Item1) }; return new List<string> () { MakePath (infos [selected].Item1) };
@@ -500,7 +524,7 @@ namespace Terminal.Gui {
IsDefault = true, IsDefault = true,
}; };
this.prompt.Clicked += () => { this.prompt.Clicked += () => {
dirListView.ExecuteSelection (); dirListView.ExecuteSelection (true);
canceled = false; canceled = false;
Application.RequestStop (); Application.RequestStop ();
}; };