FileDialog.cs 28 KB

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