TileViewNesting.cs 5.7 KB

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