FileDialogStyle.cs 9.1 KB

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