OpenDialog.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #nullable disable
  2. //
  3. // FileDialog.cs: File system dialogs for open and save
  4. //
  5. // TODO:
  6. // * Add directory selector
  7. // * Implement subclasses
  8. // * Figure out why message text does not show
  9. // * Remove the extra space when message does not show
  10. // * Use a line separator to show the file listing, so we can use same colors as the rest
  11. // * DirListView: Add mouse support
  12. using System.Collections.ObjectModel;
  13. namespace Terminal.Gui.Views;
  14. /// <summary>Provides an interactive <see cref="Dialog"/> for selecting files or directories for opening</summary>
  15. /// <remarks>
  16. /// <para>
  17. /// The open dialog can be used to select files for opening, it can be configured to allow multiple items to be
  18. /// selected (based on the AllowsMultipleSelection) variable, and you can control whether this should allow files or
  19. /// directories to be selected.
  20. /// </para>
  21. /// <para>
  22. /// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
  23. /// <see cref="IApplication.Run(IRunnable, Func{Exception, bool})"/>. This will run the dialog modally, and when this returns,
  24. /// the list of files will be available on the <see cref="FilePaths"/> property.
  25. /// </para>
  26. /// <para>To select more than one file, users can use the space key, or CTRL-T.</para>
  27. /// </remarks>
  28. public class OpenDialog : FileDialog
  29. {
  30. /// <summary>Initializes a new <see cref="OpenDialog"/>.</summary>
  31. public OpenDialog () { }
  32. /// <summary>Returns the selected files, or an empty list if nothing has been selected</summary>
  33. /// <value>The file paths.</value>
  34. public IReadOnlyList<string> FilePaths =>
  35. Canceled ? Enumerable.Empty<string> ().ToList ().AsReadOnly () :
  36. AllowsMultipleSelection ? MultiSelected : new ReadOnlyCollection<string> (new [] { Path });
  37. /// <inheritdoc/>
  38. public override OpenMode OpenMode
  39. {
  40. get => base.OpenMode;
  41. set
  42. {
  43. base.OpenMode = value;
  44. Style.OkButtonText = value == OpenMode.File ? Strings.btnOpen :
  45. value == OpenMode.Directory ? Strings.fdSelectFolder : Strings.fdSelectMixed;
  46. }
  47. }
  48. }