WindowsAndFrameViews.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Windows & FrameViews", "Stress Tests Windows, sub-Windows, and FrameViews.")]
  5. [ScenarioCategory ("Layout")]
  6. public class WindowsAndFrameViews : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. Window app = new ()
  12. {
  13. Title = GetQuitKeyAndName ()
  14. };
  15. static int? About ()
  16. {
  17. return MessageBox.Query (ApplicationImpl.Instance,
  18. "About UI Catalog",
  19. "UI Catalog is a comprehensive sample library for Terminal.Gui",
  20. "Ok"
  21. );
  22. }
  23. var margin = 2;
  24. var padding = 1;
  25. var contentHeight = 7;
  26. // list of Windows we create
  27. List<View> listWin = new ();
  28. var win = new Window
  29. {
  30. Title = $"{listWin.Count} - Scenario: {GetName ()}",
  31. X = Pos.Center (),
  32. Y = 1,
  33. Width = Dim.Fill (15),
  34. Height = 10,
  35. SchemeName = "Dialog",
  36. Arrangement = ViewArrangement.Overlapped | ViewArrangement.Movable | ViewArrangement.Resizable
  37. };
  38. win.Padding.Thickness = new (padding);
  39. win.Margin!.Thickness = new (margin);
  40. var paddingButton = new Button
  41. {
  42. X = Pos.Center (),
  43. Y = 0,
  44. SchemeName = "Error",
  45. Text = $"Padding of container is {padding}"
  46. };
  47. paddingButton.Accepting += (s, e) => About ();
  48. win.Add (paddingButton);
  49. win.Add (
  50. new Button
  51. {
  52. X = Pos.Center (),
  53. Y = Pos.AnchorEnd (),
  54. SchemeName = "Error",
  55. Text = "Press ME! (Y = Pos.AnchorEnd(1))"
  56. }
  57. );
  58. app.Add (win);
  59. // add it to our list
  60. listWin.Add (win);
  61. // create 3 more Windows in a loop, adding them Application.TopRunnable
  62. // Each with a
  63. // button
  64. // sub Window with
  65. // TextField
  66. // sub FrameView with
  67. //
  68. for (var pad = 0; pad < 3; pad++)
  69. {
  70. Window loopWin = null;
  71. loopWin = new ()
  72. {
  73. Title = $"{listWin.Count} - Window Loop - padding = {pad}",
  74. X = margin,
  75. Y = Pos.Bottom (listWin.Last ()) + margin,
  76. Width = Dim.Fill (margin),
  77. Height = contentHeight + pad * 2 + 2,
  78. Arrangement = ViewArrangement.Overlapped | ViewArrangement.Movable | ViewArrangement.Resizable
  79. };
  80. loopWin.Padding.Thickness = new (pad);
  81. loopWin.SchemeName = "Dialog";
  82. var pressMeButton = new Button
  83. {
  84. X = Pos.Center (), Y = 0, SchemeName = "Error", Text = "Press me! (Y = 0)",
  85. };
  86. pressMeButton.Accepting += (s, e) =>
  87. MessageBox.ErrorQuery ((s as View)?.App, loopWin.Title, "Neat?", "Yes", "No");
  88. loopWin.Add (pressMeButton);
  89. var subWin = new Window
  90. {
  91. Title = "Sub Window",
  92. X = Pos.Percent (0),
  93. Y = 1,
  94. Width = Dim.Percent (50),
  95. Height = 5,
  96. SchemeName = "Base",
  97. Text = "The Text in the Window",
  98. Arrangement = ViewArrangement.Overlapped | ViewArrangement.Movable | ViewArrangement.Resizable
  99. };
  100. subWin.Add (
  101. new TextField { Y = 1, SchemeName = "Error", Text = "Edit me! " + loopWin.Title }
  102. );
  103. loopWin.Add (subWin);
  104. var frameView = new FrameView
  105. {
  106. X = Pos.Percent (50),
  107. Y = 1,
  108. Width = Dim.Percent (100, DimPercentMode.Position), // Or Dim.Percent (50)
  109. Height = 5,
  110. SchemeName = "Base",
  111. Text = "The Text in the FrameView",
  112. Title = "This is a Sub-FrameView"
  113. };
  114. frameView.Add (
  115. new TextField { Y = 1, Text = "Edit Me!" }
  116. );
  117. loopWin.Add (frameView);
  118. app.Add (loopWin);
  119. listWin.Add (loopWin);
  120. }
  121. FrameView frame = null;
  122. frame = new ()
  123. {
  124. X = margin,
  125. Y = Pos.Bottom (listWin.Last ()) + margin / 2,
  126. Width = Dim.Fill (margin),
  127. Height = contentHeight + 2, // 2 for default padding
  128. Title = "This is a FrameView"
  129. };
  130. frame.SchemeName = "Dialog";
  131. frame.Add (
  132. new Label
  133. {
  134. X = Pos.Center (), Y = 0, SchemeName = "Error", Text = "This is a Label! (Y = 0)"
  135. }
  136. );
  137. var subWinofFV = new Window
  138. {
  139. Title = "This is a Sub-Window",
  140. X = Pos.Percent (0),
  141. Y = 1,
  142. Width = Dim.Percent (50),
  143. Height = Dim.Fill () - 1,
  144. SchemeName = "Base",
  145. Text = "The Text in the Window",
  146. Arrangement = ViewArrangement.Overlapped | ViewArrangement.Movable | ViewArrangement.Resizable
  147. };
  148. subWinofFV.Add (
  149. new TextField { SchemeName = "Error", Text = "Edit Me" }
  150. );
  151. subWinofFV.Add (new CheckBox { Y = 1, Text = "Check me" });
  152. subWinofFV.Add (new CheckBox { Y = 2, Text = "Or, Check me" });
  153. frame.Add (subWinofFV);
  154. var subFrameViewofFV = new FrameView
  155. {
  156. X = Pos.Percent (50),
  157. Y = 1,
  158. Width = Dim.Percent (100),
  159. Height = Dim.Fill () - 1,
  160. SchemeName = "Base",
  161. Text = "The Text in the FrameView",
  162. Title = "this is a Sub-FrameView"
  163. };
  164. subFrameViewofFV.Add (new TextField { Width = 15, Text = "Edit Me" });
  165. subFrameViewofFV.Add (new CheckBox { Y = 1, Text = "Check me" });
  166. subFrameViewofFV.Add (new CheckBox { Y = 2, Text = "Or, Check me" });
  167. frame.Add (
  168. new CheckBox { X = 0, Y = Pos.AnchorEnd (), Text = "Btn1 (Y = Pos.AnchorEnd ())" }
  169. );
  170. var c = new CheckBox { X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Btn2 (Y = Pos.AnchorEnd ())" };
  171. frame.Add (c);
  172. frame.Add (subFrameViewofFV);
  173. app.Add (frame);
  174. listWin.Add (frame);
  175. app.SchemeName = "Base";
  176. Application.Run (app);
  177. app.Dispose ();
  178. Application.Shutdown ();
  179. }
  180. }