SplitContainerNesting.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 (1,startHorizontal ?
  78. Terminal.Gui.Graphs.Orientation.Horizontal :
  79. Terminal.Gui.Graphs.Orientation.Vertical, false);
  80. root.Panel1.Add (CreateTextView (1));
  81. root.Panel2.Add (CreateTextView (2));
  82. root.IntegratedBorder = border ? BorderStyle.Rounded : BorderStyle.None;
  83. workArea.Add (root);
  84. if (numberOfPanels == 1) {
  85. root.Panel2.Visible = false;
  86. }
  87. if (numberOfPanels > 2) {
  88. panelsCreated = 2;
  89. panelsToCreate = numberOfPanels;
  90. AddMorePanels (root);
  91. }
  92. if (loaded) {
  93. workArea.LayoutSubviews ();
  94. }
  95. }
  96. private View CreateTextView (int number)
  97. {
  98. return new TextView {
  99. Width = Dim.Fill (),
  100. Height = Dim.Fill (),
  101. Text = number.ToString ().Repeat (1000),
  102. AllowsTab = false,
  103. //WordWrap = true, // TODO: This is very slow (like 10s to render with 45 panels)
  104. };
  105. }
  106. private void AddMorePanels (SplitContainer to)
  107. {
  108. if (panelsCreated == panelsToCreate) {
  109. return;
  110. }
  111. if (!(to.Panel1 is SplitContainer)) {
  112. SplitLeft (to);
  113. }
  114. if (!(to.Panel2 is SplitContainer)) {
  115. SplitRight (to);
  116. }
  117. if (to.Panel1 is SplitContainer && to.Panel2 is SplitContainer) {
  118. AddMorePanels ((SplitContainer)to.Panel1);
  119. AddMorePanels ((SplitContainer)to.Panel2);
  120. }
  121. }
  122. private void SplitLeft(SplitContainer to)
  123. {
  124. if (panelsCreated == panelsToCreate) {
  125. return;
  126. }
  127. // we can split Panel1
  128. var tv = (TextView)to.Panel1.Subviews.Single ();
  129. panelsCreated++;
  130. var newContainer = CreateSplitContainer (panelsCreated, to.Orientation, true);
  131. to.Remove (to.Panel1);
  132. to.Add (newContainer);
  133. to.Panel1 = newContainer;
  134. newContainer.Panel1.Add (tv);
  135. newContainer.Panel2.Add (CreateTextView (panelsCreated));
  136. }
  137. private void SplitRight(SplitContainer to)
  138. {
  139. if (panelsCreated == panelsToCreate) {
  140. return;
  141. }
  142. // we can split Panel2
  143. var tv = (TextView)to.Panel2.Subviews.Single ();
  144. panelsCreated++;
  145. var newContainer = CreateSplitContainer (panelsCreated, to.Orientation, true);
  146. to.Remove (to.Panel2);
  147. to.Add (newContainer);
  148. to.Panel2 = newContainer;
  149. newContainer.Panel2.Add (tv);
  150. newContainer.Panel1.Add (CreateTextView (panelsCreated));
  151. }
  152. private SplitContainer CreateSplitContainer (int titleNumber, Orientation orientation, bool flip)
  153. {
  154. var toReturn = new SplitContainer {
  155. Width = Dim.Fill (),
  156. Height = Dim.Fill (),
  157. // flip the orientation
  158. Orientation = orientation
  159. };
  160. if (flip) {
  161. toReturn.Orientation = toReturn.Orientation == Orientation.Vertical ?
  162. Orientation.Horizontal :
  163. Orientation.Vertical;
  164. }
  165. toReturn.Panel1Title = cbTitles.Checked ? $"Panel {titleNumber}" : string.Empty;
  166. toReturn.Panel2Title = cbTitles.Checked ? $"Panel {titleNumber+1}" : string.Empty;
  167. return toReturn;
  168. }
  169. private int GetNumberOfPanels ()
  170. {
  171. if (int.TryParse (textField.Text.ToString (), out var panels) && panels >= 0) {
  172. return panels;
  173. } else {
  174. return 0;
  175. }
  176. }
  177. private void Quit ()
  178. {
  179. Application.RequestStop ();
  180. }
  181. }
  182. }