FileDialogExamples.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.IO;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("FileDialog", "Demonstrates how to the FileDialog class")]
  9. [ScenarioCategory ("Dialogs")]
  10. [ScenarioCategory ("Files and IO")]
  11. public class FileDialogExamples : Scenario
  12. {
  13. private CheckBox _cbAllowMultipleSelection;
  14. private CheckBox _cbAlwaysTableShowHeaders;
  15. private CheckBox _cbCaseSensitive;
  16. private CheckBox _cbDrivesOnlyInTree;
  17. private CheckBox _cbPreserveFilenameOnDirectoryChanges;
  18. private CheckBox _cbFlipButtonOrder;
  19. private CheckBox _cbMustExist;
  20. private CheckBox _cbShowTreeBranchLines;
  21. private CheckBox _cbUseColors;
  22. private RadioGroup _rgAllowedTypes;
  23. private RadioGroup _rgCaption;
  24. private RadioGroup _rgIcons;
  25. private RadioGroup _rgOpenMode;
  26. private TextField _tbCancelButton;
  27. private TextField _tbOkButton;
  28. public override void Main ()
  29. {
  30. Application.Init ();
  31. var y = 0;
  32. var x = 1;
  33. var win = new Window { Title = GetQuitKeyAndName () };
  34. _cbMustExist = new CheckBox { CheckedState = CheckState.Checked, Y = y++, X = x, Text = "Must E_xist" };
  35. win.Add (_cbMustExist);
  36. _cbUseColors = new CheckBox { CheckedState = FileDialogStyle.DefaultUseColors ? CheckState.Checked : CheckState.UnChecked, Y = y++, X = x, Text = "_Use Colors" };
  37. win.Add (_cbUseColors);
  38. _cbCaseSensitive = new CheckBox { CheckedState = CheckState.UnChecked, Y = y++, X = x, Text = "_Case Sensitive Search" };
  39. win.Add (_cbCaseSensitive);
  40. _cbAllowMultipleSelection = new CheckBox { CheckedState = CheckState.UnChecked, Y = y++, X = x, Text = "_Multiple" };
  41. win.Add (_cbAllowMultipleSelection);
  42. _cbShowTreeBranchLines = new CheckBox { CheckedState = CheckState.Checked, Y = y++, X = x, Text = "Tree Branch _Lines" };
  43. win.Add (_cbShowTreeBranchLines);
  44. _cbAlwaysTableShowHeaders = new CheckBox { CheckedState = CheckState.Checked, Y = y++, X = x, Text = "Always Show _Headers" };
  45. win.Add (_cbAlwaysTableShowHeaders);
  46. _cbDrivesOnlyInTree = new CheckBox { CheckedState = CheckState.UnChecked, Y = y++, X = x, Text = "Only Show _Drives" };
  47. win.Add (_cbDrivesOnlyInTree);
  48. _cbPreserveFilenameOnDirectoryChanges = new CheckBox { CheckedState = CheckState.UnChecked, Y = y++, X = x, Text = "Preserve Filename" };
  49. win.Add (_cbPreserveFilenameOnDirectoryChanges);
  50. y = 0;
  51. x = 24;
  52. win.Add (
  53. new LineView (Orientation.Vertical) { X = x++, Y = 1, Height = 4 }
  54. );
  55. win.Add (new Label { X = x++, Y = y++, Text = "Caption" });
  56. _rgCaption = new RadioGroup { X = x, Y = y };
  57. _rgCaption.RadioLabels = new [] { "_Ok", "O_pen", "_Save" };
  58. win.Add (_rgCaption);
  59. y = 0;
  60. x = 34;
  61. win.Add (
  62. new LineView (Orientation.Vertical) { X = x++, Y = 1, Height = 4 }
  63. );
  64. win.Add (new Label { X = x++, Y = y++, Text = "OpenMode" });
  65. _rgOpenMode = new RadioGroup { X = x, Y = y };
  66. _rgOpenMode.RadioLabels = new [] { "_File", "D_irectory", "_Mixed" };
  67. win.Add (_rgOpenMode);
  68. y = 0;
  69. x = 48;
  70. win.Add (
  71. new LineView (Orientation.Vertical) { X = x++, Y = 1, Height = 4 }
  72. );
  73. win.Add (new Label { X = x++, Y = y++, Text = "Icons" });
  74. _rgIcons = new RadioGroup { X = x, Y = y };
  75. _rgIcons.RadioLabels = new [] { "_None", "_Unicode", "Nerd_*" };
  76. win.Add (_rgIcons);
  77. win.Add (new Label { Y = Pos.AnchorEnd (2), Text = "* Requires installing Nerd fonts" });
  78. win.Add (new Label { Y = Pos.AnchorEnd (1), Text = " (see: https://github.com/devblackops/Terminal-Icons)" });
  79. y = 5;
  80. x = 24;
  81. win.Add (
  82. new LineView (Orientation.Vertical) { X = x++, Y = y + 1, Height = 4 }
  83. );
  84. win.Add (new Label { X = x++, Y = y++, Text = "Allowed" });
  85. _rgAllowedTypes = new RadioGroup { X = x, Y = y };
  86. _rgAllowedTypes.RadioLabels = new [] { "An_y", "Cs_v (Recommended)", "Csv (S_trict)" };
  87. win.Add (_rgAllowedTypes);
  88. y = 5;
  89. x = 45;
  90. win.Add (
  91. new LineView (Orientation.Vertical) { X = x++, Y = y + 1, Height = 4 }
  92. );
  93. win.Add (new Label { X = x++, Y = y++, Text = "Buttons" });
  94. win.Add (new Label { X = x, Y = y++, Text = "O_k Text:" });
  95. _tbOkButton = new TextField { X = x, Y = y++, Width = 12 };
  96. win.Add (_tbOkButton);
  97. win.Add (new Label { X = x, Y = y++, Text = "_Cancel Text:" });
  98. _tbCancelButton = new TextField { X = x, Y = y++, Width = 12 };
  99. win.Add (_tbCancelButton);
  100. _cbFlipButtonOrder = new CheckBox { X = x, Y = y++, Text = "Flip Ord_er" };
  101. win.Add (_cbFlipButtonOrder);
  102. var btn = new Button { X = 1, Y = 9, IsDefault = true, Text = "Run Dialog" };
  103. win.Accepting += (s, e) =>
  104. {
  105. try
  106. {
  107. CreateDialog ();
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageBox.ErrorQuery ("Error", ex.ToString (), "_Ok");
  112. }
  113. finally
  114. {
  115. e.Cancel = true;
  116. }
  117. };
  118. win.Add (btn);
  119. Application.Run (win);
  120. win.Dispose ();
  121. Application.Shutdown ();
  122. }
  123. private void ConfirmOverwrite (object sender, FilesSelectedEventArgs e)
  124. {
  125. if (!string.IsNullOrWhiteSpace (e.Dialog.Path))
  126. {
  127. if (File.Exists (e.Dialog.Path))
  128. {
  129. int result = MessageBox.Query ("Overwrite?", "File already exists", "_Yes", "_No");
  130. e.Cancel = result == 1;
  131. }
  132. }
  133. }
  134. private void CreateDialog ()
  135. {
  136. var fd = new FileDialog
  137. {
  138. OpenMode = Enum.Parse<OpenMode> (
  139. _rgOpenMode.RadioLabels
  140. .Select (l => TextFormatter.FindHotKey (l, _rgOpenMode.HotKeySpecifier, out int hotPos, out Key _)
  141. // Remove the hotkey specifier at the found position
  142. ? TextFormatter.RemoveHotKeySpecifier (l, hotPos, _rgOpenMode.HotKeySpecifier)
  143. // No hotkey found, return the label as is
  144. : l)
  145. .ToArray () [_rgOpenMode.SelectedItem]
  146. ),
  147. MustExist = _cbMustExist.CheckedState == CheckState.Checked,
  148. AllowsMultipleSelection = _cbAllowMultipleSelection.CheckedState == CheckState.Checked
  149. };
  150. fd.Style.OkButtonText = _rgCaption.RadioLabels.Select (l => TextFormatter.RemoveHotKeySpecifier(l, 0, _rgCaption.HotKeySpecifier)).ToArray() [_rgCaption.SelectedItem];
  151. // If Save style dialog then give them an overwrite prompt
  152. if (_rgCaption.SelectedItem == 2)
  153. {
  154. fd.FilesSelected += ConfirmOverwrite;
  155. }
  156. fd.Style.IconProvider.UseUnicodeCharacters = _rgIcons.SelectedItem == 1;
  157. fd.Style.IconProvider.UseNerdIcons = _rgIcons.SelectedItem == 2;
  158. if (_cbCaseSensitive.CheckedState == CheckState.Checked)
  159. {
  160. fd.SearchMatcher = new CaseSensitiveSearchMatcher ();
  161. }
  162. fd.Style.UseColors = _cbUseColors.CheckedState == CheckState.Checked;
  163. fd.Style.TreeStyle.ShowBranchLines = _cbShowTreeBranchLines.CheckedState == CheckState.Checked;
  164. fd.Style.TableStyle.AlwaysShowHeaders = _cbAlwaysTableShowHeaders.CheckedState == CheckState.Checked;
  165. IDirectoryInfoFactory dirInfoFactory = new FileSystem ().DirectoryInfo;
  166. if (_cbDrivesOnlyInTree.CheckedState == CheckState.Checked)
  167. {
  168. fd.Style.TreeRootGetter = () => { return Environment.GetLogicalDrives ().ToDictionary (dirInfoFactory.New, k => k); };
  169. }
  170. fd.Style.PreserveFilenameOnDirectoryChanges = _cbPreserveFilenameOnDirectoryChanges.CheckedState == CheckState.Checked;
  171. if (_rgAllowedTypes.SelectedItem > 0)
  172. {
  173. fd.AllowedTypes.Add (new AllowedType ("Data File", ".csv", ".tsv"));
  174. if (_rgAllowedTypes.SelectedItem == 1)
  175. {
  176. fd.AllowedTypes.Insert (1, new AllowedTypeAny ());
  177. }
  178. }
  179. if (!string.IsNullOrWhiteSpace (_tbOkButton.Text))
  180. {
  181. fd.Style.OkButtonText = _tbOkButton.Text;
  182. }
  183. if (!string.IsNullOrWhiteSpace (_tbCancelButton.Text))
  184. {
  185. fd.Style.CancelButtonText = _tbCancelButton.Text;
  186. }
  187. if (_cbFlipButtonOrder.CheckedState == CheckState.Checked)
  188. {
  189. fd.Style.FlipOkCancelButtonLayoutOrder = true;
  190. }
  191. Application.Run (fd);
  192. var canceled = fd.Canceled;
  193. var multiSelected = fd.MultiSelected;
  194. var path = fd.Path;
  195. // This needs to be disposed before opening other toplevel
  196. fd.Dispose ();
  197. if (canceled)
  198. {
  199. MessageBox.Query (
  200. "Canceled",
  201. "You canceled navigation and did not pick anything",
  202. "Ok"
  203. );
  204. }
  205. else if (_cbAllowMultipleSelection.CheckedState == CheckState.Checked)
  206. {
  207. MessageBox.Query (
  208. "Chosen!",
  209. "You chose:" + Environment.NewLine + string.Join (Environment.NewLine, multiSelected.Select (m => m)),
  210. "Ok"
  211. );
  212. }
  213. else
  214. {
  215. MessageBox.Query (
  216. "Chosen!",
  217. "You chose:" + Environment.NewLine + path,
  218. "Ok"
  219. );
  220. }
  221. }
  222. private class CaseSensitiveSearchMatcher : ISearchMatcher
  223. {
  224. private string _terms;
  225. public void Initialize (string terms) { _terms = terms; }
  226. public bool IsMatch (IFileSystemInfo f) { return f.Name.Contains (_terms, StringComparison.CurrentCulture); }
  227. }
  228. }