TileViewNesting.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using Terminal.Gui;
  3. using Terminal.Gui.Graphs;
  4. using System.Linq;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Tile View Nesting", Description: "Demonstrates recursive nesting of TileViews")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("LineView")]
  9. public class TileViewNesting : 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) => SetupTileView ();
  34. cbHorizontal = new CheckBox ("Horizontal") {
  35. X = Pos.Right (textField) + 1
  36. };
  37. cbHorizontal.Toggled += (s) => SetupTileView ();
  38. cbBorder = new CheckBox ("Border") {
  39. X = Pos.Right (cbHorizontal) + 1
  40. };
  41. cbBorder.Toggled += (s) => SetupTileView ();
  42. cbTitles = new CheckBox ("Titles") {
  43. X = Pos.Right (cbBorder) + 1
  44. };
  45. cbTitles.Toggled += (s) => SetupTileView ();
  46. cbUseLabels = new CheckBox ("Use Labels") {
  47. X = Pos.Right (cbTitles) + 1
  48. };
  49. cbUseLabels.Toggled += (s) => SetupTileView ();
  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. SetupTileView ();
  68. Application.Top.Add (menu);
  69. Win.Loaded += () => loaded = true;
  70. }
  71. private void SetupTileView ()
  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 = CreateTileView (1,startHorizontal ?
  82. Terminal.Gui.Graphs.Orientation.Horizontal :
  83. Terminal.Gui.Graphs.Orientation.Vertical);
  84. root.Tiles.ElementAt(0).ContentView.Add (CreateContentControl (1));
  85. root.Tiles.ElementAt(0).Title = cbTitles.Checked ? $"View 1" : string.Empty;
  86. root.Tiles.ElementAt (1).ContentView.Add (CreateContentControl (2));
  87. root.Tiles.ElementAt(1).Title = cbTitles.Checked ? $"View 2" : string.Empty;
  88. root.Border.BorderStyle = border ? BorderStyle.Rounded : BorderStyle.None;
  89. workArea.Add (root);
  90. if (numberOfViews == 1) {
  91. root.Tiles.ElementAt (1).ContentView.Visible = false;
  92. }
  93. if (numberOfViews > 2) {
  94. viewsCreated = 2;
  95. viewsToCreate = numberOfViews;
  96. AddMoreViews (root);
  97. }
  98. if (loaded) {
  99. workArea.LayoutSubviews ();
  100. }
  101. }
  102. private View CreateContentControl (int number)
  103. {
  104. return cbUseLabels.Checked ?
  105. CreateLabelView (number) :
  106. CreateTextView (number);
  107. }
  108. private View CreateLabelView (int number)
  109. {
  110. return new Label {
  111. Width = Dim.Fill (),
  112. Height = 1,
  113. AutoSize = false,
  114. Text = number.ToString ().Repeat (1000),
  115. CanFocus = true,
  116. };
  117. }
  118. private View CreateTextView (int number)
  119. {
  120. return new TextView {
  121. Width = Dim.Fill (),
  122. Height = Dim.Fill(),
  123. Text = number.ToString ().Repeat (1000),
  124. AllowsTab = false,
  125. //WordWrap = true, // TODO: This is very slow (like 10s to render with 45 views)
  126. };
  127. }
  128. private void AddMoreViews (TileView to)
  129. {
  130. if (viewsCreated == viewsToCreate) {
  131. return;
  132. }
  133. if (!(to.Tiles.ElementAt(0).ContentView is TileView)) {
  134. Split(to,true);
  135. }
  136. if (!(to.Tiles.ElementAt (1).ContentView is TileView)) {
  137. Split(to,false);
  138. }
  139. if (to.Tiles.ElementAt (0).ContentView is TileView && to.Tiles.ElementAt (1).ContentView is TileView) {
  140. AddMoreViews ((TileView)to.Tiles.ElementAt (0).ContentView);
  141. AddMoreViews ((TileView)to.Tiles.ElementAt (1).ContentView);
  142. }
  143. }
  144. private void Split(TileView to, bool left)
  145. {
  146. if (viewsCreated == viewsToCreate) {
  147. return;
  148. }
  149. TileView newView;
  150. if (left) {
  151. to.TrySplitTile(0,2,out newView);
  152. }
  153. else {
  154. to.TrySplitTile (1,2,out newView);
  155. }
  156. viewsCreated++;
  157. // During splitting the old Title will have been migrated to View1 so we only need
  158. // to set the Title on View2 (the one that gets our new TextView)
  159. newView.Tiles.ElementAt(1).Title = cbTitles.Checked ? $"View {viewsCreated}" : string.Empty;
  160. // Flip orientation
  161. newView.Orientation = to.Orientation == Orientation.Vertical ?
  162. Orientation.Horizontal :
  163. Orientation.Vertical;
  164. newView.Tiles.ElementAt (1).ContentView.Add (CreateContentControl(viewsCreated));
  165. }
  166. private TileView CreateTileView (int titleNumber, Orientation orientation)
  167. {
  168. var toReturn = new TileView {
  169. Width = Dim.Fill (),
  170. Height = Dim.Fill (),
  171. // flip the orientation
  172. Orientation = orientation
  173. };
  174. toReturn.Tiles.ElementAt(0).Title = cbTitles.Checked ? $"View {titleNumber}" : string.Empty;
  175. toReturn.Tiles.ElementAt (1).Title = cbTitles.Checked ? $"View {titleNumber + 1}" : string.Empty;
  176. return toReturn;
  177. }
  178. private int GetNumberOfViews ()
  179. {
  180. if (int.TryParse (textField.Text.ToString (), out var views) && views >= 0) {
  181. return views;
  182. } else {
  183. return 0;
  184. }
  185. }
  186. private void Quit ()
  187. {
  188. Application.RequestStop ();
  189. }
  190. }
  191. }