FileDialogExamples.cs 9.2 KB

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