SplitContainer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. switch (Orientation) {
  26. case Orientation.Horizontal:
  27. splitterLine.X = 0;
  28. splitterLine.Y = splitterDistance;
  29. splitterLine.Width = Dim.Fill ();
  30. splitterLine.Height = 1;
  31. splitterLine.LineRune = Driver.HLine;
  32. this.Panel1.X = 0;
  33. this.Panel1.Y = 0;
  34. this.Panel1.Width = Dim.Fill ();
  35. this.Panel1.Height = new DimFunc (() =>
  36. splitterDistance.Anchor (Bounds.Height) - 1);
  37. this.Panel2.Y = Pos.Bottom (splitterLine) + 1;
  38. this.Panel2.X = 0;
  39. this.Panel2.Width = Dim.Fill ();
  40. this.Panel2.Height = Dim.Fill ();
  41. break;
  42. case Orientation.Vertical:
  43. splitterLine.X = splitterDistance;
  44. splitterLine.Y = 0;
  45. splitterLine.Width = 1;
  46. splitterLine.Height = Dim.Fill ();
  47. splitterLine.LineRune = Driver.VLine;
  48. this.Panel1.X = 0;
  49. this.Panel1.Y = 0;
  50. this.Panel1.Height = Dim.Fill ();
  51. this.Panel1.Width = new DimFunc (() =>
  52. splitterDistance.Anchor (Bounds.Width) - 1);
  53. this.Panel2.X = Pos.Right (splitterLine) + 1;
  54. this.Panel2.Y = 0;
  55. this.Panel2.Width = Dim.Fill ();
  56. this.Panel2.Height = Dim.Fill ();
  57. break;
  58. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  59. };
  60. }
  61. /// <summary>
  62. /// The left or top panel of the <see cref="SplitContainer"/>
  63. /// (depending on <see cref="Orientation"/>). Add panel contents
  64. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>.
  65. /// </summary>
  66. public View Panel1 { get; } = new View ();
  67. /// <summary>
  68. /// TODO: not implemented yet
  69. /// </summary>
  70. public int Panel1MinSize { get; set; }
  71. /// <summary>
  72. /// This determines if <see cref="Panel1"/> is collapsed.
  73. /// </summary>
  74. public bool Panel1Collapsed {
  75. get { return panel1Collapsed; }
  76. set {
  77. panel1Collapsed = value;
  78. if (value && panel2Collapsed) {
  79. panel2Collapsed = false;
  80. }
  81. Setup ();
  82. }
  83. }
  84. /// <summary>
  85. /// The right or bottom panel of the <see cref="SplitContainer"/>
  86. /// (depending on <see cref="Orientation"/>). Add panel contents
  87. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>
  88. /// </summary>
  89. public View Panel2 { get; } = new View ();
  90. /// <summary>
  91. /// TODO: not implemented yet
  92. /// </summary>
  93. public int Panel2MinSize { get; set; }
  94. /// <summary>
  95. /// This determines if <see cref="Panel2"/> is collapsed.
  96. /// </summary>
  97. public bool Panel2Collapsed {
  98. get { return panel2Collapsed; }
  99. set {
  100. panel2Collapsed = value;
  101. if (value && panel1Collapsed) {
  102. panel1Collapsed = false;
  103. }
  104. Setup ();
  105. }
  106. }
  107. public Orientation Orientation {
  108. get { return orientation; }
  109. set {
  110. orientation = value;
  111. Setup ();
  112. }
  113. }
  114. public Pos SplitterDistance {
  115. get { return splitterDistance; }
  116. set {
  117. splitterDistance = value;
  118. Setup ();
  119. }
  120. }
  121. }
  122. }