Dialogs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. // Add hotkeys
  137. var labels = Enum.GetNames<Alignment> ().Select (n => n = "_" + n);
  138. var alignmentGroup = new RadioGroup
  139. {
  140. X = Pos.Right (label) + 1,
  141. Y = Pos.Top (label),
  142. RadioLabels = labels.ToArray (),
  143. Title = "Ali_gn",
  144. BorderStyle = LineStyle.Dashed
  145. };
  146. frame.Add (alignmentGroup);
  147. alignmentGroup.SelectedItem = labels.ToList ().IndexOf ("_" + Dialog.DefaultButtonAlignment.ToString ());
  148. frame.ValidatePosDim = true;
  149. app.Add (frame);
  150. label = new ()
  151. {
  152. X = Pos.Center (), Y = Pos.Bottom (frame) + 4, TextAlignment = Alignment.End, Text = "Button Pressed:"
  153. };
  154. app.Add (label);
  155. var buttonPressedLabel = new Label
  156. {
  157. X = Pos.Center (), Y = Pos.Bottom (frame) + 5, ColorScheme = Colors.ColorSchemes ["Error"], Text = " "
  158. };
  159. var showDialogButton = new Button
  160. {
  161. X = Pos.Center (), Y = Pos.Bottom (frame) + 2, IsDefault = true, Text = "_Show Dialog"
  162. };
  163. app.Accepting += (s, e) =>
  164. {
  165. Dialog dlg = CreateDemoDialog (
  166. widthEdit,
  167. heightEdit,
  168. titleEdit,
  169. numButtonsEdit,
  170. glyphsNotWords,
  171. alignmentGroup,
  172. buttonPressedLabel
  173. );
  174. Application.Run (dlg);
  175. dlg.Dispose ();
  176. e.Cancel = true;
  177. };
  178. app.Add (showDialogButton);
  179. app.Add (buttonPressedLabel);
  180. Application.Run (app);
  181. app.Dispose ();
  182. Application.Shutdown ();
  183. }
  184. private Dialog CreateDemoDialog (
  185. TextField widthEdit,
  186. TextField heightEdit,
  187. TextField titleEdit,
  188. TextField numButtonsEdit,
  189. CheckBox glyphsNotWords,
  190. RadioGroup alignmentRadioGroup,
  191. Label buttonPressedLabel
  192. )
  193. {
  194. Dialog dialog = null;
  195. try
  196. {
  197. var width = 0;
  198. int.TryParse (widthEdit.Text, out width);
  199. var height = 0;
  200. int.TryParse (heightEdit.Text, out height);
  201. var numButtons = 3;
  202. int.TryParse (numButtonsEdit.Text, out numButtons);
  203. List<Button> buttons = new ();
  204. int clicked = -1;
  205. for (var i = 0; i < numButtons; i++)
  206. {
  207. int buttonId = i;
  208. Button button = null;
  209. if (glyphsNotWords.CheckedState == CheckState.Checked)
  210. {
  211. buttonId = i;
  212. button = new ()
  213. {
  214. Text = "_" + NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  215. IsDefault = buttonId == 0
  216. };
  217. }
  218. else
  219. {
  220. button = new () { Text = "_" + NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  221. }
  222. button.Accepting += (s, e) =>
  223. {
  224. clicked = buttonId;
  225. e.Cancel = true;
  226. Application.RequestStop ();
  227. };
  228. buttons.Add (button);
  229. }
  230. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  231. // the buttons are laid out correctly
  232. dialog = new ()
  233. {
  234. Title = titleEdit.Text,
  235. Text = "Dialog Text",
  236. ButtonAlignment = (Alignment)Enum.Parse (typeof (Alignment), alignmentRadioGroup.RadioLabels [alignmentRadioGroup.SelectedItem].Substring (1)),
  237. Buttons = buttons.ToArray ()
  238. };
  239. if (width != 0)
  240. {
  241. dialog.Width = width;
  242. }
  243. if (height != 0)
  244. {
  245. dialog.Height = height;
  246. }
  247. var add = new Button
  248. {
  249. X = Pos.Center (),
  250. Y = Pos.Center () - 1,
  251. Text = "_Add a button"
  252. };
  253. add.Accepting += (s, e) =>
  254. {
  255. int buttonId = buttons.Count;
  256. Button button;
  257. if (glyphsNotWords.CheckedState == CheckState.Checked)
  258. {
  259. button = new ()
  260. {
  261. Text = "_" + NumberToWords.Convert (buttonId) + " " + char.ConvertFromUtf32 (buttonId + CODE_POINT),
  262. IsDefault = buttonId == 0
  263. };
  264. }
  265. else
  266. {
  267. button = new () { Text = "_" + NumberToWords.Convert (buttonId), IsDefault = buttonId == 0 };
  268. }
  269. button.Accepting += (s, e) =>
  270. {
  271. clicked = buttonId;
  272. Application.RequestStop ();
  273. e.Cancel = true;
  274. };
  275. buttons.Add (button);
  276. dialog.AddButton (button);
  277. //if (buttons.Count > 1)
  278. //{
  279. // button.TabIndex = buttons [buttons.Count - 2].TabIndex + 1;
  280. //}
  281. e.Cancel = true;
  282. };
  283. dialog.Add (add);
  284. var addChar = new Button
  285. {
  286. X = Pos.Center (),
  287. Y = Pos.Center () + 1,
  288. Text = $"A_dd a {char.ConvertFromUtf32 (CODE_POINT)} to each button. This text is really long for a reason."
  289. };
  290. addChar.Accepting += (s, e) =>
  291. {
  292. foreach (Button button in buttons)
  293. {
  294. button.Text += char.ConvertFromUtf32 (CODE_POINT);
  295. }
  296. dialog.LayoutSubviews ();
  297. e.Cancel = true;
  298. };
  299. dialog.Add (addChar);
  300. dialog.Closed += (s, e) => { buttonPressedLabel.Text = $"{clicked}"; };
  301. }
  302. catch (FormatException)
  303. {
  304. buttonPressedLabel.Text = "Invalid Options";
  305. }
  306. return dialog;
  307. }
  308. }