OpenDialog.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.Collections.ObjectModel;
  12. using Terminal.Gui.Resources;
  13. namespace Terminal.Gui;
  14. /// <summary>Determine which <see cref="System.IO"/> type to open.</summary>
  15. public enum OpenMode
  16. {
  17. /// <summary>Opens only file or files.</summary>
  18. File,
  19. /// <summary>Opens only directory or directories.</summary>
  20. Directory,
  21. /// <summary>Opens files and directories.</summary>
  22. Mixed
  23. }
  24. /// <summary>The <see cref="OpenDialog"/>provides an interactive dialog box for users to select files or directories.</summary>
  25. /// <remarks>
  26. /// <para>
  27. /// The open dialog can be used to select files for opening, it can be configured to allow multiple items to be
  28. /// selected (based on the AllowsMultipleSelection) variable and you can control whether this should allow files or
  29. /// directories to be selected.
  30. /// </para>
  31. /// <para>
  32. /// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
  33. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/>. This will run the dialog modally, and when this returns,
  34. /// the list of files will be available on the <see cref="FilePaths"/> property.
  35. /// </para>
  36. /// <para>To select more than one file, users can use the spacebar, or control-t.</para>
  37. /// </remarks>
  38. public class OpenDialog : FileDialog
  39. {
  40. /// <summary>Initializes a new <see cref="OpenDialog"/>.</summary>
  41. public OpenDialog () { }
  42. /// <summary>Returns the selected files, or an empty list if nothing has been selected</summary>
  43. /// <value>The file paths.</value>
  44. public IReadOnlyList<string> FilePaths =>
  45. Canceled ? Enumerable.Empty<string> ().ToList ().AsReadOnly () :
  46. AllowsMultipleSelection ? MultiSelected : new ReadOnlyCollection<string> (new [] { Path });
  47. /// <inheritdoc/>
  48. public override OpenMode OpenMode
  49. {
  50. get => base.OpenMode;
  51. set
  52. {
  53. base.OpenMode = value;
  54. Style.OkButtonText = value == OpenMode.File ? Strings.btnOpen :
  55. value == OpenMode.Directory ? Strings.fdSelectFolder : Strings.fdSelectMixed;
  56. }
  57. }
  58. }