FileDialogStyle.cs 9.8 KB

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