FileDialog.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // FileDialog.cs: File system dialogs for open and save
  3. //
  4. // TODO:
  5. // * Raise event on file selected
  6. // * Add directory selector
  7. // * Update file name on cursor changes
  8. // * Figure out why Ok/Cancel buttons do not work
  9. // * Implement subclasses
  10. // * Figure out why message text does not show
  11. // * Remove the extra space when message does not show
  12. // * Use a line separator to show the file listing, so we can use same colors as the rest
  13. // * Implement support for the subclass properties.
  14. // * Add mouse support
  15. using System;
  16. using System.Collections.Generic;
  17. using NStack;
  18. using System.IO;
  19. using System.Linq;
  20. namespace Terminal.Gui {
  21. internal class DirListView : View {
  22. int top, selected;
  23. DirectoryInfo dirInfo;
  24. List<(string,bool)> infos;
  25. public DirListView ()
  26. {
  27. infos = new List<(string,bool)> ();
  28. CanFocus = true;
  29. }
  30. bool IsAllowed (FileSystemInfo fsi)
  31. {
  32. if (fsi.Attributes.HasFlag (FileAttributes.Directory))
  33. return true;
  34. if (allowedFileTypes == null)
  35. return true;
  36. foreach (var ft in allowedFileTypes)
  37. if (fsi.Name.EndsWith (ft))
  38. return true;
  39. return false;
  40. }
  41. void Reload ()
  42. {
  43. dirInfo = new DirectoryInfo (directory.ToString ());
  44. infos = (from x in dirInfo.GetFileSystemInfos ()
  45. where IsAllowed (x)
  46. orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name
  47. select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory))).ToList ();
  48. infos.Insert (0, ("..", true));
  49. top = 0;
  50. selected = 0;
  51. SetNeedsDisplay ();
  52. }
  53. ustring directory;
  54. public ustring Directory {
  55. get => directory;
  56. set {
  57. if (directory == value)
  58. return;
  59. directory = value;
  60. Reload ();
  61. }
  62. }
  63. public override void PositionCursor ()
  64. {
  65. Move (0, selected - top);
  66. }
  67. void DrawString (int line, string str)
  68. {
  69. var f = Frame;
  70. var width = f.Width;
  71. var ustr = ustring.Make (str);
  72. Move (2, line);
  73. int byteLen = ustr.Length;
  74. int used = 0;
  75. for (int i = 0; i < byteLen;) {
  76. (var rune, var size) = Utf8.DecodeRune (ustr, i, i - byteLen);
  77. var count = Rune.ColumnWidth (rune);
  78. if (used + count >= width)
  79. break;
  80. Driver.AddRune (rune);
  81. used += count;
  82. i += size;
  83. }
  84. for (; used < width; used++) {
  85. Driver.AddRune (' ');
  86. }
  87. }
  88. public override void Redraw (Rect region)
  89. {
  90. var current = ColorScheme.Focus;
  91. Driver.SetAttribute (current);
  92. Move (0, 0);
  93. var f = Frame;
  94. var item = top;
  95. bool focused = HasFocus;
  96. var width = region.Width;
  97. for (int row = 0; row < f.Height; row++, item++) {
  98. bool isSelected = item == selected;
  99. Move (0, row);
  100. var newcolor = focused ? (isSelected ? ColorScheme.HotNormal : ColorScheme.Focus) : ColorScheme.Focus;
  101. if (newcolor != current) {
  102. Driver.SetAttribute (newcolor);
  103. current = newcolor;
  104. }
  105. if (item >= infos.Count) {
  106. for (int c = 0; c < f.Width; c++)
  107. Driver.AddRune (' ');
  108. continue;
  109. }
  110. var fi = infos [item];
  111. Driver.AddRune (isSelected ? '>' : ' ');
  112. if (fi.Item2)
  113. Driver.AddRune ('/');
  114. else
  115. Driver.AddRune (' ');
  116. DrawString (row, fi.Item1);
  117. }
  118. }
  119. public Action<(string,bool)> SelectedChanged;
  120. public Action<ustring> DirectoryChanged;
  121. void SelectionChanged ()
  122. {
  123. if (SelectedChanged != null)
  124. SelectedChanged (infos [selected]);
  125. }
  126. public override bool ProcessKey (KeyEvent keyEvent)
  127. {
  128. switch (keyEvent.Key) {
  129. case Key.CursorUp:
  130. case Key.ControlP:
  131. if (selected > 0) {
  132. selected--;
  133. if (selected < top)
  134. top = selected;
  135. SelectionChanged ();
  136. SetNeedsDisplay ();
  137. }
  138. return true;
  139. case Key.CursorDown:
  140. case Key.ControlN:
  141. if (selected + 1 < infos.Count) {
  142. selected++;
  143. if (selected >= top + Frame.Height)
  144. top++;
  145. SelectionChanged ();
  146. SetNeedsDisplay ();
  147. }
  148. return true;
  149. case Key.ControlV:
  150. case Key.PageDown:
  151. var n = (selected + Frame.Height);
  152. if (n > infos.Count)
  153. n = infos.Count - 1;
  154. if (n != selected) {
  155. selected = n;
  156. if (infos.Count >= Frame.Height)
  157. top = selected;
  158. else
  159. top = 0;
  160. SelectionChanged ();
  161. SetNeedsDisplay ();
  162. }
  163. return true;
  164. case Key.Enter:
  165. if (infos [selected].Item2) {
  166. Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1));
  167. if (DirectoryChanged != null)
  168. DirectoryChanged (Directory);
  169. } else {
  170. // File Selected
  171. }
  172. return true;
  173. case Key.PageUp:
  174. n = (selected - Frame.Height);
  175. if (n < 0)
  176. n = 0;
  177. if (n != selected) {
  178. selected = n;
  179. top = selected;
  180. SelectionChanged ();
  181. SetNeedsDisplay ();
  182. }
  183. return true;
  184. }
  185. return base.ProcessKey (keyEvent);
  186. }
  187. string [] allowedFileTypes;
  188. public string [] AllowedFileTypes {
  189. get => allowedFileTypes;
  190. set {
  191. allowedFileTypes = value;
  192. Reload ();
  193. }
  194. }
  195. }
  196. public class FileDialog : Dialog {
  197. Button prompt, cancel;
  198. Label nameFieldLabel, message, dirLabel;
  199. TextField dirEntry, nameEntry;
  200. DirListView dirListView;
  201. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title, Driver.Cols - 20, Driver.Rows - 5, null)
  202. {
  203. this.message = new Label (Rect.Empty, "MESSAGE" + message);
  204. var msgLines = Label.MeasureLines (message, Driver.Cols - 20);
  205. dirLabel = new Label ("Directory: ") {
  206. X = 1,
  207. Y = 1 + msgLines
  208. };
  209. dirEntry = new TextField ("") {
  210. X = 11,
  211. Y = 1 + msgLines,
  212. Width = Dim.Fill () - 1
  213. };
  214. Add (dirLabel, dirEntry);
  215. this.nameFieldLabel = new Label (nameFieldLabel) {
  216. X = 1,
  217. Y = 3 + msgLines,
  218. };
  219. nameEntry = new TextField ("") {
  220. X = 1 + nameFieldLabel.RuneCount + 1,
  221. Y = 3 + msgLines,
  222. Width = Dim.Fill () - 1
  223. };
  224. Add (this.nameFieldLabel, nameEntry);
  225. dirListView = new DirListView () {
  226. X = 1,
  227. Y = 3 + msgLines + 2,
  228. Width = Dim.Fill (),
  229. Height = Dim.Fill (),
  230. Directory = "."
  231. };
  232. Add (dirListView);
  233. dirListView.DirectoryChanged = (dir) => dirEntry.Text = dir;
  234. this.cancel = new Button ("Cancel");
  235. AddButton (cancel);
  236. this.prompt = new Button (prompt);
  237. AddButton (this.prompt);
  238. }
  239. /// <summary>
  240. /// Gets or sets the prompt label for the button displayed to the user
  241. /// </summary>
  242. /// <value>The prompt.</value>
  243. public ustring Prompt {
  244. get => prompt.Text;
  245. set {
  246. prompt.Text = value;
  247. }
  248. }
  249. /// <summary>
  250. /// Gets or sets the name field label.
  251. /// </summary>
  252. /// <value>The name field label.</value>
  253. public ustring NameFieldLabel {
  254. get => nameFieldLabel.Text;
  255. set {
  256. nameFieldLabel.Text = value;
  257. }
  258. }
  259. /// <summary>
  260. /// Gets or sets the message displayed to the user, defaults to nothing
  261. /// </summary>
  262. /// <value>The message.</value>
  263. public ustring Message {
  264. get => message.Text;
  265. set {
  266. message.Text = value;
  267. }
  268. }
  269. /// <summary>
  270. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> can create directories.
  271. /// </summary>
  272. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  273. public bool CanCreateDirectories { get; set; }
  274. /// <summary>
  275. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> is extension hidden.
  276. /// </summary>
  277. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  278. public bool IsExtensionHidden { get; set; }
  279. /// <summary>
  280. /// Gets or sets the directory path for this panel
  281. /// </summary>
  282. /// <value>The directory path.</value>
  283. public ustring DirectoryPath {
  284. get => dirEntry.Text;
  285. set {
  286. dirEntry.Text = value;
  287. dirListView.Directory = value;
  288. }
  289. }
  290. /// <summary>
  291. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  292. /// </summary>
  293. /// <value>The allowed file types.</value>
  294. public string [] AllowedFileTypes {
  295. get => dirListView.AllowedFileTypes;
  296. set => dirListView.AllowedFileTypes = value;
  297. }
  298. /// <summary>
  299. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> allows the file to be saved with a different extension
  300. /// </summary>
  301. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  302. public bool AllowsOtherFileTypes { get; set; }
  303. /// <summary>
  304. /// The File path that is currently shown on the panel
  305. /// </summary>
  306. /// <value>The absolute file path for the file path entered.</value>
  307. public ustring FilePath {
  308. get => nameEntry.Text;
  309. set {
  310. nameEntry.Text = value;
  311. }
  312. }
  313. }
  314. public class SaveDialog : FileDialog {
  315. public SaveDialog (ustring title, ustring message) : base (title, prompt: "Save", nameFieldLabel: "Save as:", message: message)
  316. {
  317. }
  318. }
  319. public class OpenDialog : FileDialog {
  320. public OpenDialog (ustring title, ustring message) : base (title, prompt: "Open", nameFieldLabel: "Open", message: message)
  321. {
  322. }
  323. /// <summary>
  324. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose files.
  325. /// </summary>
  326. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>.</value>
  327. public bool CanChooseFiles { get; set; }
  328. /// <summary>
  329. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose directories.
  330. /// </summary>
  331. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c>.</value>
  332. public bool CanChooseDirectories { get; set; }
  333. /// <summary>
  334. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> allows multiple selection.
  335. /// </summary>
  336. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>.</value>
  337. public bool AllowsMultipleSelection { get; set; }
  338. /// <summary>
  339. /// Gets the file paths selected
  340. /// </summary>
  341. /// <value>The file paths.</value>
  342. public IReadOnlyList<ustring> FilePaths { get; }
  343. }
  344. }