2
0

Dialogs.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog {
  8. [ScenarioMetadata (Name: "Dialogs", Description: "Demonstrates how to the Dialog class")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("Dialogs")]
  11. class Dialogs : Scenario {
  12. public override void Setup ()
  13. {
  14. var frame = new FrameView ("Dialog Options") {
  15. X = Pos.Center(),
  16. Y = 1,
  17. Width = Dim.Percent(75),
  18. Height = 10
  19. };
  20. Win.Add (frame);
  21. var label = new Label ("width:") {
  22. X = 0,
  23. Y = 0,
  24. Width = 15,
  25. Height = 1,
  26. TextAlignment = Terminal.Gui.TextAlignment.Right,
  27. };
  28. frame.Add (label);
  29. var widthEdit = new TextField ("0") {
  30. X = Pos.Right (label) + 1,
  31. Y = Pos.Top (label),
  32. Width = 5,
  33. Height = 1
  34. };
  35. frame.Add (widthEdit);
  36. label = new Label ("height:") {
  37. X = 0,
  38. Y = Pos.Bottom (label),
  39. Width = Dim.Width(label),
  40. Height = 1,
  41. TextAlignment = Terminal.Gui.TextAlignment.Right,
  42. };
  43. frame.Add (label);
  44. var heightEdit = new TextField ("0") {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Width = 5,
  48. Height = 1
  49. };
  50. frame.Add (heightEdit);
  51. frame.Add (new Label ("If height & width are both 0,") {
  52. X = Pos.Right (widthEdit) + 2,
  53. Y = Pos.Top (widthEdit),
  54. });
  55. frame.Add (new Label ("the Dialog will size to 80% of container.") {
  56. X = Pos.Right (heightEdit) + 2,
  57. Y = Pos.Top (heightEdit),
  58. });
  59. label = new Label ("Title:") {
  60. X = 0,
  61. Y = Pos.Bottom (label),
  62. Width = Dim.Width (label),
  63. Height = 1,
  64. TextAlignment = Terminal.Gui.TextAlignment.Right,
  65. };
  66. frame.Add (label);
  67. var titleEdit = new TextField ("Title") {
  68. X = Pos.Right (label) + 1,
  69. Y = Pos.Top (label),
  70. Width = Dim.Fill(),
  71. Height = 1
  72. };
  73. frame.Add (titleEdit);
  74. label = new Label ("Num Buttons:") {
  75. X = 0,
  76. Y = Pos.Bottom (titleEdit),
  77. Width = Dim.Width (label),
  78. Height = 1,
  79. TextAlignment = Terminal.Gui.TextAlignment.Right,
  80. };
  81. frame.Add (label);
  82. var numButtonsEdit = new TextField ("3") {
  83. X = Pos.Right (label) + 1,
  84. Y = Pos.Top (label),
  85. Width = 5,
  86. Height = 1
  87. };
  88. frame.Add (numButtonsEdit);
  89. frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit)
  90. + Dim.Height(numButtonsEdit) + 2;
  91. label = new Label ("Button Pressed:") {
  92. X = Pos.Center (),
  93. Y = Pos.Bottom (frame) + 2,
  94. Height = 1,
  95. TextAlignment = Terminal.Gui.TextAlignment.Right,
  96. };
  97. Win.Add (label);
  98. var buttonPressedLabel = new Label ("") {
  99. X = Pos.Center (),
  100. Y = Pos.Bottom (frame) + 4,
  101. Width = 25,
  102. Height = 1,
  103. ColorScheme = Colors.Error,
  104. };
  105. var btnText = new [] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
  106. var showDialogButton = new Button ("Show Dialog") {
  107. X = Pos.Center(),
  108. Y = Pos.Bottom (frame) + 2 ,
  109. IsDefault = true,
  110. };
  111. showDialogButton.Clicked += () => {
  112. try {
  113. int width = int.Parse (widthEdit.Text.ToString ());
  114. int height = int.Parse (heightEdit.Text.ToString ());
  115. int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
  116. var buttons = new List<Button> ();
  117. var clicked = -1;
  118. for (int i = 0; i < numButtons; i++) {
  119. var buttonId = i;
  120. var button = new Button (btnText [buttonId % 10],
  121. is_default: buttonId == 0);
  122. button.Clicked += () => {
  123. clicked = buttonId;
  124. Application.RequestStop ();
  125. };
  126. buttons.Add (button);
  127. }
  128. // This tests dynamically adding buttons; ensuring the dialog resizes if needed and
  129. // the buttons are laid out correctly
  130. var dialog = new Dialog (titleEdit.Text, width, height,
  131. buttons.ToArray ());
  132. var add = new Button ("Add a button") {
  133. X = Pos.Center (),
  134. Y = Pos.Center (),
  135. };
  136. add.Clicked += () => {
  137. var buttonId = buttons.Count;
  138. var button = new Button (btnText [buttonId % 10],
  139. is_default: buttonId == 0);
  140. button.Clicked += () => {
  141. clicked = buttonId;
  142. Application.RequestStop ();
  143. };
  144. buttons.Add (button);
  145. dialog.AddButton (button);
  146. };
  147. dialog.Add (add);
  148. Application.Run (dialog);
  149. buttonPressedLabel.Text = $"{clicked}";
  150. } catch (FormatException) {
  151. buttonPressedLabel.Text = "Invalid Options";
  152. }
  153. };
  154. Win.Add (showDialogButton);
  155. Win.Add (buttonPressedLabel);
  156. }
  157. }
  158. }