Browse Source

Fixes #853. CanChooseDirectories and CanChooseFiles now work properly.

BDisp 5 years ago
parent
commit
4d5713e269
1 changed files with 20 additions and 11 deletions
  1. 20 11
      Terminal.Gui/Windows/FileDialog.cs

+ 20 - 11
Terminal.Gui/Windows/FileDialog.cs

@@ -44,33 +44,39 @@ 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;
+				valid = true;
 			} catch (Exception) {
 			} catch (Exception) {
-				dirInfo = null;
-				infos.Clear ();
+				valid = false;
 			} 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;
+				}
 			}
 			}
 		}
 		}
 
 
@@ -413,6 +419,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) };
@@ -472,8 +481,8 @@ namespace Terminal.Gui {
 			};
 			};
 			Add (dirLabel, dirEntry);
 			Add (dirLabel, dirEntry);
 
 
-			this.nameFieldLabel = new Label ("File(s): ") {
-				X = 3,
+			this.nameFieldLabel = new Label ("Open: ") {
+				X = 6,
 				Y = 3 + msgLines,
 				Y = 3 + msgLines,
 			};
 			};
 			nameEntry = new TextField ("") {
 			nameEntry = new TextField ("") {