TileViewNesting.cs 5.6 KB

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