SplitViewNesting.cs 5.4 KB

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