OpenDialog.cs 2.1 KB

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