Dialogs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("Dialogs", "Demonstrates how to the Dialog class")]
  7. [ScenarioCategory ("Dialogs")]
  8. public class Dialogs : Scenario
  9. {
  10. private static readonly int CODE_POINT = '你'; // We know this is a wide char
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. Window app = new ()
  15. {
  16. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
  17. };
  18. var frame = new FrameView
  19. {
  20. X = Pos.Center (),
  21. Y = 1,
  22. Width = Dim.Percent (75),
  23. Height = Dim.Auto (DimAutoStyle.Content),
  24. Title = "Dialog Options"
  25. };
  26. var numButtonsLabel = new Label
  27. {
  28. X = 0,
  29. TextAlignment = Alignment.End,
  30. Text = "_Number of Buttons:"
  31. };
  32. var label = new Label
  33. {
  34. X = 0,
  35. Y = 0,
  36. Width = Dim.Width (numButtonsLabel),
  37. Height = 1,
  38. TextAlignment = Alignment.End,
  39. Text = "_Width:"
  40. };
  41. frame.Add (label);
  42. var widthEdit = new TextField
  43. {
  44. X = Pos.Right (numButtonsLabel) + 1,
  45. Y = Pos.Top (label),
  46. Width = 5,
  47. Height = 1,
  48. Text = "0"
  49. };
  50. frame.Add (widthEdit);
  51. label = new ()
  52. {
  53. X = 0,
  54. Y = Pos.Bottom (label),
  55. Width = Dim.Width (numButtonsLabel),
  56. Height = 1,
  57. TextAlignment = Alignment.End,
  58. Text = "_Height:"
  59. };
  60. frame.Add (label);
  61. var heightEdit = new TextField
  62. {
  63. X = Pos.Right (numButtonsLabel) + 1,
  64. Y = Pos.Top (label),
  65. Width = 5,
  66. Height = 1,
  67. Text = "0"
  68. };
  69. frame.Add (heightEdit);
  70. frame.Add (
  71. new Label { X = Pos.Right (widthEdit) + 2, Y = Pos.Top (widthEdit), Text = "If height & width are both 0," }
  72. );
  73. frame.Add (
  74. new Label
  75. {
  76. X = Pos.Right (heightEdit) + 2,
  77. Y = Pos.Top (heightEdit),
  78. Text = "the Dialog will size to 80% of container."
  79. }
  80. );
  81. label = new ()
  82. {
  83. X = 0,
  84. Y = Pos.Bottom (label),
  85. Width = Dim.Width (numButtonsLabel),
  86. Height = 1,
  87. TextAlignment = Alignment.End,
  88. Text = "_Title:"
  89. };
  90. frame.Add (label);
  91. var titleEdit = new TextField
  92. {
  93. X = Pos.Right (label) + 1,
  94. Y = Pos.Top (label),
  95. Width = Dim.Fill (),
  96. Height = 1,
  97. Text = "Title"
  98. };
  99. frame.Add (titleEdit);
  100. numButtonsLabel.Y = Pos.Bottom (label);
  101. frame.Add (numButtonsLabel);
  102. var numButtonsEdit = new TextField
  103. {
  104. X = Pos.Right (numButtonsLabel) + 1,
  105. Y = Pos.Top (numButtonsLabel),
  106. Width = 5,
  107. Height = 1,
  108. Text = "3"
  109. };
  110. frame.Add (numButtonsEdit);
  111. var glyphsNotWords = new CheckBox
  112. {
  113. X = Pos.Right (numButtonsLabel) + 1,
  114. Y = Pos.Bottom (numButtonsLabel),
  115. TextAlignment = Alignment.End,
  116. Text = $"_Add {char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support",
  117. State = CheckState.UnChecked
  118. };
  119. frame.Add (glyphsNotWords);
  120. label = new ()
  121. {
  122. X = 0,
  123. Y = Pos.Bottom (glyphsNotWords),
  124. Width = Dim.Width (numButtonsLabel),
  125. Height = 1,
  126. TextAlignment = Alignment.End,
  127. Text = "Button A_lignment:"
  128. };
  129. frame.Add (label);
  130. var labels = Enum.GetNames<Alignment> ();
  131. var alignmentGroup = new RadioGroup
  132. {
  133. X = Pos.Right (label) + 1,
  134. Y = Pos.Top (label),
  135. RadioLabels = labels.ToArray (),
  136. };
  137. frame.Add (alignmentGroup);
  138. alignmentGroup.SelectedItem = labels.ToList ().IndexOf (Dialog.DefaultButtonAlignment.ToString ());
  139. frame.ValidatePosDim = true;
  140. app.Add (frame);
  141. label = new ()
  142. {
  143. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = Alignment.End, Text = "Button Pressed:"
  144. };
  145. app.Add (label);
  146. var buttonPressedLabel = new Label
  147. {
  148. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, ColorScheme = Colors.ColorSchemes ["Error"], Text = " "
  149. };
  150. var showDialogButton = new Button
  151. {
  152. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  153. };
  154. showDialogButton.Accept += (s, e) =>
  155. {
  156. Dialog dlg = CreateDemoDialog (
  157. widthEdit,
  158. heightEdit,
  159. titleEdit,
  160. numButtonsEdit,
  161. glyphsNotWords,
  162. alignmentGroup,
  163. buttonPressedLabel
  164. );
  165. Application.Run (dlg);
  166. dlg.Dispose ();
  167. };
  168. app.Add (showDialogButton);
  169. app.Add (buttonPressedLabel);
  170. Application.Run (app);
  171. app.Dispose ();
  172. Application.Shutdown ();
  173. }
  174. private Dialog CreateDemoDialog (
  175. TextField widthEdit,
  176. TextField heightEdit,
  177. TextField titleEdit,
  178. TextField numButtonsEdit,
  179. CheckBox glyphsNotWords,
  180. RadioGroup alignmentRadioGroup,
  181. Label buttonPressedLabel
  182. )
  183. {
  184. Dialog dialog = null;
  185. try
  186. {
  187. var width = 0;
  188. int.TryParse (widthEdit.Text, out width);
  189. var height = 0;
  190. int.TryParse (heightEdit.Text, out height);
  191. var numButtons = 3;
  192. int.TryParse (numButtonsEdit.Text, out numButtons);
  193. List<Button> buttons = new ();
  194. int clicked = -1;
  195. for (var i = 0; i < numButtons; i++)
  196. {
  197. int buttonId = i;
  198. Button button = null;
  199. if (glyphsNotWords.State == CheckState.Checked)
  200. {
  201. buttonId = i;
  202. button = new ()
  203. {
  204. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  205. IsDefault = buttonId == 0
  206. };
  207. }
  208. else
  209. {
  210. button = new () { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  211. }
  212. button.Accept += (s, e) =>
  213. {
  214. clicked = buttonId;
  215. Application.RequestStop ();
  216. };
  217. buttons.Add (button);
  218. }
  219. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  220. // the buttons are laid out correctly
  221. dialog = new ()
  222. {
  223. Title = titleEdit.Text,
  224. ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentRadioGroup.RadioLabels [alignmentRadioGroup.SelectedItem]),
  225. Buttons = buttons.ToArray ()
  226. };
  227. if (height != 0 || width != 0)
  228. {
  229. dialog.Height = height;
  230. dialog.Width = width;
  231. }
  232. var add = new Button
  233. {
  234. X = Pos.Center (),
  235. Y = Pos.Center (),
  236. Text = "_Add a button"
  237. };
  238. add.Accept += (s, e) =>
  239. {
  240. int buttonId = buttons.Count;
  241. Button button;
  242. if (glyphsNotWords.State == CheckState.Checked)
  243. {
  244. button = new ()
  245. {
  246. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  247. IsDefault = buttonId == 0
  248. };
  249. }
  250. else
  251. {
  252. button = new () { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  253. }
  254. button.Accept += (s, e) =>
  255. {
  256. clicked = buttonId;
  257. Application.RequestStop ();
  258. };
  259. buttons.Add (button);
  260. dialog.AddButton (button);
  261. if (buttons.Count > 1)
  262. {
  263. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  264. }
  265. };
  266. dialog.Add (add);
  267. var addChar = new Button
  268. {
  269. X = Pos.Center (),
  270. Y = Pos.Center () + 1,
  271. Text = $"A_dd a {char.ConvertFromUtf32 (CODE_POINT)} to each button. This text is really long for a reason."
  272. };
  273. addChar.Accept += (s, e) =>
  274. {
  275. foreach (Button button in buttons)
  276. {
  277. button.Text += char.ConvertFromUtf32 (CODE_POINT);
  278. }
  279. dialog.LayoutSubviews ();
  280. };
  281. dialog.Closed += (s, e) => { buttonPressedLabel.Text = $"{clicked}"; };
  282. dialog.Add (addChar);
  283. }
  284. catch (FormatException)
  285. {
  286. buttonPressedLabel.Text = "Invalid Options";
  287. }
  288. return dialog;
  289. }
  290. }