FileDialogStyle.cs 10 KB

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