FileDialogStyle.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Globalization;
  2. using System.IO.Abstractions;
  3. using Terminal.Gui.Resources;
  4. using static System.Environment;
  5. namespace Terminal.Gui;
  6. /// <summary>Stores style settings for <see cref="FileDialog"/>.</summary>
  7. public class FileDialogStyle
  8. {
  9. private readonly IFileSystem _fileSystem;
  10. /// <summary>Creates a new instance of the <see cref="FileDialogStyle"/> class.</summary>
  11. public FileDialogStyle (IFileSystem fileSystem)
  12. {
  13. _fileSystem = fileSystem;
  14. TreeRootGetter = DefaultTreeRootGetter;
  15. DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
  16. }
  17. /// <summary>Gets or sets the text on the 'Cancel' button.</summary>
  18. public string CancelButtonText { get; set; } = Strings.btnCancel;
  19. /// <summary>
  20. /// Gets or sets the class thatis responsible for determining which color to use to represent files and
  21. /// directories when <see cref="UseColors"/> is <see langword="true"/>.
  22. /// </summary>
  23. public FileSystemColorProvider ColorProvider { get; set; } = new ();
  24. /// <summary>
  25. /// Gets or sets the culture to use (e.g. for number formatting). Defaults to
  26. /// <see cref="CultureInfo.CurrentUICulture"/>.
  27. /// </summary>
  28. public CultureInfo Culture { get; set; } = CultureInfo.CurrentUICulture;
  29. /// <summary>
  30. /// Gets or sets the format to use for date/times in the Modified column. Defaults to
  31. /// <see cref="DateTimeFormatInfo.SortableDateTimePattern"/> of the <see cref="CultureInfo.CurrentCulture"/>
  32. /// </summary>
  33. public string DateFormat { get; set; }
  34. /// <summary>
  35. /// Gets or sets the default value to use for <see cref="UseColors"/>. This can be populated from .tui config
  36. /// files via <see cref="ConfigurationManager"/>
  37. /// </summary>
  38. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  39. public static bool DefaultUseColors { get; set; }
  40. /// <summary>
  41. /// Gets or sets the default value to use for <see cref="UseUnicodeCharacters"/>. This can be populated from .tui
  42. /// config files via <see cref="ConfigurationManager"/>
  43. /// </summary>
  44. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  45. public static bool DefaultUseUnicodeCharacters { get; set; }
  46. /// <summary>
  47. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.File"/> and user enters the
  48. /// name of an existing directory (File system cannot have a folder with the same name as a file).
  49. /// </summary>
  50. public string DirectoryAlreadyExistsFeedback { get; set; } = Strings.fdDirectoryAlreadyExistsFeedback;
  51. /// <summary>
  52. /// Gets or sets error message when user selects a directory that does not exist and <see cref="OpenMode"/> is
  53. /// <see cref="OpenMode.Directory"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  54. /// </summary>
  55. public string DirectoryMustExistFeedback { get; set; } = Strings.fdDirectoryMustExistFeedback;
  56. /// <summary>
  57. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/> and user
  58. /// enters the name of an existing file (File system cannot have a folder with the same name as a file).
  59. /// </summary>
  60. public string FileAlreadyExistsFeedback { get; set; } = Strings.fdFileAlreadyExistsFeedback;
  61. /// <summary>
  62. /// Gets or sets error message when user selects a file that does not exist and <see cref="OpenMode"/> is
  63. /// <see cref="OpenMode.File"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  64. /// </summary>
  65. public string FileMustExistFeedback { get; set; } = Strings.fdFileMustExistFeedback;
  66. /// <summary>Gets or sets the header text displayed in the Filename column of the files table.</summary>
  67. public string FilenameColumnName { get; set; } = Strings.fdFilename;
  68. /// <summary>
  69. /// Gets or sets error message when user selects a file/dir that does not exist and <see cref="OpenMode"/> is
  70. /// <see cref="OpenMode.Mixed"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  71. /// </summary>
  72. public string FileOrDirectoryMustExistFeedback { get; set; } = Strings.fdFileOrDirectoryMustExistFeedback;
  73. /// <summary>
  74. /// Gets or sets whether to flip the order of the Ok and Cancel buttons. Defaults to false (Ok button then Cancel
  75. /// button). Set to true to show Cancel button on left then Ok button instead.
  76. /// </summary>
  77. public bool FlipOkCancelButtonLayoutOrder { get; set; }
  78. /// <summary>Gets or sets the class responsible for determining which symbol to use to represent files and directories.</summary>
  79. public FileSystemIconProvider IconProvider { get; set; } = new ();
  80. /// <summary>Gets or sets the header text displayed in the Modified column of the files table.</summary>
  81. public string ModifiedColumnName { get; set; } = Strings.fdModified;
  82. /// <summary>Gets or sets the text on the 'Ok' button. Typically you may want to change this to "Open" or "Save" etc.</summary>
  83. public string OkButtonText { get; set; } = Strings.btnOk;
  84. /// <summary>Gets or sets the text displayed in the 'Path' text box when user has not supplied any input yet.</summary>
  85. public string PathCaption { get; set; } = Strings.fdPathCaption;
  86. /// <summary>Gets or sets the text displayed in the 'Search' text box when user has not supplied any input yet.</summary>
  87. public string SearchCaption { get; set; } = Strings.fdSearchCaption;
  88. /// <summary>Gets or sets the header text displayed in the Size column of the files table.</summary>
  89. public string SizeColumnName { get; set; } = Strings.fdSize;
  90. /// <summary>Gets the style settings for the table of files (in currently selected directory).</summary>
  91. public TableStyle TableStyle { get; internal set; }
  92. /// <summary>
  93. /// Gets or Sets the method for getting the root tree objects that are displayed in the collapse-able tree in the
  94. /// <see cref="FileDialog"/>. Defaults to all accessible <see cref="System.Environment.GetLogicalDrives"/> and unique
  95. /// <see cref="Environment.SpecialFolder"/>.
  96. /// </summary>
  97. /// <remarks>Must be configured before showing the dialog.</remarks>
  98. public Func<Dictionary<IDirectoryInfo, string>> TreeRootGetter { get; set; }
  99. /// <summary>Gets the style settings for the collapse-able directory/places tree</summary>
  100. public TreeStyle TreeStyle { get; internal set; }
  101. /// <summary>Gets or sets the header text displayed in the Type column of the files table.</summary>
  102. public string TypeColumnName { get; set; } = Strings.fdType;
  103. /// <summary>
  104. /// Gets or Sets a value indicating whether different colors should be used for different file types/directories.
  105. /// Defaults to false.
  106. /// </summary>
  107. public bool UseColors { get; set; } = DefaultUseColors;
  108. /// <summary>Gets or sets whether to use advanced unicode characters which might not be installed on all users computers.</summary>
  109. public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
  110. /// <summary>
  111. /// Gets or sets error message when user attempts to select a file type that is not one of
  112. /// <see cref="FileDialog.AllowedTypes"/>
  113. /// </summary>
  114. public string WrongFileTypeFeedback { get; set; } = Strings.fdWrongFileTypeFeedback;
  115. private Dictionary<IDirectoryInfo, string> DefaultTreeRootGetter ()
  116. {
  117. Dictionary<IDirectoryInfo, string> roots = new ();
  118. try
  119. {
  120. foreach (string d in GetLogicalDrives ())
  121. {
  122. IDirectoryInfo dir = _fileSystem.DirectoryInfo.New (d);
  123. if (!roots.ContainsKey (dir))
  124. {
  125. roots.Add (dir, d);
  126. }
  127. }
  128. }
  129. catch (Exception)
  130. {
  131. // Cannot get the system disks thats fine
  132. }
  133. try
  134. {
  135. foreach (SpecialFolder special in Enum.GetValues (typeof (SpecialFolder)).Cast<SpecialFolder> ())
  136. {
  137. try
  138. {
  139. string path = GetFolderPath (special);
  140. if (string.IsNullOrWhiteSpace (path))
  141. {
  142. continue;
  143. }
  144. IDirectoryInfo dir = _fileSystem.DirectoryInfo.New (path);
  145. if (!roots.ContainsKey (dir) && dir.Exists)
  146. {
  147. roots.Add (dir, special.ToString ());
  148. }
  149. }
  150. catch (Exception)
  151. {
  152. // Special file exists but contents are unreadable (permissions?)
  153. // skip it anyway
  154. }
  155. }
  156. }
  157. catch (Exception)
  158. {
  159. // Cannot get the special files for this OS oh well
  160. }
  161. return roots;
  162. }
  163. }