Dialogs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 = GetQuitKeyAndName ()
  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
  72. {
  73. X = Pos.Right (widthEdit) + 2,
  74. Y = Pos.Top (widthEdit),
  75. Text = $"If width is 0, the dimension will be {Dialog.DefaultMinimumWidth}%."
  76. }
  77. );
  78. frame.Add (
  79. new Label
  80. {
  81. X = Pos.Right (heightEdit) + 2,
  82. Y = Pos.Top (heightEdit),
  83. Text = $"If height is 0, the dimension will be {Dialog.DefaultMinimumWidth}%."
  84. }
  85. );
  86. label = new ()
  87. {
  88. X = 0,
  89. Y = Pos.Bottom (label),
  90. Width = Dim.Width (numButtonsLabel),
  91. Height = 1,
  92. TextAlignment = Alignment.End,
  93. Text = "_Title:"
  94. };
  95. frame.Add (label);
  96. var titleEdit = new TextField
  97. {
  98. X = Pos.Right (label) + 1,
  99. Y = Pos.Top (label),
  100. Width = Dim.Fill (),
  101. Height = 1,
  102. Text = "Title"
  103. };
  104. frame.Add (titleEdit);
  105. numButtonsLabel.Y = Pos.Bottom (label);
  106. frame.Add (numButtonsLabel);
  107. var numButtonsEdit = new TextField
  108. {
  109. X = Pos.Right (numButtonsLabel) + 1,
  110. Y = Pos.Top (numButtonsLabel),
  111. Width = 5,
  112. Height = 1,
  113. Text = "3"
  114. };
  115. frame.Add (numButtonsEdit);
  116. var glyphsNotWords = new CheckBox
  117. {
  118. X = Pos.Right (numButtonsLabel) + 1,
  119. Y = Pos.Bottom (numButtonsLabel),
  120. TextAlignment = Alignment.End,
  121. Text = $"_Add {char.ConvertFromUtf32 (CODE_POINT)} to button text to stress wide char support",
  122. State = CheckState.UnChecked
  123. };
  124. frame.Add (glyphsNotWords);
  125. label = new ()
  126. {
  127. X = 0,
  128. Y = Pos.Bottom (glyphsNotWords),
  129. Width = Dim.Width (numButtonsLabel),
  130. Height = 1,
  131. TextAlignment = Alignment.End,
  132. Text = "Button A_lignment:"
  133. };
  134. frame.Add (label);
  135. var labels = Enum.GetNames<Alignment> ();
  136. var alignmentGroup = new RadioGroup
  137. {
  138. X = Pos.Right (label) + 1,
  139. Y = Pos.Top (label),
  140. RadioLabels = labels.ToArray (),
  141. };
  142. frame.Add (alignmentGroup);
  143. alignmentGroup.SelectedItem = labels.ToList ().IndexOf (Dialog.DefaultButtonAlignment.ToString ());
  144. frame.ValidatePosDim = true;
  145. app.Add (frame);
  146. label = new ()
  147. {
  148. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = Alignment.End, Text = "Button Pressed:"
  149. };
  150. app.Add (label);
  151. var buttonPressedLabel = new Label
  152. {
  153. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, ColorScheme = Colors.ColorSchemes ["Error"], Text = " "
  154. };
  155. var showDialogButton = new Button
  156. {
  157. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  158. };
  159. showDialogButton.Accept += (s, e) =>
  160. {
  161. Dialog dlg = CreateDemoDialog (
  162. widthEdit,
  163. heightEdit,
  164. titleEdit,
  165. numButtonsEdit,
  166. glyphsNotWords,
  167. alignmentGroup,
  168. buttonPressedLabel
  169. );
  170. Application.Run (dlg);
  171. dlg.Dispose ();
  172. };
  173. app.Add (showDialogButton);
  174. app.Add (buttonPressedLabel);
  175. Application.Run (app);
  176. app.Dispose ();
  177. Application.Shutdown ();
  178. }
  179. private Dialog CreateDemoDialog (
  180. TextField widthEdit,
  181. TextField heightEdit,
  182. TextField titleEdit,
  183. TextField numButtonsEdit,
  184. CheckBox glyphsNotWords,
  185. RadioGroup alignmentRadioGroup,
  186. Label buttonPressedLabel
  187. )
  188. {
  189. Dialog dialog = null;
  190. try
  191. {
  192. var width = 0;
  193. int.TryParse (widthEdit.Text, out width);
  194. var height = 0;
  195. int.TryParse (heightEdit.Text, out height);
  196. var numButtons = 3;
  197. int.TryParse (numButtonsEdit.Text, out numButtons);
  198. List<Button> buttons = new ();
  199. int clicked = -1;
  200. for (var i = 0; i < numButtons; i++)
  201. {
  202. int buttonId = i;
  203. Button button = null;
  204. if (glyphsNotWords.State == CheckState.Checked)
  205. {
  206. buttonId = i;
  207. button = new ()
  208. {
  209. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  210. IsDefault = buttonId == 0
  211. };
  212. }
  213. else
  214. {
  215. button = new () { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  216. }
  217. button.Accept += (s, e) =>
  218. {
  219. clicked = buttonId;
  220. Application.RequestStop ();
  221. };
  222. buttons.Add (button);
  223. }
  224. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  225. // the buttons are laid out correctly
  226. dialog = new ()
  227. {
  228. Title = titleEdit.Text,
  229. ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentRadioGroup.RadioLabels [alignmentRadioGroup.SelectedItem]),
  230. Buttons = buttons.ToArray ()
  231. };
  232. if (width != 0)
  233. {
  234. dialog.Width = width;
  235. }
  236. if (height != 0)
  237. {
  238. dialog.Height = height;
  239. }
  240. var add = new Button
  241. {
  242. X = Pos.Center (),
  243. Y = Pos.Center () - 1,
  244. Text = "_Add a button"
  245. };
  246. add.Accept += (s, e) =>
  247. {
  248. int buttonId = buttons.Count;
  249. Button button;
  250. if (glyphsNotWords.State == CheckState.Checked)
  251. {
  252. button = new ()
  253. {
  254. Text = NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  255. IsDefault = buttonId == 0
  256. };
  257. }
  258. else
  259. {
  260. button = new () { Text = NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  261. }
  262. button.Accept += (s, e) =>
  263. {
  264. clicked = buttonId;
  265. Application.RequestStop ();
  266. };
  267. buttons.Add (button);
  268. dialog.AddButton (button);
  269. if (buttons.Count > 1)
  270. {
  271. button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  272. }
  273. };
  274. dialog.Add (add);
  275. var addChar = new Button
  276. {
  277. X = Pos.Center (),
  278. Y = Pos.Center () + 1,
  279. Text = $"A_dd a {char.ConvertFromUtf32 (CODE_POINT)} to each button. This text is really long for a reason."
  280. };
  281. addChar.Accept += (s, e) =>
  282. {
  283. foreach (Button button in buttons)
  284. {
  285. button.Text += char.ConvertFromUtf32 (CODE_POINT);
  286. }
  287. dialog.LayoutSubviews ();
  288. };
  289. dialog.Add (addChar);
  290. dialog.Closed += (s, e) => { buttonPressedLabel.Text = $"{clicked}"; };
  291. }
  292. catch (FormatException)
  293. {
  294. buttonPressedLabel.Text = "Invalid Options";
  295. }
  296. return dialog;
  297. }
  298. }