FileDialogStyle.cs 11 KB

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