From d232715e62b60aa5d0fef809c396bf5ea4526fa4 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 2 Aug 2020 11:17:10 +0100 Subject: [PATCH 1/4] Fixes #844. Now selects the correct Directory. --- Example/demo.cs | 2 +- Terminal.Gui/Windows/FileDialog.cs | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index 60a27c413..68307007a 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -305,7 +305,7 @@ static class Demo { Application.Run (d); 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) diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index 3d41e16b3..ec2a03526 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -347,13 +347,15 @@ namespace Terminal.Gui { } } - internal bool ExecuteSelection () + internal bool ExecuteSelection (bool isPrompt = false) { var isDir = infos [selected].Item2; if (isDir) { - Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1)); - DirectoryChanged?.Invoke (Directory); + if (!isPrompt) { + Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1)); + DirectoryChanged?.Invoke (Directory); + } } else { FileChanged?.Invoke (infos [selected].Item1); if (canChooseFiles) { @@ -467,8 +469,8 @@ namespace Terminal.Gui { }; Add (dirLabel, dirEntry); - this.nameFieldLabel = new Label ("Open: ") { - X = 6, + this.nameFieldLabel = new Label ("File(s): ") { + X = 3, Y = 3 + msgLines, }; nameEntry = new TextField ("") { @@ -500,7 +502,7 @@ namespace Terminal.Gui { IsDefault = true, }; this.prompt.Clicked += () => { - dirListView.ExecuteSelection (); + dirListView.ExecuteSelection (true); canceled = false; Application.RequestStop (); }; From 4aa228f200ba17d0310d5d6d830c0d97d3050b2e Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 7 Aug 2020 07:34:14 +0100 Subject: [PATCH 2/4] Fixes #852. Avoiding exception if infos is empty. --- Terminal.Gui/Windows/FileDialog.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index ec2a03526..c2c004f38 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -349,6 +349,9 @@ namespace Terminal.Gui { internal bool ExecuteSelection (bool isPrompt = false) { + if (infos.Count == 0) { + return false; + } var isDir = infos [selected].Item2; if (isDir) { From 4a6fe4569885610a869d3124fa2ad160d960b107 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 7 Aug 2020 10:14:40 +0100 Subject: [PATCH 3/4] Fixes #853. CanChooseDirectories and CanChooseFiles now work properly. --- Terminal.Gui/Windows/FileDialog.cs | 31 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index c2c004f38..0433ce32e 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -44,33 +44,39 @@ namespace Terminal.Gui { return false; } - internal void Reload () + internal bool Reload (ustring value = null) { + bool valid = false; try { - dirInfo = new DirectoryInfo (directory.ToString ()); + dirInfo = new DirectoryInfo (value == null ? directory.ToString () : value.ToString ()); 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 select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList (); infos.Insert (0, ("..", true, false)); top = 0; selected = 0; + valid = true; } catch (Exception) { - dirInfo = null; - infos.Clear (); + valid = false; } finally { - SetNeedsDisplay (); + if (valid) { + SetNeedsDisplay (); + } } + return valid; } ustring directory; public ustring Directory { get => directory; set { - if (directory == value) + if (directory == value) { return; - directory = value; - Reload (); + } + if (Reload (value)) { + directory = value; + } } } @@ -413,6 +419,9 @@ namespace Terminal.Gui { res.Add (MakePath (item.Item1)); return res; } else { + if (infos.Count == 0) { + return null; + } if (infos [selected].Item2) { if (canChooseDirectories) return new List () { MakePath (infos [selected].Item1) }; @@ -472,8 +481,8 @@ namespace Terminal.Gui { }; Add (dirLabel, dirEntry); - this.nameFieldLabel = new Label ("File(s): ") { - X = 3, + this.nameFieldLabel = new Label ("Open: ") { + X = 6, Y = 3 + msgLines, }; nameEntry = new TextField ("") { From 2c8f6ec175115d9dc809a619ae42d81115eff178 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 7 Aug 2020 13:49:08 +0100 Subject: [PATCH 4/4] Added conditional exceptions. --- Terminal.Gui/Windows/FileDialog.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Windows/FileDialog.cs b/Terminal.Gui/Windows/FileDialog.cs index 0433ce32e..c6fcf35d3 100644 --- a/Terminal.Gui/Windows/FileDialog.cs +++ b/Terminal.Gui/Windows/FileDialog.cs @@ -57,8 +57,18 @@ namespace Terminal.Gui { top = 0; selected = 0; valid = true; - } catch (Exception) { - valid = false; + } catch (Exception ex) { + switch (ex) { + case DirectoryNotFoundException _: + case ArgumentException _: + dirInfo = null; + infos.Clear (); + valid = true; + break; + default: + valid = false; + break; + } } finally { if (valid) { SetNeedsDisplay ();