View.ScrollBars.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public partial class View
  4. {
  5. private Lazy<ScrollBar> _horizontalScrollBar;
  6. private Lazy<ScrollBar> _verticalScrollBar;
  7. /// <summary>
  8. /// Initializes the ScrollBars of the View. Called by the constructor.
  9. /// </summary>
  10. private void SetupScrollBars ()
  11. {
  12. _horizontalScrollBar = new (
  13. () =>
  14. {
  15. var scrollBar = new ScrollBar
  16. {
  17. Orientation = Orientation.Horizontal,
  18. X = 0,
  19. Y = Pos.AnchorEnd (),
  20. Width = Dim.Fill (
  21. Dim.Func (
  22. () =>
  23. {
  24. if (_verticalScrollBar.IsValueCreated)
  25. {
  26. return _verticalScrollBar.Value.Visible ? 1 : 0;
  27. }
  28. return 0;
  29. })),
  30. Size = GetContentSize ().Width,
  31. Visible = false
  32. };
  33. Padding?.Add (scrollBar);
  34. scrollBar.Initialized += (_, _) =>
  35. {
  36. Padding!.Thickness = Padding.Thickness with
  37. {
  38. Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : 0
  39. };
  40. scrollBar.PositionChanged += (_, args) =>
  41. {
  42. Viewport = Viewport with { X = args.CurrentValue };
  43. };
  44. scrollBar.VisibleChanged += (_, _) =>
  45. {
  46. Padding.Thickness = Padding.Thickness with
  47. {
  48. Bottom = scrollBar.Visible
  49. ? Padding.Thickness.Bottom + 1
  50. : Padding.Thickness.Bottom - 1
  51. };
  52. };
  53. };
  54. return scrollBar;
  55. });
  56. _verticalScrollBar = new (
  57. () =>
  58. {
  59. var scrollBar = new ScrollBar
  60. {
  61. Orientation = Orientation.Vertical,
  62. X = Pos.AnchorEnd (),
  63. Y = Pos.Func (() => Padding.Thickness.Top),
  64. Height = Dim.Fill (
  65. Dim.Func (
  66. () =>
  67. {
  68. if (_horizontalScrollBar.IsValueCreated)
  69. {
  70. return _horizontalScrollBar.Value.Visible ? 1 : 0;
  71. }
  72. return 0;
  73. })),
  74. Size = GetContentSize ().Height,
  75. Visible = false
  76. };
  77. Padding?.Add (scrollBar);
  78. scrollBar.Initialized += (_, _) =>
  79. {
  80. if (Padding is { })
  81. {
  82. Padding.Thickness = Padding.Thickness with
  83. {
  84. Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0
  85. };
  86. scrollBar.PositionChanged += (_, args) =>
  87. {
  88. Viewport = Viewport with { Y = args.CurrentValue };
  89. };
  90. scrollBar.VisibleChanged += (_, _) =>
  91. {
  92. Padding.Thickness = Padding.Thickness with
  93. {
  94. Right = scrollBar.Visible
  95. ? Padding.Thickness.Right + 1
  96. : Padding.Thickness.Right - 1
  97. };
  98. };
  99. }
  100. };
  101. return scrollBar;
  102. });
  103. ViewportChanged += (_, _) =>
  104. {
  105. if (_verticalScrollBar.IsValueCreated)
  106. {
  107. _verticalScrollBar.Value.Position = Viewport.Y;
  108. }
  109. if (_horizontalScrollBar.IsValueCreated)
  110. {
  111. _horizontalScrollBar.Value.Position = Viewport.X;
  112. }
  113. };
  114. ContentSizeChanged += (_, _) =>
  115. {
  116. if (_verticalScrollBar.IsValueCreated)
  117. {
  118. _verticalScrollBar.Value.Size = GetContentSize ().Height;
  119. }
  120. if (_horizontalScrollBar.IsValueCreated)
  121. {
  122. _horizontalScrollBar.Value.Size = GetContentSize ().Width;
  123. }
  124. };
  125. }
  126. /// <summary>
  127. /// </summary>
  128. public ScrollBar HorizontalScrollBar => _horizontalScrollBar.Value;
  129. /// <summary>
  130. /// </summary>
  131. public ScrollBar VerticalScrollBar => _verticalScrollBar.Value;
  132. /// <summary>
  133. /// Clean up the ScrollBars of the View. Called by View.Dispose.
  134. /// </summary>
  135. private void DisposeScrollBars ()
  136. {
  137. if (_horizontalScrollBar.IsValueCreated)
  138. {
  139. Padding?.Remove (_horizontalScrollBar.Value);
  140. _horizontalScrollBar.Value.Dispose ();
  141. }
  142. if (_verticalScrollBar.IsValueCreated)
  143. {
  144. Padding?.Remove (_verticalScrollBar.Value);
  145. _verticalScrollBar.Value.Dispose ();
  146. }
  147. }
  148. }