FileDialogExamples.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Select (l => TextFormatter.RemoveHotKeySpecifier(l, 0, _rgOpenMode.HotKeySpecifier)).ToArray() [_rgOpenMode.SelectedItem]
  140. ),
  141. MustExist = _cbMustExist.CheckedState == CheckState.Checked,
  142. AllowsMultipleSelection = _cbAllowMultipleSelection.CheckedState == CheckState.Checked
  143. };
  144. fd.Style.OkButtonText = _rgCaption.RadioLabels.Select (l => TextFormatter.RemoveHotKeySpecifier(l, 0, _rgCaption.HotKeySpecifier)).ToArray() [_rgCaption.SelectedItem];
  145. // If Save style dialog then give them an overwrite prompt
  146. if (_rgCaption.SelectedItem == 2)
  147. {
  148. fd.FilesSelected += ConfirmOverwrite;
  149. }
  150. fd.Style.IconProvider.UseUnicodeCharacters = _rgIcons.SelectedItem == 1;
  151. fd.Style.IconProvider.UseNerdIcons = _rgIcons.SelectedItem == 2;
  152. if (_cbCaseSensitive.CheckedState == CheckState.Checked)
  153. {
  154. fd.SearchMatcher = new CaseSensitiveSearchMatcher ();
  155. }
  156. fd.Style.UseColors = _cbUseColors.CheckedState == CheckState.Checked;
  157. fd.Style.TreeStyle.ShowBranchLines = _cbShowTreeBranchLines.CheckedState == CheckState.Checked;
  158. fd.Style.TableStyle.AlwaysShowHeaders = _cbAlwaysTableShowHeaders.CheckedState == CheckState.Checked;
  159. IDirectoryInfoFactory dirInfoFactory = new FileSystem ().DirectoryInfo;
  160. if (_cbDrivesOnlyInTree.CheckedState == CheckState.Checked)
  161. {
  162. fd.Style.TreeRootGetter = () => { return Environment.GetLogicalDrives ().ToDictionary (dirInfoFactory.New, k => k); };
  163. }
  164. fd.Style.PreserveFilenameOnDirectoryChanges = _cbPreserveFilenameOnDirectoryChanges.CheckedState == CheckState.Checked;
  165. if (_rgAllowedTypes.SelectedItem > 0)
  166. {
  167. fd.AllowedTypes.Add (new AllowedType ("Data File", ".csv", ".tsv"));
  168. if (_rgAllowedTypes.SelectedItem == 1)
  169. {
  170. fd.AllowedTypes.Insert (1, new AllowedTypeAny ());
  171. }
  172. }
  173. if (!string.IsNullOrWhiteSpace (_tbOkButton.Text))
  174. {
  175. fd.Style.OkButtonText = _tbOkButton.Text;
  176. }
  177. if (!string.IsNullOrWhiteSpace (_tbCancelButton.Text))
  178. {
  179. fd.Style.CancelButtonText = _tbCancelButton.Text;
  180. }
  181. if (_cbFlipButtonOrder.CheckedState == CheckState.Checked)
  182. {
  183. fd.Style.FlipOkCancelButtonLayoutOrder = true;
  184. }
  185. Application.Run (fd);
  186. var canceled = fd.Canceled;
  187. var multiSelected = fd.MultiSelected;
  188. var path = fd.Path;
  189. // This needs to be disposed before opening other toplevel
  190. fd.Dispose ();
  191. if (canceled)
  192. {
  193. MessageBox.Query (
  194. "Canceled",
  195. "You canceled navigation and did not pick anything",
  196. "Ok"
  197. );
  198. }
  199. else if (_cbAllowMultipleSelection.CheckedState == CheckState.Checked)
  200. {
  201. MessageBox.Query (
  202. "Chosen!",
  203. "You chose:" + Environment.NewLine + string.Join (Environment.NewLine, multiSelected.Select (m => m)),
  204. "Ok"
  205. );
  206. }
  207. else
  208. {
  209. MessageBox.Query (
  210. "Chosen!",
  211. "You chose:" + Environment.NewLine + path,
  212. "Ok"
  213. );
  214. }
  215. }
  216. private class CaseSensitiveSearchMatcher : ISearchMatcher
  217. {
  218. private string _terms;
  219. public void Initialize (string terms) { _terms = terms; }
  220. public bool IsMatch (IFileSystemInfo f) { return f.Name.Contains (_terms, StringComparison.CurrentCulture); }
  221. }
  222. }