SplitContainer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using Terminal.Gui.Graphs;
  3. using static Terminal.Gui.Dim;
  4. namespace Terminal.Gui {
  5. public class SplitContainer : View {
  6. private LineView splitterLine = new LineView ();
  7. private bool panel1Collapsed;
  8. private bool panel2Collapsed;
  9. private Pos splitterDistance = Pos.Percent (50);
  10. private Orientation orientation = Orientation.Vertical;
  11. public SplitContainer ()
  12. {
  13. // Default to a border of 1 so that View looks nice
  14. Border = new Border ();
  15. this.Add (splitterLine);
  16. this.Add (Panel1);
  17. this.Add (Panel2);
  18. Setup ();
  19. // TODO: Actually respect collapsed statuses
  20. }
  21. private void Setup ()
  22. {
  23. // TODO: Enforce minimum sizes
  24. splitterLine.Orientation = Orientation;
  25. if(panel1Collapsed || panel2Collapsed) {
  26. SetupForCollapsedPanel ();
  27. }
  28. else {
  29. SetupForNormal ();
  30. }
  31. }
  32. private void SetupForNormal ()
  33. {
  34. // Ensure all our component views are here
  35. // (e.g. if we are transitioning from a collapsed state)
  36. if (!this.Subviews.Contains (splitterLine)) {
  37. this.Add (splitterLine);
  38. }
  39. if (!this.Subviews.Contains (Panel1)) {
  40. this.Add (Panel1);
  41. }
  42. if (!this.Subviews.Contains (Panel2)) {
  43. this.Add (Panel2);
  44. }
  45. switch (Orientation) {
  46. case Orientation.Horizontal:
  47. splitterLine.X = 0;
  48. splitterLine.Y = splitterDistance;
  49. splitterLine.Width = Dim.Fill ();
  50. splitterLine.Height = 1;
  51. splitterLine.LineRune = Driver.HLine;
  52. this.Panel1.X = 0;
  53. this.Panel1.Y = 0;
  54. this.Panel1.Width = Dim.Fill ();
  55. this.Panel1.Height = new DimFunc (() =>
  56. splitterDistance.Anchor (Bounds.Height) - 1);
  57. this.Panel2.Y = Pos.Bottom (splitterLine) + 1;
  58. this.Panel2.X = 0;
  59. this.Panel2.Width = Dim.Fill ();
  60. this.Panel2.Height = Dim.Fill ();
  61. break;
  62. case Orientation.Vertical:
  63. splitterLine.X = splitterDistance;
  64. splitterLine.Y = 0;
  65. splitterLine.Width = 1;
  66. splitterLine.Height = Dim.Fill ();
  67. splitterLine.LineRune = Driver.VLine;
  68. this.Panel1.X = 0;
  69. this.Panel1.Y = 0;
  70. this.Panel1.Height = Dim.Fill ();
  71. this.Panel1.Width = new DimFunc (() =>
  72. splitterDistance.Anchor (Bounds.Width) - 1);
  73. this.Panel2.X = Pos.Right (splitterLine) + 1;
  74. this.Panel2.Y = 0;
  75. this.Panel2.Width = Dim.Fill ();
  76. this.Panel2.Height = Dim.Fill ();
  77. break;
  78. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  79. };
  80. }
  81. private void SetupForCollapsedPanel ()
  82. {
  83. View toRemove = panel1Collapsed ? Panel1 : Panel2;
  84. View toFullSize = panel1Collapsed ? Panel2 : Panel1;
  85. if (this.Subviews.Contains (splitterLine)) {
  86. this.Subviews.Remove (splitterLine);
  87. }
  88. if (this.Subviews.Contains(toRemove)) {
  89. this.Subviews.Remove (toRemove);
  90. }
  91. if(!this.Subviews.Contains(toFullSize)) {
  92. this.Add (toFullSize);
  93. }
  94. toFullSize.X = 0;
  95. toFullSize.Y = 0;
  96. toFullSize.Width = Dim.Fill ();
  97. toFullSize.Height = Dim.Fill ();
  98. }
  99. /// <summary>
  100. /// The left or top panel of the <see cref="SplitContainer"/>
  101. /// (depending on <see cref="Orientation"/>). Add panel contents
  102. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>.
  103. /// </summary>
  104. public View Panel1 { get; } = new View ();
  105. /// <summary>
  106. /// TODO: not implemented yet
  107. /// </summary>
  108. public int Panel1MinSize { get; set; }
  109. /// <summary>
  110. /// This determines if <see cref="Panel1"/> is collapsed.
  111. /// </summary>
  112. public bool Panel1Collapsed {
  113. get { return panel1Collapsed; }
  114. set {
  115. panel1Collapsed = value;
  116. if (value && panel2Collapsed) {
  117. panel2Collapsed = false;
  118. }
  119. Setup ();
  120. }
  121. }
  122. /// <summary>
  123. /// The right or bottom panel of the <see cref="SplitContainer"/>
  124. /// (depending on <see cref="Orientation"/>). Add panel contents
  125. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>
  126. /// </summary>
  127. public View Panel2 { get; } = new View ();
  128. /// <summary>
  129. /// TODO: not implemented yet
  130. /// </summary>
  131. public int Panel2MinSize { get; set; }
  132. /// <summary>
  133. /// This determines if <see cref="Panel2"/> is collapsed.
  134. /// </summary>
  135. public bool Panel2Collapsed {
  136. get { return panel2Collapsed; }
  137. set {
  138. panel2Collapsed = value;
  139. if (value && panel1Collapsed) {
  140. panel1Collapsed = false;
  141. }
  142. Setup ();
  143. }
  144. }
  145. public Orientation Orientation {
  146. get { return orientation; }
  147. set {
  148. orientation = value;
  149. Setup ();
  150. }
  151. }
  152. public Pos SplitterDistance {
  153. get { return splitterDistance; }
  154. set {
  155. splitterDistance = value;
  156. Setup ();
  157. }
  158. }
  159. }
  160. }