2
0

FileDialogExamples.cs 11 KB

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