FileDialogExamples.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 cbUnicode;
  16. private CheckBox cbUseColors;
  17. private CheckBox cbCaseSensitive;
  18. private CheckBox cbAllowMultipleSelection;
  19. private CheckBox cbShowTreeBranchLines;
  20. private CheckBox cbAlwaysTableShowHeaders;
  21. private CheckBox cbDrivesOnlyInTree;
  22. private RadioGroup rgCaption;
  23. private RadioGroup rgOpenMode;
  24. private RadioGroup rgAllowedTypes;
  25. public override void Setup ()
  26. {
  27. var y = 0;
  28. var x = 1;
  29. cbMustExist = new CheckBox ("Must Exist") { Checked = true, Y = y++, X = x };
  30. Win.Add (cbMustExist);
  31. cbUnicode = new CheckBox ("UseUnicode") { Checked = FileDialogStyle.DefaultUseUnicodeCharacters, Y = y++, X = x };
  32. Win.Add (cbUnicode);
  33. cbUseColors = new CheckBox ("Use Colors") { Checked = FileDialogStyle.DefaultUseColors, Y = y++, X = x };
  34. Win.Add (cbUseColors);
  35. cbCaseSensitive = new CheckBox ("Case Sensitive Search") { Checked = false, Y = y++, X = x };
  36. Win.Add (cbCaseSensitive);
  37. cbAllowMultipleSelection = new CheckBox ("Multiple") { Checked = false, Y = y++, X = x };
  38. Win.Add (cbAllowMultipleSelection);
  39. cbShowTreeBranchLines = new CheckBox ("Tree Branch Lines") { Checked = true, Y = y++, X = x };
  40. Win.Add (cbShowTreeBranchLines);
  41. cbAlwaysTableShowHeaders = new CheckBox ("Always Show Headers") { Checked = true, Y = y++, X = x };
  42. Win.Add (cbAlwaysTableShowHeaders);
  43. cbDrivesOnlyInTree = new CheckBox ("Only Show Drives") { Checked = false, Y = y++, X = x };
  44. Win.Add (cbDrivesOnlyInTree);
  45. y = 0;
  46. x = 24;
  47. Win.Add (new LineView (Orientation.Vertical) {
  48. X = x++,
  49. Y = 1,
  50. Height = 4
  51. });
  52. Win.Add (new Label ("Caption") { X = x++, Y = y++ });
  53. rgCaption = new RadioGroup { X = x, Y = y };
  54. rgCaption.RadioLabels = new NStack.ustring [] { "Ok", "Open", "Save" };
  55. Win.Add (rgCaption);
  56. y = 0;
  57. x = 37;
  58. Win.Add (new LineView (Orientation.Vertical) {
  59. X = x++,
  60. Y = 1,
  61. Height = 4
  62. });
  63. Win.Add (new Label ("OpenMode") { X = x++, Y = y++ });
  64. rgOpenMode = new RadioGroup { X = x, Y = y };
  65. rgOpenMode.RadioLabels = new NStack.ustring [] { "File", "Directory", "Mixed" };
  66. Win.Add (rgOpenMode);
  67. y = 5;
  68. x = 24;
  69. Win.Add (new LineView (Orientation.Vertical) {
  70. X = x++,
  71. Y = y + 1,
  72. Height = 4
  73. });
  74. Win.Add (new Label ("Allowed") { X = x++, Y = y++ });
  75. rgAllowedTypes = new RadioGroup { X = x, Y = y };
  76. rgAllowedTypes.RadioLabels = new NStack.ustring [] { "Any", "Csv (Recommended)", "Csv (Strict)" };
  77. Win.Add (rgAllowedTypes);
  78. var btn = new Button ($"Run Dialog") {
  79. X = 1,
  80. Y = 9
  81. };
  82. SetupHandler (btn);
  83. Win.Add (btn);
  84. }
  85. private void SetupHandler (Button btn)
  86. {
  87. btn.Clicked += (s,e) => {
  88. try
  89. {
  90. CreateDialog();
  91. }
  92. catch(Exception ex)
  93. {
  94. MessageBox.ErrorQuery("Error",ex.ToString(),"Ok");
  95. }
  96. };
  97. }
  98. private void CreateDialog ()
  99. {
  100. var fd = new FileDialog () {
  101. OpenMode = Enum.Parse<OpenMode> (
  102. rgOpenMode.RadioLabels [rgOpenMode.SelectedItem].ToString ()),
  103. MustExist = cbMustExist.Checked ?? false,
  104. AllowsMultipleSelection = cbAllowMultipleSelection.Checked ?? false,
  105. };
  106. fd.Style.OkButtonText = rgCaption.RadioLabels [rgCaption.SelectedItem].ToString ();
  107. // If Save style dialog then give them an overwrite prompt
  108. if(rgCaption.SelectedItem == 2) {
  109. fd.FilesSelected += ConfirmOverwrite;
  110. }
  111. fd.Style.UseUnicodeCharacters = cbUnicode.Checked ?? false;
  112. if (cbCaseSensitive.Checked ?? false) {
  113. fd.SearchMatcher = new CaseSensitiveSearchMatcher ();
  114. }
  115. fd.Style.UseColors = cbUseColors.Checked ?? false;
  116. fd.Style.TreeStyle.ShowBranchLines = cbShowTreeBranchLines.Checked ?? false;
  117. fd.Style.TableStyle.AlwaysShowHeaders = cbAlwaysTableShowHeaders.Checked ?? false;
  118. if (cbDrivesOnlyInTree.Checked ?? false) {
  119. fd.Style.TreeRootGetter = () => {
  120. return System.Environment.GetLogicalDrives ()
  121. .Select (d => new FileDialogRootTreeNode (d, new DirectoryInfo (d)));
  122. };
  123. }
  124. if (rgAllowedTypes.SelectedItem > 0) {
  125. fd.AllowedTypes.Add (new AllowedType ("Data File", ".csv", ".tsv"));
  126. if (rgAllowedTypes.SelectedItem == 1) {
  127. fd.AllowedTypes.Insert (1, new AllowedTypeAny ());
  128. }
  129. }
  130. Application.Run (fd);
  131. if (fd.Canceled) {
  132. MessageBox.Query (
  133. "Canceled",
  134. "You canceled navigation and did not pick anything",
  135. "Ok");
  136. } else if (cbAllowMultipleSelection.Checked ?? false) {
  137. MessageBox.Query (
  138. "Chosen!",
  139. "You chose:" + Environment.NewLine +
  140. string.Join (Environment.NewLine, fd.MultiSelected.Select (m => m)),
  141. "Ok");
  142. } else {
  143. MessageBox.Query (
  144. "Chosen!",
  145. "You chose:" + Environment.NewLine + fd.Path,
  146. "Ok");
  147. }
  148. }
  149. private void ConfirmOverwrite (object sender, FilesSelectedEventArgs e)
  150. {
  151. if (!string.IsNullOrWhiteSpace (e.Dialog.Path)) {
  152. if(File.Exists(e.Dialog.Path)) {
  153. int result = MessageBox.Query ("Overwrite?", "File already exists", "Yes", "No");
  154. e.Cancel = result == 1;
  155. }
  156. }
  157. }
  158. private class CaseSensitiveSearchMatcher : ISearchMatcher {
  159. private string terms;
  160. public void Initialize (string terms)
  161. {
  162. this.terms = terms;
  163. }
  164. public bool IsMatch (IFileSystemInfo f)
  165. {
  166. return f.Name.Contains (terms, StringComparison.CurrentCulture);
  167. }
  168. }
  169. }
  170. }