FileDialogStyle.cs 10 KB

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