ComputedLayout.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog {
  7. /// <summary>
  8. /// This Scenario demonstrates how to use Termina.gui's Dim and Pos Layout System.
  9. /// [x] - Using Dim.Fill to fill a window
  10. /// [x] - Using Dim.Fill and Dim.Pos to automatically align controls based on an initial control
  11. /// [ ] - ...
  12. /// </summary>
  13. [ScenarioMetadata (Name: "Computed Layout", Description: "Demonstrates using the Computed (Dim and Pos) Layout System")]
  14. [ScenarioCategory ("Layout")]
  15. class ComputedLayout : Scenario {
  16. public override void Setup ()
  17. {
  18. //Top.LayoutStyle = LayoutStyle.Computed;
  19. // Demonstrate using Dim to create a horizontal ruler that always measures the parent window's width
  20. // BUGBUG: Dim.Fill returns too big a value sometimes.
  21. const string rule = "|123456789";
  22. var horizontalRuler = new Label ("") {
  23. X = 0,
  24. Y = 0,
  25. Width = Dim.Fill (1), // BUGBUG: I don't think this should be needed; DimFill() should respect container's frame. X does.
  26. ColorScheme = Colors.Error
  27. };
  28. Application.Resized += (sender, a) => {
  29. horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
  30. };
  31. Win.Add (horizontalRuler);
  32. // Demonstrate using Dim to create a vertical ruler that always measures the parent window's height
  33. // TODO: Either build a custom control for this or implement linewrap in Label #352
  34. const string vrule = "|\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
  35. var verticalRuler = new Label ("") {
  36. X = 0,
  37. Y = 0,
  38. Width = 1,
  39. Height = Dim.Fill (),
  40. ColorScheme = Colors.Error
  41. };
  42. Application.Resized += (sender, a) => {
  43. verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height*2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height*2)];
  44. };
  45. Win.Add (verticalRuler);
  46. // Demonstrate At - Absolute Layout using Pos
  47. var absoluteButton = new Button ("Absolute At(2,1)") {
  48. X = Pos.At (2),
  49. Y = Pos.At (1)
  50. };
  51. Win.Add (absoluteButton);
  52. // Demonstrate using Dim to create a window that fills the parent with a margin
  53. int margin = 10;
  54. var subWin = new Window ($"Centered Sub Window with {margin} character margin") {
  55. X = Pos.Center(),
  56. Y = 2,
  57. Width = Dim.Fill (margin),
  58. Height = 7
  59. };
  60. Win.Add (subWin);
  61. int i = 1;
  62. string txt = "Resize the terminal to see computed layout in action.";
  63. var labelList = new List<Label> ();
  64. labelList.Add (new Label ($"The lines below show different TextAlignments"));
  65. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Left, Width = Dim.Fill (1), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  66. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Right, Width = Dim.Fill (1), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  67. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Centered, Width = Dim.Fill (1), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  68. labelList.Add (new Label ($"{i++}-{txt}") { TextAlignment = Terminal.Gui.TextAlignment.Justified, Width = Dim.Fill (1), X = 0, Y = Pos.Bottom (labelList.LastOrDefault ()), ColorScheme = Colors.Dialog });
  69. subWin.Add (labelList.ToArray ());
  70. // Demonstrate Dim & Pos using percentages - a TextField that is 30% height and 80% wide
  71. var textView= new TextView () {
  72. X = Pos.Center (),
  73. Y = Pos.Percent (50),
  74. Width = Dim.Percent (80),
  75. Height = Dim.Percent (30),
  76. ColorScheme = Colors.TopLevel,
  77. };
  78. textView.Text = "This text view should be half-way down the terminal,\n20% of its height, and 80% of its width.";
  79. Win.Add (textView);
  80. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  81. var anchorButton = new Button ("Anchor End") {
  82. Y = Pos.AnchorEnd () - 1,
  83. };
  84. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  85. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  86. anchorButton.Clicked = () => {
  87. // Ths demonstrates how to have a dynamically sized button
  88. // Each time the button is clicked the button's text gets longer
  89. // The call to Win.LayoutSubviews causes the Computed layout to
  90. // get updated.
  91. anchorButton.Text += "!";
  92. Win.LayoutSubviews ();
  93. };
  94. Win.Add (anchorButton);
  95. // Centering multiple controls horizontally.
  96. // This is intentionally convoluted to illustrate potential bugs.
  97. var bottomLabel = new Label ("This should be the 2nd to last line (Bug #xxx).") {
  98. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  99. ColorScheme = Colors.Menu,
  100. Width = Dim.Fill (),
  101. X = Pos.Center (),
  102. Y = Pos.Bottom (Win) - 4 // BUGBUG: -2 should be two lines above border; but it has to be -4
  103. };
  104. Win.Add (bottomLabel);
  105. // Show positioning vertically using Pos.Bottom
  106. // BUGBUG: -1 should be just above border; but it has to be -3
  107. var leftButton = new Button ("Left") {
  108. Y = Pos.Bottom (Win) - 3
  109. };
  110. leftButton.Clicked = () => {
  111. // Ths demonstrates how to have a dynamically sized button
  112. // Each time the button is clicked the button's text gets longer
  113. // The call to Win.LayoutSubviews causes the Computed layout to
  114. // get updated.
  115. leftButton.Text += "!";
  116. Win.LayoutSubviews ();
  117. };
  118. // show positioning vertically using Pos.AnchorEnd
  119. var centerButton = new Button ("Center") {
  120. X = Pos.Center (),
  121. Y = Pos.AnchorEnd () - 1
  122. };
  123. centerButton.Clicked = () => {
  124. // Ths demonstrates how to have a dynamically sized button
  125. // Each time the button is clicked the button's text gets longer
  126. // The call to Win.LayoutSubviews causes the Computed layout to
  127. // get updated.
  128. centerButton.Text += "!";
  129. Win.LayoutSubviews ();
  130. };
  131. // show positioning vertically using another window and Pos.Bottom
  132. var rightButton = new Button ("Right") {
  133. Y = Pos.Y (centerButton)
  134. };
  135. rightButton.Clicked = () => {
  136. // Ths demonstrates how to have a dynamically sized button
  137. // Each time the button is clicked the button's text gets longer
  138. // The call to Win.LayoutSubviews causes the Computed layout to
  139. // get updated.
  140. rightButton.Text += "!";
  141. Win.LayoutSubviews ();
  142. };
  143. // Center three buttons with 5 spaces between them
  144. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  145. leftButton.X = Pos.Left (centerButton) - (Pos.Right(leftButton) - Pos.Left (leftButton)) - 5;
  146. rightButton.X = Pos.Right (centerButton) + 5;
  147. Win.Add (leftButton);
  148. Win.Add (centerButton);
  149. Win.Add (rightButton);
  150. }
  151. public override void Run ()
  152. {
  153. base.Run ();
  154. }
  155. }
  156. internal static class StringExtensions {
  157. public static string Repeat (this string instr, int n)
  158. {
  159. if (n <= 0) {
  160. return null;
  161. }
  162. if (string.IsNullOrEmpty (instr) || n == 1) {
  163. return instr;
  164. }
  165. return new StringBuilder (instr.Length * n)
  166. .Insert (0, instr, n)
  167. .ToString ();
  168. }
  169. }
  170. }