FileDialog.cs 27 KB

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