WindowsAndFrameViews.cs 7.0 KB

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