SplitContainerNesting.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Threading;
  5. using Terminal.Gui;
  6. using Terminal.Gui.Graphs;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "Split Container Nesting", Description: "Nest SplitContainers")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("LineView")]
  11. public class SplitContainerNesting : Scenario {
  12. private View workArea;
  13. private TextField textField;
  14. private CheckBox cbHorizontal;
  15. private CheckBox cbBorder;
  16. private CheckBox cbTitles;
  17. bool loaded = false;
  18. int panelsCreated;
  19. int panelsToCreate;
  20. /// <summary>
  21. /// Setup the scenario.
  22. /// </summary>
  23. public override void Setup ()
  24. {
  25. // Scenario Windows.
  26. Win.Title = this.GetName ();
  27. Win.Y = 1;
  28. var lblPanels = new Label ("Number Of Panels:");
  29. textField = new TextField {
  30. X = Pos.Right (lblPanels),
  31. Width = 10,
  32. Text = "2",
  33. };
  34. textField.TextChanged += (s) => SetupSplitContainer ();
  35. cbHorizontal = new CheckBox ("Horizontal") {
  36. X = Pos.Right (textField) + 1
  37. };
  38. cbHorizontal.Toggled += (s) => SetupSplitContainer ();
  39. cbBorder = new CheckBox ("Border") {
  40. X = Pos.Right (cbHorizontal) + 1
  41. };
  42. cbBorder.Toggled += (s) => SetupSplitContainer ();
  43. cbTitles = new CheckBox ("Titles") {
  44. X = Pos.Right (cbBorder) + 1
  45. };
  46. cbTitles.Toggled += (s) => SetupSplitContainer ();
  47. workArea = new View {
  48. X = 0,
  49. Y = 1,
  50. Width = Dim.Fill (),
  51. Height = Dim.Fill (),
  52. };
  53. var menu = new MenuBar (new MenuBarItem [] {
  54. new MenuBarItem ("_File", new MenuItem [] {
  55. new MenuItem ("_Quit", "", () => Quit()),
  56. }) });
  57. Win.Add (lblPanels);
  58. Win.Add (textField);
  59. Win.Add (cbHorizontal);
  60. Win.Add (cbBorder);
  61. Win.Add (cbTitles);
  62. Win.Add (workArea);
  63. SetupSplitContainer ();
  64. Application.Top.Add (menu);
  65. Win.Loaded += () => loaded = true;
  66. }
  67. private void SetupSplitContainer ()
  68. {
  69. int numberOfPanels = GetNumberOfPanels ();
  70. bool titles = cbTitles.Checked;
  71. bool border = cbBorder.Checked;
  72. bool startHorizontal = cbHorizontal.Checked;
  73. workArea.RemoveAll ();
  74. if (numberOfPanels <= 0) {
  75. return;
  76. }
  77. var root = CreateSplitContainer (startHorizontal ?
  78. Terminal.Gui.Graphs.Orientation.Horizontal :
  79. Terminal.Gui.Graphs.Orientation.Vertical, false);
  80. root.Panel1.Add (CreateTextView (1));
  81. root.Panel1Title = titles ? "Panel 1" : string.Empty;
  82. root.Panel2.Add (CreateTextView (2));
  83. root.Panel2Title = titles ? "Panel 2" : string.Empty;
  84. root.IntegratedBorder = border ? BorderStyle.Rounded : BorderStyle.None;
  85. workArea.Add (root);
  86. if (numberOfPanels == 1) {
  87. root.Panel2.Visible = false;
  88. }
  89. if (numberOfPanels > 2) {
  90. panelsCreated = 2;
  91. panelsToCreate = numberOfPanels;
  92. AddMorePanels (root);
  93. }
  94. if (loaded) {
  95. workArea.LayoutSubviews ();
  96. }
  97. }
  98. private View CreateTextView (int number)
  99. {
  100. return new TextView {
  101. Width = Dim.Fill (),
  102. Height = Dim.Fill (),
  103. Text = number.ToString ().Repeat (1000),
  104. AllowsTab = false,
  105. //WordWrap = true, // TODO: This is very slow (like 10s to render with 45 panels)
  106. };
  107. }
  108. private void AddMorePanels (SplitContainer to)
  109. {
  110. bool canSplitLeft = !(to.Panel1 is SplitContainer);
  111. bool canSplitRight = !(to.Panel2 is SplitContainer);
  112. if(canSplitRight) {
  113. SplitRight (to);
  114. }
  115. if (canSplitLeft && panelsCreated < panelsToCreate) {
  116. SplitLeft(to);
  117. }
  118. if (to.Panel1 is SplitContainer && to.Panel2 is SplitContainer) {
  119. AddMorePanels ((SplitContainer)to.Panel1);
  120. AddMorePanels ((SplitContainer)to.Panel2);
  121. }
  122. }
  123. private void SplitLeft(SplitContainer to)
  124. {
  125. // we can split Panel1
  126. var tv = (TextView)to.Panel1.Subviews.Single ();
  127. var newContainer = CreateSplitContainer (to.Orientation, true);
  128. to.Remove (to.Panel1);
  129. to.Add (newContainer);
  130. to.Panel1 = newContainer;
  131. newContainer.Panel1.Add (tv);
  132. newContainer.Panel2.Add (CreateTextView (++panelsCreated));
  133. }
  134. private void SplitRight(SplitContainer to)
  135. {
  136. // we can split Panel2
  137. var tv = (TextView)to.Panel2.Subviews.Single ();
  138. var newContainer = CreateSplitContainer (to.Orientation, true);
  139. to.Remove (to.Panel2);
  140. to.Add (newContainer);
  141. to.Panel2 = newContainer;
  142. newContainer.Panel2.Add (tv);
  143. newContainer.Panel1.Add (CreateTextView (++panelsCreated));
  144. }
  145. private SplitContainer CreateSplitContainer (Orientation orientation, bool flip)
  146. {
  147. var toReturn = new SplitContainer {
  148. Width = Dim.Fill (),
  149. Height = Dim.Fill (),
  150. // flip the orientation
  151. Orientation = orientation
  152. };
  153. if (flip) {
  154. toReturn.Orientation = toReturn.Orientation == Orientation.Vertical ?
  155. Orientation.Horizontal :
  156. Orientation.Vertical;
  157. }
  158. return toReturn;
  159. }
  160. private int GetNumberOfPanels ()
  161. {
  162. if (int.TryParse (textField.Text.ToString (), out var panels) && panels >= 0) {
  163. return panels;
  164. } else {
  165. return 0;
  166. }
  167. }
  168. private void Quit ()
  169. {
  170. Application.RequestStop ();
  171. }
  172. }
  173. }