SplitContainer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public SplitContainer ()
  10. {
  11. // Default to a border of 1 so that View looks nice
  12. Border = new Border ();
  13. this.Add (splitterLine);
  14. this.Add (Panel1);
  15. this.Add (Panel2);
  16. SetOrientation (Orientation.Vertical, Pos.Percent (50));
  17. // TODO: Actually respect collapsed statuses
  18. }
  19. private void SetOrientation (Orientation orientation, Pos splitterDistance)
  20. {
  21. // TODO: Enforce minimum sizes
  22. splitterLine.Orientation = orientation;
  23. switch (orientation) {
  24. case Orientation.Horizontal:
  25. splitterLine.X = 0;
  26. splitterLine.Y = splitterDistance;
  27. splitterLine.Width = Dim.Fill ();
  28. splitterLine.Height = 1;
  29. splitterLine.LineRune = Driver.HLine;
  30. this.Panel1.X = 0;
  31. this.Panel1.Y = 0;
  32. this.Panel1.Width = Dim.Fill ();
  33. this.Panel1.Height = new DimFunc (() =>
  34. splitterDistance.Anchor (Bounds.Height) - 1);
  35. this.Panel2.Y = Pos.Bottom (splitterLine) + 1;
  36. this.Panel2.X = 0;
  37. this.Panel2.Width = Dim.Fill ();
  38. this.Panel2.Height = Dim.Fill ();
  39. break;
  40. case Orientation.Vertical:
  41. splitterLine.X = splitterDistance;
  42. splitterLine.Y = 0;
  43. splitterLine.Width = 1;
  44. splitterLine.Height = Dim.Fill ();
  45. splitterLine.LineRune = Driver.VLine;
  46. this.Panel1.X = 0;
  47. this.Panel1.Y = 0;
  48. this.Panel1.Height = Dim.Fill ();
  49. this.Panel1.Width = new DimFunc (() =>
  50. splitterDistance.Anchor (Bounds.Width) - 1);
  51. this.Panel2.X = Pos.Right (splitterLine) + 1;
  52. this.Panel2.Y = 0;
  53. this.Panel2.Width = Dim.Fill ();
  54. this.Panel2.Height = Dim.Fill ();
  55. break;
  56. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  57. };
  58. }
  59. /// <summary>
  60. /// The left or top panel of the <see cref="SplitContainer"/>
  61. /// (depending on <see cref="Orientation"/>). Add panel contents
  62. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>.
  63. /// </summary>
  64. public View Panel1 { get; } = new View ();
  65. public int Panel1MinSize { get; set; }
  66. /// <summary>
  67. /// This determines if <see cref="Panel1"/> is collapsed.
  68. /// </summary>
  69. public bool Panel1Collapsed {
  70. get { return panel1Collapsed; }
  71. set {
  72. panel1Collapsed = value;
  73. if (value && panel2Collapsed) {
  74. panel2Collapsed = false;
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// The right or bottom panel of the <see cref="SplitContainer"/>
  80. /// (depending on <see cref="Orientation"/>). Add panel contents
  81. /// to this <see cref="View"/> using <see cref="View.Add(View)"/>
  82. /// </summary>
  83. public View Panel2 { get; } = new View ();
  84. public int Panel2MinSize { get; set; }
  85. /// <summary>
  86. /// This determines if <see cref="Panel2"/> is collapsed.
  87. /// </summary>
  88. public bool Panel2Collapsed {
  89. get { return panel2Collapsed; }
  90. set {
  91. panel2Collapsed = value;
  92. if (value && panel1Collapsed) {
  93. panel1Collapsed = false;
  94. }
  95. }
  96. }
  97. public Orientation Orientation { get; set; }
  98. public Pos SplitterDistance { get; set; }
  99. }
  100. }