ComputedLayout.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.OnResized += () => {
  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 using Dim to create a window that fills the parent with a margin
  46. int margin = 10;
  47. var subWin = new Window ($"Centered Sub Window with {margin} character margin") {
  48. X = Pos.Center(),
  49. Y = 2,
  50. Width = Dim.Fill (margin),
  51. Height = 7
  52. };
  53. Win.Add (subWin);
  54. int i = 1;
  55. string txt = "Resize the terminal to see computed layout in action.";
  56. var labelList = new List<Label> ();
  57. labelList.Add (new Label ($"The lines below show different TextAlignments"));
  58. 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 });
  59. 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 });
  60. 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 });
  61. 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 });
  62. subWin.Add (labelList.ToArray ());
  63. // Demonstrate Dim & Pos using percentages - a TextField that is 20% height and 80% wide
  64. var textView= new TextView () {
  65. X = Pos.Center (),
  66. Y = Pos.Percent (50),
  67. Width = Dim.Percent (80),
  68. Height = Dim.Percent (20),
  69. ColorScheme = Colors.TopLevel,
  70. };
  71. textView.Text = "This text view should be half-way down the terminal,\n20% of its height, and 80% of its width.";
  72. Win.Add (textView);
  73. //// Demonstrate AnchorEnd - Button anchored to bottom of textView
  74. //var clearButton = new Button ("Clear") {
  75. // X = Pos.AnchorEnd (),
  76. // Y = Pos.AnchorEnd (),
  77. // Width = 15,
  78. // Height = 1
  79. //};
  80. //Win.Add (clearButton);
  81. // Demonstrate At - Absolute Layout using Pos
  82. var absoluteButton = new Button ("At(10,10)") {
  83. X = Pos.At(10),
  84. Y = Pos.At(10)
  85. };
  86. Win.Add (absoluteButton);
  87. }
  88. public override void Run ()
  89. {
  90. base.Run ();
  91. }
  92. }
  93. public static class StringExtensions {
  94. public static string Repeat (this string instr, int n)
  95. {
  96. if (n <= 0) {
  97. return null;
  98. }
  99. if (string.IsNullOrEmpty (instr) || n == 1) {
  100. return instr;
  101. }
  102. return new StringBuilder (instr.Length * n)
  103. .Insert (0, instr, n)
  104. .ToString ();
  105. }
  106. }
  107. }