FileDialog.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. //
  2. // FileDialog.cs: File system dialogs for open and save
  3. //
  4. // TODO:
  5. // * Add directory selector
  6. // * Implement subclasses
  7. // * Figure out why message text does not show
  8. // * Remove the extra space when message does not show
  9. // * Use a line separator to show the file listing, so we can use same colors as the rest
  10. // * DirListView: Add mouse support
  11. using System;
  12. using System.Collections.Generic;
  13. using NStack;
  14. using System.IO;
  15. using System.Linq;
  16. using Terminal.Gui.Resources;
  17. namespace Terminal.Gui {
  18. internal class DirListView : View {
  19. int top, selected;
  20. DirectoryInfo dirInfo;
  21. FileSystemWatcher watcher;
  22. List<(string, bool, bool)> infos;
  23. internal bool canChooseFiles = true;
  24. internal bool canChooseDirectories = false;
  25. internal bool allowsMultipleSelection = false;
  26. FileDialog host;
  27. public DirListView (FileDialog host)
  28. {
  29. infos = new List<(string, bool, bool)> ();
  30. CanFocus = true;
  31. this.host = host;
  32. }
  33. bool IsAllowed (FileSystemInfo fsi)
  34. {
  35. if (fsi.Attributes.HasFlag (FileAttributes.Directory))
  36. return true;
  37. if (allowedFileTypes == null)
  38. return true;
  39. foreach (var ft in allowedFileTypes)
  40. if (fsi.Name.EndsWith (ft, StringComparison.InvariantCultureIgnoreCase) || ft == ".*")
  41. return true;
  42. return false;
  43. }
  44. internal bool Reload (ustring value = null)
  45. {
  46. bool valid = false;
  47. try {
  48. dirInfo = new DirectoryInfo (value == null ? directory.ToString () : value.ToString ());
  49. // Dispose of the old watcher
  50. watcher?.Dispose ();
  51. watcher = new FileSystemWatcher (dirInfo.FullName);
  52. watcher.NotifyFilter = NotifyFilters.Attributes
  53. | NotifyFilters.CreationTime
  54. | NotifyFilters.DirectoryName
  55. | NotifyFilters.FileName
  56. | NotifyFilters.LastAccess
  57. | NotifyFilters.LastWrite
  58. | NotifyFilters.Security
  59. | NotifyFilters.Size;
  60. watcher.Changed += Watcher_Changed;
  61. watcher.Created += Watcher_Changed;
  62. watcher.Deleted += Watcher_Changed;
  63. watcher.Renamed += Watcher_Changed;
  64. watcher.Error += Watcher_Error;
  65. watcher.EnableRaisingEvents = true;
  66. infos = (from x in dirInfo.GetFileSystemInfos ()
  67. where IsAllowed (x) && (!canChooseFiles ? x.Attributes.HasFlag (FileAttributes.Directory) : true)
  68. orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name
  69. select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList ();
  70. infos.Insert (0, ("..", true, false));
  71. top = 0;
  72. selected = 0;
  73. valid = true;
  74. } catch (Exception ex) {
  75. switch (ex) {
  76. case DirectoryNotFoundException _:
  77. case ArgumentException _:
  78. dirInfo = null;
  79. watcher = null;
  80. infos.Clear ();
  81. valid = true;
  82. break;
  83. default:
  84. valid = false;
  85. break;
  86. }
  87. } finally {
  88. if (valid) {
  89. SetNeedsDisplay ();
  90. }
  91. }
  92. return valid;
  93. }
  94. private bool _disposedValue;
  95. protected override void Dispose (bool disposing)
  96. {
  97. if (!_disposedValue) {
  98. if (disposing) {
  99. watcher?.Dispose ();
  100. }
  101. _disposedValue = true;
  102. }
  103. // Call base class implementation.
  104. base.Dispose (disposing);
  105. }
  106. void Watcher_Error (object sender, ErrorEventArgs e)
  107. {
  108. Debug.WriteLine ($"Watcher error: {e.GetException ()}");
  109. Application.MainLoop.Invoke (() => Reload ());
  110. }
  111. void Watcher_Changed (object sender, FileSystemEventArgs e)
  112. {
  113. Application.MainLoop.Invoke (() => Reload ());
  114. }
  115. ustring directory;
  116. public ustring Directory {
  117. get => directory;
  118. set {
  119. if (directory == value) {
  120. return;
  121. }
  122. if (Reload (value)) {
  123. directory = value;
  124. }
  125. }
  126. }
  127. public override void PositionCursor ()
  128. {
  129. Move (0, selected - top);
  130. }
  131. int lastSelected;
  132. bool shiftOnWheel;
  133. public override bool MouseEvent (MouseEvent me)
  134. {
  135. if ((me.Flags & (MouseFlags.Button1Clicked | MouseFlags.Button1DoubleClicked |
  136. MouseFlags.WheeledUp | MouseFlags.WheeledDown)) == 0)
  137. return false;
  138. if (!HasFocus)
  139. SetFocus ();
  140. if (infos == null)
  141. return false;
  142. if (me.Y + top >= infos.Count)
  143. return true;
  144. int lastSelectedCopy = shiftOnWheel ? lastSelected : selected;
  145. switch (me.Flags) {
  146. case MouseFlags.Button1Clicked:
  147. SetSelected (me);
  148. OnSelectionChanged ();
  149. SetNeedsDisplay ();
  150. break;
  151. case MouseFlags.Button1DoubleClicked:
  152. UnMarkAll ();
  153. SetSelected (me);
  154. if (ExecuteSelection ()) {
  155. host.canceled = false;
  156. Application.RequestStop ();
  157. }
  158. return true;
  159. case MouseFlags.Button1Clicked | MouseFlags.ButtonShift:
  160. SetSelected (me);
  161. if (shiftOnWheel)
  162. lastSelected = lastSelectedCopy;
  163. shiftOnWheel = false;
  164. PerformMultipleSelection (lastSelected);
  165. return true;
  166. case MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl:
  167. SetSelected (me);
  168. PerformMultipleSelection ();
  169. return true;
  170. case MouseFlags.WheeledUp:
  171. SetSelected (me);
  172. selected = lastSelected;
  173. MoveUp ();
  174. return true;
  175. case MouseFlags.WheeledDown:
  176. SetSelected (me);
  177. selected = lastSelected;
  178. MoveDown ();
  179. return true;
  180. case MouseFlags.WheeledUp | MouseFlags.ButtonShift:
  181. SetSelected (me);
  182. selected = lastSelected;
  183. lastSelected = lastSelectedCopy;
  184. shiftOnWheel = true;
  185. MoveUp ();
  186. return true;
  187. case MouseFlags.WheeledDown | MouseFlags.ButtonShift:
  188. SetSelected (me);
  189. selected = lastSelected;
  190. lastSelected = lastSelectedCopy;
  191. shiftOnWheel = true;
  192. MoveDown ();
  193. return true;
  194. }
  195. return true;
  196. }
  197. void UnMarkAll ()
  198. {
  199. for (int i = 0; i < infos.Count; i++) {
  200. if (infos [i].Item3) {
  201. infos [i] = (infos [i].Item1, infos [i].Item2, false);
  202. }
  203. }
  204. }
  205. void SetSelected (MouseEvent me)
  206. {
  207. lastSelected = selected;
  208. selected = top + me.Y;
  209. }
  210. void DrawString (int line, string str)
  211. {
  212. var f = Frame;
  213. var width = f.Width;
  214. var ustr = ustring.Make (str);
  215. Move (allowsMultipleSelection ? 3 : 2, line);
  216. int byteLen = ustr.Length;
  217. int used = allowsMultipleSelection ? 2 : 1;
  218. for (int i = 0; i < byteLen;) {
  219. (var rune, var size) = Utf8.DecodeRune (ustr, i, i - byteLen);
  220. var count = Rune.ColumnWidth (rune);
  221. if (used + count >= width)
  222. break;
  223. Driver.AddRune (rune);
  224. used += count;
  225. i += size;
  226. }
  227. for (; used < width - 1; used++) {
  228. Driver.AddRune (' ');
  229. }
  230. }
  231. public override void Redraw (Rect bounds)
  232. {
  233. var current = ColorScheme.Focus;
  234. Driver.SetAttribute (current);
  235. Move (0, 0);
  236. var f = Frame;
  237. var item = top;
  238. bool focused = HasFocus;
  239. var width = bounds.Width;
  240. for (int row = 0; row < f.Height; row++, item++) {
  241. bool isSelected = item == selected;
  242. Move (0, row);
  243. var newcolor = focused ? (isSelected ? ColorScheme.HotNormal : ColorScheme.Focus)
  244. : Enabled ? ColorScheme.Focus : ColorScheme.Disabled;
  245. if (newcolor != current) {
  246. Driver.SetAttribute (newcolor);
  247. current = newcolor;
  248. }
  249. if (item >= infos.Count) {
  250. for (int c = 0; c < f.Width; c++)
  251. Driver.AddRune (' ');
  252. continue;
  253. }
  254. var fi = infos [item];
  255. Driver.AddRune (isSelected ? '>' : ' ');
  256. if (allowsMultipleSelection)
  257. Driver.AddRune (fi.Item3 ? '*' : ' ');
  258. if (fi.Item2)
  259. Driver.AddRune ('/');
  260. else
  261. Driver.AddRune (' ');
  262. DrawString (row, fi.Item1);
  263. }
  264. }
  265. public Action<(string, bool)> SelectedChanged { get; set; }
  266. public Action<ustring> DirectoryChanged { get; set; }
  267. public Action<ustring> FileChanged { get; set; }
  268. string splitString = ",";
  269. void OnSelectionChanged ()
  270. {
  271. if (allowsMultipleSelection) {
  272. if (FilePaths.Count > 0) {
  273. FileChanged?.Invoke (string.Join (splitString, GetFilesName (FilePaths)));
  274. } else {
  275. FileChanged?.Invoke (infos [selected].Item2 && !canChooseDirectories ? "" : Path.GetFileName (infos [selected].Item1));
  276. }
  277. } else {
  278. var sel = infos [selected];
  279. SelectedChanged?.Invoke ((sel.Item1, sel.Item2));
  280. }
  281. }
  282. List<string> GetFilesName (IReadOnlyList<string> files)
  283. {
  284. List<string> filesName = new List<string> ();
  285. foreach (var file in files) {
  286. filesName.Add (Path.GetFileName (file));
  287. }
  288. return filesName;
  289. }
  290. public bool GetValidFilesName (string files, out string result)
  291. {
  292. result = string.Empty;
  293. if (infos?.Count == 0) {
  294. return false;
  295. }
  296. var valid = true;
  297. IReadOnlyList<string> filesList = new List<string> (files.Split (splitString.ToArray (), StringSplitOptions.None));
  298. var filesName = new List<string> ();
  299. UnMarkAll ();
  300. foreach (var file in filesList) {
  301. if (!allowsMultipleSelection && filesName.Count > 0) {
  302. break;
  303. }
  304. var idx = infos.IndexOf (x => x.Item1.IndexOf (file, StringComparison.OrdinalIgnoreCase) >= 0);
  305. if (idx > -1 && string.Equals (infos [idx].Item1, file, StringComparison.OrdinalIgnoreCase)) {
  306. if (canChooseDirectories && !canChooseFiles && !infos [idx].Item2) {
  307. valid = false;
  308. }
  309. if (allowsMultipleSelection && !infos [idx].Item3) {
  310. infos [idx] = (infos [idx].Item1, infos [idx].Item2, true);
  311. }
  312. if (!allowsMultipleSelection) {
  313. selected = idx;
  314. }
  315. filesName.Add (Path.GetFileName (infos [idx].Item1));
  316. } else if (idx > -1) {
  317. valid = false;
  318. filesName.Add (Path.GetFileName (file));
  319. }
  320. }
  321. result = string.Join (splitString, filesName);
  322. if (string.IsNullOrEmpty (result)) {
  323. valid = false;
  324. }
  325. return valid;
  326. }
  327. public override bool ProcessKey (KeyEvent keyEvent)
  328. {
  329. switch (keyEvent.Key) {
  330. case Key.CursorUp:
  331. case Key.P | Key.CtrlMask:
  332. MoveUp ();
  333. return true;
  334. case Key.CursorDown:
  335. case Key.N | Key.CtrlMask:
  336. MoveDown ();
  337. return true;
  338. case Key.V | Key.CtrlMask:
  339. case Key.PageDown:
  340. var n = (selected + Frame.Height);
  341. if (n > infos.Count)
  342. n = infos.Count - 1;
  343. if (n != selected) {
  344. selected = n;
  345. if (infos.Count >= Frame.Height)
  346. top = selected;
  347. else
  348. top = 0;
  349. OnSelectionChanged ();
  350. SetNeedsDisplay ();
  351. }
  352. return true;
  353. case Key.Enter:
  354. UnMarkAll ();
  355. if (ExecuteSelection ())
  356. return false;
  357. else
  358. return true;
  359. case Key.PageUp:
  360. n = (selected - Frame.Height);
  361. if (n < 0)
  362. n = 0;
  363. if (n != selected) {
  364. selected = n;
  365. top = selected;
  366. OnSelectionChanged ();
  367. SetNeedsDisplay ();
  368. }
  369. return true;
  370. case Key.Space:
  371. case Key.T | Key.CtrlMask:
  372. PerformMultipleSelection ();
  373. return true;
  374. case Key.Home:
  375. MoveFirst ();
  376. return true;
  377. case Key.End:
  378. MoveLast ();
  379. return true;
  380. }
  381. return base.ProcessKey (keyEvent);
  382. }
  383. void MoveLast ()
  384. {
  385. selected = infos.Count - 1;
  386. top = infos.Count () - 1;
  387. OnSelectionChanged ();
  388. SetNeedsDisplay ();
  389. }
  390. void MoveFirst ()
  391. {
  392. selected = 0;
  393. top = 0;
  394. OnSelectionChanged ();
  395. SetNeedsDisplay ();
  396. }
  397. void MoveDown ()
  398. {
  399. if (selected + 1 < infos.Count) {
  400. selected++;
  401. if (selected >= top + Frame.Height)
  402. top++;
  403. OnSelectionChanged ();
  404. SetNeedsDisplay ();
  405. }
  406. }
  407. void MoveUp ()
  408. {
  409. if (selected > 0) {
  410. selected--;
  411. if (selected < top)
  412. top = selected;
  413. OnSelectionChanged ();
  414. SetNeedsDisplay ();
  415. }
  416. }
  417. internal bool ExecuteSelection (bool navigateFolder = true)
  418. {
  419. if (infos.Count == 0) {
  420. return false;
  421. }
  422. var isDir = infos [selected].Item2;
  423. if (isDir) {
  424. Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1));
  425. DirectoryChanged?.Invoke (Directory);
  426. if (canChooseDirectories && !navigateFolder) {
  427. return true;
  428. }
  429. } else {
  430. OnSelectionChanged ();
  431. if (canChooseFiles) {
  432. // Ensures that at least one file is selected.
  433. if (FilePaths.Count == 0)
  434. PerformMultipleSelection ();
  435. // Let the OK handler take it over
  436. return true;
  437. }
  438. // No files allowed, do not let the default handler take it.
  439. }
  440. return false;
  441. }
  442. void PerformMultipleSelection (int? firstSelected = null)
  443. {
  444. if (allowsMultipleSelection) {
  445. int first = Math.Min (firstSelected ?? selected, selected);
  446. int last = Math.Max (selected, firstSelected ?? selected);
  447. for (int i = first; i <= last; i++) {
  448. if ((canChooseFiles && infos [i].Item2 == false) ||
  449. (canChooseDirectories && infos [i].Item2 &&
  450. infos [i].Item1 != "..")) {
  451. infos [i] = (infos [i].Item1, infos [i].Item2, !infos [i].Item3);
  452. }
  453. }
  454. OnSelectionChanged ();
  455. SetNeedsDisplay ();
  456. }
  457. }
  458. string [] allowedFileTypes;
  459. public string [] AllowedFileTypes {
  460. get => allowedFileTypes;
  461. set {
  462. allowedFileTypes = value;
  463. Reload ();
  464. }
  465. }
  466. public string MakePath (string relativePath)
  467. {
  468. var dir = Directory.ToString ();
  469. return string.IsNullOrEmpty (dir) ? "" : Path.GetFullPath (Path.Combine (dir, relativePath));
  470. }
  471. public IReadOnlyList<string> FilePaths {
  472. get {
  473. if (allowsMultipleSelection) {
  474. var res = new List<string> ();
  475. foreach (var item in infos) {
  476. if (item.Item3)
  477. res.Add (MakePath (item.Item1));
  478. }
  479. if (res.Count == 0 && infos.Count > 0 && infos [selected].Item1 != "..") {
  480. res.Add (MakePath (infos [selected].Item1));
  481. }
  482. return res;
  483. } else {
  484. if (infos.Count == 0) {
  485. return null;
  486. }
  487. if (infos [selected].Item2) {
  488. if (canChooseDirectories) {
  489. var sel = infos [selected].Item1;
  490. return sel == ".." ? new List<string> () : new List<string> () { MakePath (infos [selected].Item1) };
  491. }
  492. return Array.Empty<string> ();
  493. } else {
  494. if (canChooseFiles) {
  495. return new List<string> () { MakePath (infos [selected].Item1) };
  496. }
  497. return Array.Empty<string> ();
  498. }
  499. }
  500. }
  501. }
  502. ///<inheritdoc/>
  503. public override bool OnEnter (View view)
  504. {
  505. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  506. return base.OnEnter (view);
  507. }
  508. }
  509. /// <summary>
  510. /// Base class for the <see cref="OpenDialog"/> and the <see cref="SaveDialog"/>
  511. /// </summary>
  512. public class FileDialog : Dialog {
  513. Button prompt, cancel;
  514. Label nameFieldLabel, message, nameDirLabel;
  515. TextField dirEntry, nameEntry;
  516. internal DirListView dirListView;
  517. ComboBox cmbAllowedTypes;
  518. /// <summary>
  519. /// Initializes a new <see cref="FileDialog"/>.
  520. /// </summary>
  521. public FileDialog () : this (title: string.Empty, prompt: string.Empty,
  522. nameFieldLabel: string.Empty, message: string.Empty)
  523. { }
  524. /// <summary>
  525. /// Initializes a new instance of <see cref="FileDialog"/>
  526. /// </summary>
  527. /// <param name="title">The title.</param>
  528. /// <param name="prompt">The prompt.</param>
  529. /// <param name="nameFieldLabel">The name of the file field label..</param>
  530. /// <param name="message">The message.</param>
  531. /// <param name="allowedTypes">The allowed types.</param>
  532. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message, List<string> allowedTypes = null)
  533. : this (title, prompt, ustring.Empty, nameFieldLabel, message, allowedTypes) { }
  534. /// <summary>
  535. /// Initializes a new instance of <see cref="FileDialog"/>
  536. /// </summary>
  537. /// <param name="title">The title.</param>
  538. /// <param name="prompt">The prompt.</param>
  539. /// <param name="message">The message.</param>
  540. /// <param name="allowedTypes">The allowed types.</param>
  541. public FileDialog (ustring title, ustring prompt, ustring message, List<string> allowedTypes)
  542. : this (title, prompt, ustring.Empty, message, allowedTypes) { }
  543. /// <summary>
  544. /// Initializes a new instance of <see cref="FileDialog"/>
  545. /// </summary>
  546. /// <param name="title">The title.</param>
  547. /// <param name="prompt">The prompt.</param>
  548. /// <param name="nameDirLabel">The name of the directory field label.</param>
  549. /// <param name="nameFieldLabel">The name of the file field label..</param>
  550. /// <param name="message">The message.</param>
  551. /// <param name="allowedTypes">The allowed types.</param>
  552. public FileDialog (ustring title, ustring prompt, ustring nameDirLabel, ustring nameFieldLabel, ustring message,
  553. List<string> allowedTypes = null) : base (title)//, Driver.Cols - 20, Driver.Rows - 5, null)
  554. {
  555. this.message = new Label (message) {
  556. X = 1,
  557. Y = 0,
  558. };
  559. Add (this.message);
  560. var msgLines = TextFormatter.MaxLines (message, Driver.Cols - 20);
  561. this.nameDirLabel = new Label (nameDirLabel.IsEmpty ? $"{Strings.fdDirectory}: " : $"{nameDirLabel}: ") {
  562. X = 1,
  563. Y = 1 + msgLines,
  564. AutoSize = true
  565. };
  566. dirEntry = new TextField ("") {
  567. X = Pos.Right (this.nameDirLabel),
  568. Y = 1 + msgLines,
  569. Width = Dim.Fill () - 1,
  570. };
  571. dirEntry.TextChanged += (e) => {
  572. DirectoryPath = dirEntry.Text;
  573. nameEntry.Text = ustring.Empty;
  574. };
  575. Add (this.nameDirLabel, dirEntry);
  576. this.nameFieldLabel = new Label (nameFieldLabel.IsEmpty ? $"{Strings.fdFile}: " : $"{nameFieldLabel}: ") {
  577. X = 1,
  578. Y = 3 + msgLines,
  579. AutoSize = true
  580. };
  581. nameEntry = new TextField ("") {
  582. X = Pos.Left (dirEntry),
  583. Y = 3 + msgLines,
  584. Width = Dim.Percent (70, true)
  585. };
  586. Add (this.nameFieldLabel, nameEntry);
  587. cmbAllowedTypes = new ComboBox () {
  588. X = Pos.Right (nameEntry) + 2,
  589. Y = Pos.Top (nameEntry),
  590. Width = Dim.Fill (1),
  591. Height = allowedTypes != null ? allowedTypes.Count + 1 : 1,
  592. Text = allowedTypes?.Count > 0 ? allowedTypes [0] : string.Empty,
  593. ReadOnly = true
  594. };
  595. cmbAllowedTypes.SetSource (allowedTypes ?? new List<string> ());
  596. cmbAllowedTypes.OpenSelectedItem += (e) => AllowedFileTypes = cmbAllowedTypes.Text.ToString ().Split (';');
  597. Add (cmbAllowedTypes);
  598. dirListView = new DirListView (this) {
  599. X = 1,
  600. Y = 3 + msgLines + 2,
  601. Width = Dim.Fill () - 1,
  602. Height = Dim.Fill () - 2,
  603. };
  604. DirectoryPath = Path.GetFullPath (Environment.CurrentDirectory);
  605. Add (dirListView);
  606. AllowedFileTypes = cmbAllowedTypes.Text.ToString ().Split (';');
  607. dirListView.DirectoryChanged = (dir) => { nameEntry.Text = ustring.Empty; dirEntry.Text = dir; };
  608. dirListView.FileChanged = (file) => nameEntry.Text = file == ".." ? "" : file;
  609. dirListView.SelectedChanged = (file) => nameEntry.Text = file.Item1 == ".." ? "" : file.Item1;
  610. this.cancel = new Button ("Cancel");
  611. this.cancel.Clicked += () => {
  612. Cancel ();
  613. };
  614. AddButton (cancel);
  615. this.prompt = new Button (prompt.IsEmpty ? "Ok" : prompt) {
  616. IsDefault = true,
  617. Enabled = nameEntry.Text.IsEmpty ? false : true
  618. };
  619. this.prompt.Clicked += () => {
  620. if (this is OpenDialog) {
  621. if (!dirListView.GetValidFilesName (nameEntry.Text.ToString (), out string res)) {
  622. nameEntry.Text = res;
  623. dirListView.SetNeedsDisplay ();
  624. return;
  625. }
  626. if (!dirListView.canChooseDirectories && !dirListView.ExecuteSelection (false)) {
  627. return;
  628. }
  629. } else if (this is SaveDialog) {
  630. var name = nameEntry.Text.ToString ();
  631. if (FilePath.IsEmpty || name.Split (',').Length > 1) {
  632. return;
  633. }
  634. var ext = name.EndsWith (cmbAllowedTypes.Text.ToString ())
  635. ? "" : cmbAllowedTypes.Text.ToString ();
  636. FilePath = Path.Combine (FilePath.ToString (), $"{name}{ext}");
  637. }
  638. canceled = false;
  639. Application.RequestStop ();
  640. };
  641. AddButton (this.prompt);
  642. nameEntry.TextChanged += (e) => {
  643. if (nameEntry.Text.IsEmpty) {
  644. this.prompt.Enabled = false;
  645. } else {
  646. this.prompt.Enabled = true;
  647. }
  648. };
  649. Width = Dim.Percent (80);
  650. Height = Dim.Percent (80);
  651. // On success, we will set this to false.
  652. canceled = true;
  653. KeyPress += (e) => {
  654. if (e.KeyEvent.Key == Key.Esc) {
  655. Cancel ();
  656. e.Handled = true;
  657. }
  658. };
  659. void Cancel ()
  660. {
  661. canceled = true;
  662. Application.RequestStop ();
  663. }
  664. }
  665. internal bool canceled;
  666. ///<inheritdoc/>
  667. public override void WillPresent ()
  668. {
  669. base.WillPresent ();
  670. dirListView.SetFocus ();
  671. }
  672. //protected override void Dispose (bool disposing)
  673. //{
  674. // message?.Dispose ();
  675. // base.Dispose (disposing);
  676. //}
  677. /// <summary>
  678. /// Gets or sets the prompt label for the <see cref="Button"/> displayed to the user
  679. /// </summary>
  680. /// <value>The prompt.</value>
  681. public ustring Prompt {
  682. get => prompt.Text;
  683. set {
  684. prompt.Text = value;
  685. }
  686. }
  687. /// <summary>
  688. /// Gets or sets the name of the directory field label.
  689. /// </summary>
  690. /// <value>The name of the directory field label.</value>
  691. public ustring NameDirLabel {
  692. get => nameDirLabel.Text;
  693. set {
  694. nameDirLabel.Text = $"{value}: ";
  695. }
  696. }
  697. /// <summary>
  698. /// Gets or sets the name field label.
  699. /// </summary>
  700. /// <value>The name field label.</value>
  701. public ustring NameFieldLabel {
  702. get => nameFieldLabel.Text;
  703. set {
  704. nameFieldLabel.Text = $"{value}: ";
  705. }
  706. }
  707. /// <summary>
  708. /// Gets or sets the message displayed to the user, defaults to nothing
  709. /// </summary>
  710. /// <value>The message.</value>
  711. public ustring Message {
  712. get => message.Text;
  713. set {
  714. message.Text = value;
  715. }
  716. }
  717. /// <summary>
  718. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> can create directories.
  719. /// </summary>
  720. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  721. public bool CanCreateDirectories { get; set; }
  722. /// <summary>
  723. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> is extension hidden.
  724. /// </summary>
  725. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  726. public bool IsExtensionHidden { get; set; }
  727. /// <summary>
  728. /// Gets or sets the directory path for this panel
  729. /// </summary>
  730. /// <value>The directory path.</value>
  731. public ustring DirectoryPath {
  732. get => dirEntry.Text;
  733. set {
  734. dirEntry.Text = value;
  735. dirListView.Directory = value;
  736. }
  737. }
  738. /// <summary>
  739. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  740. /// </summary>
  741. /// <value>The allowed file types.</value>
  742. public string [] AllowedFileTypes {
  743. get => dirListView.AllowedFileTypes;
  744. set => dirListView.AllowedFileTypes = value;
  745. }
  746. /// <summary>
  747. /// Gets or sets a value indicating whether this <see cref="FileDialog"/> allows the file to be saved with a different extension
  748. /// </summary>
  749. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  750. public bool AllowsOtherFileTypes { get; set; }
  751. /// <summary>
  752. /// The File path that is currently shown on the panel
  753. /// </summary>
  754. /// <value>The absolute file path for the file path entered.</value>
  755. public ustring FilePath {
  756. get => dirListView.MakePath (nameEntry.Text.ToString ());
  757. set {
  758. nameEntry.Text = Path.GetFileName (value.ToString ());
  759. }
  760. }
  761. /// <summary>
  762. /// Check if the dialog was or not canceled.
  763. /// </summary>
  764. public bool Canceled { get => canceled; }
  765. }
  766. /// <summary>
  767. /// The <see cref="SaveDialog"/> provides an interactive dialog box for users to pick a file to
  768. /// save.
  769. /// </summary>
  770. /// <remarks>
  771. /// <para>
  772. /// To use, create an instance of <see cref="SaveDialog"/>, and pass it to
  773. /// <see cref="Application.Run(Func{Exception, bool})"/>. This will run the dialog modally,
  774. /// and when this returns, the <see cref="FileName"/>property will contain the selected file name or
  775. /// null if the user canceled.
  776. /// </para>
  777. /// </remarks>
  778. public class SaveDialog : FileDialog {
  779. /// <summary>
  780. /// Initializes a new <see cref="SaveDialog"/>.
  781. /// </summary>
  782. public SaveDialog () : this (title: string.Empty, message: string.Empty) { }
  783. /// <summary>
  784. /// Initializes a new <see cref="SaveDialog"/>.
  785. /// </summary>
  786. /// <param name="title">The title.</param>
  787. /// <param name="message">The message.</param>
  788. /// <param name="allowedTypes">The allowed types.</param>
  789. public SaveDialog (ustring title, ustring message, List<string> allowedTypes = null)
  790. : base (title, prompt: Strings.fdSave, nameFieldLabel: $"{Strings.fdSaveAs}:", message: message, allowedTypes) { }
  791. /// <summary>
  792. /// Gets the name of the file the user selected for saving, or null
  793. /// if the user canceled the <see cref="SaveDialog"/>.
  794. /// </summary>
  795. /// <value>The name of the file.</value>
  796. public ustring FileName {
  797. get {
  798. if (canceled)
  799. return null;
  800. return Path.GetFileName (FilePath.ToString ());
  801. }
  802. }
  803. }
  804. /// <summary>
  805. /// The <see cref="OpenDialog"/>provides an interactive dialog box for users to select files or directories.
  806. /// </summary>
  807. /// <remarks>
  808. /// <para>
  809. /// The open dialog can be used to select files for opening, it can be configured to allow
  810. /// multiple items to be selected (based on the AllowsMultipleSelection) variable and
  811. /// you can control whether this should allow files or directories to be selected.
  812. /// </para>
  813. /// <para>
  814. /// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
  815. /// <see cref="Application.Run(Func{Exception, bool})"/>. This will run the dialog modally,
  816. /// and when this returns, the list of files will be available on the <see cref="FilePaths"/> property.
  817. /// </para>
  818. /// <para>
  819. /// To select more than one file, users can use the spacebar, or control-t.
  820. /// </para>
  821. /// </remarks>
  822. public class OpenDialog : FileDialog {
  823. OpenMode openMode;
  824. /// <summary>
  825. /// Determine which <see cref="System.IO"/> type to open.
  826. /// </summary>
  827. public enum OpenMode {
  828. /// <summary>
  829. /// Opens only file or files.
  830. /// </summary>
  831. File,
  832. /// <summary>
  833. /// Opens only directory or directories.
  834. /// </summary>
  835. Directory,
  836. /// <summary>
  837. /// Opens files and directories.
  838. /// </summary>
  839. Mixed
  840. }
  841. /// <summary>
  842. /// Initializes a new <see cref="OpenDialog"/>.
  843. /// </summary>
  844. public OpenDialog () : this (title: string.Empty, message: string.Empty) { }
  845. /// <summary>
  846. /// Initializes a new <see cref="OpenDialog"/>.
  847. /// </summary>
  848. /// <param name="title">The title.</param>
  849. /// <param name="message">The message.</param>
  850. /// <param name="allowedTypes">The allowed types.</param>
  851. /// <param name="openMode">The open mode.</param>
  852. public OpenDialog (ustring title, ustring message, List<string> allowedTypes = null, OpenMode openMode = OpenMode.File) : base (title,
  853. prompt: openMode == OpenMode.File ? Strings.fdOpen : openMode == OpenMode.Directory ? Strings.fdSelectFolder : Strings.fdSelectMixed,
  854. nameFieldLabel: Strings.fdOpen, message: message, allowedTypes)
  855. {
  856. this.openMode = openMode;
  857. switch (openMode) {
  858. case OpenMode.File:
  859. CanChooseFiles = true;
  860. CanChooseDirectories = false;
  861. break;
  862. case OpenMode.Directory:
  863. CanChooseFiles = false;
  864. CanChooseDirectories = true;
  865. break;
  866. case OpenMode.Mixed:
  867. CanChooseFiles = true;
  868. CanChooseDirectories = true;
  869. AllowsMultipleSelection = true;
  870. break;
  871. }
  872. }
  873. /// <summary>
  874. /// Gets or sets a value indicating whether this <see cref="Terminal.Gui.OpenDialog"/> can choose files.
  875. /// </summary>
  876. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>. Defaults to <c>true</c></value>
  877. public bool CanChooseFiles {
  878. get => dirListView.canChooseFiles;
  879. set {
  880. dirListView.canChooseFiles = value;
  881. dirListView.Reload ();
  882. }
  883. }
  884. /// <summary>
  885. /// Gets or sets a value indicating whether this <see cref="OpenDialog"/> can choose directories.
  886. /// </summary>
  887. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c> defaults to <c>false</c>.</value>
  888. public bool CanChooseDirectories {
  889. get => dirListView.canChooseDirectories;
  890. set {
  891. dirListView.canChooseDirectories = value;
  892. dirListView.Reload ();
  893. }
  894. }
  895. /// <summary>
  896. /// Gets or sets a value indicating whether this <see cref="OpenDialog"/> allows multiple selection.
  897. /// </summary>
  898. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>, defaults to false.</value>
  899. public bool AllowsMultipleSelection {
  900. get => dirListView.allowsMultipleSelection;
  901. set {
  902. if (!value && openMode == OpenMode.Mixed) {
  903. return;
  904. }
  905. dirListView.allowsMultipleSelection = value;
  906. dirListView.Reload ();
  907. }
  908. }
  909. /// <summary>
  910. /// Returns the selected files, or an empty list if nothing has been selected
  911. /// </summary>
  912. /// <value>The file paths.</value>
  913. public IReadOnlyList<string> FilePaths {
  914. get => dirListView.FilePaths;
  915. }
  916. }
  917. }