ComputedLayout.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. //var verticalRuler = new Label ("") {
  35. // X = 0,
  36. // Y = 0,
  37. // Width = 1,
  38. // Height = Dim.Fill (),
  39. // ColorScheme = Colors.Error
  40. //};
  41. //Application.OnResized += () => {
  42. // verticalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height)];
  43. //};
  44. //Win.Add (verticalRuler);
  45. // Demonstrate At - Absolute Layout using Pos
  46. var absoluteButton = new Button ("Absolute At(2,1)") {
  47. X = Pos.At (2),
  48. Y = Pos.At (1)
  49. };
  50. Win.Add (absoluteButton);
  51. // Demonstrate using Dim to create a window that fills the parent with a margin
  52. int margin = 10;
  53. var subWin = new Window ($"Centered Sub Window with {margin} character margin") {
  54. X = Pos.Center(),
  55. Y = 2,
  56. Width = Dim.Fill (margin),
  57. Height = 7
  58. };
  59. Win.Add (subWin);
  60. int i = 1;
  61. string txt = "Resize the terminal to see computed layout in action.";
  62. var labelList = new List<Label> ();
  63. labelList.Add (new Label ($"The lines below show different TextAlignments"));
  64. 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 });
  65. 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 });
  66. 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 });
  67. 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 });
  68. subWin.Add (labelList.ToArray ());
  69. // Demonstrate Dim & Pos using percentages - a TextField that is 30% height and 80% wide
  70. var textView= new TextView () {
  71. X = Pos.Center (),
  72. Y = Pos.Percent (50),
  73. Width = Dim.Percent (80),
  74. Height = Dim.Percent (30),
  75. ColorScheme = Colors.TopLevel,
  76. };
  77. textView.Text = "This text view should be half-way down the terminal,\n20% of its height, and 80% of its width.";
  78. Win.Add (textView);
  79. // Demonstrate AnchorEnd - Button is anchored to bottom/right
  80. var anchorButton = new Button ("Anchor End") {
  81. Y = Pos.AnchorEnd () - 1,
  82. };
  83. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  84. anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  85. anchorButton.Clicked = () => {
  86. // Ths demonstrates how to have a dynamically sized button
  87. // Each time the button is clicked the button's text gets longer
  88. // The call to Win.LayoutSubviews causes the Computed layout to
  89. // get updated.
  90. anchorButton.Text += "!";
  91. Win.LayoutSubviews ();
  92. };
  93. Win.Add (anchorButton);
  94. // Centering multiple controls horizontally.
  95. // This is intentionally convoluted to illustrate potential bugs.
  96. var bottomLabel = new Label ("This should be the 2nd to last line (Bug #xxx).") {
  97. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  98. ColorScheme = Colors.Menu,
  99. Width = Dim.Fill (),
  100. X = Pos.Center (),
  101. Y = Pos.Bottom (Win) - 4 // BUGBUG: -2 should be two lines above border; but it has to be -4
  102. };
  103. Win.Add (bottomLabel);
  104. // Show positioning vertically using Pos.Bottom
  105. // BUGBUG: -1 should be just above border; but it has to be -3
  106. var leftButton = new Button ("Left") {
  107. Y = Pos.Bottom (Win) - 3
  108. };
  109. leftButton.Clicked = () => {
  110. // Ths demonstrates how to have a dynamically sized button
  111. // Each time the button is clicked the button's text gets longer
  112. // The call to Win.LayoutSubviews causes the Computed layout to
  113. // get updated.
  114. leftButton.Text += "!";
  115. Win.LayoutSubviews ();
  116. };
  117. // show positioning vertically using Pos.AnchorEnd
  118. var centerButton = new Button ("Center") {
  119. X = Pos.Center (),
  120. Y = Pos.AnchorEnd () - 1
  121. };
  122. centerButton.Clicked = () => {
  123. // Ths demonstrates how to have a dynamically sized button
  124. // Each time the button is clicked the button's text gets longer
  125. // The call to Win.LayoutSubviews causes the Computed layout to
  126. // get updated.
  127. centerButton.Text += "!";
  128. Win.LayoutSubviews ();
  129. };
  130. // show positioning vertically using another window and Pos.Bottom
  131. var rightButton = new Button ("Right") {
  132. Y = Pos.Y (centerButton)
  133. };
  134. rightButton.Clicked = () => {
  135. // Ths demonstrates how to have a dynamically sized button
  136. // Each time the button is clicked the button's text gets longer
  137. // The call to Win.LayoutSubviews causes the Computed layout to
  138. // get updated.
  139. rightButton.Text += "!";
  140. Win.LayoutSubviews ();
  141. };
  142. // Center three buttons with 5 spaces between them
  143. // TODO: Use Pos.Width instead of (Right-Left) when implemented (#502)
  144. leftButton.X = Pos.Left (centerButton) - (Pos.Right(leftButton) - Pos.Left (leftButton)) - 5;
  145. rightButton.X = Pos.Right (centerButton) + 5;
  146. Win.Add (leftButton);
  147. Win.Add (centerButton);
  148. Win.Add (rightButton);
  149. }
  150. public override void Run ()
  151. {
  152. base.Run ();
  153. }
  154. }
  155. internal static class StringExtensions {
  156. public static string Repeat (this string instr, int n)
  157. {
  158. if (n <= 0) {
  159. return null;
  160. }
  161. if (string.IsNullOrEmpty (instr) || n == 1) {
  162. return instr;
  163. }
  164. return new StringBuilder (instr.Length * n)
  165. .Insert (0, instr, n)
  166. .ToString ();
  167. }
  168. }
  169. }