Dialogs.cs 11 KB

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