FileDialog.cs 29 KB

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