FileDialog.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. List<(string,bool,bool)> infos;
  21. internal bool canChooseFiles = true;
  22. internal bool canChooseDirectories = false;
  23. internal bool allowsMultipleSelection = false;
  24. FileDialog host;
  25. public DirListView (FileDialog host)
  26. {
  27. infos = new List<(string,bool,bool)> ();
  28. CanFocus = true;
  29. this.host = host;
  30. }
  31. bool IsAllowed (FileSystemInfo fsi)
  32. {
  33. if (fsi.Attributes.HasFlag (FileAttributes.Directory))
  34. return true;
  35. if (allowedFileTypes == null)
  36. return true;
  37. foreach (var ft in allowedFileTypes)
  38. if (fsi.Name.EndsWith (ft))
  39. return true;
  40. return false;
  41. }
  42. internal void Reload ()
  43. {
  44. dirInfo = new DirectoryInfo (directory.ToString ());
  45. infos = (from x in dirInfo.GetFileSystemInfos ()
  46. where IsAllowed (x)
  47. orderby (!x.Attributes.HasFlag (FileAttributes.Directory)) + x.Name
  48. select (x.Name, x.Attributes.HasFlag (FileAttributes.Directory), false)).ToList ();
  49. infos.Insert (0, ("..", true, false));
  50. top = 0;
  51. selected = 0;
  52. SetNeedsDisplay ();
  53. }
  54. ustring directory;
  55. public ustring Directory {
  56. get => directory;
  57. set {
  58. if (directory == value)
  59. return;
  60. directory = value;
  61. Reload ();
  62. }
  63. }
  64. public override void PositionCursor ()
  65. {
  66. Move (0, selected - top);
  67. }
  68. int lastSelected;
  69. bool shiftOnWheel;
  70. public override bool MouseEvent (MouseEvent me)
  71. {
  72. if ((me.Flags & (MouseFlags.Button1Clicked | MouseFlags.Button1DoubleClicked |
  73. MouseFlags.WheeledUp | MouseFlags.WheeledDown)) == 0)
  74. return false;
  75. if (!HasFocus)
  76. SuperView.SetFocus (this);
  77. if (infos == null)
  78. return false;
  79. if (me.Y + top >= infos.Count)
  80. return true;
  81. int lastSelectedCopy = shiftOnWheel ? lastSelected : selected;
  82. switch (me.Flags) {
  83. case MouseFlags.Button1Clicked:
  84. SetSelected (me);
  85. SelectionChanged ();
  86. SetNeedsDisplay ();
  87. break;
  88. case MouseFlags.Button1DoubleClicked:
  89. SetSelected (me);
  90. if (ExecuteSelection ()) {
  91. host.canceled = false;
  92. Application.RequestStop ();
  93. }
  94. return true;
  95. case MouseFlags.Button1Clicked | MouseFlags.ButtonShift:
  96. SetSelected (me);
  97. if (shiftOnWheel)
  98. lastSelected = lastSelectedCopy;
  99. shiftOnWheel = false;
  100. PerformMultipleSelection (lastSelected);
  101. return true;
  102. case MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl:
  103. SetSelected (me);
  104. PerformMultipleSelection ();
  105. return true;
  106. case MouseFlags.WheeledUp:
  107. SetSelected (me);
  108. selected = lastSelected;
  109. MoveUp ();
  110. return true;
  111. case MouseFlags.WheeledDown:
  112. SetSelected (me);
  113. selected = lastSelected;
  114. MoveDown ();
  115. return true;
  116. case MouseFlags.WheeledUp | MouseFlags.ButtonShift:
  117. SetSelected (me);
  118. selected = lastSelected;
  119. lastSelected = lastSelectedCopy;
  120. shiftOnWheel = true;
  121. MoveUp ();
  122. return true;
  123. case MouseFlags.WheeledDown | MouseFlags.ButtonShift:
  124. SetSelected (me);
  125. selected = lastSelected;
  126. lastSelected = lastSelectedCopy;
  127. shiftOnWheel = true;
  128. MoveDown ();
  129. return true;
  130. }
  131. return true;
  132. }
  133. void SetSelected (MouseEvent me)
  134. {
  135. lastSelected = selected;
  136. selected = top + me.Y;
  137. }
  138. void DrawString (int line, string str)
  139. {
  140. var f = Frame;
  141. var width = f.Width;
  142. var ustr = ustring.Make (str);
  143. Move (allowsMultipleSelection ? 3 : 2, line);
  144. int byteLen = ustr.Length;
  145. int used = 0;
  146. for (int i = 0; i < byteLen;) {
  147. (var rune, var size) = Utf8.DecodeRune (ustr, i, i - byteLen);
  148. var count = Rune.ColumnWidth (rune);
  149. if (used + count >= width)
  150. break;
  151. Driver.AddRune (rune);
  152. used += count;
  153. i += size;
  154. }
  155. for (; used < width; used++) {
  156. Driver.AddRune (' ');
  157. }
  158. }
  159. public override void Redraw (Rect region)
  160. {
  161. var current = ColorScheme.Focus;
  162. Driver.SetAttribute (current);
  163. Move (0, 0);
  164. var f = Frame;
  165. var item = top;
  166. bool focused = HasFocus;
  167. var width = region.Width;
  168. for (int row = 0; row < f.Height; row++, item++) {
  169. bool isSelected = item == selected;
  170. Move (0, row);
  171. var newcolor = focused ? (isSelected ? ColorScheme.HotNormal : ColorScheme.Focus) : ColorScheme.Focus;
  172. if (newcolor != current) {
  173. Driver.SetAttribute (newcolor);
  174. current = newcolor;
  175. }
  176. if (item >= infos.Count) {
  177. for (int c = 0; c < f.Width; c++)
  178. Driver.AddRune (' ');
  179. continue;
  180. }
  181. var fi = infos [item];
  182. Driver.AddRune (isSelected ? '>' : ' ');
  183. if (allowsMultipleSelection)
  184. Driver.AddRune (fi.Item3 ? '*' : ' ');
  185. if (fi.Item2)
  186. Driver.AddRune ('/');
  187. else
  188. Driver.AddRune (' ');
  189. DrawString (row, fi.Item1);
  190. }
  191. }
  192. public Action<(string, bool)> SelectedChanged { get; set; }
  193. public Action<ustring> DirectoryChanged { get; set; }
  194. public Action<ustring> FileChanged { get; set; }
  195. void SelectionChanged ()
  196. {
  197. if (FilePaths.Count > 0)
  198. FileChanged?.Invoke (string.Join (", ", GetFilesName (FilePaths)));
  199. else
  200. FileChanged?.Invoke (infos [selected].Item2 ? "" : Path.GetFileName (infos [selected].Item1));
  201. if (SelectedChanged != null) {
  202. var sel = infos [selected];
  203. SelectedChanged ((sel.Item1, sel.Item2));
  204. }
  205. }
  206. List<string> GetFilesName (IReadOnlyList<string> files)
  207. {
  208. List<string> filesName = new List<string> ();
  209. foreach (var file in files) {
  210. filesName.Add (Path.GetFileName (file));
  211. }
  212. return filesName;
  213. }
  214. public override bool ProcessKey (KeyEvent keyEvent)
  215. {
  216. switch (keyEvent.Key) {
  217. case Key.CursorUp:
  218. case Key.ControlP:
  219. MoveUp ();
  220. return true;
  221. case Key.CursorDown:
  222. case Key.ControlN:
  223. MoveDown ();
  224. return true;
  225. case Key.ControlV:
  226. case Key.PageDown:
  227. var n = (selected + Frame.Height);
  228. if (n > infos.Count)
  229. n = infos.Count - 1;
  230. if (n != selected) {
  231. selected = n;
  232. if (infos.Count >= Frame.Height)
  233. top = selected;
  234. else
  235. top = 0;
  236. SelectionChanged ();
  237. SetNeedsDisplay ();
  238. }
  239. return true;
  240. case Key.Enter:
  241. if (ExecuteSelection ())
  242. return false;
  243. else
  244. return true;
  245. case Key.PageUp:
  246. n = (selected - Frame.Height);
  247. if (n < 0)
  248. n = 0;
  249. if (n != selected) {
  250. selected = n;
  251. top = selected;
  252. SelectionChanged ();
  253. SetNeedsDisplay ();
  254. }
  255. return true;
  256. case Key.Space:
  257. case Key.ControlT:
  258. PerformMultipleSelection ();
  259. return true;
  260. case Key.Home:
  261. MoveFirst ();
  262. return true;
  263. case Key.End:
  264. MoveLast ();
  265. return true;
  266. }
  267. return base.ProcessKey (keyEvent);
  268. }
  269. void MoveLast ()
  270. {
  271. selected = infos.Count - 1;
  272. top = infos.Count () - 1;
  273. SelectionChanged ();
  274. SetNeedsDisplay ();
  275. }
  276. void MoveFirst ()
  277. {
  278. selected = 0;
  279. top = 0;
  280. SelectionChanged ();
  281. SetNeedsDisplay ();
  282. }
  283. void MoveDown ()
  284. {
  285. if (selected + 1 < infos.Count) {
  286. selected++;
  287. if (selected >= top + Frame.Height)
  288. top++;
  289. SelectionChanged ();
  290. SetNeedsDisplay ();
  291. }
  292. }
  293. void MoveUp ()
  294. {
  295. if (selected > 0) {
  296. selected--;
  297. if (selected < top)
  298. top = selected;
  299. SelectionChanged ();
  300. SetNeedsDisplay ();
  301. }
  302. }
  303. internal bool ExecuteSelection ()
  304. {
  305. var isDir = infos [selected].Item2;
  306. if (isDir) {
  307. Directory = Path.GetFullPath (Path.Combine (Path.GetFullPath (Directory.ToString ()), infos [selected].Item1));
  308. DirectoryChanged?.Invoke (Directory);
  309. } else {
  310. FileChanged?.Invoke (infos [selected].Item1);
  311. if (canChooseFiles) {
  312. // Ensures that at least one file is selected.
  313. if (FilePaths.Count == 0)
  314. PerformMultipleSelection ();
  315. // Let the OK handler take it over
  316. return true;
  317. }
  318. // No files allowed, do not let the default handler take it.
  319. }
  320. return false;
  321. }
  322. void PerformMultipleSelection (int? firstSelected = null)
  323. {
  324. if (allowsMultipleSelection) {
  325. int first = Math.Min (firstSelected ?? selected, selected);
  326. int last = Math.Max (selected, firstSelected ?? selected);
  327. for (int i = first; i <= last; i++) {
  328. if ((canChooseFiles && infos [i].Item2 == false) ||
  329. (canChooseDirectories && infos [i].Item2 &&
  330. infos [i].Item1 != "..")) {
  331. infos [i] = (infos [i].Item1, infos [i].Item2, !infos [i].Item3);
  332. }
  333. }
  334. SelectionChanged ();
  335. SetNeedsDisplay ();
  336. }
  337. }
  338. string [] allowedFileTypes;
  339. public string [] AllowedFileTypes {
  340. get => allowedFileTypes;
  341. set {
  342. allowedFileTypes = value;
  343. Reload ();
  344. }
  345. }
  346. public string MakePath (string relativePath)
  347. {
  348. return Path.GetFullPath (Path.Combine (Directory.ToString (), relativePath));
  349. }
  350. public IReadOnlyList<string> FilePaths {
  351. get {
  352. if (allowsMultipleSelection) {
  353. var res = new List<string> ();
  354. foreach (var item in infos)
  355. if (item.Item3)
  356. res.Add (MakePath (item.Item1));
  357. return res;
  358. } else {
  359. if (infos [selected].Item2) {
  360. if (canChooseDirectories)
  361. return new List<string> () { MakePath (infos [selected].Item1) };
  362. return Array.Empty<string> ();
  363. } else {
  364. if (canChooseFiles)
  365. return new List<string> () { MakePath (infos [selected].Item1) };
  366. return Array.Empty<string> ();
  367. }
  368. }
  369. }
  370. }
  371. }
  372. /// <summary>
  373. /// Base class for the OpenDialog and the SaveDialog
  374. /// </summary>
  375. public class FileDialog : Dialog {
  376. Button prompt, cancel;
  377. Label nameFieldLabel, message, dirLabel;
  378. TextField dirEntry, nameEntry;
  379. internal DirListView dirListView;
  380. /// <summary>
  381. /// Constructor for the OpenDialog and the SaveDialog.
  382. /// </summary>
  383. /// <param name="title">The title.</param>
  384. /// <param name="prompt">The prompt.</param>
  385. /// <param name="nameFieldLabel">The name field label.</param>
  386. /// <param name="message">The message.</param>
  387. public FileDialog (ustring title, ustring prompt, ustring nameFieldLabel, ustring message) : base (title, Driver.Cols - 20, Driver.Rows - 5, null)
  388. {
  389. this.message = new Label (Rect.Empty, "MESSAGE" + message);
  390. var msgLines = Label.MeasureLines (message, Driver.Cols - 20);
  391. dirLabel = new Label ("Directory: ") {
  392. X = 1,
  393. Y = 1 + msgLines
  394. };
  395. dirEntry = new TextField ("") {
  396. X = Pos.Right (dirLabel),
  397. Y = 1 + msgLines,
  398. Width = Dim.Fill () - 1
  399. };
  400. Add (dirLabel, dirEntry);
  401. this.nameFieldLabel = new Label ("Open: ") {
  402. X = 6,
  403. Y = 3 + msgLines,
  404. };
  405. nameEntry = new TextField ("") {
  406. X = Pos.Left (dirEntry),
  407. Y = 3 + msgLines,
  408. Width = Dim.Fill () - 1
  409. };
  410. Add (this.nameFieldLabel, nameEntry);
  411. dirListView = new DirListView (this) {
  412. X = 1,
  413. Y = 3 + msgLines + 2,
  414. Width = Dim.Fill () - 3,
  415. Height = Dim.Fill () - 2,
  416. };
  417. DirectoryPath = Path.GetFullPath (Environment.CurrentDirectory);
  418. Add (dirListView);
  419. dirListView.DirectoryChanged = (dir) => dirEntry.Text = dir;
  420. dirListView.FileChanged = (file) => nameEntry.Text = file;
  421. this.cancel = new Button ("Cancel");
  422. this.cancel.Clicked += () => {
  423. canceled = true;
  424. Application.RequestStop ();
  425. };
  426. AddButton (cancel);
  427. this.prompt = new Button (prompt) {
  428. IsDefault = true,
  429. };
  430. this.prompt.Clicked += () => {
  431. dirListView.ExecuteSelection ();
  432. canceled = false;
  433. Application.RequestStop ();
  434. };
  435. AddButton (this.prompt);
  436. // On success, we will set this to false.
  437. canceled = true;
  438. }
  439. internal bool canceled;
  440. ///<inheritdoc cref="WillPresent"/>
  441. public override void WillPresent ()
  442. {
  443. base.WillPresent ();
  444. //SetFocus (nameEntry);
  445. }
  446. /// <summary>
  447. /// Gets or sets the prompt label for the button displayed to the user
  448. /// </summary>
  449. /// <value>The prompt.</value>
  450. public ustring Prompt {
  451. get => prompt.Text;
  452. set {
  453. prompt.Text = value;
  454. }
  455. }
  456. /// <summary>
  457. /// Gets or sets the name field label.
  458. /// </summary>
  459. /// <value>The name field label.</value>
  460. public ustring NameFieldLabel {
  461. get => nameFieldLabel.Text;
  462. set {
  463. nameFieldLabel.Text = value;
  464. }
  465. }
  466. /// <summary>
  467. /// Gets or sets the message displayed to the user, defaults to nothing
  468. /// </summary>
  469. /// <value>The message.</value>
  470. public ustring Message {
  471. get => message.Text;
  472. set {
  473. message.Text = value;
  474. }
  475. }
  476. /// <summary>
  477. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> can create directories.
  478. /// </summary>
  479. /// <value><c>true</c> if can create directories; otherwise, <c>false</c>.</value>
  480. public bool CanCreateDirectories { get; set; }
  481. /// <summary>
  482. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> is extension hidden.
  483. /// </summary>
  484. /// <value><c>true</c> if is extension hidden; otherwise, <c>false</c>.</value>
  485. public bool IsExtensionHidden { get; set; }
  486. /// <summary>
  487. /// Gets or sets the directory path for this panel
  488. /// </summary>
  489. /// <value>The directory path.</value>
  490. public ustring DirectoryPath {
  491. get => dirEntry.Text;
  492. set {
  493. dirEntry.Text = value;
  494. dirListView.Directory = value;
  495. }
  496. }
  497. /// <summary>
  498. /// The array of filename extensions allowed, or null if all file extensions are allowed.
  499. /// </summary>
  500. /// <value>The allowed file types.</value>
  501. public string [] AllowedFileTypes {
  502. get => dirListView.AllowedFileTypes;
  503. set => dirListView.AllowedFileTypes = value;
  504. }
  505. /// <summary>
  506. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.FileDialog"/> allows the file to be saved with a different extension
  507. /// </summary>
  508. /// <value><c>true</c> if allows other file types; otherwise, <c>false</c>.</value>
  509. public bool AllowsOtherFileTypes { get; set; }
  510. /// <summary>
  511. /// The File path that is currently shown on the panel
  512. /// </summary>
  513. /// <value>The absolute file path for the file path entered.</value>
  514. public ustring FilePath {
  515. get => dirListView.MakePath(nameEntry.Text.ToString());
  516. set {
  517. nameEntry.Text = Path.GetFileName(value.ToString());
  518. }
  519. }
  520. /// <summary>
  521. /// Check if the dialog was or not canceled.
  522. /// </summary>
  523. public bool Canceled { get => canceled; }
  524. }
  525. /// <summary>
  526. /// The save dialog provides an interactive dialog box for users to pick a file to
  527. /// save.
  528. /// </summary>
  529. /// <remarks>
  530. /// <para>
  531. /// To use it, create an instance of the SaveDialog, and then
  532. /// call Application.Run on the resulting instance. This will run the dialog modally,
  533. /// and when this returns, the FileName property will contain the selected value or
  534. /// null if the user canceled.
  535. /// </para>
  536. /// </remarks>
  537. public class SaveDialog : FileDialog {
  538. /// <summary>
  539. /// Constructor of the save dialog.
  540. /// </summary>
  541. /// <param name="title">The title.</param>
  542. /// <param name="message">The message.</param>
  543. public SaveDialog (ustring title, ustring message) : base (title, prompt: "Save", nameFieldLabel: "Save as:", message: message)
  544. {
  545. }
  546. /// <summary>
  547. /// Gets the name of the file the user selected for saving, or null
  548. /// if the user canceled the dialog box.
  549. /// </summary>
  550. /// <value>The name of the file.</value>
  551. public ustring FileName {
  552. get {
  553. if (canceled)
  554. return null;
  555. return Path.GetFileName(FilePath.ToString());
  556. }
  557. }
  558. }
  559. /// <summary>
  560. /// The Open Dialog provides an interactive dialog box for users to select files or directories.
  561. /// </summary>
  562. /// <remarks>
  563. /// <para>
  564. /// The open dialog can be used to select files for opening, it can be configured to allow
  565. /// multiple items to be selected (based on the AllowsMultipleSelection) variable and
  566. /// you can control whether this should allow files or directories to be selected.
  567. /// </para>
  568. /// <para>
  569. /// To use it, create an instance of the OpenDialog, configure its properties, and then
  570. /// call Application.Run on the resulting instance. This will run the dialog modally,
  571. /// and when this returns, the list of filds will be available on the FilePaths property.
  572. /// </para>
  573. /// <para>
  574. /// To select more than one file, users can use the spacebar, or control-t.
  575. /// </para>
  576. /// </remarks>
  577. public class OpenDialog : FileDialog {
  578. /// <summary>
  579. /// Constructor of the Open Dialog.
  580. /// </summary>
  581. /// <param name="title"></param>
  582. /// <param name="message"></param>
  583. public OpenDialog (ustring title, ustring message) : base (title, prompt: "Open", nameFieldLabel: "Open", message: message)
  584. {
  585. }
  586. /// <summary>
  587. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose files.
  588. /// </summary>
  589. /// <value><c>true</c> if can choose files; otherwise, <c>false</c>. Defaults to <c>true</c></value>
  590. public bool CanChooseFiles {
  591. get => dirListView.canChooseFiles;
  592. set {
  593. dirListView.canChooseFiles = value;
  594. dirListView.Reload ();
  595. }
  596. }
  597. /// <summary>
  598. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> can choose directories.
  599. /// </summary>
  600. /// <value><c>true</c> if can choose directories; otherwise, <c>false</c> defaults to <c>false</c>.</value>
  601. public bool CanChooseDirectories {
  602. get => dirListView.canChooseDirectories;
  603. set {
  604. dirListView.canChooseDirectories = value;
  605. dirListView.Reload ();
  606. }
  607. }
  608. /// <summary>
  609. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.OpenDialog"/> allows multiple selection.
  610. /// </summary>
  611. /// <value><c>true</c> if allows multiple selection; otherwise, <c>false</c>, defaults to false.</value>
  612. public bool AllowsMultipleSelection {
  613. get => dirListView.allowsMultipleSelection;
  614. set {
  615. dirListView.allowsMultipleSelection = value;
  616. dirListView.Reload ();
  617. }
  618. }
  619. /// <summary>
  620. /// Returns the selected files, or an empty list if nothing has been selected
  621. /// </summary>
  622. /// <value>The file paths.</value>
  623. public IReadOnlyList<string> FilePaths {
  624. get => dirListView.FilePaths;
  625. }
  626. }
  627. }