SplitContainerNesting.cs 5.3 KB

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