FileDialog.cs 27 KB

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