FileDialogStyle.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 culture to use (e.g. for number formatting).
  42. /// Defaults to <see cref="CultureInfo.CurrentUICulture"/>.
  43. /// </summary>
  44. public CultureInfo Culture {get;set;} = CultureInfo.CurrentUICulture;
  45. /// <summary>
  46. /// Sets a <see cref="ColorScheme"/> to use for directories rows of
  47. /// the <see cref="TableView"/>.
  48. /// </summary>
  49. public ColorScheme ColorSchemeDirectory { get; set; }
  50. /// <summary>
  51. /// Sets a <see cref="ColorScheme"/> to use for file rows with an image extension
  52. /// of the <see cref="TableView"/>. Defaults to White text on Black background.
  53. /// </summary>
  54. public ColorScheme ColorSchemeImage { get; set; }
  55. /// <summary>
  56. /// Sets a <see cref="ColorScheme"/> to use for file rows with an executable extension
  57. /// or that match <see cref="FileDialog.AllowedTypes"/> in the <see cref="TableView"/>.
  58. /// </summary>
  59. public ColorScheme ColorSchemeExeOrRecommended { get; set; }
  60. /// <summary>
  61. /// Colors to use when <see cref="UseColors"/> is true but file does not match any other
  62. /// classification (<see cref="ColorSchemeDirectory"/>, <see cref="ColorSchemeImage"/> etc).
  63. /// </summary>
  64. public ColorScheme ColorSchemeOther { get; set; }
  65. /// <summary>
  66. /// Gets or sets the header text displayed in the Filename column of the files table.
  67. /// </summary>
  68. public string FilenameColumnName { get; set; } = Strings.fdFilename;
  69. /// <summary>
  70. /// Gets or sets the header text displayed in the Size column of the files table.
  71. /// </summary>
  72. public string SizeColumnName { get; set; } = Strings.fdSize;
  73. /// <summary>
  74. /// Gets or sets the header text displayed in the Modified column of the files table.
  75. /// </summary>
  76. public string ModifiedColumnName { get; set; } = Strings.fdModified;
  77. /// <summary>
  78. /// Gets or sets the header text displayed in the Type column of the files table.
  79. /// </summary>
  80. public string TypeColumnName { get; set; } = Strings.fdType;
  81. /// <summary>
  82. /// Gets or sets the text displayed in the 'Search' text box when user has not supplied any input yet.
  83. /// </summary>
  84. public string SearchCaption { get; internal set; } = Strings.fdSearchCaption;
  85. /// <summary>
  86. /// Gets or sets the text displayed in the 'Path' text box when user has not supplied any input yet.
  87. /// </summary>
  88. public string PathCaption { get; internal set; } = Strings.fdPathCaption;
  89. /// <summary>
  90. /// Gets or sets the text on the 'Ok' button. Typically you may want to change this to
  91. /// "Open" or "Save" etc.
  92. /// </summary>
  93. public string OkButtonText { get; set; } = "Ok";
  94. /// <summary>
  95. /// Gets or sets error message when user attempts to select a file type that is not one of <see cref="FileDialog.AllowedTypes"/>
  96. /// </summary>
  97. public string WrongFileTypeFeedback { get; internal set; } = Strings.fdWrongFileTypeFeedback;
  98. /// <summary>
  99. /// Gets or sets error message when user selects a directory that does not exist and
  100. /// <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  101. /// </summary>
  102. public string DirectoryMustExistFeedback { get; internal set; } = Strings.fdDirectoryMustExistFeedback;
  103. /// <summary>
  104. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/>
  105. /// and user enters the name of an existing file (File system cannot have a folder with the same name as a file).
  106. /// </summary>
  107. public string FileAlreadyExistsFeedback { get; internal set; } = Strings.fdFileAlreadyExistsFeedback;
  108. /// <summary>
  109. /// Gets or sets error message when user selects a file that does not exist and
  110. /// <see cref="OpenMode"/> is <see cref="OpenMode.File"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  111. /// </summary>
  112. public string FileMustExistFeedback { get; internal set; } = Strings.fdFileMustExistFeedback;
  113. /// <summary>
  114. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.File"/>
  115. /// and user enters the name of an existing directory (File system cannot have a folder with the same name as a file).
  116. /// </summary>
  117. public string DirectoryAlreadyExistsFeedback { get; internal set; } = Strings.fdDirectoryAlreadyExistsFeedback;
  118. /// <summary>
  119. /// Gets or sets error message when user selects a file/dir that does not exist and
  120. /// <see cref="OpenMode"/> is <see cref="OpenMode.Mixed"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  121. /// </summary>
  122. public string FileOrDirectoryMustExistFeedback { get; internal set; } = Strings.fdFileOrDirectoryMustExistFeedback;
  123. /// <summary>
  124. /// Gets the style settings for the table of files (in currently selected directory).
  125. /// </summary>
  126. public TableStyle TableStyle { get; internal set; }
  127. /// <summary>
  128. /// Gets the style settings for the collapse-able directory/places tree
  129. /// </summary>
  130. public TreeStyle TreeStyle { get; internal set; }
  131. /// <summary>
  132. /// Gets or Sets the method for getting the root tree objects that are displayed in
  133. /// the collapse-able tree in the <see cref="FileDialog"/>. Defaults to all accessible
  134. /// <see cref="System.Environment.GetLogicalDrives"/> and unique
  135. /// <see cref="Environment.SpecialFolder"/>.
  136. /// </summary>
  137. /// <remarks>Must be configured before showing the dialog.</remarks>
  138. public Func<Dictionary<IDirectoryInfo, string>> TreeRootGetter { get; set; }
  139. /// <summary>
  140. /// Gets or sets whether to use advanced unicode characters which might not be installed
  141. /// on all users computers.
  142. /// </summary>
  143. public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
  144. /// <summary>
  145. /// Gets or sets the format to use for date/times in the Modified column.
  146. /// Defaults to <see cref="DateTimeFormatInfo.SortableDateTimePattern"/>
  147. /// of the <see cref="CultureInfo.CurrentCulture"/>
  148. /// </summary>
  149. public string DateFormat { get; set; }
  150. /// <summary>
  151. /// Creates a new instance of the <see cref="FileDialogStyle"/> class.
  152. /// </summary>
  153. public FileDialogStyle (IFileSystem fileSystem)
  154. {
  155. _fileSystem = fileSystem;
  156. TreeRootGetter = DefaultTreeRootGetter;
  157. DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
  158. ColorSchemeDirectory = new ColorScheme {
  159. Normal = Application.Driver.MakeAttribute (Color.Blue, Color.Black),
  160. HotNormal = Application.Driver.MakeAttribute (Color.Blue, Color.Black),
  161. Focus = Application.Driver.MakeAttribute (Color.Black, Color.Blue),
  162. HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Blue),
  163. };
  164. ColorSchemeImage = new ColorScheme {
  165. Normal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
  166. HotNormal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
  167. Focus = Application.Driver.MakeAttribute (Color.Black, Color.Magenta),
  168. HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Magenta),
  169. };
  170. ColorSchemeExeOrRecommended = new ColorScheme {
  171. Normal = Application.Driver.MakeAttribute (Color.Green, Color.Black),
  172. HotNormal = Application.Driver.MakeAttribute (Color.Green, Color.Black),
  173. Focus = Application.Driver.MakeAttribute (Color.Black, Color.Green),
  174. HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Green),
  175. };
  176. ColorSchemeOther = new ColorScheme {
  177. Normal = Application.Driver.MakeAttribute (Color.White, Color.Black),
  178. HotNormal = Application.Driver.MakeAttribute (Color.White, Color.Black),
  179. Focus = Application.Driver.MakeAttribute (Color.Black, Color.White),
  180. HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.White),
  181. };
  182. }
  183. private Dictionary<IDirectoryInfo,string> DefaultTreeRootGetter ()
  184. {
  185. var roots = new Dictionary<IDirectoryInfo, string> ();
  186. try {
  187. foreach (var d in Environment.GetLogicalDrives ()) {
  188. var dir = _fileSystem.DirectoryInfo.New (d);
  189. if (!roots.ContainsKey(dir)) {
  190. roots.Add (dir, d);
  191. }
  192. }
  193. } catch (Exception) {
  194. // Cannot get the system disks thats fine
  195. }
  196. try {
  197. foreach (var special in Enum.GetValues (typeof (Environment.SpecialFolder)).Cast<SpecialFolder> ()) {
  198. try {
  199. var path = Environment.GetFolderPath (special);
  200. if(string.IsNullOrWhiteSpace (path)) {
  201. continue;
  202. }
  203. var dir = _fileSystem.DirectoryInfo.New (path);
  204. if (!roots.ContainsKey (dir) && dir.Exists) {
  205. roots.Add (dir, special.ToString());
  206. }
  207. } catch (Exception) {
  208. // Special file exists but contents are unreadable (permissions?)
  209. // skip it anyway
  210. }
  211. }
  212. } catch (Exception) {
  213. // Cannot get the special files for this OS oh well
  214. }
  215. return roots;
  216. }
  217. }
  218. }