FileDialogExamples.cs 9.6 KB

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