SaveDialog.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.IO.Abstractions;
  12. namespace Terminal.Gui.Views;
  13. /// <summary>Provides an interactive <see cref="Dialog"/> for selecting files or directories for saving</summary>
  14. /// <remarks>
  15. /// <para>
  16. /// To use, create an instance of <see cref="SaveDialog"/>, and pass it to
  17. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/>. This will run the dialog modally, and when this returns,
  18. /// the <see cref="FileName"/>property will contain the selected file name or null if the user canceled.
  19. /// </para>
  20. /// </remarks>
  21. public class SaveDialog : FileDialog
  22. {
  23. /// <summary>Initializes a new <see cref="SaveDialog"/>.</summary>
  24. public SaveDialog ()
  25. {
  26. Style.OkButtonText = Strings.btnSave;
  27. }
  28. internal SaveDialog (IFileSystem fileSystem) : base (fileSystem)
  29. {
  30. Style.OkButtonText = Strings.btnSave;
  31. }
  32. /// <summary>
  33. /// Gets the name of the file the user selected for saving, or null if the user canceled the
  34. /// <see cref="SaveDialog"/>.
  35. /// </summary>
  36. /// <value>The name of the file.</value>
  37. public string FileName => Canceled ? null : Path;
  38. /// <summary>Gets the default title for the <see cref="SaveDialog"/>.</summary>
  39. /// <returns></returns>
  40. protected override string GetDefaultTitle ()
  41. {
  42. List<string> titleParts = [Strings.fdSave]
  43. ;
  44. if (MustExist)
  45. {
  46. titleParts.Add (Strings.fdExisting);
  47. }
  48. switch (OpenMode)
  49. {
  50. case OpenMode.File:
  51. titleParts.Add (Strings.fdFile);
  52. break;
  53. case OpenMode.Directory:
  54. titleParts.Add (Strings.fdDirectory);
  55. break;
  56. }
  57. return string.Join (' ', titleParts);
  58. }
  59. }