Browse Source

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

Fixes #844. Now selects the correct Directory.
Charlie Kindel 5 years ago
parent
commit
d4c2e94a00
2 changed files with 39 additions and 15 deletions
  1. 1 1
      Example/demo.cs
  2. 38 14
      Terminal.Gui/Windows/FileDialog.cs

+ 1 - 1
Example/demo.cs

@@ -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)

+ 38 - 14
Terminal.Gui/Windows/FileDialog.cs

@@ -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) {
-				dirInfo = null;
-				infos.Clear ();
+				valid = true;
+			} catch (Exception ex) {
+				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));
-				DirectoryChanged?.Invoke (Directory);
+				if (!isPrompt) {
+					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 ();
 			};
 			};