SaveDialog.cs 2.2 KB

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