OpenDialog.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. using System.Collections.ObjectModel;
  18. namespace Terminal.Gui {
  19. /// <summary>
  20. /// Determine which <see cref="System.IO"/> type to open.
  21. /// </summary>
  22. public enum OpenMode {
  23. /// <summary>
  24. /// Opens only file or files.
  25. /// </summary>
  26. File,
  27. /// <summary>
  28. /// Opens only directory or directories.
  29. /// </summary>
  30. Directory,
  31. /// <summary>
  32. /// Opens files and directories.
  33. /// </summary>
  34. Mixed
  35. }
  36. /// <summary>
  37. /// The <see cref="OpenDialog"/>provides an interactive dialog box for users to select files or directories.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// The open dialog can be used to select files for opening, it can be configured to allow
  42. /// multiple items to be selected (based on the AllowsMultipleSelection) variable and
  43. /// you can control whether this should allow files or directories to be selected.
  44. /// </para>
  45. /// <para>
  46. /// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
  47. /// <see cref="Application.Run(Func{Exception, bool})"/>. This will run the dialog modally,
  48. /// and when this returns, the list of files will be available on the <see cref="FilePaths"/> property.
  49. /// </para>
  50. /// <para>
  51. /// To select more than one file, users can use the spacebar, or control-t.
  52. /// </para>
  53. /// </remarks>
  54. public class OpenDialog : FileDialog {
  55. /// <summary>
  56. /// Initializes a new <see cref="OpenDialog"/>.
  57. /// </summary>
  58. public OpenDialog () : this (title: string.Empty) { }
  59. /// <summary>
  60. /// Initializes a new <see cref="OpenDialog"/>.
  61. /// </summary>
  62. /// <param name="title">The title.</param>
  63. /// <param name="allowedTypes">The allowed types.</param>
  64. /// <param name="openMode">The open mode.</param>
  65. public OpenDialog (ustring title, List<IAllowedType> allowedTypes = null, OpenMode openMode = OpenMode.File)
  66. {
  67. this.OpenMode = openMode;
  68. Title = title;
  69. Style.OkButtonText = openMode == OpenMode.File ? Strings.fdOpen : openMode == OpenMode.Directory ? Strings.fdSelectFolder : Strings.fdSelectMixed;
  70. if (allowedTypes != null) {
  71. AllowedTypes = allowedTypes;
  72. }
  73. }
  74. /// <summary>
  75. /// Returns the selected files, or an empty list if nothing has been selected
  76. /// </summary>
  77. /// <value>The file paths.</value>
  78. public IReadOnlyList<string> FilePaths {
  79. get => Canceled ? Enumerable.Empty<string> ().ToList().AsReadOnly()
  80. : AllowsMultipleSelection ? base.MultiSelected : new ReadOnlyCollection<string>(new [] { Path });
  81. }
  82. }
  83. }