ComputedLayout.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Terminal.Gui;
  8. namespace UICatalog.Scenarios {
  9. /// <summary>
  10. /// This Scenario demonstrates how to use Termina.gui's Dim and Pos Layout System.
  11. /// [x] - Using Dim.Fill to fill a window
  12. /// [x] - Using Dim.Fill and Dim.Pos to automatically align controls based on an initial control
  13. /// [ ] - ...
  14. /// </summary>
  15. [ScenarioMetadata (Name: "Computed Layout", Description: "Demonstrates the Computed (Dim and Pos) Layout System.")]
  16. [ScenarioCategory ("Layout")]
  17. public class ComputedLayout : Scenario {
  18. public override void Init ()
  19. {
  20. Application.Init ();
  21. ConfigurationManager.Themes.Theme = Theme;
  22. ConfigurationManager.Apply ();
  23. Application.Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
  24. }
  25. public override void Setup ()
  26. {
  27. // Demonstrate using Dim to create a horizontal ruler that always measures the parent window's width
  28. const string rule = "|123456789";
  29. var horizontalRuler = new Label (rule, false) {
  30. AutoSize = false,
  31. X = 0,
  32. Y = 0,
  33. Width = Dim.Fill (),
  34. Height = 1,
  35. ColorScheme = Colors.Error
  36. };
  37. Application.Top.Add (horizontalRuler);
  38. // Demonstrate using Dim to create a vertical ruler that always measures the parent window's height
  39. const string vrule = "|\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
  40. var verticalRuler = new Label (vrule, false) {
  41. AutoSize = false,
  42. X = 0,
  43. Y = 0,
  44. Width = 1,
  45. Height = Dim.Fill (),
  46. ColorScheme = Colors.Error
  47. };
  48. Application.Top.LayoutComplete += (s, a) => {
  49. horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
  50. verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height * 2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height * 2)];
  51. };
  52. Application.Top.Add (verticalRuler);
  53. // Demonstrate At - Using Pos.At to locate a view in an absolute location
  54. var atButton = new Button ("At(2,1)") {
  55. X = Pos.At (2),
  56. Y = Pos.At (1)
  57. };
  58. Application.Top.Add (atButton);
  59. // Throw in a literal absolute - Should function identically to above
  60. var absoluteButton = new Button ("X = 30, Y = 1") {
  61. X = 30,
  62. Y = 1
  63. };
  64. Application.Top.Add (absoluteButton);
  65. // Demonstrate using Dim to create a window that fills the parent with a margin
  66. int margin = 10;
  67. var subWin = new Window () {
  68. X = Pos.Center (),
  69. Y = 2,
  70. Width = Dim.Fill (margin),
  71. Height = 7
  72. };
  73. subWin.Title = $"{subWin.GetType ().Name} {{X={subWin.X},Y={subWin.Y},Width={subWin.Width},Height={subWin.Height}}}";
  74. Application.Top.Add (subWin);
  75. int i = 1;
  76. string txt = "Resize the terminal to see computed layout in action.";
  77. var labelList = new List<Label> ();
  78. labelList.Add (new Label ($"The lines below show different TextAlignments"));
  79. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Left, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  80. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Right, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  81. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Centered, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  82. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Justified, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  83. subWin.Add (labelList.ToArray ());
  84. var frameView = new FrameView () {
  85. X = 2,
  86. Y = Pos.Bottom (subWin),
  87. Width = 30,
  88. Height = 7
  89. };
  90. frameView.Title = $"{frameView.GetType ().Name} {{X={frameView.X},Y={frameView.Y},Width={frameView.Width},Height={frameView.Height}}}";
  91. i = 1;
  92. labelList = new List<Label> ();
  93. labelList.Add (new Label ($"The lines below show different TextAlignments"));
  94. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Left, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  95. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Right, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  96. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Centered, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  97. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Justified, Width = Dim.Fill (), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  98. frameView.Add (labelList.ToArray ());
  99. Application.Top.Add (frameView);
  100. frameView = new FrameView () {
  101. X = Pos.Right (frameView),
  102. Y = Pos.Top (frameView),
  103. Width = Dim.Fill (),
  104. Height = 7,
  105. };
  106. frameView.Title = $"{frameView.GetType ().Name} {{X={frameView.X},Y={frameView.Y},Width={frameView.Width},Height={frameView.Height}}}";
  107. Application.Top.Add (frameView);
  108. // Demonstrate Dim & Pos using percentages - a TextField that is 30% height and 80% wide
  109. var textView = new TextView () {
  110. X = Pos.Center (),
  111. Y = Pos.Percent (50),
  112. Width = Dim.Percent (80),
  113. Height = Dim.Percent (10),
  114. ColorScheme = Colors.TopLevel,
  115. };
  116. textView.Text = $"This TextView should horizontally & vertically centered and \n10% of the screeen height, and 80% of its width.";
  117. Application.Top.Add (textView);
  118. var oddballButton = new Button ("These buttons demo convoluted PosCombine scenarios") {
  119. X = Pos.Center (),
  120. Y = Pos.Bottom (textView) + 1
  121. };
  122. Application.Top.Add (oddballButton);
  123. #region Issue2358
  124. // Demonstrate odd-ball Combine scenarios
  125. // Until https://github.com/gui-cs/Terminal.Gui/issues/2358 is fixed these won't work right
  126. oddballButton = new Button ("Center + 0") {
  127. X = Pos.Center () + 0,
  128. Y = Pos.Bottom (oddballButton)
  129. };
  130. Application.Top.Add (oddballButton);
  131. oddballButton = new Button ("Center + 1") {
  132. X = Pos.Center () + 1,
  133. Y = Pos.Bottom (oddballButton)
  134. };
  135. Application.Top.Add (oddballButton);
  136. oddballButton = new Button ("0 + Center") {
  137. X = 0 + Pos.Center (),
  138. Y = Pos.Bottom (oddballButton)
  139. };
  140. Application.Top.Add (oddballButton);
  141. oddballButton = new Button ("1 + Center") {
  142. X = 1 + Pos.Center (),
  143. Y = Pos.Bottom (oddballButton)
  144. };
  145. Application.Top.Add (oddballButton);
  146. // This demonstrates nonsense: it the same as using Pos.AnchorEnd (100/2=50 + 100/2=50 = 100 - 50)
  147. // The `- Pos.Percent(5)` is there so at least something is visible
  148. oddballButton = new Button ("Center + Center - Percent(50)") {
  149. X = Pos.Center () + Pos.Center () - Pos.Percent (50),
  150. Y = Pos.Bottom (oddballButton)
  151. };
  152. Application.Top.Add (oddballButton);
  153. // This demonstrates nonsense: it the same as using Pos.AnchorEnd (100/2=50 + 100/2=50 = 100 - 50)
  154. // The `- Pos.Percent(5)` is there so at least something is visible
  155. oddballButton = new Button ("Percent(50) + Center - Percent(50)") {
  156. X = Pos.Percent (50) + Pos.Center () - Pos.Percent (50),
  157. Y = Pos.Bottom (oddballButton)
  158. };
  159. Application.Top.Add (oddballButton);
  160. // This demonstrates nonsense: it the same as using Pos.AnchorEnd (100/2=50 + 100/2=50 = 100 - 50)
  161. // The `- Pos.Percent(5)` is there so at least something is visible
  162. oddballButton = new Button ("Center + Percent(50) - Percent(50)") {
  163. X = Pos.Center () + Pos.Percent (50) - Pos.Percent (50),
  164. Y = Pos.Bottom (oddballButton)
  165. };
  166. Application.Top.Add (oddballButton);
  167. #endregion
  168. // This demonstrates nonsense: Same as At(0)
  169. oddballButton = new Button ("Center - Center - Percent(50)") {
  170. X = Pos.Center () + Pos.Center () - Pos.Percent (50),
  171. Y = Pos.Bottom (oddballButton)
  172. };
  173. Application.Top.Add (oddballButton);
  174. // This demonstrates combining Percents)
  175. oddballButton = new Button ("Percent(40) + Percent(10)") {
  176. X = Pos.Percent (40) + Pos.Percent (10),
  177. Y = Pos.Bottom (oddballButton)
  178. };
  179. Application.Top.Add (oddballButton);
  180. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  181. var anchorButton = new Button ("Button using AnchorEnd") {
  182. Y = Pos.AnchorEnd () - 1,
  183. };
  184. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  185. anchorButton.Clicked += (s, e) => {
  186. // This demonstrates how to have a dynamically sized button
  187. // Each time the button is clicked the button's text gets longer
  188. // The call to Application.Top.LayoutSubviews causes the Computed layout to
  189. // get updated.
  190. anchorButton.Text += "!";
  191. Application.Top.LayoutSubviews ();
  192. };
  193. Application.Top.Add (anchorButton);
  194. // Demonstrate AnchorEnd(n)
  195. // This is intentionally convoluted to illustrate potential bugs.
  196. var anchorEndLabel1 = new Label ("This Label should be the 2nd to last line (AnchorEnd (2)).") {
  197. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  198. ColorScheme = Colors.Menu,
  199. Width = Dim.Fill (5),
  200. X = 5,
  201. Y = Pos.AnchorEnd (2)
  202. };
  203. Application.Top.Add (anchorEndLabel1);
  204. // Demonstrate DimCombine (via AnchorEnd(n) - 1)
  205. // This is intentionally convoluted to illustrate potential bugs.
  206. var anchorEndLabel2 = new TextField ("This TextField should be the 3rd to last line (AnchorEnd (2) - 1).") {
  207. TextAlignment = Terminal.Gui.TextAlignment.Left,
  208. ColorScheme = Colors.Menu,
  209. Width = Dim.Fill (5),
  210. X = 5,
  211. Y = Pos.AnchorEnd (2) - 1 // Pos.Combine
  212. };
  213. Application.Top.Add (anchorEndLabel2);
  214. // Show positioning vertically using Pos.AnchorEnd via Pos.Combine
  215. var leftButton = new Button ("Left") {
  216. Y = Pos.AnchorEnd () - 1 // Pos.Combine
  217. };
  218. leftButton.Clicked += (s, e) => {
  219. // This demonstrates how to have a dynamically sized button
  220. // Each time the button is clicked the button's text gets longer
  221. // The call to Application.Top.LayoutSubviews causes the Computed layout to
  222. // get updated.
  223. leftButton.Text += "!";
  224. Application.Top.LayoutSubviews ();
  225. };
  226. // show positioning vertically using Pos.AnchorEnd
  227. var centerButton = new Button ("Center") {
  228. X = Pos.Center (),
  229. Y = Pos.AnchorEnd (1) // Pos.AnchorEnd(1)
  230. };
  231. centerButton.Clicked += (s, e) => {
  232. // This demonstrates how to have a dynamically sized button
  233. // Each time the button is clicked the button's text gets longer
  234. // The call to Application.Top.LayoutSubviews causes the Computed layout to
  235. // get updated.
  236. centerButton.Text += "!";
  237. Application.Top.LayoutSubviews ();
  238. };
  239. // show positioning vertically using another window and Pos.Bottom
  240. var rightButton = new Button ("Right") {
  241. Y = Pos.Y (centerButton)
  242. };
  243. rightButton.Clicked += (s, e) => {
  244. // This demonstrates how to have a dynamically sized button
  245. // Each time the button is clicked the button's text gets longer
  246. // The call to Application.Top.LayoutSubviews causes the Computed layout to
  247. // get updated.
  248. rightButton.Text += "!";
  249. Application.Top.LayoutSubviews ();
  250. };
  251. // Center three buttons with 5 spaces between them
  252. leftButton.X = Pos.Left (centerButton) - (Pos.Right (leftButton) - Pos.Left (leftButton)) - 5;
  253. rightButton.X = Pos.Right (centerButton) + 5;
  254. Application.Top.Add (leftButton);
  255. Application.Top.Add (centerButton);
  256. Application.Top.Add (rightButton);
  257. }
  258. }
  259. }