FileDialogExamples.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using Terminal.Gui;
  8. using static Terminal.Gui.OpenDialog;
  9. namespace UICatalog.Scenarios {
  10. [ScenarioMetadata (Name: "FileDialog", Description: "Demonstrates how to the FileDialog class")]
  11. [ScenarioCategory ("Dialogs")]
  12. [ScenarioCategory ("Files and IO")]
  13. public class FileDialogExamples : Scenario {
  14. private CheckBox cbMustExist;
  15. private CheckBox cbUseColors;
  16. private CheckBox cbCaseSensitive;
  17. private CheckBox cbAllowMultipleSelection;
  18. private CheckBox cbShowTreeBranchLines;
  19. private CheckBox cbAlwaysTableShowHeaders;
  20. private CheckBox cbDrivesOnlyInTree;
  21. private RadioGroup rgCaption;
  22. private RadioGroup rgOpenMode;
  23. private RadioGroup rgIcons;
  24. private RadioGroup rgAllowedTypes;
  25. private TextField tbOkButton;
  26. private TextField tbCancelButton;
  27. private CheckBox cbFlipButtonOrder;
  28. public override void Setup ()
  29. {
  30. var y = 0;
  31. var x = 1;
  32. cbMustExist = new CheckBox ("Must Exist") { Checked = true, Y = y++, X = x };
  33. Win.Add (cbMustExist);
  34. cbUseColors = new CheckBox ("Use Colors") { Checked = FileDialogStyle.DefaultUseColors, Y = y++, X = x };
  35. Win.Add (cbUseColors);
  36. cbCaseSensitive = new CheckBox ("Case Sensitive Search") { Checked = false, Y = y++, X = x };
  37. Win.Add (cbCaseSensitive);
  38. cbAllowMultipleSelection = new CheckBox ("Multiple") { Checked = false, Y = y++, X = x };
  39. Win.Add (cbAllowMultipleSelection);
  40. cbShowTreeBranchLines = new CheckBox ("Tree Branch Lines") { Checked = true, Y = y++, X = x };
  41. Win.Add (cbShowTreeBranchLines);
  42. cbAlwaysTableShowHeaders = new CheckBox ("Always Show Headers") { Checked = true, Y = y++, X = x };
  43. Win.Add (cbAlwaysTableShowHeaders);
  44. cbDrivesOnlyInTree = new CheckBox ("Only Show Drives") { Checked = false, Y = y++, X = x };
  45. Win.Add (cbDrivesOnlyInTree);
  46. y = 0;
  47. x = 24;
  48. Win.Add (new LineView (Orientation.Vertical) {
  49. X = x++,
  50. Y = 1,
  51. Height = 4
  52. });
  53. Win.Add (new Label ("Caption") { X = x++, Y = y++ });
  54. rgCaption = new RadioGroup { X = x, Y = y };
  55. rgCaption.RadioLabels = new string [] { "Ok", "Open", "Save" };
  56. Win.Add (rgCaption);
  57. y = 0;
  58. x = 34;
  59. Win.Add (new LineView (Orientation.Vertical) {
  60. X = x++,
  61. Y = 1,
  62. Height = 4
  63. });
  64. Win.Add (new Label ("OpenMode") { X = x++, Y = y++ });
  65. rgOpenMode = new RadioGroup { X = x, Y = y };
  66. rgOpenMode.RadioLabels = new string [] { "File", "Directory", "Mixed" };
  67. Win.Add (rgOpenMode);
  68. y = 0;
  69. x = 48;
  70. Win.Add (new LineView (Orientation.Vertical) {
  71. X = x++,
  72. Y = 1,
  73. Height = 4
  74. });
  75. Win.Add (new Label ("Icons") { X = x++, Y = y++ });
  76. rgIcons = new RadioGroup { X = x, Y = y };
  77. rgIcons.RadioLabels = new string [] { "None", "Unicode", "Nerd*" };
  78. Win.Add (rgIcons);
  79. Win.Add (new Label ("* Requires installing Nerd fonts") { Y = Pos.AnchorEnd (2) });
  80. Win.Add (new Label (" (see: https://github.com/devblackops/Terminal-Icons)") { Y = Pos.AnchorEnd (1) });
  81. y = 5;
  82. x = 24;
  83. Win.Add (new LineView (Orientation.Vertical) {
  84. X = x++,
  85. Y = y + 1,
  86. Height = 4
  87. });
  88. Win.Add (new Label ("Allowed") { X = x++, Y = y++ });
  89. rgAllowedTypes = new RadioGroup { X = x, Y = y };
  90. rgAllowedTypes.RadioLabels = new string [] { "Any", "Csv (Recommended)", "Csv (Strict)" };
  91. Win.Add (rgAllowedTypes);
  92. y = 5;
  93. x = 45;
  94. Win.Add (new LineView (Orientation.Vertical) {
  95. X = x++,
  96. Y = y + 1,
  97. Height = 4
  98. });
  99. Win.Add (new Label ("Buttons") { X = x++, Y = y++ });
  100. Win.Add (new Label ("Ok Text:") { X = x, Y = y++ });
  101. tbOkButton = new TextField () { X = x, Y = y++, Width = 12 };
  102. Win.Add (tbOkButton);
  103. Win.Add (new Label ("Cancel Text:") { X = x, Y = y++ });
  104. tbCancelButton = new TextField () { X = x, Y = y++, Width = 12 };
  105. Win.Add (tbCancelButton);
  106. cbFlipButtonOrder = new CheckBox ("Flip Order") { X = x, Y = y++ };
  107. Win.Add (cbFlipButtonOrder);
  108. var btn = new Button ($"Run Dialog") {
  109. X = 1,
  110. Y = 9
  111. };
  112. SetupHandler (btn);
  113. Win.Add (btn);
  114. }
  115. private void SetupHandler (Button btn)
  116. {
  117. btn.Clicked += (s, e) => {
  118. try {
  119. CreateDialog ();
  120. } catch (Exception ex) {
  121. MessageBox.ErrorQuery ("Error", ex.ToString (), "Ok");
  122. }
  123. };
  124. }
  125. private void CreateDialog ()
  126. {
  127. var fd = new FileDialog () {
  128. OpenMode = Enum.Parse<OpenMode> (
  129. rgOpenMode.RadioLabels [rgOpenMode.SelectedItem].ToString ()),
  130. MustExist = cbMustExist.Checked ?? false,
  131. AllowsMultipleSelection = cbAllowMultipleSelection.Checked ?? false,
  132. };
  133. fd.Style.OkButtonText = rgCaption.RadioLabels [rgCaption.SelectedItem].ToString ();
  134. // If Save style dialog then give them an overwrite prompt
  135. if (rgCaption.SelectedItem == 2) {
  136. fd.FilesSelected += ConfirmOverwrite;
  137. }
  138. fd.Style.IconProvider.UseUnicodeCharacters = rgIcons.SelectedItem == 1;
  139. fd.Style.IconProvider.UseNerdIcons = rgIcons.SelectedItem == 2;
  140. if (cbCaseSensitive.Checked ?? false) {
  141. fd.SearchMatcher = new CaseSensitiveSearchMatcher ();
  142. }
  143. fd.Style.UseColors = cbUseColors.Checked ?? false;
  144. fd.Style.TreeStyle.ShowBranchLines = cbShowTreeBranchLines.Checked ?? false;
  145. fd.Style.TableStyle.AlwaysShowHeaders = cbAlwaysTableShowHeaders.Checked ?? false;
  146. var dirInfoFactory = new FileSystem ().DirectoryInfo;
  147. if (cbDrivesOnlyInTree.Checked ?? false) {
  148. fd.Style.TreeRootGetter = () => {
  149. return System.Environment.GetLogicalDrives ().ToDictionary (dirInfoFactory.New, k => k);
  150. };
  151. }
  152. if (rgAllowedTypes.SelectedItem > 0) {
  153. fd.AllowedTypes.Add (new AllowedType ("Data File", ".csv", ".tsv"));
  154. if (rgAllowedTypes.SelectedItem == 1) {
  155. fd.AllowedTypes.Insert (1, new AllowedTypeAny ());
  156. }
  157. }
  158. if (!string.IsNullOrWhiteSpace (tbOkButton.Text)) {
  159. fd.Style.OkButtonText = tbOkButton.Text;
  160. }
  161. if (!string.IsNullOrWhiteSpace (tbCancelButton.Text)) {
  162. fd.Style.CancelButtonText = tbCancelButton.Text;
  163. }
  164. if (cbFlipButtonOrder.Checked ?? false) {
  165. fd.Style.FlipOkCancelButtonLayoutOrder = true;
  166. }
  167. Application.Run (fd);
  168. if (fd.Canceled) {
  169. MessageBox.Query (
  170. "Canceled",
  171. "You canceled navigation and did not pick anything",
  172. "Ok");
  173. } else if (cbAllowMultipleSelection.Checked ?? false) {
  174. MessageBox.Query (
  175. "Chosen!",
  176. "You chose:" + Environment.NewLine +
  177. string.Join (Environment.NewLine, fd.MultiSelected.Select (m => m)),
  178. "Ok");
  179. } else {
  180. MessageBox.Query (
  181. "Chosen!",
  182. "You chose:" + Environment.NewLine + fd.Path,
  183. "Ok");
  184. }
  185. }
  186. private void ConfirmOverwrite (object sender, FilesSelectedEventArgs e)
  187. {
  188. if (!string.IsNullOrWhiteSpace (e.Dialog.Path)) {
  189. if (File.Exists (e.Dialog.Path)) {
  190. int result = MessageBox.Query ("Overwrite?", "File already exists", "Yes", "No");
  191. e.Cancel = result == 1;
  192. }
  193. }
  194. }
  195. private class CaseSensitiveSearchMatcher : ISearchMatcher {
  196. private string terms;
  197. public void Initialize (string terms)
  198. {
  199. this.terms = terms;
  200. }
  201. public bool IsMatch (IFileSystemInfo f)
  202. {
  203. return f.Name.Contains (terms, StringComparison.CurrentCulture);
  204. }
  205. }
  206. }
  207. }