FileDialogStyle.cs 8.5 KB

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